PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/sources/ruby-1.8.5/ext/socket/extconf.rb

http://rubyworks.googlecode.com/
Ruby | 282 lines | 238 code | 26 blank | 18 comment | 55 complexity | e9bb72c33e5e3699375fe57a498ccdbe MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, AGPL-3.0, 0BSD, Unlicense
  1. require 'mkmf'
  2. case RUBY_PLATFORM
  3. when /bccwin32/
  4. test_func = "WSACleanup"
  5. have_library("ws2_32", "WSACleanup")
  6. when /mswin32|mingw/
  7. test_func = "WSACleanup"
  8. if with_config("winsock2")
  9. have_library("ws2_32", "WSACleanup")
  10. else
  11. have_library("wsock32", "WSACleanup")
  12. end
  13. when /cygwin/
  14. test_func = "socket"
  15. when /beos/
  16. test_func = "socket"
  17. have_library("net", "socket")
  18. have_func("closesocket")
  19. when /i386-os2_emx/
  20. test_func = "socket"
  21. have_library("socket", "socket")
  22. else
  23. test_func = "socket"
  24. have_library("nsl", "t_open")
  25. have_library("socket", "socket")
  26. end
  27. unless $mswin or $bccwin or $mingw
  28. headers = %w<sys/types.h netdb.h string.h sys/socket.h netinet/in.h>
  29. end
  30. if /solaris/ =~ RUBY_PLATFORM and !try_compile("")
  31. # bug of gcc 3.0 on Solaris 8 ?
  32. headers << "sys/feature_tests.h"
  33. end
  34. ipv6 = false
  35. default_ipv6 = /cygwin/ !~ RUBY_PLATFORM
  36. if enable_config("ipv6", default_ipv6)
  37. if checking_for("ipv6") {try_link(<<EOF)}
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. main()
  41. {
  42. socket(AF_INET6, SOCK_STREAM, 0);
  43. }
  44. EOF
  45. $defs << "-DENABLE_IPV6" << "-DINET6"
  46. ipv6 = true
  47. end
  48. end
  49. if ipv6
  50. ipv6lib = nil
  51. class << (fmt = "unknown")
  52. def %(s) s || self end
  53. end
  54. idirs, ldirs = dir_config("inet6", %w[/usr/inet6 /usr/local/v6].find {|d| File.directory?(d)})
  55. checking_for("ipv6 type", fmt) do
  56. if have_macro("IPV6_INRIA_VERSION", "netinet/in.h")
  57. "inria"
  58. elsif have_macro("__KAME__", "netinet/in.h")
  59. have_library(ipv6lib = "inet6")
  60. "kame"
  61. elsif have_macro("_TOSHIBA_INET6", "sys/param.h")
  62. have_library(ipv6lib = "inet6") and "toshiba"
  63. elsif have_macro("__V6D__", "sys/v6config.h")
  64. have_library(ipv6lib = "v6") and "v6d"
  65. elsif have_macro("_ZETA_MINAMI_INET6", "sys/param.h")
  66. have_library(ipv6lib = "inet6") and "zeta"
  67. elsif ipv6lib = with_config("ipv6-lib")
  68. warn <<EOS
  69. --with-ipv6-lib and --with-ipv6-libdir option will be obsolete, use
  70. --with-inet6lib and --with-inet6-{include,lib} options instead.
  71. EOS
  72. find_library(ipv6lib, nil, with_config("ipv6-libdir", ldirs)) and
  73. ipv6lib
  74. elsif have_library("inet6")
  75. "inet6"
  76. end
  77. end or not ipv6lib or abort <<EOS
  78. Fatal: no #{ipv6lib} library found. cannot continue.
  79. You need to fetch lib#{ipv6lib}.a from appropriate
  80. ipv6 kit and compile beforehand.
  81. EOS
  82. end
  83. if have_struct_member("struct sockaddr_in", "sin_len", headers)
  84. $defs[-1] = "-DHAVE_SIN_LEN"
  85. end
  86. # doug's fix, NOW add -Dss_family... only if required!
  87. [nil, " -Dss_family=__ss_family -Dss_len=__ss_len"].each do |flags|
  88. if flags
  89. cppflags = $CPPFLAGS
  90. $CPPFLAGS += flags
  91. end
  92. if have_struct_member("struct sockaddr_storage", "ss_family", headers)
  93. $defs[-1] = "-DHAVE_SOCKADDR_STORAGE"
  94. break
  95. elsif flags
  96. $CPPFLAGS = cppflags
  97. end
  98. end
  99. if have_struct_member("struct sockaddr", "sa_len", headers)
  100. $defs[-1] = "-DHAVE_SA_LEN "
  101. end
  102. have_header("netinet/tcp.h") if not /cygwin/ =~ RUBY_PLATFORM # for cygwin 1.1.5
  103. have_header("netinet/udp.h")
  104. if have_func("sendmsg") | have_func("recvmsg")
  105. have_struct_member('struct msghdr', 'msg_control', ['sys/types.h', 'sys/socket.h'])
  106. have_struct_member('struct msghdr', 'msg_accrights', ['sys/types.h', 'sys/socket.h'])
  107. end
  108. getaddr_info_ok = enable_config("wide-getaddrinfo") do
  109. checking_for("wide getaddrinfo") {try_run(<<EOF)}
  110. #{cpp_include(headers)}
  111. #include <stdlib.h>
  112. #ifndef EXIT_SUCCESS
  113. #define EXIT_SUCCESS 0
  114. #endif
  115. #ifndef EXIT_FAILURE
  116. #define EXIT_FAILURE 1
  117. #endif
  118. #ifndef AF_LOCAL
  119. #define AF_LOCAL AF_UNIX
  120. #endif
  121. main()
  122. {
  123. int passive, gaierr, inet4 = 0, inet6 = 0;
  124. struct addrinfo hints, *ai, *aitop;
  125. char straddr[INET6_ADDRSTRLEN], strport[16];
  126. for (passive = 0; passive <= 1; passive++) {
  127. memset(&hints, 0, sizeof(hints));
  128. hints.ai_family = AF_UNSPEC;
  129. hints.ai_protocol = IPPROTO_TCP;
  130. hints.ai_flags = passive ? AI_PASSIVE : 0;
  131. hints.ai_socktype = SOCK_STREAM;
  132. if ((gaierr = getaddrinfo(NULL, "54321", &hints, &aitop)) != 0) {
  133. (void)gai_strerror(gaierr);
  134. goto bad;
  135. }
  136. for (ai = aitop; ai; ai = ai->ai_next) {
  137. if (ai->ai_family == AF_LOCAL) continue;
  138. if (ai->ai_addr == NULL)
  139. goto bad;
  140. #if defined(_AIX)
  141. ai->ai_addr->sa_len = ai->ai_addrlen;
  142. ai->ai_addr->sa_family = ai->ai_family;
  143. #endif
  144. if (ai->ai_addrlen == 0 ||
  145. getnameinfo(ai->ai_addr, ai->ai_addrlen,
  146. straddr, sizeof(straddr), strport, sizeof(strport),
  147. NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
  148. goto bad;
  149. }
  150. if (strcmp(strport, "54321") != 0) {
  151. goto bad;
  152. }
  153. switch (ai->ai_family) {
  154. case AF_INET:
  155. if (passive) {
  156. if (strcmp(straddr, "0.0.0.0") != 0) {
  157. goto bad;
  158. }
  159. } else {
  160. if (strcmp(straddr, "127.0.0.1") != 0) {
  161. goto bad;
  162. }
  163. }
  164. inet4++;
  165. break;
  166. case AF_INET6:
  167. if (passive) {
  168. if (strcmp(straddr, "::") != 0) {
  169. goto bad;
  170. }
  171. } else {
  172. if (strcmp(straddr, "::1") != 0) {
  173. goto bad;
  174. }
  175. }
  176. inet6++;
  177. break;
  178. case AF_UNSPEC:
  179. goto bad;
  180. break;
  181. default:
  182. /* another family support? */
  183. break;
  184. }
  185. }
  186. }
  187. if (!(inet4 == 0 || inet4 == 2))
  188. goto bad;
  189. if (!(inet6 == 0 || inet6 == 2))
  190. goto bad;
  191. if (aitop)
  192. freeaddrinfo(aitop);
  193. exit(EXIT_SUCCESS);
  194. bad:
  195. if (aitop)
  196. freeaddrinfo(aitop);
  197. exit(EXIT_FAILURE);
  198. }
  199. EOF
  200. end
  201. if ipv6 and not getaddr_info_ok
  202. abort <<EOS
  203. Fatal: --enable-ipv6 is specified, and your OS seems to support IPv6 feature.
  204. But your getaddrinfo() and getnameinfo() are appeared to be broken. Sorry,
  205. you cannot compile IPv6 socket classes with broken these functions.
  206. You can try --enable-wide-getaddrinfo.
  207. EOS
  208. end
  209. case with_config("lookup-order-hack", "UNSPEC")
  210. when "INET"
  211. $defs << "-DLOOKUP_ORDER_HACK_INET"
  212. when "INET6"
  213. $defs << "-DLOOKUP_ORDER_HACK_INET6"
  214. when "UNSPEC"
  215. # nothing special
  216. else
  217. abort <<EOS
  218. Fatal: invalid value for --with-lookup-order-hack (expected INET, INET6 or UNSPEC)
  219. EOS
  220. end
  221. $objs = ["socket.#{$OBJEXT}"]
  222. unless getaddr_info_ok and have_func("getnameinfo", "netdb.h") and have_func("getaddrinfo", "netdb.h")
  223. if have_struct_member("struct in6_addr", "s6_addr8", headers)
  224. $defs[-1] = "-DHAVE_ADDR8"
  225. end
  226. $CPPFLAGS="-I. "+$CPPFLAGS
  227. $objs += ["getaddrinfo.#{$OBJEXT}"]
  228. $objs += ["getnameinfo.#{$OBJEXT}"]
  229. have_func("inet_ntop") or have_func("inet_ntoa")
  230. have_func("inet_pton") or have_func("inet_aton")
  231. have_func("getservbyport")
  232. have_header("arpa/inet.h")
  233. have_header("arpa/nameser.h")
  234. have_header("resolv.h")
  235. end
  236. unless have_type("socklen_t", headers)
  237. $defs << "-Dsocklen_t=int"
  238. end
  239. have_header("sys/un.h")
  240. have_header("sys/uio.h")
  241. if have_func(test_func)
  242. have_func("hsterror")
  243. have_func("getipnodebyname") or have_func("gethostbyname2")
  244. have_func("socketpair")
  245. unless have_func("gethostname")
  246. have_func("uname")
  247. end
  248. if enable_config("socks", ENV["SOCKS_SERVER"])
  249. if have_library("socks5", "SOCKSinit")
  250. $defs << "-DSOCKS5" << "-DSOCKS"
  251. elsif have_library("socks", "Rconnect")
  252. $defs << "-DSOCKS"
  253. end
  254. end
  255. create_makefile("socket")
  256. end