sock.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599
  1. /*
  2. * Server-side socket management
  3. *
  4. * Copyright (C) 1999 Marcus Meissner, Ove Kåven
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. * FIXME: we use read|write access in all cases. Shouldn't we depend that
  21. * on the access of the current handle?
  22. */
  23. #include "config.h"
  24. #include <assert.h>
  25. #include <fcntl.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <errno.h>
  31. #ifdef HAVE_IFADDRS_H
  32. # include <ifaddrs.h>
  33. #endif
  34. #ifdef HAVE_NET_IF_H
  35. # include <net/if.h>
  36. #endif
  37. #ifdef HAVE_NETINET_IN_H
  38. # include <netinet/in.h>
  39. #endif
  40. #include <poll.h>
  41. #include <sys/time.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/ioctl.h>
  45. #ifdef HAVE_SYS_FILIO_H
  46. # include <sys/filio.h>
  47. #endif
  48. #include <time.h>
  49. #include <unistd.h>
  50. #include <limits.h>
  51. #ifdef HAVE_LINUX_FILTER_H
  52. # include <linux/filter.h>
  53. #endif
  54. #ifdef HAVE_LINUX_RTNETLINK_H
  55. # include <linux/rtnetlink.h>
  56. #endif
  57. #ifdef HAVE_NETIPX_IPX_H
  58. # include <netipx/ipx.h>
  59. #elif defined(HAVE_LINUX_IPX_H)
  60. # ifdef HAVE_ASM_TYPES_H
  61. # include <asm/types.h>
  62. # endif
  63. # ifdef HAVE_LINUX_TYPES_H
  64. # include <linux/types.h>
  65. # endif
  66. # include <linux/ipx.h>
  67. #endif
  68. #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
  69. # define HAS_IPX
  70. #endif
  71. #ifdef HAVE_LINUX_IRDA_H
  72. # ifdef HAVE_LINUX_TYPES_H
  73. # include <linux/types.h>
  74. # endif
  75. # include <linux/irda.h>
  76. # define HAS_IRDA
  77. #endif
  78. #include "ntstatus.h"
  79. #define WIN32_NO_STATUS
  80. #include "windef.h"
  81. #include "winternl.h"
  82. #include "winerror.h"
  83. #define USE_WS_PREFIX
  84. #include "winsock2.h"
  85. #include "ws2tcpip.h"
  86. #include "wsipx.h"
  87. #include "af_irda.h"
  88. #include "wine/afd.h"
  89. #include "process.h"
  90. #include "file.h"
  91. #include "handle.h"
  92. #include "thread.h"
  93. #include "request.h"
  94. #include "user.h"
  95. #if defined(linux) && !defined(IP_UNICAST_IF)
  96. #define IP_UNICAST_IF 50
  97. #endif
  98. static const char magic_loopback_addr[] = {127, 12, 34, 56};
  99. union win_sockaddr
  100. {
  101. struct WS_sockaddr addr;
  102. struct WS_sockaddr_in in;
  103. struct WS_sockaddr_in6 in6;
  104. struct WS_sockaddr_ipx ipx;
  105. SOCKADDR_IRDA irda;
  106. };
  107. static struct list poll_list = LIST_INIT( poll_list );
  108. struct poll_req
  109. {
  110. struct list entry;
  111. struct async *async;
  112. struct iosb *iosb;
  113. struct timeout_user *timeout;
  114. timeout_t orig_timeout;
  115. int exclusive;
  116. unsigned int count;
  117. struct
  118. {
  119. struct sock *sock;
  120. int mask;
  121. obj_handle_t handle;
  122. int flags;
  123. unsigned int status;
  124. } sockets[1];
  125. };
  126. struct accept_req
  127. {
  128. struct list entry;
  129. struct async *async;
  130. struct iosb *iosb;
  131. struct sock *sock, *acceptsock;
  132. int accepted;
  133. unsigned int recv_len, local_len;
  134. };
  135. struct connect_req
  136. {
  137. struct async *async;
  138. struct iosb *iosb;
  139. struct sock *sock;
  140. unsigned int addr_len, send_len, send_cursor;
  141. };
  142. struct send_req
  143. {
  144. struct iosb *iosb;
  145. struct sock *sock;
  146. };
  147. enum connection_state
  148. {
  149. SOCK_LISTENING,
  150. SOCK_UNCONNECTED,
  151. SOCK_CONNECTING,
  152. SOCK_CONNECTED,
  153. SOCK_CONNECTIONLESS,
  154. };
  155. struct sock
  156. {
  157. struct object obj; /* object header */
  158. struct fd *fd; /* socket file descriptor */
  159. enum connection_state state; /* connection state */
  160. unsigned int mask; /* event mask */
  161. /* pending AFD_POLL_* events which have not yet been reported to the application */
  162. unsigned int pending_events;
  163. /* AFD_POLL_* events which have already been reported and should not be
  164. * selected for again until reset by a relevant call.
  165. *
  166. * For example, if AFD_POLL_READ is set here and not in pending_events, it
  167. * has already been reported and consumed, and we should not report it
  168. * again, even if POLLIN is signaled, until it is reset by e.g recv().
  169. *
  170. * If an event has been signaled and not consumed yet, it will be set in
  171. * both pending_events and reported_events (as we should only ever report
  172. * any event once until it is reset.) */
  173. unsigned int reported_events;
  174. unsigned int flags; /* socket flags */
  175. unsigned short proto; /* socket protocol */
  176. unsigned short type; /* socket type */
  177. unsigned short family; /* socket family */
  178. struct event *event; /* event object */
  179. user_handle_t window; /* window to send the message to */
  180. unsigned int message; /* message to send */
  181. obj_handle_t wparam; /* message wparam (socket handle) */
  182. int errors[AFD_POLL_BIT_COUNT]; /* event errors */
  183. timeout_t connect_time;/* time the socket was connected */
  184. struct sock *deferred; /* socket that waits for a deferred accept */
  185. struct async_queue read_q; /* queue for asynchronous reads */
  186. struct async_queue write_q; /* queue for asynchronous writes */
  187. struct async_queue ifchange_q; /* queue for interface change notifications */
  188. struct async_queue accept_q; /* queue for asynchronous accepts */
  189. struct async_queue connect_q; /* queue for asynchronous connects */
  190. struct async_queue poll_q; /* queue for asynchronous polls */
  191. struct object *ifchange_obj; /* the interface change notification object */
  192. struct list ifchange_entry; /* entry in ifchange notification list */
  193. struct list accept_list; /* list of pending accept requests */
  194. struct accept_req *accept_recv_req; /* pending accept-into request which will recv on this socket */
  195. struct connect_req *connect_req; /* pending connection request */
  196. struct poll_req *main_poll; /* main poll */
  197. union win_sockaddr addr; /* socket name */
  198. int addr_len; /* socket name length */
  199. unsigned int rcvbuf; /* advisory recv buffer size */
  200. unsigned int sndbuf; /* advisory send buffer size */
  201. unsigned int rcvtimeo; /* receive timeout in ms */
  202. unsigned int sndtimeo; /* send timeout in ms */
  203. unsigned int rd_shutdown : 1; /* is the read end shut down? */
  204. unsigned int wr_shutdown : 1; /* is the write end shut down? */
  205. unsigned int wr_shutdown_pending : 1; /* is a write shutdown pending? */
  206. unsigned int hangup : 1; /* has the read end received a hangup? */
  207. unsigned int aborted : 1; /* did we get a POLLERR or irregular POLLHUP? */
  208. unsigned int nonblocking : 1; /* is the socket nonblocking? */
  209. unsigned int bound : 1; /* is the socket bound? */
  210. };
  211. static void sock_dump( struct object *obj, int verbose );
  212. static struct fd *sock_get_fd( struct object *obj );
  213. static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
  214. static void sock_destroy( struct object *obj );
  215. static struct object *sock_get_ifchange( struct sock *sock );
  216. static void sock_release_ifchange( struct sock *sock );
  217. static int sock_get_poll_events( struct fd *fd );
  218. static void sock_poll_event( struct fd *fd, int event );
  219. static enum server_fd_type sock_get_fd_type( struct fd *fd );
  220. static void sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  221. static void sock_cancel_async( struct fd *fd, struct async *async );
  222. static void sock_queue_async( struct fd *fd, struct async *async, int type, int count );
  223. static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
  224. static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
  225. static struct sock *accept_socket( struct sock *sock );
  226. static int sock_get_ntstatus( int err );
  227. static unsigned int sock_get_error( int err );
  228. static void poll_socket( struct sock *poll_sock, struct async *async, int exclusive, timeout_t timeout,
  229. unsigned int count, const struct afd_poll_socket_64 *sockets );
  230. static const struct object_ops sock_ops =
  231. {
  232. sizeof(struct sock), /* size */
  233. &file_type, /* type */
  234. sock_dump, /* dump */
  235. add_queue, /* add_queue */
  236. remove_queue, /* remove_queue */
  237. default_fd_signaled, /* signaled */
  238. no_satisfied, /* satisfied */
  239. no_signal, /* signal */
  240. sock_get_fd, /* get_fd */
  241. default_map_access, /* map_access */
  242. default_get_sd, /* get_sd */
  243. default_set_sd, /* set_sd */
  244. no_get_full_name, /* get_full_name */
  245. no_lookup_name, /* lookup_name */
  246. no_link_name, /* link_name */
  247. NULL, /* unlink_name */
  248. no_open_file, /* open_file */
  249. no_kernel_obj_list, /* get_kernel_obj_list */
  250. sock_close_handle, /* close_handle */
  251. sock_destroy /* destroy */
  252. };
  253. static const struct fd_ops sock_fd_ops =
  254. {
  255. sock_get_poll_events, /* get_poll_events */
  256. sock_poll_event, /* poll_event */
  257. sock_get_fd_type, /* get_fd_type */
  258. no_fd_read, /* read */
  259. no_fd_write, /* write */
  260. no_fd_flush, /* flush */
  261. default_fd_get_file_info, /* get_file_info */
  262. no_fd_get_volume_info, /* get_volume_info */
  263. sock_ioctl, /* ioctl */
  264. sock_cancel_async, /* cancel_async */
  265. sock_queue_async, /* queue_async */
  266. sock_reselect_async /* reselect_async */
  267. };
  268. union unix_sockaddr
  269. {
  270. struct sockaddr addr;
  271. struct sockaddr_in in;
  272. struct sockaddr_in6 in6;
  273. #ifdef HAS_IPX
  274. struct sockaddr_ipx ipx;
  275. #endif
  276. #ifdef HAS_IRDA
  277. struct sockaddr_irda irda;
  278. #endif
  279. };
  280. static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
  281. {
  282. memset( wsaddr, 0, wsaddrlen );
  283. switch (uaddr->addr.sa_family)
  284. {
  285. case AF_INET:
  286. {
  287. struct WS_sockaddr_in win = {0};
  288. if (wsaddrlen < sizeof(win)) return -1;
  289. win.sin_family = WS_AF_INET;
  290. win.sin_port = uaddr->in.sin_port;
  291. memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
  292. memcpy( wsaddr, &win, sizeof(win) );
  293. return sizeof(win);
  294. }
  295. case AF_INET6:
  296. {
  297. struct WS_sockaddr_in6 win = {0};
  298. if (wsaddrlen < sizeof(win)) return -1;
  299. win.sin6_family = WS_AF_INET6;
  300. win.sin6_port = uaddr->in6.sin6_port;
  301. win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
  302. memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
  303. #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
  304. win.sin6_scope_id = uaddr->in6.sin6_scope_id;
  305. #endif
  306. memcpy( wsaddr, &win, sizeof(win) );
  307. return sizeof(win);
  308. }
  309. #ifdef HAS_IPX
  310. case AF_IPX:
  311. {
  312. struct WS_sockaddr_ipx win = {0};
  313. if (wsaddrlen < sizeof(win)) return -1;
  314. win.sa_family = WS_AF_IPX;
  315. memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
  316. memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
  317. win.sa_socket = uaddr->ipx.sipx_port;
  318. memcpy( wsaddr, &win, sizeof(win) );
  319. return sizeof(win);
  320. }
  321. #endif
  322. #ifdef HAS_IRDA
  323. case AF_IRDA:
  324. {
  325. SOCKADDR_IRDA win;
  326. if (wsaddrlen < sizeof(win)) return -1;
  327. win.irdaAddressFamily = WS_AF_IRDA;
  328. memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
  329. if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
  330. snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
  331. else
  332. memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
  333. memcpy( wsaddr, &win, sizeof(win) );
  334. return sizeof(win);
  335. }
  336. #endif
  337. case AF_UNSPEC:
  338. return 0;
  339. default:
  340. return -1;
  341. }
  342. }
  343. static socklen_t sockaddr_to_unix( const struct WS_sockaddr *wsaddr, int wsaddrlen, union unix_sockaddr *uaddr )
  344. {
  345. memset( uaddr, 0, sizeof(*uaddr) );
  346. switch (wsaddr->sa_family)
  347. {
  348. case WS_AF_INET:
  349. {
  350. struct WS_sockaddr_in win = {0};
  351. if (wsaddrlen < sizeof(win)) return 0;
  352. memcpy( &win, wsaddr, sizeof(win) );
  353. uaddr->in.sin_family = AF_INET;
  354. uaddr->in.sin_port = win.sin_port;
  355. memcpy( &uaddr->in.sin_addr, &win.sin_addr, sizeof(win.sin_addr) );
  356. return sizeof(uaddr->in);
  357. }
  358. case WS_AF_INET6:
  359. {
  360. struct WS_sockaddr_in6 win = {0};
  361. if (wsaddrlen < sizeof(win)) return 0;
  362. memcpy( &win, wsaddr, sizeof(win) );
  363. uaddr->in6.sin6_family = AF_INET6;
  364. uaddr->in6.sin6_port = win.sin6_port;
  365. uaddr->in6.sin6_flowinfo = win.sin6_flowinfo;
  366. memcpy( &uaddr->in6.sin6_addr, &win.sin6_addr, sizeof(win.sin6_addr) );
  367. #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
  368. uaddr->in6.sin6_scope_id = win.sin6_scope_id;
  369. #endif
  370. return sizeof(uaddr->in6);
  371. }
  372. #ifdef HAS_IPX
  373. case WS_AF_IPX:
  374. {
  375. struct WS_sockaddr_ipx win = {0};
  376. if (wsaddrlen < sizeof(win)) return 0;
  377. memcpy( &win, wsaddr, sizeof(win) );
  378. uaddr->ipx.sipx_family = AF_IPX;
  379. memcpy( &uaddr->ipx.sipx_network, win.sa_netnum, sizeof(win.sa_netnum) );
  380. memcpy( &uaddr->ipx.sipx_node, win.sa_nodenum, sizeof(win.sa_nodenum) );
  381. uaddr->ipx.sipx_port = win.sa_socket;
  382. return sizeof(uaddr->ipx);
  383. }
  384. #endif
  385. #ifdef HAS_IRDA
  386. case WS_AF_IRDA:
  387. {
  388. SOCKADDR_IRDA win = {0};
  389. unsigned int lsap_sel;
  390. if (wsaddrlen < sizeof(win)) return 0;
  391. memcpy( &win, wsaddr, sizeof(win) );
  392. uaddr->irda.sir_family = AF_IRDA;
  393. if (sscanf( win.irdaServiceName, "LSAP-SEL%u", &lsap_sel ) == 1)
  394. uaddr->irda.sir_lsap_sel = lsap_sel;
  395. else
  396. {
  397. uaddr->irda.sir_lsap_sel = LSAP_ANY;
  398. memcpy( uaddr->irda.sir_name, win.irdaServiceName, sizeof(win.irdaServiceName) );
  399. }
  400. memcpy( &uaddr->irda.sir_addr, win.irdaDeviceID, sizeof(win.irdaDeviceID) );
  401. return sizeof(uaddr->irda);
  402. }
  403. #endif
  404. case WS_AF_UNSPEC:
  405. switch (wsaddrlen)
  406. {
  407. default: /* likely an ipv4 address */
  408. case sizeof(struct WS_sockaddr_in):
  409. return sizeof(uaddr->in);
  410. #ifdef HAS_IPX
  411. case sizeof(struct WS_sockaddr_ipx):
  412. return sizeof(uaddr->ipx);
  413. #endif
  414. #ifdef HAS_IRDA
  415. case sizeof(SOCKADDR_IRDA):
  416. return sizeof(uaddr->irda);
  417. #endif
  418. case sizeof(struct WS_sockaddr_in6):
  419. return sizeof(uaddr->in6);
  420. }
  421. default:
  422. return 0;
  423. }
  424. }
  425. static socklen_t get_unix_sockaddr_any( union unix_sockaddr *uaddr, int ws_family )
  426. {
  427. memset( uaddr, 0, sizeof(*uaddr) );
  428. switch (ws_family)
  429. {
  430. case WS_AF_INET:
  431. uaddr->in.sin_family = AF_INET;
  432. return sizeof(uaddr->in);
  433. case WS_AF_INET6:
  434. uaddr->in6.sin6_family = AF_INET6;
  435. return sizeof(uaddr->in6);
  436. #ifdef HAS_IPX
  437. case WS_AF_IPX:
  438. uaddr->ipx.sipx_family = AF_IPX;
  439. return sizeof(uaddr->ipx);
  440. #endif
  441. #ifdef HAS_IRDA
  442. case WS_AF_IRDA:
  443. uaddr->irda.sir_family = AF_IRDA;
  444. return sizeof(uaddr->irda);
  445. #endif
  446. default:
  447. return 0;
  448. }
  449. }
  450. /* some events are generated at the same time but must be sent in a particular
  451. * order (e.g. CONNECT must be sent before READ) */
  452. static const enum afd_poll_bit event_bitorder[] =
  453. {
  454. AFD_POLL_BIT_CONNECT,
  455. AFD_POLL_BIT_CONNECT_ERR,
  456. AFD_POLL_BIT_ACCEPT,
  457. AFD_POLL_BIT_OOB,
  458. AFD_POLL_BIT_WRITE,
  459. AFD_POLL_BIT_READ,
  460. AFD_POLL_BIT_RESET,
  461. AFD_POLL_BIT_HUP,
  462. AFD_POLL_BIT_CLOSE,
  463. };
  464. typedef enum {
  465. SOCK_SHUTDOWN_ERROR = -1,
  466. SOCK_SHUTDOWN_EOF = 0,
  467. SOCK_SHUTDOWN_POLLHUP = 1
  468. } sock_shutdown_t;
  469. static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
  470. static sock_shutdown_t sock_check_pollhup(void)
  471. {
  472. sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
  473. int fd[2], n;
  474. struct pollfd pfd;
  475. char dummy;
  476. if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
  477. if ( shutdown( fd[0], 1 ) ) goto out;
  478. pfd.fd = fd[1];
  479. pfd.events = POLLIN;
  480. pfd.revents = 0;
  481. /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
  482. n = poll( &pfd, 1, 1 );
  483. if ( n != 1 ) goto out; /* error or timeout */
  484. if ( pfd.revents & POLLHUP )
  485. ret = SOCK_SHUTDOWN_POLLHUP;
  486. else if ( pfd.revents & POLLIN &&
  487. read( fd[1], &dummy, 1 ) == 0 )
  488. ret = SOCK_SHUTDOWN_EOF;
  489. out:
  490. close( fd[0] );
  491. close( fd[1] );
  492. return ret;
  493. }
  494. void sock_init(void)
  495. {
  496. sock_shutdown_type = sock_check_pollhup();
  497. switch ( sock_shutdown_type )
  498. {
  499. case SOCK_SHUTDOWN_EOF:
  500. if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
  501. break;
  502. case SOCK_SHUTDOWN_POLLHUP:
  503. if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
  504. break;
  505. default:
  506. fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
  507. sock_shutdown_type = SOCK_SHUTDOWN_EOF;
  508. }
  509. }
  510. static int sock_reselect( struct sock *sock )
  511. {
  512. int ev = sock_get_poll_events( sock->fd );
  513. if (debug_level)
  514. fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
  515. set_fd_events( sock->fd, ev );
  516. return ev;
  517. }
  518. static unsigned int afd_poll_flag_to_win32( unsigned int flags )
  519. {
  520. static const unsigned int map[] =
  521. {
  522. FD_READ, /* READ */
  523. FD_OOB, /* OOB */
  524. FD_WRITE, /* WRITE */
  525. FD_CLOSE, /* HUP */
  526. FD_CLOSE, /* RESET */
  527. 0, /* CLOSE */
  528. FD_CONNECT, /* CONNECT */
  529. FD_ACCEPT, /* ACCEPT */
  530. FD_CONNECT, /* CONNECT_ERR */
  531. };
  532. unsigned int i, ret = 0;
  533. for (i = 0; i < ARRAY_SIZE(map); ++i)
  534. {
  535. if (flags & (1 << i)) ret |= map[i];
  536. }
  537. return ret;
  538. }
  539. /* wake anybody waiting on the socket event or send the associated message */
  540. static void sock_wake_up( struct sock *sock )
  541. {
  542. unsigned int events = sock->pending_events & sock->mask;
  543. int i;
  544. if (sock->event)
  545. {
  546. if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
  547. if (events)
  548. set_event( sock->event );
  549. }
  550. if (sock->window)
  551. {
  552. if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
  553. for (i = 0; i < ARRAY_SIZE(event_bitorder); i++)
  554. {
  555. enum afd_poll_bit event = event_bitorder[i];
  556. if (events & (1 << event))
  557. {
  558. lparam_t lparam = afd_poll_flag_to_win32(1 << event) | (sock_get_error( sock->errors[event] ) << 16);
  559. post_message( sock->window, sock->message, sock->wparam, lparam );
  560. }
  561. }
  562. sock->pending_events = 0;
  563. sock_reselect( sock );
  564. }
  565. }
  566. static inline int sock_error( struct fd *fd )
  567. {
  568. unsigned int optval = 0;
  569. socklen_t optlen = sizeof(optval);
  570. getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
  571. return optval;
  572. }
  573. static void free_accept_req( void *private )
  574. {
  575. struct accept_req *req = private;
  576. list_remove( &req->entry );
  577. if (req->acceptsock)
  578. {
  579. req->acceptsock->accept_recv_req = NULL;
  580. release_object( req->acceptsock );
  581. }
  582. release_object( req->async );
  583. release_object( req->iosb );
  584. release_object( req->sock );
  585. free( req );
  586. }
  587. static void fill_accept_output( struct accept_req *req )
  588. {
  589. const data_size_t out_size = req->iosb->out_size;
  590. struct async *async = req->async;
  591. union unix_sockaddr unix_addr;
  592. struct WS_sockaddr *win_addr;
  593. unsigned int remote_len;
  594. socklen_t unix_len;
  595. int fd, size = 0;
  596. char *out_data;
  597. int win_len;
  598. if (!(out_data = mem_alloc( out_size )))
  599. {
  600. async_terminate( async, get_error() );
  601. return;
  602. }
  603. fd = get_unix_fd( req->acceptsock->fd );
  604. if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
  605. {
  606. if (!req->accepted && errno == EWOULDBLOCK)
  607. {
  608. req->accepted = 1;
  609. sock_reselect( req->acceptsock );
  610. return;
  611. }
  612. async_terminate( async, sock_get_ntstatus( errno ) );
  613. free( out_data );
  614. return;
  615. }
  616. if (req->local_len)
  617. {
  618. if (req->local_len < sizeof(int))
  619. {
  620. async_terminate( async, STATUS_BUFFER_TOO_SMALL );
  621. free( out_data );
  622. return;
  623. }
  624. unix_len = sizeof(unix_addr);
  625. win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
  626. if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
  627. (win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
  628. {
  629. async_terminate( async, sock_get_ntstatus( errno ) );
  630. free( out_data );
  631. return;
  632. }
  633. memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
  634. }
  635. unix_len = sizeof(unix_addr);
  636. win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
  637. remote_len = out_size - req->recv_len - req->local_len;
  638. if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
  639. (win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
  640. {
  641. async_terminate( async, sock_get_ntstatus( errno ) );
  642. free( out_data );
  643. return;
  644. }
  645. memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
  646. async_request_complete( req->async, STATUS_SUCCESS, size, out_size, out_data );
  647. }
  648. static void complete_async_accept( struct sock *sock, struct accept_req *req )
  649. {
  650. struct sock *acceptsock = req->acceptsock;
  651. struct async *async = req->async;
  652. if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
  653. if (acceptsock)
  654. {
  655. if (!accept_into_socket( sock, acceptsock ))
  656. {
  657. async_terminate( async, get_error() );
  658. return;
  659. }
  660. fill_accept_output( req );
  661. }
  662. else
  663. {
  664. obj_handle_t handle;
  665. if (!(acceptsock = accept_socket( sock )))
  666. {
  667. async_terminate( async, get_error() );
  668. return;
  669. }
  670. handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
  671. GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
  672. acceptsock->wparam = handle;
  673. sock_reselect( acceptsock );
  674. release_object( acceptsock );
  675. if (!handle)
  676. {
  677. async_terminate( async, get_error() );
  678. return;
  679. }
  680. async_request_complete_alloc( req->async, STATUS_SUCCESS, 0, sizeof(handle), &handle );
  681. }
  682. }
  683. static void complete_async_accept_recv( struct accept_req *req )
  684. {
  685. if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
  686. assert( req->recv_len );
  687. fill_accept_output( req );
  688. }
  689. static void free_connect_req( void *private )
  690. {
  691. struct connect_req *req = private;
  692. req->sock->connect_req = NULL;
  693. release_object( req->async );
  694. release_object( req->iosb );
  695. release_object( req->sock );
  696. free( req );
  697. }
  698. static void complete_async_connect( struct sock *sock )
  699. {
  700. struct connect_req *req = sock->connect_req;
  701. const char *in_buffer;
  702. size_t len;
  703. int ret;
  704. if (debug_level) fprintf( stderr, "completing connect request for socket %p\n", sock );
  705. sock->state = SOCK_CONNECTED;
  706. if (!req->send_len)
  707. {
  708. async_terminate( req->async, STATUS_SUCCESS );
  709. return;
  710. }
  711. in_buffer = (const char *)req->iosb->in_data + sizeof(struct afd_connect_params) + req->addr_len;
  712. len = req->send_len - req->send_cursor;
  713. ret = send( get_unix_fd( sock->fd ), in_buffer + req->send_cursor, len, 0 );
  714. if (ret < 0 && errno != EWOULDBLOCK)
  715. async_terminate( req->async, sock_get_ntstatus( errno ) );
  716. else if (ret == len)
  717. async_request_complete( req->async, STATUS_SUCCESS, req->send_len, 0, NULL );
  718. else
  719. req->send_cursor += ret;
  720. }
  721. static void free_poll_req( void *private )
  722. {
  723. struct poll_req *req = private;
  724. unsigned int i;
  725. if (req->timeout) remove_timeout_user( req->timeout );
  726. for (i = 0; i < req->count; ++i)
  727. release_object( req->sockets[i].sock );
  728. release_object( req->async );
  729. release_object( req->iosb );
  730. list_remove( &req->entry );
  731. free( req );
  732. }
  733. static int is_oobinline( struct sock *sock )
  734. {
  735. int oobinline;
  736. socklen_t len = sizeof(oobinline);
  737. return !getsockopt( get_unix_fd( sock->fd ), SOL_SOCKET, SO_OOBINLINE, (char *)&oobinline, &len ) && oobinline;
  738. }
  739. static int get_poll_flags( struct sock *sock, int event )
  740. {
  741. int flags = 0;
  742. /* A connection-mode socket which has never been connected does not return
  743. * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
  744. if (sock->state == SOCK_UNCONNECTED)
  745. event &= ~(POLLOUT | POLLHUP);
  746. if (event & POLLIN)
  747. {
  748. if (sock->state == SOCK_LISTENING)
  749. flags |= AFD_POLL_ACCEPT;
  750. else
  751. flags |= AFD_POLL_READ;
  752. }
  753. if (event & POLLPRI)
  754. flags |= is_oobinline( sock ) ? AFD_POLL_READ : AFD_POLL_OOB;
  755. if (event & POLLOUT)
  756. flags |= AFD_POLL_WRITE;
  757. if (sock->state == SOCK_CONNECTED)
  758. flags |= AFD_POLL_CONNECT;
  759. if (event & POLLHUP)
  760. flags |= AFD_POLL_HUP;
  761. if (event & POLLERR)
  762. flags |= AFD_POLL_CONNECT_ERR;
  763. return flags;
  764. }
  765. static void complete_async_poll( struct poll_req *req, unsigned int status )
  766. {
  767. unsigned int i, signaled_count = 0;
  768. for (i = 0; i < req->count; ++i)
  769. {
  770. struct sock *sock = req->sockets[i].sock;
  771. if (sock->main_poll == req)
  772. sock->main_poll = NULL;
  773. }
  774. if (!status)
  775. {
  776. for (i = 0; i < req->count; ++i)
  777. {
  778. if (req->sockets[i].flags)
  779. ++signaled_count;
  780. }
  781. }
  782. if (is_machine_64bit( async_get_thread( req->async )->process->machine ))
  783. {
  784. size_t output_size = offsetof( struct afd_poll_params_64, sockets[signaled_count] );
  785. struct afd_poll_params_64 *output;
  786. if (!(output = mem_alloc( output_size )))
  787. {
  788. async_terminate( req->async, get_error() );
  789. return;
  790. }
  791. memset( output, 0, output_size );
  792. output->timeout = req->orig_timeout;
  793. output->exclusive = req->exclusive;
  794. for (i = 0; i < req->count; ++i)
  795. {
  796. if (!req->sockets[i].flags) continue;
  797. output->sockets[output->count].socket = req->sockets[i].handle;
  798. output->sockets[output->count].flags = req->sockets[i].flags;
  799. output->sockets[output->count].status = req->sockets[i].status;
  800. ++output->count;
  801. }
  802. assert( output->count == signaled_count );
  803. async_request_complete( req->async, status, output_size, output_size, output );
  804. }
  805. else
  806. {
  807. size_t output_size = offsetof( struct afd_poll_params_32, sockets[signaled_count] );
  808. struct afd_poll_params_32 *output;
  809. if (!(output = mem_alloc( output_size )))
  810. {
  811. async_terminate( req->async, get_error() );
  812. return;
  813. }
  814. memset( output, 0, output_size );
  815. output->timeout = req->orig_timeout;
  816. output->exclusive = req->exclusive;
  817. for (i = 0; i < req->count; ++i)
  818. {
  819. if (!req->sockets[i].flags) continue;
  820. output->sockets[output->count].socket = req->sockets[i].handle;
  821. output->sockets[output->count].flags = req->sockets[i].flags;
  822. output->sockets[output->count].status = req->sockets[i].status;
  823. ++output->count;
  824. }
  825. assert( output->count == signaled_count );
  826. async_request_complete( req->async, status, output_size, output_size, output );
  827. }
  828. }
  829. static void complete_async_polls( struct sock *sock, int event, int error )
  830. {
  831. int flags = get_poll_flags( sock, event );
  832. struct poll_req *req, *next;
  833. LIST_FOR_EACH_ENTRY_SAFE( req, next, &poll_list, struct poll_req, entry )
  834. {
  835. unsigned int i;
  836. if (req->iosb->status != STATUS_PENDING) continue;
  837. for (i = 0; i < req->count; ++i)
  838. {
  839. if (req->sockets[i].sock != sock) continue;
  840. if (!(req->sockets[i].mask & flags)) continue;
  841. if (debug_level)
  842. fprintf( stderr, "completing poll for socket %p, wanted %#x got %#x\n",
  843. sock, req->sockets[i].mask, flags );
  844. req->sockets[i].flags = req->sockets[i].mask & flags;
  845. req->sockets[i].status = sock_get_ntstatus( error );
  846. complete_async_poll( req, STATUS_SUCCESS );
  847. break;
  848. }
  849. }
  850. }
  851. static void async_poll_timeout( void *private )
  852. {
  853. struct poll_req *req = private;
  854. req->timeout = NULL;
  855. if (req->iosb->status != STATUS_PENDING) return;
  856. complete_async_poll( req, STATUS_TIMEOUT );
  857. }
  858. static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
  859. {
  860. if (event & (POLLIN | POLLPRI))
  861. {
  862. struct accept_req *req;
  863. LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
  864. {
  865. if (req->iosb->status == STATUS_PENDING && !req->accepted)
  866. {
  867. complete_async_accept( sock, req );
  868. break;
  869. }
  870. }
  871. if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
  872. complete_async_accept_recv( sock->accept_recv_req );
  873. }
  874. if ((event & POLLOUT) && sock->connect_req && sock->connect_req->iosb->status == STATUS_PENDING)
  875. complete_async_connect( sock );
  876. if (event & (POLLIN | POLLPRI) && async_waiting( &sock->read_q ))
  877. {
  878. if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
  879. async_wake_up( &sock->read_q, STATUS_ALERTED );
  880. event &= ~(POLLIN | POLLPRI);
  881. }
  882. if (event & POLLOUT && async_waiting( &sock->write_q ))
  883. {
  884. if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
  885. async_wake_up( &sock->write_q, STATUS_ALERTED );
  886. event &= ~POLLOUT;
  887. }
  888. if (event & (POLLERR | POLLHUP))
  889. {
  890. int status = sock_get_ntstatus( error );
  891. struct accept_req *req, *next;
  892. if (sock->rd_shutdown || sock->hangup)
  893. async_wake_up( &sock->read_q, status );
  894. if (sock->wr_shutdown)
  895. async_wake_up( &sock->write_q, status );
  896. LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
  897. {
  898. if (req->iosb->status == STATUS_PENDING)
  899. async_terminate( req->async, status );
  900. }
  901. if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
  902. async_terminate( sock->accept_recv_req->async, status );
  903. if (sock->connect_req)
  904. async_terminate( sock->connect_req->async, status );
  905. }
  906. return event;
  907. }
  908. static void post_socket_event( struct sock *sock, enum afd_poll_bit event_bit, int error )
  909. {
  910. unsigned int event = (1 << event_bit);
  911. if (!(sock->reported_events & event))
  912. {
  913. sock->pending_events |= event;
  914. sock->reported_events |= event;
  915. sock->errors[event_bit] = error;
  916. }
  917. }
  918. static void sock_dispatch_events( struct sock *sock, enum connection_state prevstate, int event, int error )
  919. {
  920. switch (prevstate)
  921. {
  922. case SOCK_UNCONNECTED:
  923. break;
  924. case SOCK_CONNECTING:
  925. if (event & POLLOUT)
  926. {
  927. post_socket_event( sock, AFD_POLL_BIT_CONNECT, 0 );
  928. sock->errors[AFD_POLL_BIT_CONNECT_ERR] = 0;
  929. }
  930. if (event & (POLLERR | POLLHUP))
  931. post_socket_event( sock, AFD_POLL_BIT_CONNECT_ERR, error );
  932. break;
  933. case SOCK_LISTENING:
  934. if (event & (POLLIN | POLLERR | POLLHUP))
  935. post_socket_event( sock, AFD_POLL_BIT_ACCEPT, error );
  936. break;
  937. case SOCK_CONNECTED:
  938. case SOCK_CONNECTIONLESS:
  939. if (event & POLLIN)
  940. post_socket_event( sock, AFD_POLL_BIT_READ, 0 );
  941. if (event & POLLOUT)
  942. post_socket_event( sock, AFD_POLL_BIT_WRITE, 0 );
  943. if (event & POLLPRI)
  944. post_socket_event( sock, AFD_POLL_BIT_OOB, 0 );
  945. if (event & (POLLERR | POLLHUP))
  946. post_socket_event( sock, AFD_POLL_BIT_HUP, error );
  947. break;
  948. }
  949. sock_wake_up( sock );
  950. }
  951. static void sock_poll_event( struct fd *fd, int event )
  952. {
  953. struct sock *sock = get_fd_user( fd );
  954. int hangup_seen = 0;
  955. enum connection_state prevstate = sock->state;
  956. int error = 0;
  957. assert( sock->obj.ops == &sock_ops );
  958. if (debug_level)
  959. fprintf(stderr, "socket %p select event: %x\n", sock, event);
  960. /* we may change event later, remove from loop here */
  961. if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
  962. switch (sock->state)
  963. {
  964. case SOCK_UNCONNECTED:
  965. break;
  966. case SOCK_CONNECTING:
  967. if (event & (POLLERR|POLLHUP))
  968. {
  969. sock->state = SOCK_UNCONNECTED;
  970. event &= ~POLLOUT;
  971. error = sock_error( fd );
  972. }
  973. else if (event & POLLOUT)
  974. {
  975. sock->state = SOCK_CONNECTED;
  976. sock->connect_time = current_time;
  977. }
  978. break;
  979. case SOCK_LISTENING:
  980. if (event & (POLLERR|POLLHUP))
  981. error = sock_error( fd );
  982. break;
  983. case SOCK_CONNECTED:
  984. case SOCK_CONNECTIONLESS:
  985. if (sock->type == WS_SOCK_STREAM && (event & POLLIN))
  986. {
  987. char dummy;
  988. int nr;
  989. /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
  990. * has been closed, so we need to check for it explicitly here */
  991. nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
  992. if ( nr == 0 )
  993. {
  994. hangup_seen = 1;
  995. event &= ~POLLIN;
  996. }
  997. else if ( nr < 0 )
  998. {
  999. event &= ~POLLIN;
  1000. /* EAGAIN can happen if an async recv() falls between the server's poll()
  1001. call and the invocation of this routine */
  1002. if ( errno != EAGAIN )
  1003. {
  1004. error = errno;
  1005. event |= POLLERR;
  1006. if ( debug_level )
  1007. fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
  1008. }
  1009. }
  1010. }
  1011. if (hangup_seen || (sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP)))
  1012. {
  1013. sock->hangup = 1;
  1014. }
  1015. else if (event & (POLLHUP | POLLERR))
  1016. {
  1017. sock->aborted = 1;
  1018. if (debug_level)
  1019. fprintf( stderr, "socket %p aborted by error %d, event %#x\n", sock, error, event );
  1020. }
  1021. if (hangup_seen)
  1022. event |= POLLHUP;
  1023. break;
  1024. }
  1025. complete_async_polls( sock, event, error );
  1026. event = sock_dispatch_asyncs( sock, event, error );
  1027. sock_dispatch_events( sock, prevstate, event, error );
  1028. sock_reselect( sock );
  1029. }
  1030. static void sock_dump( struct object *obj, int verbose )
  1031. {
  1032. struct sock *sock = (struct sock *)obj;
  1033. assert( obj->ops == &sock_ops );
  1034. fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
  1035. sock->fd, sock->state,
  1036. sock->mask, sock->pending_events, sock->reported_events );
  1037. }
  1038. static int poll_flags_from_afd( struct sock *sock, int flags )
  1039. {
  1040. int ev = 0;
  1041. /* A connection-mode socket which has never been connected does
  1042. * not return write or hangup events, but Linux returns
  1043. * POLLOUT | POLLHUP. */
  1044. if (sock->state == SOCK_UNCONNECTED)
  1045. return -1;
  1046. if (flags & (AFD_POLL_READ | AFD_POLL_ACCEPT))
  1047. ev |= POLLIN;
  1048. if ((flags & AFD_POLL_HUP) && sock->type == WS_SOCK_STREAM)
  1049. ev |= POLLIN;
  1050. if (flags & AFD_POLL_OOB)
  1051. ev |= is_oobinline( sock ) ? POLLIN : POLLPRI;
  1052. if (flags & AFD_POLL_WRITE)
  1053. ev |= POLLOUT;
  1054. return ev;
  1055. }
  1056. static int sock_get_poll_events( struct fd *fd )
  1057. {
  1058. struct sock *sock = get_fd_user( fd );
  1059. unsigned int mask = sock->mask & ~sock->reported_events;
  1060. struct poll_req *req;
  1061. int ev = 0;
  1062. assert( sock->obj.ops == &sock_ops );
  1063. if (!sock->type) /* not initialized yet */
  1064. return -1;
  1065. switch (sock->state)
  1066. {
  1067. case SOCK_UNCONNECTED:
  1068. /* A connection-mode Windows socket which has never been connected does
  1069. * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
  1070. * need to return -1 here, to prevent the socket from being polled on at
  1071. * all. */
  1072. return -1;
  1073. case SOCK_CONNECTING:
  1074. return POLLOUT;
  1075. case SOCK_LISTENING:
  1076. if (!list_empty( &sock->accept_list ) || (mask & AFD_POLL_ACCEPT))
  1077. ev |= POLLIN;
  1078. break;
  1079. case SOCK_CONNECTED:
  1080. case SOCK_CONNECTIONLESS:
  1081. if (sock->hangup && sock->wr_shutdown && !sock->wr_shutdown_pending)
  1082. {
  1083. /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
  1084. * if both the socket and its peer are SHUT_WR.
  1085. *
  1086. * We don't use SHUT_RD, so we can only encounter this in the latter
  1087. * case. In that case there can't be any pending read requests (they
  1088. * would have already been completed with a length of zero), the
  1089. * above condition ensures that we don't have any pending write
  1090. * requests, and nothing that can change about the socket state that
  1091. * would complete a pending poll request. */
  1092. return -1;
  1093. }
  1094. if (sock->aborted)
  1095. return -1;
  1096. if (sock->accept_recv_req)
  1097. {
  1098. ev |= POLLIN;
  1099. }
  1100. else if (async_queued( &sock->read_q ))
  1101. {
  1102. if (async_waiting( &sock->read_q )) ev |= POLLIN | POLLPRI;
  1103. }
  1104. else
  1105. {
  1106. /* Don't ask for POLLIN if we got a hangup. We won't receive more
  1107. * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
  1108. if (!sock->hangup)
  1109. {
  1110. if (mask & AFD_POLL_READ)
  1111. ev |= POLLIN;
  1112. if (mask & AFD_POLL_OOB)
  1113. ev |= POLLPRI;
  1114. }
  1115. /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
  1116. if (sock->state == SOCK_CONNECTED && (mask & AFD_POLL_HUP) && !(sock->reported_events & AFD_POLL_READ))
  1117. ev |= POLLIN;
  1118. }
  1119. if (async_queued( &sock->write_q ))
  1120. {
  1121. if (async_waiting( &sock->write_q )) ev |= POLLOUT;
  1122. }
  1123. else if (!sock->wr_shutdown && (mask & AFD_POLL_WRITE))
  1124. {
  1125. ev |= POLLOUT;
  1126. }
  1127. break;
  1128. }
  1129. LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
  1130. {
  1131. unsigned int i;
  1132. for (i = 0; i < req->count; ++i)
  1133. {
  1134. if (req->sockets[i].sock != sock) continue;
  1135. ev |= poll_flags_from_afd( sock, req->sockets[i].mask );
  1136. }
  1137. }
  1138. return ev;
  1139. }
  1140. static enum server_fd_type sock_get_fd_type( struct fd *fd )
  1141. {
  1142. return FD_TYPE_SOCKET;
  1143. }
  1144. static void sock_cancel_async( struct fd *fd, struct async *async )
  1145. {
  1146. struct poll_req *req;
  1147. LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
  1148. {
  1149. unsigned int i;
  1150. if (req->async != async)
  1151. continue;
  1152. for (i = 0; i < req->count; i++)
  1153. {
  1154. struct sock *sock = req->sockets[i].sock;
  1155. if (sock->main_poll == req)
  1156. sock->main_poll = NULL;
  1157. }
  1158. }
  1159. async_terminate( async, STATUS_CANCELLED );
  1160. }
  1161. static void sock_queue_async( struct fd *fd, struct async *async, int type, int count )
  1162. {
  1163. struct sock *sock = get_fd_user( fd );
  1164. struct async_queue *queue;
  1165. assert( sock->obj.ops == &sock_ops );
  1166. switch (type)
  1167. {
  1168. case ASYNC_TYPE_READ:
  1169. if (sock->rd_shutdown)
  1170. {
  1171. set_error( STATUS_PIPE_DISCONNECTED );
  1172. return;
  1173. }
  1174. queue = &sock->read_q;
  1175. break;
  1176. case ASYNC_TYPE_WRITE:
  1177. if (sock->wr_shutdown)
  1178. {
  1179. set_error( STATUS_PIPE_DISCONNECTED );
  1180. return;
  1181. }
  1182. queue = &sock->write_q;
  1183. break;
  1184. default:
  1185. set_error( STATUS_INVALID_PARAMETER );
  1186. return;
  1187. }
  1188. if (sock->state != SOCK_CONNECTED)
  1189. {
  1190. set_error( STATUS_PIPE_DISCONNECTED );
  1191. return;
  1192. }
  1193. queue_async( queue, async );
  1194. sock_reselect( sock );
  1195. set_error( STATUS_PENDING );
  1196. }
  1197. static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
  1198. {
  1199. struct sock *sock = get_fd_user( fd );
  1200. if (sock->wr_shutdown_pending && list_empty( &sock->write_q.queue ))
  1201. {
  1202. shutdown( get_unix_fd( sock->fd ), SHUT_WR );
  1203. sock->wr_shutdown_pending = 0;
  1204. }
  1205. /* Don't reselect the ifchange queue; we always ask for POLLIN.
  1206. * Don't reselect an uninitialized socket; we can't call set_fd_events() on
  1207. * a pseudo-fd. */
  1208. if (queue != &sock->ifchange_q && sock->type)
  1209. sock_reselect( sock );
  1210. }
  1211. static struct fd *sock_get_fd( struct object *obj )
  1212. {
  1213. struct sock *sock = (struct sock *)obj;
  1214. return (struct fd *)grab_object( sock->fd );
  1215. }
  1216. static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
  1217. {
  1218. struct sock *sock = (struct sock *)obj;
  1219. if (sock->obj.handle_count == 1) /* last handle */
  1220. {
  1221. struct accept_req *accept_req, *accept_next;
  1222. struct poll_req *poll_req, *poll_next;
  1223. if (sock->accept_recv_req)
  1224. async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
  1225. LIST_FOR_EACH_ENTRY_SAFE( accept_req, accept_next, &sock->accept_list, struct accept_req, entry )
  1226. async_terminate( accept_req->async, STATUS_CANCELLED );
  1227. if (sock->connect_req)
  1228. async_terminate( sock->connect_req->async, STATUS_CANCELLED );
  1229. LIST_FOR_EACH_ENTRY_SAFE( poll_req, poll_next, &poll_list, struct poll_req, entry )
  1230. {
  1231. struct iosb *iosb = poll_req->iosb;
  1232. BOOL signaled = FALSE;
  1233. unsigned int i;
  1234. if (iosb->status != STATUS_PENDING) continue;
  1235. for (i = 0; i < poll_req->count; ++i)
  1236. {
  1237. if (poll_req->sockets[i].sock == sock)
  1238. {
  1239. signaled = TRUE;
  1240. poll_req->sockets[i].flags = AFD_POLL_CLOSE;
  1241. poll_req->sockets[i].status = 0;
  1242. }
  1243. }
  1244. if (signaled) complete_async_poll( poll_req, STATUS_SUCCESS );
  1245. }
  1246. }
  1247. return 1;
  1248. }
  1249. static void sock_destroy( struct object *obj )
  1250. {
  1251. struct sock *sock = (struct sock *)obj;
  1252. assert( obj->ops == &sock_ops );
  1253. /* FIXME: special socket shutdown stuff? */
  1254. if ( sock->deferred )
  1255. release_object( sock->deferred );
  1256. async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
  1257. sock_release_ifchange( sock );
  1258. free_async_queue( &sock->read_q );
  1259. free_async_queue( &sock->write_q );
  1260. free_async_queue( &sock->ifchange_q );
  1261. free_async_queue( &sock->accept_q );
  1262. free_async_queue( &sock->connect_q );
  1263. free_async_queue( &sock->poll_q );
  1264. if (sock->event) release_object( sock->event );
  1265. if (sock->fd)
  1266. {
  1267. /* shut the socket down to force pending poll() calls in the client to return */
  1268. shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
  1269. release_object( sock->fd );
  1270. }
  1271. }
  1272. static struct sock *create_socket(void)
  1273. {
  1274. struct sock *sock;
  1275. if (!(sock = alloc_object( &sock_ops ))) return NULL;
  1276. sock->fd = NULL;
  1277. sock->state = SOCK_UNCONNECTED;
  1278. sock->mask = 0;
  1279. sock->pending_events = 0;
  1280. sock->reported_events = 0;
  1281. sock->flags = 0;
  1282. sock->proto = 0;
  1283. sock->type = 0;
  1284. sock->family = 0;
  1285. sock->event = NULL;
  1286. sock->window = 0;
  1287. sock->message = 0;
  1288. sock->wparam = 0;
  1289. sock->connect_time = 0;
  1290. sock->deferred = NULL;
  1291. sock->ifchange_obj = NULL;
  1292. sock->accept_recv_req = NULL;
  1293. sock->connect_req = NULL;
  1294. sock->main_poll = NULL;
  1295. memset( &sock->addr, 0, sizeof(sock->addr) );
  1296. sock->addr_len = 0;
  1297. sock->rd_shutdown = 0;
  1298. sock->wr_shutdown = 0;
  1299. sock->wr_shutdown_pending = 0;
  1300. sock->hangup = 0;
  1301. sock->aborted = 0;
  1302. sock->nonblocking = 0;
  1303. sock->bound = 0;
  1304. sock->rcvbuf = 0;
  1305. sock->sndbuf = 0;
  1306. sock->rcvtimeo = 0;
  1307. sock->sndtimeo = 0;
  1308. init_async_queue( &sock->read_q );
  1309. init_async_queue( &sock->write_q );
  1310. init_async_queue( &sock->ifchange_q );
  1311. init_async_queue( &sock->accept_q );
  1312. init_async_queue( &sock->connect_q );
  1313. init_async_queue( &sock->poll_q );
  1314. memset( sock->errors, 0, sizeof(sock->errors) );
  1315. list_init( &sock->accept_list );
  1316. return sock;
  1317. }
  1318. static int get_unix_family( int family )
  1319. {
  1320. switch (family)
  1321. {
  1322. case WS_AF_INET: return AF_INET;
  1323. case WS_AF_INET6: return AF_INET6;
  1324. #ifdef HAS_IPX
  1325. case WS_AF_IPX: return AF_IPX;
  1326. #endif
  1327. #ifdef AF_IRDA
  1328. case WS_AF_IRDA: return AF_IRDA;
  1329. #endif
  1330. case WS_AF_UNSPEC: return AF_UNSPEC;
  1331. default: return -1;
  1332. }
  1333. }
  1334. static int get_unix_type( int type )
  1335. {
  1336. switch (type)
  1337. {
  1338. case WS_SOCK_DGRAM: return SOCK_DGRAM;
  1339. case WS_SOCK_RAW: return SOCK_RAW;
  1340. case WS_SOCK_STREAM: return SOCK_STREAM;
  1341. default: return -1;
  1342. }
  1343. }
  1344. static int get_unix_protocol( int protocol )
  1345. {
  1346. if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
  1347. return protocol;
  1348. switch (protocol)
  1349. {
  1350. case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
  1351. case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
  1352. case WS_IPPROTO_IP: return IPPROTO_IP;
  1353. case WS_IPPROTO_IPV4: return IPPROTO_IPIP;
  1354. case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
  1355. case WS_IPPROTO_RAW: return IPPROTO_RAW;
  1356. case WS_IPPROTO_TCP: return IPPROTO_TCP;
  1357. case WS_IPPROTO_UDP: return IPPROTO_UDP;
  1358. default: return -1;
  1359. }
  1360. }
  1361. static void set_dont_fragment( int fd, int level, int value )
  1362. {
  1363. int optname;
  1364. if (level == IPPROTO_IP)
  1365. {
  1366. #ifdef IP_DONTFRAG
  1367. optname = IP_DONTFRAG;
  1368. #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
  1369. optname = IP_MTU_DISCOVER;
  1370. value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
  1371. #else
  1372. return;
  1373. #endif
  1374. }
  1375. else
  1376. {
  1377. #ifdef IPV6_DONTFRAG
  1378. optname = IPV6_DONTFRAG;
  1379. #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
  1380. optname = IPV6_MTU_DISCOVER;
  1381. value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
  1382. #else
  1383. return;
  1384. #endif
  1385. }
  1386. setsockopt( fd, level, optname, &value, sizeof(value) );
  1387. }
  1388. static int init_socket( struct sock *sock, int family, int type, int protocol, unsigned int flags )
  1389. {
  1390. unsigned int options = 0;
  1391. int sockfd, unix_type, unix_family, unix_protocol, value;
  1392. socklen_t len;
  1393. unix_family = get_unix_family( family );
  1394. unix_type = get_unix_type( type );
  1395. unix_protocol = get_unix_protocol( protocol );
  1396. if (unix_protocol < 0)
  1397. {
  1398. if (type && unix_type < 0)
  1399. set_win32_error( WSAESOCKTNOSUPPORT );
  1400. else
  1401. set_win32_error( WSAEPROTONOSUPPORT );
  1402. return -1;
  1403. }
  1404. if (unix_family < 0)
  1405. {
  1406. if (family >= 0 && unix_type < 0)
  1407. set_win32_error( WSAESOCKTNOSUPPORT );
  1408. else
  1409. set_win32_error( WSAEAFNOSUPPORT );
  1410. return -1;
  1411. }
  1412. sockfd = socket( unix_family, unix_type, unix_protocol );
  1413. if (sockfd == -1)
  1414. {
  1415. if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
  1416. else set_win32_error( sock_get_error( errno ));
  1417. return -1;
  1418. }
  1419. fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
  1420. if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
  1421. {
  1422. #ifdef HAS_IPX
  1423. int ipx_type = protocol - WS_NSPROTO_IPX;
  1424. #ifdef SOL_IPX
  1425. setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
  1426. #else
  1427. struct ipx val;
  1428. /* Should we retrieve val using a getsockopt call and then
  1429. * set the modified one? */
  1430. val.ipx_pt = ipx_type;
  1431. setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
  1432. #endif
  1433. #endif
  1434. }
  1435. if (unix_family == AF_INET || unix_family == AF_INET6)
  1436. {
  1437. /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
  1438. if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
  1439. set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
  1440. else if (unix_type == SOCK_STREAM)
  1441. set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
  1442. }
  1443. #ifdef IPV6_V6ONLY
  1444. if (unix_family == AF_INET6)
  1445. {
  1446. static const int enable = 1;
  1447. setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
  1448. }
  1449. #endif
  1450. len = sizeof(value);
  1451. if (!getsockopt( sockfd, SOL_SOCKET, SO_RCVBUF, &value, &len ))
  1452. sock->rcvbuf = value;
  1453. len = sizeof(value);
  1454. if (!getsockopt( sockfd, SOL_SOCKET, SO_SNDBUF, &value, &len ))
  1455. sock->sndbuf = value;
  1456. sock->state = (type == WS_SOCK_STREAM ? SOCK_UNCONNECTED : SOCK_CONNECTIONLESS);
  1457. sock->flags = flags;
  1458. sock->proto = protocol;
  1459. sock->type = type;
  1460. sock->family = family;
  1461. if (sock->fd)
  1462. {
  1463. options = get_fd_options( sock->fd );
  1464. release_object( sock->fd );
  1465. }
  1466. if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
  1467. {
  1468. return -1;
  1469. }
  1470. /* We can't immediately allow caching for a connection-mode socket, since it
  1471. * might be accepted into (changing the underlying fd object.) */
  1472. if (sock->type != WS_SOCK_STREAM) allow_fd_caching( sock->fd );
  1473. return 0;
  1474. }
  1475. /* accepts a socket and inits it */
  1476. static int accept_new_fd( struct sock *sock )
  1477. {
  1478. /* Try to accept(2). We can't be safe that this an already connected socket
  1479. * or that accept() is allowed on it. In those cases we will get -1/errno
  1480. * return.
  1481. */
  1482. struct sockaddr saddr;
  1483. socklen_t slen = sizeof(saddr);
  1484. int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
  1485. if (acceptfd != -1)
  1486. fcntl( acceptfd, F_SETFL, O_NONBLOCK );
  1487. else
  1488. set_error( sock_get_ntstatus( errno ));
  1489. return acceptfd;
  1490. }
  1491. /* accept a socket (creates a new fd) */
  1492. static struct sock *accept_socket( struct sock *sock )
  1493. {
  1494. struct sock *acceptsock;
  1495. int acceptfd;
  1496. if (get_unix_fd( sock->fd ) == -1) return NULL;
  1497. if ( sock->deferred )
  1498. {
  1499. acceptsock = sock->deferred;
  1500. sock->deferred = NULL;
  1501. }
  1502. else
  1503. {
  1504. union unix_sockaddr unix_addr;
  1505. socklen_t unix_len;
  1506. if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
  1507. if (!(acceptsock = create_socket()))
  1508. {
  1509. close( acceptfd );
  1510. return NULL;
  1511. }
  1512. /* newly created socket gets the same properties of the listening socket */
  1513. acceptsock->state = SOCK_CONNECTED;
  1514. acceptsock->bound = 1;
  1515. acceptsock->nonblocking = sock->nonblocking;
  1516. acceptsock->mask = sock->mask;
  1517. acceptsock->proto = sock->proto;
  1518. acceptsock->type = sock->type;
  1519. acceptsock->family = sock->family;
  1520. acceptsock->window = sock->window;
  1521. acceptsock->message = sock->message;
  1522. acceptsock->connect_time = current_time;
  1523. if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
  1524. acceptsock->flags = sock->flags;
  1525. if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
  1526. get_fd_options( sock->fd ) )))
  1527. {
  1528. release_object( acceptsock );
  1529. return NULL;
  1530. }
  1531. unix_len = sizeof(unix_addr);
  1532. if (!getsockname( acceptfd, &unix_addr.addr, &unix_len ))
  1533. acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
  1534. }
  1535. clear_error();
  1536. sock->pending_events &= ~AFD_POLL_ACCEPT;
  1537. sock->reported_events &= ~AFD_POLL_ACCEPT;
  1538. sock_reselect( sock );
  1539. return acceptsock;
  1540. }
  1541. static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
  1542. {
  1543. union unix_sockaddr unix_addr;
  1544. socklen_t unix_len;
  1545. int acceptfd;
  1546. struct fd *newfd;
  1547. if (get_unix_fd( sock->fd ) == -1) return FALSE;
  1548. if ( sock->deferred )
  1549. {
  1550. newfd = dup_fd_object( sock->deferred->fd, 0, 0,
  1551. get_fd_options( acceptsock->fd ) );
  1552. if ( !newfd )
  1553. return FALSE;
  1554. set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
  1555. release_object( sock->deferred );
  1556. sock->deferred = NULL;
  1557. }
  1558. else
  1559. {
  1560. if ((acceptfd = accept_new_fd( sock )) == -1)
  1561. return FALSE;
  1562. if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
  1563. get_fd_options( acceptsock->fd ) )))
  1564. return FALSE;
  1565. }
  1566. acceptsock->state = SOCK_CONNECTED;
  1567. acceptsock->pending_events = 0;
  1568. acceptsock->reported_events = 0;
  1569. acceptsock->proto = sock->proto;
  1570. acceptsock->type = sock->type;
  1571. acceptsock->family = sock->family;
  1572. acceptsock->wparam = 0;
  1573. acceptsock->deferred = NULL;
  1574. acceptsock->connect_time = current_time;
  1575. fd_copy_completion( acceptsock->fd, newfd );
  1576. release_object( acceptsock->fd );
  1577. acceptsock->fd = newfd;
  1578. unix_len = sizeof(unix_addr);
  1579. if (!getsockname( get_unix_fd( newfd ), &unix_addr.addr, &unix_len ))
  1580. acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
  1581. clear_error();
  1582. sock->pending_events &= ~AFD_POLL_ACCEPT;
  1583. sock->reported_events &= ~AFD_POLL_ACCEPT;
  1584. sock_reselect( sock );
  1585. return TRUE;
  1586. }
  1587. #ifdef IP_BOUND_IF
  1588. static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
  1589. {
  1590. static const int enable = 1;
  1591. unsigned int index;
  1592. if (!(index = if_nametoindex( name )))
  1593. return -1;
  1594. if (setsockopt( fd, IPPROTO_IP, IP_BOUND_IF, &index, sizeof(index) ))
  1595. return -1;
  1596. return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable) );
  1597. }
  1598. #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
  1599. struct interface_filter
  1600. {
  1601. struct sock_filter iface_memaddr;
  1602. struct sock_filter iface_rule;
  1603. struct sock_filter ip_memaddr;
  1604. struct sock_filter ip_rule;
  1605. struct sock_filter return_keep;
  1606. struct sock_filter return_dump;
  1607. };
  1608. # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
  1609. -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
  1610. /sizeof(struct sock_filter)
  1611. # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
  1612. -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
  1613. /sizeof(struct sock_filter)
  1614. # define FILTER_JUMP_NEXT() (u_char)(0)
  1615. # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
  1616. static struct interface_filter generic_interface_filter =
  1617. {
  1618. /* This filter rule allows incoming packets on the specified interface, which works for all
  1619. * remotely generated packets and for locally generated broadcast packets. */
  1620. BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_AD_OFF+SKF_AD_IFINDEX),
  1621. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule), FILTER_JUMP_NEXT()),
  1622. /* This rule allows locally generated packets targeted at the specific IP address of the chosen
  1623. * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
  1624. BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_NET_OFF+SKF_NET_DESTIP),
  1625. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule), FILTER_JUMP_DUMP(ip_rule)),
  1626. BPF_STMT(BPF_RET+BPF_K, (u_int)-1), /* keep packet */
  1627. BPF_STMT(BPF_RET+BPF_K, 0) /* dump packet */
  1628. };
  1629. static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
  1630. {
  1631. struct interface_filter specific_interface_filter;
  1632. struct sock_fprog filter_prog;
  1633. static const int enable = 1;
  1634. unsigned int index;
  1635. in_addr_t ifindex;
  1636. if (!setsockopt( fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen( name ) + 1 ))
  1637. return 0;
  1638. /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
  1639. if (debug_level)
  1640. fprintf( stderr, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
  1641. fd, name, strerror( errno ));
  1642. if (!(index = if_nametoindex( name )))
  1643. return -1;
  1644. ifindex = htonl( index );
  1645. if (setsockopt( fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex, sizeof(ifindex) ) < 0)
  1646. return -1;
  1647. specific_interface_filter = generic_interface_filter;
  1648. specific_interface_filter.iface_rule.k = index;
  1649. specific_interface_filter.ip_rule.k = htonl( bind_addr );
  1650. filter_prog.len = sizeof(generic_interface_filter) / sizeof(struct sock_filter);
  1651. filter_prog.filter = (struct sock_filter *)&specific_interface_filter;
  1652. if (setsockopt( fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog) ))
  1653. return -1;
  1654. return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable) );
  1655. }
  1656. #else
  1657. static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
  1658. {
  1659. errno = EOPNOTSUPP;
  1660. return -1;
  1661. }
  1662. #endif /* LINUX_BOUND_IF */
  1663. /* Take bind() calls on any name corresponding to a local network adapter and
  1664. * restrict the given socket to operating only on the specified interface. This
  1665. * restriction consists of two components:
  1666. * 1) An outgoing packet restriction suggesting the egress interface for all
  1667. * packets.
  1668. * 2) An incoming packet restriction dropping packets not meant for the
  1669. * interface.
  1670. * If the function succeeds in placing these restrictions, then the name for the
  1671. * bind() may safely be changed to INADDR_ANY, permitting the transmission and
  1672. * receipt of broadcast packets on the socket. This behavior is only relevant to
  1673. * UDP sockets and is needed for applications that expect to be able to receive
  1674. * broadcast packets on a socket that is bound to a specific network interface.
  1675. */
  1676. static int bind_to_interface( struct sock *sock, const struct sockaddr_in *addr )
  1677. {
  1678. in_addr_t bind_addr = addr->sin_addr.s_addr;
  1679. struct ifaddrs *ifaddrs, *ifaddr;
  1680. int fd = get_unix_fd( sock->fd );
  1681. int err = 0;
  1682. if (bind_addr == htonl( INADDR_ANY ) || bind_addr == htonl( INADDR_LOOPBACK ))
  1683. return 0;
  1684. if (sock->type != WS_SOCK_DGRAM)
  1685. return 0;
  1686. if (getifaddrs( &ifaddrs ) < 0) return 0;
  1687. for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
  1688. {
  1689. if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET
  1690. && ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr == bind_addr)
  1691. {
  1692. if ((err = bind_to_iface_name( fd, bind_addr, ifaddr->ifa_name )) < 0)
  1693. {
  1694. if (debug_level)
  1695. fprintf( stderr, "failed to bind to interface: %s\n", strerror( errno ) );
  1696. }
  1697. break;
  1698. }
  1699. }
  1700. freeifaddrs( ifaddrs );
  1701. return !err;
  1702. }
  1703. #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
  1704. static unsigned int get_ipv6_interface_index( const struct in6_addr *addr )
  1705. {
  1706. struct ifaddrs *ifaddrs, *ifaddr;
  1707. if (getifaddrs( &ifaddrs ) < 0) return 0;
  1708. for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
  1709. {
  1710. if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET6
  1711. && !memcmp( &((struct sockaddr_in6 *)ifaddr->ifa_addr)->sin6_addr, addr, sizeof(*addr) ))
  1712. {
  1713. unsigned int index = if_nametoindex( ifaddr->ifa_name );
  1714. if (!index)
  1715. {
  1716. if (debug_level)
  1717. fprintf( stderr, "Unable to look up interface index for %s: %s\n",
  1718. ifaddr->ifa_name, strerror( errno ) );
  1719. continue;
  1720. }
  1721. freeifaddrs( ifaddrs );
  1722. return index;
  1723. }
  1724. }
  1725. freeifaddrs( ifaddrs );
  1726. return 0;
  1727. }
  1728. #endif
  1729. /* return an errno value mapped to a WSA error */
  1730. static unsigned int sock_get_error( int err )
  1731. {
  1732. switch (err)
  1733. {
  1734. case EINTR: return WSAEINTR;
  1735. case EBADF: return WSAEBADF;
  1736. case EPERM:
  1737. case EACCES: return WSAEACCES;
  1738. case EFAULT: return WSAEFAULT;
  1739. case EINVAL: return WSAEINVAL;
  1740. case EMFILE: return WSAEMFILE;
  1741. case EINPROGRESS:
  1742. case EWOULDBLOCK: return WSAEWOULDBLOCK;
  1743. case EALREADY: return WSAEALREADY;
  1744. case ENOTSOCK: return WSAENOTSOCK;
  1745. case EDESTADDRREQ: return WSAEDESTADDRREQ;
  1746. case EMSGSIZE: return WSAEMSGSIZE;
  1747. case EPROTOTYPE: return WSAEPROTOTYPE;
  1748. case ENOPROTOOPT: return WSAENOPROTOOPT;
  1749. case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
  1750. case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
  1751. case EOPNOTSUPP: return WSAEOPNOTSUPP;
  1752. case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
  1753. case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
  1754. case EADDRINUSE: return WSAEADDRINUSE;
  1755. case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
  1756. case ENETDOWN: return WSAENETDOWN;
  1757. case ENETUNREACH: return WSAENETUNREACH;
  1758. case ENETRESET: return WSAENETRESET;
  1759. case ECONNABORTED: return WSAECONNABORTED;
  1760. case EPIPE:
  1761. case ECONNRESET: return WSAECONNRESET;
  1762. case ENOBUFS: return WSAENOBUFS;
  1763. case EISCONN: return WSAEISCONN;
  1764. case ENOTCONN: return WSAENOTCONN;
  1765. case ESHUTDOWN: return WSAESHUTDOWN;
  1766. case ETOOMANYREFS: return WSAETOOMANYREFS;
  1767. case ETIMEDOUT: return WSAETIMEDOUT;
  1768. case ECONNREFUSED: return WSAECONNREFUSED;
  1769. case ELOOP: return WSAELOOP;
  1770. case ENAMETOOLONG: return WSAENAMETOOLONG;
  1771. case EHOSTDOWN: return WSAEHOSTDOWN;
  1772. case EHOSTUNREACH: return WSAEHOSTUNREACH;
  1773. case ENOTEMPTY: return WSAENOTEMPTY;
  1774. #ifdef EPROCLIM
  1775. case EPROCLIM: return WSAEPROCLIM;
  1776. #endif
  1777. #ifdef EUSERS
  1778. case EUSERS: return WSAEUSERS;
  1779. #endif
  1780. #ifdef EDQUOT
  1781. case EDQUOT: return WSAEDQUOT;
  1782. #endif
  1783. #ifdef ESTALE
  1784. case ESTALE: return WSAESTALE;
  1785. #endif
  1786. #ifdef EREMOTE
  1787. case EREMOTE: return WSAEREMOTE;
  1788. #endif
  1789. case 0: return 0;
  1790. default:
  1791. errno = err;
  1792. perror("wineserver: sock_get_error() can't map error");
  1793. return WSAEFAULT;
  1794. }
  1795. }
  1796. static int sock_get_ntstatus( int err )
  1797. {
  1798. switch ( err )
  1799. {
  1800. case EBADF: return STATUS_INVALID_HANDLE;
  1801. case EBUSY: return STATUS_DEVICE_BUSY;
  1802. case EPERM:
  1803. case EACCES: return STATUS_ACCESS_DENIED;
  1804. case EFAULT: return STATUS_ACCESS_VIOLATION;
  1805. case EINVAL: return STATUS_INVALID_PARAMETER;
  1806. case ENFILE:
  1807. case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
  1808. case EINPROGRESS:
  1809. case EWOULDBLOCK: return STATUS_DEVICE_NOT_READY;
  1810. case EALREADY: return STATUS_NETWORK_BUSY;
  1811. case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
  1812. case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
  1813. case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
  1814. case EPROTONOSUPPORT:
  1815. case ESOCKTNOSUPPORT:
  1816. case EPFNOSUPPORT:
  1817. case EAFNOSUPPORT:
  1818. case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
  1819. case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
  1820. case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
  1821. case EADDRINUSE: return STATUS_SHARING_VIOLATION;
  1822. /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
  1823. * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
  1824. case ENODEV:
  1825. case EADDRNOTAVAIL: return STATUS_INVALID_ADDRESS_COMPONENT;
  1826. case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
  1827. case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
  1828. case ENOTCONN: return STATUS_INVALID_CONNECTION;
  1829. case ETIMEDOUT: return STATUS_IO_TIMEOUT;
  1830. case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
  1831. case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
  1832. case ENETDOWN: return STATUS_NETWORK_BUSY;
  1833. case EPIPE:
  1834. case ECONNRESET: return STATUS_CONNECTION_RESET;
  1835. case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
  1836. case EISCONN: return STATUS_CONNECTION_ACTIVE;
  1837. case 0: return STATUS_SUCCESS;
  1838. default:
  1839. errno = err;
  1840. perror("wineserver: sock_get_ntstatus() can't map error");
  1841. return STATUS_UNSUCCESSFUL;
  1842. }
  1843. }
  1844. static struct accept_req *alloc_accept_req( struct sock *sock, struct sock *acceptsock, struct async *async,
  1845. const struct afd_accept_into_params *params )
  1846. {
  1847. struct accept_req *req = mem_alloc( sizeof(*req) );
  1848. if (req)
  1849. {
  1850. req->async = (struct async *)grab_object( async );
  1851. req->iosb = async_get_iosb( async );
  1852. req->sock = (struct sock *)grab_object( sock );
  1853. req->acceptsock = acceptsock;
  1854. if (acceptsock) grab_object( acceptsock );
  1855. req->accepted = 0;
  1856. req->recv_len = 0;
  1857. req->local_len = 0;
  1858. if (params)
  1859. {
  1860. req->recv_len = params->recv_len;
  1861. req->local_len = params->local_len;
  1862. }
  1863. }
  1864. return req;
  1865. }
  1866. static void sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1867. {
  1868. struct sock *sock = get_fd_user( fd );
  1869. int unix_fd;
  1870. assert( sock->obj.ops == &sock_ops );
  1871. if (code != IOCTL_AFD_WINE_CREATE && (unix_fd = get_unix_fd( fd )) < 0) return;
  1872. switch(code)
  1873. {
  1874. case IOCTL_AFD_WINE_CREATE:
  1875. {
  1876. const struct afd_create_params *params = get_req_data();
  1877. if (get_req_data_size() != sizeof(*params))
  1878. {
  1879. set_error( STATUS_INVALID_PARAMETER );
  1880. return;
  1881. }
  1882. init_socket( sock, params->family, params->type, params->protocol, params->flags );
  1883. return;
  1884. }
  1885. case IOCTL_AFD_WINE_ACCEPT:
  1886. {
  1887. struct sock *acceptsock;
  1888. obj_handle_t handle;
  1889. if (get_reply_max_size() != sizeof(handle))
  1890. {
  1891. set_error( STATUS_BUFFER_TOO_SMALL );
  1892. return;
  1893. }
  1894. if (!(acceptsock = accept_socket( sock )))
  1895. {
  1896. struct accept_req *req;
  1897. if (sock->nonblocking) return;
  1898. if (get_error() != STATUS_DEVICE_NOT_READY) return;
  1899. if (!(req = alloc_accept_req( sock, NULL, async, NULL ))) return;
  1900. list_add_tail( &sock->accept_list, &req->entry );
  1901. async_set_completion_callback( async, free_accept_req, req );
  1902. queue_async( &sock->accept_q, async );
  1903. sock_reselect( sock );
  1904. set_error( STATUS_PENDING );
  1905. return;
  1906. }
  1907. handle = alloc_handle( current->process, &acceptsock->obj,
  1908. GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
  1909. acceptsock->wparam = handle;
  1910. sock_reselect( acceptsock );
  1911. release_object( acceptsock );
  1912. set_reply_data( &handle, sizeof(handle) );
  1913. return;
  1914. }
  1915. case IOCTL_AFD_WINE_ACCEPT_INTO:
  1916. {
  1917. static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
  1918. const struct afd_accept_into_params *params = get_req_data();
  1919. struct sock *acceptsock;
  1920. unsigned int remote_len;
  1921. struct accept_req *req;
  1922. if (get_req_data_size() != sizeof(*params) ||
  1923. get_reply_max_size() < params->recv_len ||
  1924. get_reply_max_size() - params->recv_len < params->local_len)
  1925. {
  1926. set_error( STATUS_BUFFER_TOO_SMALL );
  1927. return;
  1928. }
  1929. remote_len = get_reply_max_size() - params->recv_len - params->local_len;
  1930. if (remote_len < sizeof(int))
  1931. {
  1932. set_error( STATUS_INVALID_PARAMETER );
  1933. return;
  1934. }
  1935. if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
  1936. return;
  1937. if (acceptsock->accept_recv_req)
  1938. {
  1939. release_object( acceptsock );
  1940. set_error( STATUS_INVALID_PARAMETER );
  1941. return;
  1942. }
  1943. if (!(req = alloc_accept_req( sock, acceptsock, async, params )))
  1944. {
  1945. release_object( acceptsock );
  1946. return;
  1947. }
  1948. list_add_tail( &sock->accept_list, &req->entry );
  1949. acceptsock->accept_recv_req = req;
  1950. release_object( acceptsock );
  1951. acceptsock->wparam = params->accept_handle;
  1952. async_set_completion_callback( async, free_accept_req, req );
  1953. queue_async( &sock->accept_q, async );
  1954. sock_reselect( sock );
  1955. set_error( STATUS_PENDING );
  1956. return;
  1957. }
  1958. case IOCTL_AFD_LISTEN:
  1959. {
  1960. const struct afd_listen_params *params = get_req_data();
  1961. if (get_req_data_size() < sizeof(*params))
  1962. {
  1963. set_error( STATUS_INVALID_PARAMETER );
  1964. return;
  1965. }
  1966. if (!sock->bound)
  1967. {
  1968. set_error( STATUS_INVALID_PARAMETER );
  1969. return;
  1970. }
  1971. if (listen( unix_fd, params->backlog ) < 0)
  1972. {
  1973. set_error( sock_get_ntstatus( errno ) );
  1974. return;
  1975. }
  1976. sock->state = SOCK_LISTENING;
  1977. /* a listening socket can no longer be accepted into */
  1978. allow_fd_caching( sock->fd );
  1979. /* we may already be selecting for AFD_POLL_ACCEPT */
  1980. sock_reselect( sock );
  1981. return;
  1982. }
  1983. case IOCTL_AFD_WINE_CONNECT:
  1984. {
  1985. const struct afd_connect_params *params = get_req_data();
  1986. const struct WS_sockaddr *addr;
  1987. union unix_sockaddr unix_addr;
  1988. struct connect_req *req;
  1989. socklen_t unix_len;
  1990. int send_len, ret;
  1991. if (get_req_data_size() < sizeof(*params) ||
  1992. get_req_data_size() - sizeof(*params) < params->addr_len)
  1993. {
  1994. set_error( STATUS_BUFFER_TOO_SMALL );
  1995. return;
  1996. }
  1997. send_len = get_req_data_size() - sizeof(*params) - params->addr_len;
  1998. addr = (const struct WS_sockaddr *)(params + 1);
  1999. if (!params->synchronous && !sock->bound)
  2000. {
  2001. set_error( STATUS_INVALID_PARAMETER );
  2002. return;
  2003. }
  2004. if (sock->accept_recv_req)
  2005. {
  2006. set_error( STATUS_INVALID_PARAMETER );
  2007. return;
  2008. }
  2009. if (sock->connect_req)
  2010. {
  2011. set_error( STATUS_INVALID_PARAMETER );
  2012. return;
  2013. }
  2014. switch (sock->state)
  2015. {
  2016. case SOCK_LISTENING:
  2017. set_error( STATUS_INVALID_PARAMETER );
  2018. return;
  2019. case SOCK_CONNECTING:
  2020. /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
  2021. * but there's no status code that maps to WSAEALREADY... */
  2022. set_error( params->synchronous ? STATUS_ADDRESS_ALREADY_ASSOCIATED : STATUS_INVALID_PARAMETER );
  2023. return;
  2024. case SOCK_CONNECTED:
  2025. set_error( STATUS_CONNECTION_ACTIVE );
  2026. return;
  2027. case SOCK_UNCONNECTED:
  2028. case SOCK_CONNECTIONLESS:
  2029. break;
  2030. }
  2031. unix_len = sockaddr_to_unix( addr, params->addr_len, &unix_addr );
  2032. if (!unix_len)
  2033. {
  2034. set_error( STATUS_INVALID_ADDRESS );
  2035. return;
  2036. }
  2037. if (unix_addr.addr.sa_family == AF_INET && !memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 ))
  2038. unix_addr.in.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
  2039. ret = connect( unix_fd, &unix_addr.addr, unix_len );
  2040. if (ret < 0 && errno != EINPROGRESS)
  2041. {
  2042. set_error( sock_get_ntstatus( errno ) );
  2043. return;
  2044. }
  2045. /* a connected or connecting socket can no longer be accepted into */
  2046. allow_fd_caching( sock->fd );
  2047. unix_len = sizeof(unix_addr);
  2048. if (!getsockname( unix_fd, &unix_addr.addr, &unix_len ))
  2049. sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
  2050. sock->bound = 1;
  2051. if (!ret)
  2052. {
  2053. sock->state = SOCK_CONNECTED;
  2054. if (!send_len) return;
  2055. }
  2056. sock->state = SOCK_CONNECTING;
  2057. if (params->synchronous && sock->nonblocking)
  2058. {
  2059. sock_reselect( sock );
  2060. set_error( STATUS_DEVICE_NOT_READY );
  2061. return;
  2062. }
  2063. if (!(req = mem_alloc( sizeof(*req) )))
  2064. return;
  2065. req->async = (struct async *)grab_object( async );
  2066. req->iosb = async_get_iosb( async );
  2067. req->sock = (struct sock *)grab_object( sock );
  2068. req->addr_len = params->addr_len;
  2069. req->send_len = send_len;
  2070. req->send_cursor = 0;
  2071. async_set_completion_callback( async, free_connect_req, req );
  2072. sock->connect_req = req;
  2073. queue_async( &sock->connect_q, async );
  2074. sock_reselect( sock );
  2075. set_error( STATUS_PENDING );
  2076. return;
  2077. }
  2078. case IOCTL_AFD_WINE_SHUTDOWN:
  2079. {
  2080. unsigned int how;
  2081. if (get_req_data_size() < sizeof(int))
  2082. {
  2083. set_error( STATUS_BUFFER_TOO_SMALL );
  2084. return;
  2085. }
  2086. how = *(int *)get_req_data();
  2087. if (how > SD_BOTH)
  2088. {
  2089. set_error( STATUS_INVALID_PARAMETER );
  2090. return;
  2091. }
  2092. if (sock->state != SOCK_CONNECTED && sock->state != SOCK_CONNECTIONLESS)
  2093. {
  2094. set_error( STATUS_INVALID_CONNECTION );
  2095. return;
  2096. }
  2097. if (how != SD_SEND)
  2098. {
  2099. sock->rd_shutdown = 1;
  2100. }
  2101. if (how != SD_RECEIVE)
  2102. {
  2103. sock->wr_shutdown = 1;
  2104. if (list_empty( &sock->write_q.queue ))
  2105. shutdown( unix_fd, SHUT_WR );
  2106. else
  2107. sock->wr_shutdown_pending = 1;
  2108. }
  2109. if (how == SD_BOTH)
  2110. {
  2111. if (sock->event) release_object( sock->event );
  2112. sock->event = NULL;
  2113. sock->window = 0;
  2114. sock->mask = 0;
  2115. sock->nonblocking = 1;
  2116. }
  2117. sock_reselect( sock );
  2118. return;
  2119. }
  2120. case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE:
  2121. {
  2122. int force_async;
  2123. if (get_req_data_size() < sizeof(int))
  2124. {
  2125. set_error( STATUS_BUFFER_TOO_SMALL );
  2126. return;
  2127. }
  2128. force_async = *(int *)get_req_data();
  2129. if (sock->nonblocking && !force_async)
  2130. {
  2131. set_error( STATUS_DEVICE_NOT_READY );
  2132. return;
  2133. }
  2134. if (!sock_get_ifchange( sock )) return;
  2135. queue_async( &sock->ifchange_q, async );
  2136. set_error( STATUS_PENDING );
  2137. return;
  2138. }
  2139. case IOCTL_AFD_WINE_FIONBIO:
  2140. if (get_req_data_size() < sizeof(int))
  2141. {
  2142. set_error( STATUS_BUFFER_TOO_SMALL );
  2143. return;
  2144. }
  2145. if (*(int *)get_req_data())
  2146. {
  2147. sock->nonblocking = 1;
  2148. }
  2149. else
  2150. {
  2151. if (sock->mask)
  2152. {
  2153. set_error( STATUS_INVALID_PARAMETER );
  2154. return;
  2155. }
  2156. sock->nonblocking = 0;
  2157. }
  2158. return;
  2159. case IOCTL_AFD_GET_EVENTS:
  2160. {
  2161. struct afd_get_events_params params = {0};
  2162. unsigned int i;
  2163. if (get_reply_max_size() < sizeof(params))
  2164. {
  2165. set_error( STATUS_INVALID_PARAMETER );
  2166. return;
  2167. }
  2168. params.flags = sock->pending_events & sock->mask;
  2169. for (i = 0; i < ARRAY_SIZE( params.status ); ++i)
  2170. params.status[i] = sock_get_ntstatus( sock->errors[i] );
  2171. sock->pending_events = 0;
  2172. sock_reselect( sock );
  2173. set_reply_data( &params, sizeof(params) );
  2174. return;
  2175. }
  2176. case IOCTL_AFD_EVENT_SELECT:
  2177. {
  2178. struct event *event = NULL;
  2179. obj_handle_t event_handle;
  2180. int mask;
  2181. set_async_pending( async );
  2182. if (is_machine_64bit( current->process->machine ))
  2183. {
  2184. const struct afd_event_select_params_64 *params = get_req_data();
  2185. if (get_req_data_size() < sizeof(*params))
  2186. {
  2187. set_error( STATUS_INVALID_PARAMETER );
  2188. return;
  2189. }
  2190. event_handle = params->event;
  2191. mask = params->mask;
  2192. }
  2193. else
  2194. {
  2195. const struct afd_event_select_params_32 *params = get_req_data();
  2196. if (get_req_data_size() < sizeof(*params))
  2197. {
  2198. set_error( STATUS_INVALID_PARAMETER );
  2199. return;
  2200. }
  2201. event_handle = params->event;
  2202. mask = params->mask;
  2203. }
  2204. if ((event_handle || mask) &&
  2205. !(event = get_event_obj( current->process, event_handle, EVENT_MODIFY_STATE )))
  2206. {
  2207. set_error( STATUS_INVALID_PARAMETER );
  2208. return;
  2209. }
  2210. if (sock->event) release_object( sock->event );
  2211. sock->event = event;
  2212. sock->mask = mask;
  2213. sock->window = 0;
  2214. sock->message = 0;
  2215. sock->wparam = 0;
  2216. sock->nonblocking = 1;
  2217. sock_reselect( sock );
  2218. /* Explicitly wake the socket up if the mask causes it to become
  2219. * signaled. Note that reselecting isn't enough, since we might already
  2220. * have had events recorded in sock->reported_events and we don't want
  2221. * to select for them again. */
  2222. sock_wake_up( sock );
  2223. return;
  2224. }
  2225. case IOCTL_AFD_WINE_MESSAGE_SELECT:
  2226. {
  2227. const struct afd_message_select_params *params = get_req_data();
  2228. if (get_req_data_size() < sizeof(params))
  2229. {
  2230. set_error( STATUS_BUFFER_TOO_SMALL );
  2231. return;
  2232. }
  2233. if (sock->event) release_object( sock->event );
  2234. if (params->window)
  2235. {
  2236. sock->pending_events = 0;
  2237. sock->reported_events = 0;
  2238. }
  2239. sock->event = NULL;
  2240. sock->mask = params->mask;
  2241. sock->window = params->window;
  2242. sock->message = params->message;
  2243. sock->wparam = params->handle;
  2244. sock->nonblocking = 1;
  2245. sock_reselect( sock );
  2246. return;
  2247. }
  2248. case IOCTL_AFD_BIND:
  2249. {
  2250. const struct afd_bind_params *params = get_req_data();
  2251. union unix_sockaddr unix_addr, bind_addr;
  2252. data_size_t in_size;
  2253. socklen_t unix_len;
  2254. /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
  2255. * input */
  2256. if (get_req_data_size() < get_reply_max_size())
  2257. {
  2258. set_error( STATUS_BUFFER_TOO_SMALL );
  2259. return;
  2260. }
  2261. in_size = get_req_data_size() - get_reply_max_size();
  2262. if (in_size < offsetof(struct afd_bind_params, addr.sa_data)
  2263. || get_reply_max_size() < in_size - sizeof(int))
  2264. {
  2265. set_error( STATUS_INVALID_PARAMETER );
  2266. return;
  2267. }
  2268. if (sock->bound)
  2269. {
  2270. set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED );
  2271. return;
  2272. }
  2273. unix_len = sockaddr_to_unix( &params->addr, in_size - sizeof(int), &unix_addr );
  2274. if (!unix_len)
  2275. {
  2276. set_error( STATUS_INVALID_ADDRESS );
  2277. return;
  2278. }
  2279. bind_addr = unix_addr;
  2280. if (unix_addr.addr.sa_family == AF_INET)
  2281. {
  2282. if (!memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 )
  2283. || bind_to_interface( sock, &unix_addr.in ))
  2284. bind_addr.in.sin_addr.s_addr = htonl( INADDR_ANY );
  2285. }
  2286. else if (unix_addr.addr.sa_family == AF_INET6)
  2287. {
  2288. #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
  2289. /* Windows allows specifying zero to use the default scope. Linux
  2290. * interprets it as an interface index and requires that it be
  2291. * nonzero. */
  2292. if (!unix_addr.in6.sin6_scope_id)
  2293. bind_addr.in6.sin6_scope_id = get_ipv6_interface_index( &unix_addr.in6.sin6_addr );
  2294. #endif
  2295. }
  2296. set_async_pending( async );
  2297. if (bind( unix_fd, &bind_addr.addr, unix_len ) < 0)
  2298. {
  2299. if (errno == EADDRINUSE)
  2300. {
  2301. int reuse;
  2302. socklen_t len = sizeof(reuse);
  2303. if (!getsockopt( unix_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, &len ) && reuse)
  2304. errno = EACCES;
  2305. }
  2306. set_error( sock_get_ntstatus( errno ) );
  2307. return;
  2308. }
  2309. sock->bound = 1;
  2310. unix_len = sizeof(bind_addr);
  2311. if (!getsockname( unix_fd, &bind_addr.addr, &unix_len ))
  2312. {
  2313. /* store the interface or magic loopback address instead of the
  2314. * actual unix address */
  2315. if (bind_addr.addr.sa_family == AF_INET)
  2316. bind_addr.in.sin_addr = unix_addr.in.sin_addr;
  2317. sock->addr_len = sockaddr_from_unix( &bind_addr, &sock->addr.addr, sizeof(sock->addr) );
  2318. }
  2319. if (get_reply_max_size() >= sock->addr_len)
  2320. set_reply_data( &sock->addr, sock->addr_len );
  2321. return;
  2322. }
  2323. case IOCTL_AFD_GETSOCKNAME:
  2324. if (!sock->bound)
  2325. {
  2326. set_error( STATUS_INVALID_PARAMETER );
  2327. return;
  2328. }
  2329. if (get_reply_max_size() < sock->addr_len)
  2330. {
  2331. set_error( STATUS_BUFFER_TOO_SMALL );
  2332. return;
  2333. }
  2334. set_reply_data( &sock->addr, sock->addr_len );
  2335. return;
  2336. case IOCTL_AFD_WINE_DEFER:
  2337. {
  2338. const obj_handle_t *handle = get_req_data();
  2339. struct sock *acceptsock;
  2340. if (get_req_data_size() < sizeof(*handle))
  2341. {
  2342. set_error( STATUS_BUFFER_TOO_SMALL );
  2343. return;
  2344. }
  2345. acceptsock = (struct sock *)get_handle_obj( current->process, *handle, 0, &sock_ops );
  2346. if (!acceptsock) return;
  2347. sock->deferred = acceptsock;
  2348. return;
  2349. }
  2350. case IOCTL_AFD_WINE_GET_INFO:
  2351. {
  2352. struct afd_get_info_params params;
  2353. if (get_reply_max_size() < sizeof(params))
  2354. {
  2355. set_error( STATUS_BUFFER_TOO_SMALL );
  2356. return;
  2357. }
  2358. params.family = sock->family;
  2359. params.type = sock->type;
  2360. params.protocol = sock->proto;
  2361. set_reply_data( &params, sizeof(params) );
  2362. return;
  2363. }
  2364. case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN:
  2365. {
  2366. int listening = (sock->state == SOCK_LISTENING);
  2367. if (get_reply_max_size() < sizeof(listening))
  2368. {
  2369. set_error( STATUS_BUFFER_TOO_SMALL );
  2370. return;
  2371. }
  2372. set_reply_data( &listening, sizeof(listening) );
  2373. return;
  2374. }
  2375. case IOCTL_AFD_WINE_GET_SO_ERROR:
  2376. {
  2377. int error;
  2378. socklen_t len = sizeof(error);
  2379. unsigned int i;
  2380. if (get_reply_max_size() < sizeof(error))
  2381. {
  2382. set_error( STATUS_BUFFER_TOO_SMALL );
  2383. return;
  2384. }
  2385. if (getsockopt( unix_fd, SOL_SOCKET, SO_ERROR, (char *)&error, &len ) < 0)
  2386. {
  2387. set_error( sock_get_ntstatus( errno ) );
  2388. return;
  2389. }
  2390. if (!error)
  2391. {
  2392. for (i = 0; i < ARRAY_SIZE( sock->errors ); ++i)
  2393. {
  2394. if (sock->errors[i])
  2395. {
  2396. error = sock_get_error( sock->errors[i] );
  2397. break;
  2398. }
  2399. }
  2400. }
  2401. set_reply_data( &error, sizeof(error) );
  2402. return;
  2403. }
  2404. case IOCTL_AFD_WINE_GET_SO_RCVBUF:
  2405. {
  2406. int rcvbuf = sock->rcvbuf;
  2407. if (get_reply_max_size() < sizeof(rcvbuf))
  2408. {
  2409. set_error( STATUS_BUFFER_TOO_SMALL );
  2410. return;
  2411. }
  2412. set_reply_data( &rcvbuf, sizeof(rcvbuf) );
  2413. return;
  2414. }
  2415. case IOCTL_AFD_WINE_SET_SO_RCVBUF:
  2416. {
  2417. DWORD rcvbuf;
  2418. if (get_req_data_size() < sizeof(rcvbuf))
  2419. {
  2420. set_error( STATUS_BUFFER_TOO_SMALL );
  2421. return;
  2422. }
  2423. rcvbuf = *(DWORD *)get_req_data();
  2424. if (!setsockopt( unix_fd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbuf, sizeof(rcvbuf) ))
  2425. sock->rcvbuf = rcvbuf;
  2426. else
  2427. set_error( sock_get_ntstatus( errno ) );
  2428. return;
  2429. }
  2430. case IOCTL_AFD_WINE_GET_SO_RCVTIMEO:
  2431. {
  2432. DWORD rcvtimeo = sock->rcvtimeo;
  2433. if (get_reply_max_size() < sizeof(rcvtimeo))
  2434. {
  2435. set_error( STATUS_BUFFER_TOO_SMALL );
  2436. return;
  2437. }
  2438. set_reply_data( &rcvtimeo, sizeof(rcvtimeo) );
  2439. return;
  2440. }
  2441. case IOCTL_AFD_WINE_SET_SO_RCVTIMEO:
  2442. {
  2443. DWORD rcvtimeo;
  2444. if (get_req_data_size() < sizeof(rcvtimeo))
  2445. {
  2446. set_error( STATUS_BUFFER_TOO_SMALL );
  2447. return;
  2448. }
  2449. rcvtimeo = *(DWORD *)get_req_data();
  2450. sock->rcvtimeo = rcvtimeo;
  2451. return;
  2452. }
  2453. case IOCTL_AFD_WINE_GET_SO_SNDBUF:
  2454. {
  2455. int sndbuf = sock->sndbuf;
  2456. if (get_reply_max_size() < sizeof(sndbuf))
  2457. {
  2458. set_error( STATUS_BUFFER_TOO_SMALL );
  2459. return;
  2460. }
  2461. set_reply_data( &sndbuf, sizeof(sndbuf) );
  2462. return;
  2463. }
  2464. case IOCTL_AFD_WINE_SET_SO_SNDBUF:
  2465. {
  2466. DWORD sndbuf;
  2467. if (get_req_data_size() < sizeof(sndbuf))
  2468. {
  2469. set_error( STATUS_BUFFER_TOO_SMALL );
  2470. return;
  2471. }
  2472. sndbuf = *(DWORD *)get_req_data();
  2473. #ifdef __APPLE__
  2474. if (!sndbuf)
  2475. {
  2476. /* setsockopt fails if a zero value is passed */
  2477. sock->sndbuf = sndbuf;
  2478. return;
  2479. }
  2480. #endif
  2481. if (!setsockopt( unix_fd, SOL_SOCKET, SO_SNDBUF, (char *)&sndbuf, sizeof(sndbuf) ))
  2482. sock->sndbuf = sndbuf;
  2483. else
  2484. set_error( sock_get_ntstatus( errno ) );
  2485. return;
  2486. }
  2487. case IOCTL_AFD_WINE_GET_SO_SNDTIMEO:
  2488. {
  2489. DWORD sndtimeo = sock->sndtimeo;
  2490. if (get_reply_max_size() < sizeof(sndtimeo))
  2491. {
  2492. set_error( STATUS_BUFFER_TOO_SMALL );
  2493. return;
  2494. }
  2495. set_reply_data( &sndtimeo, sizeof(sndtimeo) );
  2496. return;
  2497. }
  2498. case IOCTL_AFD_WINE_SET_SO_SNDTIMEO:
  2499. {
  2500. DWORD sndtimeo;
  2501. if (get_req_data_size() < sizeof(sndtimeo))
  2502. {
  2503. set_error( STATUS_BUFFER_TOO_SMALL );
  2504. return;
  2505. }
  2506. sndtimeo = *(DWORD *)get_req_data();
  2507. sock->sndtimeo = sndtimeo;
  2508. return;
  2509. }
  2510. case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME:
  2511. {
  2512. DWORD time = ~0u;
  2513. if (get_reply_max_size() < sizeof(time))
  2514. {
  2515. set_error( STATUS_BUFFER_TOO_SMALL );
  2516. return;
  2517. }
  2518. if (sock->state == SOCK_CONNECTED)
  2519. time = (current_time - sock->connect_time) / 10000000;
  2520. set_reply_data( &time, sizeof(time) );
  2521. return;
  2522. }
  2523. case IOCTL_AFD_POLL:
  2524. {
  2525. if (get_reply_max_size() < get_req_data_size())
  2526. {
  2527. set_error( STATUS_INVALID_PARAMETER );
  2528. return;
  2529. }
  2530. if (is_machine_64bit( current->process->machine ))
  2531. {
  2532. const struct afd_poll_params_64 *params = get_req_data();
  2533. if (get_req_data_size() < sizeof(struct afd_poll_params_64) ||
  2534. get_req_data_size() < offsetof( struct afd_poll_params_64, sockets[params->count] ))
  2535. {
  2536. set_error( STATUS_INVALID_PARAMETER );
  2537. return;
  2538. }
  2539. poll_socket( sock, async, params->exclusive, params->timeout, params->count, params->sockets );
  2540. }
  2541. else
  2542. {
  2543. const struct afd_poll_params_32 *params = get_req_data();
  2544. struct afd_poll_socket_64 *sockets;
  2545. unsigned int i;
  2546. if (get_req_data_size() < sizeof(struct afd_poll_params_32) ||
  2547. get_req_data_size() < offsetof( struct afd_poll_params_32, sockets[params->count] ))
  2548. {
  2549. set_error( STATUS_INVALID_PARAMETER );
  2550. return;
  2551. }
  2552. if (!(sockets = mem_alloc( params->count * sizeof(*sockets) ))) return;
  2553. for (i = 0; i < params->count; ++i)
  2554. {
  2555. sockets[i].socket = params->sockets[i].socket;
  2556. sockets[i].flags = params->sockets[i].flags;
  2557. sockets[i].status = params->sockets[i].status;
  2558. }
  2559. poll_socket( sock, async, params->exclusive, params->timeout, params->count, sockets );
  2560. free( sockets );
  2561. }
  2562. return;
  2563. }
  2564. default:
  2565. set_error( STATUS_NOT_SUPPORTED );
  2566. return;
  2567. }
  2568. }
  2569. static int poll_single_socket( struct sock *sock, int mask )
  2570. {
  2571. struct pollfd pollfd;
  2572. pollfd.fd = get_unix_fd( sock->fd );
  2573. pollfd.events = poll_flags_from_afd( sock, mask );
  2574. if (pollfd.events < 0 || poll( &pollfd, 1, 0 ) < 0)
  2575. return 0;
  2576. if (sock->state == SOCK_CONNECTING && (pollfd.revents & (POLLERR | POLLHUP)))
  2577. pollfd.revents &= ~POLLOUT;
  2578. if ((mask & AFD_POLL_HUP) && (pollfd.revents & POLLIN) && sock->type == WS_SOCK_STREAM)
  2579. {
  2580. char dummy;
  2581. if (!recv( get_unix_fd( sock->fd ), &dummy, 1, MSG_PEEK ))
  2582. {
  2583. pollfd.revents &= ~POLLIN;
  2584. pollfd.revents |= POLLHUP;
  2585. }
  2586. }
  2587. return get_poll_flags( sock, pollfd.revents ) & mask;
  2588. }
  2589. static void handle_exclusive_poll(struct poll_req *req)
  2590. {
  2591. unsigned int i;
  2592. for (i = 0; i < req->count; ++i)
  2593. {
  2594. struct sock *sock = req->sockets[i].sock;
  2595. struct poll_req *main_poll = sock->main_poll;
  2596. if (main_poll && main_poll->exclusive && req->exclusive)
  2597. {
  2598. complete_async_poll( main_poll, STATUS_SUCCESS );
  2599. main_poll = NULL;
  2600. }
  2601. if (!main_poll)
  2602. sock->main_poll = req;
  2603. }
  2604. }
  2605. static void poll_socket( struct sock *poll_sock, struct async *async, int exclusive, timeout_t timeout,
  2606. unsigned int count, const struct afd_poll_socket_64 *sockets )
  2607. {
  2608. BOOL signaled = FALSE;
  2609. struct poll_req *req;
  2610. unsigned int i, j;
  2611. if (!count)
  2612. {
  2613. set_error( STATUS_INVALID_PARAMETER );
  2614. return;
  2615. }
  2616. if (!(req = mem_alloc( offsetof( struct poll_req, sockets[count] ) )))
  2617. return;
  2618. req->timeout = NULL;
  2619. if (timeout && timeout != TIMEOUT_INFINITE &&
  2620. !(req->timeout = add_timeout_user( timeout, async_poll_timeout, req )))
  2621. {
  2622. free( req );
  2623. return;
  2624. }
  2625. req->orig_timeout = timeout;
  2626. for (i = 0; i < count; ++i)
  2627. {
  2628. req->sockets[i].sock = (struct sock *)get_handle_obj( current->process, sockets[i].socket, 0, &sock_ops );
  2629. if (!req->sockets[i].sock)
  2630. {
  2631. for (j = 0; j < i; ++j) release_object( req->sockets[j].sock );
  2632. if (req->timeout) remove_timeout_user( req->timeout );
  2633. free( req );
  2634. return;
  2635. }
  2636. req->sockets[i].handle = sockets[i].socket;
  2637. req->sockets[i].mask = sockets[i].flags;
  2638. req->sockets[i].flags = 0;
  2639. }
  2640. req->exclusive = exclusive;
  2641. req->count = count;
  2642. req->async = (struct async *)grab_object( async );
  2643. req->iosb = async_get_iosb( async );
  2644. handle_exclusive_poll(req);
  2645. list_add_tail( &poll_list, &req->entry );
  2646. async_set_completion_callback( async, free_poll_req, req );
  2647. queue_async( &poll_sock->poll_q, async );
  2648. for (i = 0; i < count; ++i)
  2649. {
  2650. struct sock *sock = req->sockets[i].sock;
  2651. int mask = req->sockets[i].mask;
  2652. int flags = poll_single_socket( sock, mask );
  2653. if (flags)
  2654. {
  2655. signaled = TRUE;
  2656. req->sockets[i].flags = flags;
  2657. req->sockets[i].status = sock_get_ntstatus( sock_error( sock->fd ) );
  2658. }
  2659. /* FIXME: do other error conditions deserve a similar treatment? */
  2660. if (sock->state != SOCK_CONNECTING && sock->errors[AFD_POLL_BIT_CONNECT_ERR] && (mask & AFD_POLL_CONNECT_ERR))
  2661. {
  2662. signaled = TRUE;
  2663. req->sockets[i].flags |= AFD_POLL_CONNECT_ERR;
  2664. req->sockets[i].status = sock_get_ntstatus( sock->errors[AFD_POLL_BIT_CONNECT_ERR] );
  2665. }
  2666. }
  2667. if (!timeout || signaled)
  2668. complete_async_poll( req, STATUS_SUCCESS );
  2669. for (i = 0; i < req->count; ++i)
  2670. sock_reselect( req->sockets[i].sock );
  2671. set_error( STATUS_PENDING );
  2672. }
  2673. #ifdef HAVE_LINUX_RTNETLINK_H
  2674. /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
  2675. static struct object *ifchange_object;
  2676. static void ifchange_dump( struct object *obj, int verbose );
  2677. static struct fd *ifchange_get_fd( struct object *obj );
  2678. static void ifchange_destroy( struct object *obj );
  2679. static int ifchange_get_poll_events( struct fd *fd );
  2680. static void ifchange_poll_event( struct fd *fd, int event );
  2681. struct ifchange
  2682. {
  2683. struct object obj; /* object header */
  2684. struct fd *fd; /* interface change file descriptor */
  2685. struct list sockets; /* list of sockets to send interface change notifications */
  2686. };
  2687. static const struct object_ops ifchange_ops =
  2688. {
  2689. sizeof(struct ifchange), /* size */
  2690. &no_type, /* type */
  2691. ifchange_dump, /* dump */
  2692. no_add_queue, /* add_queue */
  2693. NULL, /* remove_queue */
  2694. NULL, /* signaled */
  2695. no_satisfied, /* satisfied */
  2696. no_signal, /* signal */
  2697. ifchange_get_fd, /* get_fd */
  2698. default_map_access, /* map_access */
  2699. default_get_sd, /* get_sd */
  2700. default_set_sd, /* set_sd */
  2701. no_get_full_name, /* get_full_name */
  2702. no_lookup_name, /* lookup_name */
  2703. no_link_name, /* link_name */
  2704. NULL, /* unlink_name */
  2705. no_open_file, /* open_file */
  2706. no_kernel_obj_list, /* get_kernel_obj_list */
  2707. no_close_handle, /* close_handle */
  2708. ifchange_destroy /* destroy */
  2709. };
  2710. static const struct fd_ops ifchange_fd_ops =
  2711. {
  2712. ifchange_get_poll_events, /* get_poll_events */
  2713. ifchange_poll_event, /* poll_event */
  2714. NULL, /* get_fd_type */
  2715. no_fd_read, /* read */
  2716. no_fd_write, /* write */
  2717. no_fd_flush, /* flush */
  2718. no_fd_get_file_info, /* get_file_info */
  2719. no_fd_get_volume_info, /* get_volume_info */
  2720. no_fd_ioctl, /* ioctl */
  2721. NULL, /* cancel_async */
  2722. NULL, /* queue_async */
  2723. NULL /* reselect_async */
  2724. };
  2725. static void ifchange_dump( struct object *obj, int verbose )
  2726. {
  2727. assert( obj->ops == &ifchange_ops );
  2728. fprintf( stderr, "Interface change\n" );
  2729. }
  2730. static struct fd *ifchange_get_fd( struct object *obj )
  2731. {
  2732. struct ifchange *ifchange = (struct ifchange *)obj;
  2733. return (struct fd *)grab_object( ifchange->fd );
  2734. }
  2735. static void ifchange_destroy( struct object *obj )
  2736. {
  2737. struct ifchange *ifchange = (struct ifchange *)obj;
  2738. assert( obj->ops == &ifchange_ops );
  2739. release_object( ifchange->fd );
  2740. /* reset the global ifchange object so that it will be recreated if it is needed again */
  2741. assert( obj == ifchange_object );
  2742. ifchange_object = NULL;
  2743. }
  2744. static int ifchange_get_poll_events( struct fd *fd )
  2745. {
  2746. return POLLIN;
  2747. }
  2748. /* wake up all the sockets waiting for a change notification event */
  2749. static void ifchange_wake_up( struct object *obj, unsigned int status )
  2750. {
  2751. struct ifchange *ifchange = (struct ifchange *)obj;
  2752. struct list *ptr, *next;
  2753. assert( obj->ops == &ifchange_ops );
  2754. assert( obj == ifchange_object );
  2755. LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
  2756. {
  2757. struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
  2758. assert( sock->ifchange_obj );
  2759. async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
  2760. sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
  2761. }
  2762. }
  2763. static void ifchange_poll_event( struct fd *fd, int event )
  2764. {
  2765. struct object *ifchange = get_fd_user( fd );
  2766. unsigned int status = STATUS_PENDING;
  2767. char buffer[PIPE_BUF];
  2768. int r;
  2769. r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
  2770. if (r < 0)
  2771. {
  2772. if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
  2773. return; /* retry when poll() says the socket is ready */
  2774. status = sock_get_ntstatus( errno );
  2775. }
  2776. else if (r > 0)
  2777. {
  2778. struct nlmsghdr *nlh;
  2779. for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
  2780. {
  2781. if (nlh->nlmsg_type == NLMSG_DONE)
  2782. break;
  2783. if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
  2784. status = STATUS_SUCCESS;
  2785. }
  2786. }
  2787. else status = STATUS_CANCELLED;
  2788. if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
  2789. }
  2790. #endif
  2791. /* we only need one of these interface notification objects, all of the sockets dependent upon
  2792. * it will wake up when a notification event occurs */
  2793. static struct object *get_ifchange( void )
  2794. {
  2795. #ifdef HAVE_LINUX_RTNETLINK_H
  2796. struct ifchange *ifchange;
  2797. struct sockaddr_nl addr;
  2798. int unix_fd;
  2799. if (ifchange_object)
  2800. {
  2801. /* increment the refcount for each socket that uses the ifchange object */
  2802. return grab_object( ifchange_object );
  2803. }
  2804. /* create the socket we need for processing interface change notifications */
  2805. unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
  2806. if (unix_fd == -1)
  2807. {
  2808. set_error( sock_get_ntstatus( errno ));
  2809. return NULL;
  2810. }
  2811. fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
  2812. memset( &addr, 0, sizeof(addr) );
  2813. addr.nl_family = AF_NETLINK;
  2814. addr.nl_groups = RTMGRP_IPV4_IFADDR;
  2815. /* bind the socket to the special netlink kernel interface */
  2816. if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
  2817. {
  2818. close( unix_fd );
  2819. set_error( sock_get_ntstatus( errno ));
  2820. return NULL;
  2821. }
  2822. if (!(ifchange = alloc_object( &ifchange_ops )))
  2823. {
  2824. close( unix_fd );
  2825. set_error( STATUS_NO_MEMORY );
  2826. return NULL;
  2827. }
  2828. list_init( &ifchange->sockets );
  2829. if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
  2830. {
  2831. release_object( ifchange );
  2832. set_error( STATUS_NO_MEMORY );
  2833. return NULL;
  2834. }
  2835. set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
  2836. /* the ifchange object is now successfully configured */
  2837. ifchange_object = &ifchange->obj;
  2838. return &ifchange->obj;
  2839. #else
  2840. set_error( STATUS_NOT_SUPPORTED );
  2841. return NULL;
  2842. #endif
  2843. }
  2844. /* add the socket to the interface change notification list */
  2845. static void ifchange_add_sock( struct object *obj, struct sock *sock )
  2846. {
  2847. #ifdef HAVE_LINUX_RTNETLINK_H
  2848. struct ifchange *ifchange = (struct ifchange *)obj;
  2849. list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
  2850. #endif
  2851. }
  2852. /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
  2853. static struct object *sock_get_ifchange( struct sock *sock )
  2854. {
  2855. struct object *ifchange;
  2856. if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
  2857. return sock->ifchange_obj;
  2858. if (!(ifchange = get_ifchange()))
  2859. return NULL;
  2860. /* add the socket to the ifchange notification list */
  2861. ifchange_add_sock( ifchange, sock );
  2862. sock->ifchange_obj = ifchange;
  2863. return ifchange;
  2864. }
  2865. /* destroy an existing ifchange queue for a specific socket */
  2866. static void sock_release_ifchange( struct sock *sock )
  2867. {
  2868. if (sock->ifchange_obj)
  2869. {
  2870. list_remove( &sock->ifchange_entry );
  2871. release_object( sock->ifchange_obj );
  2872. sock->ifchange_obj = NULL;
  2873. }
  2874. }
  2875. static void socket_device_dump( struct object *obj, int verbose );
  2876. static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
  2877. unsigned int attr, struct object *root );
  2878. static struct object *socket_device_open_file( struct object *obj, unsigned int access,
  2879. unsigned int sharing, unsigned int options );
  2880. static const struct object_ops socket_device_ops =
  2881. {
  2882. sizeof(struct object), /* size */
  2883. &device_type, /* type */
  2884. socket_device_dump, /* dump */
  2885. no_add_queue, /* add_queue */
  2886. NULL, /* remove_queue */
  2887. NULL, /* signaled */
  2888. no_satisfied, /* satisfied */
  2889. no_signal, /* signal */
  2890. no_get_fd, /* get_fd */
  2891. default_map_access, /* map_access */
  2892. default_get_sd, /* get_sd */
  2893. default_set_sd, /* set_sd */
  2894. default_get_full_name, /* get_full_name */
  2895. socket_device_lookup_name, /* lookup_name */
  2896. directory_link_name, /* link_name */
  2897. default_unlink_name, /* unlink_name */
  2898. socket_device_open_file, /* open_file */
  2899. no_kernel_obj_list, /* get_kernel_obj_list */
  2900. no_close_handle, /* close_handle */
  2901. no_destroy /* destroy */
  2902. };
  2903. static void socket_device_dump( struct object *obj, int verbose )
  2904. {
  2905. fputs( "Socket device\n", stderr );
  2906. }
  2907. static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
  2908. unsigned int attr, struct object *root )
  2909. {
  2910. if (name) name->len = 0;
  2911. return NULL;
  2912. }
  2913. static struct object *socket_device_open_file( struct object *obj, unsigned int access,
  2914. unsigned int sharing, unsigned int options )
  2915. {
  2916. struct sock *sock;
  2917. if (!(sock = create_socket())) return NULL;
  2918. if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
  2919. {
  2920. release_object( sock );
  2921. return NULL;
  2922. }
  2923. return &sock->obj;
  2924. }
  2925. struct object *create_socket_device( struct object *root, const struct unicode_str *name,
  2926. unsigned int attr, const struct security_descriptor *sd )
  2927. {
  2928. return create_named_object( root, &socket_device_ops, name, attr, sd );
  2929. }
  2930. DECL_HANDLER(recv_socket)
  2931. {
  2932. struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
  2933. unsigned int status = STATUS_PENDING;
  2934. timeout_t timeout = 0;
  2935. struct async *async;
  2936. struct fd *fd;
  2937. if (!sock) return;
  2938. fd = sock->fd;
  2939. if (!req->force_async && !sock->nonblocking && is_fd_overlapped( fd ))
  2940. timeout = (timeout_t)sock->rcvtimeo * -10000;
  2941. if (sock->rd_shutdown) status = STATUS_PIPE_DISCONNECTED;
  2942. else if (!async_queued( &sock->read_q ))
  2943. {
  2944. /* If read_q is not empty, we cannot really tell if the already queued
  2945. * asyncs will not consume all available data; if there's no data
  2946. * available, the current request won't be immediately satiable.
  2947. */
  2948. struct pollfd pollfd;
  2949. pollfd.fd = get_unix_fd( sock->fd );
  2950. pollfd.events = req->oob ? POLLPRI : POLLIN;
  2951. pollfd.revents = 0;
  2952. if (poll(&pollfd, 1, 0) >= 0 && pollfd.revents)
  2953. {
  2954. /* Give the client opportunity to complete synchronously.
  2955. * If it turns out that the I/O request is not actually immediately satiable,
  2956. * the client may then choose to re-queue the async (with STATUS_PENDING). */
  2957. status = STATUS_ALERTED;
  2958. }
  2959. }
  2960. if (status == STATUS_PENDING && !req->force_async && sock->nonblocking)
  2961. status = STATUS_DEVICE_NOT_READY;
  2962. sock->pending_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
  2963. sock->reported_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
  2964. if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
  2965. {
  2966. set_error( status );
  2967. if (timeout)
  2968. async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
  2969. if (status == STATUS_PENDING || status == STATUS_ALERTED)
  2970. queue_async( &sock->read_q, async );
  2971. /* always reselect; we changed reported_events above */
  2972. sock_reselect( sock );
  2973. reply->wait = async_handoff( async, NULL, 0 );
  2974. reply->options = get_fd_options( fd );
  2975. reply->nonblocking = sock->nonblocking;
  2976. release_object( async );
  2977. }
  2978. release_object( sock );
  2979. }
  2980. static void send_socket_completion_callback( void *private )
  2981. {
  2982. struct send_req *send_req = private;
  2983. struct iosb *iosb = send_req->iosb;
  2984. struct sock *sock = send_req->sock;
  2985. if (iosb->status != STATUS_SUCCESS)
  2986. {
  2987. /* send() calls only clear and reselect events if unsuccessful. */
  2988. sock->pending_events &= ~AFD_POLL_WRITE;
  2989. sock->reported_events &= ~AFD_POLL_WRITE;
  2990. sock_reselect( sock );
  2991. }
  2992. release_object( iosb );
  2993. release_object( sock );
  2994. free( send_req );
  2995. }
  2996. DECL_HANDLER(send_socket)
  2997. {
  2998. struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
  2999. unsigned int status = STATUS_PENDING;
  3000. timeout_t timeout = 0;
  3001. struct async *async;
  3002. struct fd *fd;
  3003. int bind_errno = 0;
  3004. if (!sock) return;
  3005. fd = sock->fd;
  3006. if (sock->type == WS_SOCK_DGRAM && !sock->bound)
  3007. {
  3008. union unix_sockaddr unix_addr;
  3009. socklen_t unix_len;
  3010. int unix_fd = get_unix_fd( fd );
  3011. unix_len = get_unix_sockaddr_any( &unix_addr, sock->family );
  3012. if (bind( unix_fd, &unix_addr.addr, unix_len ) < 0)
  3013. bind_errno = errno;
  3014. if (getsockname( unix_fd, &unix_addr.addr, &unix_len ) >= 0)
  3015. {
  3016. sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
  3017. sock->bound = 1;
  3018. }
  3019. else if (!bind_errno) bind_errno = errno;
  3020. }
  3021. if (!req->force_async && !sock->nonblocking && is_fd_overlapped( fd ))
  3022. timeout = (timeout_t)sock->sndtimeo * -10000;
  3023. if (bind_errno) status = sock_get_ntstatus( bind_errno );
  3024. else if (sock->wr_shutdown) status = STATUS_PIPE_DISCONNECTED;
  3025. else if (!async_queued( &sock->write_q ))
  3026. {
  3027. /* If write_q is not empty, we cannot really tell if the already queued
  3028. * asyncs will not consume all available space; if there's no space
  3029. * available, the current request won't be immediately satiable.
  3030. */
  3031. struct pollfd pollfd;
  3032. pollfd.fd = get_unix_fd( sock->fd );
  3033. pollfd.events = POLLOUT;
  3034. pollfd.revents = 0;
  3035. if (poll(&pollfd, 1, 0) >= 0 && pollfd.revents)
  3036. {
  3037. /* Give the client opportunity to complete synchronously.
  3038. * If it turns out that the I/O request is not actually immediately satiable,
  3039. * the client may then choose to re-queue the async (with STATUS_PENDING). */
  3040. status = STATUS_ALERTED;
  3041. }
  3042. }
  3043. if (status == STATUS_PENDING && !req->force_async && sock->nonblocking)
  3044. status = STATUS_DEVICE_NOT_READY;
  3045. if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
  3046. {
  3047. struct send_req *send_req;
  3048. struct iosb *iosb = async_get_iosb( async );
  3049. if ((send_req = mem_alloc( sizeof(*send_req) )))
  3050. {
  3051. send_req->iosb = (struct iosb *)grab_object( iosb );
  3052. send_req->sock = (struct sock *)grab_object( sock );
  3053. async_set_completion_callback( async, send_socket_completion_callback, send_req );
  3054. }
  3055. else if (status == STATUS_PENDING || status == STATUS_DEVICE_NOT_READY)
  3056. status = STATUS_NO_MEMORY;
  3057. release_object( iosb );
  3058. set_error( status );
  3059. if (timeout)
  3060. async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
  3061. if (status == STATUS_PENDING || status == STATUS_ALERTED)
  3062. {
  3063. queue_async( &sock->write_q, async );
  3064. sock_reselect( sock );
  3065. }
  3066. reply->wait = async_handoff( async, NULL, 0 );
  3067. reply->options = get_fd_options( fd );
  3068. reply->nonblocking = sock->nonblocking;
  3069. release_object( async );
  3070. }
  3071. release_object( sock );
  3072. }