b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | From 37b2dda9b1d62eb91028f6d590beddd36f7b79c8 Mon Sep 17 00:00:00 2001 |
| 2 | From: Nathan Samson <nathan@nathansamson.be> |
| 3 | Date: Mon, 1 Feb 2016 21:37:22 +0100 |
| 4 | Subject: [PATCH] Modify get ip from iface method. |
| 5 | |
| 6 | This used to use a RAW socket, while now it used a DGRAM socket. |
| 7 | Previously it failed with operation not permitted, while this version |
| 8 | seems to work reliably. |
| 9 | |
| 10 | --- a/src/util.c |
| 11 | +++ b/src/util.c |
| 12 | @@ -174,11 +174,13 @@ get_iface_ip(const char *ifname) |
| 13 | u_int32_t ip; |
| 14 | |
| 15 | /* Create a socket */ |
| 16 | - if ((sockd = socket(AF_INET, SOCK_RAW, htons(0x8086))) < 0) { |
| 17 | + if ((sockd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 18 | debug(LOG_ERR, "socket(): %s", strerror(errno)); |
| 19 | return NULL; |
| 20 | } |
| 21 | |
| 22 | + /* I want to get an IPv4 IP address */ |
| 23 | + if_data.ifr_addr.sa_family = AF_INET; |
| 24 | /* Get IP of internal interface */ |
| 25 | strncpy(if_data.ifr_name, ifname, 15); |
| 26 | if_data.ifr_name[15] = '\0'; |