tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / network / interfaces.cpp
blobc402932cd45b16b5d4f687c868c5bd48a98965a9
1 /*
2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, axeld@pinc-software.de
7 */
10 #include <errno.h>
11 #include <net/if.h>
12 #include <sys/socket.h>
13 #include <sys/sockio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
18 #include <AutoDeleter.h>
20 #include "compatibility/bsd/ifaddrs.h"
23 namespace BPrivate {
24 class Socket {
25 public:
26 Socket()
28 fFD = socket(AF_INET, SOCK_DGRAM, 0);
31 ~Socket()
33 if (fFD >= 0)
34 close(fFD);
37 int FD() const { return fFD; }
39 private:
40 int fFD;
42 } // namespace BPrivate
45 // #pragma mark -
48 unsigned
49 if_nametoindex(const char* name)
51 BPrivate::Socket socket;
52 if (socket.FD() < 0)
53 return 0;
55 ifreq request;
56 strlcpy(request.ifr_name, name, IF_NAMESIZE);
57 if (ioctl(socket.FD(), SIOCGIFINDEX, &request, sizeof(struct ifreq)) < 0)
58 return 0;
60 return request.ifr_index;
64 char*
65 if_indextoname(unsigned index, char* nameBuffer)
67 BPrivate::Socket socket;
68 if (socket.FD() < 0)
69 return NULL;
71 ifreq request;
72 request.ifr_index = index;
73 if (ioctl(socket.FD(), SIOCGIFNAME, &request, sizeof(struct ifreq)) < 0)
74 return NULL;
76 strlcpy(nameBuffer, request.ifr_name, IF_NAMESIZE);
77 return nameBuffer;
81 struct if_nameindex*
82 if_nameindex(void)
84 BPrivate::Socket socket;
85 if (socket.FD() < 0)
86 return NULL;
88 // get a list of all interfaces
90 ifconf config;
91 config.ifc_len = sizeof(config.ifc_value);
92 if (ioctl(socket.FD(), SIOCGIFCOUNT, &config, sizeof(struct ifconf)) < 0)
93 return NULL;
95 int count = (int)config.ifc_value;
96 ifreq* interfaces = (ifreq*)malloc(count * sizeof(struct ifreq));
97 if (interfaces == NULL)
98 return NULL;
100 MemoryDeleter deleter(interfaces);
102 config.ifc_len = count * sizeof(struct ifreq);
103 config.ifc_req = interfaces;
104 if (ioctl(socket.FD(), SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
105 return NULL;
107 struct if_nameindex* interfaceArray = (struct if_nameindex*)malloc(
108 sizeof(struct if_nameindex) * (count + 1));
109 if (interfaceArray == NULL)
110 return NULL;
112 int i = 0;
113 while (count > 0) {
114 // retrieve interface index
115 ifreq request;
116 strlcpy(((struct ifreq&)request).ifr_name, interfaces->ifr_name,
117 IF_NAMESIZE);
119 if (ioctl(socket.FD(), SIOCGIFINDEX, &request,
120 sizeof(struct ifreq)) >= 0) {
121 interfaceArray[i].if_index = request.ifr_index;
122 interfaceArray[i].if_name = strdup(interfaces->ifr_name);
123 i++;
126 interfaces = (ifreq*)((char*)interfaces
127 + _SIZEOF_ADDR_IFREQ(interfaces[0]));
128 count--;
131 interfaceArray[i].if_index = 0;
132 interfaceArray[i].if_name = NULL;
134 return interfaceArray;
138 void
139 if_freenameindex(struct if_nameindex *interfaceArray)
141 for (int i = 0; interfaceArray[i].if_name; i++)
142 free(interfaceArray[i].if_name);
144 free(interfaceArray);