r9705: r9685@blu: tridge | 2005-08-27 19:43:44 +1000
[ira/wip.git] / source4 / lib / socket / socket_unix.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    unix domain socket functions
5
6    Copyright (C) Stefan Metzmacher 2004
7    Copyright (C) Andrew Tridgell 2004-2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "lib/socket/socket.h"
26 #include "system/network.h"
27 #include "system/filesys.h"
28
29
30
31 /*
32   approximate errno mapping
33 */
34 static NTSTATUS unixdom_error(int ernum)
35 {
36         return map_nt_error_from_unix(ernum);
37 }
38
39 static NTSTATUS unixdom_init(struct socket_context *sock)
40 {
41         int type;
42
43         switch (sock->type) {
44         case SOCKET_TYPE_STREAM:
45                 type = SOCK_STREAM;
46                 break;
47         case SOCKET_TYPE_DGRAM:
48                 type = SOCK_DGRAM;
49                 break;
50         default:
51                 return NT_STATUS_INVALID_PARAMETER;
52         }
53
54         sock->fd = socket(PF_UNIX, type, 0);
55         if (sock->fd == -1) {
56                 return map_nt_error_from_unix(errno);
57         }
58         sock->private_data = NULL;
59
60         sock->backend_name = "unix";
61
62         return NT_STATUS_OK;
63 }
64
65 static void unixdom_close(struct socket_context *sock)
66 {
67         close(sock->fd);
68 }
69
70 static NTSTATUS unixdom_connect_complete(struct socket_context *sock, uint32_t flags)
71 {
72         int error=0, ret;
73         socklen_t len = sizeof(error);
74
75         /* check for any errors that may have occurred - this is needed
76            for non-blocking connect */
77         ret = getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, &error, &len);
78         if (ret == -1) {
79                 return map_nt_error_from_unix(errno);
80         }
81         if (error != 0) {
82                 return map_nt_error_from_unix(error);
83         }
84
85         if (!(flags & SOCKET_FLAG_BLOCK)) {
86                 ret = set_blocking(sock->fd, False);
87                 if (ret == -1) {
88                         return map_nt_error_from_unix(errno);
89                 }
90         }
91
92         sock->state = SOCKET_STATE_CLIENT_CONNECTED;
93
94         return NT_STATUS_OK;
95 }
96
97 static NTSTATUS unixdom_connect(struct socket_context *sock,
98                                 const char *my_address, int my_port,
99                                 const char *srv_address, int srv_port,
100                                 uint32_t flags)
101 {
102         struct sockaddr_un srv_addr;
103         int ret;
104
105         if (strlen(srv_address)+1 > sizeof(srv_addr.sun_path)) {
106                 return NT_STATUS_OBJECT_PATH_INVALID;
107         }
108
109         ZERO_STRUCT(srv_addr);
110         srv_addr.sun_family = AF_UNIX;
111         strncpy(srv_addr.sun_path, srv_address, sizeof(srv_addr.sun_path));
112
113         ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
114         if (ret == -1) {
115                 return unixdom_error(errno);
116         }
117
118         return unixdom_connect_complete(sock, flags);
119 }
120
121 static NTSTATUS unixdom_listen(struct socket_context *sock,
122                                const char *my_address, int port,
123                                int queue_size, uint32_t flags)
124 {
125         struct sockaddr_un my_addr;
126         int ret;
127
128         if (strlen(my_address)+1 > sizeof(my_addr.sun_path)) {
129                 return NT_STATUS_OBJECT_PATH_INVALID;
130         }
131
132         /* delete if it already exists */
133         unlink(my_address);
134
135         ZERO_STRUCT(my_addr);
136         my_addr.sun_family = AF_UNIX;
137         strncpy(my_addr.sun_path, my_address, sizeof(my_addr.sun_path));
138
139         ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
140         if (ret == -1) {
141                 return unixdom_error(errno);
142         }
143
144         if (sock->type == SOCKET_TYPE_STREAM) {
145                 ret = listen(sock->fd, queue_size);
146                 if (ret == -1) {
147                         return unixdom_error(errno);
148                 }
149         }
150
151         if (!(flags & SOCKET_FLAG_BLOCK)) {
152                 ret = set_blocking(sock->fd, False);
153                 if (ret == -1) {
154                         return unixdom_error(errno);
155                 }
156         }
157
158         sock->state = SOCKET_STATE_SERVER_LISTEN;
159         sock->private_data = (void *)talloc_strdup(sock, my_address);
160
161         return NT_STATUS_OK;
162 }
163
164 static NTSTATUS unixdom_accept(struct socket_context *sock, 
165                                struct socket_context **new_sock)
166 {
167         struct sockaddr_un cli_addr;
168         socklen_t cli_addr_len = sizeof(cli_addr);
169         int new_fd;
170
171         if (sock->type != SOCKET_TYPE_STREAM) {
172                 return NT_STATUS_INVALID_PARAMETER;
173         }
174
175         new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
176         if (new_fd == -1) {
177                 return unixdom_error(errno);
178         }
179
180         if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
181                 int ret = set_blocking(new_fd, False);
182                 if (ret == -1) {
183                         close(new_fd);
184                         return map_nt_error_from_unix(errno);
185                 }
186         }
187
188         (*new_sock) = talloc(NULL, struct socket_context);
189         if (!(*new_sock)) {
190                 close(new_fd);
191                 return NT_STATUS_NO_MEMORY;
192         }
193
194         /* copy the socket_context */
195         (*new_sock)->type               = sock->type;
196         (*new_sock)->state              = SOCKET_STATE_SERVER_CONNECTED;
197         (*new_sock)->flags              = sock->flags;
198
199         (*new_sock)->fd                 = new_fd;
200
201         (*new_sock)->private_data       = NULL;
202         (*new_sock)->ops                = sock->ops;
203         (*new_sock)->backend_name       = sock->backend_name;
204
205         return NT_STATUS_OK;
206 }
207
208 static NTSTATUS unixdom_recv(struct socket_context *sock, void *buf, 
209                              size_t wantlen, size_t *nread, uint32_t flags)
210 {
211         ssize_t gotlen;
212         int flgs = 0;
213
214         /* TODO: we need to map all flags here */
215         if (flags & SOCKET_FLAG_PEEK) {
216                 flgs |= MSG_PEEK;
217         }
218
219         if (flags & SOCKET_FLAG_BLOCK) {
220                 flgs |= MSG_WAITALL;
221         }
222
223         *nread = 0;
224
225         gotlen = recv(sock->fd, buf, wantlen, flgs);
226         if (gotlen == 0) {
227                 return NT_STATUS_END_OF_FILE;
228         } else if (gotlen == -1) {
229                 return unixdom_error(errno);
230         }
231
232         *nread = gotlen;
233
234         return NT_STATUS_OK;
235 }
236
237 static NTSTATUS unixdom_send(struct socket_context *sock,
238                              const DATA_BLOB *blob, size_t *sendlen, uint32_t flags)
239 {
240         ssize_t len;
241         int flgs = 0;
242
243         *sendlen = 0;
244
245         len = send(sock->fd, blob->data, blob->length, flgs);
246         if (len == -1) {
247                 return unixdom_error(errno);
248         }       
249
250         *sendlen = len;
251
252         return NT_STATUS_OK;
253 }
254
255
256 static NTSTATUS unixdom_sendto(struct socket_context *sock, 
257                                const DATA_BLOB *blob, size_t *sendlen, uint32_t flags,
258                                const char *dest_addr, int dest_port)
259 {
260         ssize_t len;
261         int flgs = 0;
262         struct sockaddr_un srv_addr;
263
264         if (strlen(dest_addr)+1 > sizeof(srv_addr.sun_path)) {
265                 return NT_STATUS_OBJECT_PATH_INVALID;
266         }
267
268         ZERO_STRUCT(srv_addr);
269         srv_addr.sun_family = AF_UNIX;
270         strncpy(srv_addr.sun_path, dest_addr, sizeof(srv_addr.sun_path));
271
272         *sendlen = 0;
273
274         len = sendto(sock->fd, blob->data, blob->length, flgs, 
275                      (struct sockaddr *)&srv_addr, sizeof(srv_addr));
276         if (len == -1) {
277                 return map_nt_error_from_unix(errno);
278         }       
279
280         *sendlen = len;
281
282         return NT_STATUS_OK;
283 }
284
285
286 static NTSTATUS unixdom_set_option(struct socket_context *sock, 
287                                    const char *option, const char *val)
288 {
289         return NT_STATUS_OK;
290 }
291
292 static char *unixdom_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
293 {
294         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
295 }
296
297 static char *unixdom_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
298 {
299         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
300 }
301
302 static int unixdom_get_peer_port(struct socket_context *sock)
303 {
304         return 0;
305 }
306
307 static char *unixdom_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
308 {
309         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
310 }
311
312 static int unixdom_get_my_port(struct socket_context *sock)
313 {
314         return 0;
315 }
316
317 static int unixdom_get_fd(struct socket_context *sock)
318 {
319         return sock->fd;
320 }
321
322 static NTSTATUS unixdom_pending(struct socket_context *sock, size_t *npending)
323 {
324         int value = 0;
325         if (ioctl(sock->fd, FIONREAD, &value) == 0) {
326                 *npending = value;
327                 return NT_STATUS_OK;
328         }
329         return map_nt_error_from_unix(errno);
330 }
331
332 static const struct socket_ops unixdom_ops = {
333         .name                   = "unix",
334         .fn_init                = unixdom_init,
335         .fn_connect             = unixdom_connect,
336         .fn_connect_complete    = unixdom_connect_complete,
337         .fn_listen              = unixdom_listen,
338         .fn_accept              = unixdom_accept,
339         .fn_recv                = unixdom_recv,
340         .fn_send                = unixdom_send,
341         .fn_sendto              = unixdom_sendto,
342         .fn_close               = unixdom_close,
343         .fn_pending             = unixdom_pending,
344
345         .fn_set_option          = unixdom_set_option,
346
347         .fn_get_peer_name       = unixdom_get_peer_name,
348         .fn_get_peer_addr       = unixdom_get_peer_addr,
349         .fn_get_peer_port       = unixdom_get_peer_port,
350         .fn_get_my_addr         = unixdom_get_my_addr,
351         .fn_get_my_port         = unixdom_get_my_port,
352
353         .fn_get_fd              = unixdom_get_fd
354 };
355
356 const struct socket_ops *socket_unixdom_ops(enum socket_type type)
357 {
358         return &unixdom_ops;
359 }