sockets.c 79 KB

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