Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.93 KB | None | 0 0
  1. /* fpont 12/99 */
  2. /* pont.net    */
  3. /* udpClient.c */
  4. /** sudo gcc -lpthread udpClient.c -o cliente*/
  5.  
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <sys/time.h> /* select() */
  11. #include <stdio.h>          //printf
  12. #include <stdlib.h>
  13. #include <arpa/inet.h>
  14. #include <linux/if_packet.h>
  15. #include <string.h>         //strncpy
  16. #include <sys/ioctl.h>
  17. #include <sys/socket.h>
  18. #include <net/if.h>         //ifreq
  19. #include <unistd.h>         //close
  20. #include <netinet/ether.h>
  21. #include <sys/time.h>       //gettimeofday
  22. #include <pthread.h>
  23. #include <string.h>
  24. #include <arpa/inet.h>
  25. #include <netinet/in.h>
  26. #include <sys/ioctl.h>
  27. #include <linux/if_packet.h>
  28. #include <time.h>
  29. #include <math.h>
  30.  
  31. #define LOCAL_SERVER_PORT 1500
  32.  
  33. #define REMOTE_SERVER_PORT 1500
  34. #define MAX_MSG 100
  35. #define TAMANHO_BUF     256
  36.  
  37. #define DESTINO_MAC0    0x01
  38. #define DESTINO_MAC1    0x0c
  39. #define DESTINO_MAC2    0xcd
  40. #define DESTINO_MAC3    0x04
  41. #define DESTINO_MAC4    0x00
  42. #define DESTINO_MAC5    0x01
  43.  
  44. //#define DEFAULT_IF      "localhost"
  45. //#define DEFAULT_IF_PRP  "localhost"
  46.  
  47. #define DEFAULT_IF      "h1-eth0"
  48. #define DEFAULT_IF_PRP  "h1-eth1"
  49. #define DESTINO         "10.0.0.6"
  50.  
  51. int criaPacoteCompleto(char *B, uint16_t AppID, char *gocbRef, char *datSet,
  52.         char *goID, const char * interface);
  53. int criaPacote(char *buffer, const char * interface);
  54. void *enviaPacote(void *args);
  55. void *inicia_servidor(void *args);
  56. void printCurrentTime();
  57.  
  58. void pausar (float);
  59.  
  60. typedef struct { char *mensagem; int tamanho; const char * interface; } argumentos;
  61. argumentos *v;
  62. argumentos *v_prp;
  63.  
  64. clock_t enviado;
  65. clock_t recebido;
  66.  
  67. int main(int argc, char *argv[]) {
  68.  
  69.   //pthread_t *server = (pthread_t*) malloc (sizeof(pthread_t));
  70.     //pthread_create(server, NULL, inicia_servidor, NULL);
  71.  
  72.   struct timeval tempo1, tempo2;
  73.     //printf("\n#  ##### Programa Envia Pkt SV #####  #\n");
  74.     int qtd_pacotes = 10;
  75.     int tipo_seguranca = 0;
  76.     if (argc > 1)
  77.         sscanf(argv[1], "%d", &qtd_pacotes);
  78.     //printf("Quantidade a ser enviada: %d\n", qtd_pacotes);
  79.  
  80.     char buffer[TAMANHO_BUF]; //vetor onde sera montado o pacote
  81.     int t_buffer = 0; //tamanho do pacote
  82.     int t_buffer_prp = 0; //tamanho do pacote
  83.  
  84.     int totalThreads = qtd_pacotes* 2;
  85.     pthread_t *threads;
  86.     threads = malloc(totalThreads * sizeof(pthread_t));
  87.    
  88.     //pthread_t *t1;
  89.     //pthread_t *t2;
  90.  
  91.     for (int i = 0; i < (qtd_pacotes*2); i=i+2) {
  92.  
  93.         //t1 = (pthread_t*) malloc (sizeof(pthread_t));
  94.         //t2 = (pthread_t*) malloc (sizeof(pthread_t));
  95.  
  96.         gettimeofday(&tempo1, NULL);
  97.         t_buffer = criaPacote(buffer, DEFAULT_IF);
  98.         t_buffer_prp = criaPacote(buffer, DEFAULT_IF_PRP);
  99.         gettimeofday(&tempo2, NULL);
  100.         //printf("[PKT %d]Tempo de GERAÇÃO   = %ld microssegundos\n", i+1,
  101.                 //(tempo2.tv_sec - tempo1.tv_sec) * 1000000
  102.                         //+ (tempo2.tv_usec - tempo1.tv_usec));
  103.  
  104.         gettimeofday(&tempo1, NULL);
  105.  
  106.         v = (argumentos*) malloc (sizeof(argumentos));
  107.         v->mensagem =   buffer;  
  108.         v->tamanho =    t_buffer;
  109.         v->interface =  DEFAULT_IF;
  110.  
  111.         v_prp = (argumentos*) malloc (sizeof(argumentos));
  112.         v_prp->mensagem =   buffer;  
  113.         v_prp->tamanho =    t_buffer_prp;
  114.         v_prp->interface =  DEFAULT_IF_PRP;
  115.  
  116.        
  117.         pthread_create(&threads [i], NULL, enviaPacote, (void *)v);
  118.         pthread_join(threads [i],NULL);
  119.  
  120.         pthread_create(&threads [i+1], NULL, enviaPacote, (void *)v_prp);
  121.         pthread_join(threads [i+1],NULL);
  122.  
  123.         gettimeofday(&tempo2, NULL);
  124.         //printf("[PKT %d]Tempo de ENVIO     = %ld microssegundos\n\n", i + 1,
  125.                 //(tempo2.tv_sec - tempo1.tv_sec) * 1000000
  126.                         //+ (tempo2.tv_usec - tempo1.tv_usec));
  127.         //i++;
  128.  
  129.         free(v);
  130.         free(v_prp);
  131.        
  132.         //free (t1);
  133.         //free (t2);
  134.  
  135.         //usleep(5000);
  136.     }
  137.  
  138.  
  139.     printf("Mensagem enviada com sucesso !");
  140.     printf("\n\n");
  141.    
  142.  
  143.     free(threads);
  144.  
  145.  
  146. return 0;
  147.  
  148. }
  149.  
  150. int criaPacote(char *buffer, const char * interface) {
  151.     uint16_t AppID = 65535;
  152.     unsigned char *gocbRef = "teste_iec61850_luana";
  153.     unsigned char *datSet = "Device900/SV";
  154.     unsigned char *svID = "XJPA_MU0001";
  155.  
  156.     return criaPacoteCompleto(buffer, (uint16_t) AppID, gocbRef, datSet, svID, interface);
  157. }
  158.  
  159. int criaPacoteCompleto(char *B, uint16_t AppID, char *gocbRef, char *datSet,
  160.                         char *svID, const char * interface) {
  161.     int fd;
  162.     int tx_len = 48;
  163.     struct ifreq origem;
  164.     struct timeval agora;
  165.  
  166.     uint8_t* dstAddr;
  167.     uint8_t priority;
  168.     uint16_t vlanId;
  169.     uint16_t appId;
  170.  
  171.     fd = socket(AF_INET, SOCK_DGRAM, 0);
  172.     origem.ifr_addr.sa_family = AF_INET;
  173.     //strncpy(origem.ifr_name, DEFAULT_IF, IFNAMSIZ-1);
  174.     strcpy(origem.ifr_name, interface);
  175.     ioctl(fd, SIOCGIFHWADDR, &origem);
  176.     close(fd);
  177.     unsigned char *mac = (unsigned char *) origem.ifr_hwaddr.sa_data;
  178.     //printf("MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
  179.     B[0] = DESTINO_MAC0;
  180.     B[1] = DESTINO_MAC1;
  181.     B[2] = DESTINO_MAC2;
  182.     B[3] = DESTINO_MAC3;
  183.     B[4] = DESTINO_MAC4;
  184.     B[5] = DESTINO_MAC5;
  185.     for (int i = 6; i < 12; i++)
  186.         B[i] = mac[i - 6]; //MAC de origem, da placa de rede
  187.     tx_len += 12;
  188.     //ate aqui inseriu os 2 macs
  189.  
  190.     /* Priority tag - IEEE 802.1Q */
  191.     B[tx_len++] = 0x81;
  192.     B[tx_len++] = 0x00;
  193.  
  194.     uint8_t tci1 = priority << 5;
  195.     tci1 += vlanId / 256;
  196.     uint8_t tci2 = vlanId % 256;
  197.  
  198.     B[tx_len++] = tci1;
  199.     B[tx_len++] = tci2;
  200.  
  201.     //ethertype sv
  202.     B[tx_len++] = 0x88;
  203.     B[tx_len++] = 0xBa;
  204.  
  205.     int TOTAL_LENGTH_PACKET = 0;
  206.     int TOTAL_LENGTH_APDU = 0;
  207.  
  208.     B[tx_len++] = 0x40; //SV APPID
  209.     B[tx_len++] = 0x00; //SV APPID
  210.     int local_goose_length = tx_len;
  211.     B[tx_len++] = 0x00;
  212.     B[tx_len++] = 0x00;
  213.  
  214.     TOTAL_LENGTH_PACKET += 4;   //# APPID (2 bytes) + Length (2 bytes)
  215.  
  216.     /* Reserved1 */
  217.     B[tx_len++] = 0x80;
  218.     B[tx_len++] = 0x00;
  219.  
  220.     /* Reserved2 */
  221.     B[tx_len++] = 0x00;
  222.     B[tx_len++] = 0x00;
  223.  
  224.     TOTAL_LENGTH_PACKET += 4;   //# Reserved1 (2 bytes) + Reserved2 (2 bytes)
  225.  
  226.     //aqui termina o cabecalho sv
  227.  
  228.     //aqui comeca o sv APDU
  229.     B[tx_len++] = 0x60;
  230.     int local_goosePDU_length = tx_len;
  231.     B[tx_len++] = (uint8_t) 0;
  232.  
  233.     TOTAL_LENGTH_PACKET += 2;       //# SavPDU (2 bytes)
  234.  
  235.  
  236.     //noASDU
  237.     B[tx_len++] = 0x80;
  238.     B[tx_len++] = (uint8_t) 1;
  239.     B[tx_len++] = 0x01;
  240.     B[local_goosePDU_length] += 3;
  241.  
  242.     TOTAL_LENGTH_APDU += 3;
  243.  
  244.     //Sequence of ASDU
  245.     B[tx_len++] = 0xA2;
  246.     int tam = tx_len;
  247.     B[tx_len++] = (uint8_t) 0;
  248.     B[local_goosePDU_length] += 2;
  249.  
  250.     TOTAL_LENGTH_APDU += 2;
  251.  
  252.  
  253.     B[tx_len++] = 0x30;
  254.     int tam2 = tx_len;
  255.     B[tx_len++] = (uint8_t) 0;
  256.     B[local_goosePDU_length] += 2;
  257.  
  258.     TOTAL_LENGTH_APDU += 2;
  259.  
  260.     //svID
  261.     B[tx_len++] = 0x80;
  262.     B[tx_len++] = (uint8_t) strlen(svID);
  263.     for (int i = 0; i < strlen(svID); i++)
  264.         B[tx_len++] = svID[i];
  265.     B[local_goosePDU_length] += 2 + strlen(svID);
  266.  
  267.     TOTAL_LENGTH_APDU += 2 + strlen(svID);
  268.  
  269.     //smpCnt
  270.     B[tx_len++] = 0x82;
  271.     B[tx_len++] = 0x02;
  272.     B[tx_len++] = (uint8_t) 0x00;
  273.     B[tx_len++] = (uint8_t) 0x01;
  274.     B[local_goosePDU_length] += 4;
  275.  
  276.     TOTAL_LENGTH_APDU += 4;
  277.  
  278.     //confRev
  279.     B[tx_len++] = 0x83;
  280.     B[tx_len++] = (uint8_t) 4;
  281.     B[tx_len++] = (uint8_t) 0x01;
  282.     B[tx_len++] = (uint8_t) 0x01;
  283.     B[tx_len++] = (uint8_t) 0x01;
  284.     B[tx_len++] = (uint8_t) 0x01;
  285.     B[local_goosePDU_length] += 6;
  286.  
  287.     TOTAL_LENGTH_APDU += 6;
  288.  
  289.     //smpSynch
  290.     B[tx_len++] = 0x85;
  291.     B[tx_len++] = (uint8_t) 1;
  292.     B[tx_len++] = '0' - '0';
  293.     B[local_goosePDU_length] += 3;
  294.  
  295.     TOTAL_LENGTH_APDU += 3;
  296.  
  297.     //Sequence of Data
  298.     B[tx_len++] = 0x87;
  299.     B[tx_len++] = ((uint8_t) strlen(datSet));
  300.     for (int i = 0; i < strlen(datSet); i++)
  301.         B[tx_len++] = datSet[i];
  302.     B[local_goosePDU_length] += 2 + strlen(datSet);
  303.  
  304.     TOTAL_LENGTH_APDU += 2 + strlen(datSet);
  305.  
  306.         //printf("COMPARACAO ENTRE TAMANHOS...%d (%d)...\n", TOTAL_LENGTH_PACKET + TOTAL_LENGTH_APDU, TOTAL_LENGTH_APDU);
  307.  
  308.     int tamanho_final = 10 + B[local_goosePDU_length]; //atualiza size SV cabecalho
  309.     int tam_final = -5 + B[local_goosePDU_length];
  310.     B[tam] = tam_final;
  311.     int tam2_final = -7 + B[local_goosePDU_length];
  312.     B[tam2] = tam2_final;
  313.  
  314.     B[local_goose_length] = tamanho_final >> 8;      //SV LENGTH
  315.     B[local_goose_length + 1] = tamanho_final ;       //SV LENGTH
  316.  
  317.  
  318.     return tx_len;
  319.  
  320. }
  321.  
  322.  
  323. void *enviaPacote(void *args){
  324.  
  325. //vc passa numa struct com os argumentos e depois faz um typecasting interno para pegar os argumentos
  326.     argumentos *argus = (argumentos *) args;
  327. //melhorar no nome da variável acima
  328.    
  329.   int sd, rc, i;
  330.   char ifName[IFNAMSIZ];
  331.   struct sockaddr_in cliAddr, remoteServAddr;
  332.   struct hostent *h;
  333.  
  334.   /* check command line args
  335.   if(argc<3) {
  336.     printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
  337.     exit(1);
  338.   }*/
  339.  
  340.   /* get server IP address (no check if input is IP address or DNS name */
  341.   h = gethostbyname(DESTINO);
  342.   if(h==NULL) {
  343.     printf("%s: unknown host '%s' \n", argus[0], argus[1]);
  344.     exit(1);
  345.   }
  346.  
  347. //printf ("%s\n", argus-> mensagem);
  348. //printf ("%s\n", argus [1]);
  349.  
  350.   //printf("%s: sending data to '%s' (IP : %s) \n", argus[0], h->h_name,
  351.     // inet_ntoa(*(struct in_addr *)h->h_addr_list[0])!=NULL?"ok":"nok");
  352.  
  353.   remoteServAddr.sin_family = h->h_addrtype;
  354.   memcpy((char *) &remoteServAddr.sin_addr.s_addr,
  355.      h->h_addr_list[0], h->h_length);
  356.   remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
  357.  
  358.   /* socket creation */
  359.   sd = socket(AF_INET,SOCK_DGRAM,0);
  360.   //printf ("(%d)\n", sd);
  361.   if(sd<0) {
  362.     //printf("%s: cannot open socket \n",argus[0]);
  363.     exit(1);
  364.   }
  365.  
  366.   /* bind any port */
  367.   cliAddr.sin_family = AF_INET;
  368.   //cliAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  369.   cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  370.   cliAddr.sin_port = htons(0);
  371.  
  372.   strcpy(ifName, argus->interface);
  373.   setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, ifName, IFNAMSIZ-1);
  374.  
  375.   rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
  376.   if(rc<0) {
  377.     //printf("%s: cannot bind port\n", argus[0]);
  378.     exit(1);
  379.   }
  380.  
  381.   //printf ("enviado: %lu\n", clock ());
  382.   printCurrentTime();
  383.   rc = sendto(sd, argus->mensagem, argus->tamanho+1, 0,
  384.        (struct sockaddr *) &remoteServAddr,
  385.          sizeof(remoteServAddr));
  386.   printCurrentTime();      
  387.  if(rc<0) {
  388.     //printf("%s: cannot send data %d \n",argus[0],i-1);
  389.     close(sd);
  390.     exit(1);
  391.  }
  392.  
  393.     close(sd);
  394.        
  395. }
  396.  
  397.  
  398. void *inicia_servidor (void *argv) {
  399.  
  400.   int sd, rc, n, cliLen;
  401.   struct sockaddr_in cliAddr, servAddr;
  402.   char msg[MAX_MSG];
  403.  
  404.   /* socket creation */
  405.   sd=socket(AF_INET, SOCK_DGRAM, 0);
  406.   if(sd<0) {
  407.     printf("%s: cannot open socket \n", "");
  408.     exit(1);
  409.   }
  410.  
  411.   /* bind local server port */
  412.   servAddr.sin_family = AF_INET;
  413.   //servAddr.sin_addr.s_addr = inet_addr(DESTINO);
  414.   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  415.   servAddr.sin_port = htons(LOCAL_SERVER_PORT);
  416.   rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
  417.  
  418.   if(rc<0) {
  419.     printf("%s: cannot bind port number %d \n",
  420.        "", LOCAL_SERVER_PORT);
  421.     exit(1);
  422.   }
  423.  
  424.   printf("%s: waiting for data on port UDP %u\n",
  425.        "",LOCAL_SERVER_PORT);
  426.  
  427.   /* server infinite loop */
  428.   int i = 1;
  429.   while(1) {
  430.    
  431.     /* init buffer */
  432.     memset(msg,0x0,MAX_MSG);
  433.  
  434.  
  435.     /* receive message */
  436.     cliLen = sizeof(cliAddr);
  437.     n = recvfrom(sd, msg, MAX_MSG, 0,
  438.          (struct sockaddr *) &cliAddr, &cliLen);
  439.  
  440.     //printf ("recebido: %lu\n", clock ());
  441.     //recebido = clock ();
  442.     //printf ("%lf\n", (double)(recebido - enviado)/(CLOCKS_PER_SEC/1000.));
  443.  
  444.     if(n<0) {
  445.       printf("%s: cannot receive data \n","");
  446.       continue;
  447.     }
  448.      
  449.     /* print received message */
  450.     //printf("%s: from %s:UDP%u : %s \n",
  451.       // "",inet_ntoa(cliAddr.sin_addr),
  452.        //ntohs(cliAddr.sin_port),msg);
  453.    
  454.     //printf ("(%d)\n", i++);
  455.   }/* end of server infinite loop */
  456.  
  457. return 0;
  458.  
  459. }
  460.  
  461. void printCurrentTime() {
  462.   char buffer[26];
  463.   int millisec;
  464.   struct tm* tm_info;
  465.   struct timeval tv;
  466.  
  467.   gettimeofday(&tv, NULL);
  468.  
  469.   millisec = lrint(tv.tv_usec/1000.0); // Round to nearest millisec
  470.   if (millisec>=1000) { // Allow for rounding up to nearest second
  471.     millisec -=1000;
  472.     tv.tv_sec++;
  473.   }
  474.  
  475.   tm_info = localtime(&tv.tv_sec);
  476.  
  477.   strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", tm_info);
  478.   printf("%s.%03d\n", buffer, millisec);
  479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement