r6184: the beginnings of the libcli/dgram/ library, and the dgram
[samba.git] / source / libcli / dgram / dgramsocket.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    low level socket handling for nbt dgram requests (UDP138)
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "dlinklist.h"
26 #include "libcli/nbt/libnbt.h"
27 #include "libcli/dgram/libdgram.h"
28 #include "lib/socket/socket.h"
29
30 #define DGRAM_MAX_PACKET_SIZE 2048
31
32
33 /*
34   handle recv events on a nbt dgram socket
35 */
36 static void dgm_socket_recv(struct nbt_dgram_socket *nbtsock)
37 {
38         TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
39         NTSTATUS status;
40         const char *src_addr;
41         int src_port;
42         DATA_BLOB blob;
43         size_t nread;
44         struct nbt_dgram_packet *packet;
45
46         blob = data_blob_talloc(tmp_ctx, NULL, DGRAM_MAX_PACKET_SIZE);
47         if (blob.data == NULL) {
48                 talloc_free(tmp_ctx);
49                 return;
50         }
51
52         status = socket_recvfrom(nbtsock->sock, blob.data, blob.length, &nread, 0,
53                                  &src_addr, &src_port);
54         if (!NT_STATUS_IS_OK(status)) {
55                 talloc_free(tmp_ctx);
56                 return;
57         }
58         talloc_steal(tmp_ctx, src_addr);
59         blob.length = nread;
60
61         DEBUG(0,("Received dgram packet of length %d from %s:%d\n", 
62                  blob.length, src_addr, src_port));
63
64         packet = talloc(tmp_ctx, struct nbt_dgram_packet);
65         if (packet == NULL) {
66                 talloc_free(tmp_ctx);
67                 return;
68         }
69
70         /* parse the request */
71         status = ndr_pull_struct_blob(&blob, packet, packet, 
72                                       (ndr_pull_flags_fn_t)ndr_pull_nbt_dgram_packet);
73         if (!NT_STATUS_IS_OK(status)) {
74                 DEBUG(2,("Failed to parse incoming NBT DGRAM packet - %s\n",
75                          nt_errstr(status)));
76                 talloc_free(tmp_ctx);
77                 return;
78         }
79
80         NDR_PRINT_DEBUG(nbt_dgram_packet, packet);
81
82         talloc_free(tmp_ctx);
83 }
84
85
86 /*
87   handle fd events on a nbt_dgram_socket
88 */
89 static void dgm_socket_handler(struct event_context *ev, struct fd_event *fde,
90                                uint16_t flags, void *private)
91 {
92         struct nbt_dgram_socket *dgmsock = talloc_get_type(private, 
93                                                            struct nbt_dgram_socket);
94         if (flags & EVENT_FD_WRITE) {
95                 /* nothing at the moment */
96         } else if (flags & EVENT_FD_READ) {
97                 dgm_socket_recv(dgmsock);
98         }
99 }
100
101 /*
102   initialise a nbt_dgram_socket. The event_ctx is optional, if provided
103   then operations will use that event context
104 */
105 struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx, 
106                                               struct event_context *event_ctx)
107 {
108         struct nbt_dgram_socket *dgmsock;
109         NTSTATUS status;
110
111         dgmsock = talloc(mem_ctx, struct nbt_dgram_socket);
112         if (dgmsock == NULL) goto failed;
113
114         if (event_ctx == NULL) {
115                 dgmsock->event_ctx = event_context_init(dgmsock);
116         } else {
117                 dgmsock->event_ctx = talloc_reference(dgmsock, event_ctx);
118         }
119         if (dgmsock->event_ctx == NULL) goto failed;
120
121         status = socket_create("ip", SOCKET_TYPE_DGRAM, &dgmsock->sock, 0);
122         if (!NT_STATUS_IS_OK(status)) goto failed;
123
124         socket_set_option(dgmsock->sock, "SO_BROADCAST", "1");
125
126         talloc_steal(dgmsock, dgmsock->sock);
127
128         dgmsock->fde = event_add_fd(dgmsock->event_ctx, dgmsock, 
129                                     socket_get_fd(dgmsock->sock), 0,
130                                     dgm_socket_handler, dgmsock);
131         
132         return dgmsock;
133
134 failed:
135         talloc_free(dgmsock);
136         return NULL;
137 }
138
139
140 /*
141   setup a handler for incoming requests
142 */
143 NTSTATUS dgram_set_incoming_handler(struct nbt_dgram_socket *dgmsock,
144                                     void (*handler)(struct nbt_dgram_socket *, 
145                                                     struct nbt_dgram_packet *, 
146                                                     const char *, int ),
147                                     void *private)
148 {
149         dgmsock->incoming.handler = handler;
150         dgmsock->incoming.private = private;
151         EVENT_FD_READABLE(dgmsock->fde);
152         return NT_STATUS_OK;
153 }