r11334: Print error status in debug.
[kai/samba-autobuild/.git] / source4 / 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
31 /*
32   handle recv events on a nbt dgram socket
33 */
34 static void dgm_socket_recv(struct nbt_dgram_socket *dgmsock)
35 {
36         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
37         NTSTATUS status;
38         struct nbt_peer_socket src;
39         DATA_BLOB blob;
40         size_t nread, dsize;
41         struct nbt_dgram_packet *packet;
42         const char *mailslot_name;
43
44         status = socket_pending(dgmsock->sock, &dsize);
45         if (!NT_STATUS_IS_OK(status)) {
46                 talloc_free(tmp_ctx);
47                 return;
48         }
49
50         blob = data_blob_talloc(tmp_ctx, NULL, dsize);
51         if (blob.data == NULL) {
52                 talloc_free(tmp_ctx);
53                 return;
54         }
55
56         status = socket_recvfrom(dgmsock->sock, blob.data, blob.length, &nread, 0,
57                                  &src.addr, &src.port);
58         if (!NT_STATUS_IS_OK(status)) {
59                 talloc_free(tmp_ctx);
60                 return;
61         }
62         talloc_steal(tmp_ctx, src.addr);
63         blob.length = nread;
64
65         DEBUG(2,("Received dgram packet of length %d from %s:%d\n", 
66                  (int)blob.length, src.addr, src.port));
67
68         packet = talloc(tmp_ctx, struct nbt_dgram_packet);
69         if (packet == NULL) {
70                 talloc_free(tmp_ctx);
71                 return;
72         }
73
74         /* parse the request */
75         status = ndr_pull_struct_blob(&blob, packet, packet, 
76                                       (ndr_pull_flags_fn_t)ndr_pull_nbt_dgram_packet);
77         if (!NT_STATUS_IS_OK(status)) {
78                 DEBUG(2,("Failed to parse incoming NBT DGRAM packet - %s\n",
79                          nt_errstr(status)));
80                 talloc_free(tmp_ctx);
81                 return;
82         }
83
84         /* if this is a mailslot message, then see if we can dispatch it to a handler */
85         mailslot_name = dgram_mailslot_name(packet);
86         if (mailslot_name) {
87                 struct dgram_mailslot_handler *dgmslot;
88                 dgmslot = dgram_mailslot_find(dgmsock, mailslot_name);
89                 if (dgmslot) {
90                         dgmslot->handler(dgmslot, packet, &src);
91                 } else {
92                         DEBUG(2,("No mailslot handler for '%s'\n", mailslot_name));
93                 }
94         } else {
95                 /* dispatch if there is a general handler */
96                 if (dgmsock->incoming.handler) {
97                         dgmsock->incoming.handler(dgmsock, packet, &src);
98                 }
99         }
100
101         talloc_free(tmp_ctx);
102 }
103
104
105 /*
106   handle send events on a nbt dgram socket
107 */
108 static void dgm_socket_send(struct nbt_dgram_socket *dgmsock)
109 {
110         struct nbt_dgram_request *req;
111         NTSTATUS status;
112
113         while ((req = dgmsock->send_queue)) {
114                 size_t len;
115                 
116                 len = req->encoded.length;
117                 status = socket_sendto(dgmsock->sock, &req->encoded, &len, 0, 
118                                        req->dest.addr, req->dest.port);
119                 if (NT_STATUS_IS_ERR(status)) {
120                         DEBUG(3,("Failed to send datagram of length %u to %s:%d: %s\n",
121                                  (unsigned)req->encoded.length, req->dest.addr, req->dest.port, 
122                                  nt_errstr(status)));
123                         DLIST_REMOVE(dgmsock->send_queue, req);
124                         talloc_free(req);
125                         continue;
126                 }
127
128                 if (!NT_STATUS_IS_OK(status)) return;
129
130                 DLIST_REMOVE(dgmsock->send_queue, req);
131                 talloc_free(req);
132         }
133
134         EVENT_FD_NOT_WRITEABLE(dgmsock->fde);
135         return;
136 }
137
138
139 /*
140   handle fd events on a nbt_dgram_socket
141 */
142 static void dgm_socket_handler(struct event_context *ev, struct fd_event *fde,
143                                uint16_t flags, void *private)
144 {
145         struct nbt_dgram_socket *dgmsock = talloc_get_type(private, 
146                                                            struct nbt_dgram_socket);
147         if (flags & EVENT_FD_WRITE) {
148                 dgm_socket_send(dgmsock);
149         } 
150         if (flags & EVENT_FD_READ) {
151                 dgm_socket_recv(dgmsock);
152         }
153 }
154
155 /*
156   initialise a nbt_dgram_socket. The event_ctx is optional, if provided
157   then operations will use that event context
158 */
159 struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx, 
160                                               struct event_context *event_ctx)
161 {
162         struct nbt_dgram_socket *dgmsock;
163         NTSTATUS status;
164
165         dgmsock = talloc(mem_ctx, struct nbt_dgram_socket);
166         if (dgmsock == NULL) goto failed;
167
168         if (event_ctx == NULL) {
169                 dgmsock->event_ctx = event_context_init(dgmsock);
170         } else {
171                 dgmsock->event_ctx = talloc_reference(dgmsock, event_ctx);
172         }
173         if (dgmsock->event_ctx == NULL) goto failed;
174
175         status = socket_create("ip", SOCKET_TYPE_DGRAM, &dgmsock->sock, 0);
176         if (!NT_STATUS_IS_OK(status)) goto failed;
177
178         socket_set_option(dgmsock->sock, "SO_BROADCAST", "1");
179
180         talloc_steal(dgmsock, dgmsock->sock);
181
182         dgmsock->fde = event_add_fd(dgmsock->event_ctx, dgmsock, 
183                                     socket_get_fd(dgmsock->sock), 0,
184                                     dgm_socket_handler, dgmsock);
185
186         dgmsock->send_queue = NULL;
187         dgmsock->incoming.handler = NULL;
188         dgmsock->mailslot_handlers = NULL;
189         
190         return dgmsock;
191
192 failed:
193         talloc_free(dgmsock);
194         return NULL;
195 }
196
197
198 /*
199   setup a handler for generic incoming requests
200 */
201 NTSTATUS dgram_set_incoming_handler(struct nbt_dgram_socket *dgmsock,
202                                     void (*handler)(struct nbt_dgram_socket *, 
203                                                     struct nbt_dgram_packet *, 
204                                                     const struct nbt_peer_socket *),
205                                     void *private)
206 {
207         dgmsock->incoming.handler = handler;
208         dgmsock->incoming.private = private;
209         EVENT_FD_READABLE(dgmsock->fde);
210         return NT_STATUS_OK;
211 }
212
213
214 /*
215   queue a datagram for send
216 */
217 NTSTATUS nbt_dgram_send(struct nbt_dgram_socket *dgmsock,
218                         struct nbt_dgram_packet *packet,
219                         const struct nbt_peer_socket *dest)
220 {
221         struct nbt_dgram_request *req;
222         NTSTATUS status = NT_STATUS_NO_MEMORY;
223
224         req = talloc(dgmsock, struct nbt_dgram_request);
225         if (req == NULL) goto failed;
226
227         req->dest.port = dest->port;
228         req->dest.addr = talloc_strdup(req, dest->addr);
229         if (req->dest.addr == NULL) goto failed;
230
231         status = ndr_push_struct_blob(&req->encoded, req, packet, 
232                                       (ndr_push_flags_fn_t)ndr_push_nbt_dgram_packet);
233         if (!NT_STATUS_IS_OK(status)) goto failed;
234
235         DLIST_ADD_END(dgmsock->send_queue, req, struct nbt_dgram_request *);
236
237         EVENT_FD_WRITEABLE(dgmsock->fde);
238
239         return NT_STATUS_OK;
240
241 failed:
242         talloc_free(req);
243         return status;
244 }