xref: /minix/minix/net/uds/uds.h (revision e3b78ef1)
1 #ifndef __UDS_UDS_H
2 #define __UDS_UDS_H
3 
4 #include <minix/drivers.h>
5 #include <minix/chardriver.h>
6 #undef send
7 #include <sys/socket.h>
8 #include <sys/ioctl.h>
9 #include <sys/ucred.h>
10 #include <sys/un.h>
11 #include <sys/mman.h>
12 
13 /* Maximum number of UNIX domain sockets. */
14 #define NR_FDS		256
15 
16 /* Connection backlog size for incoming connections. */
17 #define UDS_SOMAXCONN	64
18 
19 /* Maximum UDS socket buffer size. */
20 #define UDS_BUF		PIPE_BUF
21 
22 /* Output debugging information? */
23 #define DEBUG		0
24 
25 #if DEBUG
26 #define dprintf(x)	printf x
27 #else
28 #define dprintf(x)
29 #endif
30 
31 /* ancillary data to be sent */
32 struct ancillary {
33 	int fds[OPEN_MAX];
34 	int nfiledes;
35 	struct uucred cred;
36 };
37 
38 #define UDS_R	0x1
39 #define UDS_W	0x2
40 
41 /*
42  * Internal State Information for a socket descriptor.
43  */
44 struct uds_fd {
45 
46 /* Flags */
47 
48 	enum UDS_STATE {
49 		/* This file descriptor is UDS_FREE and can be allocated. */
50 		UDS_FREE  = 0,
51 
52 		/* OR it is UDS_INUSE and can't be allocated. */
53 		UDS_INUSE = 1
54 
55 	/* state is set to UDS_INUSE in uds_open(). state is Set to
56 	 * UDS_FREE in uds_init() and uds_close(). state should be
57 	 * checked prior to all operations.
58 	 */
59 	} state;
60 
61 /* Owner Info */
62 
63 	/* Socket Owner */
64 	endpoint_t owner;
65 
66 /* Pipe Housekeeping */
67 
68 	char *buf;			/* ring buffer */
69 	size_t pos;			/* tail position into ring buffer */
70 	size_t size;			/* size of used part of ring buffer */
71 
72 	/* control read/write, set by uds_open() and shutdown(2).
73 	 * Can be set to UDS_R|UDS_W, UDS_R, UDS_W, or 0
74 	 * for read and write, read only, write only, or neither.
75 	 * default is UDS_R|UDS_W.
76 	 */
77 	int mode;
78 
79 /* Socket Info */
80 
81 	/* socket type - SOCK_STREAM, SOCK_DGRAM, or SOCK_SEQPACKET
82 	 * Set by uds_ioctl(NWIOSUDSTYPE). It defaults to -1 in
83 	 * uds_open(). Any action on a socket with type -1 besides
84 	 * uds_ioctl(NWIOSUDSTYPE) and uds_close() will result in
85 	 * an error.
86 	 */
87 	int type;
88 
89 	/* queue of pending connections for server sockets.
90 	 * connect(2) inserts and accept(2) removes from the queue
91 	 */
92 	int backlog[UDS_SOMAXCONN];
93 
94 	/* requested connection backlog size. Set by listen(2)
95 	 * Bounds (0 <= backlog_size <= UDS_SOMAXCONN)
96 	 * Defaults to UDS_SOMAXCONN which is defined above.
97 	 */
98 	unsigned char backlog_size;
99 
100 	/* index of peer in uds_fd_table for connected sockets.
101 	 * -1 is used to mean no peer. Assumptions: peer != -1 means
102 	 * connected.
103 	 */
104 	int peer;
105 
106 	/* index of child (client sd returned by accept(2))
107 	 * -1 is used to mean no child.
108 	 */
109 	int child;
110 
111 	/* address -- the address the socket is bound to.
112 	 * Assumptions: addr.sun_family == AF_UNIX means its bound.
113 	 */
114 	struct sockaddr_un addr;
115 
116 	/* target -- where DGRAMs are sent to on the next uds_write(). */
117 	struct sockaddr_un target;
118 
119 	/* source -- address where DGRAMs are from. used to fill in the
120 	 * from address in recvfrom(2) and recvmsg(2).
121 	 */
122 	struct sockaddr_un source;
123 
124 	/* Flag (1 or 0) - listening for incoming connections.
125 	 * Default to 0. Set to 1 by do_listen()
126 	 */
127 	int listening;
128 
129 	/* stores file pointers and credentials being sent between
130 	 * processes with sendmsg(2) and recvmsg(2).
131 	 */
132 	struct ancillary ancillary_data;
133 
134 	/* Holds an errno. This is set when a connected socket is
135 	 * closed and we need to pass ECONNRESET on to a suspended
136 	 * peer.
137 	 */
138 	int err;
139 
140 /* Suspend/Revive Housekeeping */
141 
142 	/* SUSPEND State Flags */
143 	enum UDS_SUSPENDED {
144 
145 		/* Socket isn't blocked. */
146 		UDS_NOT_SUSPENDED     = 0,
147 
148 		/* Socket is blocked on read(2) waiting for data to read. */
149 		UDS_SUSPENDED_READ    = 1,
150 
151 		/* Socket is blocked on write(2) for space to write data. */
152 		UDS_SUSPENDED_WRITE   = 2,
153 
154 		/* Socket is blocked on connect(2) waiting for the server. */
155 		UDS_SUSPENDED_CONNECT = 4,
156 
157 		/* Socket is blocked on accept(2) waiting for clients. */
158 		UDS_SUSPENDED_ACCEPT  = 8
159 	} suspended;
160 
161 	/* source endpoint, saved for later use by suspended procs */
162 	endpoint_t susp_endpt;
163 
164 	/* i/o grant, saved for later use by suspended procs */
165 	cp_grant_id_t susp_grant;
166 
167 	/* size of request, saved for later use by suspended procs */
168 	size_t susp_size;
169 
170 	/* request ID, saved for later use by suspended procs */
171 	cdev_id_t susp_id;
172 
173 /* select() */
174 
175 	/* when a select is in progress, we notify this endpoint
176 	 * of new data.
177 	 */
178 	endpoint_t sel_endpt;
179 
180 	/* Options (CDEV_OP_RD,WR,ERR) that are requested. */
181 	unsigned int sel_ops;
182 };
183 
184 typedef struct uds_fd uds_fd_t;
185 
186 /* File Descriptor Table -- Defined in uds.c */
187 EXTERN uds_fd_t uds_fd_table[NR_FDS];
188 
189 /* Function prototypes. */
190 
191 /* ioc_uds.c */
192 int uds_clear_fds(devminor_t minor, struct ancillary *data);
193 int uds_do_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
194 	cp_grant_id_t grant);
195 
196 /* uds.c */
197 ssize_t uds_perform_read(devminor_t minor, endpoint_t endpt,
198 	cp_grant_id_t grant, size_t size, int pretend);
199 void uds_unsuspend(devminor_t minor);
200 
201 #endif /* !__UDS_UDS_H */
202