netmask.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. code to query kernel netmask
  5. Copyright (C) Andrew Tridgell 1998
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. /* working out the netmask for an interface is an incredibly non-portable
  19. thing. We have several possible implementations below, and autoconf
  20. tries each of them to see what works
  21. Note that this file does _not_ include includes.h. That is so this code
  22. can be called directly from the autoconf tests. That also means
  23. this code cannot use any of the normal Samba debug stuff or defines.
  24. This is standalone code.
  25. */
  26. #ifndef AUTOCONF
  27. #include "config.h"
  28. #endif
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #ifdef HAVE_NETMASK_IFCONF
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <arpa/inet.h>
  38. #include <netdb.h>
  39. #include <sys/ioctl.h>
  40. #include <net/if.h>
  41. #ifndef SIOCGIFCONF
  42. #include <sys/sockio.h>
  43. #endif
  44. /*
  45. * Prototype for gcc in fussy mode.
  46. */
  47. int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask);
  48. /****************************************************************************
  49. get the netmask address for a local interface
  50. ****************************************************************************/
  51. int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask)
  52. {
  53. struct ifconf ifc;
  54. char buff[2048];
  55. int fd, i, n;
  56. struct ifreq *ifr=NULL;
  57. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  58. #ifdef DEBUG
  59. fprintf(stderr,"socket failed\n");
  60. #endif
  61. return -1;
  62. }
  63. ifc.ifc_len = sizeof(buff);
  64. ifc.ifc_buf = buff;
  65. if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) {
  66. #ifdef DEBUG
  67. fprintf(stderr,"SIOCGIFCONF failed\n");
  68. #endif
  69. close(fd);
  70. return -1;
  71. }
  72. ifr = ifc.ifc_req;
  73. n = ifc.ifc_len / sizeof(struct ifreq);
  74. #ifdef DEBUG
  75. fprintf(stderr,"%d interfaces - looking for %s\n",
  76. n, inet_ntoa(*ipaddr));
  77. #endif
  78. /* Loop through interfaces, looking for given IP address */
  79. for (i=n-1;i>=0;i--) {
  80. if (ioctl(fd, SIOCGIFADDR, &ifr[i]) != 0) {
  81. #ifdef DEBUG
  82. fprintf(stderr,"SIOCGIFADDR failed\n");
  83. #endif
  84. continue;
  85. }
  86. #ifdef DEBUG
  87. fprintf(stderr,"interface %s\n",
  88. inet_ntoa((*(struct sockaddr_in *)&ifr[i].ifr_addr).sin_addr));
  89. #endif
  90. if (ipaddr->s_addr !=
  91. (*(struct sockaddr_in *)&ifr[i].ifr_addr).sin_addr.s_addr) {
  92. continue;
  93. }
  94. if (ioctl(fd, SIOCGIFNETMASK, &ifr[i]) != 0) {
  95. #ifdef DEBUG
  96. fprintf(stderr,"SIOCGIFNETMASK failed\n");
  97. #endif
  98. close(fd);
  99. return -1;
  100. }
  101. close(fd);
  102. (*nmask) = ((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr;
  103. #ifdef DEBUG
  104. fprintf(stderr,"netmask %s\n", inet_ntoa(*nmask));
  105. #endif
  106. return 0;
  107. }
  108. #ifdef DEBUG
  109. fprintf(stderr,"interface not found\n");
  110. #endif
  111. close(fd);
  112. return -1;
  113. }
  114. #elif defined(HAVE_NETMASK_IFREQ)
  115. #include <stdio.h>
  116. #include <sys/types.h>
  117. #include <sys/socket.h>
  118. #include <netinet/in.h>
  119. #include <arpa/inet.h>
  120. #include <netdb.h>
  121. #include <sys/ioctl.h>
  122. #include <net/if.h>
  123. #ifndef SIOCGIFCONF
  124. #include <sys/sockio.h>
  125. #endif
  126. #ifndef I_STR
  127. #include <sys/stropts.h>
  128. #endif
  129. /****************************************************************************
  130. this should cover most of the rest of systems
  131. ****************************************************************************/
  132. int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask)
  133. {
  134. struct ifreq ifreq;
  135. struct strioctl strioctl;
  136. struct ifconf *ifc;
  137. char buff[2048];
  138. int fd, i, n;
  139. struct ifreq *ifr=NULL;
  140. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  141. #ifdef DEBUG
  142. fprintf(stderr,"socket failed\n");
  143. #endif
  144. return -1;
  145. }
  146. ifc = (struct ifconf *)buff;
  147. ifc->ifc_len = BUFSIZ - sizeof(struct ifconf);
  148. strioctl.ic_cmd = SIOCGIFCONF;
  149. strioctl.ic_dp = (char *)ifc;
  150. strioctl.ic_len = sizeof(buff);
  151. if (ioctl(fd, I_STR, &strioctl) < 0) {
  152. #ifdef DEBUG
  153. fprintf(stderr,"SIOCGIFCONF failed\n");
  154. #endif
  155. close(fd);
  156. return -1;
  157. }
  158. ifr = (struct ifreq *)ifc->ifc_req;
  159. /* Loop through interfaces, looking for given IP address */
  160. n = ifc->ifc_len / sizeof(struct ifreq);
  161. for (i = 0; i<n; i++, ifr++) {
  162. #ifdef DEBUG
  163. fprintf(stderr,"interface %s\n",
  164. inet_ntoa((*(struct sockaddr_in *)&ifr->ifr_addr).sin_addr.s_addr));
  165. #endif
  166. if (ipaddr->s_addr ==
  167. (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
  168. break;
  169. }
  170. }
  171. #ifdef DEBUG
  172. if (i == n) {
  173. fprintf(stderr,"interface not found\n");
  174. close(fd);
  175. return -1;
  176. }
  177. #endif
  178. ifreq = *ifr;
  179. strioctl.ic_cmd = SIOCGIFNETMASK;
  180. strioctl.ic_dp = (char *)&ifreq;
  181. strioctl.ic_len = sizeof(struct ifreq);
  182. if (ioctl(fd, I_STR, &strioctl) != 0) {
  183. #ifdef DEBUG
  184. fprintf(stderr,"Failed SIOCGIFNETMASK\n");
  185. #endif
  186. close(fd);
  187. return -1;
  188. }
  189. close(fd);
  190. *nmask = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
  191. #ifdef DEBUG
  192. fprintf(stderr,"netmask %s\n", inet_ntoa(*nmask));
  193. #endif
  194. return 0;
  195. }
  196. #elif defined(HAVE_NETMASK_AIX)
  197. #include <stdio.h>
  198. #include <unistd.h> /* close() declaration for gcc in fussy mode */
  199. #include <sys/types.h>
  200. #include <sys/socket.h>
  201. #include <netinet/in.h>
  202. #include <arpa/inet.h>
  203. #include <netdb.h>
  204. #include <sys/ioctl.h>
  205. #include <net/if.h>
  206. #ifndef SIOCGIFCONF
  207. #include <sys/sockio.h>
  208. #endif
  209. /*
  210. * Prototype for gcc in fussy mode.
  211. */
  212. int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask);
  213. /****************************************************************************
  214. this one is for AIX
  215. ****************************************************************************/
  216. int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask)
  217. {
  218. char buff[2048];
  219. int fd, i;
  220. struct ifconf ifc;
  221. struct ifreq *ifr=NULL;
  222. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  223. #ifdef DEBUG
  224. fprintf(stderr,"socket failed\n");
  225. #endif
  226. return -1;
  227. }
  228. ifc.ifc_len = sizeof(buff);
  229. ifc.ifc_buf = buff;
  230. if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) {
  231. #ifdef DEBUG
  232. fprintf(stderr,"SIOCGIFCONF failed\n");
  233. #endif
  234. close(fd);
  235. return -1;
  236. }
  237. ifr = ifc.ifc_req;
  238. /* Loop through interfaces, looking for given IP address */
  239. i = ifc.ifc_len;
  240. while (i > 0) {
  241. #ifdef DEBUG
  242. fprintf(stderr,"interface %s\n",
  243. inet_ntoa((*(struct sockaddr_in *)&ifr->ifr_addr).sin_addr));
  244. #endif
  245. if (ipaddr->s_addr ==
  246. (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
  247. break;
  248. }
  249. i -= ifr->ifr_addr.sa_len + IFNAMSIZ;
  250. ifr = (struct ifreq*) ((char*) ifr + ifr->ifr_addr.sa_len +
  251. IFNAMSIZ);
  252. }
  253. #ifdef DEBUG
  254. if (i <= 0) {
  255. fprintf(stderr,"interface not found\n");
  256. close(fd);
  257. return -1;
  258. }
  259. #endif
  260. if (ioctl(fd, SIOCGIFNETMASK, ifr) != 0) {
  261. #ifdef DEBUG
  262. fprintf(stderr,"SIOCGIFNETMASK failed\n");
  263. #endif
  264. close(fd);
  265. return -1;
  266. }
  267. close(fd);
  268. (*nmask) = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
  269. #ifdef DEBUG
  270. fprintf(stderr,"netmask %s\n", inet_ntoa(*nmask));
  271. #endif
  272. return 0;
  273. }
  274. #else /* a dummy version */
  275. struct in_addr; /* it may not have been declared before */
  276. int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask);
  277. int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask)
  278. {
  279. return -1;
  280. }
  281. #endif
  282. #ifdef AUTOCONF
  283. /* this is the autoconf driver to test get_netmask() */
  284. main()
  285. {
  286. char buf[1024];
  287. struct hostent *hp;
  288. struct in_addr ip, nmask;
  289. if (gethostname(buf, sizeof(buf)-1) != 0) {
  290. fprintf(stderr,"gethostname failed\n");
  291. exit(1);
  292. }
  293. hp = gethostbyname(buf);
  294. if (!hp) {
  295. fprintf(stderr,"gethostbyname failed\n");
  296. exit(1);
  297. }
  298. memcpy((char *)&ip, (char *)hp->h_addr, hp->h_length);
  299. if (get_netmask(&ip, &nmask) == 0) exit(0);
  300. fprintf(stderr,"get_netmask failed\n");
  301. exit(1);
  302. }
  303. #endif