r3278: - rewrote the client side rpc connection code to use lib/socket/
[mat/samba.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
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
26
27
28 /*
29   approximate errno mapping
30 */
31 static NTSTATUS unixdom_error(int ernum)
32 {
33         return map_nt_error_from_unix(ernum);
34 }
35
36 static NTSTATUS unixdom_init(struct socket_context *sock)
37 {
38         sock->fd = socket(PF_UNIX, SOCK_STREAM, 0);
39         if (sock->fd == -1) {
40                 return NT_STATUS_INSUFFICIENT_RESOURCES;
41         }
42         sock->private_data = NULL;
43
44         return NT_STATUS_OK;
45 }
46
47 static void unixdom_close(struct socket_context *sock)
48 {
49         close(sock->fd);
50 }
51
52 static NTSTATUS unixdom_connect(struct socket_context *sock,
53                                 const char *my_address, int my_port,
54                                 const char *srv_address, int srv_port,
55                                 uint32_t flags)
56 {
57         struct sockaddr_un srv_addr;
58         int ret;
59
60         if (strlen(srv_address)+1 > sizeof(srv_addr.sun_path)) {
61                 return NT_STATUS_INVALID_PARAMETER;
62         }
63
64         ZERO_STRUCT(srv_addr);
65         srv_addr.sun_family = AF_UNIX;
66         strncpy(srv_addr.sun_path, srv_address, sizeof(srv_addr.sun_path));
67
68         if (!(flags & SOCKET_FLAG_BLOCK)) {
69                 ret = set_blocking(sock->fd, False);
70                 if (ret == -1) {
71                         return NT_STATUS_INVALID_PARAMETER;
72                 }
73         }
74
75         ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
76         if (ret == -1) {
77                 return unixdom_error(errno);
78         }
79
80         sock->state = SOCKET_STATE_CLIENT_CONNECTED;
81
82         return NT_STATUS_OK;
83 }
84
85 static NTSTATUS unixdom_listen(struct socket_context *sock,
86                                const char *my_address, int port,
87                                int queue_size, uint32_t flags)
88 {
89         struct sockaddr_un my_addr;
90         int ret;
91
92         if (strlen(my_address)+1 > sizeof(my_addr.sun_path)) {
93                 return NT_STATUS_INVALID_PARAMETER;
94         }
95
96         /* delete if it already exists */
97         unlink(my_address);
98
99         ZERO_STRUCT(my_addr);
100         my_addr.sun_family = AF_UNIX;
101         strncpy(my_addr.sun_path, my_address, sizeof(my_addr.sun_path));
102
103         ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
104         if (ret == -1) {
105                 return unixdom_error(errno);
106         }
107
108         ret = listen(sock->fd, queue_size);
109         if (ret == -1) {
110                 return unixdom_error(errno);
111         }
112
113         if (!(flags & SOCKET_FLAG_BLOCK)) {
114                 ret = set_blocking(sock->fd, False);
115                 if (ret == -1) {
116                         return unixdom_error(errno);
117                 }
118         }
119
120         sock->state = SOCKET_STATE_SERVER_LISTEN;
121         sock->private_data = (void *)talloc_strdup(sock, my_address);
122
123         return NT_STATUS_OK;
124 }
125
126 static NTSTATUS unixdom_accept(struct socket_context *sock, 
127                                struct socket_context **new_sock, 
128                                uint32_t flags)
129 {
130         struct sockaddr_un cli_addr;
131         socklen_t cli_addr_len = sizeof(cli_addr);
132         int new_fd;
133
134         new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
135         if (new_fd == -1) {
136                 return unixdom_error(errno);
137         }
138
139         (*new_sock) = talloc_p(NULL, struct socket_context);
140         if (!(*new_sock)) {
141                 close(new_fd);
142                 return NT_STATUS_NO_MEMORY;
143         }
144
145         /* copy the socket_context */
146         (*new_sock)->type               = sock->type;
147         (*new_sock)->state              = SOCKET_STATE_SERVER_CONNECTED;
148         (*new_sock)->flags              = flags;
149
150         (*new_sock)->fd                 = new_fd;
151
152         (*new_sock)->private_data       = NULL;
153         (*new_sock)->ops                = sock->ops;
154
155         return NT_STATUS_OK;
156 }
157
158 static NTSTATUS unixdom_recv(struct socket_context *sock, TALLOC_CTX *mem_ctx,
159                              DATA_BLOB *blob, size_t wantlen, uint32_t flags)
160 {
161         ssize_t gotlen;
162         void *buf;
163         int flgs = 0;
164
165         buf = talloc(mem_ctx, wantlen);
166         if (!buf) {
167                 return NT_STATUS_NO_MEMORY;
168         }
169
170         /* TODO: we need to map all flags here */
171         if (flags & SOCKET_FLAG_PEEK) {
172                 flgs |= MSG_PEEK;
173         }
174
175         if (!(flags & SOCKET_FLAG_BLOCK)) {
176                 flgs |= MSG_DONTWAIT;
177         }
178
179         if (flags & SOCKET_FLAG_BLOCK) {
180                 flgs |= MSG_WAITALL;
181         }
182
183         gotlen = recv(sock->fd, buf, wantlen, flgs);
184         if (gotlen == 0) {
185                 talloc_free(buf);
186                 return NT_STATUS_END_OF_FILE;
187         } else if (gotlen == -1) {
188                 NTSTATUS status = unixdom_error(errno);
189                 talloc_free(buf);
190                 return status;
191         }
192
193         blob->length = gotlen;
194         blob->data = talloc_realloc(mem_ctx, buf, gotlen);
195         if (!blob->data) {
196                 return NT_STATUS_NO_MEMORY;
197         }
198
199         return NT_STATUS_OK;
200 }
201
202 static NTSTATUS unixdom_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
203                              const DATA_BLOB *blob, size_t *sendlen, uint32_t flags)
204 {
205         ssize_t len;
206         int flgs = 0;
207
208         *sendlen = 0;
209
210         /* TODO: we need to map all flags here */
211         if (!(flags & SOCKET_FLAG_BLOCK)) {
212                 flgs |= MSG_DONTWAIT;
213         }
214
215         len = send(sock->fd, blob->data, blob->length, flgs);
216         if (len == -1) {
217                 return unixdom_error(errno);
218         }       
219
220         *sendlen = len;
221
222         return NT_STATUS_OK;
223 }
224
225 static NTSTATUS unixdom_set_option(struct socket_context *sock, 
226                                    const char *option, const char *val)
227 {
228         return NT_STATUS_OK;
229 }
230
231 static char *unixdom_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
232 {
233         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
234 }
235
236 static char *unixdom_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
237 {
238         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
239 }
240
241 static int unixdom_get_peer_port(struct socket_context *sock)
242 {
243         return 0;
244 }
245
246 static char *unixdom_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
247 {
248         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
249 }
250
251 static int unixdom_get_my_port(struct socket_context *sock)
252 {
253         return 0;
254 }
255
256 static int unixdom_get_fd(struct socket_context *sock)
257 {
258         return sock->fd;
259 }
260
261 static const struct socket_ops unixdom_ops = {
262         .name           = "unix",
263         .type           = SOCKET_TYPE_STREAM,
264
265         .init           = unixdom_init,
266         .connect        = unixdom_connect,
267         .listen         = unixdom_listen,
268         .accept         = unixdom_accept,
269         .recv           = unixdom_recv,
270         .send           = unixdom_send,
271         .close          = unixdom_close,
272
273         .set_option     = unixdom_set_option,
274
275         .get_peer_name  = unixdom_get_peer_name,
276         .get_peer_addr  = unixdom_get_peer_addr,
277         .get_peer_port  = unixdom_get_peer_port,
278         .get_my_addr    = unixdom_get_my_addr,
279         .get_my_port    = unixdom_get_my_port,
280
281         .get_fd         = unixdom_get_fd
282 };
283
284 const struct socket_ops *socket_unixdom_ops(void)
285 {
286         return &unixdom_ops;
287 }
288
289 NTSTATUS socket_unixdom_init(void)
290 {
291         return NT_STATUS_OK;
292 }