PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

lib/libc/net/if_nameindex.c

http://www.minix3.org/
C | 156 lines | 64 code | 10 blank | 82 comment | 12 complexity | 9c7b4516a78eb9c6c7da4e650b7c257f MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /* $NetBSD: if_nameindex.c,v 1.6 2000/12/20 18:47:11 christos Exp $ */
  2. /* $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $ */
  3. /*-
  4. * Copyright (c) 1997, 2000
  5. * Berkeley Software Design, Inc. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
  17. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. * SUCH DAMAGE.
  24. *
  25. * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
  26. */
  27. #include <sys/cdefs.h>
  28. #if defined(LIBC_SCCS) && !defined(lint)
  29. __RCSID("$NetBSD: if_nameindex.c,v 1.6 2000/12/20 18:47:11 christos Exp $");
  30. #endif /* LIBC_SCCS and not lint */
  31. #include "namespace.h"
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <net/if_dl.h>
  35. #include <net/if.h>
  36. #include <ifaddrs.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #ifdef __weak_alias
  40. __weak_alias(if_nameindex,_if_nameindex)
  41. __weak_alias(if_freenameindex,_if_freenameindex)
  42. #endif
  43. /*
  44. * From RFC 2553:
  45. *
  46. * 4.3 Return All Interface Names and Indexes
  47. *
  48. * The if_nameindex structure holds the information about a single
  49. * interface and is defined as a result of including the <net/if.h>
  50. * header.
  51. *
  52. * struct if_nameindex {
  53. * unsigned int if_index;
  54. * char *if_name;
  55. * };
  56. *
  57. * The final function returns an array of if_nameindex structures, one
  58. * structure per interface.
  59. *
  60. * struct if_nameindex *if_nameindex(void);
  61. *
  62. * The end of the array of structures is indicated by a structure with
  63. * an if_index of 0 and an if_name of NULL. The function returns a NULL
  64. * pointer upon an error, and would set errno to the appropriate value.
  65. *
  66. * The memory used for this array of structures along with the interface
  67. * names pointed to by the if_name members is obtained dynamically.
  68. * This memory is freed by the next function.
  69. *
  70. * 4.4. Free Memory
  71. *
  72. * The following function frees the dynamic memory that was allocated by
  73. * if_nameindex().
  74. *
  75. * #include <net/if.h>
  76. *
  77. * void if_freenameindex(struct if_nameindex *ptr);
  78. *
  79. * The argument to this function must be a pointer that was returned by
  80. * if_nameindex().
  81. */
  82. struct if_nameindex *
  83. if_nameindex(void)
  84. {
  85. struct ifaddrs *ifaddrs, *ifa;
  86. unsigned int ni;
  87. int nbytes;
  88. struct if_nameindex *ifni, *ifni2;
  89. char *cp;
  90. if (getifaddrs(&ifaddrs) < 0)
  91. return(NULL);
  92. /*
  93. * First, find out how many interfaces there are, and how
  94. * much space we need for the string names.
  95. */
  96. ni = 0;
  97. nbytes = 0;
  98. for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
  99. if (ifa->ifa_addr &&
  100. ifa->ifa_addr->sa_family == AF_LINK) {
  101. nbytes += strlen(ifa->ifa_name) + 1;
  102. ni++;
  103. }
  104. }
  105. /*
  106. * Next, allocate a chunk of memory, use the first part
  107. * for the array of structures, and the last part for
  108. * the strings.
  109. */
  110. cp = malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes);
  111. ifni = (struct if_nameindex *)(void *)cp;
  112. if (ifni == NULL)
  113. goto out;
  114. cp += (ni + 1) * sizeof(struct if_nameindex);
  115. /*
  116. * Now just loop through the list of interfaces again,
  117. * filling in the if_nameindex array and making copies
  118. * of all the strings.
  119. */
  120. ifni2 = ifni;
  121. for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
  122. if (ifa->ifa_addr &&
  123. ifa->ifa_addr->sa_family == AF_LINK) {
  124. ifni2->if_index =
  125. ((struct sockaddr_dl*)
  126. (void *)ifa->ifa_addr)->sdl_index;
  127. ifni2->if_name = cp;
  128. strcpy(cp, ifa->ifa_name);
  129. ifni2++;
  130. cp += strlen(cp) + 1;
  131. }
  132. }
  133. /*
  134. * Finally, don't forget to terminate the array.
  135. */
  136. ifni2->if_index = 0;
  137. ifni2->if_name = NULL;
  138. out:
  139. freeifaddrs(ifaddrs);
  140. return(ifni);
  141. }
  142. void
  143. if_freenameindex(struct if_nameindex *ptr)
  144. {
  145. free(ptr);
  146. }