source4/lib: Fix prototypes for all functions.
[Samba/gbeck.git] / source4 / lib / socket / socket_unix.c
blobf1fa0a3a30c42d6c232bf5a206c8541cb08e405d
1 /*
2 Unix SMB/CIFS implementation.
4 unix domain socket functions
6 Copyright (C) Stefan Metzmacher 2004
7 Copyright (C) Andrew Tridgell 2004-2005
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 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "lib/socket/socket.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
28 _PUBLIC_ const struct socket_ops *socket_unixdom_ops(enum socket_type type);
32 approximate errno mapping
34 static NTSTATUS unixdom_error(int ernum)
36 return map_nt_error_from_unix(ernum);
39 static NTSTATUS unixdom_init(struct socket_context *sock)
41 int type;
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;
54 sock->fd = socket(PF_UNIX, type, 0);
55 if (sock->fd == -1) {
56 return map_nt_error_from_unix(errno);
58 sock->private_data = NULL;
60 sock->backend_name = "unix";
62 return NT_STATUS_OK;
65 static void unixdom_close(struct socket_context *sock)
67 close(sock->fd);
70 static NTSTATUS unixdom_connect_complete(struct socket_context *sock, uint32_t flags)
72 int error=0, ret;
73 socklen_t len = sizeof(error);
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);
81 if (error != 0) {
82 return map_nt_error_from_unix(error);
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);
92 sock->state = SOCKET_STATE_CLIENT_CONNECTED;
94 return NT_STATUS_OK;
97 static NTSTATUS unixdom_connect(struct socket_context *sock,
98 const struct socket_address *my_address,
99 const struct socket_address *srv_address,
100 uint32_t flags)
102 int ret;
104 if (srv_address->sockaddr) {
105 ret = connect(sock->fd, srv_address->sockaddr, srv_address->sockaddrlen);
106 } else {
107 struct sockaddr_un srv_addr;
108 if (strlen(srv_address->addr)+1 > sizeof(srv_addr.sun_path)) {
109 return NT_STATUS_OBJECT_PATH_INVALID;
112 ZERO_STRUCT(srv_addr);
113 srv_addr.sun_family = AF_UNIX;
114 strncpy(srv_addr.sun_path, srv_address->addr, sizeof(srv_addr.sun_path));
116 ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
118 if (ret == -1) {
119 return unixdom_error(errno);
122 return unixdom_connect_complete(sock, flags);
125 static NTSTATUS unixdom_listen(struct socket_context *sock,
126 const struct socket_address *my_address,
127 int queue_size, uint32_t flags)
129 struct sockaddr_un my_addr;
130 int ret;
132 /* delete if it already exists */
133 if (my_address->addr) {
134 unlink(my_address->addr);
137 if (my_address->sockaddr) {
138 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
139 } else if (my_address->addr == NULL) {
140 return NT_STATUS_INVALID_PARAMETER;
141 } else {
142 if (strlen(my_address->addr)+1 > sizeof(my_addr.sun_path)) {
143 return NT_STATUS_OBJECT_PATH_INVALID;
147 ZERO_STRUCT(my_addr);
148 my_addr.sun_family = AF_UNIX;
149 strncpy(my_addr.sun_path, my_address->addr, sizeof(my_addr.sun_path));
151 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
153 if (ret == -1) {
154 return unixdom_error(errno);
157 if (sock->type == SOCKET_TYPE_STREAM) {
158 ret = listen(sock->fd, queue_size);
159 if (ret == -1) {
160 return unixdom_error(errno);
164 if (!(flags & SOCKET_FLAG_BLOCK)) {
165 ret = set_blocking(sock->fd, false);
166 if (ret == -1) {
167 return unixdom_error(errno);
171 sock->state = SOCKET_STATE_SERVER_LISTEN;
172 sock->private_data = (void *)talloc_strdup(sock, my_address->addr);
174 return NT_STATUS_OK;
177 static NTSTATUS unixdom_accept(struct socket_context *sock,
178 struct socket_context **new_sock)
180 struct sockaddr_un cli_addr;
181 socklen_t cli_addr_len = sizeof(cli_addr);
182 int new_fd;
184 if (sock->type != SOCKET_TYPE_STREAM) {
185 return NT_STATUS_INVALID_PARAMETER;
188 new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
189 if (new_fd == -1) {
190 return unixdom_error(errno);
193 if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
194 int ret = set_blocking(new_fd, false);
195 if (ret == -1) {
196 close(new_fd);
197 return map_nt_error_from_unix(errno);
201 (*new_sock) = talloc(NULL, struct socket_context);
202 if (!(*new_sock)) {
203 close(new_fd);
204 return NT_STATUS_NO_MEMORY;
207 /* copy the socket_context */
208 (*new_sock)->type = sock->type;
209 (*new_sock)->state = SOCKET_STATE_SERVER_CONNECTED;
210 (*new_sock)->flags = sock->flags;
212 (*new_sock)->fd = new_fd;
214 (*new_sock)->private_data = NULL;
215 (*new_sock)->ops = sock->ops;
216 (*new_sock)->backend_name = sock->backend_name;
218 return NT_STATUS_OK;
221 static NTSTATUS unixdom_recv(struct socket_context *sock, void *buf,
222 size_t wantlen, size_t *nread)
224 ssize_t gotlen;
226 *nread = 0;
228 gotlen = recv(sock->fd, buf, wantlen, 0);
229 if (gotlen == 0) {
230 return NT_STATUS_END_OF_FILE;
231 } else if (gotlen == -1) {
232 return unixdom_error(errno);
235 *nread = gotlen;
237 return NT_STATUS_OK;
240 static NTSTATUS unixdom_send(struct socket_context *sock,
241 const DATA_BLOB *blob, size_t *sendlen)
243 ssize_t len;
245 *sendlen = 0;
247 len = send(sock->fd, blob->data, blob->length, 0);
248 if (len == -1) {
249 return unixdom_error(errno);
252 *sendlen = len;
254 return NT_STATUS_OK;
258 static NTSTATUS unixdom_sendto(struct socket_context *sock,
259 const DATA_BLOB *blob, size_t *sendlen,
260 const struct socket_address *dest)
262 ssize_t len;
263 *sendlen = 0;
265 if (dest->sockaddr) {
266 len = sendto(sock->fd, blob->data, blob->length, 0,
267 dest->sockaddr, dest->sockaddrlen);
268 } else {
269 struct sockaddr_un srv_addr;
271 if (strlen(dest->addr)+1 > sizeof(srv_addr.sun_path)) {
272 return NT_STATUS_OBJECT_PATH_INVALID;
275 ZERO_STRUCT(srv_addr);
276 srv_addr.sun_family = AF_UNIX;
277 strncpy(srv_addr.sun_path, dest->addr, sizeof(srv_addr.sun_path));
279 len = sendto(sock->fd, blob->data, blob->length, 0,
280 (struct sockaddr *)&srv_addr, sizeof(srv_addr));
282 if (len == -1) {
283 return map_nt_error_from_unix(errno);
286 *sendlen = len;
288 return NT_STATUS_OK;
292 static NTSTATUS unixdom_set_option(struct socket_context *sock,
293 const char *option, const char *val)
295 return NT_STATUS_OK;
298 static char *unixdom_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
300 return talloc_strdup(mem_ctx, "LOCAL/unixdom");
303 static struct socket_address *unixdom_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
305 struct sockaddr_in *peer_addr;
306 socklen_t len = sizeof(*peer_addr);
307 struct socket_address *peer;
308 int ret;
310 peer = talloc(mem_ctx, struct socket_address);
311 if (!peer) {
312 return NULL;
315 peer->family = sock->backend_name;
316 peer_addr = talloc(peer, struct sockaddr_in);
317 if (!peer_addr) {
318 talloc_free(peer);
319 return NULL;
322 peer->sockaddr = (struct sockaddr *)peer_addr;
324 ret = getpeername(sock->fd, peer->sockaddr, &len);
325 if (ret == -1) {
326 talloc_free(peer);
327 return NULL;
330 peer->sockaddrlen = len;
332 peer->port = 0;
333 peer->addr = talloc_strdup(peer, "LOCAL/unixdom");
334 if (!peer->addr) {
335 talloc_free(peer);
336 return NULL;
339 return peer;
342 static struct socket_address *unixdom_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
344 struct sockaddr_in *local_addr;
345 socklen_t len = sizeof(*local_addr);
346 struct socket_address *local;
347 int ret;
349 local = talloc(mem_ctx, struct socket_address);
350 if (!local) {
351 return NULL;
354 local->family = sock->backend_name;
355 local_addr = talloc(local, struct sockaddr_in);
356 if (!local_addr) {
357 talloc_free(local);
358 return NULL;
361 local->sockaddr = (struct sockaddr *)local_addr;
363 ret = getsockname(sock->fd, local->sockaddr, &len);
364 if (ret == -1) {
365 talloc_free(local);
366 return NULL;
369 local->sockaddrlen = len;
371 local->port = 0;
372 local->addr = talloc_strdup(local, "LOCAL/unixdom");
373 if (!local->addr) {
374 talloc_free(local);
375 return NULL;
378 return local;
381 static int unixdom_get_fd(struct socket_context *sock)
383 return sock->fd;
386 static NTSTATUS unixdom_pending(struct socket_context *sock, size_t *npending)
388 int value = 0;
389 if (ioctl(sock->fd, FIONREAD, &value) == 0) {
390 *npending = value;
391 return NT_STATUS_OK;
393 return map_nt_error_from_unix(errno);
396 static const struct socket_ops unixdom_ops = {
397 .name = "unix",
398 .fn_init = unixdom_init,
399 .fn_connect = unixdom_connect,
400 .fn_connect_complete = unixdom_connect_complete,
401 .fn_listen = unixdom_listen,
402 .fn_accept = unixdom_accept,
403 .fn_recv = unixdom_recv,
404 .fn_send = unixdom_send,
405 .fn_sendto = unixdom_sendto,
406 .fn_close = unixdom_close,
407 .fn_pending = unixdom_pending,
409 .fn_set_option = unixdom_set_option,
411 .fn_get_peer_name = unixdom_get_peer_name,
412 .fn_get_peer_addr = unixdom_get_peer_addr,
413 .fn_get_my_addr = unixdom_get_my_addr,
415 .fn_get_fd = unixdom_get_fd
418 _PUBLIC_ const struct socket_ops *socket_unixdom_ops(enum socket_type type)
420 return &unixdom_ops;