network.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Stig Venaas <venaas@uninett.no> |
  14. | Streams work by Wez Furlong <wez@thebrainroom.com> |
  15. +----------------------------------------------------------------------+
  16. */
  17. /*#define DEBUG_MAIN_NETWORK 1*/
  18. #include "php.h"
  19. #include <stddef.h>
  20. #include <errno.h>
  21. #ifdef PHP_WIN32
  22. # include <Ws2tcpip.h>
  23. # include "win32/inet.h"
  24. # include "win32/winutil.h"
  25. # define O_RDONLY _O_RDONLY
  26. # include "win32/param.h"
  27. #else
  28. #include <sys/param.h>
  29. #endif
  30. #include <sys/types.h>
  31. #if HAVE_SYS_SOCKET_H
  32. #include <sys/socket.h>
  33. #endif
  34. #ifndef _FCNTL_H
  35. #include <fcntl.h>
  36. #endif
  37. #ifdef HAVE_SYS_SELECT_H
  38. #include <sys/select.h>
  39. #endif
  40. #if HAVE_POLL_H
  41. #include <poll.h>
  42. #elif HAVE_SYS_POLL_H
  43. #include <sys/poll.h>
  44. #endif
  45. #ifndef PHP_WIN32
  46. #include <netinet/in.h>
  47. #include <netdb.h>
  48. #if HAVE_ARPA_INET_H
  49. #include <arpa/inet.h>
  50. #endif
  51. #endif
  52. #ifndef HAVE_INET_ATON
  53. int inet_aton(const char *, struct in_addr *);
  54. #endif
  55. #include "php_network.h"
  56. #if defined(PHP_WIN32) || defined(__riscos__)
  57. #undef AF_UNIX
  58. #endif
  59. #if defined(AF_UNIX)
  60. #include <sys/un.h>
  61. #endif
  62. #include "ext/standard/file.h"
  63. #ifdef PHP_WIN32
  64. # include "win32/time.h"
  65. # define SOCK_ERR INVALID_SOCKET
  66. # define SOCK_CONN_ERR SOCKET_ERROR
  67. # define PHP_TIMEOUT_ERROR_VALUE WSAETIMEDOUT
  68. #if HAVE_IPV6
  69. const struct in6_addr in6addr_any = {0}; /* IN6ADDR_ANY_INIT; */
  70. #endif
  71. #else
  72. # define SOCK_ERR -1
  73. # define SOCK_CONN_ERR -1
  74. # define PHP_TIMEOUT_ERROR_VALUE ETIMEDOUT
  75. #endif
  76. #if HAVE_GETADDRINFO
  77. #ifdef HAVE_GAI_STRERROR
  78. # define PHP_GAI_STRERROR(x) (gai_strerror(x))
  79. #else
  80. # define PHP_GAI_STRERROR(x) (php_gai_strerror(x))
  81. /* {{{ php_gai_strerror */
  82. static const char *php_gai_strerror(int code)
  83. {
  84. static struct {
  85. int code;
  86. const char *msg;
  87. } values[] = {
  88. # ifdef EAI_ADDRFAMILY
  89. {EAI_ADDRFAMILY, "Address family for hostname not supported"},
  90. # endif
  91. {EAI_AGAIN, "Temporary failure in name resolution"},
  92. {EAI_BADFLAGS, "Bad value for ai_flags"},
  93. {EAI_FAIL, "Non-recoverable failure in name resolution"},
  94. {EAI_FAMILY, "ai_family not supported"},
  95. {EAI_MEMORY, "Memory allocation failure"},
  96. # ifdef EAI_NODATA
  97. {EAI_NODATA, "No address associated with hostname"},
  98. # endif
  99. {EAI_NONAME, "Name or service not known"},
  100. {EAI_SERVICE, "Servname not supported for ai_socktype"},
  101. {EAI_SOCKTYPE, "ai_socktype not supported"},
  102. {EAI_SYSTEM, "System error"},
  103. {0, NULL}
  104. };
  105. int i;
  106. for (i = 0; values[i].msg != NULL; i++) {
  107. if (values[i].code == code) {
  108. return (char *)values[i].msg;
  109. }
  110. }
  111. return "Unknown error";
  112. }
  113. /* }}} */
  114. #endif
  115. #endif
  116. /* {{{ php_network_freeaddresses */
  117. PHPAPI void php_network_freeaddresses(struct sockaddr **sal)
  118. {
  119. struct sockaddr **sap;
  120. if (sal == NULL)
  121. return;
  122. for (sap = sal; *sap != NULL; sap++)
  123. efree(*sap);
  124. efree(sal);
  125. }
  126. /* }}} */
  127. /* {{{ php_network_getaddresses
  128. * Returns number of addresses, 0 for none/error
  129. */
  130. PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string)
  131. {
  132. struct sockaddr **sap;
  133. int n;
  134. #if HAVE_GETADDRINFO
  135. # if HAVE_IPV6
  136. static int ipv6_borked = -1; /* the way this is used *is* thread safe */
  137. # endif
  138. struct addrinfo hints, *res, *sai;
  139. #else
  140. struct hostent *host_info;
  141. struct in_addr in;
  142. #endif
  143. if (host == NULL) {
  144. return 0;
  145. }
  146. #if HAVE_GETADDRINFO
  147. memset(&hints, '\0', sizeof(hints));
  148. hints.ai_family = AF_INET; /* default to regular inet (see below) */
  149. hints.ai_socktype = socktype;
  150. # if HAVE_IPV6
  151. /* probe for a working IPv6 stack; even if detected as having v6 at compile
  152. * time, at runtime some stacks are slow to resolve or have other issues
  153. * if they are not correctly configured.
  154. * static variable use is safe here since simple store or fetch operations
  155. * are atomic and because the actual probe process is not in danger of
  156. * collisions or race conditions. */
  157. if (ipv6_borked == -1) {
  158. int s;
  159. s = socket(PF_INET6, SOCK_DGRAM, 0);
  160. if (s == SOCK_ERR) {
  161. ipv6_borked = 1;
  162. } else {
  163. ipv6_borked = 0;
  164. closesocket(s);
  165. }
  166. }
  167. hints.ai_family = ipv6_borked ? AF_INET : AF_UNSPEC;
  168. # endif
  169. if ((n = getaddrinfo(host, NULL, &hints, &res))) {
  170. if (error_string) {
  171. /* free error string received during previous iteration (if any) */
  172. if (*error_string) {
  173. zend_string_release_ex(*error_string, 0);
  174. }
  175. *error_string = strpprintf(0, "php_network_getaddresses: getaddrinfo for %s failed: %s", host, PHP_GAI_STRERROR(n));
  176. php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(*error_string));
  177. } else {
  178. php_error_docref(NULL, E_WARNING, "php_network_getaddresses: getaddrinfo for %s failed: %s", host, PHP_GAI_STRERROR(n));
  179. }
  180. return 0;
  181. } else if (res == NULL) {
  182. if (error_string) {
  183. /* free error string received during previous iteration (if any) */
  184. if (*error_string) {
  185. zend_string_release_ex(*error_string, 0);
  186. }
  187. *error_string = strpprintf(0, "php_network_getaddresses: getaddrinfo for %s failed (null result pointer) errno=%d", host, errno);
  188. php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(*error_string));
  189. } else {
  190. php_error_docref(NULL, E_WARNING, "php_network_getaddresses: getaddrinfo for %s failed (null result pointer)", host);
  191. }
  192. return 0;
  193. }
  194. sai = res;
  195. for (n = 1; (sai = sai->ai_next) != NULL; n++)
  196. ;
  197. *sal = safe_emalloc((n + 1), sizeof(*sal), 0);
  198. sai = res;
  199. sap = *sal;
  200. do {
  201. *sap = emalloc(sai->ai_addrlen);
  202. memcpy(*sap, sai->ai_addr, sai->ai_addrlen);
  203. sap++;
  204. } while ((sai = sai->ai_next) != NULL);
  205. freeaddrinfo(res);
  206. #else
  207. #ifdef HAVE_INET_PTON
  208. if (!inet_pton(AF_INET, host, &in)) {
  209. #else
  210. if (!inet_aton(host, &in)) {
  211. #endif
  212. if(strlen(host) > MAXFQDNLEN) {
  213. host_info = NULL;
  214. errno = E2BIG;
  215. } else {
  216. host_info = php_network_gethostbyname(host);
  217. }
  218. if (host_info == NULL) {
  219. if (error_string) {
  220. /* free error string received during previous iteration (if any) */
  221. if (*error_string) {
  222. zend_string_release_ex(*error_string, 0);
  223. }
  224. *error_string = strpprintf(0, "php_network_getaddresses: gethostbyname failed. errno=%d", errno);
  225. php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(*error_string));
  226. } else {
  227. php_error_docref(NULL, E_WARNING, "php_network_getaddresses: gethostbyname failed");
  228. }
  229. return 0;
  230. }
  231. in = *((struct in_addr *) host_info->h_addr);
  232. }
  233. *sal = safe_emalloc(2, sizeof(*sal), 0);
  234. sap = *sal;
  235. *sap = emalloc(sizeof(struct sockaddr_in));
  236. (*sap)->sa_family = AF_INET;
  237. ((struct sockaddr_in *)*sap)->sin_addr = in;
  238. sap++;
  239. n = 1;
  240. #endif
  241. *sap = NULL;
  242. return n;
  243. }
  244. /* }}} */
  245. #ifndef O_NONBLOCK
  246. #define O_NONBLOCK O_NDELAY
  247. #endif
  248. #ifdef PHP_WIN32
  249. typedef u_long php_non_blocking_flags_t;
  250. # define SET_SOCKET_BLOCKING_MODE(sock, save) \
  251. save = TRUE; ioctlsocket(sock, FIONBIO, &save)
  252. # define RESTORE_SOCKET_BLOCKING_MODE(sock, save) \
  253. ioctlsocket(sock, FIONBIO, &save)
  254. #else
  255. typedef int php_non_blocking_flags_t;
  256. # define SET_SOCKET_BLOCKING_MODE(sock, save) \
  257. save = fcntl(sock, F_GETFL, 0); \
  258. fcntl(sock, F_SETFL, save | O_NONBLOCK)
  259. # define RESTORE_SOCKET_BLOCKING_MODE(sock, save) \
  260. fcntl(sock, F_SETFL, save)
  261. #endif
  262. /* Connect to a socket using an interruptible connect with optional timeout.
  263. * Optionally, the connect can be made asynchronously, which will implicitly
  264. * enable non-blocking mode on the socket.
  265. * */
  266. /* {{{ php_network_connect_socket */
  267. PHPAPI int php_network_connect_socket(php_socket_t sockfd,
  268. const struct sockaddr *addr,
  269. socklen_t addrlen,
  270. int asynchronous,
  271. struct timeval *timeout,
  272. zend_string **error_string,
  273. int *error_code)
  274. {
  275. php_non_blocking_flags_t orig_flags;
  276. int n;
  277. int error = 0;
  278. socklen_t len;
  279. int ret = 0;
  280. SET_SOCKET_BLOCKING_MODE(sockfd, orig_flags);
  281. if ((n = connect(sockfd, addr, addrlen)) != 0) {
  282. error = php_socket_errno();
  283. if (error_code) {
  284. *error_code = error;
  285. }
  286. if (error != EINPROGRESS) {
  287. if (error_string) {
  288. *error_string = php_socket_error_str(error);
  289. }
  290. return -1;
  291. }
  292. if (asynchronous && error == EINPROGRESS) {
  293. /* this is fine by us */
  294. return 0;
  295. }
  296. }
  297. if (n == 0) {
  298. goto ok;
  299. }
  300. # ifdef PHP_WIN32
  301. /* The documentation for connect() says in case of non-blocking connections
  302. * the select function reports success in the writefds set and failure in
  303. * the exceptfds set. Indeed, using PHP_POLLREADABLE results in select
  304. * failing only due to the timeout and not immediately as would be
  305. * expected when a connection is actively refused. This way,
  306. * php_pollfd_for will return a mask with POLLOUT if the connection
  307. * is successful and with POLLPRI otherwise. */
  308. if ((n = php_pollfd_for(sockfd, POLLOUT|POLLPRI, timeout)) == 0) {
  309. #else
  310. if ((n = php_pollfd_for(sockfd, PHP_POLLREADABLE|POLLOUT, timeout)) == 0) {
  311. #endif
  312. error = PHP_TIMEOUT_ERROR_VALUE;
  313. }
  314. if (n > 0) {
  315. len = sizeof(error);
  316. /*
  317. BSD-derived systems set errno correctly
  318. Solaris returns -1 from getsockopt in case of error
  319. */
  320. if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char*)&error, &len) != 0) {
  321. ret = -1;
  322. }
  323. } else {
  324. /* whoops: sockfd has disappeared */
  325. ret = -1;
  326. }
  327. ok:
  328. if (!asynchronous) {
  329. /* back to blocking mode */
  330. RESTORE_SOCKET_BLOCKING_MODE(sockfd, orig_flags);
  331. }
  332. if (error_code) {
  333. *error_code = error;
  334. }
  335. if (error) {
  336. ret = -1;
  337. if (error_string) {
  338. *error_string = php_socket_error_str(error);
  339. }
  340. }
  341. return ret;
  342. }
  343. /* }}} */
  344. /* {{{ sub_times */
  345. static inline void sub_times(struct timeval a, struct timeval b, struct timeval *result)
  346. {
  347. result->tv_usec = a.tv_usec - b.tv_usec;
  348. if (result->tv_usec < 0L) {
  349. a.tv_sec--;
  350. result->tv_usec += 1000000L;
  351. }
  352. result->tv_sec = a.tv_sec - b.tv_sec;
  353. if (result->tv_sec < 0L) {
  354. result->tv_sec++;
  355. result->tv_usec -= 1000000L;
  356. }
  357. }
  358. /* }}} */
  359. /* Bind to a local IP address.
  360. * Returns the bound socket, or -1 on failure.
  361. * */
  362. /* {{{ php_network_bind_socket_to_local_addr */
  363. php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
  364. int socktype, long sockopts, zend_string **error_string, int *error_code
  365. )
  366. {
  367. int num_addrs, n, err = 0;
  368. php_socket_t sock;
  369. struct sockaddr **sal, **psal, *sa;
  370. socklen_t socklen;
  371. int sockoptval = 1;
  372. num_addrs = php_network_getaddresses(host, socktype, &psal, error_string);
  373. if (num_addrs == 0) {
  374. /* could not resolve address(es) */
  375. return -1;
  376. }
  377. for (sal = psal; *sal != NULL; sal++) {
  378. sa = *sal;
  379. switch (sa->sa_family) {
  380. #if HAVE_GETADDRINFO && HAVE_IPV6
  381. case AF_INET6:
  382. ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
  383. socklen = sizeof(struct sockaddr_in6);
  384. break;
  385. #endif
  386. case AF_INET:
  387. ((struct sockaddr_in *)sa)->sin_port = htons(port);
  388. socklen = sizeof(struct sockaddr_in);
  389. break;
  390. default:
  391. /* Unsupported family, skip to the next */
  392. continue;
  393. }
  394. /* create a socket for this address */
  395. sock = socket(sa->sa_family, socktype, 0);
  396. if (sock == SOCK_ERR) {
  397. continue;
  398. }
  399. /* attempt to bind */
  400. #ifdef SO_REUSEADDR
  401. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&sockoptval, sizeof(sockoptval));
  402. #endif
  403. #ifdef IPV6_V6ONLY
  404. if (sockopts & STREAM_SOCKOP_IPV6_V6ONLY) {
  405. int ipv6_val = !!(sockopts & STREAM_SOCKOP_IPV6_V6ONLY_ENABLED);
  406. setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&ipv6_val, sizeof(sockoptval));
  407. }
  408. #endif
  409. #ifdef SO_REUSEPORT
  410. if (sockopts & STREAM_SOCKOP_SO_REUSEPORT) {
  411. setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (char*)&sockoptval, sizeof(sockoptval));
  412. }
  413. #endif
  414. #ifdef SO_BROADCAST
  415. if (sockopts & STREAM_SOCKOP_SO_BROADCAST) {
  416. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&sockoptval, sizeof(sockoptval));
  417. }
  418. #endif
  419. #ifdef TCP_NODELAY
  420. if (sockopts & STREAM_SOCKOP_TCP_NODELAY) {
  421. setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&sockoptval, sizeof(sockoptval));
  422. }
  423. #endif
  424. n = bind(sock, sa, socklen);
  425. if (n != SOCK_CONN_ERR) {
  426. goto bound;
  427. }
  428. err = php_socket_errno();
  429. closesocket(sock);
  430. }
  431. sock = -1;
  432. if (error_code) {
  433. *error_code = err;
  434. }
  435. if (error_string) {
  436. *error_string = php_socket_error_str(err);
  437. }
  438. bound:
  439. php_network_freeaddresses(psal);
  440. return sock;
  441. }
  442. /* }}} */
  443. PHPAPI int php_network_parse_network_address_with_port(const char *addr, zend_long addrlen, struct sockaddr *sa, socklen_t *sl)
  444. {
  445. char *colon;
  446. char *tmp;
  447. int ret = FAILURE;
  448. short port;
  449. struct sockaddr_in *in4 = (struct sockaddr_in*)sa;
  450. struct sockaddr **psal;
  451. int n;
  452. zend_string *errstr = NULL;
  453. #if HAVE_IPV6
  454. struct sockaddr_in6 *in6 = (struct sockaddr_in6*)sa;
  455. memset(in6, 0, sizeof(struct sockaddr_in6));
  456. #else
  457. memset(in4, 0, sizeof(struct sockaddr_in));
  458. #endif
  459. if (*addr == '[') {
  460. colon = memchr(addr + 1, ']', addrlen-1);
  461. if (!colon || colon[1] != ':') {
  462. return FAILURE;
  463. }
  464. port = atoi(colon + 2);
  465. addr++;
  466. } else {
  467. colon = memchr(addr, ':', addrlen);
  468. if (!colon) {
  469. return FAILURE;
  470. }
  471. port = atoi(colon + 1);
  472. }
  473. tmp = estrndup(addr, colon - addr);
  474. /* first, try interpreting the address as a numeric address */
  475. #if HAVE_IPV6 && HAVE_INET_PTON
  476. if (inet_pton(AF_INET6, tmp, &in6->sin6_addr) > 0) {
  477. in6->sin6_port = htons(port);
  478. in6->sin6_family = AF_INET6;
  479. *sl = sizeof(struct sockaddr_in6);
  480. ret = SUCCESS;
  481. goto out;
  482. }
  483. #endif
  484. #ifdef HAVE_INET_PTON
  485. if (inet_pton(AF_INET, tmp, &in4->sin_addr) > 0) {
  486. #else
  487. if (inet_aton(tmp, &in4->sin_addr) > 0) {
  488. #endif
  489. in4->sin_port = htons(port);
  490. in4->sin_family = AF_INET;
  491. *sl = sizeof(struct sockaddr_in);
  492. ret = SUCCESS;
  493. goto out;
  494. }
  495. /* looks like we'll need to resolve it */
  496. n = php_network_getaddresses(tmp, SOCK_DGRAM, &psal, &errstr);
  497. if (n == 0) {
  498. if (errstr) {
  499. php_error_docref(NULL, E_WARNING, "Failed to resolve `%s': %s", tmp, ZSTR_VAL(errstr));
  500. zend_string_release_ex(errstr, 0);
  501. }
  502. goto out;
  503. }
  504. /* copy the details from the first item */
  505. switch ((*psal)->sa_family) {
  506. #if HAVE_GETADDRINFO && HAVE_IPV6
  507. case AF_INET6:
  508. *in6 = **(struct sockaddr_in6**)psal;
  509. in6->sin6_port = htons(port);
  510. *sl = sizeof(struct sockaddr_in6);
  511. ret = SUCCESS;
  512. break;
  513. #endif
  514. case AF_INET:
  515. *in4 = **(struct sockaddr_in**)psal;
  516. in4->sin_port = htons(port);
  517. *sl = sizeof(struct sockaddr_in);
  518. ret = SUCCESS;
  519. break;
  520. }
  521. php_network_freeaddresses(psal);
  522. out:
  523. efree(tmp);
  524. return ret;
  525. }
  526. PHPAPI void php_network_populate_name_from_sockaddr(
  527. /* input address */
  528. struct sockaddr *sa, socklen_t sl,
  529. /* output readable address */
  530. zend_string **textaddr,
  531. /* output address */
  532. struct sockaddr **addr,
  533. socklen_t *addrlen
  534. )
  535. {
  536. if (addr) {
  537. *addr = emalloc(sl);
  538. memcpy(*addr, sa, sl);
  539. *addrlen = sl;
  540. }
  541. if (textaddr) {
  542. #ifdef HAVE_INET_NTOP
  543. char abuf[256];
  544. #endif
  545. const char *buf = NULL;
  546. switch (sa->sa_family) {
  547. case AF_INET:
  548. /* generally not thread safe, but it *is* thread safe under win32 */
  549. #ifdef HAVE_INET_NTOP
  550. buf = inet_ntop(AF_INET, &((struct sockaddr_in*)sa)->sin_addr, (char *)&abuf, sizeof(abuf));
  551. #else
  552. buf = inet_ntoa(((struct sockaddr_in*)sa)->sin_addr);
  553. #endif
  554. if (buf) {
  555. *textaddr = strpprintf(0, "%s:%d",
  556. buf, ntohs(((struct sockaddr_in*)sa)->sin_port));
  557. }
  558. break;
  559. #if HAVE_IPV6 && HAVE_INET_NTOP
  560. case AF_INET6:
  561. buf = (char*)inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, (char *)&abuf, sizeof(abuf));
  562. if (buf) {
  563. *textaddr = strpprintf(0, "[%s]:%d",
  564. buf, ntohs(((struct sockaddr_in6*)sa)->sin6_port));
  565. }
  566. break;
  567. #endif
  568. #ifdef AF_UNIX
  569. case AF_UNIX:
  570. {
  571. struct sockaddr_un *ua = (struct sockaddr_un*)sa;
  572. if (ua->sun_path[0] == '\0') {
  573. /* abstract name */
  574. int len = sl - sizeof(sa_family_t);
  575. *textaddr = zend_string_init((char*)ua->sun_path, len, 0);
  576. } else {
  577. int len = strlen(ua->sun_path);
  578. *textaddr = zend_string_init((char*)ua->sun_path, len, 0);
  579. }
  580. }
  581. break;
  582. #endif
  583. }
  584. }
  585. }
  586. PHPAPI int php_network_get_peer_name(php_socket_t sock,
  587. zend_string **textaddr,
  588. struct sockaddr **addr,
  589. socklen_t *addrlen
  590. )
  591. {
  592. php_sockaddr_storage sa;
  593. socklen_t sl = sizeof(sa);
  594. memset(&sa, 0, sizeof(sa));
  595. if (getpeername(sock, (struct sockaddr*)&sa, &sl) == 0) {
  596. php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
  597. textaddr,
  598. addr, addrlen
  599. );
  600. return 0;
  601. }
  602. return -1;
  603. }
  604. PHPAPI int php_network_get_sock_name(php_socket_t sock,
  605. zend_string **textaddr,
  606. struct sockaddr **addr,
  607. socklen_t *addrlen
  608. )
  609. {
  610. php_sockaddr_storage sa;
  611. socklen_t sl = sizeof(sa);
  612. memset(&sa, 0, sizeof(sa));
  613. if (getsockname(sock, (struct sockaddr*)&sa, &sl) == 0) {
  614. php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
  615. textaddr,
  616. addr, addrlen
  617. );
  618. return 0;
  619. }
  620. return -1;
  621. }
  622. /* Accept a client connection from a server socket,
  623. * using an optional timeout.
  624. * Returns the peer address in addr/addrlen (it will emalloc
  625. * these, so be sure to efree the result).
  626. * If you specify textaddr, a text-printable
  627. * version of the address will be emalloc'd and returned.
  628. * */
  629. /* {{{ php_network_accept_incoming */
  630. PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
  631. zend_string **textaddr,
  632. struct sockaddr **addr,
  633. socklen_t *addrlen,
  634. struct timeval *timeout,
  635. zend_string **error_string,
  636. int *error_code,
  637. int tcp_nodelay
  638. )
  639. {
  640. php_socket_t clisock = -1;
  641. int error = 0, n;
  642. php_sockaddr_storage sa;
  643. socklen_t sl;
  644. n = php_pollfd_for(srvsock, PHP_POLLREADABLE, timeout);
  645. if (n == 0) {
  646. error = PHP_TIMEOUT_ERROR_VALUE;
  647. } else if (n == -1) {
  648. error = php_socket_errno();
  649. } else {
  650. sl = sizeof(sa);
  651. clisock = accept(srvsock, (struct sockaddr*)&sa, &sl);
  652. if (clisock != SOCK_ERR) {
  653. php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
  654. textaddr,
  655. addr, addrlen
  656. );
  657. if (tcp_nodelay) {
  658. #ifdef TCP_NODELAY
  659. setsockopt(clisock, IPPROTO_TCP, TCP_NODELAY, (char*)&tcp_nodelay, sizeof(tcp_nodelay));
  660. #endif
  661. }
  662. } else {
  663. error = php_socket_errno();
  664. }
  665. }
  666. if (error_code) {
  667. *error_code = error;
  668. }
  669. if (error_string) {
  670. *error_string = php_socket_error_str(error);
  671. }
  672. return clisock;
  673. }
  674. /* }}} */
  675. /* Connect to a remote host using an interruptible connect with optional timeout.
  676. * Optionally, the connect can be made asynchronously, which will implicitly
  677. * enable non-blocking mode on the socket.
  678. * Returns the connected (or connecting) socket, or -1 on failure.
  679. * */
  680. /* {{{ php_network_connect_socket_to_host */
  681. php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
  682. int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
  683. int *error_code, const char *bindto, unsigned short bindport, long sockopts
  684. )
  685. {
  686. int num_addrs, n, fatal = 0;
  687. php_socket_t sock;
  688. struct sockaddr **sal, **psal, *sa;
  689. struct timeval working_timeout;
  690. socklen_t socklen;
  691. #if HAVE_GETTIMEOFDAY
  692. struct timeval limit_time, time_now;
  693. #endif
  694. num_addrs = php_network_getaddresses(host, socktype, &psal, error_string);
  695. if (num_addrs == 0) {
  696. /* could not resolve address(es) */
  697. return -1;
  698. }
  699. if (timeout) {
  700. memcpy(&working_timeout, timeout, sizeof(working_timeout));
  701. #if HAVE_GETTIMEOFDAY
  702. gettimeofday(&limit_time, NULL);
  703. limit_time.tv_sec += working_timeout.tv_sec;
  704. limit_time.tv_usec += working_timeout.tv_usec;
  705. if (limit_time.tv_usec >= 1000000) {
  706. limit_time.tv_usec -= 1000000;
  707. limit_time.tv_sec++;
  708. }
  709. #endif
  710. }
  711. for (sal = psal; !fatal && *sal != NULL; sal++) {
  712. sa = *sal;
  713. switch (sa->sa_family) {
  714. #if HAVE_GETADDRINFO && HAVE_IPV6
  715. case AF_INET6:
  716. if (!bindto || strchr(bindto, ':')) {
  717. ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
  718. socklen = sizeof(struct sockaddr_in6);
  719. } else {
  720. /* Expect IPV4 address, skip to the next */
  721. continue;
  722. }
  723. break;
  724. #endif
  725. case AF_INET:
  726. ((struct sockaddr_in *)sa)->sin_port = htons(port);
  727. socklen = sizeof(struct sockaddr_in);
  728. if (bindto && strchr(bindto, ':')) {
  729. /* IPV4 sock can not bind to IPV6 address */
  730. bindto = NULL;
  731. }
  732. break;
  733. default:
  734. /* Unsupported family, skip to the next */
  735. continue;
  736. }
  737. /* create a socket for this address */
  738. sock = socket(sa->sa_family, socktype, 0);
  739. if (sock == SOCK_ERR) {
  740. continue;
  741. }
  742. /* make a connection attempt */
  743. if (bindto) {
  744. union {
  745. struct sockaddr common;
  746. struct sockaddr_in in4;
  747. #if HAVE_IPV6 && HAVE_INET_PTON
  748. struct sockaddr_in6 in6;
  749. #endif
  750. } local_address;
  751. int local_address_len = 0;
  752. if (sa->sa_family == AF_INET) {
  753. #ifdef HAVE_INET_PTON
  754. if (inet_pton(AF_INET, bindto, &local_address.in4.sin_addr) == 1) {
  755. #else
  756. if (inet_aton(bindto, &local_address.in4.sin_addr)) {
  757. #endif
  758. local_address_len = sizeof(struct sockaddr_in);
  759. local_address.in4.sin_family = sa->sa_family;
  760. local_address.in4.sin_port = htons(bindport);
  761. memset(&(local_address.in4.sin_zero), 0, sizeof(local_address.in4.sin_zero));
  762. }
  763. }
  764. #if HAVE_IPV6 && HAVE_INET_PTON
  765. else { /* IPV6 */
  766. if (inet_pton(AF_INET6, bindto, &local_address.in6.sin6_addr) == 1) {
  767. local_address_len = sizeof(struct sockaddr_in6);
  768. local_address.in6.sin6_family = sa->sa_family;
  769. local_address.in6.sin6_port = htons(bindport);
  770. }
  771. }
  772. #endif
  773. if (local_address_len == 0) {
  774. php_error_docref(NULL, E_WARNING, "Invalid IP Address: %s", bindto);
  775. } else if (bind(sock, &local_address.common, local_address_len)) {
  776. php_error_docref(NULL, E_WARNING, "Failed to bind to '%s:%d', system said: %s", bindto, bindport, strerror(errno));
  777. }
  778. }
  779. /* free error string received during previous iteration (if any) */
  780. if (error_string && *error_string) {
  781. zend_string_release_ex(*error_string, 0);
  782. *error_string = NULL;
  783. }
  784. #ifdef SO_BROADCAST
  785. {
  786. int val = 1;
  787. if (sockopts & STREAM_SOCKOP_SO_BROADCAST) {
  788. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&val, sizeof(val));
  789. }
  790. }
  791. #endif
  792. #ifdef TCP_NODELAY
  793. {
  794. int val = 1;
  795. if (sockopts & STREAM_SOCKOP_TCP_NODELAY) {
  796. setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&val, sizeof(val));
  797. }
  798. }
  799. #endif
  800. n = php_network_connect_socket(sock, sa, socklen, asynchronous,
  801. timeout ? &working_timeout : NULL,
  802. error_string, error_code);
  803. if (n != -1) {
  804. goto connected;
  805. }
  806. /* adjust timeout for next attempt */
  807. #if HAVE_GETTIMEOFDAY
  808. if (timeout) {
  809. gettimeofday(&time_now, NULL);
  810. if (!timercmp(&time_now, &limit_time, <)) {
  811. /* time limit expired; don't attempt any further connections */
  812. fatal = 1;
  813. } else {
  814. /* work out remaining time */
  815. sub_times(limit_time, time_now, &working_timeout);
  816. }
  817. }
  818. #else
  819. if (error_code && *error_code == PHP_TIMEOUT_ERROR_VALUE) {
  820. /* Don't even bother trying to connect to the next alternative;
  821. * we have no way to determine how long we have already taken
  822. * and it is quite likely that the next attempt will fail too. */
  823. fatal = 1;
  824. } else {
  825. /* re-use the same initial timeout.
  826. * Not the best thing, but in practice it should be good-enough */
  827. if (timeout) {
  828. memcpy(&working_timeout, timeout, sizeof(working_timeout));
  829. }
  830. }
  831. #endif
  832. closesocket(sock);
  833. }
  834. sock = -1;
  835. connected:
  836. php_network_freeaddresses(psal);
  837. return sock;
  838. }
  839. /* }}} */
  840. /* {{{ php_any_addr
  841. * Fills the any (wildcard) address into php_sockaddr_storage
  842. */
  843. PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port)
  844. {
  845. memset(addr, 0, sizeof(php_sockaddr_storage));
  846. switch (family) {
  847. #if HAVE_IPV6
  848. case AF_INET6: {
  849. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
  850. sin6->sin6_family = AF_INET6;
  851. sin6->sin6_port = htons(port);
  852. sin6->sin6_addr = in6addr_any;
  853. break;
  854. }
  855. #endif
  856. case AF_INET: {
  857. struct sockaddr_in *sin = (struct sockaddr_in *) addr;
  858. sin->sin_family = AF_INET;
  859. sin->sin_port = htons(port);
  860. sin->sin_addr.s_addr = htonl(INADDR_ANY);
  861. break;
  862. }
  863. }
  864. }
  865. /* }}} */
  866. /* {{{ php_sockaddr_size
  867. * Returns the size of struct sockaddr_xx for the family
  868. */
  869. PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr)
  870. {
  871. switch (((struct sockaddr *)addr)->sa_family) {
  872. case AF_INET:
  873. return sizeof(struct sockaddr_in);
  874. #if HAVE_IPV6
  875. case AF_INET6:
  876. return sizeof(struct sockaddr_in6);
  877. #endif
  878. #ifdef AF_UNIX
  879. case AF_UNIX:
  880. return sizeof(struct sockaddr_un);
  881. #endif
  882. default:
  883. return 0;
  884. }
  885. }
  886. /* }}} */
  887. /* Given a socket error code, if buf == NULL:
  888. * emallocs storage for the error message and returns
  889. * else
  890. * sprintf message into provided buffer and returns buf
  891. */
  892. /* {{{ php_socket_strerror */
  893. PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize)
  894. {
  895. #ifndef PHP_WIN32
  896. char *errstr;
  897. errstr = strerror(err);
  898. if (buf == NULL) {
  899. buf = estrdup(errstr);
  900. } else {
  901. strncpy(buf, errstr, bufsize);
  902. buf[bufsize?(bufsize-1):0] = 0;
  903. }
  904. return buf;
  905. #else
  906. char *sysbuf = php_win32_error_to_msg(err);
  907. if (!sysbuf[0]) {
  908. sysbuf = "Unknown Error";
  909. }
  910. if (buf == NULL) {
  911. buf = estrdup(sysbuf);
  912. } else {
  913. strncpy(buf, sysbuf, bufsize);
  914. buf[bufsize?(bufsize-1):0] = 0;
  915. }
  916. php_win32_error_msg_free(sysbuf);
  917. return buf;
  918. #endif
  919. }
  920. /* }}} */
  921. /* {{{ php_socket_error_str */
  922. PHPAPI zend_string *php_socket_error_str(long err)
  923. {
  924. #ifndef PHP_WIN32
  925. char *errstr;
  926. errstr = strerror(err);
  927. return zend_string_init(errstr, strlen(errstr), 0);
  928. #else
  929. zend_string *ret;
  930. char *sysbuf = php_win32_error_to_msg(err);
  931. if (!sysbuf[0]) {
  932. sysbuf = "Unknown Error";
  933. }
  934. ret = zend_string_init(sysbuf, strlen(sysbuf), 0);
  935. php_win32_error_msg_free(sysbuf);
  936. return ret;
  937. #endif
  938. }
  939. /* }}} */
  940. /* deprecated */
  941. PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC)
  942. {
  943. php_stream *stream;
  944. php_netstream_data_t *sock;
  945. sock = pemalloc(sizeof(php_netstream_data_t), persistent_id ? 1 : 0);
  946. memset(sock, 0, sizeof(php_netstream_data_t));
  947. sock->is_blocked = 1;
  948. sock->timeout.tv_sec = FG(default_socket_timeout);
  949. sock->timeout.tv_usec = 0;
  950. sock->socket = socket;
  951. stream = php_stream_alloc_rel(&php_stream_generic_socket_ops, sock, persistent_id, "r+");
  952. if (stream == NULL) {
  953. pefree(sock, persistent_id ? 1 : 0);
  954. } else {
  955. stream->flags |= PHP_STREAM_FLAG_AVOID_BLOCKING;
  956. }
  957. return stream;
  958. }
  959. PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
  960. int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC)
  961. {
  962. char *res;
  963. zend_long reslen;
  964. php_stream *stream;
  965. reslen = spprintf(&res, 0, "tcp://%s:%d", host, port);
  966. stream = php_stream_xport_create(res, reslen, REPORT_ERRORS,
  967. STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, persistent_id, timeout, NULL, NULL, NULL);
  968. efree(res);
  969. return stream;
  970. }
  971. PHPAPI int php_set_sock_blocking(php_socket_t socketd, int block)
  972. {
  973. int ret = SUCCESS;
  974. #ifdef PHP_WIN32
  975. u_long flags;
  976. /* with ioctlsocket, a non-zero sets nonblocking, a zero sets blocking */
  977. flags = !block;
  978. if (ioctlsocket(socketd, FIONBIO, &flags) == SOCKET_ERROR) {
  979. ret = FAILURE;
  980. }
  981. #else
  982. int myflag = 0;
  983. int flags = fcntl(socketd, F_GETFL);
  984. #ifdef O_NONBLOCK
  985. myflag = O_NONBLOCK; /* POSIX version */
  986. #elif defined(O_NDELAY)
  987. myflag = O_NDELAY; /* old non-POSIX version */
  988. #endif
  989. if (!block) {
  990. flags |= myflag;
  991. } else {
  992. flags &= ~myflag;
  993. }
  994. if (fcntl(socketd, F_SETFL, flags) == -1) {
  995. ret = FAILURE;
  996. }
  997. #endif
  998. return ret;
  999. }
  1000. PHPAPI void _php_emit_fd_setsize_warning(int max_fd)
  1001. {
  1002. #ifdef PHP_WIN32
  1003. php_error_docref(NULL, E_WARNING,
  1004. "PHP needs to be recompiled with a larger value of FD_SETSIZE.\n"
  1005. "If this binary is from an official www.php.net package, file a bug report\n"
  1006. "at http://bugs.php.net, including the following information:\n"
  1007. "FD_SETSIZE=%d, but you are using %d.\n"
  1008. " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
  1009. "to match to maximum number of sockets each script will work with at\n"
  1010. "one time, in order to avoid seeing this error again at a later date.",
  1011. FD_SETSIZE, max_fd, (max_fd + 128) & ~127);
  1012. #else
  1013. php_error_docref(NULL, E_WARNING,
  1014. "You MUST recompile PHP with a larger value of FD_SETSIZE.\n"
  1015. "It is set to %d, but you have descriptors numbered at least as high as %d.\n"
  1016. " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
  1017. "to equal the maximum number of open files supported by your system,\n"
  1018. "in order to avoid seeing this error again at a later date.",
  1019. FD_SETSIZE, max_fd, (max_fd + 1024) & ~1023);
  1020. #endif
  1021. }
  1022. #if defined(PHP_USE_POLL_2_EMULATION)
  1023. /* emulate poll(2) using select(2), safely. */
  1024. PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout)
  1025. {
  1026. fd_set rset, wset, eset;
  1027. php_socket_t max_fd = SOCK_ERR; /* effectively unused on Windows */
  1028. unsigned int i;
  1029. int n;
  1030. struct timeval tv;
  1031. #ifndef PHP_WIN32
  1032. /* check the highest numbered descriptor */
  1033. for (i = 0; i < nfds; i++) {
  1034. if (ufds[i].fd > max_fd)
  1035. max_fd = ufds[i].fd;
  1036. }
  1037. #endif
  1038. if (!PHP_SAFE_MAX_FD(max_fd, nfds + 1)) {
  1039. #ifdef PHP_WIN32
  1040. WSASetLastError(WSAEINVAL);
  1041. #else
  1042. errno = ERANGE;
  1043. #endif
  1044. return -1;
  1045. }
  1046. FD_ZERO(&rset);
  1047. FD_ZERO(&wset);
  1048. FD_ZERO(&eset);
  1049. for (i = 0; i < nfds; i++) {
  1050. if (ufds[i].events & PHP_POLLREADABLE) {
  1051. PHP_SAFE_FD_SET(ufds[i].fd, &rset);
  1052. }
  1053. if (ufds[i].events & POLLOUT) {
  1054. PHP_SAFE_FD_SET(ufds[i].fd, &wset);
  1055. }
  1056. if (ufds[i].events & POLLPRI) {
  1057. PHP_SAFE_FD_SET(ufds[i].fd, &eset);
  1058. }
  1059. }
  1060. if (timeout >= 0) {
  1061. tv.tv_sec = timeout / 1000;
  1062. tv.tv_usec = (timeout - (tv.tv_sec * 1000)) * 1000;
  1063. }
  1064. /* Resetting/initializing */
  1065. #ifdef PHP_WIN32
  1066. WSASetLastError(0);
  1067. #else
  1068. errno = 0;
  1069. #endif
  1070. n = select(max_fd + 1, &rset, &wset, &eset, timeout >= 0 ? &tv : NULL);
  1071. if (n >= 0) {
  1072. for (i = 0; i < nfds; i++) {
  1073. ufds[i].revents = 0;
  1074. if (PHP_SAFE_FD_ISSET(ufds[i].fd, &rset)) {
  1075. /* could be POLLERR or POLLHUP but can't tell without probing */
  1076. ufds[i].revents |= POLLIN;
  1077. }
  1078. if (PHP_SAFE_FD_ISSET(ufds[i].fd, &wset)) {
  1079. ufds[i].revents |= POLLOUT;
  1080. }
  1081. if (PHP_SAFE_FD_ISSET(ufds[i].fd, &eset)) {
  1082. ufds[i].revents |= POLLPRI;
  1083. }
  1084. }
  1085. }
  1086. return n;
  1087. }
  1088. #endif
  1089. #if defined(HAVE_GETHOSTBYNAME_R)
  1090. #ifdef HAVE_FUNC_GETHOSTBYNAME_R_6
  1091. struct hostent * gethostname_re (const char *host,struct hostent *hostbuf,char **tmphstbuf,size_t *hstbuflen)
  1092. {
  1093. struct hostent *hp;
  1094. int herr,res;
  1095. if (*hstbuflen == 0) {
  1096. *hstbuflen = 1024;
  1097. *tmphstbuf = (char *)malloc (*hstbuflen);
  1098. }
  1099. while (( res =
  1100. gethostbyname_r(host,hostbuf,*tmphstbuf,*hstbuflen,&hp,&herr))
  1101. && (errno == ERANGE)) {
  1102. /* Enlarge the buffer. */
  1103. *hstbuflen *= 2;
  1104. *tmphstbuf = (char *)realloc (*tmphstbuf,*hstbuflen);
  1105. }
  1106. if (res != SUCCESS) {
  1107. return NULL;
  1108. }
  1109. return hp;
  1110. }
  1111. #endif
  1112. #ifdef HAVE_FUNC_GETHOSTBYNAME_R_5
  1113. struct hostent * gethostname_re (const char *host,struct hostent *hostbuf,char **tmphstbuf,size_t *hstbuflen)
  1114. {
  1115. struct hostent *hp;
  1116. int herr;
  1117. if (*hstbuflen == 0) {
  1118. *hstbuflen = 1024;
  1119. *tmphstbuf = (char *)malloc (*hstbuflen);
  1120. }
  1121. while ((NULL == ( hp =
  1122. gethostbyname_r(host,hostbuf,*tmphstbuf,*hstbuflen,&herr)))
  1123. && (errno == ERANGE)) {
  1124. /* Enlarge the buffer. */
  1125. *hstbuflen *= 2;
  1126. *tmphstbuf = (char *)realloc (*tmphstbuf,*hstbuflen);
  1127. }
  1128. return hp;
  1129. }
  1130. #endif
  1131. #ifdef HAVE_FUNC_GETHOSTBYNAME_R_3
  1132. struct hostent * gethostname_re (const char *host,struct hostent *hostbuf,char **tmphstbuf,size_t *hstbuflen)
  1133. {
  1134. if (*hstbuflen == 0) {
  1135. *hstbuflen = sizeof(struct hostent_data);
  1136. *tmphstbuf = (char *)malloc (*hstbuflen);
  1137. } else {
  1138. if (*hstbuflen < sizeof(struct hostent_data)) {
  1139. *hstbuflen = sizeof(struct hostent_data);
  1140. *tmphstbuf = (char *)realloc(*tmphstbuf, *hstbuflen);
  1141. }
  1142. }
  1143. memset((void *)(*tmphstbuf),0,*hstbuflen);
  1144. if (SUCCESS != gethostbyname_r(host,hostbuf,(struct hostent_data *)*tmphstbuf)) {
  1145. return NULL;
  1146. }
  1147. return hostbuf;
  1148. }
  1149. #endif
  1150. #endif
  1151. PHPAPI struct hostent* php_network_gethostbyname(const char *name) {
  1152. #if !defined(HAVE_GETHOSTBYNAME_R)
  1153. return gethostbyname(name);
  1154. #else
  1155. if (FG(tmp_host_buf)) {
  1156. free(FG(tmp_host_buf));
  1157. }
  1158. FG(tmp_host_buf) = NULL;
  1159. FG(tmp_host_buf_len) = 0;
  1160. memset(&FG(tmp_host_info), 0, sizeof(struct hostent));
  1161. return gethostname_re(name, &FG(tmp_host_info), &FG(tmp_host_buf), &FG(tmp_host_buf_len));
  1162. #endif
  1163. }