• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

faspplugin/faspsimul.cxx (r4864/r4189)

Go to the documentation of this file.
00001 #include <arpa/inet.h>
00002 #include <linux/if_packet.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <unistd.h>
00006 #include <errno.h>
00007 #include <math.h>
00008 #include <stdlib.h>
00009 #include <sys/ioctl.h>
00010 #include <sys/socket.h>
00011 #include <net/if.h>
00012 #include <netinet/ether.h>
00013 
00014 #define BUF_SIZ 1024
00015 
00016 uint8_t dest_mac[6] = { 0xb8, 0xac, 0x6f, 0x92, 0x2b, 0x34 };
00017 
00018 const int FaspBlockSize = 44;
00019 const int FaspSyncPos = 34;
00020 
00021 
00022 double Gauss_Rnd(double mean, double sigma)
00023 {
00024    double x, y, z;
00025    z = 1.* rand() / RAND_MAX;
00026    y = 1.* rand() / RAND_MAX;
00027    x = z * 6.28318530717958623;
00028    return mean + sigma*sin(x)*sqrt(-2*log(y));
00029 }
00030 
00031 int main(int argc, char *argv[])
00032 {
00033    int sockfd;
00034    struct ifreq if_idx;
00035    struct ifreq if_mac;
00036    int tx_len = 0;
00037    bool debug = false;
00038    char sendbuf[BUF_SIZ], recvbuf[BUF_SIZ];
00039 
00040    struct sockaddr_ll socket_address;
00041    char ifName[IFNAMSIZ];
00042 
00043     if (argc<3) {
00044        printf("FASP simulatror. usage: faspsimul ifname mac\n");
00045        printf("example: faspsimul eth0 BC:30:5B:E8:9B:86\n");
00046        return 1;
00047     }
00048 
00049    // Get interface name
00050    strcpy(ifName, argv[1]);
00051 
00052    if ((argc > 2) && (strlen(argv[2])>=17)) {
00053       unsigned ddd[6];
00054       sscanf(argv[2], "%x:%x:%x:%x:%x:%x", ddd, ddd+1, ddd+2, ddd+3, ddd+4,ddd+5);
00055       for (int n=0;n<6;n++) dest_mac[n] = ddd[n];
00056    } else {
00057       printf("destination MAC not specified - fail\n");
00058       return 1;
00059    }
00060 
00061    // Open RAW socket to send on
00062    if ((sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL) /*IPPROTO_RAW*/)) == -1) {
00063       printf("Failure - cannot open raw socket\n");
00064       return 1;
00065    }
00066 
00067    // Get the index of the interface to send on
00068    memset(&if_idx, 0, sizeof(struct ifreq));
00069    strncpy(if_idx.ifr_name, ifName, IFNAMSIZ-1);
00070    if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0) {
00071       printf("SIOCGIFINDEX - cannot het if index\n");
00072       return 1;
00073    }
00074    // Get the MAC address of the interface to send on
00075    memset(&if_mac, 0, sizeof(struct ifreq));
00076    strncpy(if_mac.ifr_name, ifName, IFNAMSIZ-1);
00077    if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0) {
00078       printf("SIOCGIFHWADDR - cannot get own MAC\n");
00079       return 1;
00080    }
00081 
00082    /* Index of the network device */
00083    socket_address.sll_family = PF_PACKET;
00084    socket_address.sll_protocol = htons(ETH_P_IP);
00085    socket_address.sll_ifindex = if_idx.ifr_ifindex;
00086    /* Address length*/
00087    socket_address.sll_halen = ETH_ALEN;
00088    /* Destination MAC */
00089    socket_address.sll_hatype = ARPHRD_ETHER;
00090    socket_address.sll_pkttype = PACKET_OTHERHOST;
00091    memcpy(socket_address.sll_addr, dest_mac, 6);
00092 
00093    printf("Start sniffing on iface: %s for packets from MAC: %s\n", argv[1], argv[2]);
00094   
00095    unsigned sync_tm(0);
00096    unsigned adc_tm(0);
00097 
00098    int cnt(100000000), cnt1(0);
00099    
00100    unsigned sync_num(1);
00101    int sync_repeat(3);
00102    bool first_sync_in_loop = true;
00103 
00104    while (cnt--> 0) {
00105 
00106       socklen_t addr_len = sizeof(struct sockaddr_ll);
00107 
00108 //      size_t res = recvfrom(sockfd, recvbuf, BUF_SIZ, MSG_DONTWAIT,
00109 //            (struct sockaddr*)&socket_address, &addr_len);
00110 
00111       size_t res = recvfrom(sockfd, recvbuf, BUF_SIZ, 0, 0, 0);
00112 
00113       if (res == -1) {
00114          if (errno != EAGAIN)
00115             printf("error %d  EAGAIN %d\n", errno, EAGAIN);
00116          else
00117             usleep(1000);
00118       } else
00119       if (res>0) {
00120 
00121          struct ether_header *eh2 = (struct ether_header *) recvbuf;
00122 
00123          bool is_my = (res==60);
00124          for (int cnt=0;cnt<6 && is_my;cnt++)
00125             if ((unsigned) eh2->ether_shost[cnt] != (unsigned) dest_mac[cnt]) is_my = false;
00126 
00127          if (is_my) {
00128             if (cnt1++ % 10000 == 0) printf("Recv %d packets\n", cnt1);
00129             if (debug)
00130                printf("Recv packet of size %3d  src: %02x:%02x:%02x:%02x:%02x:%02x    %x  %x  %x\n", (int) res,
00131                      eh2->ether_shost[0], eh2->ether_shost[1], eh2->ether_shost[2],
00132                      eh2->ether_shost[3], eh2->ether_shost[4], eh2->ether_shost[5]);
00133             memcpy(sendbuf, recvbuf+6, 6);
00134             memcpy(sendbuf+6, recvbuf, 6);
00135             memcpy(sendbuf+12, recvbuf+12, res-12);
00136             uint8_t* data = ((uint8_t*) sendbuf) + 14;
00137             
00138             
00139 //            for (int n=0;n<46;n++) data[n] = n;
00140             // reset adc data
00141             for (int n=0;n<16;n++) {
00142                unsigned adc = (unsigned) Gauss_Rnd(230 + n*4, 50 - 2*n);
00143                data[n*2] = (adc >> 2) & 0xff;
00144                data[n*2+1] = adc & 0x3;
00145             }
00146             
00147             if (first_sync_in_loop) {
00148               adc_tm = (sync_tm - (unsigned) Gauss_Rnd(200, 30)/50) ;
00149             } else {
00150                adc_tm = sync_tm + 100 + (unsigned) (1000*rand()/RAND_MAX);
00151             }
00152             
00153 //            printf("produce sync %u sync_tm %u adc_tm %u \n", sync_num, sync_tm, adc_tm);
00154 
00155             // set sync number
00156             data[FaspSyncPos] = (sync_num >> 16) & 0xff;
00157             data[FaspSyncPos+1] = (sync_num >> 8) & 0xff;
00158             data[FaspSyncPos+2] = sync_num & 0xff;
00159 
00160             data[FaspSyncPos+4] = (adc_tm >> 16) & 0xff;
00161             data[FaspSyncPos+5] = (adc_tm >> 8) & 0xff;
00162             data[FaspSyncPos+6] = adc_tm & 0xff;
00163 
00164             data[FaspSyncPos+7] = (sync_tm >> 16) & 0xff;
00165             data[FaspSyncPos+8] = (sync_tm >> 8) & 0xff;
00166             data[FaspSyncPos+9] = sync_tm & 0xff;
00167 
00168 //            printf("produce sync %u \n", sync_num);
00169 
00170             // change sync id every third packet
00171 
00172             first_sync_in_loop = false;
00173             if (--sync_repeat <= 0) {
00174                 do {
00175                    sync_num++;
00176                 } while ((sync_repeat = (int) (30.* rand() / RAND_MAX)) > 20);
00177                 sync_tm = (unsigned) rand() & 0xffffff;
00178                 first_sync_in_loop = true;
00179              }
00180 
00181             // sync_num++;
00182             
00183             if (sendto(sockfd, sendbuf, 60, 0, (struct sockaddr*)&socket_address, sizeof(socket_address))<0)
00184                printf("Fail to reply\n");
00185             //else
00186             //   printf("Reply on the packet\n");
00187           }
00188       } else
00189          usleep(1000);
00190    }
00191 
00192    close(sockfd);
00193 
00194    return 0;
00195 }

Generated on Tue Dec 10 2013 04:52:22 for ROCsoft by  doxygen 1.7.1