r3313: in socket_accept() make the new socket non-blocking unless SOCKET_FLAG_BLOCK...
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket / socket_ipv4.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Socket IPv4 functions
4    Copyright (C) Stefan Metzmacher 2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 static NTSTATUS ipv4_tcp_init(struct socket_context *sock)
24 {
25         sock->fd = socket(PF_INET, SOCK_STREAM, 0);
26         if (sock->fd == -1) {
27                 return map_nt_error_from_unix(errno);
28         }
29
30         return NT_STATUS_OK;
31 }
32
33 static void ipv4_tcp_close(struct socket_context *sock)
34 {
35         close(sock->fd);
36 }
37
38 static NTSTATUS ipv4_tcp_connect(struct socket_context *sock,
39                                  const char *my_address, int my_port,
40                                  const char *srv_address, int srv_port,
41                                  uint32_t flags)
42 {
43         struct sockaddr_in srv_addr;
44         struct in_addr my_ip;
45         struct in_addr srv_ip;
46         int ret;
47
48         my_ip = interpret_addr2(my_address);
49
50         if (my_ip.s_addr != 0 || my_port != 0) {
51                 struct sockaddr_in my_addr;
52                 ZERO_STRUCT(my_addr);
53 #ifdef HAVE_SOCK_SIN_LEN
54                 my_addr.sin_len         = sizeof(my_addr);
55 #endif
56                 my_addr.sin_addr        = my_ip;
57                 my_addr.sin_port        = htons(my_port);
58                 my_addr.sin_family      = PF_INET;
59                 
60                 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
61                 if (ret == -1) {
62                         return map_nt_error_from_unix(errno);
63                 }
64         }
65
66         srv_ip = interpret_addr2(srv_address);
67
68         ZERO_STRUCT(srv_addr);
69 #ifdef HAVE_SOCK_SIN_LEN
70         srv_addr.sin_len        = sizeof(srv_addr);
71 #endif
72         srv_addr.sin_addr       = srv_ip;
73         srv_addr.sin_port       = htons(srv_port);
74         srv_addr.sin_family     = PF_INET;
75
76         ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
77         if (ret == -1) {
78                 return map_nt_error_from_unix(errno);
79         }
80
81         if (!(flags & SOCKET_FLAG_BLOCK)) {
82                 ret = set_blocking(sock->fd, False);
83                 if (ret == -1) {
84                         return map_nt_error_from_unix(errno);
85                 }
86         }
87
88         sock->state = SOCKET_STATE_CLIENT_CONNECTED;
89
90         return NT_STATUS_OK;
91 }
92
93 static NTSTATUS ipv4_tcp_listen(struct socket_context *sock,
94                                         const char *my_address, int port,
95                                         int queue_size, uint32_t flags)
96 {
97         struct sockaddr_in my_addr;
98         struct in_addr ip_addr;
99         int ret;
100
101         ip_addr = interpret_addr2(my_address);
102
103         ZERO_STRUCT(my_addr);
104 #ifdef HAVE_SOCK_SIN_LEN
105         my_addr.sin_len         = sizeof(my_addr);
106 #endif
107         my_addr.sin_addr        = ip_addr;
108         my_addr.sin_port        = htons(port);
109         my_addr.sin_family      = PF_INET;
110
111         ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
112         if (ret == -1) {
113                 return map_nt_error_from_unix(errno);
114         }
115
116         ret = listen(sock->fd, queue_size);
117         if (ret == -1) {
118                 return map_nt_error_from_unix(errno);
119         }
120
121         if (!(flags & SOCKET_FLAG_BLOCK)) {
122                 ret = set_blocking(sock->fd, False);
123                 if (ret == -1) {
124                         return map_nt_error_from_unix(errno);
125                 }
126         }
127
128         sock->state= SOCKET_STATE_SERVER_LISTEN;
129
130         return NT_STATUS_OK;
131 }
132
133 static NTSTATUS ipv4_tcp_accept(struct socket_context *sock, struct socket_context **new_sock, uint32_t flags)
134 {
135         struct sockaddr_in cli_addr;
136         socklen_t cli_addr_len = sizeof(cli_addr);
137         int new_fd;
138
139         new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
140         if (new_fd == -1) {
141                 return map_nt_error_from_unix(errno);
142         }
143
144         if (!(flags & SOCKET_FLAG_BLOCK)) {
145                 int ret = set_blocking(new_fd, False);
146                 if (ret == -1) {
147                         close(new_fd);
148                         return map_nt_error_from_unix(errno);
149                 }
150         }
151
152         /* TODO: we could add a 'accept_check' hook here
153          *       which get the black/white lists via socket_set_accept_filter()
154          *       or something like that
155          *       --metze
156          */
157
158         (*new_sock) = talloc_p(NULL, struct socket_context);
159         if (!(*new_sock)) {
160                 close(new_fd);
161                 return NT_STATUS_NO_MEMORY;
162         }
163
164         /* copy the socket_context */
165         (*new_sock)->type               = sock->type;
166         (*new_sock)->state              = SOCKET_STATE_SERVER_CONNECTED;
167         (*new_sock)->flags              = flags;
168
169         (*new_sock)->fd                 = new_fd;
170
171         (*new_sock)->private_data       = NULL;
172         (*new_sock)->ops                = sock->ops;
173
174         return NT_STATUS_OK;
175 }
176
177 static NTSTATUS ipv4_tcp_recv(struct socket_context *sock, void *buf, 
178                               size_t wantlen, size_t *nread, uint32_t flags)
179 {
180         ssize_t gotlen;
181         int flgs = 0;
182
183         /* TODO: we need to map all flags here */
184         if (flags & SOCKET_FLAG_PEEK) {
185                 flgs |= MSG_PEEK;
186         }
187
188         if (flags & SOCKET_FLAG_BLOCK) {
189                 flgs |= MSG_WAITALL;
190         }
191
192         *nread = 0;
193
194         gotlen = recv(sock->fd, buf, wantlen, flgs);
195         if (gotlen == 0) {
196                 return NT_STATUS_END_OF_FILE;
197         } else if (gotlen == -1) {
198                 return map_nt_error_from_unix(errno);
199         }
200
201         *nread = gotlen;
202
203         return NT_STATUS_OK;
204 }
205
206 static NTSTATUS ipv4_tcp_send(struct socket_context *sock, 
207                               const DATA_BLOB *blob, size_t *sendlen, uint32_t flags)
208 {
209         ssize_t len;
210         int flgs = 0;
211
212         *sendlen = 0;
213
214         len = send(sock->fd, blob->data, blob->length, flgs);
215         if (len == -1) {
216                 return map_nt_error_from_unix(errno);
217         }       
218
219         *sendlen = len;
220
221         return NT_STATUS_OK;
222 }
223
224 static NTSTATUS ipv4_tcp_set_option(struct socket_context *sock, const char *option, const char *val)
225 {
226         set_socket_options(sock->fd, option);
227         return NT_STATUS_OK;
228 }
229
230 static char *ipv4_tcp_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
231 {
232         struct sockaddr_in peer_addr;
233         socklen_t len = sizeof(peer_addr);
234         struct hostent *he;
235         int ret;
236
237         ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
238         if (ret == -1) {
239                 return NULL;
240         }
241
242         he = gethostbyaddr((char *)&peer_addr.sin_addr, sizeof(peer_addr.sin_addr), AF_INET);
243         if (he == NULL) {
244                 return NULL;
245         }
246
247         return talloc_strdup(mem_ctx, he->h_name);
248 }
249
250 static char *ipv4_tcp_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
251 {
252         struct sockaddr_in peer_addr;
253         socklen_t len = sizeof(peer_addr);
254         int ret;
255
256         ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
257         if (ret == -1) {
258                 return NULL;
259         }
260
261         return talloc_strdup(mem_ctx, inet_ntoa(peer_addr.sin_addr));
262 }
263
264 static int ipv4_tcp_get_peer_port(struct socket_context *sock)
265 {
266         struct sockaddr_in peer_addr;
267         socklen_t len = sizeof(peer_addr);
268         int ret;
269
270         ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
271         if (ret == -1) {
272                 return -1;
273         }
274
275         return ntohs(peer_addr.sin_port);
276 }
277
278 static char *ipv4_tcp_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
279 {
280         struct sockaddr_in my_addr;
281         socklen_t len = sizeof(my_addr);
282         int ret;
283
284         ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
285         if (ret == -1) {
286                 return NULL;
287         }
288
289         return talloc_strdup(mem_ctx, inet_ntoa(my_addr.sin_addr));
290 }
291
292 static int ipv4_tcp_get_my_port(struct socket_context *sock)
293 {
294         struct sockaddr_in my_addr;
295         socklen_t len = sizeof(my_addr);
296         int ret;
297
298         ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
299         if (ret == -1) {
300                 return -1;
301         }
302
303         return ntohs(my_addr.sin_port);
304 }
305
306 static int ipv4_tcp_get_fd(struct socket_context *sock)
307 {
308         return sock->fd;
309 }
310
311 static const struct socket_ops ipv4_tcp_ops = {
312         .name           = "ipv4",
313         .type           = SOCKET_TYPE_STREAM,
314
315         .init           = ipv4_tcp_init,
316         .connect        = ipv4_tcp_connect,
317         .listen         = ipv4_tcp_listen,
318         .accept         = ipv4_tcp_accept,
319         .recv           = ipv4_tcp_recv,
320         .send           = ipv4_tcp_send,
321         .close          = ipv4_tcp_close,
322
323         .set_option     = ipv4_tcp_set_option,
324
325         .get_peer_name  = ipv4_tcp_get_peer_name,
326         .get_peer_addr  = ipv4_tcp_get_peer_addr,
327         .get_peer_port  = ipv4_tcp_get_peer_port,
328         .get_my_addr    = ipv4_tcp_get_my_addr,
329         .get_my_port    = ipv4_tcp_get_my_port,
330
331         .get_fd         = ipv4_tcp_get_fd
332 };
333
334 const struct socket_ops *socket_ipv4_ops(void)
335 {
336         return &ipv4_tcp_ops;
337 }
338
339 NTSTATUS socket_ipv4_init(void)
340 {
341         return NT_STATUS_OK;
342 }