sockets.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714
  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. | Authors: Chris Vandomelen <chrisv@b0rked.dhs.org> |
  14. | Sterling Hughes <sterling@php.net> |
  15. | Jason Greene <jason@php.net> |
  16. | Gustavo Lopes <cataphract@php.net> |
  17. | WinSock: Daniel Beulshausen <daniel@php4win.de> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_network.h"
  25. #include "ext/standard/file.h"
  26. #include "ext/standard/info.h"
  27. #include "php_ini.h"
  28. #ifdef PHP_WIN32
  29. # include "windows_common.h"
  30. # include <win32/inet.h>
  31. # include <windows.h>
  32. # include <Ws2tcpip.h>
  33. # include "php_sockets.h"
  34. # include <win32/sockets.h>
  35. # include <win32/winutil.h>
  36. #else
  37. # include <sys/types.h>
  38. # include <sys/socket.h>
  39. # include <netdb.h>
  40. # include <netinet/in.h>
  41. # include <netinet/tcp.h>
  42. # include <sys/un.h>
  43. # include <arpa/inet.h>
  44. # include <sys/time.h>
  45. # include <unistd.h>
  46. # include <errno.h>
  47. # include <fcntl.h>
  48. # include <signal.h>
  49. # include <sys/uio.h>
  50. # define set_errno(a) (errno = a)
  51. # include "php_sockets.h"
  52. # if HAVE_IF_NAMETOINDEX
  53. # include <net/if.h>
  54. # endif
  55. #endif
  56. #include <stddef.h>
  57. #include "sockaddr_conv.h"
  58. #include "multicast.h"
  59. #include "sendrecvmsg.h"
  60. #include "sockets_arginfo.h"
  61. ZEND_DECLARE_MODULE_GLOBALS(sockets)
  62. #ifndef MSG_WAITALL
  63. #ifdef LINUX
  64. #define MSG_WAITALL 0x00000100
  65. #else
  66. #define MSG_WAITALL 0x00000000
  67. #endif
  68. #endif
  69. #ifndef MSG_EOF
  70. #ifdef MSG_FIN
  71. #define MSG_EOF MSG_FIN
  72. #endif
  73. #endif
  74. #ifndef SUN_LEN
  75. #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
  76. #endif
  77. #ifndef PF_INET
  78. #define PF_INET AF_INET
  79. #endif
  80. #define PHP_NORMAL_READ 0x0001
  81. #define PHP_BINARY_READ 0x0002
  82. static PHP_GINIT_FUNCTION(sockets);
  83. static PHP_GSHUTDOWN_FUNCTION(sockets);
  84. static PHP_MINIT_FUNCTION(sockets);
  85. static PHP_MSHUTDOWN_FUNCTION(sockets);
  86. static PHP_MINFO_FUNCTION(sockets);
  87. static PHP_RSHUTDOWN_FUNCTION(sockets);
  88. /* Socket class */
  89. zend_class_entry *socket_ce;
  90. static zend_object_handlers socket_object_handlers;
  91. static zend_object *socket_create_object(zend_class_entry *class_type) {
  92. php_socket *intern = zend_object_alloc(sizeof(php_socket), class_type);
  93. zend_object_std_init(&intern->std, class_type);
  94. object_properties_init(&intern->std, class_type);
  95. intern->std.handlers = &socket_object_handlers;
  96. intern->bsd_socket = -1; /* invalid socket */
  97. intern->type = PF_UNSPEC;
  98. intern->error = 0;
  99. intern->blocking = 1;
  100. ZVAL_UNDEF(&intern->zstream);
  101. return &intern->std;
  102. }
  103. static zend_function *socket_get_constructor(zend_object *object) {
  104. zend_throw_error(NULL, "Cannot directly construct Socket, use socket_create() instead");
  105. return NULL;
  106. }
  107. static void socket_free_obj(zend_object *object)
  108. {
  109. php_socket *socket = socket_from_obj(object);
  110. if (Z_ISUNDEF(socket->zstream)) {
  111. if (!IS_INVALID_SOCKET(socket)) {
  112. close(socket->bsd_socket);
  113. }
  114. } else {
  115. zval_ptr_dtor(&socket->zstream);
  116. }
  117. zend_object_std_dtor(&socket->std);
  118. }
  119. static HashTable *socket_get_gc(zend_object *object, zval **table, int *n)
  120. {
  121. php_socket *socket = socket_from_obj(object);
  122. *table = &socket->zstream;
  123. *n = 1;
  124. return zend_std_get_properties(object);
  125. }
  126. /* AddressInfo class */
  127. typedef struct {
  128. struct addrinfo addrinfo;
  129. zend_object std;
  130. } php_addrinfo;
  131. zend_class_entry *address_info_ce;
  132. static zend_object_handlers address_info_object_handlers;
  133. static inline php_addrinfo *address_info_from_obj(zend_object *obj) {
  134. return (php_addrinfo *)((char *)(obj) - XtOffsetOf(php_addrinfo, std));
  135. }
  136. #define Z_ADDRESS_INFO_P(zv) address_info_from_obj(Z_OBJ_P(zv))
  137. static zend_object *address_info_create_object(zend_class_entry *class_type) {
  138. php_addrinfo *intern = zend_object_alloc(sizeof(php_addrinfo), class_type);
  139. zend_object_std_init(&intern->std, class_type);
  140. object_properties_init(&intern->std, class_type);
  141. intern->std.handlers = &address_info_object_handlers;
  142. return &intern->std;
  143. }
  144. static zend_function *address_info_get_constructor(zend_object *object) {
  145. zend_throw_error(NULL, "Cannot directly construct AddressInfo, use socket_addrinfo_lookup() instead");
  146. return NULL;
  147. }
  148. static void address_info_free_obj(zend_object *object)
  149. {
  150. php_addrinfo *address_info = address_info_from_obj(object);
  151. if (address_info->addrinfo.ai_canonname != NULL) {
  152. efree(address_info->addrinfo.ai_canonname);
  153. }
  154. efree(address_info->addrinfo.ai_addr);
  155. zend_object_std_dtor(&address_info->std);
  156. }
  157. /* Module registration */
  158. zend_module_entry sockets_module_entry = {
  159. STANDARD_MODULE_HEADER,
  160. "sockets",
  161. ext_functions,
  162. PHP_MINIT(sockets),
  163. PHP_MSHUTDOWN(sockets),
  164. NULL,
  165. PHP_RSHUTDOWN(sockets),
  166. PHP_MINFO(sockets),
  167. PHP_SOCKETS_VERSION,
  168. PHP_MODULE_GLOBALS(sockets),
  169. PHP_GINIT(sockets),
  170. PHP_GSHUTDOWN(sockets),
  171. NULL,
  172. STANDARD_MODULE_PROPERTIES_EX
  173. };
  174. #ifdef COMPILE_DL_SOCKETS
  175. #ifdef ZTS
  176. ZEND_TSRMLS_CACHE_DEFINE()
  177. #endif
  178. ZEND_GET_MODULE(sockets)
  179. #endif
  180. #ifndef HAVE_INET_NTOP
  181. /* inet_ntop should be used instead of inet_ntoa */
  182. int inet_ntoa_lock = 0;
  183. #endif
  184. static int php_open_listen_sock(php_socket *sock, int port, int backlog) /* {{{ */
  185. {
  186. struct sockaddr_in la;
  187. struct hostent *hp;
  188. #ifndef PHP_WIN32
  189. if ((hp = php_network_gethostbyname("0.0.0.0")) == NULL) {
  190. #else
  191. if ((hp = php_network_gethostbyname("localhost")) == NULL) {
  192. #endif
  193. return 0;
  194. }
  195. memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
  196. la.sin_family = hp->h_addrtype;
  197. la.sin_port = htons((unsigned short) port);
  198. sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
  199. sock->blocking = 1;
  200. if (IS_INVALID_SOCKET(sock)) {
  201. PHP_SOCKET_ERROR(sock, "unable to create listening socket", errno);
  202. return 0;
  203. }
  204. sock->type = PF_INET;
  205. if (bind(sock->bsd_socket, (struct sockaddr *)&la, sizeof(la)) != 0) {
  206. PHP_SOCKET_ERROR(sock, "unable to bind to given address", errno);
  207. close(sock->bsd_socket);
  208. return 0;
  209. }
  210. if (listen(sock->bsd_socket, backlog) != 0) {
  211. PHP_SOCKET_ERROR(sock, "unable to listen on socket", errno);
  212. close(sock->bsd_socket);
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. /* }}} */
  218. static int php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct sockaddr *la, socklen_t *la_len) /* {{{ */
  219. {
  220. out_sock->bsd_socket = accept(in_sock->bsd_socket, la, la_len);
  221. if (IS_INVALID_SOCKET(out_sock)) {
  222. PHP_SOCKET_ERROR(out_sock, "unable to accept incoming connection", errno);
  223. return 0;
  224. }
  225. out_sock->error = 0;
  226. out_sock->blocking = 1;
  227. out_sock->type = la->sa_family;
  228. return 1;
  229. }
  230. /* }}} */
  231. /* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */
  232. static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags)
  233. {
  234. int m = 0;
  235. size_t n = 0;
  236. int no_read = 0;
  237. int nonblock = 0;
  238. char *t = (char *) buf;
  239. #ifndef PHP_WIN32
  240. m = fcntl(sock->bsd_socket, F_GETFL);
  241. if (m < 0) {
  242. return m;
  243. }
  244. nonblock = (m & O_NONBLOCK);
  245. m = 0;
  246. #else
  247. nonblock = !sock->blocking;
  248. #endif
  249. set_errno(0);
  250. *t = '\0';
  251. while (*t != '\n' && *t != '\r' && n < maxlen) {
  252. if (m > 0) {
  253. t++;
  254. n++;
  255. } else if (m == 0) {
  256. no_read++;
  257. if (nonblock && no_read >= 2) {
  258. return n;
  259. /* The first pass, m always is 0, so no_read becomes 1
  260. * in the first pass. no_read becomes 2 in the second pass,
  261. * and if this is nonblocking, we should return.. */
  262. }
  263. if (no_read > 200) {
  264. set_errno(ECONNRESET);
  265. return -1;
  266. }
  267. }
  268. if (n < maxlen) {
  269. m = recv(sock->bsd_socket, (void *) t, 1, flags);
  270. }
  271. if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
  272. return -1;
  273. }
  274. set_errno(0);
  275. }
  276. if (n < maxlen) {
  277. n++;
  278. /* The only reasons it makes it to here is
  279. * if '\n' or '\r' are encountered. So, increase
  280. * the return by 1 to make up for the lack of the
  281. * '\n' or '\r' in the count (since read() takes
  282. * place at the end of the loop..) */
  283. }
  284. return n;
  285. }
  286. /* }}} */
  287. char *sockets_strerror(int error) /* {{{ */
  288. {
  289. const char *buf;
  290. #ifndef PHP_WIN32
  291. if (error < -10000) {
  292. error = -error - 10000;
  293. #ifdef HAVE_HSTRERROR
  294. buf = hstrerror(error);
  295. #else
  296. {
  297. if (SOCKETS_G(strerror_buf)) {
  298. efree(SOCKETS_G(strerror_buf));
  299. }
  300. spprintf(&(SOCKETS_G(strerror_buf)), 0, "Host lookup error %d", error);
  301. buf = SOCKETS_G(strerror_buf);
  302. }
  303. #endif
  304. } else {
  305. buf = strerror(error);
  306. }
  307. #else
  308. {
  309. char *tmp = php_win32_error_to_msg(error);
  310. buf = NULL;
  311. if (tmp[0]) {
  312. if (SOCKETS_G(strerror_buf)) {
  313. efree(SOCKETS_G(strerror_buf));
  314. }
  315. SOCKETS_G(strerror_buf) = estrdup(tmp);
  316. free(tmp);
  317. buf = SOCKETS_G(strerror_buf);
  318. }
  319. }
  320. #endif
  321. return (buf ? (char *) buf : "");
  322. }
  323. /* }}} */
  324. #ifdef PHP_WIN32
  325. static void sockets_destroy_wsa_info(zval *data)
  326. {/*{{{*/
  327. HANDLE h = (HANDLE)Z_PTR_P(data);
  328. CloseHandle(h);
  329. }/*}}}*/
  330. #endif
  331. /* {{{ PHP_GINIT_FUNCTION */
  332. static PHP_GINIT_FUNCTION(sockets)
  333. {
  334. #if defined(COMPILE_DL_SOCKETS) && defined(ZTS)
  335. ZEND_TSRMLS_CACHE_UPDATE();
  336. #endif
  337. sockets_globals->last_error = 0;
  338. sockets_globals->strerror_buf = NULL;
  339. #ifdef PHP_WIN32
  340. sockets_globals->wsa_child_count = 0;
  341. zend_hash_init(&sockets_globals->wsa_info, 0, NULL, sockets_destroy_wsa_info, 1);
  342. #endif
  343. }
  344. /* }}} */
  345. /* {{{ PHP_GSHUTDOWN_FUNCTION */
  346. static PHP_GSHUTDOWN_FUNCTION(sockets)
  347. {
  348. #ifdef PHP_WIN32
  349. zend_hash_destroy(&sockets_globals->wsa_info);
  350. #endif
  351. }
  352. /* }}} */
  353. /* {{{ PHP_MINIT_FUNCTION */
  354. static PHP_MINIT_FUNCTION(sockets)
  355. {
  356. #if defined(COMPILE_DL_SOCKETS) && defined(ZTS)
  357. ZEND_TSRMLS_CACHE_UPDATE();
  358. #endif
  359. socket_ce = register_class_Socket();
  360. socket_ce->create_object = socket_create_object;
  361. memcpy(&socket_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  362. socket_object_handlers.offset = XtOffsetOf(php_socket, std);
  363. socket_object_handlers.free_obj = socket_free_obj;
  364. socket_object_handlers.get_constructor = socket_get_constructor;
  365. socket_object_handlers.clone_obj = NULL;
  366. socket_object_handlers.get_gc = socket_get_gc;
  367. socket_object_handlers.compare = zend_objects_not_comparable;
  368. address_info_ce = register_class_AddressInfo();
  369. address_info_ce->create_object = address_info_create_object;
  370. memcpy(&address_info_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  371. address_info_object_handlers.offset = XtOffsetOf(php_addrinfo, std);
  372. address_info_object_handlers.free_obj = address_info_free_obj;
  373. address_info_object_handlers.get_constructor = address_info_get_constructor;
  374. address_info_object_handlers.clone_obj = NULL;
  375. address_info_object_handlers.compare = zend_objects_not_comparable;
  376. REGISTER_LONG_CONSTANT("AF_UNIX", AF_UNIX, CONST_CS | CONST_PERSISTENT);
  377. REGISTER_LONG_CONSTANT("AF_INET", AF_INET, CONST_CS | CONST_PERSISTENT);
  378. #if HAVE_IPV6
  379. REGISTER_LONG_CONSTANT("AF_INET6", AF_INET6, CONST_CS | CONST_PERSISTENT);
  380. #endif
  381. REGISTER_LONG_CONSTANT("SOCK_STREAM", SOCK_STREAM, CONST_CS | CONST_PERSISTENT);
  382. REGISTER_LONG_CONSTANT("SOCK_DGRAM", SOCK_DGRAM, CONST_CS | CONST_PERSISTENT);
  383. REGISTER_LONG_CONSTANT("SOCK_RAW", SOCK_RAW, CONST_CS | CONST_PERSISTENT);
  384. REGISTER_LONG_CONSTANT("SOCK_SEQPACKET",SOCK_SEQPACKET, CONST_CS | CONST_PERSISTENT);
  385. #ifdef SOCK_RDM
  386. REGISTER_LONG_CONSTANT("SOCK_RDM", SOCK_RDM, CONST_CS | CONST_PERSISTENT);
  387. #endif
  388. REGISTER_LONG_CONSTANT("MSG_OOB", MSG_OOB, CONST_CS | CONST_PERSISTENT);
  389. REGISTER_LONG_CONSTANT("MSG_WAITALL", MSG_WAITALL, CONST_CS | CONST_PERSISTENT);
  390. REGISTER_LONG_CONSTANT("MSG_CTRUNC", MSG_CTRUNC, CONST_CS | CONST_PERSISTENT);
  391. REGISTER_LONG_CONSTANT("MSG_TRUNC", MSG_TRUNC, CONST_CS | CONST_PERSISTENT);
  392. REGISTER_LONG_CONSTANT("MSG_PEEK", MSG_PEEK, CONST_CS | CONST_PERSISTENT);
  393. REGISTER_LONG_CONSTANT("MSG_DONTROUTE", MSG_DONTROUTE, CONST_CS | CONST_PERSISTENT);
  394. #ifdef MSG_EOR
  395. REGISTER_LONG_CONSTANT("MSG_EOR", MSG_EOR, CONST_CS | CONST_PERSISTENT);
  396. #endif
  397. #ifdef MSG_EOF
  398. REGISTER_LONG_CONSTANT("MSG_EOF", MSG_EOF, CONST_CS | CONST_PERSISTENT);
  399. #endif
  400. #ifdef MSG_CONFIRM
  401. REGISTER_LONG_CONSTANT("MSG_CONFIRM", MSG_CONFIRM, CONST_CS | CONST_PERSISTENT);
  402. #endif
  403. #ifdef MSG_ERRQUEUE
  404. REGISTER_LONG_CONSTANT("MSG_ERRQUEUE", MSG_ERRQUEUE, CONST_CS | CONST_PERSISTENT);
  405. #endif
  406. #ifdef MSG_NOSIGNAL
  407. REGISTER_LONG_CONSTANT("MSG_NOSIGNAL", MSG_NOSIGNAL, CONST_CS | CONST_PERSISTENT);
  408. #endif
  409. #ifdef MSG_DONTWAIT
  410. REGISTER_LONG_CONSTANT("MSG_DONTWAIT", MSG_DONTWAIT, CONST_CS | CONST_PERSISTENT);
  411. #endif
  412. #ifdef MSG_MORE
  413. REGISTER_LONG_CONSTANT("MSG_MORE", MSG_MORE, CONST_CS | CONST_PERSISTENT);
  414. #endif
  415. #ifdef MSG_WAITFORONE
  416. REGISTER_LONG_CONSTANT("MSG_WAITFORONE",MSG_WAITFORONE, CONST_CS | CONST_PERSISTENT);
  417. #endif
  418. #ifdef MSG_CMSG_CLOEXEC
  419. REGISTER_LONG_CONSTANT("MSG_CMSG_CLOEXEC",MSG_CMSG_CLOEXEC,CONST_CS | CONST_PERSISTENT);
  420. #endif
  421. REGISTER_LONG_CONSTANT("SO_DEBUG", SO_DEBUG, CONST_CS | CONST_PERSISTENT);
  422. REGISTER_LONG_CONSTANT("SO_REUSEADDR", SO_REUSEADDR, CONST_CS | CONST_PERSISTENT);
  423. #ifdef SO_REUSEPORT
  424. REGISTER_LONG_CONSTANT("SO_REUSEPORT", SO_REUSEPORT, CONST_CS | CONST_PERSISTENT);
  425. #endif
  426. REGISTER_LONG_CONSTANT("SO_KEEPALIVE", SO_KEEPALIVE, CONST_CS | CONST_PERSISTENT);
  427. REGISTER_LONG_CONSTANT("SO_DONTROUTE", SO_DONTROUTE, CONST_CS | CONST_PERSISTENT);
  428. REGISTER_LONG_CONSTANT("SO_LINGER", SO_LINGER, CONST_CS | CONST_PERSISTENT);
  429. REGISTER_LONG_CONSTANT("SO_BROADCAST", SO_BROADCAST, CONST_CS | CONST_PERSISTENT);
  430. REGISTER_LONG_CONSTANT("SO_OOBINLINE", SO_OOBINLINE, CONST_CS | CONST_PERSISTENT);
  431. REGISTER_LONG_CONSTANT("SO_SNDBUF", SO_SNDBUF, CONST_CS | CONST_PERSISTENT);
  432. REGISTER_LONG_CONSTANT("SO_RCVBUF", SO_RCVBUF, CONST_CS | CONST_PERSISTENT);
  433. REGISTER_LONG_CONSTANT("SO_SNDLOWAT", SO_SNDLOWAT, CONST_CS | CONST_PERSISTENT);
  434. REGISTER_LONG_CONSTANT("SO_RCVLOWAT", SO_RCVLOWAT, CONST_CS | CONST_PERSISTENT);
  435. REGISTER_LONG_CONSTANT("SO_SNDTIMEO", SO_SNDTIMEO, CONST_CS | CONST_PERSISTENT);
  436. REGISTER_LONG_CONSTANT("SO_RCVTIMEO", SO_RCVTIMEO, CONST_CS | CONST_PERSISTENT);
  437. REGISTER_LONG_CONSTANT("SO_TYPE", SO_TYPE, CONST_CS | CONST_PERSISTENT);
  438. #ifdef SO_FAMILY
  439. REGISTER_LONG_CONSTANT("SO_FAMILY", SO_FAMILY, CONST_CS | CONST_PERSISTENT);
  440. #endif
  441. REGISTER_LONG_CONSTANT("SO_ERROR", SO_ERROR, CONST_CS | CONST_PERSISTENT);
  442. #ifdef SO_BINDTODEVICE
  443. REGISTER_LONG_CONSTANT("SO_BINDTODEVICE", SO_BINDTODEVICE, CONST_CS | CONST_PERSISTENT);
  444. #endif
  445. #ifdef SO_USER_COOKIE
  446. REGISTER_LONG_CONSTANT("SO_LABEL", SO_LABEL, CONST_CS | CONST_PERSISTENT);
  447. REGISTER_LONG_CONSTANT("SO_PEERLABEL", SO_PEERLABEL, CONST_CS | CONST_PERSISTENT);
  448. REGISTER_LONG_CONSTANT("SO_LISTENQLIMIT", SO_LISTENQLIMIT, CONST_CS | CONST_PERSISTENT);
  449. REGISTER_LONG_CONSTANT("SO_LISTENQLEN", SO_LISTENQLEN, CONST_CS | CONST_PERSISTENT);
  450. REGISTER_LONG_CONSTANT("SO_USER_COOKIE", SO_USER_COOKIE, CONST_CS | CONST_PERSISTENT);
  451. #endif
  452. #ifdef SO_ACCEPTFILTER
  453. REGISTER_LONG_CONSTANT("SO_ACCEPTFILTER", SO_ACCEPTFILTER, CONST_CS | CONST_PERSISTENT);
  454. #endif
  455. #ifdef SO_DONTTRUNC
  456. REGISTER_LONG_CONSTANT("SO_DONTTRUNC", SO_DONTTRUNC, CONST_CS | CONST_PERSISTENT);
  457. #endif
  458. #ifdef SO_WANTMORE
  459. REGISTER_LONG_CONSTANT("SO_WANTMORE", SO_WANTMORE, CONST_CS | CONST_PERSISTENT);
  460. #endif
  461. REGISTER_LONG_CONSTANT("SOL_SOCKET", SOL_SOCKET, CONST_CS | CONST_PERSISTENT);
  462. REGISTER_LONG_CONSTANT("SOMAXCONN", SOMAXCONN, CONST_CS | CONST_PERSISTENT);
  463. #ifdef SO_MARK
  464. REGISTER_LONG_CONSTANT("SO_MARK", SO_MARK, CONST_CS | CONST_PERSISTENT);
  465. #endif
  466. #ifdef TCP_NODELAY
  467. REGISTER_LONG_CONSTANT("TCP_NODELAY", TCP_NODELAY, CONST_CS | CONST_PERSISTENT);
  468. #endif
  469. #ifdef TCP_DEFER_ACCEPT
  470. REGISTER_LONG_CONSTANT("TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT, CONST_CS | CONST_PERSISTENT);
  471. #endif
  472. REGISTER_LONG_CONSTANT("PHP_NORMAL_READ", PHP_NORMAL_READ, CONST_CS | CONST_PERSISTENT);
  473. REGISTER_LONG_CONSTANT("PHP_BINARY_READ", PHP_BINARY_READ, CONST_CS | CONST_PERSISTENT);
  474. REGISTER_LONG_CONSTANT("MCAST_JOIN_GROUP", PHP_MCAST_JOIN_GROUP, CONST_CS | CONST_PERSISTENT);
  475. REGISTER_LONG_CONSTANT("MCAST_LEAVE_GROUP", PHP_MCAST_LEAVE_GROUP, CONST_CS | CONST_PERSISTENT);
  476. #ifdef HAS_MCAST_EXT
  477. REGISTER_LONG_CONSTANT("MCAST_BLOCK_SOURCE", PHP_MCAST_BLOCK_SOURCE, CONST_CS | CONST_PERSISTENT);
  478. REGISTER_LONG_CONSTANT("MCAST_UNBLOCK_SOURCE", PHP_MCAST_UNBLOCK_SOURCE, CONST_CS | CONST_PERSISTENT);
  479. REGISTER_LONG_CONSTANT("MCAST_JOIN_SOURCE_GROUP", PHP_MCAST_JOIN_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT);
  480. REGISTER_LONG_CONSTANT("MCAST_LEAVE_SOURCE_GROUP", PHP_MCAST_LEAVE_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT);
  481. #endif
  482. REGISTER_LONG_CONSTANT("IP_MULTICAST_IF", IP_MULTICAST_IF, CONST_CS | CONST_PERSISTENT);
  483. REGISTER_LONG_CONSTANT("IP_MULTICAST_TTL", IP_MULTICAST_TTL, CONST_CS | CONST_PERSISTENT);
  484. REGISTER_LONG_CONSTANT("IP_MULTICAST_LOOP", IP_MULTICAST_LOOP, CONST_CS | CONST_PERSISTENT);
  485. #if HAVE_IPV6
  486. REGISTER_LONG_CONSTANT("IPV6_MULTICAST_IF", IPV6_MULTICAST_IF, CONST_CS | CONST_PERSISTENT);
  487. REGISTER_LONG_CONSTANT("IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS, CONST_CS | CONST_PERSISTENT);
  488. REGISTER_LONG_CONSTANT("IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP, CONST_CS | CONST_PERSISTENT);
  489. #endif
  490. #ifdef IPV6_V6ONLY
  491. REGISTER_LONG_CONSTANT("IPV6_V6ONLY", IPV6_V6ONLY, CONST_CS | CONST_PERSISTENT);
  492. #endif
  493. #ifndef WIN32
  494. # include "unix_socket_constants.h"
  495. #else
  496. # include "win32_socket_constants.h"
  497. #endif
  498. REGISTER_LONG_CONSTANT("IPPROTO_IP", IPPROTO_IP, CONST_CS | CONST_PERSISTENT);
  499. #if HAVE_IPV6
  500. REGISTER_LONG_CONSTANT("IPPROTO_IPV6", IPPROTO_IPV6, CONST_CS | CONST_PERSISTENT);
  501. #endif
  502. REGISTER_LONG_CONSTANT("SOL_TCP", IPPROTO_TCP, CONST_CS | CONST_PERSISTENT);
  503. REGISTER_LONG_CONSTANT("SOL_UDP", IPPROTO_UDP, CONST_CS | CONST_PERSISTENT);
  504. #if HAVE_IPV6
  505. REGISTER_LONG_CONSTANT("IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS, CONST_CS | CONST_PERSISTENT);
  506. #endif
  507. REGISTER_LONG_CONSTANT("AI_PASSIVE", AI_PASSIVE, CONST_CS | CONST_PERSISTENT);
  508. REGISTER_LONG_CONSTANT("AI_CANONNAME", AI_CANONNAME, CONST_CS | CONST_PERSISTENT);
  509. REGISTER_LONG_CONSTANT("AI_NUMERICHOST", AI_NUMERICHOST, CONST_CS | CONST_PERSISTENT);
  510. #if HAVE_AI_V4MAPPED
  511. REGISTER_LONG_CONSTANT("AI_V4MAPPED", AI_V4MAPPED, CONST_CS | CONST_PERSISTENT);
  512. #endif
  513. #if HAVE_AI_ALL
  514. REGISTER_LONG_CONSTANT("AI_ALL", AI_ALL, CONST_CS | CONST_PERSISTENT);
  515. #endif
  516. REGISTER_LONG_CONSTANT("AI_ADDRCONFIG", AI_ADDRCONFIG, CONST_CS | CONST_PERSISTENT);
  517. #if HAVE_AI_IDN
  518. REGISTER_LONG_CONSTANT("AI_IDN", AI_IDN, CONST_CS | CONST_PERSISTENT);
  519. REGISTER_LONG_CONSTANT("AI_CANONIDN", AI_CANONIDN, CONST_CS | CONST_PERSISTENT);
  520. #endif
  521. #ifdef AI_NUMERICSERV
  522. REGISTER_LONG_CONSTANT("AI_NUMERICSERV", AI_NUMERICSERV, CONST_CS | CONST_PERSISTENT);
  523. #endif
  524. php_socket_sendrecvmsg_init(INIT_FUNC_ARGS_PASSTHRU);
  525. return SUCCESS;
  526. }
  527. /* }}} */
  528. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  529. static PHP_MSHUTDOWN_FUNCTION(sockets)
  530. {
  531. php_socket_sendrecvmsg_shutdown(SHUTDOWN_FUNC_ARGS_PASSTHRU);
  532. return SUCCESS;
  533. }
  534. /* }}} */
  535. /* {{{ PHP_MINFO_FUNCTION */
  536. static PHP_MINFO_FUNCTION(sockets)
  537. {
  538. php_info_print_table_start();
  539. php_info_print_table_row(2, "Sockets Support", "enabled");
  540. php_info_print_table_end();
  541. }
  542. /* }}} */
  543. /* {{{ PHP_RSHUTDOWN_FUNCTION */
  544. static PHP_RSHUTDOWN_FUNCTION(sockets)
  545. {
  546. if (SOCKETS_G(strerror_buf)) {
  547. efree(SOCKETS_G(strerror_buf));
  548. SOCKETS_G(strerror_buf) = NULL;
  549. }
  550. return SUCCESS;
  551. }
  552. /* }}} */
  553. static int php_sock_array_to_fd_set(uint32_t arg_num, zval *sock_array, fd_set *fds, PHP_SOCKET *max_fd) /* {{{ */
  554. {
  555. zval *element;
  556. php_socket *php_sock;
  557. int num = 0;
  558. if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
  559. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(sock_array), element) {
  560. ZVAL_DEREF(element);
  561. if (Z_TYPE_P(element) != IS_OBJECT || Z_OBJCE_P(element) != socket_ce) {
  562. zend_argument_type_error(arg_num, "must only have elements of type Socket, %s given", zend_zval_type_name(element));
  563. return -1;
  564. }
  565. php_sock = Z_SOCKET_P(element);
  566. if (IS_INVALID_SOCKET(php_sock)) {
  567. zend_argument_type_error(arg_num, "contains a closed socket");
  568. return -1;
  569. }
  570. PHP_SAFE_FD_SET(php_sock->bsd_socket, fds);
  571. if (php_sock->bsd_socket > *max_fd) {
  572. *max_fd = php_sock->bsd_socket;
  573. }
  574. num++;
  575. } ZEND_HASH_FOREACH_END();
  576. return num ? 1 : 0;
  577. }
  578. /* }}} */
  579. static int php_sock_array_from_fd_set(zval *sock_array, fd_set *fds) /* {{{ */
  580. {
  581. zval *element;
  582. zval *dest_element;
  583. php_socket *php_sock;
  584. zval new_hash;
  585. int num = 0;
  586. zend_ulong num_key;
  587. zend_string *key;
  588. ZEND_ASSERT(Z_TYPE_P(sock_array) == IS_ARRAY);
  589. array_init(&new_hash);
  590. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(sock_array), num_key, key, element) {
  591. ZVAL_DEREF(element);
  592. php_sock = Z_SOCKET_P(element);
  593. ZEND_ASSERT(php_sock); /* element is supposed to be Socket object */
  594. ZEND_ASSERT(!IS_INVALID_SOCKET(php_sock));
  595. if (PHP_SAFE_FD_ISSET(php_sock->bsd_socket, fds)) {
  596. /* Add fd to new array */
  597. if (key) {
  598. dest_element = zend_hash_add(Z_ARRVAL(new_hash), key, element);
  599. } else {
  600. dest_element = zend_hash_index_update(Z_ARRVAL(new_hash), num_key, element);
  601. }
  602. if (dest_element) {
  603. Z_ADDREF_P(dest_element);
  604. }
  605. }
  606. num++;
  607. } ZEND_HASH_FOREACH_END();
  608. /* Destroy old array, add new one */
  609. zval_ptr_dtor(sock_array);
  610. ZVAL_COPY_VALUE(sock_array, &new_hash);
  611. return num ? 1 : 0;
  612. }
  613. /* }}} */
  614. /* {{{ Runs the select() system call on the sets mentioned with a timeout specified by tv_sec and tv_usec */
  615. PHP_FUNCTION(socket_select)
  616. {
  617. zval *r_array, *w_array, *e_array;
  618. struct timeval tv;
  619. struct timeval *tv_p = NULL;
  620. fd_set rfds, wfds, efds;
  621. PHP_SOCKET max_fd = 0;
  622. int retval, sets = 0;
  623. zend_long sec, usec = 0;
  624. bool sec_is_null = 0;
  625. if (zend_parse_parameters(ZEND_NUM_ARGS(), "a!a!a!l!|l", &r_array, &w_array, &e_array, &sec, &sec_is_null, &usec) == FAILURE) {
  626. RETURN_THROWS();
  627. }
  628. FD_ZERO(&rfds);
  629. FD_ZERO(&wfds);
  630. FD_ZERO(&efds);
  631. if (r_array != NULL) {
  632. sets += retval = php_sock_array_to_fd_set(1, r_array, &rfds, &max_fd);
  633. if (retval == -1) {
  634. RETURN_THROWS();
  635. }
  636. }
  637. if (w_array != NULL) {
  638. sets += retval = php_sock_array_to_fd_set(2, w_array, &wfds, &max_fd);
  639. if (retval == -1) {
  640. RETURN_THROWS();
  641. }
  642. }
  643. if (e_array != NULL) {
  644. sets += retval = php_sock_array_to_fd_set(3, e_array, &efds, &max_fd);
  645. if (retval == -1) {
  646. RETURN_THROWS();
  647. }
  648. }
  649. if (!sets) {
  650. zend_value_error("socket_select(): At least one array argument must be passed");
  651. RETURN_THROWS();
  652. }
  653. PHP_SAFE_MAX_FD(max_fd, 0); /* someone needs to make this look more like stream_socket_select */
  654. /* If seconds is not set to null, build the timeval, else we wait indefinitely */
  655. if (!sec_is_null) {
  656. /* Solaris + BSD do not like microsecond values which are >= 1 sec */
  657. if (usec > 999999) {
  658. tv.tv_sec = sec + (usec / 1000000);
  659. tv.tv_usec = usec % 1000000;
  660. } else {
  661. tv.tv_sec = sec;
  662. tv.tv_usec = usec;
  663. }
  664. tv_p = &tv;
  665. }
  666. retval = select(max_fd+1, &rfds, &wfds, &efds, tv_p);
  667. if (retval == -1) {
  668. SOCKETS_G(last_error) = errno;
  669. php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s", errno, sockets_strerror(errno));
  670. RETURN_FALSE;
  671. }
  672. if (r_array != NULL) php_sock_array_from_fd_set(r_array, &rfds);
  673. if (w_array != NULL) php_sock_array_from_fd_set(w_array, &wfds);
  674. if (e_array != NULL) php_sock_array_from_fd_set(e_array, &efds);
  675. RETURN_LONG(retval);
  676. }
  677. /* }}} */
  678. /* {{{ Opens a socket on port to accept connections */
  679. PHP_FUNCTION(socket_create_listen)
  680. {
  681. php_socket *php_sock;
  682. zend_long port, backlog = 128;
  683. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &port, &backlog) == FAILURE) {
  684. RETURN_THROWS();
  685. }
  686. object_init_ex(return_value, socket_ce);
  687. php_sock = Z_SOCKET_P(return_value);
  688. if (!php_open_listen_sock(php_sock, port, backlog)) {
  689. zval_ptr_dtor(return_value);
  690. RETURN_FALSE;
  691. }
  692. php_sock->error = 0;
  693. php_sock->blocking = 1;
  694. }
  695. /* }}} */
  696. /* {{{ Accepts a connection on the listening socket fd */
  697. PHP_FUNCTION(socket_accept)
  698. {
  699. zval *arg1;
  700. php_socket *php_sock, *new_sock;
  701. php_sockaddr_storage sa;
  702. socklen_t php_sa_len = sizeof(sa);
  703. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, socket_ce) == FAILURE) {
  704. RETURN_THROWS();
  705. }
  706. php_sock = Z_SOCKET_P(arg1);
  707. ENSURE_SOCKET_VALID(php_sock);
  708. object_init_ex(return_value, socket_ce);
  709. new_sock = Z_SOCKET_P(return_value);
  710. if (!php_accept_connect(php_sock, new_sock, (struct sockaddr*)&sa, &php_sa_len)) {
  711. zval_ptr_dtor(return_value);
  712. RETURN_FALSE;
  713. }
  714. }
  715. /* }}} */
  716. /* {{{ Sets nonblocking mode on a socket resource */
  717. PHP_FUNCTION(socket_set_nonblock)
  718. {
  719. zval *arg1;
  720. php_socket *php_sock;
  721. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, socket_ce) == FAILURE) {
  722. RETURN_THROWS();
  723. }
  724. php_sock = Z_SOCKET_P(arg1);
  725. ENSURE_SOCKET_VALID(php_sock);
  726. if (!Z_ISUNDEF(php_sock->zstream)) {
  727. php_stream *stream;
  728. /* omit notice if resource doesn't exist anymore */
  729. stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
  730. if (stream != NULL) {
  731. if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 0,
  732. NULL) != -1) {
  733. php_sock->blocking = 0;
  734. RETURN_TRUE;
  735. }
  736. }
  737. }
  738. if (php_set_sock_blocking(php_sock->bsd_socket, 0) == SUCCESS) {
  739. php_sock->blocking = 0;
  740. RETURN_TRUE;
  741. } else {
  742. PHP_SOCKET_ERROR(php_sock, "unable to set nonblocking mode", errno);
  743. RETURN_FALSE;
  744. }
  745. }
  746. /* }}} */
  747. /* {{{ Sets blocking mode on a socket resource */
  748. PHP_FUNCTION(socket_set_block)
  749. {
  750. zval *arg1;
  751. php_socket *php_sock;
  752. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, socket_ce) == FAILURE) {
  753. RETURN_THROWS();
  754. }
  755. php_sock = Z_SOCKET_P(arg1);
  756. ENSURE_SOCKET_VALID(php_sock);
  757. /* if socket was created from a stream, give the stream a chance to take
  758. * care of the operation itself, thereby allowing it to update its internal
  759. * state */
  760. if (!Z_ISUNDEF(php_sock->zstream)) {
  761. php_stream *stream;
  762. stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
  763. if (stream != NULL) {
  764. if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 1,
  765. NULL) != -1) {
  766. php_sock->blocking = 1;
  767. RETURN_TRUE;
  768. }
  769. }
  770. }
  771. if (php_set_sock_blocking(php_sock->bsd_socket, 1) == SUCCESS) {
  772. php_sock->blocking = 1;
  773. RETURN_TRUE;
  774. } else {
  775. PHP_SOCKET_ERROR(php_sock, "unable to set blocking mode", errno);
  776. RETURN_FALSE;
  777. }
  778. }
  779. /* }}} */
  780. /* {{{ Sets the maximum number of connections allowed to be waited for on the socket specified by fd */
  781. PHP_FUNCTION(socket_listen)
  782. {
  783. zval *arg1;
  784. php_socket *php_sock;
  785. zend_long backlog = 0;
  786. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &arg1, socket_ce, &backlog) == FAILURE) {
  787. RETURN_THROWS();
  788. }
  789. php_sock = Z_SOCKET_P(arg1);
  790. ENSURE_SOCKET_VALID(php_sock);
  791. if (listen(php_sock->bsd_socket, backlog) != 0) {
  792. PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno);
  793. RETURN_FALSE;
  794. }
  795. RETURN_TRUE;
  796. }
  797. /* }}} */
  798. /* {{{ Closes a file descriptor */
  799. PHP_FUNCTION(socket_close)
  800. {
  801. zval *arg1;
  802. php_socket *php_socket;
  803. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, socket_ce) == FAILURE) {
  804. RETURN_THROWS();
  805. }
  806. php_socket = Z_SOCKET_P(arg1);
  807. ENSURE_SOCKET_VALID(php_socket);
  808. if (!Z_ISUNDEF(php_socket->zstream)) {
  809. php_stream *stream = NULL;
  810. php_stream_from_zval_no_verify(stream, &php_socket->zstream);
  811. if (stream != NULL) {
  812. /* close & destroy stream, incl. removing it from the rsrc list;
  813. * resource stored in php_sock->zstream will become invalid */
  814. php_stream_free(stream,
  815. PHP_STREAM_FREE_KEEP_RSRC | PHP_STREAM_FREE_CLOSE |
  816. (stream->is_persistent?PHP_STREAM_FREE_CLOSE_PERSISTENT:0));
  817. }
  818. } else {
  819. if (!IS_INVALID_SOCKET(php_socket)) {
  820. close(php_socket->bsd_socket);
  821. }
  822. }
  823. ZVAL_UNDEF(&php_socket->zstream);
  824. php_socket->bsd_socket = -1;
  825. }
  826. /* }}} */
  827. /* {{{ Writes the buffer to the socket resource, length is optional */
  828. PHP_FUNCTION(socket_write)
  829. {
  830. zval *arg1;
  831. php_socket *php_sock;
  832. int retval;
  833. size_t str_len;
  834. zend_long length = 0;
  835. bool length_is_null = 1;
  836. char *str;
  837. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l!", &arg1, socket_ce, &str, &str_len, &length, &length_is_null) == FAILURE) {
  838. RETURN_THROWS();
  839. }
  840. php_sock = Z_SOCKET_P(arg1);
  841. ENSURE_SOCKET_VALID(php_sock);
  842. if (length < 0) {
  843. zend_argument_value_error(3, "must be greater than or equal to 0");
  844. RETURN_THROWS();
  845. }
  846. if (length_is_null) {
  847. length = str_len;
  848. }
  849. #ifndef PHP_WIN32
  850. retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
  851. #else
  852. retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
  853. #endif
  854. if (retval < 0) {
  855. PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
  856. RETURN_FALSE;
  857. }
  858. RETURN_LONG(retval);
  859. }
  860. /* }}} */
  861. /* {{{ Reads a maximum of length bytes from socket */
  862. PHP_FUNCTION(socket_read)
  863. {
  864. zval *arg1;
  865. php_socket *php_sock;
  866. zend_string *tmpbuf;
  867. int retval;
  868. zend_long length, type = PHP_BINARY_READ;
  869. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|l", &arg1, socket_ce, &length, &type) == FAILURE) {
  870. RETURN_THROWS();
  871. }
  872. php_sock = Z_SOCKET_P(arg1);
  873. ENSURE_SOCKET_VALID(php_sock);
  874. /* overflow check */
  875. if ((length + 1) < 2) {
  876. RETURN_FALSE;
  877. }
  878. tmpbuf = zend_string_alloc(length, 0);
  879. if (type == PHP_NORMAL_READ) {
  880. retval = php_read(php_sock, ZSTR_VAL(tmpbuf), length, 0);
  881. } else {
  882. retval = recv(php_sock->bsd_socket, ZSTR_VAL(tmpbuf), length, 0);
  883. }
  884. if (retval == -1) {
  885. /* if the socket is in non-blocking mode and there's no data to read,
  886. don't output any error, as this is a normal situation, and not an error */
  887. if (PHP_IS_TRANSIENT_ERROR(errno)) {
  888. php_sock->error = errno;
  889. SOCKETS_G(last_error) = errno;
  890. } else {
  891. PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
  892. }
  893. zend_string_efree(tmpbuf);
  894. RETURN_FALSE;
  895. } else if (!retval) {
  896. zend_string_efree(tmpbuf);
  897. RETURN_EMPTY_STRING();
  898. }
  899. tmpbuf = zend_string_truncate(tmpbuf, retval, 0);
  900. ZSTR_LEN(tmpbuf) = retval;
  901. ZSTR_VAL(tmpbuf)[ZSTR_LEN(tmpbuf)] = '\0' ;
  902. RETURN_NEW_STR(tmpbuf);
  903. }
  904. /* }}} */
  905. /* {{{ Queries the remote side of the given socket which may either result in host/port or in a UNIX filesystem path, dependent on its type. */
  906. PHP_FUNCTION(socket_getsockname)
  907. {
  908. zval *arg1, *addr, *port = NULL;
  909. php_sockaddr_storage sa_storage;
  910. php_socket *php_sock;
  911. struct sockaddr *sa;
  912. struct sockaddr_in *sin;
  913. #if HAVE_IPV6
  914. struct sockaddr_in6 *sin6;
  915. #endif
  916. #ifdef HAVE_INET_NTOP
  917. char addrbuf[INET6_ADDRSTRLEN];
  918. #endif
  919. struct sockaddr_un *s_un;
  920. const char *addr_string;
  921. socklen_t salen = sizeof(php_sockaddr_storage);
  922. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &addr, &port) == FAILURE) {
  923. RETURN_THROWS();
  924. }
  925. php_sock = Z_SOCKET_P(arg1);
  926. ENSURE_SOCKET_VALID(php_sock);
  927. sa = (struct sockaddr *) &sa_storage;
  928. if (getsockname(php_sock->bsd_socket, sa, &salen) != 0) {
  929. PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket name", errno);
  930. RETURN_FALSE;
  931. }
  932. switch (sa->sa_family) {
  933. #if HAVE_IPV6
  934. case AF_INET6:
  935. sin6 = (struct sockaddr_in6 *) sa;
  936. inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
  937. ZEND_TRY_ASSIGN_REF_STRING(addr, addrbuf);
  938. if (port != NULL) {
  939. ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin6->sin6_port));
  940. }
  941. RETURN_TRUE;
  942. break;
  943. #endif
  944. case AF_INET:
  945. sin = (struct sockaddr_in *) sa;
  946. #ifdef HAVE_INET_NTOP
  947. addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
  948. #else
  949. while (inet_ntoa_lock == 1);
  950. inet_ntoa_lock = 1;
  951. addr_string = inet_ntoa(sin->sin_addr);
  952. inet_ntoa_lock = 0;
  953. #endif
  954. ZEND_TRY_ASSIGN_REF_STRING(addr, addr_string);
  955. if (port != NULL) {
  956. ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin->sin_port));
  957. }
  958. RETURN_TRUE;
  959. break;
  960. case AF_UNIX:
  961. s_un = (struct sockaddr_un *) sa;
  962. ZEND_TRY_ASSIGN_REF_STRING(addr, s_un->sun_path);
  963. RETURN_TRUE;
  964. break;
  965. default:
  966. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  967. RETURN_THROWS();
  968. }
  969. }
  970. /* }}} */
  971. /* {{{ Queries the remote side of the given socket which may either result in host/port or in a UNIX filesystem path, dependent on its type. */
  972. PHP_FUNCTION(socket_getpeername)
  973. {
  974. zval *arg1, *arg2, *arg3 = NULL;
  975. php_sockaddr_storage sa_storage;
  976. php_socket *php_sock;
  977. struct sockaddr *sa;
  978. struct sockaddr_in *sin;
  979. #if HAVE_IPV6
  980. struct sockaddr_in6 *sin6;
  981. #endif
  982. #ifdef HAVE_INET_NTOP
  983. char addrbuf[INET6_ADDRSTRLEN];
  984. #endif
  985. struct sockaddr_un *s_un;
  986. const char *addr_string;
  987. socklen_t salen = sizeof(php_sockaddr_storage);
  988. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &arg2, &arg3) == FAILURE) {
  989. RETURN_THROWS();
  990. }
  991. php_sock = Z_SOCKET_P(arg1);
  992. ENSURE_SOCKET_VALID(php_sock);
  993. sa = (struct sockaddr *) &sa_storage;
  994. if (getpeername(php_sock->bsd_socket, sa, &salen) < 0) {
  995. PHP_SOCKET_ERROR(php_sock, "unable to retrieve peer name", errno);
  996. RETURN_FALSE;
  997. }
  998. switch (sa->sa_family) {
  999. #if HAVE_IPV6
  1000. case AF_INET6:
  1001. sin6 = (struct sockaddr_in6 *) sa;
  1002. inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
  1003. ZEND_TRY_ASSIGN_REF_STRING(arg2, addrbuf);
  1004. if (arg3 != NULL) {
  1005. ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin6->sin6_port));
  1006. }
  1007. RETURN_TRUE;
  1008. break;
  1009. #endif
  1010. case AF_INET:
  1011. sin = (struct sockaddr_in *) sa;
  1012. #ifdef HAVE_INET_NTOP
  1013. addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
  1014. #else
  1015. while (inet_ntoa_lock == 1);
  1016. inet_ntoa_lock = 1;
  1017. addr_string = inet_ntoa(sin->sin_addr);
  1018. inet_ntoa_lock = 0;
  1019. #endif
  1020. ZEND_TRY_ASSIGN_REF_STRING(arg2, addr_string);
  1021. if (arg3 != NULL) {
  1022. ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin->sin_port));
  1023. }
  1024. RETURN_TRUE;
  1025. break;
  1026. case AF_UNIX:
  1027. s_un = (struct sockaddr_un *) sa;
  1028. ZEND_TRY_ASSIGN_REF_STRING(arg2, s_un->sun_path);
  1029. RETURN_TRUE;
  1030. break;
  1031. default:
  1032. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1033. RETURN_THROWS();
  1034. }
  1035. }
  1036. /* }}} */
  1037. /* {{{ Creates an endpoint for communication in the domain specified by domain, of type specified by type */
  1038. PHP_FUNCTION(socket_create)
  1039. {
  1040. zend_long domain, type, protocol;
  1041. php_socket *php_sock;
  1042. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &domain, &type, &protocol) == FAILURE) {
  1043. RETURN_THROWS();
  1044. }
  1045. if (domain != AF_UNIX
  1046. #if HAVE_IPV6
  1047. && domain != AF_INET6
  1048. #endif
  1049. && domain != AF_INET) {
  1050. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET6, or AF_INET");
  1051. RETURN_THROWS();
  1052. }
  1053. if (type > 10) {
  1054. zend_argument_value_error(2, "must be one of SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET,"
  1055. " SOCK_RAW, or SOCK_RDM");
  1056. RETURN_THROWS();
  1057. }
  1058. object_init_ex(return_value, socket_ce);
  1059. php_sock = Z_SOCKET_P(return_value);
  1060. php_sock->bsd_socket = socket(domain, type, protocol);
  1061. php_sock->type = domain;
  1062. if (IS_INVALID_SOCKET(php_sock)) {
  1063. SOCKETS_G(last_error) = errno;
  1064. php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
  1065. zval_ptr_dtor(return_value);
  1066. RETURN_FALSE;
  1067. }
  1068. php_sock->error = 0;
  1069. php_sock->blocking = 1;
  1070. }
  1071. /* }}} */
  1072. /* {{{ Opens a connection to addr:port on the socket specified by socket */
  1073. PHP_FUNCTION(socket_connect)
  1074. {
  1075. zval *resource_socket;
  1076. php_socket *php_sock;
  1077. char *addr;
  1078. int retval;
  1079. size_t addr_len;
  1080. zend_long port;
  1081. bool port_is_null = 1;
  1082. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l!", &resource_socket, socket_ce, &addr, &addr_len, &port, &port_is_null) == FAILURE) {
  1083. RETURN_THROWS();
  1084. }
  1085. php_sock = Z_SOCKET_P(resource_socket);
  1086. ENSURE_SOCKET_VALID(php_sock);
  1087. switch(php_sock->type) {
  1088. #if HAVE_IPV6
  1089. case AF_INET6: {
  1090. struct sockaddr_in6 sin6 = {0};
  1091. if (port_is_null) {
  1092. zend_argument_value_error(3, "cannot be null when the socket type is AF_INET6");
  1093. RETURN_THROWS();
  1094. }
  1095. memset(&sin6, 0, sizeof(struct sockaddr_in6));
  1096. sin6.sin6_family = AF_INET6;
  1097. sin6.sin6_port = htons((unsigned short int)port);
  1098. if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
  1099. RETURN_FALSE;
  1100. }
  1101. retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6));
  1102. break;
  1103. }
  1104. #endif
  1105. case AF_INET: {
  1106. struct sockaddr_in sin = {0};
  1107. if (port_is_null) {
  1108. zend_argument_value_error(3, "cannot be null when the socket type is AF_INET");
  1109. RETURN_THROWS();
  1110. }
  1111. sin.sin_family = AF_INET;
  1112. sin.sin_port = htons((unsigned short int)port);
  1113. if (! php_set_inet_addr(&sin, addr, php_sock)) {
  1114. RETURN_FALSE;
  1115. }
  1116. retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
  1117. break;
  1118. }
  1119. case AF_UNIX: {
  1120. struct sockaddr_un s_un = {0};
  1121. if (addr_len >= sizeof(s_un.sun_path)) {
  1122. zend_argument_value_error(2, "must be less than %d", sizeof(s_un.sun_path));
  1123. RETURN_THROWS();
  1124. }
  1125. s_un.sun_family = AF_UNIX;
  1126. memcpy(&s_un.sun_path, addr, addr_len);
  1127. retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un,
  1128. (socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + addr_len));
  1129. break;
  1130. }
  1131. default:
  1132. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1133. RETURN_THROWS();
  1134. }
  1135. if (retval != 0) {
  1136. PHP_SOCKET_ERROR(php_sock, "unable to connect", errno);
  1137. RETURN_FALSE;
  1138. }
  1139. RETURN_TRUE;
  1140. }
  1141. /* }}} */
  1142. /* {{{ Returns a string describing an error */
  1143. PHP_FUNCTION(socket_strerror)
  1144. {
  1145. zend_long arg1;
  1146. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &arg1) == FAILURE) {
  1147. RETURN_THROWS();
  1148. }
  1149. RETURN_STRING(sockets_strerror(arg1));
  1150. }
  1151. /* }}} */
  1152. /* {{{ Binds an open socket to a listening port, port is only specified in AF_INET family. */
  1153. PHP_FUNCTION(socket_bind)
  1154. {
  1155. zval *arg1;
  1156. php_sockaddr_storage sa_storage = {0};
  1157. struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
  1158. php_socket *php_sock;
  1159. char *addr;
  1160. size_t addr_len;
  1161. zend_long port = 0;
  1162. zend_long retval = 0;
  1163. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l", &arg1, socket_ce, &addr, &addr_len, &port) == FAILURE) {
  1164. RETURN_THROWS();
  1165. }
  1166. php_sock = Z_SOCKET_P(arg1);
  1167. ENSURE_SOCKET_VALID(php_sock);
  1168. switch(php_sock->type) {
  1169. case AF_UNIX:
  1170. {
  1171. struct sockaddr_un *sa = (struct sockaddr_un *) sock_type;
  1172. sa->sun_family = AF_UNIX;
  1173. if (addr_len >= sizeof(sa->sun_path)) {
  1174. zend_argument_value_error(2, "must be less than %d", sizeof(sa->sun_path));
  1175. RETURN_THROWS();
  1176. }
  1177. memcpy(&sa->sun_path, addr, addr_len);
  1178. retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa,
  1179. offsetof(struct sockaddr_un, sun_path) + addr_len);
  1180. break;
  1181. }
  1182. case AF_INET:
  1183. {
  1184. struct sockaddr_in *sa = (struct sockaddr_in *) sock_type;
  1185. sa->sin_family = AF_INET;
  1186. sa->sin_port = htons((unsigned short) port);
  1187. if (! php_set_inet_addr(sa, addr, php_sock)) {
  1188. RETURN_FALSE;
  1189. }
  1190. retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
  1191. break;
  1192. }
  1193. #if HAVE_IPV6
  1194. case AF_INET6:
  1195. {
  1196. struct sockaddr_in6 *sa = (struct sockaddr_in6 *) sock_type;
  1197. sa->sin6_family = AF_INET6;
  1198. sa->sin6_port = htons((unsigned short) port);
  1199. if (! php_set_inet6_addr(sa, addr, php_sock)) {
  1200. RETURN_FALSE;
  1201. }
  1202. retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in6));
  1203. break;
  1204. }
  1205. #endif
  1206. default:
  1207. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1208. RETURN_THROWS();
  1209. }
  1210. if (retval != 0) {
  1211. PHP_SOCKET_ERROR(php_sock, "Unable to bind address", errno);
  1212. RETURN_FALSE;
  1213. }
  1214. RETURN_TRUE;
  1215. }
  1216. /* }}} */
  1217. /* {{{ Receives data from a connected socket */
  1218. PHP_FUNCTION(socket_recv)
  1219. {
  1220. zval *php_sock_res, *buf;
  1221. zend_string *recv_buf;
  1222. php_socket *php_sock;
  1223. int retval;
  1224. zend_long len, flags;
  1225. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ozll", &php_sock_res, socket_ce, &buf, &len, &flags) == FAILURE) {
  1226. RETURN_THROWS();
  1227. }
  1228. php_sock = Z_SOCKET_P(php_sock_res);
  1229. ENSURE_SOCKET_VALID(php_sock);
  1230. /* overflow check */
  1231. if ((len + 1) < 2) {
  1232. RETURN_FALSE;
  1233. }
  1234. recv_buf = zend_string_alloc(len, 0);
  1235. if ((retval = recv(php_sock->bsd_socket, ZSTR_VAL(recv_buf), len, flags)) < 1) {
  1236. zend_string_efree(recv_buf);
  1237. ZEND_TRY_ASSIGN_REF_NULL(buf);
  1238. } else {
  1239. ZSTR_LEN(recv_buf) = retval;
  1240. ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
  1241. ZEND_TRY_ASSIGN_REF_NEW_STR(buf, recv_buf);
  1242. }
  1243. if (retval == -1) {
  1244. PHP_SOCKET_ERROR(php_sock, "Unable to read from socket", errno);
  1245. RETURN_FALSE;
  1246. }
  1247. RETURN_LONG(retval);
  1248. }
  1249. /* }}} */
  1250. /* {{{ Sends data to a connected socket */
  1251. PHP_FUNCTION(socket_send)
  1252. {
  1253. zval *arg1;
  1254. php_socket *php_sock;
  1255. size_t buf_len, retval;
  1256. zend_long len, flags;
  1257. char *buf;
  1258. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osll", &arg1, socket_ce, &buf, &buf_len, &len, &flags) == FAILURE) {
  1259. RETURN_THROWS();
  1260. }
  1261. php_sock = Z_SOCKET_P(arg1);
  1262. ENSURE_SOCKET_VALID(php_sock);
  1263. if (len < 0) {
  1264. zend_argument_value_error(3, "must be greater than or equal to 0");
  1265. RETURN_THROWS();
  1266. }
  1267. retval = send(php_sock->bsd_socket, buf, (buf_len < (size_t)len ? buf_len : (size_t)len), flags);
  1268. if (retval == (size_t)-1) {
  1269. PHP_SOCKET_ERROR(php_sock, "Unable to write to socket", errno);
  1270. RETURN_FALSE;
  1271. }
  1272. RETURN_LONG(retval);
  1273. }
  1274. /* }}} */
  1275. /* {{{ Receives data from a socket, connected or not */
  1276. PHP_FUNCTION(socket_recvfrom)
  1277. {
  1278. zval *arg1, *arg2, *arg5, *arg6 = NULL;
  1279. php_socket *php_sock;
  1280. struct sockaddr_un s_un;
  1281. struct sockaddr_in sin;
  1282. #if HAVE_IPV6
  1283. struct sockaddr_in6 sin6;
  1284. #endif
  1285. #ifdef HAVE_INET_NTOP
  1286. char addrbuf[INET6_ADDRSTRLEN];
  1287. #endif
  1288. socklen_t slen;
  1289. int retval;
  1290. zend_long arg3, arg4;
  1291. const char *address;
  1292. zend_string *recv_buf;
  1293. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ozllz|z", &arg1, socket_ce, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE) {
  1294. RETURN_THROWS();
  1295. }
  1296. php_sock = Z_SOCKET_P(arg1);
  1297. ENSURE_SOCKET_VALID(php_sock);
  1298. /* overflow check */
  1299. /* Shouldthrow ? */
  1300. if ((arg3 + 2) < 3) {
  1301. RETURN_FALSE;
  1302. }
  1303. recv_buf = zend_string_alloc(arg3 + 1, 0);
  1304. switch (php_sock->type) {
  1305. case AF_UNIX:
  1306. slen = sizeof(s_un);
  1307. memset(&s_un, 0, slen);
  1308. s_un.sun_family = AF_UNIX;
  1309. retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
  1310. if (retval < 0) {
  1311. PHP_SOCKET_ERROR(php_sock, "Unable to recvfrom", errno);
  1312. zend_string_efree(recv_buf);
  1313. RETURN_FALSE;
  1314. }
  1315. ZSTR_LEN(recv_buf) = retval;
  1316. ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
  1317. ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
  1318. ZEND_TRY_ASSIGN_REF_STRING(arg5, s_un.sun_path);
  1319. break;
  1320. case AF_INET:
  1321. slen = sizeof(sin);
  1322. memset(&sin, 0, slen);
  1323. sin.sin_family = AF_INET;
  1324. if (arg6 == NULL) {
  1325. zend_string_efree(recv_buf);
  1326. WRONG_PARAM_COUNT;
  1327. }
  1328. retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sin, (socklen_t *)&slen);
  1329. if (retval < 0) {
  1330. PHP_SOCKET_ERROR(php_sock, "Unable to recvfrom", errno);
  1331. zend_string_efree(recv_buf);
  1332. RETURN_FALSE;
  1333. }
  1334. ZSTR_LEN(recv_buf) = retval;
  1335. ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
  1336. #ifdef HAVE_INET_NTOP
  1337. address = inet_ntop(AF_INET, &sin.sin_addr, addrbuf, sizeof(addrbuf));
  1338. #else
  1339. address = inet_ntoa(sin.sin_addr);
  1340. #endif
  1341. ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
  1342. ZEND_TRY_ASSIGN_REF_STRING(arg5, address ? address : "0.0.0.0");
  1343. ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin.sin_port));
  1344. break;
  1345. #if HAVE_IPV6
  1346. case AF_INET6:
  1347. slen = sizeof(sin6);
  1348. memset(&sin6, 0, slen);
  1349. sin6.sin6_family = AF_INET6;
  1350. if (arg6 == NULL) {
  1351. zend_string_efree(recv_buf);
  1352. WRONG_PARAM_COUNT;
  1353. }
  1354. retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sin6, (socklen_t *)&slen);
  1355. if (retval < 0) {
  1356. PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
  1357. zend_string_efree(recv_buf);
  1358. RETURN_FALSE;
  1359. }
  1360. ZSTR_LEN(recv_buf) = retval;
  1361. ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
  1362. memset(addrbuf, 0, INET6_ADDRSTRLEN);
  1363. inet_ntop(AF_INET6, &sin6.sin6_addr, addrbuf, sizeof(addrbuf));
  1364. ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
  1365. ZEND_TRY_ASSIGN_REF_STRING(arg5, addrbuf[0] ? addrbuf : "::");
  1366. ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin6.sin6_port));
  1367. break;
  1368. #endif
  1369. default:
  1370. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1371. RETURN_THROWS();
  1372. }
  1373. RETURN_LONG(retval);
  1374. }
  1375. /* }}} */
  1376. /* {{{ Sends a message to a socket, whether it is connected or not */
  1377. PHP_FUNCTION(socket_sendto)
  1378. {
  1379. zval *arg1;
  1380. php_socket *php_sock;
  1381. struct sockaddr_un s_un;
  1382. struct sockaddr_in sin;
  1383. #if HAVE_IPV6
  1384. struct sockaddr_in6 sin6;
  1385. #endif
  1386. int retval;
  1387. size_t buf_len, addr_len;
  1388. zend_long len, flags, port;
  1389. bool port_is_null = 1;
  1390. char *buf, *addr;
  1391. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oslls|l!", &arg1, socket_ce, &buf, &buf_len, &len, &flags, &addr, &addr_len, &port, &port_is_null) == FAILURE) {
  1392. RETURN_THROWS();
  1393. }
  1394. php_sock = Z_SOCKET_P(arg1);
  1395. ENSURE_SOCKET_VALID(php_sock);
  1396. if (len < 0) {
  1397. zend_argument_value_error(3, "must be greater than or equal to 0");
  1398. RETURN_THROWS();
  1399. }
  1400. switch (php_sock->type) {
  1401. case AF_UNIX:
  1402. memset(&s_un, 0, sizeof(s_un));
  1403. s_un.sun_family = AF_UNIX;
  1404. snprintf(s_un.sun_path, sizeof(s_un.sun_path), "%s", addr);
  1405. retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &s_un, SUN_LEN(&s_un));
  1406. break;
  1407. case AF_INET:
  1408. if (port_is_null) {
  1409. zend_argument_value_error(6, "cannot be null when the socket type is AF_INET");
  1410. RETURN_THROWS();
  1411. }
  1412. memset(&sin, 0, sizeof(sin));
  1413. sin.sin_family = AF_INET;
  1414. sin.sin_port = htons((unsigned short) port);
  1415. if (! php_set_inet_addr(&sin, addr, php_sock)) {
  1416. RETURN_FALSE;
  1417. }
  1418. retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin, sizeof(sin));
  1419. break;
  1420. #if HAVE_IPV6
  1421. case AF_INET6:
  1422. if (port_is_null) {
  1423. zend_argument_value_error(6, "cannot be null when the socket type is AF_INET6");
  1424. RETURN_THROWS();
  1425. }
  1426. memset(&sin6, 0, sizeof(sin6));
  1427. sin6.sin6_family = AF_INET6;
  1428. sin6.sin6_port = htons((unsigned short) port);
  1429. if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
  1430. RETURN_FALSE;
  1431. }
  1432. retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin6, sizeof(sin6));
  1433. break;
  1434. #endif
  1435. default:
  1436. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1437. RETURN_THROWS();
  1438. }
  1439. if (retval == -1) {
  1440. PHP_SOCKET_ERROR(php_sock, "Unable to write to socket", errno);
  1441. RETURN_FALSE;
  1442. }
  1443. RETURN_LONG(retval);
  1444. }
  1445. /* }}} */
  1446. /* {{{ Gets socket options for the socket */
  1447. PHP_FUNCTION(socket_get_option)
  1448. {
  1449. zval *arg1;
  1450. struct linger linger_val;
  1451. struct timeval tv;
  1452. #ifdef PHP_WIN32
  1453. int timeout = 0;
  1454. #endif
  1455. socklen_t optlen;
  1456. php_socket *php_sock;
  1457. int other_val;
  1458. zend_long level, optname;
  1459. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll", &arg1, socket_ce, &level, &optname) == FAILURE) {
  1460. RETURN_THROWS();
  1461. }
  1462. php_sock = Z_SOCKET_P(arg1);
  1463. ENSURE_SOCKET_VALID(php_sock);
  1464. if (level == IPPROTO_IP) {
  1465. switch (optname) {
  1466. case IP_MULTICAST_IF: {
  1467. struct in_addr if_addr;
  1468. unsigned int if_index;
  1469. optlen = sizeof(if_addr);
  1470. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&if_addr, &optlen) != 0) {
  1471. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1472. RETURN_FALSE;
  1473. }
  1474. if (php_add4_to_if_index(&if_addr, php_sock, &if_index) == SUCCESS) {
  1475. RETURN_LONG((zend_long) if_index);
  1476. } else {
  1477. RETURN_FALSE;
  1478. }
  1479. }
  1480. }
  1481. }
  1482. #if HAVE_IPV6
  1483. else if (level == IPPROTO_IPV6) {
  1484. int ret = php_do_getsockopt_ipv6_rfc3542(php_sock, level, optname, return_value);
  1485. if (ret == SUCCESS) {
  1486. return;
  1487. } else if (ret == FAILURE) {
  1488. RETURN_FALSE;
  1489. } /* else continue */
  1490. }
  1491. #endif
  1492. if (level == SOL_SOCKET) {
  1493. switch (optname) {
  1494. case SO_LINGER:
  1495. optlen = sizeof(linger_val);
  1496. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&linger_val, &optlen) != 0) {
  1497. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1498. RETURN_FALSE;
  1499. }
  1500. array_init(return_value);
  1501. add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
  1502. add_assoc_long(return_value, "l_linger", linger_val.l_linger);
  1503. return;
  1504. case SO_RCVTIMEO:
  1505. case SO_SNDTIMEO:
  1506. #ifndef PHP_WIN32
  1507. optlen = sizeof(tv);
  1508. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&tv, &optlen) != 0) {
  1509. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1510. RETURN_FALSE;
  1511. }
  1512. #else
  1513. optlen = sizeof(int);
  1514. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&timeout, &optlen) != 0) {
  1515. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1516. RETURN_FALSE;
  1517. }
  1518. tv.tv_sec = timeout ? timeout / 1000 : 0;
  1519. tv.tv_usec = timeout ? (timeout * 1000) % 1000000 : 0;
  1520. #endif
  1521. array_init(return_value);
  1522. add_assoc_long(return_value, "sec", tv.tv_sec);
  1523. add_assoc_long(return_value, "usec", tv.tv_usec);
  1524. return;
  1525. #ifdef SO_ACCEPTFILTER
  1526. case SO_ACCEPTFILTER: {
  1527. struct accept_filter_arg af = {0};
  1528. optlen = sizeof(af);
  1529. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&af, &optlen) != 0) {
  1530. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1531. RETURN_FALSE;
  1532. }
  1533. array_init(return_value);
  1534. add_assoc_string(return_value, "af_name", af.af_name);
  1535. return;
  1536. }
  1537. #endif
  1538. }
  1539. }
  1540. optlen = sizeof(other_val);
  1541. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&other_val, &optlen) != 0) {
  1542. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1543. RETURN_FALSE;
  1544. }
  1545. if (optlen == 1) {
  1546. other_val = *((unsigned char *)&other_val);
  1547. }
  1548. RETURN_LONG(other_val);
  1549. }
  1550. /* }}} */
  1551. /* {{{ Sets socket options for the socket */
  1552. PHP_FUNCTION(socket_set_option)
  1553. {
  1554. zval *arg1, *arg4;
  1555. struct linger lv;
  1556. php_socket *php_sock;
  1557. int ov, optlen, retval;
  1558. #ifdef PHP_WIN32
  1559. int timeout;
  1560. #else
  1561. struct timeval tv;
  1562. #endif
  1563. zend_long level, optname;
  1564. void *opt_ptr;
  1565. HashTable *opt_ht;
  1566. zval *l_onoff, *l_linger;
  1567. zval *sec, *usec;
  1568. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollz", &arg1, socket_ce, &level, &optname, &arg4) == FAILURE) {
  1569. RETURN_THROWS();
  1570. }
  1571. php_sock = Z_SOCKET_P(arg1);
  1572. ENSURE_SOCKET_VALID(php_sock);
  1573. set_errno(0);
  1574. #define HANDLE_SUBCALL(res) \
  1575. do { \
  1576. if (res == 1) { goto default_case; } \
  1577. else if (res == SUCCESS) { RETURN_TRUE; } \
  1578. else { RETURN_FALSE; } \
  1579. } while (0)
  1580. if (level == IPPROTO_IP) {
  1581. int res = php_do_setsockopt_ip_mcast(php_sock, level, optname, arg4);
  1582. HANDLE_SUBCALL(res);
  1583. }
  1584. #if HAVE_IPV6
  1585. else if (level == IPPROTO_IPV6) {
  1586. int res = php_do_setsockopt_ipv6_mcast(php_sock, level, optname, arg4);
  1587. if (res == 1) {
  1588. res = php_do_setsockopt_ipv6_rfc3542(php_sock, level, optname, arg4);
  1589. }
  1590. HANDLE_SUBCALL(res);
  1591. }
  1592. #endif
  1593. switch (optname) {
  1594. case SO_LINGER: {
  1595. const char l_onoff_key[] = "l_onoff";
  1596. const char l_linger_key[] = "l_linger";
  1597. convert_to_array(arg4);
  1598. opt_ht = Z_ARRVAL_P(arg4);
  1599. if ((l_onoff = zend_hash_str_find(opt_ht, l_onoff_key, sizeof(l_onoff_key) - 1)) == NULL) {
  1600. zend_argument_value_error(4, "must have key \"%s\"", l_onoff_key);
  1601. RETURN_THROWS();
  1602. }
  1603. if ((l_linger = zend_hash_str_find(opt_ht, l_linger_key, sizeof(l_linger_key) - 1)) == NULL) {
  1604. zend_argument_value_error(4, "must have key \"%s\"", l_linger_key);
  1605. RETURN_THROWS();
  1606. }
  1607. convert_to_long(l_onoff);
  1608. convert_to_long(l_linger);
  1609. lv.l_onoff = (unsigned short)Z_LVAL_P(l_onoff);
  1610. lv.l_linger = (unsigned short)Z_LVAL_P(l_linger);
  1611. optlen = sizeof(lv);
  1612. opt_ptr = &lv;
  1613. break;
  1614. }
  1615. case SO_RCVTIMEO:
  1616. case SO_SNDTIMEO: {
  1617. const char sec_key[] = "sec";
  1618. const char usec_key[] = "usec";
  1619. convert_to_array(arg4);
  1620. opt_ht = Z_ARRVAL_P(arg4);
  1621. if ((sec = zend_hash_str_find(opt_ht, sec_key, sizeof(sec_key) - 1)) == NULL) {
  1622. zend_argument_value_error(4, "must have key \"%s\"", sec_key);
  1623. RETURN_THROWS();
  1624. }
  1625. if ((usec = zend_hash_str_find(opt_ht, usec_key, sizeof(usec_key) - 1)) == NULL) {
  1626. zend_argument_value_error(4, "must have key \"%s\"", usec_key);
  1627. RETURN_THROWS();
  1628. }
  1629. convert_to_long(sec);
  1630. convert_to_long(usec);
  1631. #ifndef PHP_WIN32
  1632. tv.tv_sec = Z_LVAL_P(sec);
  1633. tv.tv_usec = Z_LVAL_P(usec);
  1634. optlen = sizeof(tv);
  1635. opt_ptr = &tv;
  1636. #else
  1637. timeout = Z_LVAL_P(sec) * 1000 + Z_LVAL_P(usec) / 1000;
  1638. optlen = sizeof(int);
  1639. opt_ptr = &timeout;
  1640. #endif
  1641. break;
  1642. }
  1643. #ifdef SO_BINDTODEVICE
  1644. case SO_BINDTODEVICE: {
  1645. if (Z_TYPE_P(arg4) == IS_STRING) {
  1646. opt_ptr = Z_STRVAL_P(arg4);
  1647. optlen = Z_STRLEN_P(arg4);
  1648. } else {
  1649. opt_ptr = "";
  1650. optlen = 0;
  1651. }
  1652. break;
  1653. }
  1654. #endif
  1655. #ifdef SO_ACCEPTFILTER
  1656. case SO_ACCEPTFILTER: {
  1657. if (Z_TYPE_P(arg4) != IS_STRING) {
  1658. php_error_docref(NULL, E_WARNING, "Invalid filter argument type");
  1659. RETURN_FALSE;
  1660. }
  1661. struct accept_filter_arg af = {0};
  1662. strlcpy(af.af_name, Z_STRVAL_P(arg4), sizeof(af.af_name));
  1663. opt_ptr = &af;
  1664. optlen = sizeof(af);
  1665. break;
  1666. }
  1667. #endif
  1668. default:
  1669. default_case:
  1670. convert_to_long(arg4);
  1671. ov = Z_LVAL_P(arg4);
  1672. optlen = sizeof(ov);
  1673. opt_ptr = &ov;
  1674. break;
  1675. }
  1676. retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
  1677. if (retval != 0) {
  1678. PHP_SOCKET_ERROR(php_sock, "Unable to set socket option", errno);
  1679. RETURN_FALSE;
  1680. }
  1681. RETURN_TRUE;
  1682. }
  1683. /* }}} */
  1684. #ifdef HAVE_SOCKETPAIR
  1685. /* {{{ Creates a pair of indistinguishable sockets and stores them in fds. */
  1686. PHP_FUNCTION(socket_create_pair)
  1687. {
  1688. zval retval[2], *fds_array_zval;
  1689. php_socket *php_sock[2];
  1690. PHP_SOCKET fds_array[2];
  1691. zend_long domain, type, protocol;
  1692. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lllz", &domain, &type, &protocol, &fds_array_zval) == FAILURE) {
  1693. RETURN_THROWS();
  1694. }
  1695. if (domain != AF_INET
  1696. #if HAVE_IPV6
  1697. && domain != AF_INET6
  1698. #endif
  1699. && domain != AF_UNIX) {
  1700. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET6, or AF_INET");
  1701. RETURN_THROWS();
  1702. }
  1703. if (type > 10) {
  1704. zend_argument_value_error(2, "must be one of SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET,"
  1705. " SOCK_RAW, or SOCK_RDM");
  1706. RETURN_THROWS();
  1707. }
  1708. object_init_ex(&retval[0], socket_ce);
  1709. php_sock[0] = Z_SOCKET_P(&retval[0]);
  1710. object_init_ex(&retval[1], socket_ce);
  1711. php_sock[1] = Z_SOCKET_P(&retval[1]);
  1712. if (socketpair(domain, type, protocol, fds_array) != 0) {
  1713. SOCKETS_G(last_error) = errno;
  1714. php_error_docref(NULL, E_WARNING, "Unable to create socket pair [%d]: %s", errno, sockets_strerror(errno));
  1715. zval_ptr_dtor(&retval[0]);
  1716. zval_ptr_dtor(&retval[1]);
  1717. RETURN_FALSE;
  1718. }
  1719. fds_array_zval = zend_try_array_init(fds_array_zval);
  1720. if (!fds_array_zval) {
  1721. zval_ptr_dtor(&retval[0]);
  1722. zval_ptr_dtor(&retval[1]);
  1723. RETURN_THROWS();
  1724. }
  1725. php_sock[0]->bsd_socket = fds_array[0];
  1726. php_sock[1]->bsd_socket = fds_array[1];
  1727. php_sock[0]->type = domain;
  1728. php_sock[1]->type = domain;
  1729. php_sock[0]->error = 0;
  1730. php_sock[1]->error = 0;
  1731. php_sock[0]->blocking = 1;
  1732. php_sock[1]->blocking = 1;
  1733. add_index_zval(fds_array_zval, 0, &retval[0]);
  1734. add_index_zval(fds_array_zval, 1, &retval[1]);
  1735. RETURN_TRUE;
  1736. }
  1737. /* }}} */
  1738. #endif
  1739. #ifdef HAVE_SHUTDOWN
  1740. /* {{{ Shuts down a socket for receiving, sending, or both. */
  1741. PHP_FUNCTION(socket_shutdown)
  1742. {
  1743. zval *arg1;
  1744. zend_long how_shutdown = 2;
  1745. php_socket *php_sock;
  1746. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &arg1, socket_ce, &how_shutdown) == FAILURE) {
  1747. RETURN_THROWS();
  1748. }
  1749. php_sock = Z_SOCKET_P(arg1);
  1750. ENSURE_SOCKET_VALID(php_sock);
  1751. if (shutdown(php_sock->bsd_socket, how_shutdown) != 0) {
  1752. PHP_SOCKET_ERROR(php_sock, "Unable to shutdown socket", errno);
  1753. RETURN_FALSE;
  1754. }
  1755. RETURN_TRUE;
  1756. }
  1757. /* }}} */
  1758. #endif
  1759. /* {{{ Returns the last socket error (either the last used or the provided socket resource) */
  1760. PHP_FUNCTION(socket_last_error)
  1761. {
  1762. zval *arg1 = NULL;
  1763. php_socket *php_sock;
  1764. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &arg1, socket_ce) == FAILURE) {
  1765. RETURN_THROWS();
  1766. }
  1767. if (arg1) {
  1768. php_sock = Z_SOCKET_P(arg1);
  1769. ENSURE_SOCKET_VALID(php_sock);
  1770. RETVAL_LONG(php_sock->error);
  1771. } else {
  1772. RETVAL_LONG(SOCKETS_G(last_error));
  1773. }
  1774. }
  1775. /* }}} */
  1776. /* {{{ Clears the error on the socket or the last error code. */
  1777. PHP_FUNCTION(socket_clear_error)
  1778. {
  1779. zval *arg1 = NULL;
  1780. php_socket *php_sock;
  1781. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &arg1, socket_ce) == FAILURE) {
  1782. RETURN_THROWS();
  1783. }
  1784. if (arg1) {
  1785. php_sock = Z_SOCKET_P(arg1);
  1786. ENSURE_SOCKET_VALID(php_sock);
  1787. php_sock->error = 0;
  1788. } else {
  1789. SOCKETS_G(last_error) = 0;
  1790. }
  1791. return;
  1792. }
  1793. /* }}} */
  1794. int socket_import_file_descriptor(PHP_SOCKET socket, php_socket *retsock)
  1795. {
  1796. #ifdef SO_DOMAIN
  1797. int type;
  1798. socklen_t type_len = sizeof(type);
  1799. #endif
  1800. php_sockaddr_storage addr;
  1801. socklen_t addr_len = sizeof(addr);
  1802. #ifndef PHP_WIN32
  1803. int t;
  1804. #endif
  1805. retsock->bsd_socket = socket;
  1806. /* determine family */
  1807. #ifdef SO_DOMAIN
  1808. if (getsockopt(socket, SOL_SOCKET, SO_DOMAIN, &type, &type_len) == 0) {
  1809. retsock->type = type;
  1810. } else
  1811. #endif
  1812. if (getsockname(socket, (struct sockaddr*)&addr, &addr_len) == 0) {
  1813. retsock->type = addr.ss_family;
  1814. } else {
  1815. PHP_SOCKET_ERROR(retsock, "Unable to obtain socket family", errno);
  1816. return 0;
  1817. }
  1818. /* determine blocking mode */
  1819. #ifndef PHP_WIN32
  1820. t = fcntl(socket, F_GETFL);
  1821. if (t == -1) {
  1822. PHP_SOCKET_ERROR(retsock, "Unable to obtain blocking state", errno);
  1823. return 0;
  1824. } else {
  1825. retsock->blocking = !(t & O_NONBLOCK);
  1826. }
  1827. #endif
  1828. return 1;
  1829. }
  1830. /* {{{ Imports a stream that encapsulates a socket into a socket extension resource. */
  1831. PHP_FUNCTION(socket_import_stream)
  1832. {
  1833. zval *zstream;
  1834. php_stream *stream;
  1835. php_socket *retsock = NULL;
  1836. PHP_SOCKET socket; /* fd */
  1837. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream) == FAILURE) {
  1838. RETURN_THROWS();
  1839. }
  1840. php_stream_from_zval(stream, zstream);
  1841. if (php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void**)&socket, 1)) {
  1842. /* error supposedly already shown */
  1843. RETURN_FALSE;
  1844. }
  1845. object_init_ex(return_value, socket_ce);
  1846. retsock = Z_SOCKET_P(return_value);
  1847. if (!socket_import_file_descriptor(socket, retsock)) {
  1848. zval_ptr_dtor(return_value);
  1849. RETURN_FALSE;
  1850. }
  1851. #ifdef PHP_WIN32
  1852. /* on windows, check if the stream is a socket stream and read its
  1853. * private data; otherwise assume it's in non-blocking mode */
  1854. if (php_stream_is(stream, PHP_STREAM_IS_SOCKET)) {
  1855. retsock->blocking =
  1856. ((php_netstream_data_t *)stream->abstract)->is_blocked;
  1857. } else {
  1858. retsock->blocking = 1;
  1859. }
  1860. #endif
  1861. /* hold a zval reference to the stream (holding a php_stream* directly could
  1862. * also be done, but this makes socket_export_stream a bit simpler) */
  1863. ZVAL_COPY(&retsock->zstream, zstream);
  1864. php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
  1865. }
  1866. /* }}} */
  1867. /* {{{ Exports a socket extension resource into a stream that encapsulates a socket. */
  1868. PHP_FUNCTION(socket_export_stream)
  1869. {
  1870. zval *zsocket;
  1871. php_socket *socket;
  1872. php_stream *stream = NULL;
  1873. php_netstream_data_t *stream_data;
  1874. char *protocol = NULL;
  1875. size_t protocollen = 0;
  1876. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zsocket, socket_ce) == FAILURE) {
  1877. RETURN_THROWS();
  1878. }
  1879. socket = Z_SOCKET_P(zsocket);
  1880. ENSURE_SOCKET_VALID(socket);
  1881. /* Either we already exported a stream or the socket came from an import,
  1882. * just return the existing stream */
  1883. if (!Z_ISUNDEF(socket->zstream)) {
  1884. RETURN_COPY(&socket->zstream);
  1885. }
  1886. /* Determine if socket is using a protocol with one of the default registered
  1887. * socket stream wrappers */
  1888. if (socket->type == PF_INET
  1889. #if HAVE_IPV6
  1890. || socket->type == PF_INET6
  1891. #endif
  1892. ) {
  1893. int protoid;
  1894. socklen_t protoidlen = sizeof(protoid);
  1895. getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &protoid, &protoidlen);
  1896. if (protoid == SOCK_STREAM) {
  1897. /* SO_PROTOCOL is not (yet?) supported on OS X, so lets assume it's TCP there */
  1898. #ifdef SO_PROTOCOL
  1899. protoidlen = sizeof(protoid);
  1900. getsockopt(socket->bsd_socket, SOL_SOCKET, SO_PROTOCOL, (char *) &protoid, &protoidlen);
  1901. if (protoid == IPPROTO_TCP)
  1902. #endif
  1903. {
  1904. protocol = "tcp";
  1905. protocollen = 3;
  1906. }
  1907. } else if (protoid == SOCK_DGRAM) {
  1908. protocol = "udp";
  1909. protocollen = 3;
  1910. }
  1911. #ifdef PF_UNIX
  1912. } else if (socket->type == PF_UNIX) {
  1913. int type;
  1914. socklen_t typelen = sizeof(type);
  1915. getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &type, &typelen);
  1916. if (type == SOCK_STREAM) {
  1917. protocol = "unix";
  1918. protocollen = 4;
  1919. } else if (type == SOCK_DGRAM) {
  1920. protocol = "udg";
  1921. protocollen = 3;
  1922. }
  1923. #endif
  1924. }
  1925. /* Try to get a stream with the registered sockops for the protocol in use
  1926. * We don't want streams to actually *do* anything though, so don't give it
  1927. * anything apart from the protocol */
  1928. if (protocol != NULL) {
  1929. stream = php_stream_xport_create(protocol, protocollen, 0, 0, NULL, NULL, NULL, NULL, NULL);
  1930. }
  1931. /* Fall back to creating a generic socket stream */
  1932. if (stream == NULL) {
  1933. stream = php_stream_sock_open_from_socket(socket->bsd_socket, 0);
  1934. if (stream == NULL) {
  1935. php_error_docref(NULL, E_WARNING, "Failed to create stream");
  1936. RETURN_FALSE;
  1937. }
  1938. }
  1939. stream_data = (php_netstream_data_t *) stream->abstract;
  1940. stream_data->socket = socket->bsd_socket;
  1941. stream_data->is_blocked = socket->blocking;
  1942. stream_data->timeout.tv_sec = FG(default_socket_timeout);
  1943. stream_data->timeout.tv_usec = 0;
  1944. php_stream_to_zval(stream, &socket->zstream);
  1945. RETURN_COPY(&socket->zstream);
  1946. }
  1947. /* }}} */
  1948. /* {{{ Gets array with contents of getaddrinfo about the given hostname. */
  1949. PHP_FUNCTION(socket_addrinfo_lookup)
  1950. {
  1951. char *service = NULL;
  1952. size_t service_len = 0;
  1953. zend_string *hostname, *key;
  1954. zval *hint, *zhints = NULL;
  1955. struct addrinfo hints, *result, *rp;
  1956. php_addrinfo *res;
  1957. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|s!a", &hostname, &service, &service_len, &zhints) == FAILURE) {
  1958. RETURN_THROWS();
  1959. }
  1960. memset(&hints, 0, sizeof(hints));
  1961. if (zhints) {
  1962. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zhints), key, hint) {
  1963. if (key) {
  1964. if (zend_string_equals_literal(key, "ai_flags")) {
  1965. hints.ai_flags = zval_get_long(hint);
  1966. } else if (zend_string_equals_literal(key, "ai_socktype")) {
  1967. hints.ai_socktype = zval_get_long(hint);
  1968. } else if (zend_string_equals_literal(key, "ai_protocol")) {
  1969. hints.ai_protocol = zval_get_long(hint);
  1970. } else if (zend_string_equals_literal(key, "ai_family")) {
  1971. hints.ai_family = zval_get_long(hint);
  1972. } else {
  1973. zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
  1974. "\"ai_protocol\", or \"ai_family\"");
  1975. RETURN_THROWS();
  1976. }
  1977. }
  1978. } ZEND_HASH_FOREACH_END();
  1979. }
  1980. if (getaddrinfo(ZSTR_VAL(hostname), service, &hints, &result) != 0) {
  1981. RETURN_FALSE;
  1982. }
  1983. array_init(return_value);
  1984. for (rp = result; rp != NULL; rp = rp->ai_next) {
  1985. if (rp->ai_family != AF_UNSPEC) {
  1986. zval zaddr;
  1987. object_init_ex(&zaddr, address_info_ce);
  1988. res = Z_ADDRESS_INFO_P(&zaddr);
  1989. memcpy(&res->addrinfo, rp, sizeof(struct addrinfo));
  1990. res->addrinfo.ai_addr = emalloc(rp->ai_addrlen);
  1991. memcpy(res->addrinfo.ai_addr, rp->ai_addr, rp->ai_addrlen);
  1992. if (rp->ai_canonname != NULL) {
  1993. res->addrinfo.ai_canonname = estrdup(rp->ai_canonname);
  1994. }
  1995. add_next_index_zval(return_value, &zaddr);
  1996. }
  1997. }
  1998. freeaddrinfo(result);
  1999. }
  2000. /* }}} */
  2001. /* {{{ Creates and binds to a socket from a given addrinfo resource */
  2002. PHP_FUNCTION(socket_addrinfo_bind)
  2003. {
  2004. zval *arg1;
  2005. int retval;
  2006. php_addrinfo *ai;
  2007. php_socket *php_sock;
  2008. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, address_info_ce) == FAILURE) {
  2009. RETURN_THROWS();
  2010. }
  2011. ai = Z_ADDRESS_INFO_P(arg1);
  2012. object_init_ex(return_value, socket_ce);
  2013. php_sock = Z_SOCKET_P(return_value);
  2014. php_sock->bsd_socket = socket(ai->addrinfo.ai_family, ai->addrinfo.ai_socktype, ai->addrinfo.ai_protocol);
  2015. php_sock->type = ai->addrinfo.ai_family;
  2016. if (IS_INVALID_SOCKET(php_sock)) {
  2017. SOCKETS_G(last_error) = errno;
  2018. php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
  2019. zval_ptr_dtor(return_value);
  2020. RETURN_FALSE;
  2021. }
  2022. php_sock->error = 0;
  2023. php_sock->blocking = 1;
  2024. switch(php_sock->type) {
  2025. case AF_UNIX:
  2026. {
  2027. // AF_UNIX sockets via getaddrino are not implemented due to security problems
  2028. close(php_sock->bsd_socket);
  2029. zval_ptr_dtor(return_value);
  2030. RETURN_FALSE;
  2031. }
  2032. case AF_INET:
  2033. #if HAVE_IPV6
  2034. case AF_INET6:
  2035. #endif
  2036. {
  2037. retval = bind(php_sock->bsd_socket, ai->addrinfo.ai_addr, ai->addrinfo.ai_addrlen);
  2038. break;
  2039. }
  2040. default:
  2041. close(php_sock->bsd_socket);
  2042. zval_ptr_dtor(return_value);
  2043. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  2044. RETURN_THROWS();
  2045. }
  2046. if (retval != 0) {
  2047. PHP_SOCKET_ERROR(php_sock, "Unable to bind address", errno);
  2048. close(php_sock->bsd_socket);
  2049. zval_ptr_dtor(return_value);
  2050. RETURN_FALSE;
  2051. }
  2052. }
  2053. /* }}} */
  2054. /* {{{ Creates and connects to a socket from a given addrinfo resource */
  2055. PHP_FUNCTION(socket_addrinfo_connect)
  2056. {
  2057. zval *arg1;
  2058. int retval;
  2059. php_addrinfo *ai;
  2060. php_socket *php_sock;
  2061. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, address_info_ce) == FAILURE) {
  2062. RETURN_THROWS();
  2063. }
  2064. ai = Z_ADDRESS_INFO_P(arg1);
  2065. object_init_ex(return_value, socket_ce);
  2066. php_sock = Z_SOCKET_P(return_value);
  2067. php_sock->bsd_socket = socket(ai->addrinfo.ai_family, ai->addrinfo.ai_socktype, ai->addrinfo.ai_protocol);
  2068. php_sock->type = ai->addrinfo.ai_family;
  2069. if (IS_INVALID_SOCKET(php_sock)) {
  2070. SOCKETS_G(last_error) = errno;
  2071. php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
  2072. zval_ptr_dtor(return_value);
  2073. RETURN_FALSE;
  2074. }
  2075. php_sock->error = 0;
  2076. php_sock->blocking = 1;
  2077. switch(php_sock->type) {
  2078. case AF_UNIX:
  2079. {
  2080. // AF_UNIX sockets via getaddrino are not implemented due to security problems
  2081. close(php_sock->bsd_socket);
  2082. zval_ptr_dtor(return_value);
  2083. RETURN_FALSE;
  2084. }
  2085. case AF_INET:
  2086. #if HAVE_IPV6
  2087. case AF_INET6:
  2088. #endif
  2089. {
  2090. retval = connect(php_sock->bsd_socket, ai->addrinfo.ai_addr, ai->addrinfo.ai_addrlen);
  2091. break;
  2092. }
  2093. default:
  2094. zend_argument_value_error(1, "socket type must be one of AF_UNIX, AF_INET, or AF_INET6");
  2095. close(php_sock->bsd_socket);
  2096. zval_ptr_dtor(return_value);
  2097. RETURN_THROWS();
  2098. }
  2099. if (retval != 0) {
  2100. PHP_SOCKET_ERROR(php_sock, "Unable to connect address", errno);
  2101. close(php_sock->bsd_socket);
  2102. zval_ptr_dtor(return_value);
  2103. RETURN_FALSE;
  2104. }
  2105. }
  2106. /* }}} */
  2107. /* {{{ Creates and connects to a socket from a given addrinfo resource */
  2108. PHP_FUNCTION(socket_addrinfo_explain)
  2109. {
  2110. zval *arg1, sockaddr;
  2111. php_addrinfo *ai;
  2112. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, address_info_ce) == FAILURE) {
  2113. RETURN_THROWS();
  2114. }
  2115. ai = Z_ADDRESS_INFO_P(arg1);
  2116. array_init(return_value);
  2117. add_assoc_long(return_value, "ai_flags", ai->addrinfo.ai_flags);
  2118. add_assoc_long(return_value, "ai_family", ai->addrinfo.ai_family);
  2119. add_assoc_long(return_value, "ai_socktype", ai->addrinfo.ai_socktype);
  2120. add_assoc_long(return_value, "ai_protocol", ai->addrinfo.ai_protocol);
  2121. if (ai->addrinfo.ai_canonname != NULL) {
  2122. add_assoc_string(return_value, "ai_canonname", ai->addrinfo.ai_canonname);
  2123. }
  2124. array_init(&sockaddr);
  2125. switch (ai->addrinfo.ai_family) {
  2126. case AF_INET:
  2127. {
  2128. struct sockaddr_in *sa = (struct sockaddr_in *) ai->addrinfo.ai_addr;
  2129. char addr[INET_ADDRSTRLEN];
  2130. add_assoc_long(&sockaddr, "sin_port", ntohs((unsigned short) sa->sin_port));
  2131. inet_ntop(ai->addrinfo.ai_family, &sa->sin_addr, addr, sizeof(addr));
  2132. add_assoc_string(&sockaddr, "sin_addr", addr);
  2133. break;
  2134. }
  2135. #if HAVE_IPV6
  2136. case AF_INET6:
  2137. {
  2138. struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ai->addrinfo.ai_addr;
  2139. char addr[INET6_ADDRSTRLEN];
  2140. add_assoc_long(&sockaddr, "sin6_port", ntohs((unsigned short) sa->sin6_port));
  2141. inet_ntop(ai->addrinfo.ai_family, &sa->sin6_addr, addr, sizeof(addr));
  2142. add_assoc_string(&sockaddr, "sin6_addr", addr);
  2143. break;
  2144. }
  2145. #endif
  2146. }
  2147. add_assoc_zval(return_value, "ai_addr", &sockaddr);
  2148. }
  2149. /* }}} */
  2150. #ifdef PHP_WIN32
  2151. /* {{{ Exports the network socket information suitable to be used in another process and returns the info id. */
  2152. PHP_FUNCTION(socket_wsaprotocol_info_export)
  2153. {
  2154. WSAPROTOCOL_INFO wi;
  2155. zval *zsocket;
  2156. php_socket *socket;
  2157. zend_long target_pid;
  2158. zend_string *seg_name;
  2159. HANDLE map;
  2160. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &zsocket, socket_ce, &target_pid) == FAILURE) {
  2161. RETURN_THROWS();
  2162. }
  2163. socket = Z_SOCKET_P(zsocket);
  2164. ENSURE_SOCKET_VALID(socket);
  2165. if (SOCKET_ERROR == WSADuplicateSocket(socket->bsd_socket, (DWORD)target_pid, &wi)) {
  2166. DWORD err = WSAGetLastError();
  2167. char *buf = php_win32_error_to_msg(err);
  2168. if (!buf[0]) {
  2169. php_error_docref(NULL, E_WARNING, "Unable to export WSA protocol info [0x%08lx]", err);
  2170. } else {
  2171. php_error_docref(NULL, E_WARNING, "Unable to export WSA protocol info [0x%08lx]: %s", err, buf);
  2172. }
  2173. php_win32_error_msg_free(buf);
  2174. RETURN_FALSE;
  2175. }
  2176. seg_name = zend_strpprintf(0, "php_wsa_for_%u", SOCKETS_G(wsa_child_count)++);
  2177. map = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(WSAPROTOCOL_INFO), ZSTR_VAL(seg_name));
  2178. if (NULL != map) {
  2179. LPVOID view = MapViewOfFile(map, FILE_MAP_WRITE, 0, 0, 0);
  2180. if (view) {
  2181. memcpy(view, &wi, sizeof(wi));
  2182. UnmapViewOfFile(view);
  2183. zend_hash_add_ptr(&(SOCKETS_G(wsa_info)), seg_name, map);
  2184. RETURN_STR(seg_name);
  2185. } else {
  2186. DWORD err = GetLastError();
  2187. php_error_docref(NULL, E_WARNING, "Unable to map file view [0x%08lx]", err);
  2188. }
  2189. } else {
  2190. DWORD err = GetLastError();
  2191. php_error_docref(NULL, E_WARNING, "Unable to create file mapping [0x%08lx]", err);
  2192. }
  2193. zend_string_release_ex(seg_name, 0);
  2194. RETURN_FALSE;
  2195. }
  2196. /* }}} */
  2197. /* {{{ Imports the network socket information using the supplied id and creates a new socket on its base. */
  2198. PHP_FUNCTION(socket_wsaprotocol_info_import)
  2199. {
  2200. char *id;
  2201. size_t id_len;
  2202. WSAPROTOCOL_INFO wi;
  2203. PHP_SOCKET sock;
  2204. php_socket *php_sock;
  2205. HANDLE map;
  2206. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &id, &id_len) == FAILURE) {
  2207. RETURN_THROWS();
  2208. }
  2209. map = OpenFileMapping(FILE_MAP_READ, FALSE, id);
  2210. if (map) {
  2211. LPVOID view = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
  2212. if (view) {
  2213. memcpy(&wi, view, sizeof(WSAPROTOCOL_INFO));
  2214. UnmapViewOfFile(view);
  2215. } else {
  2216. DWORD err = GetLastError();
  2217. php_error_docref(NULL, E_WARNING, "Unable to map file view [0x%08lx]", err);
  2218. RETURN_FALSE;
  2219. }
  2220. CloseHandle(map);
  2221. } else {
  2222. DWORD err = GetLastError();
  2223. php_error_docref(NULL, E_WARNING, "Unable to open file mapping [0x%08lx]", err);
  2224. RETURN_FALSE;
  2225. }
  2226. sock = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, &wi, 0, 0);
  2227. if (INVALID_SOCKET == sock) {
  2228. DWORD err = WSAGetLastError();
  2229. char *buf = php_win32_error_to_msg(err);
  2230. if (!buf[0]) {
  2231. php_error_docref(NULL, E_WARNING, "Unable to import WSA protocol info [0x%08lx]", err);
  2232. } else {
  2233. php_error_docref(NULL, E_WARNING, "Unable to import WSA protocol info [0x%08lx]: %s", err, buf);
  2234. }
  2235. php_win32_error_msg_free(buf);
  2236. RETURN_FALSE;
  2237. }
  2238. object_init_ex(return_value, socket_ce);
  2239. php_sock = Z_SOCKET_P(return_value);
  2240. php_sock->bsd_socket = sock;
  2241. php_sock->type = wi.iAddressFamily;
  2242. php_sock->error = 0;
  2243. php_sock->blocking = 1;
  2244. }
  2245. /* }}} */
  2246. /* {{{ Frees the exported info and corresponding resources using the supplied id. */
  2247. PHP_FUNCTION(socket_wsaprotocol_info_release)
  2248. {
  2249. char *id;
  2250. size_t id_len;
  2251. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &id, &id_len) == FAILURE) {
  2252. RETURN_THROWS();
  2253. }
  2254. RETURN_BOOL(SUCCESS == zend_hash_str_del(&(SOCKETS_G(wsa_info)), id, id_len));
  2255. }
  2256. /* }}} */
  2257. #endif