opensock.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (C) 1999-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <sys/socket.h>
  20. /* Return a socket of any type. The socket can be used in subsequent
  21. ioctl calls to talk to the kernel. */
  22. int
  23. __opensock (void)
  24. {
  25. static int last_family; /* Available socket family we will use. */
  26. static int last_type;
  27. static const struct
  28. {
  29. int family;
  30. const char procname[15];
  31. } afs[] =
  32. {
  33. { AF_UNIX, "net/unix" },
  34. { AF_INET, "" },
  35. { AF_INET6, "net/if_inet6" },
  36. { AF_AX25, "net/ax25" },
  37. { AF_NETROM, "net/nr" },
  38. { AF_ROSE, "net/rose" },
  39. { AF_IPX, "net/ipx" },
  40. { AF_APPLETALK, "net/appletalk" },
  41. { AF_ECONET, "sys/net/econet" },
  42. { AF_ASH, "sys/net/ash" },
  43. { AF_X25, "net/x25" },
  44. #ifdef NEED_AF_IUCV
  45. { AF_IUCV, "net/iucv" }
  46. #endif
  47. };
  48. #define nafs (sizeof (afs) / sizeof (afs[0]))
  49. char fname[sizeof "/proc/" + 14];
  50. int result;
  51. int has_proc;
  52. size_t cnt;
  53. /* We already know which family to use from the last call. Use it
  54. again. */
  55. if (last_family != 0)
  56. {
  57. assert (last_type != 0);
  58. result = __socket (last_family, last_type | SOCK_CLOEXEC, 0);
  59. if (result != -1 || errno != EAFNOSUPPORT)
  60. /* Maybe the socket type isn't supported anymore (module is
  61. unloaded). In this case again try to find the type. */
  62. return result;
  63. /* Reset the values. They seem not valid anymore. */
  64. last_family = 0;
  65. last_type = 0;
  66. }
  67. /* Check whether the /proc filesystem is available. */
  68. has_proc = __access ("/proc/net", R_OK) != -1;
  69. strcpy (fname, "/proc/");
  70. /* Iterate over the interface families and find one which is
  71. available. */
  72. for (cnt = 0; cnt < nafs; ++cnt)
  73. {
  74. int type = SOCK_DGRAM;
  75. if (has_proc && afs[cnt].procname[0] != '\0')
  76. {
  77. strcpy (fname + 6, afs[cnt].procname);
  78. if (__access (fname, R_OK) == -1)
  79. /* The /proc entry is not available. I.e., we cannot
  80. create a socket of this type (without loading the
  81. module). Don't look for it since this might trigger
  82. loading the module. */
  83. continue;
  84. }
  85. if (afs[cnt].family == AF_NETROM || afs[cnt].family == AF_X25)
  86. type = SOCK_SEQPACKET;
  87. result = __socket (afs[cnt].family, type | SOCK_CLOEXEC, 0);
  88. if (result != -1)
  89. {
  90. /* Found an available family. */
  91. last_type = type;
  92. last_family = afs[cnt].family;
  93. return result;
  94. }
  95. }
  96. /* None of the protocol families is available. It is unclear what kind
  97. of error is returned. ENOENT seems like a reasonable choice. */
  98. __set_errno (ENOENT);
  99. return -1;
  100. }