sock.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  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_NETINET_IN_H
  32. # include <netinet/in.h>
  33. #endif
  34. #ifdef HAVE_POLL_H
  35. # include <poll.h>
  36. #endif
  37. #include <sys/time.h>
  38. #include <sys/types.h>
  39. #ifdef HAVE_SYS_SOCKET_H
  40. # include <sys/socket.h>
  41. #endif
  42. #ifdef HAVE_SYS_IOCTL_H
  43. #include <sys/ioctl.h>
  44. #endif
  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_RTNETLINK_H
  52. # include <linux/rtnetlink.h>
  53. #endif
  54. #ifdef HAVE_NETIPX_IPX_H
  55. # include <netipx/ipx.h>
  56. #elif defined(HAVE_LINUX_IPX_H)
  57. # ifdef HAVE_ASM_TYPES_H
  58. # include <asm/types.h>
  59. # endif
  60. # ifdef HAVE_LINUX_TYPES_H
  61. # include <linux/types.h>
  62. # endif
  63. # include <linux/ipx.h>
  64. #endif
  65. #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
  66. # define HAS_IPX
  67. #endif
  68. #ifdef HAVE_LINUX_IRDA_H
  69. # ifdef HAVE_LINUX_TYPES_H
  70. # include <linux/types.h>
  71. # endif
  72. # include <linux/irda.h>
  73. # define HAS_IRDA
  74. #endif
  75. #include "ntstatus.h"
  76. #define WIN32_NO_STATUS
  77. #include "windef.h"
  78. #include "winternl.h"
  79. #include "winerror.h"
  80. #define USE_WS_PREFIX
  81. #include "winsock2.h"
  82. #include "ws2tcpip.h"
  83. #include "wsipx.h"
  84. #include "af_irda.h"
  85. #include "wine/afd.h"
  86. #include "process.h"
  87. #include "file.h"
  88. #include "handle.h"
  89. #include "thread.h"
  90. #include "request.h"
  91. #include "user.h"
  92. /* From winsock.h */
  93. #define FD_MAX_EVENTS 10
  94. #define FD_READ_BIT 0
  95. #define FD_WRITE_BIT 1
  96. #define FD_OOB_BIT 2
  97. #define FD_ACCEPT_BIT 3
  98. #define FD_CONNECT_BIT 4
  99. #define FD_CLOSE_BIT 5
  100. /*
  101. * Define flags to be used with the WSAAsyncSelect() call.
  102. */
  103. #define FD_READ 0x00000001
  104. #define FD_WRITE 0x00000002
  105. #define FD_OOB 0x00000004
  106. #define FD_ACCEPT 0x00000008
  107. #define FD_CONNECT 0x00000010
  108. #define FD_CLOSE 0x00000020
  109. /* internal per-socket flags */
  110. #define FD_WINE_LISTENING 0x10000000
  111. #define FD_WINE_NONBLOCKING 0x20000000
  112. #define FD_WINE_CONNECTED 0x40000000
  113. #define FD_WINE_RAW 0x80000000
  114. #define FD_WINE_INTERNAL 0xFFFF0000
  115. struct accept_req
  116. {
  117. struct list entry;
  118. struct async *async;
  119. struct iosb *iosb;
  120. struct sock *sock, *acceptsock;
  121. int accepted;
  122. unsigned int recv_len, local_len;
  123. };
  124. struct sock
  125. {
  126. struct object obj; /* object header */
  127. struct fd *fd; /* socket file descriptor */
  128. unsigned int state; /* status bits */
  129. unsigned int mask; /* event mask */
  130. unsigned int hmask; /* held (blocked) events */
  131. unsigned int pmask; /* pending events */
  132. unsigned int flags; /* socket flags */
  133. int polling; /* is socket being polled? */
  134. unsigned short proto; /* socket protocol */
  135. unsigned short type; /* socket type */
  136. unsigned short family; /* socket family */
  137. struct event *event; /* event object */
  138. user_handle_t window; /* window to send the message to */
  139. unsigned int message; /* message to send */
  140. obj_handle_t wparam; /* message wparam (socket handle) */
  141. unsigned int errors[FD_MAX_EVENTS]; /* event errors */
  142. timeout_t connect_time;/* time the socket was connected */
  143. struct sock *deferred; /* socket that waits for a deferred accept */
  144. struct async_queue read_q; /* queue for asynchronous reads */
  145. struct async_queue write_q; /* queue for asynchronous writes */
  146. struct async_queue ifchange_q; /* queue for interface change notifications */
  147. struct async_queue accept_q; /* queue for asynchronous accepts */
  148. struct object *ifchange_obj; /* the interface change notification object */
  149. struct list ifchange_entry; /* entry in ifchange notification list */
  150. struct list accept_list; /* list of pending accept requests */
  151. struct accept_req *accept_recv_req; /* pending accept-into request which will recv on this socket */
  152. };
  153. static void sock_dump( struct object *obj, int verbose );
  154. static struct fd *sock_get_fd( struct object *obj );
  155. static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
  156. static void sock_destroy( struct object *obj );
  157. static struct object *sock_get_ifchange( struct sock *sock );
  158. static void sock_release_ifchange( struct sock *sock );
  159. static int sock_get_poll_events( struct fd *fd );
  160. static void sock_poll_event( struct fd *fd, int event );
  161. static enum server_fd_type sock_get_fd_type( struct fd *fd );
  162. static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  163. static void sock_queue_async( struct fd *fd, struct async *async, int type, int count );
  164. static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
  165. static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
  166. static struct sock *accept_socket( struct sock *sock );
  167. static int sock_get_ntstatus( int err );
  168. static unsigned int sock_get_error( int err );
  169. static const struct object_ops sock_ops =
  170. {
  171. sizeof(struct sock), /* size */
  172. &file_type, /* type */
  173. sock_dump, /* dump */
  174. add_queue, /* add_queue */
  175. remove_queue, /* remove_queue */
  176. default_fd_signaled, /* signaled */
  177. no_satisfied, /* satisfied */
  178. no_signal, /* signal */
  179. sock_get_fd, /* get_fd */
  180. default_map_access, /* map_access */
  181. default_get_sd, /* get_sd */
  182. default_set_sd, /* set_sd */
  183. no_get_full_name, /* get_full_name */
  184. no_lookup_name, /* lookup_name */
  185. no_link_name, /* link_name */
  186. NULL, /* unlink_name */
  187. no_open_file, /* open_file */
  188. no_kernel_obj_list, /* get_kernel_obj_list */
  189. sock_close_handle, /* close_handle */
  190. sock_destroy /* destroy */
  191. };
  192. static const struct fd_ops sock_fd_ops =
  193. {
  194. sock_get_poll_events, /* get_poll_events */
  195. sock_poll_event, /* poll_event */
  196. sock_get_fd_type, /* get_fd_type */
  197. no_fd_read, /* read */
  198. no_fd_write, /* write */
  199. no_fd_flush, /* flush */
  200. default_fd_get_file_info, /* get_file_info */
  201. no_fd_get_volume_info, /* get_volume_info */
  202. sock_ioctl, /* ioctl */
  203. sock_queue_async, /* queue_async */
  204. sock_reselect_async /* reselect_async */
  205. };
  206. union unix_sockaddr
  207. {
  208. struct sockaddr addr;
  209. struct sockaddr_in in;
  210. struct sockaddr_in6 in6;
  211. #ifdef HAS_IPX
  212. struct sockaddr_ipx ipx;
  213. #endif
  214. #ifdef HAS_IRDA
  215. struct sockaddr_irda irda;
  216. #endif
  217. };
  218. static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
  219. {
  220. memset( wsaddr, 0, wsaddrlen );
  221. switch (uaddr->addr.sa_family)
  222. {
  223. case AF_INET:
  224. {
  225. struct WS_sockaddr_in win = {0};
  226. if (wsaddrlen < sizeof(win)) return -1;
  227. win.sin_family = WS_AF_INET;
  228. win.sin_port = uaddr->in.sin_port;
  229. memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
  230. memcpy( wsaddr, &win, sizeof(win) );
  231. return sizeof(win);
  232. }
  233. case AF_INET6:
  234. {
  235. struct WS_sockaddr_in6 win = {0};
  236. if (wsaddrlen < sizeof(struct WS_sockaddr_in6_old)) return -1;
  237. win.sin6_family = WS_AF_INET6;
  238. win.sin6_port = uaddr->in6.sin6_port;
  239. win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
  240. memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
  241. #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
  242. win.sin6_scope_id = uaddr->in6.sin6_scope_id;
  243. #endif
  244. if (wsaddrlen >= sizeof(struct WS_sockaddr_in6))
  245. {
  246. memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6) );
  247. return sizeof(struct WS_sockaddr_in6);
  248. }
  249. memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6_old) );
  250. return sizeof(struct WS_sockaddr_in6_old);
  251. }
  252. #ifdef HAS_IPX
  253. case AF_IPX:
  254. {
  255. struct WS_sockaddr_ipx win = {0};
  256. if (wsaddrlen < sizeof(win)) return -1;
  257. win.sa_family = WS_AF_IPX;
  258. memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
  259. memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
  260. win.sa_socket = uaddr->ipx.sipx_port;
  261. memcpy( wsaddr, &win, sizeof(win) );
  262. return sizeof(win);
  263. }
  264. #endif
  265. #ifdef HAS_IRDA
  266. case AF_IRDA:
  267. {
  268. SOCKADDR_IRDA win;
  269. if (wsaddrlen < sizeof(win)) return -1;
  270. win.irdaAddressFamily = WS_AF_IRDA;
  271. memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
  272. if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
  273. snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
  274. else
  275. memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
  276. memcpy( wsaddr, &win, sizeof(win) );
  277. return sizeof(win);
  278. }
  279. #endif
  280. case AF_UNSPEC:
  281. return 0;
  282. default:
  283. return -1;
  284. }
  285. }
  286. /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
  287. * we post messages if there are multiple events. Used to send
  288. * messages. The problem is if there is both a FD_CONNECT event and,
  289. * say, an FD_READ event available on the same socket, we want to
  290. * notify the app of the connect event first. Otherwise it may
  291. * discard the read event because it thinks it hasn't connected yet.
  292. */
  293. static const int event_bitorder[FD_MAX_EVENTS] =
  294. {
  295. FD_CONNECT_BIT,
  296. FD_ACCEPT_BIT,
  297. FD_OOB_BIT,
  298. FD_WRITE_BIT,
  299. FD_READ_BIT,
  300. FD_CLOSE_BIT,
  301. 6, 7, 8, 9 /* leftovers */
  302. };
  303. /* Flags that make sense only for SOCK_STREAM sockets */
  304. #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
  305. typedef enum {
  306. SOCK_SHUTDOWN_ERROR = -1,
  307. SOCK_SHUTDOWN_EOF = 0,
  308. SOCK_SHUTDOWN_POLLHUP = 1
  309. } sock_shutdown_t;
  310. static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
  311. static sock_shutdown_t sock_check_pollhup(void)
  312. {
  313. sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
  314. int fd[2], n;
  315. struct pollfd pfd;
  316. char dummy;
  317. if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
  318. if ( shutdown( fd[0], 1 ) ) goto out;
  319. pfd.fd = fd[1];
  320. pfd.events = POLLIN;
  321. pfd.revents = 0;
  322. /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
  323. n = poll( &pfd, 1, 1 );
  324. if ( n != 1 ) goto out; /* error or timeout */
  325. if ( pfd.revents & POLLHUP )
  326. ret = SOCK_SHUTDOWN_POLLHUP;
  327. else if ( pfd.revents & POLLIN &&
  328. read( fd[1], &dummy, 1 ) == 0 )
  329. ret = SOCK_SHUTDOWN_EOF;
  330. out:
  331. close( fd[0] );
  332. close( fd[1] );
  333. return ret;
  334. }
  335. void sock_init(void)
  336. {
  337. sock_shutdown_type = sock_check_pollhup();
  338. switch ( sock_shutdown_type )
  339. {
  340. case SOCK_SHUTDOWN_EOF:
  341. if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
  342. break;
  343. case SOCK_SHUTDOWN_POLLHUP:
  344. if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
  345. break;
  346. default:
  347. fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
  348. sock_shutdown_type = SOCK_SHUTDOWN_EOF;
  349. }
  350. }
  351. static int sock_reselect( struct sock *sock )
  352. {
  353. int ev = sock_get_poll_events( sock->fd );
  354. if (debug_level)
  355. fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
  356. if (!sock->polling) /* FIXME: should find a better way to do this */
  357. {
  358. /* previously unconnected socket, is this reselect supposed to connect it? */
  359. if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
  360. /* ok, it is, attach it to the wineserver's main poll loop */
  361. sock->polling = 1;
  362. allow_fd_caching( sock->fd );
  363. }
  364. /* update condition mask */
  365. set_fd_events( sock->fd, ev );
  366. return ev;
  367. }
  368. /* wake anybody waiting on the socket event or send the associated message */
  369. static void sock_wake_up( struct sock *sock )
  370. {
  371. unsigned int events = sock->pmask & sock->mask;
  372. int i;
  373. if ( !events ) return;
  374. if (sock->event)
  375. {
  376. if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
  377. set_event( sock->event );
  378. }
  379. if (sock->window)
  380. {
  381. if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
  382. for (i = 0; i < FD_MAX_EVENTS; i++)
  383. {
  384. int event = event_bitorder[i];
  385. if (sock->pmask & (1 << event))
  386. {
  387. lparam_t lparam = (1 << event) | (sock->errors[event] << 16);
  388. post_message( sock->window, sock->message, sock->wparam, lparam );
  389. }
  390. }
  391. sock->pmask = 0;
  392. sock_reselect( sock );
  393. }
  394. }
  395. static inline int sock_error( struct fd *fd )
  396. {
  397. unsigned int optval = 0;
  398. socklen_t optlen = sizeof(optval);
  399. getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
  400. return optval;
  401. }
  402. static void free_accept_req( void *private )
  403. {
  404. struct accept_req *req = private;
  405. list_remove( &req->entry );
  406. if (req->acceptsock)
  407. {
  408. req->acceptsock->accept_recv_req = NULL;
  409. release_object( req->acceptsock );
  410. }
  411. release_object( req->async );
  412. release_object( req->iosb );
  413. release_object( req->sock );
  414. free( req );
  415. }
  416. static void fill_accept_output( struct accept_req *req )
  417. {
  418. struct iosb *iosb = req->iosb;
  419. union unix_sockaddr unix_addr;
  420. struct WS_sockaddr *win_addr;
  421. unsigned int remote_len;
  422. socklen_t unix_len;
  423. int fd, size = 0;
  424. char *out_data;
  425. int win_len;
  426. if (!(out_data = mem_alloc( iosb->out_size ))) return;
  427. fd = get_unix_fd( req->acceptsock->fd );
  428. if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
  429. {
  430. if (!req->accepted && errno == EWOULDBLOCK)
  431. {
  432. req->accepted = 1;
  433. sock_reselect( req->acceptsock );
  434. set_error( STATUS_PENDING );
  435. return;
  436. }
  437. set_win32_error( sock_get_error( errno ) );
  438. free( out_data );
  439. return;
  440. }
  441. if (req->local_len)
  442. {
  443. if (req->local_len < sizeof(int))
  444. {
  445. set_error( STATUS_BUFFER_TOO_SMALL );
  446. free( out_data );
  447. return;
  448. }
  449. unix_len = sizeof(unix_addr);
  450. win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
  451. if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
  452. (win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
  453. {
  454. set_win32_error( sock_get_error( errno ) );
  455. free( out_data );
  456. return;
  457. }
  458. memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
  459. }
  460. unix_len = sizeof(unix_addr);
  461. win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
  462. remote_len = iosb->out_size - req->recv_len - req->local_len;
  463. if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
  464. (win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
  465. {
  466. set_win32_error( sock_get_error( errno ) );
  467. free( out_data );
  468. return;
  469. }
  470. memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
  471. iosb->status = STATUS_SUCCESS;
  472. iosb->result = size;
  473. iosb->out_data = out_data;
  474. set_error( STATUS_ALERTED );
  475. }
  476. static void complete_async_accept( struct sock *sock, struct accept_req *req )
  477. {
  478. struct sock *acceptsock = req->acceptsock;
  479. struct async *async = req->async;
  480. if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
  481. if (acceptsock)
  482. {
  483. if (!accept_into_socket( sock, acceptsock )) return;
  484. fill_accept_output( req );
  485. }
  486. else
  487. {
  488. struct iosb *iosb = req->iosb;
  489. obj_handle_t handle;
  490. if (!(acceptsock = accept_socket( sock ))) return;
  491. handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
  492. GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
  493. acceptsock->wparam = handle;
  494. release_object( acceptsock );
  495. if (!handle) return;
  496. if (!(iosb->out_data = malloc( sizeof(handle) ))) return;
  497. iosb->status = STATUS_SUCCESS;
  498. iosb->out_size = sizeof(handle);
  499. memcpy( iosb->out_data, &handle, sizeof(handle) );
  500. set_error( STATUS_ALERTED );
  501. }
  502. }
  503. static void complete_async_accept_recv( struct accept_req *req )
  504. {
  505. if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
  506. assert( req->recv_len );
  507. fill_accept_output( req );
  508. }
  509. static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
  510. {
  511. if (event & (POLLIN | POLLPRI))
  512. {
  513. struct accept_req *req;
  514. LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
  515. {
  516. if (req->iosb->status == STATUS_PENDING && !req->accepted)
  517. {
  518. complete_async_accept( sock, req );
  519. if (get_error() != STATUS_PENDING)
  520. async_terminate( req->async, get_error() );
  521. break;
  522. }
  523. }
  524. if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
  525. {
  526. complete_async_accept_recv( sock->accept_recv_req );
  527. if (get_error() != STATUS_PENDING)
  528. async_terminate( sock->accept_recv_req->async, get_error() );
  529. }
  530. }
  531. if (is_fd_overlapped( sock->fd ))
  532. {
  533. if (event & (POLLIN|POLLPRI) && async_waiting( &sock->read_q ))
  534. {
  535. if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
  536. async_wake_up( &sock->read_q, STATUS_ALERTED );
  537. event &= ~(POLLIN|POLLPRI);
  538. }
  539. if (event & POLLOUT && async_waiting( &sock->write_q ))
  540. {
  541. if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
  542. async_wake_up( &sock->write_q, STATUS_ALERTED );
  543. event &= ~POLLOUT;
  544. }
  545. }
  546. if (event & (POLLERR | POLLHUP))
  547. {
  548. int status = sock_get_ntstatus( error );
  549. struct accept_req *req, *next;
  550. if (!(sock->state & FD_READ))
  551. async_wake_up( &sock->read_q, status );
  552. if (!(sock->state & FD_WRITE))
  553. async_wake_up( &sock->write_q, status );
  554. LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
  555. {
  556. if (req->iosb->status == STATUS_PENDING)
  557. async_terminate( req->async, status );
  558. }
  559. if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
  560. async_terminate( sock->accept_recv_req->async, status );
  561. }
  562. return event;
  563. }
  564. static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
  565. {
  566. if (prevstate & FD_CONNECT)
  567. {
  568. sock->pmask |= FD_CONNECT;
  569. sock->hmask |= FD_CONNECT;
  570. sock->errors[FD_CONNECT_BIT] = sock_get_error( error );
  571. goto end;
  572. }
  573. if (prevstate & FD_WINE_LISTENING)
  574. {
  575. sock->pmask |= FD_ACCEPT;
  576. sock->hmask |= FD_ACCEPT;
  577. sock->errors[FD_ACCEPT_BIT] = sock_get_error( error );
  578. goto end;
  579. }
  580. if (event & POLLIN)
  581. {
  582. sock->pmask |= FD_READ;
  583. sock->hmask |= FD_READ;
  584. sock->errors[FD_READ_BIT] = 0;
  585. }
  586. if (event & POLLOUT)
  587. {
  588. sock->pmask |= FD_WRITE;
  589. sock->hmask |= FD_WRITE;
  590. sock->errors[FD_WRITE_BIT] = 0;
  591. }
  592. if (event & POLLPRI)
  593. {
  594. sock->pmask |= FD_OOB;
  595. sock->hmask |= FD_OOB;
  596. sock->errors[FD_OOB_BIT] = 0;
  597. }
  598. if (event & (POLLERR|POLLHUP))
  599. {
  600. sock->pmask |= FD_CLOSE;
  601. sock->hmask |= FD_CLOSE;
  602. sock->errors[FD_CLOSE_BIT] = sock_get_error( error );
  603. }
  604. end:
  605. sock_wake_up( sock );
  606. }
  607. static void sock_poll_event( struct fd *fd, int event )
  608. {
  609. struct sock *sock = get_fd_user( fd );
  610. int hangup_seen = 0;
  611. int prevstate = sock->state;
  612. int error = 0;
  613. assert( sock->obj.ops == &sock_ops );
  614. if (debug_level)
  615. fprintf(stderr, "socket %p select event: %x\n", sock, event);
  616. /* we may change event later, remove from loop here */
  617. if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
  618. if (sock->state & FD_CONNECT)
  619. {
  620. if (event & (POLLERR|POLLHUP))
  621. {
  622. /* we didn't get connected? */
  623. sock->state &= ~FD_CONNECT;
  624. event &= ~POLLOUT;
  625. error = sock_error( fd );
  626. }
  627. else if (event & POLLOUT)
  628. {
  629. /* we got connected */
  630. sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
  631. sock->state &= ~FD_CONNECT;
  632. sock->connect_time = current_time;
  633. }
  634. }
  635. else if (sock->state & FD_WINE_LISTENING)
  636. {
  637. /* listening */
  638. if (event & (POLLERR|POLLHUP))
  639. error = sock_error( fd );
  640. }
  641. else
  642. {
  643. /* normal data flow */
  644. if (sock->type == WS_SOCK_STREAM && (event & POLLIN))
  645. {
  646. char dummy;
  647. int nr;
  648. /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
  649. * has been closed, so we need to check for it explicitly here */
  650. nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
  651. if ( nr == 0 )
  652. {
  653. hangup_seen = 1;
  654. event &= ~POLLIN;
  655. }
  656. else if ( nr < 0 )
  657. {
  658. event &= ~POLLIN;
  659. /* EAGAIN can happen if an async recv() falls between the server's poll()
  660. call and the invocation of this routine */
  661. if ( errno != EAGAIN )
  662. {
  663. error = errno;
  664. event |= POLLERR;
  665. if ( debug_level )
  666. fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
  667. }
  668. }
  669. }
  670. if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
  671. {
  672. error = error ? error : sock_error( fd );
  673. if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
  674. sock->state &= ~FD_WRITE;
  675. sock->state &= ~FD_READ;
  676. if (debug_level)
  677. fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
  678. }
  679. if (hangup_seen)
  680. event |= POLLHUP;
  681. }
  682. event = sock_dispatch_asyncs( sock, event, error );
  683. sock_dispatch_events( sock, prevstate, event, error );
  684. sock_reselect( sock );
  685. }
  686. static void sock_dump( struct object *obj, int verbose )
  687. {
  688. struct sock *sock = (struct sock *)obj;
  689. assert( obj->ops == &sock_ops );
  690. fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
  691. sock->fd, sock->state,
  692. sock->mask, sock->pmask, sock->hmask );
  693. }
  694. static int sock_get_poll_events( struct fd *fd )
  695. {
  696. struct sock *sock = get_fd_user( fd );
  697. unsigned int mask = sock->mask & ~sock->hmask;
  698. unsigned int smask = sock->state & mask;
  699. int ev = 0;
  700. assert( sock->obj.ops == &sock_ops );
  701. if (sock->state & FD_CONNECT)
  702. /* connecting, wait for writable */
  703. return POLLOUT;
  704. if (!list_empty( &sock->accept_list ) || sock->accept_recv_req )
  705. {
  706. ev |= POLLIN | POLLPRI;
  707. }
  708. else if (async_queued( &sock->read_q ))
  709. {
  710. if (async_waiting( &sock->read_q )) ev |= POLLIN | POLLPRI;
  711. }
  712. else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
  713. ev |= POLLIN | POLLPRI;
  714. /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
  715. else if (sock->type == WS_SOCK_STREAM && (sock->state & FD_READ) && (mask & FD_CLOSE) &&
  716. !(sock->hmask & FD_READ))
  717. ev |= POLLIN;
  718. if (async_queued( &sock->write_q ))
  719. {
  720. if (async_waiting( &sock->write_q )) ev |= POLLOUT;
  721. }
  722. else if (smask & FD_WRITE)
  723. ev |= POLLOUT;
  724. return ev;
  725. }
  726. static enum server_fd_type sock_get_fd_type( struct fd *fd )
  727. {
  728. return FD_TYPE_SOCKET;
  729. }
  730. static void sock_queue_async( struct fd *fd, struct async *async, int type, int count )
  731. {
  732. struct sock *sock = get_fd_user( fd );
  733. struct async_queue *queue;
  734. assert( sock->obj.ops == &sock_ops );
  735. switch (type)
  736. {
  737. case ASYNC_TYPE_READ:
  738. queue = &sock->read_q;
  739. break;
  740. case ASYNC_TYPE_WRITE:
  741. queue = &sock->write_q;
  742. break;
  743. default:
  744. set_error( STATUS_INVALID_PARAMETER );
  745. return;
  746. }
  747. if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
  748. ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
  749. {
  750. set_error( STATUS_PIPE_DISCONNECTED );
  751. return;
  752. }
  753. queue_async( queue, async );
  754. sock_reselect( sock );
  755. set_error( STATUS_PENDING );
  756. }
  757. static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
  758. {
  759. struct sock *sock = get_fd_user( fd );
  760. /* ignore reselect on ifchange queue */
  761. if (&sock->ifchange_q != queue)
  762. sock_reselect( sock );
  763. }
  764. static struct fd *sock_get_fd( struct object *obj )
  765. {
  766. struct sock *sock = (struct sock *)obj;
  767. return (struct fd *)grab_object( sock->fd );
  768. }
  769. static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
  770. {
  771. struct sock *sock = (struct sock *)obj;
  772. struct accept_req *req, *next;
  773. if (sock->obj.handle_count == 1) /* last handle */
  774. {
  775. if (sock->accept_recv_req)
  776. async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
  777. LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
  778. async_terminate( req->async, STATUS_CANCELLED );
  779. }
  780. return 1;
  781. }
  782. static void sock_destroy( struct object *obj )
  783. {
  784. struct sock *sock = (struct sock *)obj;
  785. assert( obj->ops == &sock_ops );
  786. /* FIXME: special socket shutdown stuff? */
  787. if ( sock->deferred )
  788. release_object( sock->deferred );
  789. async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
  790. sock_release_ifchange( sock );
  791. free_async_queue( &sock->read_q );
  792. free_async_queue( &sock->write_q );
  793. free_async_queue( &sock->ifchange_q );
  794. free_async_queue( &sock->accept_q );
  795. if (sock->event) release_object( sock->event );
  796. if (sock->fd)
  797. {
  798. /* shut the socket down to force pending poll() calls in the client to return */
  799. shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
  800. release_object( sock->fd );
  801. }
  802. }
  803. static struct sock *create_socket(void)
  804. {
  805. struct sock *sock;
  806. if (!(sock = alloc_object( &sock_ops ))) return NULL;
  807. sock->fd = NULL;
  808. sock->state = 0;
  809. sock->mask = 0;
  810. sock->hmask = 0;
  811. sock->pmask = 0;
  812. sock->polling = 0;
  813. sock->flags = 0;
  814. sock->proto = 0;
  815. sock->type = 0;
  816. sock->family = 0;
  817. sock->event = NULL;
  818. sock->window = 0;
  819. sock->message = 0;
  820. sock->wparam = 0;
  821. sock->connect_time = 0;
  822. sock->deferred = NULL;
  823. sock->ifchange_obj = NULL;
  824. sock->accept_recv_req = NULL;
  825. init_async_queue( &sock->read_q );
  826. init_async_queue( &sock->write_q );
  827. init_async_queue( &sock->ifchange_q );
  828. init_async_queue( &sock->accept_q );
  829. memset( sock->errors, 0, sizeof(sock->errors) );
  830. list_init( &sock->accept_list );
  831. return sock;
  832. }
  833. static int get_unix_family( int family )
  834. {
  835. switch (family)
  836. {
  837. case WS_AF_INET: return AF_INET;
  838. case WS_AF_INET6: return AF_INET6;
  839. #ifdef HAS_IPX
  840. case WS_AF_IPX: return AF_IPX;
  841. #endif
  842. #ifdef AF_IRDA
  843. case WS_AF_IRDA: return AF_IRDA;
  844. #endif
  845. case WS_AF_UNSPEC: return AF_UNSPEC;
  846. default: return -1;
  847. }
  848. }
  849. static int get_unix_type( int type )
  850. {
  851. switch (type)
  852. {
  853. case WS_SOCK_DGRAM: return SOCK_DGRAM;
  854. case WS_SOCK_RAW: return SOCK_RAW;
  855. case WS_SOCK_STREAM: return SOCK_STREAM;
  856. default: return -1;
  857. }
  858. }
  859. static int get_unix_protocol( int protocol )
  860. {
  861. if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
  862. return protocol;
  863. switch (protocol)
  864. {
  865. case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
  866. case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
  867. case WS_IPPROTO_IP: return IPPROTO_IP;
  868. case WS_IPPROTO_IPIP: return IPPROTO_IPIP;
  869. case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
  870. case WS_IPPROTO_RAW: return IPPROTO_RAW;
  871. case WS_IPPROTO_TCP: return IPPROTO_TCP;
  872. case WS_IPPROTO_UDP: return IPPROTO_UDP;
  873. default: return -1;
  874. }
  875. }
  876. static void set_dont_fragment( int fd, int level, int value )
  877. {
  878. int optname;
  879. if (level == IPPROTO_IP)
  880. {
  881. #ifdef IP_DONTFRAG
  882. optname = IP_DONTFRAG;
  883. #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
  884. optname = IP_MTU_DISCOVER;
  885. value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
  886. #else
  887. return;
  888. #endif
  889. }
  890. else
  891. {
  892. #ifdef IPV6_DONTFRAG
  893. optname = IPV6_DONTFRAG;
  894. #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
  895. optname = IPV6_MTU_DISCOVER;
  896. value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
  897. #else
  898. return;
  899. #endif
  900. }
  901. setsockopt( fd, level, optname, &value, sizeof(value) );
  902. }
  903. static int init_socket( struct sock *sock, int family, int type, int protocol, unsigned int flags )
  904. {
  905. unsigned int options = 0;
  906. int sockfd, unix_type, unix_family, unix_protocol;
  907. unix_family = get_unix_family( family );
  908. unix_type = get_unix_type( type );
  909. unix_protocol = get_unix_protocol( protocol );
  910. if (unix_protocol < 0)
  911. {
  912. if (type && unix_type < 0)
  913. set_win32_error( WSAESOCKTNOSUPPORT );
  914. else
  915. set_win32_error( WSAEPROTONOSUPPORT );
  916. return -1;
  917. }
  918. if (unix_family < 0)
  919. {
  920. if (family >= 0 && unix_type < 0)
  921. set_win32_error( WSAESOCKTNOSUPPORT );
  922. else
  923. set_win32_error( WSAEAFNOSUPPORT );
  924. return -1;
  925. }
  926. sockfd = socket( unix_family, unix_type, unix_protocol );
  927. if (sockfd == -1)
  928. {
  929. if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
  930. else set_win32_error( sock_get_error( errno ));
  931. return -1;
  932. }
  933. fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
  934. if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
  935. {
  936. #ifdef HAS_IPX
  937. int ipx_type = protocol - WS_NSPROTO_IPX;
  938. #ifdef SOL_IPX
  939. setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
  940. #else
  941. struct ipx val;
  942. /* Should we retrieve val using a getsockopt call and then
  943. * set the modified one? */
  944. val.ipx_pt = ipx_type;
  945. setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
  946. #endif
  947. #endif
  948. }
  949. if (unix_family == AF_INET || unix_family == AF_INET6)
  950. {
  951. /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
  952. if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
  953. set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
  954. else if (unix_type == SOCK_STREAM)
  955. set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
  956. }
  957. #ifdef IPV6_V6ONLY
  958. if (unix_family == AF_INET6)
  959. {
  960. static const int enable = 1;
  961. setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
  962. }
  963. #endif
  964. sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
  965. sock->flags = flags;
  966. sock->proto = protocol;
  967. sock->type = type;
  968. sock->family = family;
  969. if (sock->fd)
  970. {
  971. options = get_fd_options( sock->fd );
  972. release_object( sock->fd );
  973. }
  974. if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
  975. {
  976. return -1;
  977. }
  978. sock_reselect( sock );
  979. clear_error();
  980. return 0;
  981. }
  982. /* accepts a socket and inits it */
  983. static int accept_new_fd( struct sock *sock )
  984. {
  985. /* Try to accept(2). We can't be safe that this an already connected socket
  986. * or that accept() is allowed on it. In those cases we will get -1/errno
  987. * return.
  988. */
  989. struct sockaddr saddr;
  990. socklen_t slen = sizeof(saddr);
  991. int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
  992. if (acceptfd != -1)
  993. fcntl( acceptfd, F_SETFL, O_NONBLOCK );
  994. else
  995. set_win32_error( sock_get_error( errno ));
  996. return acceptfd;
  997. }
  998. /* accept a socket (creates a new fd) */
  999. static struct sock *accept_socket( struct sock *sock )
  1000. {
  1001. struct sock *acceptsock;
  1002. int acceptfd;
  1003. if (get_unix_fd( sock->fd ) == -1) return NULL;
  1004. if ( sock->deferred )
  1005. {
  1006. acceptsock = sock->deferred;
  1007. sock->deferred = NULL;
  1008. }
  1009. else
  1010. {
  1011. if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
  1012. if (!(acceptsock = create_socket()))
  1013. {
  1014. close( acceptfd );
  1015. return NULL;
  1016. }
  1017. /* newly created socket gets the same properties of the listening socket */
  1018. acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
  1019. if (sock->state & FD_WINE_NONBLOCKING)
  1020. acceptsock->state |= FD_WINE_NONBLOCKING;
  1021. acceptsock->mask = sock->mask;
  1022. acceptsock->proto = sock->proto;
  1023. acceptsock->type = sock->type;
  1024. acceptsock->family = sock->family;
  1025. acceptsock->window = sock->window;
  1026. acceptsock->message = sock->message;
  1027. acceptsock->connect_time = current_time;
  1028. if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
  1029. acceptsock->flags = sock->flags;
  1030. if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
  1031. get_fd_options( sock->fd ) )))
  1032. {
  1033. release_object( acceptsock );
  1034. return NULL;
  1035. }
  1036. }
  1037. clear_error();
  1038. sock->pmask &= ~FD_ACCEPT;
  1039. sock->hmask &= ~FD_ACCEPT;
  1040. sock_reselect( sock );
  1041. return acceptsock;
  1042. }
  1043. static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
  1044. {
  1045. int acceptfd;
  1046. struct fd *newfd;
  1047. if (get_unix_fd( sock->fd ) == -1) return FALSE;
  1048. if ( sock->deferred )
  1049. {
  1050. newfd = dup_fd_object( sock->deferred->fd, 0, 0,
  1051. get_fd_options( acceptsock->fd ) );
  1052. if ( !newfd )
  1053. return FALSE;
  1054. set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
  1055. release_object( sock->deferred );
  1056. sock->deferred = NULL;
  1057. }
  1058. else
  1059. {
  1060. if ((acceptfd = accept_new_fd( sock )) == -1)
  1061. return FALSE;
  1062. if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
  1063. get_fd_options( acceptsock->fd ) )))
  1064. return FALSE;
  1065. }
  1066. acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
  1067. acceptsock->hmask = 0;
  1068. acceptsock->pmask = 0;
  1069. acceptsock->polling = 0;
  1070. acceptsock->proto = sock->proto;
  1071. acceptsock->type = sock->type;
  1072. acceptsock->family = sock->family;
  1073. acceptsock->wparam = 0;
  1074. acceptsock->deferred = NULL;
  1075. acceptsock->connect_time = current_time;
  1076. fd_copy_completion( acceptsock->fd, newfd );
  1077. release_object( acceptsock->fd );
  1078. acceptsock->fd = newfd;
  1079. clear_error();
  1080. sock->pmask &= ~FD_ACCEPT;
  1081. sock->hmask &= ~FD_ACCEPT;
  1082. sock_reselect( sock );
  1083. return TRUE;
  1084. }
  1085. /* return an errno value mapped to a WSA error */
  1086. static unsigned int sock_get_error( int err )
  1087. {
  1088. switch (err)
  1089. {
  1090. case EINTR: return WSAEINTR;
  1091. case EBADF: return WSAEBADF;
  1092. case EPERM:
  1093. case EACCES: return WSAEACCES;
  1094. case EFAULT: return WSAEFAULT;
  1095. case EINVAL: return WSAEINVAL;
  1096. case EMFILE: return WSAEMFILE;
  1097. case EWOULDBLOCK: return WSAEWOULDBLOCK;
  1098. case EINPROGRESS: return WSAEINPROGRESS;
  1099. case EALREADY: return WSAEALREADY;
  1100. case ENOTSOCK: return WSAENOTSOCK;
  1101. case EDESTADDRREQ: return WSAEDESTADDRREQ;
  1102. case EMSGSIZE: return WSAEMSGSIZE;
  1103. case EPROTOTYPE: return WSAEPROTOTYPE;
  1104. case ENOPROTOOPT: return WSAENOPROTOOPT;
  1105. case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
  1106. case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
  1107. case EOPNOTSUPP: return WSAEOPNOTSUPP;
  1108. case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
  1109. case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
  1110. case EADDRINUSE: return WSAEADDRINUSE;
  1111. case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
  1112. case ENETDOWN: return WSAENETDOWN;
  1113. case ENETUNREACH: return WSAENETUNREACH;
  1114. case ENETRESET: return WSAENETRESET;
  1115. case ECONNABORTED: return WSAECONNABORTED;
  1116. case EPIPE:
  1117. case ECONNRESET: return WSAECONNRESET;
  1118. case ENOBUFS: return WSAENOBUFS;
  1119. case EISCONN: return WSAEISCONN;
  1120. case ENOTCONN: return WSAENOTCONN;
  1121. case ESHUTDOWN: return WSAESHUTDOWN;
  1122. case ETOOMANYREFS: return WSAETOOMANYREFS;
  1123. case ETIMEDOUT: return WSAETIMEDOUT;
  1124. case ECONNREFUSED: return WSAECONNREFUSED;
  1125. case ELOOP: return WSAELOOP;
  1126. case ENAMETOOLONG: return WSAENAMETOOLONG;
  1127. case EHOSTDOWN: return WSAEHOSTDOWN;
  1128. case EHOSTUNREACH: return WSAEHOSTUNREACH;
  1129. case ENOTEMPTY: return WSAENOTEMPTY;
  1130. #ifdef EPROCLIM
  1131. case EPROCLIM: return WSAEPROCLIM;
  1132. #endif
  1133. #ifdef EUSERS
  1134. case EUSERS: return WSAEUSERS;
  1135. #endif
  1136. #ifdef EDQUOT
  1137. case EDQUOT: return WSAEDQUOT;
  1138. #endif
  1139. #ifdef ESTALE
  1140. case ESTALE: return WSAESTALE;
  1141. #endif
  1142. #ifdef EREMOTE
  1143. case EREMOTE: return WSAEREMOTE;
  1144. #endif
  1145. case 0: return 0;
  1146. default:
  1147. errno = err;
  1148. perror("wineserver: sock_get_error() can't map error");
  1149. return WSAEFAULT;
  1150. }
  1151. }
  1152. static int sock_get_ntstatus( int err )
  1153. {
  1154. switch ( err )
  1155. {
  1156. case EBADF: return STATUS_INVALID_HANDLE;
  1157. case EBUSY: return STATUS_DEVICE_BUSY;
  1158. case EPERM:
  1159. case EACCES: return STATUS_ACCESS_DENIED;
  1160. case EFAULT: return STATUS_NO_MEMORY;
  1161. case EINVAL: return STATUS_INVALID_PARAMETER;
  1162. case ENFILE:
  1163. case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
  1164. case EWOULDBLOCK: return STATUS_CANT_WAIT;
  1165. case EINPROGRESS: return STATUS_PENDING;
  1166. case EALREADY: return STATUS_NETWORK_BUSY;
  1167. case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
  1168. case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
  1169. case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
  1170. case EPROTONOSUPPORT:
  1171. case ESOCKTNOSUPPORT:
  1172. case EPFNOSUPPORT:
  1173. case EAFNOSUPPORT:
  1174. case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
  1175. case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
  1176. case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
  1177. case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
  1178. case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
  1179. case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
  1180. case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
  1181. case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
  1182. case ETIMEDOUT: return STATUS_IO_TIMEOUT;
  1183. case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
  1184. case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
  1185. case ENETDOWN: return STATUS_NETWORK_BUSY;
  1186. case EPIPE:
  1187. case ECONNRESET: return STATUS_CONNECTION_RESET;
  1188. case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
  1189. case 0: return STATUS_SUCCESS;
  1190. default:
  1191. errno = err;
  1192. perror("wineserver: sock_get_ntstatus() can't map error");
  1193. return STATUS_UNSUCCESSFUL;
  1194. }
  1195. }
  1196. static struct accept_req *alloc_accept_req( struct sock *sock, struct sock *acceptsock, struct async *async,
  1197. const struct afd_accept_into_params *params )
  1198. {
  1199. struct accept_req *req = mem_alloc( sizeof(*req) );
  1200. if (req)
  1201. {
  1202. req->async = (struct async *)grab_object( async );
  1203. req->iosb = async_get_iosb( async );
  1204. req->sock = (struct sock *)grab_object( sock );
  1205. req->acceptsock = acceptsock;
  1206. if (acceptsock) grab_object( acceptsock );
  1207. req->accepted = 0;
  1208. req->recv_len = 0;
  1209. req->local_len = 0;
  1210. if (params)
  1211. {
  1212. req->recv_len = params->recv_len;
  1213. req->local_len = params->local_len;
  1214. }
  1215. }
  1216. return req;
  1217. }
  1218. static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1219. {
  1220. struct sock *sock = get_fd_user( fd );
  1221. assert( sock->obj.ops == &sock_ops );
  1222. if (get_unix_fd( fd ) == -1 && code != IOCTL_AFD_CREATE) return 0;
  1223. switch(code)
  1224. {
  1225. case IOCTL_AFD_CREATE:
  1226. {
  1227. const struct afd_create_params *params = get_req_data();
  1228. if (get_req_data_size() != sizeof(*params))
  1229. {
  1230. set_error( STATUS_INVALID_PARAMETER );
  1231. return 0;
  1232. }
  1233. init_socket( sock, params->family, params->type, params->protocol, params->flags );
  1234. return 0;
  1235. }
  1236. case IOCTL_AFD_ACCEPT:
  1237. {
  1238. struct sock *acceptsock;
  1239. obj_handle_t handle;
  1240. if (get_reply_max_size() != sizeof(handle))
  1241. {
  1242. set_error( STATUS_BUFFER_TOO_SMALL );
  1243. return 0;
  1244. }
  1245. if (!(acceptsock = accept_socket( sock )))
  1246. {
  1247. struct accept_req *req;
  1248. if (sock->state & FD_WINE_NONBLOCKING) return 0;
  1249. if (get_error() != (0xc0010000 | WSAEWOULDBLOCK)) return 0;
  1250. if (!(req = alloc_accept_req( sock, NULL, async, NULL ))) return 0;
  1251. list_add_tail( &sock->accept_list, &req->entry );
  1252. async_set_completion_callback( async, free_accept_req, req );
  1253. queue_async( &sock->accept_q, async );
  1254. sock_reselect( sock );
  1255. set_error( STATUS_PENDING );
  1256. return 1;
  1257. }
  1258. handle = alloc_handle( current->process, &acceptsock->obj,
  1259. GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
  1260. acceptsock->wparam = handle;
  1261. release_object( acceptsock );
  1262. set_reply_data( &handle, sizeof(handle) );
  1263. return 0;
  1264. }
  1265. case IOCTL_AFD_ACCEPT_INTO:
  1266. {
  1267. static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
  1268. const struct afd_accept_into_params *params = get_req_data();
  1269. struct sock *acceptsock;
  1270. unsigned int remote_len;
  1271. struct accept_req *req;
  1272. if (get_req_data_size() != sizeof(*params) ||
  1273. get_reply_max_size() < params->recv_len ||
  1274. get_reply_max_size() - params->recv_len < params->local_len)
  1275. {
  1276. set_error( STATUS_BUFFER_TOO_SMALL );
  1277. return 0;
  1278. }
  1279. remote_len = get_reply_max_size() - params->recv_len - params->local_len;
  1280. if (remote_len < sizeof(int))
  1281. {
  1282. set_error( STATUS_INVALID_PARAMETER );
  1283. return 0;
  1284. }
  1285. if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
  1286. return 0;
  1287. if (acceptsock->accept_recv_req)
  1288. {
  1289. release_object( acceptsock );
  1290. set_win32_error( WSAEINVAL );
  1291. return 0;
  1292. }
  1293. if (!(req = alloc_accept_req( sock, acceptsock, async, params )))
  1294. {
  1295. release_object( acceptsock );
  1296. return 0;
  1297. }
  1298. list_add_tail( &sock->accept_list, &req->entry );
  1299. acceptsock->accept_recv_req = req;
  1300. release_object( acceptsock );
  1301. acceptsock->wparam = params->accept_handle;
  1302. async_set_completion_callback( async, free_accept_req, req );
  1303. queue_async( &sock->accept_q, async );
  1304. sock_reselect( sock );
  1305. set_error( STATUS_PENDING );
  1306. return 1;
  1307. }
  1308. case IOCTL_AFD_ADDRESS_LIST_CHANGE:
  1309. if ((sock->state & FD_WINE_NONBLOCKING) && async_is_blocking( async ))
  1310. {
  1311. set_win32_error( WSAEWOULDBLOCK );
  1312. return 0;
  1313. }
  1314. if (!sock_get_ifchange( sock )) return 0;
  1315. queue_async( &sock->ifchange_q, async );
  1316. set_error( STATUS_PENDING );
  1317. return 1;
  1318. default:
  1319. set_error( STATUS_NOT_SUPPORTED );
  1320. return 0;
  1321. }
  1322. }
  1323. #ifdef HAVE_LINUX_RTNETLINK_H
  1324. /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
  1325. static struct object *ifchange_object;
  1326. static void ifchange_dump( struct object *obj, int verbose );
  1327. static struct fd *ifchange_get_fd( struct object *obj );
  1328. static void ifchange_destroy( struct object *obj );
  1329. static int ifchange_get_poll_events( struct fd *fd );
  1330. static void ifchange_poll_event( struct fd *fd, int event );
  1331. struct ifchange
  1332. {
  1333. struct object obj; /* object header */
  1334. struct fd *fd; /* interface change file descriptor */
  1335. struct list sockets; /* list of sockets to send interface change notifications */
  1336. };
  1337. static const struct object_ops ifchange_ops =
  1338. {
  1339. sizeof(struct ifchange), /* size */
  1340. &no_type, /* type */
  1341. ifchange_dump, /* dump */
  1342. no_add_queue, /* add_queue */
  1343. NULL, /* remove_queue */
  1344. NULL, /* signaled */
  1345. no_satisfied, /* satisfied */
  1346. no_signal, /* signal */
  1347. ifchange_get_fd, /* get_fd */
  1348. default_map_access, /* map_access */
  1349. default_get_sd, /* get_sd */
  1350. default_set_sd, /* set_sd */
  1351. no_get_full_name, /* get_full_name */
  1352. no_lookup_name, /* lookup_name */
  1353. no_link_name, /* link_name */
  1354. NULL, /* unlink_name */
  1355. no_open_file, /* open_file */
  1356. no_kernel_obj_list, /* get_kernel_obj_list */
  1357. no_close_handle, /* close_handle */
  1358. ifchange_destroy /* destroy */
  1359. };
  1360. static const struct fd_ops ifchange_fd_ops =
  1361. {
  1362. ifchange_get_poll_events, /* get_poll_events */
  1363. ifchange_poll_event, /* poll_event */
  1364. NULL, /* get_fd_type */
  1365. no_fd_read, /* read */
  1366. no_fd_write, /* write */
  1367. no_fd_flush, /* flush */
  1368. no_fd_get_file_info, /* get_file_info */
  1369. no_fd_get_volume_info, /* get_volume_info */
  1370. no_fd_ioctl, /* ioctl */
  1371. NULL, /* queue_async */
  1372. NULL /* reselect_async */
  1373. };
  1374. static void ifchange_dump( struct object *obj, int verbose )
  1375. {
  1376. assert( obj->ops == &ifchange_ops );
  1377. fprintf( stderr, "Interface change\n" );
  1378. }
  1379. static struct fd *ifchange_get_fd( struct object *obj )
  1380. {
  1381. struct ifchange *ifchange = (struct ifchange *)obj;
  1382. return (struct fd *)grab_object( ifchange->fd );
  1383. }
  1384. static void ifchange_destroy( struct object *obj )
  1385. {
  1386. struct ifchange *ifchange = (struct ifchange *)obj;
  1387. assert( obj->ops == &ifchange_ops );
  1388. release_object( ifchange->fd );
  1389. /* reset the global ifchange object so that it will be recreated if it is needed again */
  1390. assert( obj == ifchange_object );
  1391. ifchange_object = NULL;
  1392. }
  1393. static int ifchange_get_poll_events( struct fd *fd )
  1394. {
  1395. return POLLIN;
  1396. }
  1397. /* wake up all the sockets waiting for a change notification event */
  1398. static void ifchange_wake_up( struct object *obj, unsigned int status )
  1399. {
  1400. struct ifchange *ifchange = (struct ifchange *)obj;
  1401. struct list *ptr, *next;
  1402. assert( obj->ops == &ifchange_ops );
  1403. assert( obj == ifchange_object );
  1404. LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
  1405. {
  1406. struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
  1407. assert( sock->ifchange_obj );
  1408. async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
  1409. sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
  1410. }
  1411. }
  1412. static void ifchange_poll_event( struct fd *fd, int event )
  1413. {
  1414. struct object *ifchange = get_fd_user( fd );
  1415. unsigned int status = STATUS_PENDING;
  1416. char buffer[PIPE_BUF];
  1417. int r;
  1418. r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
  1419. if (r < 0)
  1420. {
  1421. if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
  1422. return; /* retry when poll() says the socket is ready */
  1423. status = sock_get_ntstatus( errno );
  1424. }
  1425. else if (r > 0)
  1426. {
  1427. struct nlmsghdr *nlh;
  1428. for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
  1429. {
  1430. if (nlh->nlmsg_type == NLMSG_DONE)
  1431. break;
  1432. if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
  1433. status = STATUS_SUCCESS;
  1434. }
  1435. }
  1436. else status = STATUS_CANCELLED;
  1437. if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
  1438. }
  1439. #endif
  1440. /* we only need one of these interface notification objects, all of the sockets dependent upon
  1441. * it will wake up when a notification event occurs */
  1442. static struct object *get_ifchange( void )
  1443. {
  1444. #ifdef HAVE_LINUX_RTNETLINK_H
  1445. struct ifchange *ifchange;
  1446. struct sockaddr_nl addr;
  1447. int unix_fd;
  1448. if (ifchange_object)
  1449. {
  1450. /* increment the refcount for each socket that uses the ifchange object */
  1451. return grab_object( ifchange_object );
  1452. }
  1453. /* create the socket we need for processing interface change notifications */
  1454. unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
  1455. if (unix_fd == -1)
  1456. {
  1457. set_win32_error( sock_get_error( errno ));
  1458. return NULL;
  1459. }
  1460. fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
  1461. memset( &addr, 0, sizeof(addr) );
  1462. addr.nl_family = AF_NETLINK;
  1463. addr.nl_groups = RTMGRP_IPV4_IFADDR;
  1464. /* bind the socket to the special netlink kernel interface */
  1465. if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
  1466. {
  1467. close( unix_fd );
  1468. set_win32_error( sock_get_error( errno ));
  1469. return NULL;
  1470. }
  1471. if (!(ifchange = alloc_object( &ifchange_ops )))
  1472. {
  1473. close( unix_fd );
  1474. set_error( STATUS_NO_MEMORY );
  1475. return NULL;
  1476. }
  1477. list_init( &ifchange->sockets );
  1478. if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
  1479. {
  1480. release_object( ifchange );
  1481. set_error( STATUS_NO_MEMORY );
  1482. return NULL;
  1483. }
  1484. set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
  1485. /* the ifchange object is now successfully configured */
  1486. ifchange_object = &ifchange->obj;
  1487. return &ifchange->obj;
  1488. #else
  1489. set_error( STATUS_NOT_SUPPORTED );
  1490. return NULL;
  1491. #endif
  1492. }
  1493. /* add the socket to the interface change notification list */
  1494. static void ifchange_add_sock( struct object *obj, struct sock *sock )
  1495. {
  1496. #ifdef HAVE_LINUX_RTNETLINK_H
  1497. struct ifchange *ifchange = (struct ifchange *)obj;
  1498. list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
  1499. #endif
  1500. }
  1501. /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
  1502. static struct object *sock_get_ifchange( struct sock *sock )
  1503. {
  1504. struct object *ifchange;
  1505. if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
  1506. return sock->ifchange_obj;
  1507. if (!(ifchange = get_ifchange()))
  1508. return NULL;
  1509. /* add the socket to the ifchange notification list */
  1510. ifchange_add_sock( ifchange, sock );
  1511. sock->ifchange_obj = ifchange;
  1512. return ifchange;
  1513. }
  1514. /* destroy an existing ifchange queue for a specific socket */
  1515. static void sock_release_ifchange( struct sock *sock )
  1516. {
  1517. if (sock->ifchange_obj)
  1518. {
  1519. list_remove( &sock->ifchange_entry );
  1520. release_object( sock->ifchange_obj );
  1521. sock->ifchange_obj = NULL;
  1522. }
  1523. }
  1524. static void socket_device_dump( struct object *obj, int verbose );
  1525. static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
  1526. unsigned int attr, struct object *root );
  1527. static struct object *socket_device_open_file( struct object *obj, unsigned int access,
  1528. unsigned int sharing, unsigned int options );
  1529. static const struct object_ops socket_device_ops =
  1530. {
  1531. sizeof(struct object), /* size */
  1532. &device_type, /* type */
  1533. socket_device_dump, /* dump */
  1534. no_add_queue, /* add_queue */
  1535. NULL, /* remove_queue */
  1536. NULL, /* signaled */
  1537. no_satisfied, /* satisfied */
  1538. no_signal, /* signal */
  1539. no_get_fd, /* get_fd */
  1540. default_map_access, /* map_access */
  1541. default_get_sd, /* get_sd */
  1542. default_set_sd, /* set_sd */
  1543. default_get_full_name, /* get_full_name */
  1544. socket_device_lookup_name, /* lookup_name */
  1545. directory_link_name, /* link_name */
  1546. default_unlink_name, /* unlink_name */
  1547. socket_device_open_file, /* open_file */
  1548. no_kernel_obj_list, /* get_kernel_obj_list */
  1549. no_close_handle, /* close_handle */
  1550. no_destroy /* destroy */
  1551. };
  1552. static void socket_device_dump( struct object *obj, int verbose )
  1553. {
  1554. fputs( "Socket device\n", stderr );
  1555. }
  1556. static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
  1557. unsigned int attr, struct object *root )
  1558. {
  1559. return NULL;
  1560. }
  1561. static struct object *socket_device_open_file( struct object *obj, unsigned int access,
  1562. unsigned int sharing, unsigned int options )
  1563. {
  1564. struct sock *sock;
  1565. if (!(sock = create_socket())) return NULL;
  1566. if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
  1567. {
  1568. release_object( sock );
  1569. return NULL;
  1570. }
  1571. return &sock->obj;
  1572. }
  1573. struct object *create_socket_device( struct object *root, const struct unicode_str *name,
  1574. unsigned int attr, const struct security_descriptor *sd )
  1575. {
  1576. return create_named_object( root, &socket_device_ops, name, attr, sd );
  1577. }
  1578. /* set socket event parameters */
  1579. DECL_HANDLER(set_socket_event)
  1580. {
  1581. struct sock *sock;
  1582. struct event *old_event;
  1583. if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
  1584. FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
  1585. if (get_unix_fd( sock->fd ) == -1) return;
  1586. old_event = sock->event;
  1587. sock->mask = req->mask;
  1588. sock->hmask &= ~req->mask; /* re-enable held events */
  1589. sock->event = NULL;
  1590. sock->window = req->window;
  1591. sock->message = req->msg;
  1592. sock->wparam = req->handle; /* wparam is the socket handle */
  1593. if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
  1594. if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
  1595. sock_reselect( sock );
  1596. sock->state |= FD_WINE_NONBLOCKING;
  1597. /* if a network event is pending, signal the event object
  1598. it is possible that FD_CONNECT or FD_ACCEPT network events has happened
  1599. before a WSAEventSelect() was done on it.
  1600. (when dealing with Asynchronous socket) */
  1601. sock_wake_up( sock );
  1602. if (old_event) release_object( old_event ); /* we're through with it */
  1603. release_object( &sock->obj );
  1604. }
  1605. /* get socket event parameters */
  1606. DECL_HANDLER(get_socket_event)
  1607. {
  1608. struct sock *sock;
  1609. if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
  1610. FILE_READ_ATTRIBUTES, &sock_ops ))) return;
  1611. if (get_unix_fd( sock->fd ) == -1) return;
  1612. reply->mask = sock->mask;
  1613. reply->pmask = sock->pmask;
  1614. reply->state = sock->state;
  1615. set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
  1616. if (req->service)
  1617. {
  1618. if (req->c_event)
  1619. {
  1620. struct event *cevent = get_event_obj( current->process, req->c_event,
  1621. EVENT_MODIFY_STATE );
  1622. if (cevent)
  1623. {
  1624. reset_event( cevent );
  1625. release_object( cevent );
  1626. }
  1627. }
  1628. sock->pmask = 0;
  1629. sock_reselect( sock );
  1630. }
  1631. release_object( &sock->obj );
  1632. }
  1633. /* re-enable pending socket events */
  1634. DECL_HANDLER(enable_socket_event)
  1635. {
  1636. struct sock *sock;
  1637. if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
  1638. FILE_WRITE_ATTRIBUTES, &sock_ops)))
  1639. return;
  1640. if (get_unix_fd( sock->fd ) == -1) return;
  1641. /* for event-based notification, windows erases stale events */
  1642. sock->pmask &= ~req->mask;
  1643. sock->hmask &= ~req->mask;
  1644. sock->state |= req->sstate;
  1645. sock->state &= ~req->cstate;
  1646. if (sock->type != WS_SOCK_STREAM) sock->state &= ~STREAM_FLAG_MASK;
  1647. sock_reselect( sock );
  1648. release_object( &sock->obj );
  1649. }
  1650. DECL_HANDLER(set_socket_deferred)
  1651. {
  1652. struct sock *sock, *acceptsock;
  1653. sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
  1654. if ( !sock )
  1655. return;
  1656. acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
  1657. if ( !acceptsock )
  1658. {
  1659. release_object( sock );
  1660. return;
  1661. }
  1662. sock->deferred = acceptsock;
  1663. release_object( sock );
  1664. }
  1665. DECL_HANDLER(get_socket_info)
  1666. {
  1667. struct sock *sock;
  1668. sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
  1669. if (!sock) return;
  1670. if (get_unix_fd( sock->fd ) == -1) return;
  1671. reply->family = sock->family;
  1672. reply->type = sock->type;
  1673. reply->protocol = sock->proto;
  1674. release_object( &sock->obj );
  1675. }