Advertisement
tux_mind

recvfrom_debug2

Dec 3rd, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.88 KB | None | 0 0
  1. #include <netinet/in.h>
  2. #include <sys/socket.h>
  3. #include <sys/ioctl.h>
  4. #include <net/if.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <linux/if_packet.h>
  11. #include <netinet/if_ether.h>
  12. #include <stdlib.h>
  13. #include <sys/wait.h>
  14.  
  15. #define NBNS_NBSTATREQ_LEN 50
  16.  
  17. /**
  18.  * @brief static NetBIOS NBSTAT request.
  19.  *
  20.  * RFC 1002, Paragraph 4.2.17
  21.  */
  22. uint8_t nbns_nbstat_request[NBNS_NBSTATREQ_LEN] = {
  23.     0x82, 0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  24.     0x00, 0x00, 0x20, 0x43, 0x4B, 0x41, 0x41, 0x41, 0x41, 0x41,
  25.     0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
  26.     0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
  27.     0x41, 0x41, 0x41, 0x41, 0x41, 0x00, 0x00, 0x21, 0x00, 0x01
  28. };
  29.  
  30. char running = 1;
  31. //pid_t receiver_pid;
  32. long unsigned int total, bad;
  33.  
  34. void *sender(void *arg) {
  35.   int sockfd;
  36.   struct sockaddr_in addr;
  37.  
  38.   sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  39.  
  40.   memset(&addr, 0, sizeof(addr));
  41.  
  42.   addr.sin_family = AF_INET;
  43.   addr.sin_port = htons(137);
  44.   addr.sin_addr.s_addr = htonl(0x0AA9A7FE); // 10.169.167.254
  45.  
  46.   connect(sockfd, (struct sockaddr *) &addr, sizeof(addr));
  47.  
  48.   while(running) {
  49.     write(sockfd, nbns_nbstat_request, NBNS_NBSTATREQ_LEN);
  50.     usleep(100);
  51.   }
  52.  
  53.   sendto(sockfd, nbns_nbstat_request, NBNS_NBSTATREQ_LEN, 0, (struct sockaddr *) &addr, sizeof(addr));
  54.  
  55.   return NULL;
  56. }
  57.  
  58. void stop(int signal) {
  59.   running = 0;
  60. }
  61.  
  62. static int
  63. iface_get_id(const char *device)
  64. {
  65.   struct ifreq  ifr;
  66.   int fd;
  67.  
  68.   // this is the main difference from pcap:
  69.   // we use another socket for perform ioctl interrogations
  70.   fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  71.  
  72.   memset(&ifr, 0, sizeof(ifr));
  73.   strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  74.  
  75.   if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
  76.     fprintf(stderr, "SIOCGIFINDEX: %s\n", strerror(errno));
  77.     close(fd);
  78.     return -1;
  79.   }
  80.   close(fd);
  81.  
  82.   return ifr.ifr_ifindex;
  83. }
  84.  
  85. void *receiver(void *arg) {
  86.   int sockfd;
  87.   int ifindex;
  88.   struct sockaddr_ll  sll;
  89.   int     err;
  90.   socklen_t   errlen = sizeof(err);
  91.   socklen_t   fromlen;
  92.   size_t packet_len;
  93.   char buffer[1514];
  94.  
  95.   /* pcap_open_live */
  96.  
  97.   sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  98.  
  99.   ifindex = iface_get_id((char *) arg);
  100.  
  101.   if(ifindex==-1) return NULL;
  102.  
  103.   memset(&sll, 0, sizeof(sll));
  104.   sll.sll_family    = AF_PACKET;
  105.   sll.sll_ifindex   = ifindex;
  106.   sll.sll_protocol  = htons(ETH_P_ALL);
  107.  
  108.   if (bind(sockfd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
  109.     fprintf(stderr, "bind: %s\n", strerror(errno));
  110.     return NULL;
  111.   }
  112.  
  113.   /* Any pending errors, e.g., network is down? */
  114.  
  115.   if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
  116.     fprintf(stderr, "getsockopt: %s\n", strerror(errno));
  117.     return NULL;
  118.   }
  119.  
  120.   if (err > 0) {
  121.     fprintf(stderr, "bind: %s\n", strerror(err));
  122.     return NULL;
  123.   }
  124.  
  125.   /* pcap_open_live ends */
  126.  
  127.   /* pcap_loop */
  128.  
  129.   fromlen = sizeof(sll);
  130.  
  131.   while(running) {
  132.  
  133.     do {
  134.       packet_len = recvfrom(sockfd, buffer, 1514, MSG_TRUNC,
  135.                             (struct sockaddr *) &sll, &fromlen);
  136.     } while(running && packet_len == -1 && (errno == EINTR || errno == EAGAIN));
  137.    
  138.     if(running && packet_len == -1) {
  139.       fprintf(stderr, "recvfrom: %s\n", strerror(errno));
  140.       return NULL;
  141.     }
  142.    
  143.     if(packet_len > 14) {
  144.       total++;
  145.      
  146.       fprintf(stderr, "sll: { sll_family=%d, sll_protocol=%d, sll_ifindex=%d, sll_hatype=%d, sll_pkttype=%d, sll_halen=%d, sll_addr=[%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX] }\n",
  147.               sll.sll_family, sll.sll_protocol, sll.sll_ifindex, sll.sll_hatype,
  148.               sll.sll_pkttype, sll.sll_halen, sll.sll_addr[0], sll.sll_addr[1],
  149.               sll.sll_addr[2], sll.sll_addr[3], sll.sll_addr[4], sll.sll_addr[5],
  150.               sll.sll_addr[6], sll.sll_addr[7]);
  151.      
  152.      
  153.       fprintf(stderr, "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX => %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX\n",
  154.               buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[11],
  155.               buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
  156.       fflush(stderr);
  157.      
  158.       if(memcmp(buffer, sll.sll_addr, 6) && memcmp(buffer + 6, sll.sll_addr, 6)) {
  159.         bad++;
  160.       }
  161.     }
  162.    
  163.   }
  164.  
  165.   /* pcap_loop ends */
  166.  
  167.   close(sockfd);
  168.  
  169.   return NULL;
  170. }
  171.  
  172. int main(int argc, char **argv) {
  173.  
  174.   if(argc != 2) return 1;
  175.  
  176.   if(signal(SIGINT, stop) == SIG_ERR) return 2;
  177.  
  178.   bad = total = 0;
  179.  
  180. #ifdef SENDER
  181.   sender((void *) argv[1]);
  182. #else
  183.   receiver((void *) argv[1]);
  184. #endif
  185.  
  186.   running=0;
  187.  
  188. #ifndef SENDER
  189.   fprintf(stderr, "total packets: %lu\nbad ones: %lu\n", total, bad);
  190. #endif
  191.  
  192.   exit(0); // close all unclear stuff [ hey, is a test program ;) ]
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement