| | 121 | |
| | 122 | char *get_iface_ip(char *ifname) { |
| | 123 | struct ifreq if_data; |
| | 124 | struct in_addr in; |
| | 125 | int sockd; |
| | 126 | u_int32_t ip; |
| | 127 | |
| | 128 | /* Create a socket */ |
| | 129 | if ((sockd = socket (AF_INET, SOCK_PACKET, htons(0x8086))) < 0) { |
| | 130 | debug(LOG_ERR, "socket(): %s", strerror(errno)); |
| | 131 | return NULL; |
| | 132 | } |
| | 133 | |
| | 134 | /* Get IP of internal interface */ |
| | 135 | strcpy (if_data.ifr_name, ifname); |
| | 136 | |
| | 137 | /* Get the IP address */ |
| | 138 | if (ioctl (sockd, SIOCGIFADDR, &if_data) < 0) { |
| | 139 | debug(LOG_ERR, "ioctl(): SIOCGIFADDR %s", strerror(errno)); |
| | 140 | return NULL; |
| | 141 | } |
| | 142 | memcpy ((void *) &ip, (void *) &if_data.ifr_addr.sa_data + 2, 4); |
| | 143 | in.s_addr = ip; |
| | 144 | |
| | 145 | return (char *)inet_ntoa(in); |
| | 146 | } |