File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -112,7 +112,7 @@ int __getifaddrs(struct ifaddrs **ifap)
112
112
/* Now copy the information we already have from SIOCGIFCONF. */
113
113
storage -> ia .ifa_name = names ;
114
114
strcpy (names , ifr -> ifr_name );
115
- names += strlen (names );
115
+ names += strlen (names ) + 1 ;
116
116
storage -> addr = ifr -> ifr_addr ;
117
117
storage -> ia .ifa_addr = & storage -> addr ;
118
118
Original file line number Diff line number Diff line change
1
+ #include <ifaddrs.h>
2
+ #include <net/if.h>
3
+ #include <stdio.h>
4
+ #include <sys/socket.h>
5
+ #include <sys/ioctl.h>
6
+ #include <unistd.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+ #include <errno.h>
10
+ #include <netinet/in.h>
11
+ #include <arpa/inet.h>
12
+
13
+ int main (void )
14
+ {
15
+ struct ifaddrs * ifap ;
16
+ struct ifaddrs * ifa ;
17
+ struct sockaddr_in * sa ;
18
+ char * addr ;
19
+
20
+ if (getifaddrs (& ifap ) == -1 )
21
+ {
22
+ perror ("getifaddrs" );
23
+ return 1 ;
24
+ }
25
+ for (ifa = ifap ; ifa ; ifa = ifa -> ifa_next )
26
+ {
27
+ if (ifa -> ifa_addr && ifa -> ifa_addr -> sa_family == AF_INET )
28
+ {
29
+ sa = (struct sockaddr_in * ) ifa -> ifa_addr ;
30
+ addr = inet_ntoa (sa -> sin_addr );
31
+ printf ("Interface: %s\tAddress: %s\n" , ifa -> ifa_name , addr );
32
+ }
33
+
34
+ }
35
+
36
+ freeifaddrs (ifap );
37
+ return 0 ;
38
+ }
You can’t perform that action at this time.
0 commit comments