sockets.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716
  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. if (!PHP_SAFE_MAX_FD(max_fd, 0)) {
  654. RETURN_FALSE;
  655. }
  656. /* If seconds is not set to null, build the timeval, else we wait indefinitely */
  657. if (!sec_is_null) {
  658. /* Solaris + BSD do not like microsecond values which are >= 1 sec */
  659. if (usec > 999999) {
  660. tv.tv_sec = sec + (usec / 1000000);
  661. tv.tv_usec = usec % 1000000;
  662. } else {
  663. tv.tv_sec = sec;
  664. tv.tv_usec = usec;
  665. }
  666. tv_p = &tv;
  667. }
  668. retval = select(max_fd+1, &rfds, &wfds, &efds, tv_p);
  669. if (retval == -1) {
  670. SOCKETS_G(last_error) = errno;
  671. php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s", errno, sockets_strerror(errno));
  672. RETURN_FALSE;
  673. }
  674. if (r_array != NULL) php_sock_array_from_fd_set(r_array, &rfds);
  675. if (w_array != NULL) php_sock_array_from_fd_set(w_array, &wfds);
  676. if (e_array != NULL) php_sock_array_from_fd_set(e_array, &efds);
  677. RETURN_LONG(retval);
  678. }
  679. /* }}} */
  680. /* {{{ Opens a socket on port to accept connections */
  681. PHP_FUNCTION(socket_create_listen)
  682. {
  683. php_socket *php_sock;
  684. zend_long port, backlog = 128;
  685. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &port, &backlog) == FAILURE) {
  686. RETURN_THROWS();
  687. }
  688. object_init_ex(return_value, socket_ce);
  689. php_sock = Z_SOCKET_P(return_value);
  690. if (!php_open_listen_sock(php_sock, port, backlog)) {
  691. zval_ptr_dtor(return_value);
  692. RETURN_FALSE;
  693. }
  694. php_sock->error = 0;
  695. php_sock->blocking = 1;
  696. }
  697. /* }}} */
  698. /* {{{ Accepts a connection on the listening socket fd */
  699. PHP_FUNCTION(socket_accept)
  700. {
  701. zval *arg1;
  702. php_socket *php_sock, *new_sock;
  703. php_sockaddr_storage sa;
  704. socklen_t php_sa_len = sizeof(sa);
  705. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, socket_ce) == FAILURE) {
  706. RETURN_THROWS();
  707. }
  708. php_sock = Z_SOCKET_P(arg1);
  709. ENSURE_SOCKET_VALID(php_sock);
  710. object_init_ex(return_value, socket_ce);
  711. new_sock = Z_SOCKET_P(return_value);
  712. if (!php_accept_connect(php_sock, new_sock, (struct sockaddr*)&sa, &php_sa_len)) {
  713. zval_ptr_dtor(return_value);
  714. RETURN_FALSE;
  715. }
  716. }
  717. /* }}} */
  718. /* {{{ Sets nonblocking mode on a socket resource */
  719. PHP_FUNCTION(socket_set_nonblock)
  720. {
  721. zval *arg1;
  722. php_socket *php_sock;
  723. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, socket_ce) == FAILURE) {
  724. RETURN_THROWS();
  725. }
  726. php_sock = Z_SOCKET_P(arg1);
  727. ENSURE_SOCKET_VALID(php_sock);
  728. if (!Z_ISUNDEF(php_sock->zstream)) {
  729. php_stream *stream;
  730. /* omit notice if resource doesn't exist anymore */
  731. stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
  732. if (stream != NULL) {
  733. if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 0,
  734. NULL) != -1) {
  735. php_sock->blocking = 0;
  736. RETURN_TRUE;
  737. }
  738. }
  739. }
  740. if (php_set_sock_blocking(php_sock->bsd_socket, 0) == SUCCESS) {
  741. php_sock->blocking = 0;
  742. RETURN_TRUE;
  743. } else {
  744. PHP_SOCKET_ERROR(php_sock, "unable to set nonblocking mode", errno);
  745. RETURN_FALSE;
  746. }
  747. }
  748. /* }}} */
  749. /* {{{ Sets blocking mode on a socket resource */
  750. PHP_FUNCTION(socket_set_block)
  751. {
  752. zval *arg1;
  753. php_socket *php_sock;
  754. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, socket_ce) == FAILURE) {
  755. RETURN_THROWS();
  756. }
  757. php_sock = Z_SOCKET_P(arg1);
  758. ENSURE_SOCKET_VALID(php_sock);
  759. /* if socket was created from a stream, give the stream a chance to take
  760. * care of the operation itself, thereby allowing it to update its internal
  761. * state */
  762. if (!Z_ISUNDEF(php_sock->zstream)) {
  763. php_stream *stream;
  764. stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
  765. if (stream != NULL) {
  766. if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 1,
  767. NULL) != -1) {
  768. php_sock->blocking = 1;
  769. RETURN_TRUE;
  770. }
  771. }
  772. }
  773. if (php_set_sock_blocking(php_sock->bsd_socket, 1) == SUCCESS) {
  774. php_sock->blocking = 1;
  775. RETURN_TRUE;
  776. } else {
  777. PHP_SOCKET_ERROR(php_sock, "unable to set blocking mode", errno);
  778. RETURN_FALSE;
  779. }
  780. }
  781. /* }}} */
  782. /* {{{ Sets the maximum number of connections allowed to be waited for on the socket specified by fd */
  783. PHP_FUNCTION(socket_listen)
  784. {
  785. zval *arg1;
  786. php_socket *php_sock;
  787. zend_long backlog = 0;
  788. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &arg1, socket_ce, &backlog) == FAILURE) {
  789. RETURN_THROWS();
  790. }
  791. php_sock = Z_SOCKET_P(arg1);
  792. ENSURE_SOCKET_VALID(php_sock);
  793. if (listen(php_sock->bsd_socket, backlog) != 0) {
  794. PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno);
  795. RETURN_FALSE;
  796. }
  797. RETURN_TRUE;
  798. }
  799. /* }}} */
  800. /* {{{ Closes a file descriptor */
  801. PHP_FUNCTION(socket_close)
  802. {
  803. zval *arg1;
  804. php_socket *php_socket;
  805. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, socket_ce) == FAILURE) {
  806. RETURN_THROWS();
  807. }
  808. php_socket = Z_SOCKET_P(arg1);
  809. ENSURE_SOCKET_VALID(php_socket);
  810. if (!Z_ISUNDEF(php_socket->zstream)) {
  811. php_stream *stream = NULL;
  812. php_stream_from_zval_no_verify(stream, &php_socket->zstream);
  813. if (stream != NULL) {
  814. /* close & destroy stream, incl. removing it from the rsrc list;
  815. * resource stored in php_sock->zstream will become invalid */
  816. php_stream_free(stream,
  817. PHP_STREAM_FREE_KEEP_RSRC | PHP_STREAM_FREE_CLOSE |
  818. (stream->is_persistent?PHP_STREAM_FREE_CLOSE_PERSISTENT:0));
  819. }
  820. } else {
  821. if (!IS_INVALID_SOCKET(php_socket)) {
  822. close(php_socket->bsd_socket);
  823. }
  824. }
  825. ZVAL_UNDEF(&php_socket->zstream);
  826. php_socket->bsd_socket = -1;
  827. }
  828. /* }}} */
  829. /* {{{ Writes the buffer to the socket resource, length is optional */
  830. PHP_FUNCTION(socket_write)
  831. {
  832. zval *arg1;
  833. php_socket *php_sock;
  834. int retval;
  835. size_t str_len;
  836. zend_long length = 0;
  837. bool length_is_null = 1;
  838. char *str;
  839. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l!", &arg1, socket_ce, &str, &str_len, &length, &length_is_null) == FAILURE) {
  840. RETURN_THROWS();
  841. }
  842. php_sock = Z_SOCKET_P(arg1);
  843. ENSURE_SOCKET_VALID(php_sock);
  844. if (length < 0) {
  845. zend_argument_value_error(3, "must be greater than or equal to 0");
  846. RETURN_THROWS();
  847. }
  848. if (length_is_null) {
  849. length = str_len;
  850. }
  851. #ifndef PHP_WIN32
  852. retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
  853. #else
  854. retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
  855. #endif
  856. if (retval < 0) {
  857. PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
  858. RETURN_FALSE;
  859. }
  860. RETURN_LONG(retval);
  861. }
  862. /* }}} */
  863. /* {{{ Reads a maximum of length bytes from socket */
  864. PHP_FUNCTION(socket_read)
  865. {
  866. zval *arg1;
  867. php_socket *php_sock;
  868. zend_string *tmpbuf;
  869. int retval;
  870. zend_long length, type = PHP_BINARY_READ;
  871. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|l", &arg1, socket_ce, &length, &type) == FAILURE) {
  872. RETURN_THROWS();
  873. }
  874. php_sock = Z_SOCKET_P(arg1);
  875. ENSURE_SOCKET_VALID(php_sock);
  876. /* overflow check */
  877. if ((length + 1) < 2) {
  878. RETURN_FALSE;
  879. }
  880. tmpbuf = zend_string_alloc(length, 0);
  881. if (type == PHP_NORMAL_READ) {
  882. retval = php_read(php_sock, ZSTR_VAL(tmpbuf), length, 0);
  883. } else {
  884. retval = recv(php_sock->bsd_socket, ZSTR_VAL(tmpbuf), length, 0);
  885. }
  886. if (retval == -1) {
  887. /* if the socket is in non-blocking mode and there's no data to read,
  888. don't output any error, as this is a normal situation, and not an error */
  889. if (PHP_IS_TRANSIENT_ERROR(errno)) {
  890. php_sock->error = errno;
  891. SOCKETS_G(last_error) = errno;
  892. } else {
  893. PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
  894. }
  895. zend_string_efree(tmpbuf);
  896. RETURN_FALSE;
  897. } else if (!retval) {
  898. zend_string_efree(tmpbuf);
  899. RETURN_EMPTY_STRING();
  900. }
  901. tmpbuf = zend_string_truncate(tmpbuf, retval, 0);
  902. ZSTR_LEN(tmpbuf) = retval;
  903. ZSTR_VAL(tmpbuf)[ZSTR_LEN(tmpbuf)] = '\0' ;
  904. RETURN_NEW_STR(tmpbuf);
  905. }
  906. /* }}} */
  907. /* {{{ 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. */
  908. PHP_FUNCTION(socket_getsockname)
  909. {
  910. zval *arg1, *addr, *port = NULL;
  911. php_sockaddr_storage sa_storage;
  912. php_socket *php_sock;
  913. struct sockaddr *sa;
  914. struct sockaddr_in *sin;
  915. #if HAVE_IPV6
  916. struct sockaddr_in6 *sin6;
  917. #endif
  918. #ifdef HAVE_INET_NTOP
  919. char addrbuf[INET6_ADDRSTRLEN];
  920. #endif
  921. struct sockaddr_un *s_un;
  922. const char *addr_string;
  923. socklen_t salen = sizeof(php_sockaddr_storage);
  924. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &addr, &port) == FAILURE) {
  925. RETURN_THROWS();
  926. }
  927. php_sock = Z_SOCKET_P(arg1);
  928. ENSURE_SOCKET_VALID(php_sock);
  929. sa = (struct sockaddr *) &sa_storage;
  930. if (getsockname(php_sock->bsd_socket, sa, &salen) != 0) {
  931. PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket name", errno);
  932. RETURN_FALSE;
  933. }
  934. switch (sa->sa_family) {
  935. #if HAVE_IPV6
  936. case AF_INET6:
  937. sin6 = (struct sockaddr_in6 *) sa;
  938. inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
  939. ZEND_TRY_ASSIGN_REF_STRING(addr, addrbuf);
  940. if (port != NULL) {
  941. ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin6->sin6_port));
  942. }
  943. RETURN_TRUE;
  944. break;
  945. #endif
  946. case AF_INET:
  947. sin = (struct sockaddr_in *) sa;
  948. #ifdef HAVE_INET_NTOP
  949. addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
  950. #else
  951. while (inet_ntoa_lock == 1);
  952. inet_ntoa_lock = 1;
  953. addr_string = inet_ntoa(sin->sin_addr);
  954. inet_ntoa_lock = 0;
  955. #endif
  956. ZEND_TRY_ASSIGN_REF_STRING(addr, addr_string);
  957. if (port != NULL) {
  958. ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin->sin_port));
  959. }
  960. RETURN_TRUE;
  961. break;
  962. case AF_UNIX:
  963. s_un = (struct sockaddr_un *) sa;
  964. ZEND_TRY_ASSIGN_REF_STRING(addr, s_un->sun_path);
  965. RETURN_TRUE;
  966. break;
  967. default:
  968. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  969. RETURN_THROWS();
  970. }
  971. }
  972. /* }}} */
  973. /* {{{ 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. */
  974. PHP_FUNCTION(socket_getpeername)
  975. {
  976. zval *arg1, *arg2, *arg3 = NULL;
  977. php_sockaddr_storage sa_storage;
  978. php_socket *php_sock;
  979. struct sockaddr *sa;
  980. struct sockaddr_in *sin;
  981. #if HAVE_IPV6
  982. struct sockaddr_in6 *sin6;
  983. #endif
  984. #ifdef HAVE_INET_NTOP
  985. char addrbuf[INET6_ADDRSTRLEN];
  986. #endif
  987. struct sockaddr_un *s_un;
  988. const char *addr_string;
  989. socklen_t salen = sizeof(php_sockaddr_storage);
  990. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &arg2, &arg3) == FAILURE) {
  991. RETURN_THROWS();
  992. }
  993. php_sock = Z_SOCKET_P(arg1);
  994. ENSURE_SOCKET_VALID(php_sock);
  995. sa = (struct sockaddr *) &sa_storage;
  996. if (getpeername(php_sock->bsd_socket, sa, &salen) < 0) {
  997. PHP_SOCKET_ERROR(php_sock, "unable to retrieve peer name", errno);
  998. RETURN_FALSE;
  999. }
  1000. switch (sa->sa_family) {
  1001. #if HAVE_IPV6
  1002. case AF_INET6:
  1003. sin6 = (struct sockaddr_in6 *) sa;
  1004. inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
  1005. ZEND_TRY_ASSIGN_REF_STRING(arg2, addrbuf);
  1006. if (arg3 != NULL) {
  1007. ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin6->sin6_port));
  1008. }
  1009. RETURN_TRUE;
  1010. break;
  1011. #endif
  1012. case AF_INET:
  1013. sin = (struct sockaddr_in *) sa;
  1014. #ifdef HAVE_INET_NTOP
  1015. addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
  1016. #else
  1017. while (inet_ntoa_lock == 1);
  1018. inet_ntoa_lock = 1;
  1019. addr_string = inet_ntoa(sin->sin_addr);
  1020. inet_ntoa_lock = 0;
  1021. #endif
  1022. ZEND_TRY_ASSIGN_REF_STRING(arg2, addr_string);
  1023. if (arg3 != NULL) {
  1024. ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin->sin_port));
  1025. }
  1026. RETURN_TRUE;
  1027. break;
  1028. case AF_UNIX:
  1029. s_un = (struct sockaddr_un *) sa;
  1030. ZEND_TRY_ASSIGN_REF_STRING(arg2, s_un->sun_path);
  1031. RETURN_TRUE;
  1032. break;
  1033. default:
  1034. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1035. RETURN_THROWS();
  1036. }
  1037. }
  1038. /* }}} */
  1039. /* {{{ Creates an endpoint for communication in the domain specified by domain, of type specified by type */
  1040. PHP_FUNCTION(socket_create)
  1041. {
  1042. zend_long domain, type, protocol;
  1043. php_socket *php_sock;
  1044. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &domain, &type, &protocol) == FAILURE) {
  1045. RETURN_THROWS();
  1046. }
  1047. if (domain != AF_UNIX
  1048. #if HAVE_IPV6
  1049. && domain != AF_INET6
  1050. #endif
  1051. && domain != AF_INET) {
  1052. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET6, or AF_INET");
  1053. RETURN_THROWS();
  1054. }
  1055. if (type > 10) {
  1056. zend_argument_value_error(2, "must be one of SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET,"
  1057. " SOCK_RAW, or SOCK_RDM");
  1058. RETURN_THROWS();
  1059. }
  1060. object_init_ex(return_value, socket_ce);
  1061. php_sock = Z_SOCKET_P(return_value);
  1062. php_sock->bsd_socket = socket(domain, type, protocol);
  1063. php_sock->type = domain;
  1064. if (IS_INVALID_SOCKET(php_sock)) {
  1065. SOCKETS_G(last_error) = errno;
  1066. php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
  1067. zval_ptr_dtor(return_value);
  1068. RETURN_FALSE;
  1069. }
  1070. php_sock->error = 0;
  1071. php_sock->blocking = 1;
  1072. }
  1073. /* }}} */
  1074. /* {{{ Opens a connection to addr:port on the socket specified by socket */
  1075. PHP_FUNCTION(socket_connect)
  1076. {
  1077. zval *resource_socket;
  1078. php_socket *php_sock;
  1079. char *addr;
  1080. int retval;
  1081. size_t addr_len;
  1082. zend_long port;
  1083. bool port_is_null = 1;
  1084. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l!", &resource_socket, socket_ce, &addr, &addr_len, &port, &port_is_null) == FAILURE) {
  1085. RETURN_THROWS();
  1086. }
  1087. php_sock = Z_SOCKET_P(resource_socket);
  1088. ENSURE_SOCKET_VALID(php_sock);
  1089. switch(php_sock->type) {
  1090. #if HAVE_IPV6
  1091. case AF_INET6: {
  1092. struct sockaddr_in6 sin6 = {0};
  1093. if (port_is_null) {
  1094. zend_argument_value_error(3, "cannot be null when the socket type is AF_INET6");
  1095. RETURN_THROWS();
  1096. }
  1097. memset(&sin6, 0, sizeof(struct sockaddr_in6));
  1098. sin6.sin6_family = AF_INET6;
  1099. sin6.sin6_port = htons((unsigned short int)port);
  1100. if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
  1101. RETURN_FALSE;
  1102. }
  1103. retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6));
  1104. break;
  1105. }
  1106. #endif
  1107. case AF_INET: {
  1108. struct sockaddr_in sin = {0};
  1109. if (port_is_null) {
  1110. zend_argument_value_error(3, "cannot be null when the socket type is AF_INET");
  1111. RETURN_THROWS();
  1112. }
  1113. sin.sin_family = AF_INET;
  1114. sin.sin_port = htons((unsigned short int)port);
  1115. if (! php_set_inet_addr(&sin, addr, php_sock)) {
  1116. RETURN_FALSE;
  1117. }
  1118. retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
  1119. break;
  1120. }
  1121. case AF_UNIX: {
  1122. struct sockaddr_un s_un = {0};
  1123. if (addr_len >= sizeof(s_un.sun_path)) {
  1124. zend_argument_value_error(2, "must be less than %d", sizeof(s_un.sun_path));
  1125. RETURN_THROWS();
  1126. }
  1127. s_un.sun_family = AF_UNIX;
  1128. memcpy(&s_un.sun_path, addr, addr_len);
  1129. retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un,
  1130. (socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + addr_len));
  1131. break;
  1132. }
  1133. default:
  1134. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1135. RETURN_THROWS();
  1136. }
  1137. if (retval != 0) {
  1138. PHP_SOCKET_ERROR(php_sock, "unable to connect", errno);
  1139. RETURN_FALSE;
  1140. }
  1141. RETURN_TRUE;
  1142. }
  1143. /* }}} */
  1144. /* {{{ Returns a string describing an error */
  1145. PHP_FUNCTION(socket_strerror)
  1146. {
  1147. zend_long arg1;
  1148. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &arg1) == FAILURE) {
  1149. RETURN_THROWS();
  1150. }
  1151. RETURN_STRING(sockets_strerror(arg1));
  1152. }
  1153. /* }}} */
  1154. /* {{{ Binds an open socket to a listening port, port is only specified in AF_INET family. */
  1155. PHP_FUNCTION(socket_bind)
  1156. {
  1157. zval *arg1;
  1158. php_sockaddr_storage sa_storage = {0};
  1159. struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
  1160. php_socket *php_sock;
  1161. char *addr;
  1162. size_t addr_len;
  1163. zend_long port = 0;
  1164. zend_long retval = 0;
  1165. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l", &arg1, socket_ce, &addr, &addr_len, &port) == FAILURE) {
  1166. RETURN_THROWS();
  1167. }
  1168. php_sock = Z_SOCKET_P(arg1);
  1169. ENSURE_SOCKET_VALID(php_sock);
  1170. switch(php_sock->type) {
  1171. case AF_UNIX:
  1172. {
  1173. struct sockaddr_un *sa = (struct sockaddr_un *) sock_type;
  1174. sa->sun_family = AF_UNIX;
  1175. if (addr_len >= sizeof(sa->sun_path)) {
  1176. zend_argument_value_error(2, "must be less than %d", sizeof(sa->sun_path));
  1177. RETURN_THROWS();
  1178. }
  1179. memcpy(&sa->sun_path, addr, addr_len);
  1180. retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa,
  1181. offsetof(struct sockaddr_un, sun_path) + addr_len);
  1182. break;
  1183. }
  1184. case AF_INET:
  1185. {
  1186. struct sockaddr_in *sa = (struct sockaddr_in *) sock_type;
  1187. sa->sin_family = AF_INET;
  1188. sa->sin_port = htons((unsigned short) port);
  1189. if (! php_set_inet_addr(sa, addr, php_sock)) {
  1190. RETURN_FALSE;
  1191. }
  1192. retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
  1193. break;
  1194. }
  1195. #if HAVE_IPV6
  1196. case AF_INET6:
  1197. {
  1198. struct sockaddr_in6 *sa = (struct sockaddr_in6 *) sock_type;
  1199. sa->sin6_family = AF_INET6;
  1200. sa->sin6_port = htons((unsigned short) port);
  1201. if (! php_set_inet6_addr(sa, addr, php_sock)) {
  1202. RETURN_FALSE;
  1203. }
  1204. retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in6));
  1205. break;
  1206. }
  1207. #endif
  1208. default:
  1209. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1210. RETURN_THROWS();
  1211. }
  1212. if (retval != 0) {
  1213. PHP_SOCKET_ERROR(php_sock, "Unable to bind address", errno);
  1214. RETURN_FALSE;
  1215. }
  1216. RETURN_TRUE;
  1217. }
  1218. /* }}} */
  1219. /* {{{ Receives data from a connected socket */
  1220. PHP_FUNCTION(socket_recv)
  1221. {
  1222. zval *php_sock_res, *buf;
  1223. zend_string *recv_buf;
  1224. php_socket *php_sock;
  1225. int retval;
  1226. zend_long len, flags;
  1227. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ozll", &php_sock_res, socket_ce, &buf, &len, &flags) == FAILURE) {
  1228. RETURN_THROWS();
  1229. }
  1230. php_sock = Z_SOCKET_P(php_sock_res);
  1231. ENSURE_SOCKET_VALID(php_sock);
  1232. /* overflow check */
  1233. if ((len + 1) < 2) {
  1234. RETURN_FALSE;
  1235. }
  1236. recv_buf = zend_string_alloc(len, 0);
  1237. if ((retval = recv(php_sock->bsd_socket, ZSTR_VAL(recv_buf), len, flags)) < 1) {
  1238. zend_string_efree(recv_buf);
  1239. ZEND_TRY_ASSIGN_REF_NULL(buf);
  1240. } else {
  1241. ZSTR_LEN(recv_buf) = retval;
  1242. ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
  1243. ZEND_TRY_ASSIGN_REF_NEW_STR(buf, recv_buf);
  1244. }
  1245. if (retval == -1) {
  1246. PHP_SOCKET_ERROR(php_sock, "Unable to read from socket", errno);
  1247. RETURN_FALSE;
  1248. }
  1249. RETURN_LONG(retval);
  1250. }
  1251. /* }}} */
  1252. /* {{{ Sends data to a connected socket */
  1253. PHP_FUNCTION(socket_send)
  1254. {
  1255. zval *arg1;
  1256. php_socket *php_sock;
  1257. size_t buf_len, retval;
  1258. zend_long len, flags;
  1259. char *buf;
  1260. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osll", &arg1, socket_ce, &buf, &buf_len, &len, &flags) == FAILURE) {
  1261. RETURN_THROWS();
  1262. }
  1263. php_sock = Z_SOCKET_P(arg1);
  1264. ENSURE_SOCKET_VALID(php_sock);
  1265. if (len < 0) {
  1266. zend_argument_value_error(3, "must be greater than or equal to 0");
  1267. RETURN_THROWS();
  1268. }
  1269. retval = send(php_sock->bsd_socket, buf, (buf_len < (size_t)len ? buf_len : (size_t)len), flags);
  1270. if (retval == (size_t)-1) {
  1271. PHP_SOCKET_ERROR(php_sock, "Unable to write to socket", errno);
  1272. RETURN_FALSE;
  1273. }
  1274. RETURN_LONG(retval);
  1275. }
  1276. /* }}} */
  1277. /* {{{ Receives data from a socket, connected or not */
  1278. PHP_FUNCTION(socket_recvfrom)
  1279. {
  1280. zval *arg1, *arg2, *arg5, *arg6 = NULL;
  1281. php_socket *php_sock;
  1282. struct sockaddr_un s_un;
  1283. struct sockaddr_in sin;
  1284. #if HAVE_IPV6
  1285. struct sockaddr_in6 sin6;
  1286. #endif
  1287. #ifdef HAVE_INET_NTOP
  1288. char addrbuf[INET6_ADDRSTRLEN];
  1289. #endif
  1290. socklen_t slen;
  1291. int retval;
  1292. zend_long arg3, arg4;
  1293. const char *address;
  1294. zend_string *recv_buf;
  1295. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ozllz|z", &arg1, socket_ce, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE) {
  1296. RETURN_THROWS();
  1297. }
  1298. php_sock = Z_SOCKET_P(arg1);
  1299. ENSURE_SOCKET_VALID(php_sock);
  1300. /* overflow check */
  1301. /* Shouldthrow ? */
  1302. if ((arg3 + 2) < 3) {
  1303. RETURN_FALSE;
  1304. }
  1305. recv_buf = zend_string_alloc(arg3 + 1, 0);
  1306. switch (php_sock->type) {
  1307. case AF_UNIX:
  1308. slen = sizeof(s_un);
  1309. memset(&s_un, 0, slen);
  1310. s_un.sun_family = AF_UNIX;
  1311. retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
  1312. if (retval < 0) {
  1313. PHP_SOCKET_ERROR(php_sock, "Unable to recvfrom", errno);
  1314. zend_string_efree(recv_buf);
  1315. RETURN_FALSE;
  1316. }
  1317. ZSTR_LEN(recv_buf) = retval;
  1318. ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
  1319. ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
  1320. ZEND_TRY_ASSIGN_REF_STRING(arg5, s_un.sun_path);
  1321. break;
  1322. case AF_INET:
  1323. slen = sizeof(sin);
  1324. memset(&sin, 0, slen);
  1325. sin.sin_family = AF_INET;
  1326. if (arg6 == NULL) {
  1327. zend_string_efree(recv_buf);
  1328. WRONG_PARAM_COUNT;
  1329. }
  1330. retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sin, (socklen_t *)&slen);
  1331. if (retval < 0) {
  1332. PHP_SOCKET_ERROR(php_sock, "Unable to recvfrom", errno);
  1333. zend_string_efree(recv_buf);
  1334. RETURN_FALSE;
  1335. }
  1336. ZSTR_LEN(recv_buf) = retval;
  1337. ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
  1338. #ifdef HAVE_INET_NTOP
  1339. address = inet_ntop(AF_INET, &sin.sin_addr, addrbuf, sizeof(addrbuf));
  1340. #else
  1341. address = inet_ntoa(sin.sin_addr);
  1342. #endif
  1343. ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
  1344. ZEND_TRY_ASSIGN_REF_STRING(arg5, address ? address : "0.0.0.0");
  1345. ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin.sin_port));
  1346. break;
  1347. #if HAVE_IPV6
  1348. case AF_INET6:
  1349. slen = sizeof(sin6);
  1350. memset(&sin6, 0, slen);
  1351. sin6.sin6_family = AF_INET6;
  1352. if (arg6 == NULL) {
  1353. zend_string_efree(recv_buf);
  1354. WRONG_PARAM_COUNT;
  1355. }
  1356. retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sin6, (socklen_t *)&slen);
  1357. if (retval < 0) {
  1358. PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
  1359. zend_string_efree(recv_buf);
  1360. RETURN_FALSE;
  1361. }
  1362. ZSTR_LEN(recv_buf) = retval;
  1363. ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
  1364. memset(addrbuf, 0, INET6_ADDRSTRLEN);
  1365. inet_ntop(AF_INET6, &sin6.sin6_addr, addrbuf, sizeof(addrbuf));
  1366. ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
  1367. ZEND_TRY_ASSIGN_REF_STRING(arg5, addrbuf[0] ? addrbuf : "::");
  1368. ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin6.sin6_port));
  1369. break;
  1370. #endif
  1371. default:
  1372. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1373. RETURN_THROWS();
  1374. }
  1375. RETURN_LONG(retval);
  1376. }
  1377. /* }}} */
  1378. /* {{{ Sends a message to a socket, whether it is connected or not */
  1379. PHP_FUNCTION(socket_sendto)
  1380. {
  1381. zval *arg1;
  1382. php_socket *php_sock;
  1383. struct sockaddr_un s_un;
  1384. struct sockaddr_in sin;
  1385. #if HAVE_IPV6
  1386. struct sockaddr_in6 sin6;
  1387. #endif
  1388. int retval;
  1389. size_t buf_len, addr_len;
  1390. zend_long len, flags, port;
  1391. bool port_is_null = 1;
  1392. char *buf, *addr;
  1393. 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) {
  1394. RETURN_THROWS();
  1395. }
  1396. php_sock = Z_SOCKET_P(arg1);
  1397. ENSURE_SOCKET_VALID(php_sock);
  1398. if (len < 0) {
  1399. zend_argument_value_error(3, "must be greater than or equal to 0");
  1400. RETURN_THROWS();
  1401. }
  1402. switch (php_sock->type) {
  1403. case AF_UNIX:
  1404. memset(&s_un, 0, sizeof(s_un));
  1405. s_un.sun_family = AF_UNIX;
  1406. snprintf(s_un.sun_path, sizeof(s_un.sun_path), "%s", addr);
  1407. 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));
  1408. break;
  1409. case AF_INET:
  1410. if (port_is_null) {
  1411. zend_argument_value_error(6, "cannot be null when the socket type is AF_INET");
  1412. RETURN_THROWS();
  1413. }
  1414. memset(&sin, 0, sizeof(sin));
  1415. sin.sin_family = AF_INET;
  1416. sin.sin_port = htons((unsigned short) port);
  1417. if (! php_set_inet_addr(&sin, addr, php_sock)) {
  1418. RETURN_FALSE;
  1419. }
  1420. retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin, sizeof(sin));
  1421. break;
  1422. #if HAVE_IPV6
  1423. case AF_INET6:
  1424. if (port_is_null) {
  1425. zend_argument_value_error(6, "cannot be null when the socket type is AF_INET6");
  1426. RETURN_THROWS();
  1427. }
  1428. memset(&sin6, 0, sizeof(sin6));
  1429. sin6.sin6_family = AF_INET6;
  1430. sin6.sin6_port = htons((unsigned short) port);
  1431. if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
  1432. RETURN_FALSE;
  1433. }
  1434. retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin6, sizeof(sin6));
  1435. break;
  1436. #endif
  1437. default:
  1438. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  1439. RETURN_THROWS();
  1440. }
  1441. if (retval == -1) {
  1442. PHP_SOCKET_ERROR(php_sock, "Unable to write to socket", errno);
  1443. RETURN_FALSE;
  1444. }
  1445. RETURN_LONG(retval);
  1446. }
  1447. /* }}} */
  1448. /* {{{ Gets socket options for the socket */
  1449. PHP_FUNCTION(socket_get_option)
  1450. {
  1451. zval *arg1;
  1452. struct linger linger_val;
  1453. struct timeval tv;
  1454. #ifdef PHP_WIN32
  1455. int timeout = 0;
  1456. #endif
  1457. socklen_t optlen;
  1458. php_socket *php_sock;
  1459. int other_val;
  1460. zend_long level, optname;
  1461. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll", &arg1, socket_ce, &level, &optname) == FAILURE) {
  1462. RETURN_THROWS();
  1463. }
  1464. php_sock = Z_SOCKET_P(arg1);
  1465. ENSURE_SOCKET_VALID(php_sock);
  1466. if (level == IPPROTO_IP) {
  1467. switch (optname) {
  1468. case IP_MULTICAST_IF: {
  1469. struct in_addr if_addr;
  1470. unsigned int if_index;
  1471. optlen = sizeof(if_addr);
  1472. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&if_addr, &optlen) != 0) {
  1473. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1474. RETURN_FALSE;
  1475. }
  1476. if (php_add4_to_if_index(&if_addr, php_sock, &if_index) == SUCCESS) {
  1477. RETURN_LONG((zend_long) if_index);
  1478. } else {
  1479. RETURN_FALSE;
  1480. }
  1481. }
  1482. }
  1483. }
  1484. #if HAVE_IPV6
  1485. else if (level == IPPROTO_IPV6) {
  1486. int ret = php_do_getsockopt_ipv6_rfc3542(php_sock, level, optname, return_value);
  1487. if (ret == SUCCESS) {
  1488. return;
  1489. } else if (ret == FAILURE) {
  1490. RETURN_FALSE;
  1491. } /* else continue */
  1492. }
  1493. #endif
  1494. if (level == SOL_SOCKET) {
  1495. switch (optname) {
  1496. case SO_LINGER:
  1497. optlen = sizeof(linger_val);
  1498. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&linger_val, &optlen) != 0) {
  1499. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1500. RETURN_FALSE;
  1501. }
  1502. array_init(return_value);
  1503. add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
  1504. add_assoc_long(return_value, "l_linger", linger_val.l_linger);
  1505. return;
  1506. case SO_RCVTIMEO:
  1507. case SO_SNDTIMEO:
  1508. #ifndef PHP_WIN32
  1509. optlen = sizeof(tv);
  1510. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&tv, &optlen) != 0) {
  1511. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1512. RETURN_FALSE;
  1513. }
  1514. #else
  1515. optlen = sizeof(int);
  1516. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&timeout, &optlen) != 0) {
  1517. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1518. RETURN_FALSE;
  1519. }
  1520. tv.tv_sec = timeout ? timeout / 1000 : 0;
  1521. tv.tv_usec = timeout ? (timeout * 1000) % 1000000 : 0;
  1522. #endif
  1523. array_init(return_value);
  1524. add_assoc_long(return_value, "sec", tv.tv_sec);
  1525. add_assoc_long(return_value, "usec", tv.tv_usec);
  1526. return;
  1527. #ifdef SO_ACCEPTFILTER
  1528. case SO_ACCEPTFILTER: {
  1529. struct accept_filter_arg af = {0};
  1530. optlen = sizeof(af);
  1531. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&af, &optlen) != 0) {
  1532. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1533. RETURN_FALSE;
  1534. }
  1535. array_init(return_value);
  1536. add_assoc_string(return_value, "af_name", af.af_name);
  1537. return;
  1538. }
  1539. #endif
  1540. }
  1541. }
  1542. optlen = sizeof(other_val);
  1543. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&other_val, &optlen) != 0) {
  1544. PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
  1545. RETURN_FALSE;
  1546. }
  1547. if (optlen == 1) {
  1548. other_val = *((unsigned char *)&other_val);
  1549. }
  1550. RETURN_LONG(other_val);
  1551. }
  1552. /* }}} */
  1553. /* {{{ Sets socket options for the socket */
  1554. PHP_FUNCTION(socket_set_option)
  1555. {
  1556. zval *arg1, *arg4;
  1557. struct linger lv;
  1558. php_socket *php_sock;
  1559. int ov, optlen, retval;
  1560. #ifdef PHP_WIN32
  1561. int timeout;
  1562. #else
  1563. struct timeval tv;
  1564. #endif
  1565. zend_long level, optname;
  1566. void *opt_ptr;
  1567. HashTable *opt_ht;
  1568. zval *l_onoff, *l_linger;
  1569. zval *sec, *usec;
  1570. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollz", &arg1, socket_ce, &level, &optname, &arg4) == FAILURE) {
  1571. RETURN_THROWS();
  1572. }
  1573. php_sock = Z_SOCKET_P(arg1);
  1574. ENSURE_SOCKET_VALID(php_sock);
  1575. set_errno(0);
  1576. #define HANDLE_SUBCALL(res) \
  1577. do { \
  1578. if (res == 1) { goto default_case; } \
  1579. else if (res == SUCCESS) { RETURN_TRUE; } \
  1580. else { RETURN_FALSE; } \
  1581. } while (0)
  1582. if (level == IPPROTO_IP) {
  1583. int res = php_do_setsockopt_ip_mcast(php_sock, level, optname, arg4);
  1584. HANDLE_SUBCALL(res);
  1585. }
  1586. #if HAVE_IPV6
  1587. else if (level == IPPROTO_IPV6) {
  1588. int res = php_do_setsockopt_ipv6_mcast(php_sock, level, optname, arg4);
  1589. if (res == 1) {
  1590. res = php_do_setsockopt_ipv6_rfc3542(php_sock, level, optname, arg4);
  1591. }
  1592. HANDLE_SUBCALL(res);
  1593. }
  1594. #endif
  1595. switch (optname) {
  1596. case SO_LINGER: {
  1597. const char l_onoff_key[] = "l_onoff";
  1598. const char l_linger_key[] = "l_linger";
  1599. convert_to_array(arg4);
  1600. opt_ht = Z_ARRVAL_P(arg4);
  1601. if ((l_onoff = zend_hash_str_find(opt_ht, l_onoff_key, sizeof(l_onoff_key) - 1)) == NULL) {
  1602. zend_argument_value_error(4, "must have key \"%s\"", l_onoff_key);
  1603. RETURN_THROWS();
  1604. }
  1605. if ((l_linger = zend_hash_str_find(opt_ht, l_linger_key, sizeof(l_linger_key) - 1)) == NULL) {
  1606. zend_argument_value_error(4, "must have key \"%s\"", l_linger_key);
  1607. RETURN_THROWS();
  1608. }
  1609. convert_to_long(l_onoff);
  1610. convert_to_long(l_linger);
  1611. lv.l_onoff = (unsigned short)Z_LVAL_P(l_onoff);
  1612. lv.l_linger = (unsigned short)Z_LVAL_P(l_linger);
  1613. optlen = sizeof(lv);
  1614. opt_ptr = &lv;
  1615. break;
  1616. }
  1617. case SO_RCVTIMEO:
  1618. case SO_SNDTIMEO: {
  1619. const char sec_key[] = "sec";
  1620. const char usec_key[] = "usec";
  1621. convert_to_array(arg4);
  1622. opt_ht = Z_ARRVAL_P(arg4);
  1623. if ((sec = zend_hash_str_find(opt_ht, sec_key, sizeof(sec_key) - 1)) == NULL) {
  1624. zend_argument_value_error(4, "must have key \"%s\"", sec_key);
  1625. RETURN_THROWS();
  1626. }
  1627. if ((usec = zend_hash_str_find(opt_ht, usec_key, sizeof(usec_key) - 1)) == NULL) {
  1628. zend_argument_value_error(4, "must have key \"%s\"", usec_key);
  1629. RETURN_THROWS();
  1630. }
  1631. convert_to_long(sec);
  1632. convert_to_long(usec);
  1633. #ifndef PHP_WIN32
  1634. tv.tv_sec = Z_LVAL_P(sec);
  1635. tv.tv_usec = Z_LVAL_P(usec);
  1636. optlen = sizeof(tv);
  1637. opt_ptr = &tv;
  1638. #else
  1639. timeout = Z_LVAL_P(sec) * 1000 + Z_LVAL_P(usec) / 1000;
  1640. optlen = sizeof(int);
  1641. opt_ptr = &timeout;
  1642. #endif
  1643. break;
  1644. }
  1645. #ifdef SO_BINDTODEVICE
  1646. case SO_BINDTODEVICE: {
  1647. if (Z_TYPE_P(arg4) == IS_STRING) {
  1648. opt_ptr = Z_STRVAL_P(arg4);
  1649. optlen = Z_STRLEN_P(arg4);
  1650. } else {
  1651. opt_ptr = "";
  1652. optlen = 0;
  1653. }
  1654. break;
  1655. }
  1656. #endif
  1657. #ifdef SO_ACCEPTFILTER
  1658. case SO_ACCEPTFILTER: {
  1659. if (Z_TYPE_P(arg4) != IS_STRING) {
  1660. php_error_docref(NULL, E_WARNING, "Invalid filter argument type");
  1661. RETURN_FALSE;
  1662. }
  1663. struct accept_filter_arg af = {0};
  1664. strlcpy(af.af_name, Z_STRVAL_P(arg4), sizeof(af.af_name));
  1665. opt_ptr = &af;
  1666. optlen = sizeof(af);
  1667. break;
  1668. }
  1669. #endif
  1670. default:
  1671. default_case:
  1672. convert_to_long(arg4);
  1673. ov = Z_LVAL_P(arg4);
  1674. optlen = sizeof(ov);
  1675. opt_ptr = &ov;
  1676. break;
  1677. }
  1678. retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
  1679. if (retval != 0) {
  1680. PHP_SOCKET_ERROR(php_sock, "Unable to set socket option", errno);
  1681. RETURN_FALSE;
  1682. }
  1683. RETURN_TRUE;
  1684. }
  1685. /* }}} */
  1686. #ifdef HAVE_SOCKETPAIR
  1687. /* {{{ Creates a pair of indistinguishable sockets and stores them in fds. */
  1688. PHP_FUNCTION(socket_create_pair)
  1689. {
  1690. zval retval[2], *fds_array_zval;
  1691. php_socket *php_sock[2];
  1692. PHP_SOCKET fds_array[2];
  1693. zend_long domain, type, protocol;
  1694. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lllz", &domain, &type, &protocol, &fds_array_zval) == FAILURE) {
  1695. RETURN_THROWS();
  1696. }
  1697. if (domain != AF_INET
  1698. #if HAVE_IPV6
  1699. && domain != AF_INET6
  1700. #endif
  1701. && domain != AF_UNIX) {
  1702. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET6, or AF_INET");
  1703. RETURN_THROWS();
  1704. }
  1705. if (type > 10) {
  1706. zend_argument_value_error(2, "must be one of SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET,"
  1707. " SOCK_RAW, or SOCK_RDM");
  1708. RETURN_THROWS();
  1709. }
  1710. object_init_ex(&retval[0], socket_ce);
  1711. php_sock[0] = Z_SOCKET_P(&retval[0]);
  1712. object_init_ex(&retval[1], socket_ce);
  1713. php_sock[1] = Z_SOCKET_P(&retval[1]);
  1714. if (socketpair(domain, type, protocol, fds_array) != 0) {
  1715. SOCKETS_G(last_error) = errno;
  1716. php_error_docref(NULL, E_WARNING, "Unable to create socket pair [%d]: %s", errno, sockets_strerror(errno));
  1717. zval_ptr_dtor(&retval[0]);
  1718. zval_ptr_dtor(&retval[1]);
  1719. RETURN_FALSE;
  1720. }
  1721. fds_array_zval = zend_try_array_init(fds_array_zval);
  1722. if (!fds_array_zval) {
  1723. zval_ptr_dtor(&retval[0]);
  1724. zval_ptr_dtor(&retval[1]);
  1725. RETURN_THROWS();
  1726. }
  1727. php_sock[0]->bsd_socket = fds_array[0];
  1728. php_sock[1]->bsd_socket = fds_array[1];
  1729. php_sock[0]->type = domain;
  1730. php_sock[1]->type = domain;
  1731. php_sock[0]->error = 0;
  1732. php_sock[1]->error = 0;
  1733. php_sock[0]->blocking = 1;
  1734. php_sock[1]->blocking = 1;
  1735. add_index_zval(fds_array_zval, 0, &retval[0]);
  1736. add_index_zval(fds_array_zval, 1, &retval[1]);
  1737. RETURN_TRUE;
  1738. }
  1739. /* }}} */
  1740. #endif
  1741. #ifdef HAVE_SHUTDOWN
  1742. /* {{{ Shuts down a socket for receiving, sending, or both. */
  1743. PHP_FUNCTION(socket_shutdown)
  1744. {
  1745. zval *arg1;
  1746. zend_long how_shutdown = 2;
  1747. php_socket *php_sock;
  1748. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &arg1, socket_ce, &how_shutdown) == FAILURE) {
  1749. RETURN_THROWS();
  1750. }
  1751. php_sock = Z_SOCKET_P(arg1);
  1752. ENSURE_SOCKET_VALID(php_sock);
  1753. if (shutdown(php_sock->bsd_socket, how_shutdown) != 0) {
  1754. PHP_SOCKET_ERROR(php_sock, "Unable to shutdown socket", errno);
  1755. RETURN_FALSE;
  1756. }
  1757. RETURN_TRUE;
  1758. }
  1759. /* }}} */
  1760. #endif
  1761. /* {{{ Returns the last socket error (either the last used or the provided socket resource) */
  1762. PHP_FUNCTION(socket_last_error)
  1763. {
  1764. zval *arg1 = NULL;
  1765. php_socket *php_sock;
  1766. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &arg1, socket_ce) == FAILURE) {
  1767. RETURN_THROWS();
  1768. }
  1769. if (arg1) {
  1770. php_sock = Z_SOCKET_P(arg1);
  1771. ENSURE_SOCKET_VALID(php_sock);
  1772. RETVAL_LONG(php_sock->error);
  1773. } else {
  1774. RETVAL_LONG(SOCKETS_G(last_error));
  1775. }
  1776. }
  1777. /* }}} */
  1778. /* {{{ Clears the error on the socket or the last error code. */
  1779. PHP_FUNCTION(socket_clear_error)
  1780. {
  1781. zval *arg1 = NULL;
  1782. php_socket *php_sock;
  1783. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &arg1, socket_ce) == FAILURE) {
  1784. RETURN_THROWS();
  1785. }
  1786. if (arg1) {
  1787. php_sock = Z_SOCKET_P(arg1);
  1788. ENSURE_SOCKET_VALID(php_sock);
  1789. php_sock->error = 0;
  1790. } else {
  1791. SOCKETS_G(last_error) = 0;
  1792. }
  1793. return;
  1794. }
  1795. /* }}} */
  1796. int socket_import_file_descriptor(PHP_SOCKET socket, php_socket *retsock)
  1797. {
  1798. #ifdef SO_DOMAIN
  1799. int type;
  1800. socklen_t type_len = sizeof(type);
  1801. #endif
  1802. php_sockaddr_storage addr;
  1803. socklen_t addr_len = sizeof(addr);
  1804. #ifndef PHP_WIN32
  1805. int t;
  1806. #endif
  1807. retsock->bsd_socket = socket;
  1808. /* determine family */
  1809. #ifdef SO_DOMAIN
  1810. if (getsockopt(socket, SOL_SOCKET, SO_DOMAIN, &type, &type_len) == 0) {
  1811. retsock->type = type;
  1812. } else
  1813. #endif
  1814. if (getsockname(socket, (struct sockaddr*)&addr, &addr_len) == 0) {
  1815. retsock->type = addr.ss_family;
  1816. } else {
  1817. PHP_SOCKET_ERROR(retsock, "Unable to obtain socket family", errno);
  1818. return 0;
  1819. }
  1820. /* determine blocking mode */
  1821. #ifndef PHP_WIN32
  1822. t = fcntl(socket, F_GETFL);
  1823. if (t == -1) {
  1824. PHP_SOCKET_ERROR(retsock, "Unable to obtain blocking state", errno);
  1825. return 0;
  1826. } else {
  1827. retsock->blocking = !(t & O_NONBLOCK);
  1828. }
  1829. #endif
  1830. return 1;
  1831. }
  1832. /* {{{ Imports a stream that encapsulates a socket into a socket extension resource. */
  1833. PHP_FUNCTION(socket_import_stream)
  1834. {
  1835. zval *zstream;
  1836. php_stream *stream;
  1837. php_socket *retsock = NULL;
  1838. PHP_SOCKET socket; /* fd */
  1839. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream) == FAILURE) {
  1840. RETURN_THROWS();
  1841. }
  1842. php_stream_from_zval(stream, zstream);
  1843. if (php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void**)&socket, 1)) {
  1844. /* error supposedly already shown */
  1845. RETURN_FALSE;
  1846. }
  1847. object_init_ex(return_value, socket_ce);
  1848. retsock = Z_SOCKET_P(return_value);
  1849. if (!socket_import_file_descriptor(socket, retsock)) {
  1850. zval_ptr_dtor(return_value);
  1851. RETURN_FALSE;
  1852. }
  1853. #ifdef PHP_WIN32
  1854. /* on windows, check if the stream is a socket stream and read its
  1855. * private data; otherwise assume it's in non-blocking mode */
  1856. if (php_stream_is(stream, PHP_STREAM_IS_SOCKET)) {
  1857. retsock->blocking =
  1858. ((php_netstream_data_t *)stream->abstract)->is_blocked;
  1859. } else {
  1860. retsock->blocking = 1;
  1861. }
  1862. #endif
  1863. /* hold a zval reference to the stream (holding a php_stream* directly could
  1864. * also be done, but this makes socket_export_stream a bit simpler) */
  1865. ZVAL_COPY(&retsock->zstream, zstream);
  1866. php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
  1867. }
  1868. /* }}} */
  1869. /* {{{ Exports a socket extension resource into a stream that encapsulates a socket. */
  1870. PHP_FUNCTION(socket_export_stream)
  1871. {
  1872. zval *zsocket;
  1873. php_socket *socket;
  1874. php_stream *stream = NULL;
  1875. php_netstream_data_t *stream_data;
  1876. char *protocol = NULL;
  1877. size_t protocollen = 0;
  1878. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zsocket, socket_ce) == FAILURE) {
  1879. RETURN_THROWS();
  1880. }
  1881. socket = Z_SOCKET_P(zsocket);
  1882. ENSURE_SOCKET_VALID(socket);
  1883. /* Either we already exported a stream or the socket came from an import,
  1884. * just return the existing stream */
  1885. if (!Z_ISUNDEF(socket->zstream)) {
  1886. RETURN_COPY(&socket->zstream);
  1887. }
  1888. /* Determine if socket is using a protocol with one of the default registered
  1889. * socket stream wrappers */
  1890. if (socket->type == PF_INET
  1891. #if HAVE_IPV6
  1892. || socket->type == PF_INET6
  1893. #endif
  1894. ) {
  1895. int protoid;
  1896. socklen_t protoidlen = sizeof(protoid);
  1897. getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &protoid, &protoidlen);
  1898. if (protoid == SOCK_STREAM) {
  1899. /* SO_PROTOCOL is not (yet?) supported on OS X, so lets assume it's TCP there */
  1900. #ifdef SO_PROTOCOL
  1901. protoidlen = sizeof(protoid);
  1902. getsockopt(socket->bsd_socket, SOL_SOCKET, SO_PROTOCOL, (char *) &protoid, &protoidlen);
  1903. if (protoid == IPPROTO_TCP)
  1904. #endif
  1905. {
  1906. protocol = "tcp";
  1907. protocollen = 3;
  1908. }
  1909. } else if (protoid == SOCK_DGRAM) {
  1910. protocol = "udp";
  1911. protocollen = 3;
  1912. }
  1913. #ifdef PF_UNIX
  1914. } else if (socket->type == PF_UNIX) {
  1915. int type;
  1916. socklen_t typelen = sizeof(type);
  1917. getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &type, &typelen);
  1918. if (type == SOCK_STREAM) {
  1919. protocol = "unix";
  1920. protocollen = 4;
  1921. } else if (type == SOCK_DGRAM) {
  1922. protocol = "udg";
  1923. protocollen = 3;
  1924. }
  1925. #endif
  1926. }
  1927. /* Try to get a stream with the registered sockops for the protocol in use
  1928. * We don't want streams to actually *do* anything though, so don't give it
  1929. * anything apart from the protocol */
  1930. if (protocol != NULL) {
  1931. stream = php_stream_xport_create(protocol, protocollen, 0, 0, NULL, NULL, NULL, NULL, NULL);
  1932. }
  1933. /* Fall back to creating a generic socket stream */
  1934. if (stream == NULL) {
  1935. stream = php_stream_sock_open_from_socket(socket->bsd_socket, 0);
  1936. if (stream == NULL) {
  1937. php_error_docref(NULL, E_WARNING, "Failed to create stream");
  1938. RETURN_FALSE;
  1939. }
  1940. }
  1941. stream_data = (php_netstream_data_t *) stream->abstract;
  1942. stream_data->socket = socket->bsd_socket;
  1943. stream_data->is_blocked = socket->blocking;
  1944. stream_data->timeout.tv_sec = FG(default_socket_timeout);
  1945. stream_data->timeout.tv_usec = 0;
  1946. php_stream_to_zval(stream, &socket->zstream);
  1947. RETURN_COPY(&socket->zstream);
  1948. }
  1949. /* }}} */
  1950. /* {{{ Gets array with contents of getaddrinfo about the given hostname. */
  1951. PHP_FUNCTION(socket_addrinfo_lookup)
  1952. {
  1953. char *service = NULL;
  1954. size_t service_len = 0;
  1955. zend_string *hostname, *key;
  1956. zval *hint, *zhints = NULL;
  1957. struct addrinfo hints, *result, *rp;
  1958. php_addrinfo *res;
  1959. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|s!a", &hostname, &service, &service_len, &zhints) == FAILURE) {
  1960. RETURN_THROWS();
  1961. }
  1962. memset(&hints, 0, sizeof(hints));
  1963. if (zhints) {
  1964. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zhints), key, hint) {
  1965. if (key) {
  1966. if (zend_string_equals_literal(key, "ai_flags")) {
  1967. hints.ai_flags = zval_get_long(hint);
  1968. } else if (zend_string_equals_literal(key, "ai_socktype")) {
  1969. hints.ai_socktype = zval_get_long(hint);
  1970. } else if (zend_string_equals_literal(key, "ai_protocol")) {
  1971. hints.ai_protocol = zval_get_long(hint);
  1972. } else if (zend_string_equals_literal(key, "ai_family")) {
  1973. hints.ai_family = zval_get_long(hint);
  1974. } else {
  1975. zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
  1976. "\"ai_protocol\", or \"ai_family\"");
  1977. RETURN_THROWS();
  1978. }
  1979. }
  1980. } ZEND_HASH_FOREACH_END();
  1981. }
  1982. if (getaddrinfo(ZSTR_VAL(hostname), service, &hints, &result) != 0) {
  1983. RETURN_FALSE;
  1984. }
  1985. array_init(return_value);
  1986. for (rp = result; rp != NULL; rp = rp->ai_next) {
  1987. if (rp->ai_family != AF_UNSPEC) {
  1988. zval zaddr;
  1989. object_init_ex(&zaddr, address_info_ce);
  1990. res = Z_ADDRESS_INFO_P(&zaddr);
  1991. memcpy(&res->addrinfo, rp, sizeof(struct addrinfo));
  1992. res->addrinfo.ai_addr = emalloc(rp->ai_addrlen);
  1993. memcpy(res->addrinfo.ai_addr, rp->ai_addr, rp->ai_addrlen);
  1994. if (rp->ai_canonname != NULL) {
  1995. res->addrinfo.ai_canonname = estrdup(rp->ai_canonname);
  1996. }
  1997. add_next_index_zval(return_value, &zaddr);
  1998. }
  1999. }
  2000. freeaddrinfo(result);
  2001. }
  2002. /* }}} */
  2003. /* {{{ Creates and binds to a socket from a given addrinfo resource */
  2004. PHP_FUNCTION(socket_addrinfo_bind)
  2005. {
  2006. zval *arg1;
  2007. int retval;
  2008. php_addrinfo *ai;
  2009. php_socket *php_sock;
  2010. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, address_info_ce) == FAILURE) {
  2011. RETURN_THROWS();
  2012. }
  2013. ai = Z_ADDRESS_INFO_P(arg1);
  2014. object_init_ex(return_value, socket_ce);
  2015. php_sock = Z_SOCKET_P(return_value);
  2016. php_sock->bsd_socket = socket(ai->addrinfo.ai_family, ai->addrinfo.ai_socktype, ai->addrinfo.ai_protocol);
  2017. php_sock->type = ai->addrinfo.ai_family;
  2018. if (IS_INVALID_SOCKET(php_sock)) {
  2019. SOCKETS_G(last_error) = errno;
  2020. php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
  2021. zval_ptr_dtor(return_value);
  2022. RETURN_FALSE;
  2023. }
  2024. php_sock->error = 0;
  2025. php_sock->blocking = 1;
  2026. switch(php_sock->type) {
  2027. case AF_UNIX:
  2028. {
  2029. // AF_UNIX sockets via getaddrino are not implemented due to security problems
  2030. close(php_sock->bsd_socket);
  2031. zval_ptr_dtor(return_value);
  2032. RETURN_FALSE;
  2033. }
  2034. case AF_INET:
  2035. #if HAVE_IPV6
  2036. case AF_INET6:
  2037. #endif
  2038. {
  2039. retval = bind(php_sock->bsd_socket, ai->addrinfo.ai_addr, ai->addrinfo.ai_addrlen);
  2040. break;
  2041. }
  2042. default:
  2043. close(php_sock->bsd_socket);
  2044. zval_ptr_dtor(return_value);
  2045. zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
  2046. RETURN_THROWS();
  2047. }
  2048. if (retval != 0) {
  2049. PHP_SOCKET_ERROR(php_sock, "Unable to bind address", errno);
  2050. close(php_sock->bsd_socket);
  2051. zval_ptr_dtor(return_value);
  2052. RETURN_FALSE;
  2053. }
  2054. }
  2055. /* }}} */
  2056. /* {{{ Creates and connects to a socket from a given addrinfo resource */
  2057. PHP_FUNCTION(socket_addrinfo_connect)
  2058. {
  2059. zval *arg1;
  2060. int retval;
  2061. php_addrinfo *ai;
  2062. php_socket *php_sock;
  2063. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, address_info_ce) == FAILURE) {
  2064. RETURN_THROWS();
  2065. }
  2066. ai = Z_ADDRESS_INFO_P(arg1);
  2067. object_init_ex(return_value, socket_ce);
  2068. php_sock = Z_SOCKET_P(return_value);
  2069. php_sock->bsd_socket = socket(ai->addrinfo.ai_family, ai->addrinfo.ai_socktype, ai->addrinfo.ai_protocol);
  2070. php_sock->type = ai->addrinfo.ai_family;
  2071. if (IS_INVALID_SOCKET(php_sock)) {
  2072. SOCKETS_G(last_error) = errno;
  2073. php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
  2074. zval_ptr_dtor(return_value);
  2075. RETURN_FALSE;
  2076. }
  2077. php_sock->error = 0;
  2078. php_sock->blocking = 1;
  2079. switch(php_sock->type) {
  2080. case AF_UNIX:
  2081. {
  2082. // AF_UNIX sockets via getaddrino are not implemented due to security problems
  2083. close(php_sock->bsd_socket);
  2084. zval_ptr_dtor(return_value);
  2085. RETURN_FALSE;
  2086. }
  2087. case AF_INET:
  2088. #if HAVE_IPV6
  2089. case AF_INET6:
  2090. #endif
  2091. {
  2092. retval = connect(php_sock->bsd_socket, ai->addrinfo.ai_addr, ai->addrinfo.ai_addrlen);
  2093. break;
  2094. }
  2095. default:
  2096. zend_argument_value_error(1, "socket type must be one of AF_UNIX, AF_INET, or AF_INET6");
  2097. close(php_sock->bsd_socket);
  2098. zval_ptr_dtor(return_value);
  2099. RETURN_THROWS();
  2100. }
  2101. if (retval != 0) {
  2102. PHP_SOCKET_ERROR(php_sock, "Unable to connect address", errno);
  2103. close(php_sock->bsd_socket);
  2104. zval_ptr_dtor(return_value);
  2105. RETURN_FALSE;
  2106. }
  2107. }
  2108. /* }}} */
  2109. /* {{{ Creates and connects to a socket from a given addrinfo resource */
  2110. PHP_FUNCTION(socket_addrinfo_explain)
  2111. {
  2112. zval *arg1, sockaddr;
  2113. php_addrinfo *ai;
  2114. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg1, address_info_ce) == FAILURE) {
  2115. RETURN_THROWS();
  2116. }
  2117. ai = Z_ADDRESS_INFO_P(arg1);
  2118. array_init(return_value);
  2119. add_assoc_long(return_value, "ai_flags", ai->addrinfo.ai_flags);
  2120. add_assoc_long(return_value, "ai_family", ai->addrinfo.ai_family);
  2121. add_assoc_long(return_value, "ai_socktype", ai->addrinfo.ai_socktype);
  2122. add_assoc_long(return_value, "ai_protocol", ai->addrinfo.ai_protocol);
  2123. if (ai->addrinfo.ai_canonname != NULL) {
  2124. add_assoc_string(return_value, "ai_canonname", ai->addrinfo.ai_canonname);
  2125. }
  2126. array_init(&sockaddr);
  2127. switch (ai->addrinfo.ai_family) {
  2128. case AF_INET:
  2129. {
  2130. struct sockaddr_in *sa = (struct sockaddr_in *) ai->addrinfo.ai_addr;
  2131. char addr[INET_ADDRSTRLEN];
  2132. add_assoc_long(&sockaddr, "sin_port", ntohs((unsigned short) sa->sin_port));
  2133. inet_ntop(ai->addrinfo.ai_family, &sa->sin_addr, addr, sizeof(addr));
  2134. add_assoc_string(&sockaddr, "sin_addr", addr);
  2135. break;
  2136. }
  2137. #if HAVE_IPV6
  2138. case AF_INET6:
  2139. {
  2140. struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ai->addrinfo.ai_addr;
  2141. char addr[INET6_ADDRSTRLEN];
  2142. add_assoc_long(&sockaddr, "sin6_port", ntohs((unsigned short) sa->sin6_port));
  2143. inet_ntop(ai->addrinfo.ai_family, &sa->sin6_addr, addr, sizeof(addr));
  2144. add_assoc_string(&sockaddr, "sin6_addr", addr);
  2145. break;
  2146. }
  2147. #endif
  2148. }
  2149. add_assoc_zval(return_value, "ai_addr", &sockaddr);
  2150. }
  2151. /* }}} */
  2152. #ifdef PHP_WIN32
  2153. /* {{{ Exports the network socket information suitable to be used in another process and returns the info id. */
  2154. PHP_FUNCTION(socket_wsaprotocol_info_export)
  2155. {
  2156. WSAPROTOCOL_INFO wi;
  2157. zval *zsocket;
  2158. php_socket *socket;
  2159. zend_long target_pid;
  2160. zend_string *seg_name;
  2161. HANDLE map;
  2162. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &zsocket, socket_ce, &target_pid) == FAILURE) {
  2163. RETURN_THROWS();
  2164. }
  2165. socket = Z_SOCKET_P(zsocket);
  2166. ENSURE_SOCKET_VALID(socket);
  2167. if (SOCKET_ERROR == WSADuplicateSocket(socket->bsd_socket, (DWORD)target_pid, &wi)) {
  2168. DWORD err = WSAGetLastError();
  2169. char *buf = php_win32_error_to_msg(err);
  2170. if (!buf[0]) {
  2171. php_error_docref(NULL, E_WARNING, "Unable to export WSA protocol info [0x%08lx]", err);
  2172. } else {
  2173. php_error_docref(NULL, E_WARNING, "Unable to export WSA protocol info [0x%08lx]: %s", err, buf);
  2174. }
  2175. php_win32_error_msg_free(buf);
  2176. RETURN_FALSE;
  2177. }
  2178. seg_name = zend_strpprintf(0, "php_wsa_for_%u", SOCKETS_G(wsa_child_count)++);
  2179. map = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(WSAPROTOCOL_INFO), ZSTR_VAL(seg_name));
  2180. if (NULL != map) {
  2181. LPVOID view = MapViewOfFile(map, FILE_MAP_WRITE, 0, 0, 0);
  2182. if (view) {
  2183. memcpy(view, &wi, sizeof(wi));
  2184. UnmapViewOfFile(view);
  2185. zend_hash_add_ptr(&(SOCKETS_G(wsa_info)), seg_name, map);
  2186. RETURN_STR(seg_name);
  2187. } else {
  2188. DWORD err = GetLastError();
  2189. php_error_docref(NULL, E_WARNING, "Unable to map file view [0x%08lx]", err);
  2190. }
  2191. } else {
  2192. DWORD err = GetLastError();
  2193. php_error_docref(NULL, E_WARNING, "Unable to create file mapping [0x%08lx]", err);
  2194. }
  2195. zend_string_release_ex(seg_name, 0);
  2196. RETURN_FALSE;
  2197. }
  2198. /* }}} */
  2199. /* {{{ Imports the network socket information using the supplied id and creates a new socket on its base. */
  2200. PHP_FUNCTION(socket_wsaprotocol_info_import)
  2201. {
  2202. char *id;
  2203. size_t id_len;
  2204. WSAPROTOCOL_INFO wi;
  2205. PHP_SOCKET sock;
  2206. php_socket *php_sock;
  2207. HANDLE map;
  2208. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &id, &id_len) == FAILURE) {
  2209. RETURN_THROWS();
  2210. }
  2211. map = OpenFileMapping(FILE_MAP_READ, FALSE, id);
  2212. if (map) {
  2213. LPVOID view = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
  2214. if (view) {
  2215. memcpy(&wi, view, sizeof(WSAPROTOCOL_INFO));
  2216. UnmapViewOfFile(view);
  2217. } else {
  2218. DWORD err = GetLastError();
  2219. php_error_docref(NULL, E_WARNING, "Unable to map file view [0x%08lx]", err);
  2220. RETURN_FALSE;
  2221. }
  2222. CloseHandle(map);
  2223. } else {
  2224. DWORD err = GetLastError();
  2225. php_error_docref(NULL, E_WARNING, "Unable to open file mapping [0x%08lx]", err);
  2226. RETURN_FALSE;
  2227. }
  2228. sock = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, &wi, 0, 0);
  2229. if (INVALID_SOCKET == sock) {
  2230. DWORD err = WSAGetLastError();
  2231. char *buf = php_win32_error_to_msg(err);
  2232. if (!buf[0]) {
  2233. php_error_docref(NULL, E_WARNING, "Unable to import WSA protocol info [0x%08lx]", err);
  2234. } else {
  2235. php_error_docref(NULL, E_WARNING, "Unable to import WSA protocol info [0x%08lx]: %s", err, buf);
  2236. }
  2237. php_win32_error_msg_free(buf);
  2238. RETURN_FALSE;
  2239. }
  2240. object_init_ex(return_value, socket_ce);
  2241. php_sock = Z_SOCKET_P(return_value);
  2242. php_sock->bsd_socket = sock;
  2243. php_sock->type = wi.iAddressFamily;
  2244. php_sock->error = 0;
  2245. php_sock->blocking = 1;
  2246. }
  2247. /* }}} */
  2248. /* {{{ Frees the exported info and corresponding resources using the supplied id. */
  2249. PHP_FUNCTION(socket_wsaprotocol_info_release)
  2250. {
  2251. char *id;
  2252. size_t id_len;
  2253. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &id, &id_len) == FAILURE) {
  2254. RETURN_THROWS();
  2255. }
  2256. RETURN_BOOL(SUCCESS == zend_hash_str_del(&(SOCKETS_G(wsa_info)), id, id_len));
  2257. }
  2258. /* }}} */
  2259. #endif