Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* fpont 12/99 */
- /* pont.net */
- /* udpClient.c */
- /** sudo gcc -lpthread udpClient.c -o cliente*/
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <sys/time.h> /* select() */
- #include <stdio.h> //printf
- #include <stdlib.h>
- #include <arpa/inet.h>
- #include <linux/if_packet.h>
- #include <string.h> //strncpy
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <net/if.h> //ifreq
- #include <unistd.h> //close
- #include <netinet/ether.h>
- #include <sys/time.h> //gettimeofday
- #include <pthread.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <sys/ioctl.h>
- #include <linux/if_packet.h>
- #include <time.h>
- #include <math.h>
- #define LOCAL_SERVER_PORT 1500
- #define REMOTE_SERVER_PORT 1500
- #define MAX_MSG 100
- #define TAMANHO_BUF 256
- #define DESTINO_MAC0 0x01
- #define DESTINO_MAC1 0x0c
- #define DESTINO_MAC2 0xcd
- #define DESTINO_MAC3 0x04
- #define DESTINO_MAC4 0x00
- #define DESTINO_MAC5 0x01
- //#define DEFAULT_IF "localhost"
- //#define DEFAULT_IF_PRP "localhost"
- #define DEFAULT_IF "h1-eth0"
- #define DEFAULT_IF_PRP "h1-eth1"
- #define DESTINO "10.0.0.6"
- int criaPacoteCompleto(char *B, uint16_t AppID, char *gocbRef, char *datSet,
- char *goID, const char * interface);
- int criaPacote(char *buffer, const char * interface);
- void *enviaPacote(void *args);
- void *inicia_servidor(void *args);
- void printCurrentTime();
- void pausar (float);
- typedef struct { char *mensagem; int tamanho; const char * interface; } argumentos;
- argumentos *v;
- argumentos *v_prp;
- clock_t enviado;
- clock_t recebido;
- int main(int argc, char *argv[]) {
- //pthread_t *server = (pthread_t*) malloc (sizeof(pthread_t));
- //pthread_create(server, NULL, inicia_servidor, NULL);
- struct timeval tempo1, tempo2;
- //printf("\n# ##### Programa Envia Pkt SV ##### #\n");
- int qtd_pacotes = 10;
- int tipo_seguranca = 0;
- if (argc > 1)
- sscanf(argv[1], "%d", &qtd_pacotes);
- //printf("Quantidade a ser enviada: %d\n", qtd_pacotes);
- char buffer[TAMANHO_BUF]; //vetor onde sera montado o pacote
- int t_buffer = 0; //tamanho do pacote
- int t_buffer_prp = 0; //tamanho do pacote
- int totalThreads = qtd_pacotes* 2;
- pthread_t *threads;
- threads = malloc(totalThreads * sizeof(pthread_t));
- //pthread_t *t1;
- //pthread_t *t2;
- for (int i = 0; i < (qtd_pacotes*2); i=i+2) {
- //t1 = (pthread_t*) malloc (sizeof(pthread_t));
- //t2 = (pthread_t*) malloc (sizeof(pthread_t));
- gettimeofday(&tempo1, NULL);
- t_buffer = criaPacote(buffer, DEFAULT_IF);
- t_buffer_prp = criaPacote(buffer, DEFAULT_IF_PRP);
- gettimeofday(&tempo2, NULL);
- //printf("[PKT %d]Tempo de GERAÇÃO = %ld microssegundos\n", i+1,
- //(tempo2.tv_sec - tempo1.tv_sec) * 1000000
- //+ (tempo2.tv_usec - tempo1.tv_usec));
- gettimeofday(&tempo1, NULL);
- v = (argumentos*) malloc (sizeof(argumentos));
- v->mensagem = buffer;
- v->tamanho = t_buffer;
- v->interface = DEFAULT_IF;
- v_prp = (argumentos*) malloc (sizeof(argumentos));
- v_prp->mensagem = buffer;
- v_prp->tamanho = t_buffer_prp;
- v_prp->interface = DEFAULT_IF_PRP;
- pthread_create(&threads [i], NULL, enviaPacote, (void *)v);
- pthread_join(threads [i],NULL);
- pthread_create(&threads [i+1], NULL, enviaPacote, (void *)v_prp);
- pthread_join(threads [i+1],NULL);
- gettimeofday(&tempo2, NULL);
- //printf("[PKT %d]Tempo de ENVIO = %ld microssegundos\n\n", i + 1,
- //(tempo2.tv_sec - tempo1.tv_sec) * 1000000
- //+ (tempo2.tv_usec - tempo1.tv_usec));
- //i++;
- free(v);
- free(v_prp);
- //free (t1);
- //free (t2);
- //usleep(5000);
- }
- printf("Mensagem enviada com sucesso !");
- printf("\n\n");
- free(threads);
- return 0;
- }
- int criaPacote(char *buffer, const char * interface) {
- uint16_t AppID = 65535;
- unsigned char *gocbRef = "teste_iec61850_luana";
- unsigned char *datSet = "Device900/SV";
- unsigned char *svID = "XJPA_MU0001";
- return criaPacoteCompleto(buffer, (uint16_t) AppID, gocbRef, datSet, svID, interface);
- }
- int criaPacoteCompleto(char *B, uint16_t AppID, char *gocbRef, char *datSet,
- char *svID, const char * interface) {
- int fd;
- int tx_len = 48;
- struct ifreq origem;
- struct timeval agora;
- uint8_t* dstAddr;
- uint8_t priority;
- uint16_t vlanId;
- uint16_t appId;
- fd = socket(AF_INET, SOCK_DGRAM, 0);
- origem.ifr_addr.sa_family = AF_INET;
- //strncpy(origem.ifr_name, DEFAULT_IF, IFNAMSIZ-1);
- strcpy(origem.ifr_name, interface);
- ioctl(fd, SIOCGIFHWADDR, &origem);
- close(fd);
- unsigned char *mac = (unsigned char *) origem.ifr_hwaddr.sa_data;
- //printf("MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
- B[0] = DESTINO_MAC0;
- B[1] = DESTINO_MAC1;
- B[2] = DESTINO_MAC2;
- B[3] = DESTINO_MAC3;
- B[4] = DESTINO_MAC4;
- B[5] = DESTINO_MAC5;
- for (int i = 6; i < 12; i++)
- B[i] = mac[i - 6]; //MAC de origem, da placa de rede
- tx_len += 12;
- //ate aqui inseriu os 2 macs
- /* Priority tag - IEEE 802.1Q */
- B[tx_len++] = 0x81;
- B[tx_len++] = 0x00;
- uint8_t tci1 = priority << 5;
- tci1 += vlanId / 256;
- uint8_t tci2 = vlanId % 256;
- B[tx_len++] = tci1;
- B[tx_len++] = tci2;
- //ethertype sv
- B[tx_len++] = 0x88;
- B[tx_len++] = 0xBa;
- int TOTAL_LENGTH_PACKET = 0;
- int TOTAL_LENGTH_APDU = 0;
- B[tx_len++] = 0x40; //SV APPID
- B[tx_len++] = 0x00; //SV APPID
- int local_goose_length = tx_len;
- B[tx_len++] = 0x00;
- B[tx_len++] = 0x00;
- TOTAL_LENGTH_PACKET += 4; //# APPID (2 bytes) + Length (2 bytes)
- /* Reserved1 */
- B[tx_len++] = 0x80;
- B[tx_len++] = 0x00;
- /* Reserved2 */
- B[tx_len++] = 0x00;
- B[tx_len++] = 0x00;
- TOTAL_LENGTH_PACKET += 4; //# Reserved1 (2 bytes) + Reserved2 (2 bytes)
- //aqui termina o cabecalho sv
- //aqui comeca o sv APDU
- B[tx_len++] = 0x60;
- int local_goosePDU_length = tx_len;
- B[tx_len++] = (uint8_t) 0;
- TOTAL_LENGTH_PACKET += 2; //# SavPDU (2 bytes)
- //noASDU
- B[tx_len++] = 0x80;
- B[tx_len++] = (uint8_t) 1;
- B[tx_len++] = 0x01;
- B[local_goosePDU_length] += 3;
- TOTAL_LENGTH_APDU += 3;
- //Sequence of ASDU
- B[tx_len++] = 0xA2;
- int tam = tx_len;
- B[tx_len++] = (uint8_t) 0;
- B[local_goosePDU_length] += 2;
- TOTAL_LENGTH_APDU += 2;
- B[tx_len++] = 0x30;
- int tam2 = tx_len;
- B[tx_len++] = (uint8_t) 0;
- B[local_goosePDU_length] += 2;
- TOTAL_LENGTH_APDU += 2;
- //svID
- B[tx_len++] = 0x80;
- B[tx_len++] = (uint8_t) strlen(svID);
- for (int i = 0; i < strlen(svID); i++)
- B[tx_len++] = svID[i];
- B[local_goosePDU_length] += 2 + strlen(svID);
- TOTAL_LENGTH_APDU += 2 + strlen(svID);
- //smpCnt
- B[tx_len++] = 0x82;
- B[tx_len++] = 0x02;
- B[tx_len++] = (uint8_t) 0x00;
- B[tx_len++] = (uint8_t) 0x01;
- B[local_goosePDU_length] += 4;
- TOTAL_LENGTH_APDU += 4;
- //confRev
- B[tx_len++] = 0x83;
- B[tx_len++] = (uint8_t) 4;
- B[tx_len++] = (uint8_t) 0x01;
- B[tx_len++] = (uint8_t) 0x01;
- B[tx_len++] = (uint8_t) 0x01;
- B[tx_len++] = (uint8_t) 0x01;
- B[local_goosePDU_length] += 6;
- TOTAL_LENGTH_APDU += 6;
- //smpSynch
- B[tx_len++] = 0x85;
- B[tx_len++] = (uint8_t) 1;
- B[tx_len++] = '0' - '0';
- B[local_goosePDU_length] += 3;
- TOTAL_LENGTH_APDU += 3;
- //Sequence of Data
- B[tx_len++] = 0x87;
- B[tx_len++] = ((uint8_t) strlen(datSet));
- for (int i = 0; i < strlen(datSet); i++)
- B[tx_len++] = datSet[i];
- B[local_goosePDU_length] += 2 + strlen(datSet);
- TOTAL_LENGTH_APDU += 2 + strlen(datSet);
- //printf("COMPARACAO ENTRE TAMANHOS...%d (%d)...\n", TOTAL_LENGTH_PACKET + TOTAL_LENGTH_APDU, TOTAL_LENGTH_APDU);
- int tamanho_final = 10 + B[local_goosePDU_length]; //atualiza size SV cabecalho
- int tam_final = -5 + B[local_goosePDU_length];
- B[tam] = tam_final;
- int tam2_final = -7 + B[local_goosePDU_length];
- B[tam2] = tam2_final;
- B[local_goose_length] = tamanho_final >> 8; //SV LENGTH
- B[local_goose_length + 1] = tamanho_final ; //SV LENGTH
- return tx_len;
- }
- void *enviaPacote(void *args){
- //vc passa numa struct com os argumentos e depois faz um typecasting interno para pegar os argumentos
- argumentos *argus = (argumentos *) args;
- //melhorar no nome da variável acima
- int sd, rc, i;
- char ifName[IFNAMSIZ];
- struct sockaddr_in cliAddr, remoteServAddr;
- struct hostent *h;
- /* check command line args
- if(argc<3) {
- printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
- exit(1);
- }*/
- /* get server IP address (no check if input is IP address or DNS name */
- h = gethostbyname(DESTINO);
- if(h==NULL) {
- printf("%s: unknown host '%s' \n", argus[0], argus[1]);
- exit(1);
- }
- //printf ("%s\n", argus-> mensagem);
- //printf ("%s\n", argus [1]);
- //printf("%s: sending data to '%s' (IP : %s) \n", argus[0], h->h_name,
- // inet_ntoa(*(struct in_addr *)h->h_addr_list[0])!=NULL?"ok":"nok");
- remoteServAddr.sin_family = h->h_addrtype;
- memcpy((char *) &remoteServAddr.sin_addr.s_addr,
- h->h_addr_list[0], h->h_length);
- remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
- /* socket creation */
- sd = socket(AF_INET,SOCK_DGRAM,0);
- //printf ("(%d)\n", sd);
- if(sd<0) {
- //printf("%s: cannot open socket \n",argus[0]);
- exit(1);
- }
- /* bind any port */
- cliAddr.sin_family = AF_INET;
- //cliAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- cliAddr.sin_port = htons(0);
- strcpy(ifName, argus->interface);
- setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, ifName, IFNAMSIZ-1);
- rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
- if(rc<0) {
- //printf("%s: cannot bind port\n", argus[0]);
- exit(1);
- }
- //printf ("enviado: %lu\n", clock ());
- printCurrentTime();
- rc = sendto(sd, argus->mensagem, argus->tamanho+1, 0,
- (struct sockaddr *) &remoteServAddr,
- sizeof(remoteServAddr));
- printCurrentTime();
- if(rc<0) {
- //printf("%s: cannot send data %d \n",argus[0],i-1);
- close(sd);
- exit(1);
- }
- close(sd);
- }
- void *inicia_servidor (void *argv) {
- int sd, rc, n, cliLen;
- struct sockaddr_in cliAddr, servAddr;
- char msg[MAX_MSG];
- /* socket creation */
- sd=socket(AF_INET, SOCK_DGRAM, 0);
- if(sd<0) {
- printf("%s: cannot open socket \n", "");
- exit(1);
- }
- /* bind local server port */
- servAddr.sin_family = AF_INET;
- //servAddr.sin_addr.s_addr = inet_addr(DESTINO);
- servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servAddr.sin_port = htons(LOCAL_SERVER_PORT);
- rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
- if(rc<0) {
- printf("%s: cannot bind port number %d \n",
- "", LOCAL_SERVER_PORT);
- exit(1);
- }
- printf("%s: waiting for data on port UDP %u\n",
- "",LOCAL_SERVER_PORT);
- /* server infinite loop */
- int i = 1;
- while(1) {
- /* init buffer */
- memset(msg,0x0,MAX_MSG);
- /* receive message */
- cliLen = sizeof(cliAddr);
- n = recvfrom(sd, msg, MAX_MSG, 0,
- (struct sockaddr *) &cliAddr, &cliLen);
- //printf ("recebido: %lu\n", clock ());
- //recebido = clock ();
- //printf ("%lf\n", (double)(recebido - enviado)/(CLOCKS_PER_SEC/1000.));
- if(n<0) {
- printf("%s: cannot receive data \n","");
- continue;
- }
- /* print received message */
- //printf("%s: from %s:UDP%u : %s \n",
- // "",inet_ntoa(cliAddr.sin_addr),
- //ntohs(cliAddr.sin_port),msg);
- //printf ("(%d)\n", i++);
- }/* end of server infinite loop */
- return 0;
- }
- void printCurrentTime() {
- char buffer[26];
- int millisec;
- struct tm* tm_info;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- millisec = lrint(tv.tv_usec/1000.0); // Round to nearest millisec
- if (millisec>=1000) { // Allow for rounding up to nearest second
- millisec -=1000;
- tv.tv_sec++;
- }
- tm_info = localtime(&tv.tv_sec);
- strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", tm_info);
- printf("%s.%03d\n", buffer, millisec);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement