addrs_dlpi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * addrs_dlpi.c:
  3. *
  4. * Provides the get_addrs_dlpi() function for use on systems that require
  5. * the use of the System V STREAMS DataLink Programming Interface for
  6. * acquiring low-level ethernet information about interfaces.
  7. *
  8. * Like Solaris.
  9. *
  10. */
  11. #include "config.h"
  12. #ifdef HAVE_DLPI
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <fcntl.h>
  19. #include <sys/types.h>
  20. #include <sys/sockio.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/socket.h>
  23. #include <sys/dlpi.h>
  24. #include <net/if.h>
  25. #include "dlcommon.h"
  26. extern char *split_dname(char *device, int *unitp);
  27. extern char *strncpy2(char *dest, char *src, int n);
  28. extern char *strncat2(char *dest, char *src, int n);
  29. /*
  30. * This function identifies the IP address and ethernet address for the interface
  31. * specified
  32. *
  33. * This function returns -1 on catastrophic failure, or a bitwise OR of the
  34. * following values:
  35. * XXX: change this to perfom "best effort" identification of addresses.
  36. * Failure to find an address - for whatever reason - isn't fatal, just a
  37. * nuisance.
  38. *
  39. * 1 - Was able to get the ethernet address
  40. * 2 - Was able to get the IP address
  41. *
  42. * This function should return 3 if all information was found
  43. */
  44. int
  45. get_addrs_dlpi(char *interface, char if_hw_addr[], struct in_addr *if_ip_addr)
  46. {
  47. int got_hw_addr = 0;
  48. int got_ip_addr = 0;
  49. int fd;
  50. long buf[MAXDLBUF]; /* long aligned */
  51. union DL_primitives *dlp;
  52. char *cp;
  53. int unit_num = 0;
  54. int sap = 0;
  55. char *devname = NULL;
  56. char *devname2 = NULL;
  57. char fulldevpath[256];
  58. struct ifreq ifr = {};
  59. /* -- */
  60. memset(if_hw_addr, 0, 6);
  61. // we want to be able to process either a fully qualified /dev/ge0
  62. // type interface definition, or just ge0.
  63. if (strncmp(interface, "/dev/", strlen("/dev/")) == 0) {
  64. devname = interface + strlen("/dev/");
  65. } else {
  66. devname = interface;
  67. }
  68. strncpy2(fulldevpath, "/dev/", sizeof(fulldevpath)-1);
  69. cp = strncat2(fulldevpath, interface, sizeof(fulldevpath));
  70. if (strlen(cp) != 0) {
  71. fprintf(stderr, "device name buffer overflow %s\n", fulldevpath);
  72. return -1;
  73. }
  74. fprintf(stderr,"interface: %s\n", devname);
  75. // on Solaris, even though we are wanting to talk to ethernet device
  76. // ge0, we have to open /dev/ge, then bind to unit 0. Dupe our
  77. // full path, then identify and cut off the unit number
  78. devname2 = strdup(fulldevpath);
  79. cp = split_dname(devname2, &unit_num);
  80. if (cp == NULL) {
  81. free(devname2);
  82. goto get_ip_address;
  83. } else {
  84. *cp = '\0'; /* null terminate devname2 right before numeric extension */
  85. }
  86. // devname2 should now be something akin to /dev/ge. Try to open
  87. // it, and if it fails, fall back to the full /dev/ge0.
  88. if ((fd = open(devname2, O_RDWR)) < 0) {
  89. if (errno != ENOENT) {
  90. fprintf(stderr, "Couldn't open %s\n", devname2);
  91. free(devname2);
  92. goto get_ip_address;
  93. } else {
  94. if ((fd = open(fulldevpath, O_RDWR)) < 0) {
  95. fprintf(stderr, "Couldn't open %s\n", fulldevpath);
  96. free(devname2);
  97. goto get_ip_address;
  98. }
  99. }
  100. }
  101. free(devname2);
  102. devname2 = NULL;
  103. /* Use the dlcommon functions to get access to the DLPI information for this
  104. * interface. All of these functions exit() out on failure
  105. */
  106. dlp = (union DL_primitives*) buf;
  107. /*
  108. * DLPI attach to our low-level device
  109. */
  110. dlattachreq(fd, unit_num);
  111. dlokack(fd, buf);
  112. /*
  113. * DLPI bind
  114. */
  115. dlbindreq(fd, sap, 0, DL_CLDLS, 0, 0);
  116. dlbindack(fd, buf);
  117. /*
  118. * DLPI DL_INFO_REQ
  119. */
  120. dlinforeq(fd);
  121. dlinfoack(fd, buf);
  122. /*
  123. printdlprim(dlp); // uncomment this to dump out info from DLPI
  124. */
  125. if (dlp->info_ack.dl_addr_length + dlp->info_ack.dl_sap_length == 6) {
  126. memcpy(if_hw_addr,
  127. OFFADDR(dlp, dlp->info_ack.dl_addr_offset),
  128. dlp->info_ack.dl_addr_length);
  129. got_hw_addr = 1;
  130. } else {
  131. fprintf(stderr, "Error, bad length for hardware interface %s -- %d\n",
  132. interface,
  133. dlp->info_ack.dl_addr_length);
  134. }
  135. close(fd);
  136. get_ip_address:
  137. /* Get the IP address of the interface */
  138. #ifdef SIOCGIFADDR
  139. fd = socket(PF_INET, SOCK_DGRAM, 0); /* any sort of IP socket will do */
  140. strncpy(ifr.ifr_name, interface, IFNAMSIZ);
  141. (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = AF_INET;
  142. if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
  143. fprintf(stderr, "Error getting IP address for interface: %s\n", "ge0");
  144. perror("ioctl(SIOCGIFADDR)");
  145. } else {
  146. memcpy(if_ip_addr, &((*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr), sizeof(struct in_addr));
  147. got_ip_addr = 2;
  148. }
  149. #else
  150. fprintf(stderr, "Cannot obtain IP address on this platform\n");
  151. #endif
  152. close(fd);
  153. return got_hw_addr + got_ip_addr;
  154. }
  155. /*
  156. * Split a device name into a device type name and a unit number;
  157. * return the a pointer to the beginning of the unit number, which
  158. * is the end of the device type name, and set "*unitp" to the unit
  159. * number.
  160. *
  161. * Returns NULL on error, and fills "ebuf" with an error message.
  162. */
  163. char *
  164. split_dname(char *device, int *unitp)
  165. {
  166. char *cp;
  167. char *eos;
  168. int unit;
  169. /* -- */
  170. /*
  171. * Look for a number at the end of the device name string.
  172. */
  173. cp = device + strlen(device) - 1;
  174. if (*cp < '0' || *cp > '9') {
  175. fprintf(stderr, "%s missing unit number", device);
  176. return (NULL);
  177. }
  178. /* Digits at end of string are unit number */
  179. while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9')
  180. cp--;
  181. unit = (int) strtol(cp, &eos, 10);
  182. if (*eos != '\0') {
  183. fprintf(stderr, "%s bad unit number", device);
  184. return (NULL);
  185. }
  186. *unitp = unit;
  187. return (cp);
  188. }
  189. /*------------------------------------------------------------------------------
  190. strncpy2()
  191. strncpy2() is like strncpy(), except that strncpy2() will always
  192. insure that the <dest> buffer is null terminated. strncpy() will not
  193. NULL terminate the destination buffer if the <src> string is <n>
  194. characters long or longer, not counting the terminating NULL character.
  195. STRNCPY2() IS NOT A COMPATIBLE REPLACEMENT FOR STRNCPY()!!
  196. There are two reasons to use strncpy2().
  197. The first reason is to guarantee that <dest> buffer's bounds are not
  198. violated. In this case, <n> should be the size of the <dest> buffer
  199. minus one.
  200. i.e.,
  201. char tempstring[MAXLINE];
  202. strncpy2(tempstring, my_own_string, MAXLINE - 1);
  203. The second reason is to copy a specific number of characters from
  204. <src> to <dest>. In this case, <n> should be the number of characters
  205. you want to transfer, not including the terminating NULL character.
  206. The following example copies "abc" into tempstring, and NULL
  207. terminates it.
  208. char tempstring[MAXLINE];
  209. strncpy2(tempstring, "abcdef123", 3);
  210. strncpy2() returns a pointer to the first character in <src> that was
  211. not copied to <dest>. If all of <src> was copied to <dest>,
  212. strncpy2() will return a pointer to the NULL character terminating the
  213. <src> string.
  214. ------------------------------------------------------------------------------*/
  215. char *
  216. strncpy2(char *dest, char *src, int n)
  217. {
  218. int
  219. i = 0;
  220. char
  221. *char_ptr;
  222. /* -- */
  223. if ((!dest) || (!src))
  224. return(src);
  225. char_ptr = dest;
  226. while ((i++ < n) && *src)
  227. *char_ptr++ = *src++;
  228. *char_ptr = '\0';
  229. return(src);
  230. }
  231. /*------------------------------------------------------------------------------
  232. strncat2()
  233. Similar to strncat except that <n> is the size of the <dest> buffer
  234. (INCLUDING SPACE FOR THE TRAILING NULL CHAR), NOT the number of
  235. characters to add to the buffer.
  236. STRNCAT2() IS NOT A COMPATIBLE REPLACEMENT FOR STRNCAT()!
  237. strncat2() always guarantees that the <dest> will be null terminated, and that
  238. the buffer limits will be honored. strncat2() will not write even one
  239. byte beyond the end of the <dest> buffer.
  240. strncat2() concatenates up to <n-1> - strlen(<dest>) characters from
  241. <src> to <dest>.
  242. So if the <dest> buffer has a size of 20 bytes (including trailing NULL),
  243. and <dest> contains a 19 character string, nothing will be done to
  244. <dest>.
  245. If the string in <dest> is longer than <n-1> characters upon entry to
  246. strncat2(), <dest> will be truncated after the <n-1>th character.
  247. strncat2() returns a pointer to the first character in the src buffer that
  248. was not copied into dest.. so if strncat2() returns a non-zero character,
  249. string truncation occurred in the concat operation.
  250. ------------------------------------------------------------------------------*/
  251. char *
  252. strncat2(char *dest, char *src, int n)
  253. {
  254. int
  255. i = 0;
  256. char
  257. *dest_ptr,
  258. *src_ptr;
  259. /* -- */
  260. if (!dest || !src)
  261. return NULL;
  262. dest_ptr = dest;
  263. src_ptr = src;
  264. /* i = 0 */
  265. while ((i < (n-1)) && *dest_ptr)
  266. {
  267. i++;
  268. dest_ptr++;
  269. }
  270. /* i is the number of characters in dest before the concatenation
  271. operation.. a number between 0 and n-1 */
  272. while ((i++ < (n-1)) && *src_ptr)
  273. *dest_ptr++ = *src_ptr++;
  274. /* i is the number of characters in dest after the concatenation
  275. operation, or n if the concat operation got truncated.. a number
  276. between 0 and n
  277. We need to check src_ptr here because i will be equal to n if
  278. <dest> was full before the concatenation operation started (which
  279. effectively causes instant truncation even if the <src> string is
  280. empty..
  281. We could just test src_ptr here, but that would report
  282. a string truncation if <src> was empty, which we don't
  283. necessarily want. */
  284. if ((i == n) && *src_ptr)
  285. {
  286. // we could log truncation here
  287. }
  288. *dest_ptr = '\0';
  289. /* should point to a non-empty substring only if the concatenation
  290. operation got truncated.
  291. If src_ptr points to an empty string, the operation always
  292. succeeded, either due to an empty <src> or because of
  293. sufficient room in <dest>. */
  294. return(src_ptr);
  295. }
  296. #endif /* HAVE_DLPI */