memcpy

local ip with checking

Dec 6th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1.     printf("Retrieving data for %s...\n",hostname);
  2.     short i, j, ifd_num = 0, hst_num = 0;  
  3.    
  4.     char *host_chk[20];
  5.     char *ifad_chk[20];
  6.    
  7.     struct hostent * h_local_chk;
  8.     char *host = (char *)malloc(255);
  9.     gethostname(host, 255);
  10.     if ((h_local_chk = gethostbyname(host)) < 0) die_with_signal("hostname");
  11.     free(host);
  12.     for (i=0; i < h_local_chk->h_length; i++) {
  13.         host_chk[i] = calloc(1, INET_ADDRSTRLEN+1);
  14.         inet_ntop(AF_INET, (struct in_addr *)h_local_chk->h_addr_list[i], host_chk[i], INET_ADDRSTRLEN);
  15.         hst_num++;
  16.     }
  17.     struct ifaddrs *ifa = (struct ifaddrs *)malloc(sizeof(*ifa));
  18.     if (getifaddrs(&ifa)) die_with_signal("local retr.");
  19.     i = 0;
  20.     for (; ifa != NULL; ifa = ifa->ifa_next) {
  21.         if (!memcmp(ifa->ifa_name, syn->device, strlen(syn->device))) {
  22.             ifad_chk[i] = calloc(1, INET_ADDRSTRLEN+1);
  23.             inet_ntop(AF_INET, (void *)&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, ifad_chk[i], INET_ADDRSTRLEN);
  24.             ifd_num++, i++;
  25.         }
  26.     }
  27.     free(ifa);
  28.     for (i = 0; i < hst_num; i++) {
  29.         for (j = 0; j < ifd_num; j++) {
  30.             if (!(memcmp(host_chk[i], ifad_chk[j], 10))) {
  31.                 syn->syn_data.p_src = (char *)malloc(16);  
  32.                 memcpy( syn->syn_data.p_src, host_chk[i], INET_ADDRSTRLEN);
  33.                 printf("Got local IP: %s\n",syn->syn_data.p_src);
  34.                 free(host_chk[i]);
  35.                 free(ifad_chk[j]);
  36.                 break;
  37.             }
  38.         }
  39.         if (syn->syn_data.p_src) break;
  40.     }
  41.    
  42.  
Advertisement
Add Comment
Please, Sign In to add comment