c5af48da0fa1ead76c88dd544e243b5e54acfe26
[jra/samba/.git] / source / 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
204         return NT_STATUS_OK;
205 }
206
207 static NTSTATUS unixdom_recv(struct socket_context *sock, void *buf, 
208                              size_t wantlen, size_t *nread, uint32_t flags)
209 {
210         ssize_t gotlen;
211         int flgs = 0;
212
213         /* TODO: we need to map all flags here */
214         if (flags & SOCKET_FLAG_PEEK) {
215                 flgs |= MSG_PEEK;
216         }
217
218         if (flags & SOCKET_FLAG_BLOCK) {
219                 flgs |= MSG_WAITALL;
220         }
221
222         *nread = 0;
223
224         gotlen = recv(sock->fd, buf, wantlen, flgs);
225         if (gotlen == 0) {
226                 return NT_STATUS_END_OF_FILE;
227         } else if (gotlen == -1) {
228                 return unixdom_error(errno);
229         }
230
231         *nread = gotlen;
232
233         return NT_STATUS_OK;
234 }
235
236 static NTSTATUS unixdom_send(struct socket_context *sock,
237                              const DATA_BLOB *blob, size_t *sendlen, uint32_t flags)
238 {
239         ssize_t len;
240         int flgs = 0;
241
242         *sendlen = 0;
243
244         len = send(sock->fd, blob->data, blob->length, flgs);
245         if (len == -1) {
246                 return unixdom_error(errno);
247         }       
248
249         *sendlen = len;
250
251         return NT_STATUS_OK;
252 }
253
254
255 static NTSTATUS unixdom_sendto(struct socket_context *sock, 
256                                const DATA_BLOB *blob, size_t *sendlen, uint32_t flags,
257                                const char *dest_addr, int dest_port)
258 {
259         ssize_t len;
260         int flgs = 0;
261         struct sockaddr_un srv_addr;
262
263         if (strlen(dest_addr)+1 > sizeof(srv_addr.sun_path)) {
264                 return NT_STATUS_OBJECT_PATH_INVALID;
265         }
266
267         ZERO_STRUCT(srv_addr);
268         srv_addr.sun_family = AF_UNIX;
269         strncpy(srv_addr.sun_path, dest_addr, sizeof(srv_addr.sun_path));
270
271         *sendlen = 0;
272
273         len = sendto(sock->fd, blob->data, blob->length, flgs, 
274                      (struct sockaddr *)&srv_addr, sizeof(srv_addr));
275         if (len == -1) {
276                 return map_nt_error_from_unix(errno);
277         }       
278
279         *sendlen = len;
280
281         return NT_STATUS_OK;
282 }
283
284
285 static NTSTATUS unixdom_set_option(struct socket_context *sock, 
286                                    const char *option, const char *val)
287 {
288         return NT_STATUS_OK;
289 }
290
291 static char *unixdom_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
292 {
293         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
294 }
295
296 static char *unixdom_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
297 {
298         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
299 }
300
301 static int unixdom_get_peer_port(struct socket_context *sock)
302 {
303         return 0;
304 }
305
306 static char *unixdom_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
307 {
308         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
309 }
310
311 static int unixdom_get_my_port(struct socket_context *sock)
312 {
313         return 0;
314 }
315
316 static int unixdom_get_fd(struct socket_context *sock)
317 {
318         return sock->fd;
319 }
320
321 static NTSTATUS unixdom_pending(struct socket_context *sock, size_t *npending)
322 {
323         int value = 0;
324         if (ioctl(sock->fd, FIONREAD, &value) == 0) {
325                 *npending = value;
326                 return NT_STATUS_OK;
327         }
328         return map_nt_error_from_unix(errno);
329 }
330
331 static const struct socket_ops unixdom_ops = {
332         .name                   = "unix",
333         .fn_init                = unixdom_init,
334         .fn_connect             = unixdom_connect,
335         .fn_connect_complete    = unixdom_connect_complete,
336         .fn_listen              = unixdom_listen,
337         .fn_accept              = unixdom_accept,
338         .fn_recv                = unixdom_recv,
339         .fn_send                = unixdom_send,
340         .fn_sendto              = unixdom_sendto,
341         .fn_close               = unixdom_close,
342         .fn_pending             = unixdom_pending,
343
344         .fn_set_option          = unixdom_set_option,
345
346         .fn_get_peer_name       = unixdom_get_peer_name,
347         .fn_get_peer_addr       = unixdom_get_peer_addr,
348         .fn_get_peer_port       = unixdom_get_peer_port,
349         .fn_get_my_addr         = unixdom_get_my_addr,
350         .fn_get_my_port         = unixdom_get_my_port,
351
352         .fn_get_fd              = unixdom_get_fd
353 };
354
355 const struct socket_ops *socket_unixdom_ops(enum socket_type type)
356 {
357         return &unixdom_ops;
358 }