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

/ruby/ext/socket/extconf.rb

https://github.com/bg/vmsruby
Ruby | 400 lines | 302 code | 30 blank | 68 comment | 58 complexity | 0b37d4e1585198de82405df6c4c383d1 MD5 | raw file
  1. require 'mkmf'
  2. case RUBY_PLATFORM
  3. when /bccwin32/
  4. test_func = "WSACleanup"
  5. have_library("ws2_32", "WSACleanup")
  6. have_func("closesocket")
  7. when /mswin32|mingw/
  8. test_func = "WSACleanup"
  9. have_library("wsock32", "WSACleanup")
  10. have_func("closesocket")
  11. when /cygwin/
  12. test_func = "socket"
  13. when /beos/
  14. test_func = "socket"
  15. have_library("net", "socket")
  16. have_func("closesocket")
  17. when /i386-os2_emx/
  18. test_func = "socket"
  19. have_library("socket", "socket")
  20. else
  21. test_func = "socket"
  22. have_library("nsl", "t_open")
  23. have_library("socket", "socket")
  24. end
  25. $ipv6 = false
  26. default_ipv6 = /cygwin/ !~ RUBY_PLATFORM
  27. if enable_config("ipv6", default_ipv6)
  28. if try_link(<<EOF)
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. main()
  32. {
  33. socket(AF_INET6, SOCK_STREAM, 0);
  34. }
  35. EOF
  36. $CFLAGS+=" -DENABLE_IPV6"
  37. $ipv6 = true
  38. end
  39. end
  40. $ipv6type = nil
  41. $ipv6lib = nil
  42. $ipv6libdir = nil
  43. $ipv6trylibc = nil
  44. if $ipv6
  45. if macro_defined?("IPV6_INRIA_VERSION", <<EOF)
  46. #include <netinet/in.h>
  47. EOF
  48. $ipv6type = "inria"
  49. $CFLAGS="-DINET6 "+$CFLAGS
  50. elsif macro_defined?("__KAME__", <<EOF)
  51. #include <netinet/in.h>
  52. EOF
  53. $ipv6type = "kame"
  54. $ipv6lib="inet6"
  55. $ipv6libdir="/usr/local/v6/lib"
  56. $ipv6trylibc=true
  57. $CFLAGS="-DINET6 "+$CFLAGS
  58. elsif File.directory? "/usr/inet6"
  59. $ipv6type = "linux"
  60. $ipv6lib="inet6"
  61. $ipv6libdir="/usr/inet6/lib"
  62. $CFLAGS="-DINET6 -I/usr/inet6/include "+$CFLAGS
  63. elsif macro_defined?("_TOSHIBA_INET6", <<EOF)
  64. #include <sys/param.h>
  65. EOF
  66. $ipv6type = "toshiba"
  67. $ipv6lib="inet6"
  68. $ipv6libdir="/usr/local/v6/lib"
  69. $CFLAGS="-DINET6 "+$CFLAGS
  70. elsif macro_defined?("__V6D__", <<EOF)
  71. #include </usr/local/v6/include/sys/v6config.h>
  72. EOF
  73. $ipv6type = "v6d"
  74. $ipv6lib="v6"
  75. $ipv6libdir="/usr/local/v6/lib"
  76. $CFLAGS="-DINET6 -I/usr/local/v6/include "+$CFLAGS
  77. elsif macro_defined?("_ZETA_MINAMI_INET6", <<EOF)
  78. #include <sys/param.h>
  79. EOF
  80. $ipv6type = "zeta"
  81. $ipv6lib="inet6"
  82. $ipv6libdir="/usr/local/v6/lib"
  83. $CFLAGS="-DINET6 "+$CFLAGS
  84. else
  85. $ipv6lib=with_config("ipv6-lib", nil)
  86. $ipv6libdir=with_config("ipv6-libdir", nil)
  87. $CFLAGS="-DINET6 "+$CFLAGS
  88. end
  89. if $ipv6lib
  90. if File.directory? $ipv6libdir and File.exist? "#{$ipv6libdir}/lib#{$ipv6lib}.a"
  91. $LOCAL_LIBS = " -L#$ipv6libdir -l#$ipv6lib"
  92. elsif !$ipv6trylibc
  93. print <<EOS
  94. Fatal: no #$ipv6lib library found. cannot continue.
  95. You need to fetch lib#{$ipv6lib}.a from appropriate
  96. ipv6 kit and compile beforehand.
  97. EOS
  98. exit
  99. end
  100. end
  101. end
  102. if try_link(<<EOF)
  103. #ifdef _WIN32
  104. # include <windows.h>
  105. # include <winsock.h>
  106. #else
  107. # include <sys/types.h>
  108. # include <netdb.h>
  109. # include <string.h>
  110. # include <sys/socket.h>
  111. # include <netinet/in.h>
  112. #endif
  113. int
  114. main()
  115. {
  116. struct sockaddr_in sin;
  117. sin.sin_len;
  118. return 0;
  119. }
  120. EOF
  121. $CFLAGS="-DHAVE_SIN_LEN "+$CFLAGS
  122. end
  123. if try_link(<<EOF)
  124. #ifdef _WIN32
  125. # include <windows.h>
  126. # include <winsock.h>
  127. #else
  128. # include <sys/types.h>
  129. # include <netdb.h>
  130. # include <string.h>
  131. # include <sys/socket.h>
  132. #endif
  133. int
  134. main()
  135. {
  136. struct sockaddr_storage ss;
  137. ss.ss_family;
  138. return 0;
  139. }
  140. EOF
  141. $CFLAGS="-DHAVE_SOCKADDR_STORAGE "+$CFLAGS
  142. else # doug's fix, NOW add -Dss_family... only if required!
  143. $CPPFLAGS += " -Dss_family=__ss_family -Dss_len=__ss_len"
  144. if try_link(<<EOF)
  145. #ifdef _WIN32
  146. # include <windows.h>
  147. # include <winsock.h>
  148. #else
  149. # include <sys/types.h>
  150. # include <netdb.h>
  151. # include <string.h>
  152. # include <sys/socket.h>
  153. #endif
  154. int
  155. main()
  156. {
  157. struct sockaddr_storage ss;
  158. ss.ss_family;
  159. return 0;
  160. }
  161. EOF
  162. $CFLAGS="-DHAVE_SOCKADDR_STORAGE "+$CFLAGS
  163. end
  164. end
  165. if try_link(<<EOF)
  166. #include <sys/types.h>
  167. #include <netdb.h>
  168. #include <string.h>
  169. #include <sys/socket.h>
  170. #include <netinet/in.h>
  171. int
  172. main()
  173. {
  174. struct sockaddr sa;
  175. sa.sa_len;
  176. return 0;
  177. }
  178. EOF
  179. $CFLAGS="-DHAVE_SA_LEN "+$CFLAGS
  180. end
  181. have_header("netinet/tcp.h") if not /cygwin/ =~ RUBY_PLATFORM # for cygwin 1.1.5
  182. have_header("netinet/udp.h")
  183. if have_func("sendmsg") | have_func("recvmsg")
  184. have_struct_member('struct msghdr', 'msg_control', ['sys/types.h', 'sys/socket.h'])
  185. have_struct_member('struct msghdr', 'msg_accrights', ['sys/types.h', 'sys/socket.h'])
  186. end
  187. $getaddr_info_ok = false
  188. if !enable_config("wide-getaddrinfo", false) and try_run(<<EOF)
  189. #include <sys/types.h>
  190. #include <netdb.h>
  191. #include <string.h>
  192. #include <sys/socket.h>
  193. #include <netinet/in.h>
  194. #ifndef AF_LOCAL
  195. #define AF_LOCAL AF_UNIX
  196. #endif
  197. main()
  198. {
  199. int passive, gaierr, inet4 = 0, inet6 = 0;
  200. struct addrinfo hints, *ai, *aitop;
  201. char straddr[INET6_ADDRSTRLEN], strport[16];
  202. for (passive = 0; passive <= 1; passive++) {
  203. memset(&hints, 0, sizeof(hints));
  204. hints.ai_family = AF_UNSPEC;
  205. hints.ai_protocol = IPPROTO_TCP;
  206. hints.ai_flags = passive ? AI_PASSIVE : 0;
  207. hints.ai_socktype = SOCK_STREAM;
  208. if ((gaierr = getaddrinfo(NULL, "54321", &hints, &aitop)) != 0) {
  209. (void)gai_strerror(gaierr);
  210. goto bad;
  211. }
  212. for (ai = aitop; ai; ai = ai->ai_next) {
  213. if (ai->ai_family == AF_LOCAL) continue;
  214. if (ai->ai_addr == NULL ||
  215. ai->ai_addrlen == 0 ||
  216. getnameinfo(ai->ai_addr, ai->ai_addrlen,
  217. straddr, sizeof(straddr), strport, sizeof(strport),
  218. NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
  219. goto bad;
  220. }
  221. if (strcmp(strport, "54321") != 0) {
  222. goto bad;
  223. }
  224. switch (ai->ai_family) {
  225. case AF_INET:
  226. if (passive) {
  227. if (strcmp(straddr, "0.0.0.0") != 0) {
  228. goto bad;
  229. }
  230. } else {
  231. if (strcmp(straddr, "127.0.0.1") != 0) {
  232. goto bad;
  233. }
  234. }
  235. inet4++;
  236. break;
  237. case AF_INET6:
  238. if (passive) {
  239. if (strcmp(straddr, "::") != 0) {
  240. goto bad;
  241. }
  242. } else {
  243. if (strcmp(straddr, "::1") != 0) {
  244. goto bad;
  245. }
  246. }
  247. inet6++;
  248. break;
  249. case AF_UNSPEC:
  250. goto bad;
  251. break;
  252. default:
  253. /* another family support? */
  254. break;
  255. }
  256. }
  257. }
  258. if (!(inet4 == 0 || inet4 == 2))
  259. goto bad;
  260. if (!(inet6 == 0 || inet6 == 2))
  261. goto bad;
  262. if (aitop)
  263. freeaddrinfo(aitop);
  264. exit(0);
  265. bad:
  266. if (aitop)
  267. freeaddrinfo(aitop);
  268. exit(1);
  269. }
  270. EOF
  271. $getaddr_info_ok = true
  272. end
  273. if $ipv6 and not $getaddr_info_ok
  274. print <<EOS
  275. Fatal: --enable-ipv6 is specified, and your OS seems to support IPv6 feature.
  276. But your getaddrinfo() and getnameinfo() are appeared to be broken. Sorry,
  277. you cannot compile IPv6 socket classes with broken these functions.
  278. You can try --enable-wide-getaddrinfo.
  279. EOS
  280. exit
  281. end
  282. case with_config("lookup-order-hack", "UNSPEC")
  283. when "INET"
  284. $CFLAGS="-DLOOKUP_ORDER_HACK_INET "+$CFLAGS
  285. when "INET6"
  286. $CFLAGS="-DLOOKUP_ORDER_HACK_INET6 "+$CFLAGS
  287. when "UNSPEC"
  288. # nothing special
  289. else
  290. print <<EOS
  291. Fatal: invalid value for --with-lookup-order-hack (expected INET, INET6 or UNSPEC)
  292. EOS
  293. exit
  294. end
  295. $objs = ["socket.#{$OBJEXT}"]
  296. if $getaddr_info_ok and have_func("getaddrinfo", "netdb.h") and have_func("getnameinfo", "netdb.h")
  297. have_getaddrinfo = true
  298. else
  299. if try_link(<<EOF)
  300. #ifndef _WIN32
  301. # include <sys/types.h>
  302. # include <netdb.h>
  303. # include <string.h>
  304. # include <sys/socket.h>
  305. # include <netinet/in.h>
  306. #else
  307. # include <windows.h>
  308. # ifdef _WIN32_WCE
  309. # include <winsock.h>
  310. # else
  311. # include <winsock.h>
  312. # endif
  313. #endif
  314. int
  315. main()
  316. {
  317. struct in6_addr addr;
  318. unsigned char c;
  319. c = addr.s6_addr8;
  320. return 0;
  321. }
  322. EOF
  323. $CFLAGS="-DHAVE_ADDR8 "+$CFLAGS
  324. end
  325. end
  326. if have_getaddrinfo
  327. $CFLAGS="-DHAVE_GETADDRINFO "+$CFLAGS
  328. else
  329. $CFLAGS="-I. "+$CFLAGS
  330. $objs += ["getaddrinfo.#{$OBJEXT}"]
  331. $objs += ["getnameinfo.#{$OBJEXT}"]
  332. have_func("inet_ntop") or have_func("inet_ntoa")
  333. have_func("inet_pton") or have_func("inet_aton")
  334. have_func("getservbyport")
  335. have_header("arpa/inet.h")
  336. have_header("arpa/nameser.h")
  337. have_header("resolv.h")
  338. end
  339. if !try_link(<<EOF)
  340. #include <sys/types.h>
  341. #include <netdb.h>
  342. #include <string.h>
  343. #include <sys/socket.h>
  344. #include <netinet/in.h>
  345. int
  346. main()
  347. {
  348. socklen_t len;
  349. return 0;
  350. }
  351. EOF
  352. $CFLAGS="-Dsocklen_t=int "+$CFLAGS
  353. end
  354. have_header("sys/un.h")
  355. have_header("sys/uio.h")
  356. if have_func(test_func)
  357. have_func("hsterror")
  358. have_func("getipnodebyname") or have_func("gethostbyname2")
  359. unless have_func("gethostname")
  360. have_func("uname")
  361. end
  362. if enable_config("socks", ENV["SOCKS_SERVER"])
  363. if have_library("socks5", "SOCKSinit")
  364. $CFLAGS+=" -DSOCKS5 -DSOCKS"
  365. elsif have_library("socks", "Rconnect")
  366. $CFLAGS+=" -DSOCKS"
  367. end
  368. end
  369. create_makefile("socket")
  370. end