1 /*
2     Copyright (c) 2013 Martin Sustrik  All rights reserved.
3 
4     Permission is hereby granted, free of charge, to any person obtaining a copy
5     of this software and associated documentation files (the "Software"),
6     to deal in the Software without restriction, including without limitation
7     the rights to use, copy, modify, merge, publish, distribute, sublicense,
8     and/or sell copies of the Software, and to permit persons to whom
9     the Software is furnished to do so, subject to the following conditions:
10 
11     The above copyright notice and this permission notice shall be included
12     in all copies or substantial portions of the Software.
13 
14     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20     IN THE SOFTWARE.
21 */
22 
23 #include "fsm.h"
24 #include "worker.h"
25 
26 #include "../utils/win.h"
27 
28 struct nn_usock {
29 
30     /*  The state machine. */
31     struct nn_fsm fsm;
32     int state;
33 
34     union {
35 
36         /*  The actual underlying socket. Can be used as a HANDLE too. */
37         SOCKET s;
38 
39         /*  Named pipe handle. Cannot be used as a SOCKET. */
40         HANDLE p;
41     };
42 
43     /*  For NamedPipes, closing an accepted pipe differs from other pipes.
44         If the NamedPipe was accepted, this member is set to 1. 0 otherwise. */
45     int isaccepted;
46 
47     /*  Asynchronous operations being executed on the socket. */
48     struct nn_worker_op in;
49     struct nn_worker_op out;
50 
51     /*  When accepting new socket, they have to be created with same
52         type as the listening socket. Thus, in listening socket we
53         have to store its exact type. */
54     int domain;
55     int type;
56     int protocol;
57 
58     /*  Events raised by the usock. */
59     struct nn_fsm_event event_established;
60     struct nn_fsm_event event_sent;
61     struct nn_fsm_event event_received;
62     struct nn_fsm_event event_error;
63 
64     /*  In ACCEPTING state points to the socket being accepted.
65         In BEING_ACCEPTED state points to the listener socket. */
66     struct nn_usock *asock;
67 
68     /*  Buffer allocated for output of AcceptEx function. If accepting is not
69         done on this socket, the field is set to NULL. */
70     void *ainfo;
71 
72     /*  For NamedPipes, we store the address inside the socket. */
73     struct sockaddr_un pipename;
74 
75     /*  For now we allocate a new buffer for each write to a named pipe. */
76     void *pipesendbuf;
77 
78     /* Pointer to the security attribute structure */
79     SECURITY_ATTRIBUTES *sec_attr;
80 
81     /* Out Buffer and In Buffer size */
82     int outbuffersz;
83     int inbuffersz;
84 
85     /*  Errno remembered in NN_USOCK_ERROR state  */
86     int errnum;
87 };
88