talloc: release 2.4.2
[Samba.git] / source3 / nmbd / nmbd_packets.c
bloba1d8deec0f32cfc2447b4c5f9461001295ecc0c8
1 /*
2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 Copyright (C) Jeremy Allison 1994-2003
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "nmbd/nmbd.h"
24 #include "../lib/util/select.h"
25 #include "system/select.h"
26 #include "libsmb/libsmb.h"
27 #include "libsmb/unexpected.h"
28 #include "lib/util/string_wrappers.h"
30 extern int ClientNMB;
31 extern int ClientDGRAM;
32 extern int global_nmb_port;
34 extern int num_response_packets;
36 bool rescan_listen_set = False;
38 static struct nb_packet_server *packet_server;
40 bool nmbd_init_packet_server(void)
42 NTSTATUS status;
44 status = nb_packet_server_create(
45 NULL, nmbd_event_context(),
46 lp_parm_int(-1, "nmbd", "unexpected_clients", 200),
47 &packet_server);
48 if (!NT_STATUS_IS_OK(status)) {
49 DEBUG(0, ("ERROR: nb_packet_server_create failed: %s\n",
50 nt_errstr(status)));
51 return false;
53 return true;
57 /*******************************************************************
58 The global packet linked-list. Incoming entries are
59 added to the end of this list. It is supposed to remain fairly
60 short so we won't bother with an end pointer.
61 ******************************************************************/
63 static struct packet_struct *packet_queue = NULL;
65 /***************************************************************************
66 Utility function to find the specific fd to send a packet out on.
67 **************************************************************************/
69 static int find_subnet_fd_for_address( struct in_addr local_ip )
71 struct subnet_record *subrec;
73 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
74 if(ip_equal_v4(local_ip, subrec->myip))
75 return subrec->nmb_sock;
77 return ClientNMB;
80 /***************************************************************************
81 Utility function to find the specific fd to send a mailslot packet out on.
82 **************************************************************************/
84 static int find_subnet_mailslot_fd_for_address( struct in_addr local_ip )
86 struct subnet_record *subrec;
88 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
89 if(ip_equal_v4(local_ip, subrec->myip))
90 return subrec->dgram_sock;
92 return ClientDGRAM;
95 /***************************************************************************
96 Get/Set problematic nb_flags as network byte order 16 bit int.
97 **************************************************************************/
99 uint16_t get_nb_flags(char *buf)
101 return ((((uint16_t)*buf)&0xFFFF) & NB_FLGMSK);
104 void set_nb_flags(char *buf, uint16_t nb_flags)
106 *buf++ = ((nb_flags & NB_FLGMSK) & 0xFF);
107 *buf = '\0';
110 /***************************************************************************
111 Dumps out the browse packet data.
112 **************************************************************************/
114 static void debug_browse_data(const char *outbuf, int len)
116 int i,j;
118 DEBUG( 4, ( "debug_browse_data():\n" ) );
119 for (i = 0; i < len; i+= 16) {
120 DEBUGADD( 4, ( "%3x char ", i ) );
122 for (j = 0; j < 16; j++) {
123 unsigned char x;
124 if (i+j >= len)
125 break;
127 x = outbuf[i+j];
128 if (x < 32 || x > 127)
129 x = '.';
131 DEBUGADD( 4, ( "%c", x ) );
134 DEBUGADD( 4, ( "%*s hex", 16-j, "" ) );
136 for (j = 0; j < 16; j++) {
137 if (i+j >= len)
138 break;
139 DEBUGADD( 4, ( " %02x", (unsigned char)outbuf[i+j] ) );
142 DEBUGADD( 4, ("\n") );
146 /***************************************************************************
147 Generates the unique transaction identifier
148 **************************************************************************/
150 static uint16_t name_trn_id=0;
152 static uint16_t generate_name_trn_id(void)
154 if (!name_trn_id) {
155 name_trn_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)getpid()%(unsigned)100);
157 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
158 return name_trn_id;
161 /***************************************************************************
162 Either loops back or sends out a completed NetBIOS packet.
163 **************************************************************************/
165 static bool send_netbios_packet(struct packet_struct *p)
167 bool loopback_this_packet = False;
169 /* Check if we are sending to or from ourselves as a WINS server. */
170 if(ismyip_v4(p->ip) && (p->port == global_nmb_port))
171 loopback_this_packet = True;
173 if(loopback_this_packet) {
174 struct packet_struct *lo_packet = NULL;
175 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
176 if((lo_packet = copy_packet(p)) == NULL)
177 return False;
178 queue_packet(lo_packet);
179 } else if (!send_packet(p)) {
180 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
181 inet_ntoa(p->ip),p->port));
182 return False;
185 return True;
188 /***************************************************************************
189 Sets up the common elements of an outgoing NetBIOS packet.
191 Note: do not attempt to rationalise whether rec_des should be set or not
192 in a particular situation. Just follow rfc_1002 or look at examples from WinXX.
193 It does NOT follow the rule that requests to the wins server always have
194 rec_des true. See for example name releases and refreshes
195 **************************************************************************/
197 static struct packet_struct *create_and_init_netbios_packet(struct nmb_name *nmbname,
198 bool bcast, bool rec_des,
199 struct in_addr to_ip)
201 struct packet_struct *packet = NULL;
202 struct nmb_packet *nmb = NULL;
204 /* Allocate the packet_struct we will return. */
205 if((packet = SMB_MALLOC_P(struct packet_struct)) == NULL) {
206 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
207 return NULL;
210 memset((char *)packet,'\0',sizeof(*packet));
212 nmb = &packet->packet.nmb;
214 nmb->header.name_trn_id = generate_name_trn_id();
215 nmb->header.response = False;
216 nmb->header.nm_flags.recursion_desired = rec_des;
217 nmb->header.nm_flags.recursion_available = False;
218 nmb->header.nm_flags.trunc = False;
219 nmb->header.nm_flags.authoritative = False;
220 nmb->header.nm_flags.bcast = bcast;
222 nmb->header.rcode = 0;
223 nmb->header.qdcount = 1;
224 nmb->header.ancount = 0;
225 nmb->header.nscount = 0;
227 nmb->question.question_name = *nmbname;
228 nmb->question.question_type = QUESTION_TYPE_NB_QUERY;
229 nmb->question.question_class = QUESTION_CLASS_IN;
231 packet->ip = to_ip;
232 packet->port = NMB_PORT;
233 packet->recv_fd = -1;
234 packet->send_fd = ClientNMB;
235 packet->timestamp = time(NULL);
236 packet->packet_type = NMB_PACKET;
237 packet->locked = False;
239 return packet; /* Caller must free. */
242 /***************************************************************************
243 Sets up the common elements of register, refresh or release packet.
244 **************************************************************************/
246 static bool create_and_init_additional_record(struct packet_struct *packet,
247 uint16_t nb_flags,
248 const struct in_addr *register_ip)
250 struct nmb_packet *nmb = &packet->packet.nmb;
252 if((nmb->additional = SMB_MALLOC_P(struct res_rec)) == NULL) {
253 DEBUG(0,("create_and_init_additional_record: malloc fail for additional record.\n"));
254 return False;
257 memset((char *)nmb->additional,'\0',sizeof(struct res_rec));
259 nmb->additional->rr_name = nmb->question.question_name;
260 nmb->additional->rr_type = RR_TYPE_NB;
261 nmb->additional->rr_class = RR_CLASS_IN;
263 /* See RFC 1002, sections 5.1.1.1, 5.1.1.2 and 5.1.1.3 */
264 if (nmb->header.nm_flags.bcast)
265 nmb->additional->ttl = PERMANENT_TTL;
266 else
267 nmb->additional->ttl = lp_max_ttl();
269 nmb->additional->rdlength = 6;
271 set_nb_flags(nmb->additional->rdata,nb_flags);
273 /* Set the address for the name we are registering. */
274 putip(&nmb->additional->rdata[2], register_ip);
277 it turns out that Jeremys code was correct, we are supposed
278 to send registrations from the IP we are registering. The
279 trick is what to do on timeouts! When we send on a
280 non-routable IP then the reply will timeout, and we should
281 treat this as success, not failure. That means we go into
282 our standard refresh cycle for that name which copes nicely
283 with disconnected networks.
285 packet->recv_fd = -1;
286 packet->send_fd = find_subnet_fd_for_address(*register_ip);
288 return True;
291 /***************************************************************************
292 Sends out a name query.
293 **************************************************************************/
295 static bool initiate_name_query_packet( struct packet_struct *packet)
297 struct nmb_packet *nmb = NULL;
299 nmb = &packet->packet.nmb;
301 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
302 nmb->header.arcount = 0;
304 nmb->header.nm_flags.recursion_desired = True;
306 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
307 nmb_namestr(&nmb->question.question_name),
308 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
310 return send_netbios_packet( packet );
313 /***************************************************************************
314 Sends out a name query - from a WINS server.
315 **************************************************************************/
317 static bool initiate_name_query_packet_from_wins_server( struct packet_struct *packet)
319 struct nmb_packet *nmb = NULL;
321 nmb = &packet->packet.nmb;
323 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
324 nmb->header.arcount = 0;
326 nmb->header.nm_flags.recursion_desired = False;
328 DEBUG(4,("initiate_name_query_packet_from_wins_server: sending query for name %s (bcast=%s) to IP %s\n",
329 nmb_namestr(&nmb->question.question_name),
330 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
332 return send_netbios_packet( packet );
335 /***************************************************************************
336 Sends out a name register.
337 **************************************************************************/
339 static bool initiate_name_register_packet( struct packet_struct *packet,
340 uint16_t nb_flags, const struct in_addr *register_ip)
342 struct nmb_packet *nmb = &packet->packet.nmb;
344 nmb->header.opcode = NMB_NAME_REG_OPCODE;
345 nmb->header.arcount = 1;
347 nmb->header.nm_flags.recursion_desired = True;
349 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
350 return False;
352 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
353 nmb_namestr(&nmb->additional->rr_name),
354 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
356 return send_netbios_packet( packet );
359 /***************************************************************************
360 Sends out a multihomed name register.
361 **************************************************************************/
363 static bool initiate_multihomed_name_register_packet(struct packet_struct *packet,
364 uint16_t nb_flags, struct in_addr *register_ip)
366 struct nmb_packet *nmb = &packet->packet.nmb;
367 fstring second_ip_buf;
369 fstrcpy(second_ip_buf, inet_ntoa(packet->ip));
371 nmb->header.opcode = NMB_NAME_MULTIHOMED_REG_OPCODE;
372 nmb->header.arcount = 1;
374 nmb->header.nm_flags.recursion_desired = True;
376 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
377 return False;
379 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
380 for name %s IP %s (bcast=%s) to IP %s\n",
381 nmb_namestr(&nmb->additional->rr_name), inet_ntoa(*register_ip),
382 BOOLSTR(nmb->header.nm_flags.bcast), second_ip_buf ));
384 return send_netbios_packet( packet );
387 /***************************************************************************
388 Sends out a name refresh.
389 **************************************************************************/
391 static bool initiate_name_refresh_packet( struct packet_struct *packet,
392 uint16_t nb_flags, struct in_addr *refresh_ip)
394 struct nmb_packet *nmb = &packet->packet.nmb;
396 nmb->header.opcode = NMB_NAME_REFRESH_OPCODE_8;
397 nmb->header.arcount = 1;
399 nmb->header.nm_flags.recursion_desired = False;
401 if(create_and_init_additional_record(packet, nb_flags, refresh_ip) == False)
402 return False;
404 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
405 nmb_namestr(&nmb->additional->rr_name),
406 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
408 return send_netbios_packet( packet );
411 /***************************************************************************
412 Sends out a name release.
413 **************************************************************************/
415 static bool initiate_name_release_packet( struct packet_struct *packet,
416 uint16_t nb_flags, struct in_addr *release_ip)
418 struct nmb_packet *nmb = &packet->packet.nmb;
420 nmb->header.opcode = NMB_NAME_RELEASE_OPCODE;
421 nmb->header.arcount = 1;
423 nmb->header.nm_flags.recursion_desired = False;
425 if(create_and_init_additional_record(packet, nb_flags, release_ip) == False)
426 return False;
428 DEBUG(4,("initiate_name_release_packet: sending release for name %s (bcast=%s) to IP %s\n",
429 nmb_namestr(&nmb->additional->rr_name),
430 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
432 return send_netbios_packet( packet );
435 /***************************************************************************
436 Sends out a node status.
437 **************************************************************************/
439 static bool initiate_node_status_packet( struct packet_struct *packet )
441 struct nmb_packet *nmb = &packet->packet.nmb;
443 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
444 nmb->header.arcount = 0;
446 nmb->header.nm_flags.recursion_desired = False;
448 nmb->question.question_type = QUESTION_TYPE_NB_STATUS;
450 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
451 nmb_namestr(&nmb->question.question_name),
452 inet_ntoa(packet->ip)));
454 return send_netbios_packet( packet );
457 /****************************************************************************
458 Simplification functions for queuing standard packets.
459 These should be the only publicly callable functions for sending
460 out packets.
461 ****************************************************************************/
463 /****************************************************************************
464 Assertion - we should never be sending nmbd packets on the remote
465 broadcast subnet.
466 ****************************************************************************/
468 static bool assert_check_subnet(struct subnet_record *subrec)
470 if( subrec == remote_broadcast_subnet) {
471 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
472 This is a bug.\n"));
473 return True;
475 return False;
478 /****************************************************************************
479 Queue a register name packet to the broadcast address of a subnet.
480 ****************************************************************************/
482 struct response_record *queue_register_name( struct subnet_record *subrec,
483 response_function resp_fn,
484 timeout_response_function timeout_fn,
485 register_name_success_function success_fn,
486 register_name_fail_function fail_fn,
487 struct userdata_struct *userdata,
488 struct nmb_name *nmbname,
489 uint16_t nb_flags)
491 struct packet_struct *p;
492 struct response_record *rrec;
493 struct sockaddr_storage ss;
494 const struct sockaddr_storage *pss = NULL;
495 if(assert_check_subnet(subrec))
496 return NULL;
498 /* note that all name registration requests have RD set (rfc1002 - section 4.2.2 */
499 if ((p = create_and_init_netbios_packet(nmbname, (subrec != unicast_subnet), True,
500 subrec->bcast_ip)) == NULL)
501 return NULL;
503 in_addr_to_sockaddr_storage(&ss, subrec->bcast_ip);
504 pss = iface_ip((struct sockaddr *)(void *)&ss);
505 if (!pss || pss->ss_family != AF_INET) {
506 p->locked = False;
507 free_packet(p);
508 return NULL;
511 if(initiate_name_register_packet(p, nb_flags,
512 &((const struct sockaddr_in *)pss)->sin_addr) == False) {
513 p->locked = False;
514 free_packet(p);
515 return NULL;
518 if((rrec = make_response_record(subrec, /* subnet record. */
519 p, /* packet we sent. */
520 resp_fn, /* function to call on response. */
521 timeout_fn, /* function to call on timeout. */
522 (success_function)success_fn, /* function to call on operation success. */
523 (fail_function)fail_fn, /* function to call on operation fail. */
524 userdata)) == NULL) {
525 p->locked = False;
526 free_packet(p);
527 return NULL;
530 return rrec;
533 /****************************************************************************
534 Queue a refresh name packet to the broadcast address of a subnet.
535 ****************************************************************************/
537 void queue_wins_refresh(struct nmb_name *nmbname,
538 response_function resp_fn,
539 timeout_response_function timeout_fn,
540 uint16_t nb_flags,
541 struct in_addr refresh_ip,
542 const char *tag)
544 struct packet_struct *p;
545 struct response_record *rrec;
546 struct in_addr wins_ip;
547 struct userdata_struct *userdata;
548 fstring ip_str;
550 wins_ip = wins_srv_ip_tag(tag, refresh_ip);
552 if ((p = create_and_init_netbios_packet(nmbname, False, False, wins_ip)) == NULL) {
553 return;
556 if (!initiate_name_refresh_packet(p, nb_flags, &refresh_ip)) {
557 p->locked = False;
558 free_packet(p);
559 return;
562 fstrcpy(ip_str, inet_ntoa(refresh_ip));
564 DEBUG(6,("Refreshing name %s IP %s with WINS server %s using tag '%s'\n",
565 nmb_namestr(nmbname), ip_str, inet_ntoa(wins_ip), tag));
567 userdata = (struct userdata_struct *)SMB_MALLOC(sizeof(*userdata) + strlen(tag) + 1);
568 if (!userdata) {
569 p->locked = False;
570 free_packet(p);
571 DEBUG(0,("Failed to allocate userdata structure!\n"));
572 return;
574 ZERO_STRUCTP(userdata);
575 userdata->userdata_len = strlen(tag) + 1;
576 strlcpy(userdata->data, tag, userdata->userdata_len);
578 if ((rrec = make_response_record(unicast_subnet,
580 resp_fn, timeout_fn,
581 NULL,
582 NULL,
583 userdata)) == NULL) {
584 p->locked = False;
585 free_packet(p);
586 return;
589 free(userdata);
591 /* we don't want to repeat refresh packets */
592 rrec->repeat_count = 0;
596 /****************************************************************************
597 Queue a multihomed register name packet to a given WINS server IP
598 ****************************************************************************/
600 struct response_record *queue_register_multihomed_name( struct subnet_record *subrec,
601 response_function resp_fn,
602 timeout_response_function timeout_fn,
603 register_name_success_function success_fn,
604 register_name_fail_function fail_fn,
605 struct userdata_struct *userdata,
606 struct nmb_name *nmbname,
607 uint16_t nb_flags,
608 struct in_addr register_ip,
609 struct in_addr wins_ip)
611 struct packet_struct *p;
612 struct response_record *rrec;
613 bool ret;
615 /* Sanity check. */
616 if(subrec != unicast_subnet) {
617 DEBUG(0,("queue_register_multihomed_name: should only be done on \
618 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
619 return NULL;
622 if(assert_check_subnet(subrec))
623 return NULL;
625 if ((p = create_and_init_netbios_packet(nmbname, False, True, wins_ip)) == NULL)
626 return NULL;
628 if (nb_flags & NB_GROUP)
629 ret = initiate_name_register_packet( p, nb_flags, &register_ip);
630 else
631 ret = initiate_multihomed_name_register_packet(p, nb_flags, &register_ip);
633 if (ret == False) {
634 p->locked = False;
635 free_packet(p);
636 return NULL;
639 if ((rrec = make_response_record(subrec, /* subnet record. */
640 p, /* packet we sent. */
641 resp_fn, /* function to call on response. */
642 timeout_fn, /* function to call on timeout. */
643 (success_function)success_fn, /* function to call on operation success. */
644 (fail_function)fail_fn, /* function to call on operation fail. */
645 userdata)) == NULL) {
646 p->locked = False;
647 free_packet(p);
648 return NULL;
651 return rrec;
654 /****************************************************************************
655 Queue a release name packet to the broadcast address of a subnet.
656 ****************************************************************************/
658 struct response_record *queue_release_name( struct subnet_record *subrec,
659 response_function resp_fn,
660 timeout_response_function timeout_fn,
661 release_name_success_function success_fn,
662 release_name_fail_function fail_fn,
663 struct userdata_struct *userdata,
664 struct nmb_name *nmbname,
665 uint16_t nb_flags,
666 struct in_addr release_ip,
667 struct in_addr dest_ip)
669 struct packet_struct *p;
670 struct response_record *rrec;
672 if(assert_check_subnet(subrec))
673 return NULL;
675 if ((p = create_and_init_netbios_packet(nmbname, (subrec != unicast_subnet), False, dest_ip)) == NULL)
676 return NULL;
678 if(initiate_name_release_packet( p, nb_flags, &release_ip) == False) {
679 p->locked = False;
680 free_packet(p);
681 return NULL;
684 if((rrec = make_response_record(subrec, /* subnet record. */
685 p, /* packet we sent. */
686 resp_fn, /* function to call on response. */
687 timeout_fn, /* function to call on timeout. */
688 (success_function)success_fn, /* function to call on operation success. */
689 (fail_function)fail_fn, /* function to call on operation fail. */
690 userdata)) == NULL) {
691 p->locked = False;
692 free_packet(p);
693 return NULL;
697 * For a broadcast release packet, only send once.
698 * This will cause us to remove the name asap. JRA.
701 if (subrec != unicast_subnet) {
702 rrec->repeat_count = 0;
703 rrec->repeat_time = 0;
706 return rrec;
709 /****************************************************************************
710 Queue a query name packet to the broadcast address of a subnet.
711 ****************************************************************************/
713 struct response_record *queue_query_name( struct subnet_record *subrec,
714 response_function resp_fn,
715 timeout_response_function timeout_fn,
716 query_name_success_function success_fn,
717 query_name_fail_function fail_fn,
718 struct userdata_struct *userdata,
719 struct nmb_name *nmbname)
721 struct packet_struct *p;
722 struct response_record *rrec;
723 struct in_addr to_ip;
725 if(assert_check_subnet(subrec))
726 return NULL;
728 to_ip = subrec->bcast_ip;
730 /* queries to the WINS server turn up here as queries to IP 0.0.0.0
731 These need to be handled a bit differently */
732 if (subrec->type == UNICAST_SUBNET && is_zero_ip_v4(to_ip)) {
733 /* What we really need to do is loop over each of our wins
734 * servers and wins server tags here, but that just doesn't
735 * fit our architecture at the moment (userdata may already
736 * be used when we get here). For now we just query the first
737 * active wins server on the first tag.
739 char **tags = wins_srv_tags();
740 if (!tags) {
741 return NULL;
743 to_ip = wins_srv_ip_tag(tags[0], to_ip);
744 wins_srv_tags_free(tags);
747 if(( p = create_and_init_netbios_packet(nmbname,
748 (subrec != unicast_subnet),
749 (subrec == unicast_subnet),
750 to_ip)) == NULL)
751 return NULL;
753 if(lp_bind_interfaces_only()) {
754 int i;
756 DEBUG(10,("queue_query_name: bind_interfaces_only is set, looking for suitable source IP\n"));
757 for(i = 0; i < iface_count(); i++) {
758 const struct in_addr *ifip = iface_n_ip_v4(i);
760 if (ifip == NULL) {
761 DEBUG(0,("queue_query_name: interface %d has NULL IP address !\n", i));
762 continue;
765 if (is_loopback_ip_v4(*ifip)) {
766 DEBUG(5,("queue_query_name: ignoring loopback interface (%d)\n", i));
767 continue;
770 DEBUG(10,("queue_query_name: using source IP %s\n",inet_ntoa(*ifip)));
771 p->send_fd = find_subnet_fd_for_address( *ifip );
772 break;
776 if(initiate_name_query_packet( p ) == False) {
777 p->locked = False;
778 free_packet(p);
779 return NULL;
782 if((rrec = make_response_record(subrec, /* subnet record. */
783 p, /* packet we sent. */
784 resp_fn, /* function to call on response. */
785 timeout_fn, /* function to call on timeout. */
786 (success_function)success_fn, /* function to call on operation success. */
787 (fail_function)fail_fn, /* function to call on operation fail. */
788 userdata)) == NULL) {
789 p->locked = False;
790 free_packet(p);
791 return NULL;
794 return rrec;
797 /****************************************************************************
798 Queue a query name packet to a given address from the WINS subnet.
799 ****************************************************************************/
801 struct response_record *queue_query_name_from_wins_server( struct in_addr to_ip,
802 response_function resp_fn,
803 timeout_response_function timeout_fn,
804 query_name_success_function success_fn,
805 query_name_fail_function fail_fn,
806 struct userdata_struct *userdata,
807 struct nmb_name *nmbname)
809 struct packet_struct *p;
810 struct response_record *rrec;
812 if ((p = create_and_init_netbios_packet(nmbname, False, False, to_ip)) == NULL)
813 return NULL;
815 if(initiate_name_query_packet_from_wins_server( p ) == False) {
816 p->locked = False;
817 free_packet(p);
818 return NULL;
821 if((rrec = make_response_record(wins_server_subnet, /* subnet record. */
822 p, /* packet we sent. */
823 resp_fn, /* function to call on response. */
824 timeout_fn, /* function to call on timeout. */
825 (success_function)success_fn, /* function to call on operation success. */
826 (fail_function)fail_fn, /* function to call on operation fail. */
827 userdata)) == NULL) {
828 p->locked = False;
829 free_packet(p);
830 return NULL;
833 return rrec;
836 /****************************************************************************
837 Queue a node status packet to a given name and address.
838 ****************************************************************************/
840 struct response_record *queue_node_status( struct subnet_record *subrec,
841 response_function resp_fn,
842 timeout_response_function timeout_fn,
843 node_status_success_function success_fn,
844 node_status_fail_function fail_fn,
845 struct userdata_struct *userdata,
846 struct nmb_name *nmbname,
847 struct in_addr send_ip)
849 struct packet_struct *p;
850 struct response_record *rrec;
852 /* Sanity check. */
853 if(subrec != unicast_subnet) {
854 DEBUG(0,("queue_register_multihomed_name: should only be done on \
855 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
856 return NULL;
859 if(assert_check_subnet(subrec))
860 return NULL;
862 if(( p = create_and_init_netbios_packet(nmbname, False, False, send_ip)) == NULL)
863 return NULL;
865 if(initiate_node_status_packet(p) == False) {
866 p->locked = False;
867 free_packet(p);
868 return NULL;
871 if((rrec = make_response_record(subrec, /* subnet record. */
872 p, /* packet we sent. */
873 resp_fn, /* function to call on response. */
874 timeout_fn, /* function to call on timeout. */
875 (success_function)success_fn, /* function to call on operation success. */
876 (fail_function)fail_fn, /* function to call on operation fail. */
877 userdata)) == NULL) {
878 p->locked = False;
879 free_packet(p);
880 return NULL;
883 return rrec;
886 /****************************************************************************
887 Reply to a netbios name packet. see rfc1002.txt
888 ****************************************************************************/
890 void reply_netbios_packet(struct packet_struct *orig_packet,
891 int rcode, enum netbios_reply_type_code rcv_code, int opcode,
892 int ttl, char *data,int len)
894 struct packet_struct packet;
895 struct nmb_packet *nmb = NULL;
896 struct res_rec answers;
897 struct nmb_packet *orig_nmb = &orig_packet->packet.nmb;
898 bool loopback_this_packet = False;
899 int rr_type = RR_TYPE_NB;
900 const char *packet_type = "unknown";
902 /* Check if we are sending to or from ourselves. */
903 if(ismyip_v4(orig_packet->ip) && (orig_packet->port == global_nmb_port))
904 loopback_this_packet = True;
906 nmb = &packet.packet.nmb;
908 /* Do a partial copy of the packet. We clear the locked flag and
909 the resource record pointers. */
910 packet = *orig_packet; /* Full structure copy. */
911 packet.locked = False;
912 nmb->answers = NULL;
913 nmb->nsrecs = NULL;
914 nmb->additional = NULL;
916 switch (rcv_code) {
917 case NMB_STATUS:
918 packet_type = "nmb_status";
919 nmb->header.nm_flags.recursion_desired = False;
920 nmb->header.nm_flags.recursion_available = False;
921 rr_type = RR_TYPE_NBSTAT;
922 break;
923 case NMB_QUERY:
924 packet_type = "nmb_query";
925 nmb->header.nm_flags.recursion_desired = True;
926 nmb->header.nm_flags.recursion_available = True;
927 if (rcode) {
928 rr_type = RR_TYPE_NULL;
930 break;
931 case NMB_REG:
932 case NMB_REG_REFRESH:
933 packet_type = "nmb_reg";
934 nmb->header.nm_flags.recursion_desired = True;
935 nmb->header.nm_flags.recursion_available = True;
936 break;
937 case NMB_REL:
938 packet_type = "nmb_rel";
939 nmb->header.nm_flags.recursion_desired = False;
940 nmb->header.nm_flags.recursion_available = False;
941 break;
942 case NMB_WAIT_ACK:
943 packet_type = "nmb_wack";
944 nmb->header.nm_flags.recursion_desired = False;
945 nmb->header.nm_flags.recursion_available = False;
946 rr_type = RR_TYPE_NULL;
947 break;
948 case WINS_REG:
949 packet_type = "wins_reg";
950 nmb->header.nm_flags.recursion_desired = True;
951 nmb->header.nm_flags.recursion_available = True;
952 break;
953 case WINS_QUERY:
954 packet_type = "wins_query";
955 nmb->header.nm_flags.recursion_desired = True;
956 nmb->header.nm_flags.recursion_available = True;
957 if (rcode) {
958 rr_type = RR_TYPE_NULL;
960 break;
961 default:
962 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
963 packet_type, nmb_namestr(&orig_nmb->question.question_name),
964 inet_ntoa(packet.ip)));
965 return;
968 DEBUG(4, ("reply_netbios_packet: sending a reply of packet type: %s "
969 "%s to ip %s for id %d\n", packet_type,
970 nmb_namestr(&orig_nmb->question.question_name),
971 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
973 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
974 nmb->header.opcode = opcode;
975 nmb->header.response = True;
976 nmb->header.nm_flags.bcast = False;
977 nmb->header.nm_flags.trunc = False;
978 nmb->header.nm_flags.authoritative = True;
980 nmb->header.rcode = rcode;
981 nmb->header.qdcount = 0;
982 nmb->header.ancount = 1;
983 nmb->header.nscount = 0;
984 nmb->header.arcount = 0;
986 memset((char*)&nmb->question,'\0',sizeof(nmb->question));
988 nmb->answers = &answers;
989 memset((char*)nmb->answers,'\0',sizeof(*nmb->answers));
991 nmb->answers->rr_name = orig_nmb->question.question_name;
992 nmb->answers->rr_type = rr_type;
993 nmb->answers->rr_class = RR_CLASS_IN;
994 nmb->answers->ttl = ttl;
996 if (data && len) {
997 if (len < 0 || len > sizeof(nmb->answers->rdata)) {
998 DEBUG(5,("reply_netbios_packet: "
999 "invalid packet len (%d)\n",
1000 len ));
1001 return;
1003 nmb->answers->rdlength = len;
1004 memcpy(nmb->answers->rdata, data, len);
1007 packet.packet_type = NMB_PACKET;
1008 packet.recv_fd = -1;
1009 /* Ensure we send out on the same fd that the original
1010 packet came in on to give the correct source IP address. */
1011 if (orig_packet->send_fd != -1) {
1012 packet.send_fd = orig_packet->send_fd;
1013 } else {
1014 packet.send_fd = orig_packet->recv_fd;
1016 packet.timestamp = time(NULL);
1018 debug_nmb_packet(&packet);
1020 if(loopback_this_packet) {
1021 struct packet_struct *lo_packet;
1022 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
1023 if((lo_packet = copy_packet(&packet)) == NULL)
1024 return;
1025 queue_packet(lo_packet);
1026 } else if (!send_packet(&packet)) {
1027 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
1028 inet_ntoa(packet.ip),packet.port));
1032 /*******************************************************************
1033 Queue a packet into a packet queue
1034 ******************************************************************/
1036 void queue_packet(struct packet_struct *packet)
1038 DLIST_ADD_END(packet_queue, packet);
1041 /****************************************************************************
1042 Try and find a matching subnet record for a datagram port 138 packet.
1043 ****************************************************************************/
1045 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
1047 struct subnet_record *subrec;
1049 /* Go through all the broadcast subnets and see if the mask matches. */
1050 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1051 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1052 return subrec;
1055 /* If the subnet record is the remote announce broadcast subnet,
1056 hack it here to be the first subnet. This is really gross and
1057 is needed due to people turning on port 137/138 broadcast
1058 forwarding on their routers. May fire and brimstone rain
1059 down upon them...
1062 return FIRST_SUBNET;
1065 /****************************************************************************
1066 Dispatch a browse frame from port 138 to the correct processing function.
1067 ****************************************************************************/
1069 static void process_browse_packet(struct packet_struct *p, const char *buf,int len)
1071 struct dgram_packet *dgram = &p->packet.dgram;
1072 int command = CVAL(buf,0);
1073 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1074 char scope[64];
1075 unstring src_name;
1077 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1078 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1079 if (!strequal(scope, lp_netbios_scope())) {
1080 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
1081 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, lp_netbios_scope()));
1082 return;
1085 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1086 if (is_myname(src_name)) {
1087 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Source name \
1088 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1089 return;
1092 switch (command) {
1093 case ANN_HostAnnouncement:
1094 debug_browse_data(buf, len);
1095 process_host_announce(subrec, p, buf+1);
1096 break;
1097 case ANN_DomainAnnouncement:
1098 debug_browse_data(buf, len);
1099 process_workgroup_announce(subrec, p, buf+1);
1100 break;
1101 case ANN_LocalMasterAnnouncement:
1102 debug_browse_data(buf, len);
1103 process_local_master_announce(subrec, p, buf+1);
1104 break;
1105 case ANN_AnnouncementRequest:
1106 debug_browse_data(buf, len);
1107 process_announce_request(subrec, p, buf+1);
1108 break;
1109 case ANN_Election:
1110 debug_browse_data(buf, len);
1111 process_election(subrec, p, buf+1);
1112 break;
1113 case ANN_GetBackupListReq:
1114 debug_browse_data(buf, len);
1115 process_get_backup_list_request(subrec, p, buf+1);
1116 break;
1117 case ANN_GetBackupListResp:
1118 debug_browse_data(buf, len);
1119 /* We never send ANN_GetBackupListReq so we should never get these. */
1120 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1121 packet from %s IP %s\n", nmb_namestr(&dgram->source_name), inet_ntoa(p->ip)));
1122 break;
1123 case ANN_ResetBrowserState:
1124 debug_browse_data(buf, len);
1125 process_reset_browser(subrec, p, buf+1);
1126 break;
1127 case ANN_MasterAnnouncement:
1128 /* Master browser datagrams must be processed on the unicast subnet. */
1129 subrec = unicast_subnet;
1131 debug_browse_data(buf, len);
1132 process_master_browser_announce(subrec, p, buf+1);
1133 break;
1134 case ANN_BecomeBackup:
1136 * We don't currently implement this. Log it just in case.
1138 debug_browse_data(buf, len);
1139 DEBUG(10,("process_browse_packet: On subnet %s ignoring browse packet \
1140 command ANN_BecomeBackup from %s IP %s to %s\n", subrec->subnet_name, nmb_namestr(&dgram->source_name),
1141 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1142 break;
1143 default:
1144 debug_browse_data(buf, len);
1145 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1146 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1147 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1148 break;
1152 /****************************************************************************
1153 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1154 ****************************************************************************/
1156 static void process_lanman_packet(struct packet_struct *p, const char *buf,int len)
1158 struct dgram_packet *dgram = &p->packet.dgram;
1159 int command = SVAL(buf,0);
1160 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1161 char scope[64];
1162 unstring src_name;
1164 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1166 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1167 if (!strequal(scope, lp_netbios_scope())) {
1168 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1169 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, lp_netbios_scope()));
1170 return;
1173 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1174 if (is_myname(src_name)) {
1175 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1176 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1177 return;
1180 switch (command) {
1181 case ANN_HostAnnouncement:
1182 debug_browse_data(buf, len);
1183 process_lm_host_announce(subrec, p, buf+1, len > 1 ? len-1 : 0);
1184 break;
1185 case ANN_AnnouncementRequest:
1186 process_lm_announce_request(subrec, p, buf+1, len > 1 ? len-1 : 0);
1187 break;
1188 default:
1189 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1190 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1191 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1192 break;
1196 /****************************************************************************
1197 Determine if a packet is for us on port 138. Note that to have any chance of
1198 being efficient we need to drop as many packets as possible at this
1199 stage as subsequent processing is expensive.
1200 ****************************************************************************/
1202 static bool listening(struct packet_struct *p,struct nmb_name *nbname)
1204 struct subnet_record *subrec = NULL;
1206 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1207 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1208 break;
1211 if(subrec == NULL)
1212 subrec = unicast_subnet;
1214 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1217 /****************************************************************************
1218 Process udp 138 datagrams
1219 ****************************************************************************/
1221 static void process_dgram(struct packet_struct *p)
1223 const char *buf;
1224 const char *buf2;
1225 int len;
1226 struct dgram_packet *dgram = &p->packet.dgram;
1228 /* If we aren't listening to the destination name then ignore the packet */
1229 if (!listening(p,&dgram->dest_name)) {
1230 nb_packet_dispatch(packet_server, p);
1231 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1232 nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1233 return;
1236 if (dgram->header.msg_type != 0x10 && dgram->header.msg_type != 0x11 && dgram->header.msg_type != 0x12) {
1237 nb_packet_dispatch(packet_server, p);
1238 /* Don't process error packets etc yet */
1239 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1240 an error packet of type %x\n", nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1241 return;
1244 /* Ensure we have a large enough packet before looking inside. */
1245 if (dgram->datasize < (smb_vwv12 - 2)) {
1246 /* That's the offset minus the 4 byte length + 2 bytes of offset. */
1247 DEBUG(0,("process_dgram: ignoring too short dgram packet (%u) sent to name %s from IP %s\n",
1248 (unsigned int)dgram->datasize,
1249 nmb_namestr(&dgram->dest_name),
1250 inet_ntoa(p->ip) ));
1251 return;
1254 buf = &dgram->data[0];
1255 buf -= 4; /* XXXX for the pseudo tcp length - someday I need to get rid of this */
1257 if (CVAL(buf,smb_com) != SMBtrans)
1258 return;
1260 len = SVAL(buf,smb_vwv11);
1261 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1263 if (len <= 0 || len > dgram->datasize) {
1264 DEBUG(0,("process_dgram: ignoring malformed1 (datasize = %d, len = %d) datagram \
1265 packet sent to name %s from IP %s\n",
1266 dgram->datasize,
1267 len,
1268 nmb_namestr(&dgram->dest_name),
1269 inet_ntoa(p->ip) ));
1270 return;
1273 if (buf2 < dgram->data || (buf2 >= dgram->data + dgram->datasize)) {
1274 DEBUG(0,("process_dgram: ignoring malformed2 (datasize = %d, len=%d, off=%d) datagram \
1275 packet sent to name %s from IP %s\n",
1276 dgram->datasize,
1277 len,
1278 (int)PTR_DIFF(buf2, dgram->data),
1279 nmb_namestr(&dgram->dest_name),
1280 inet_ntoa(p->ip) ));
1281 return;
1284 if ((buf2 + len < dgram->data) || (buf2 + len > dgram->data + dgram->datasize)) {
1285 DEBUG(0,("process_dgram: ignoring malformed3 (datasize = %d, len=%d, off=%d) datagram \
1286 packet sent to name %s from IP %s\n",
1287 dgram->datasize,
1288 len,
1289 (int)PTR_DIFF(buf2, dgram->data),
1290 nmb_namestr(&dgram->dest_name),
1291 inet_ntoa(p->ip) ));
1292 return;
1295 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1296 nmb_namestr(&dgram->source_name),nmb_namestr(&dgram->dest_name),
1297 inet_ntoa(p->ip), smb_buf_const(buf),CVAL(buf2,0),len));
1299 /* Datagram packet received for the browser mailslot */
1300 if (strequal(smb_buf_const(buf),BROWSE_MAILSLOT)) {
1301 process_browse_packet(p,buf2,len);
1302 return;
1305 /* Datagram packet received for the LAN Manager mailslot */
1306 if (strequal(smb_buf_const(buf),LANMAN_MAILSLOT)) {
1307 process_lanman_packet(p,buf2,len);
1308 return;
1311 /* Datagram packet received for the domain logon mailslot */
1312 if (strequal(smb_buf_const(buf),NET_LOGON_MAILSLOT)) {
1313 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1314 return;
1317 /* Datagram packet received for the NT domain logon mailslot */
1318 if (strequal(smb_buf_const(buf),NT_LOGON_MAILSLOT)) {
1319 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1320 return;
1323 nb_packet_dispatch(packet_server, p);
1326 /****************************************************************************
1327 Validate a response nmb packet.
1328 ****************************************************************************/
1330 static bool validate_nmb_response_packet( struct nmb_packet *nmb )
1332 bool ignore = False;
1334 switch (nmb->header.opcode) {
1335 case NMB_NAME_REG_OPCODE:
1336 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1337 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1338 if (nmb->header.ancount == 0) {
1339 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1340 ignore = True;
1342 break;
1344 case NMB_NAME_QUERY_OPCODE:
1345 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1)) {
1346 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1347 ignore = True;
1349 break;
1351 case NMB_NAME_RELEASE_OPCODE:
1352 if (nmb->header.ancount == 0) {
1353 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1354 ignore = True;
1356 break;
1358 case NMB_WACK_OPCODE:
1359 /* Check WACK response here. */
1360 if (nmb->header.ancount != 1) {
1361 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1362 ignore = True;
1364 break;
1365 default:
1366 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1367 nmb->header.opcode));
1368 return True;
1371 if(ignore)
1372 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1374 return ignore;
1377 /****************************************************************************
1378 Validate a request nmb packet.
1379 ****************************************************************************/
1381 static bool validate_nmb_packet( struct nmb_packet *nmb )
1383 bool ignore = False;
1385 switch (nmb->header.opcode) {
1386 case NMB_NAME_REG_OPCODE:
1387 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1388 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1389 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1390 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1391 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1392 ignore = True;
1394 break;
1396 case NMB_NAME_QUERY_OPCODE:
1397 if ((nmb->header.qdcount == 0) || ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1398 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS))) {
1399 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1400 ignore = True;
1402 break;
1404 case NMB_NAME_RELEASE_OPCODE:
1405 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1406 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1407 ignore = True;
1409 break;
1410 default:
1411 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1412 nmb->header.opcode));
1413 return True;
1416 if(ignore)
1417 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1419 return ignore;
1422 /****************************************************************************
1423 Find a subnet (and potentially a response record) for a packet.
1424 ****************************************************************************/
1426 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1427 struct response_record **pprrec)
1429 struct nmb_packet *nmb = &p->packet.nmb;
1430 struct response_record *rrec = NULL;
1431 struct subnet_record *subrec = NULL;
1433 if(pprrec != NULL)
1434 *pprrec = NULL;
1436 if(nmb->header.response) {
1437 /* It's a response packet. Find a record for it or it's an error. */
1439 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1440 if(rrec == NULL) {
1441 DEBUG(3, ("find_subnet_for_nmb_packet: response "
1442 "record not found for response id %d\n",
1443 nmb->header.name_trn_id));
1444 nb_packet_dispatch(packet_server, p);
1445 return NULL;
1448 if(subrec == NULL) {
1449 DEBUG(0, ("find_subnet_for_nmb_packet: subnet record "
1450 "not found for response id %d\n",
1451 nmb->header.name_trn_id));
1452 return NULL;
1455 if(pprrec != NULL)
1456 *pprrec = rrec;
1457 return subrec;
1460 /* Try and see what subnet this packet belongs to. */
1462 /* WINS server ? */
1463 if(packet_is_for_wins_server(p))
1464 return wins_server_subnet;
1466 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1467 if(nmb->header.nm_flags.bcast == False)
1468 return unicast_subnet;
1470 /* Go through all the broadcast subnets and see if the mask matches. */
1471 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1472 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1473 return subrec;
1476 /* If none match it must have been a directed broadcast - assign the remote_broadcast_subnet. */
1477 return remote_broadcast_subnet;
1480 /****************************************************************************
1481 Process a nmb request packet - validate the packet and route it.
1482 ****************************************************************************/
1484 static void process_nmb_request(struct packet_struct *p)
1486 struct nmb_packet *nmb = &p->packet.nmb;
1487 struct subnet_record *subrec = NULL;
1489 debug_nmb_packet(p);
1491 /* Ensure we have a good packet. */
1492 if(validate_nmb_packet(nmb))
1493 return;
1495 /* Allocate a subnet to this packet - if we cannot - fail. */
1496 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1497 return;
1499 switch (nmb->header.opcode) {
1500 case NMB_NAME_REG_OPCODE:
1501 if(subrec == wins_server_subnet)
1502 wins_process_name_registration_request(subrec, p);
1503 else
1504 process_name_registration_request(subrec, p);
1505 break;
1507 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1508 case NMB_NAME_REFRESH_OPCODE_9:
1509 if(subrec == wins_server_subnet)
1510 wins_process_name_refresh_request(subrec, p);
1511 else
1512 process_name_refresh_request(subrec, p);
1513 break;
1515 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1516 if(subrec == wins_server_subnet) {
1517 wins_process_multihomed_name_registration_request(subrec, p);
1518 } else {
1519 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1520 directed at a WINS server.\n"));
1522 break;
1524 case NMB_NAME_QUERY_OPCODE:
1525 switch (nmb->question.question_type) {
1526 case QUESTION_TYPE_NB_QUERY:
1527 if(subrec == wins_server_subnet)
1528 wins_process_name_query_request(subrec, p);
1529 else
1530 process_name_query_request(subrec, p);
1531 break;
1532 case QUESTION_TYPE_NB_STATUS:
1533 if(subrec == wins_server_subnet) {
1534 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1535 not allowed.\n"));
1536 break;
1537 } else {
1538 process_node_status_request(subrec, p);
1540 break;
1542 break;
1544 case NMB_NAME_RELEASE_OPCODE:
1545 if(subrec == wins_server_subnet)
1546 wins_process_name_release_request(subrec, p);
1547 else
1548 process_name_release_request(subrec, p);
1549 break;
1553 /****************************************************************************
1554 Process a nmb response packet - validate the packet and route it.
1555 to either the WINS server or a normal response.
1556 ****************************************************************************/
1558 static void process_nmb_response(struct packet_struct *p)
1560 struct nmb_packet *nmb = &p->packet.nmb;
1561 struct subnet_record *subrec = NULL;
1562 struct response_record *rrec = NULL;
1564 debug_nmb_packet(p);
1566 if(validate_nmb_response_packet(nmb))
1567 return;
1569 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1570 return;
1572 if(rrec == NULL) {
1573 DEBUG(0, ("process_nmb_response: response packet received but "
1574 "no response record found for id = %d. Ignoring "
1575 "packet.\n", nmb->header.name_trn_id));
1576 return;
1579 /* Increment the number of responses received for this record. */
1580 rrec->num_msgs++;
1581 /* Ensure we don't re-send the request. */
1582 rrec->repeat_count = 0;
1584 /* Call the response received function for this packet. */
1585 (*rrec->resp_fn)(subrec, rrec, p);
1588 /*******************************************************************
1589 Run elements off the packet queue till its empty
1590 ******************************************************************/
1592 void run_packet_queue(void)
1594 struct packet_struct *p;
1596 while ((p = packet_queue)) {
1597 DLIST_REMOVE(packet_queue, p);
1599 switch (p->packet_type) {
1600 case NMB_PACKET:
1601 if(p->packet.nmb.header.response)
1602 process_nmb_response(p);
1603 else
1604 process_nmb_request(p);
1605 break;
1607 case DGRAM_PACKET:
1608 process_dgram(p);
1609 break;
1611 free_packet(p);
1615 /*******************************************************************
1616 Retransmit or timeout elements from all the outgoing subnet response
1617 record queues. NOTE that this code must also check the WINS server
1618 subnet for response records to timeout as the WINS server code
1619 can send requests to check if a client still owns a name.
1620 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1621 ******************************************************************/
1623 void retransmit_or_expire_response_records(time_t t)
1625 struct subnet_record *subrec;
1627 for (subrec = FIRST_SUBNET; subrec; subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec)) {
1628 struct response_record *rrec, *nextrrec;
1630 restart:
1632 for (rrec = subrec->responselist; rrec; rrec = nextrrec) {
1633 nextrrec = rrec->next;
1635 if (rrec->repeat_time <= t) {
1636 if (rrec->repeat_count > 0) {
1637 /* Resend while we have a non-zero repeat_count. */
1638 if(!send_packet(rrec->packet)) {
1639 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1640 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1642 rrec->repeat_time = t + rrec->repeat_interval;
1643 rrec->repeat_count--;
1644 } else {
1645 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1646 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1649 * Check the flag in this record to prevent recursion if we end
1650 * up in this function again via the timeout function call.
1653 if(!rrec->in_expiration_processing) {
1656 * Set the recursion protection flag in this record.
1659 rrec->in_expiration_processing = True;
1661 /* Call the timeout function. This will deal with removing the
1662 timed out packet. */
1663 if(rrec->timeout_fn) {
1664 (*rrec->timeout_fn)(subrec, rrec);
1665 } else {
1666 /* We must remove the record ourself if there is
1667 no timeout function. */
1668 remove_response_record(subrec, rrec);
1670 /* We have changed subrec->responselist,
1671 * restart from the beginning of this list. */
1672 goto restart;
1673 } /* !rrec->in_expitation_processing */
1674 } /* rrec->repeat_count > 0 */
1675 } /* rrec->repeat_time <= t */
1676 } /* end for rrec */
1677 } /* end for subnet */
1680 /****************************************************************************
1681 Create an fd_set containing all the sockets in the subnet structures,
1682 plus the broadcast sockets.
1683 ***************************************************************************/
1685 struct socket_attributes {
1686 enum packet_type type;
1687 bool broadcast;
1688 int fd;
1689 bool triggered;
1692 static bool create_listen_array(struct socket_attributes **pattrs,
1693 int *pnum_sockets)
1695 struct subnet_record *subrec = NULL;
1696 int count = 0;
1697 int num = 0;
1698 struct socket_attributes *attrs;
1700 /* The ClientNMB and ClientDGRAM sockets */
1701 count = 2;
1703 /* Check that we can add all the fd's we need. */
1704 for (subrec = FIRST_SUBNET;
1705 subrec != NULL;
1706 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1707 if (subrec->nmb_sock != -1) {
1708 count += 1;
1710 if (subrec->dgram_sock != -1) {
1711 count += 1;
1713 if (subrec->nmb_bcast != -1) {
1714 count += 1;
1716 if (subrec->dgram_bcast != -1) {
1717 count += 1;
1721 attrs = talloc_zero_array(NULL, struct socket_attributes, count);
1722 if (attrs == NULL) {
1723 DEBUG(1, ("talloc fail for attrs. "
1724 "size %d\n", count));
1725 return true;
1728 num = 0;
1730 attrs[num].fd = ClientNMB;
1731 attrs[num].type = NMB_PACKET;
1732 attrs[num].broadcast = false;
1733 num += 1;
1735 attrs[num].fd = ClientDGRAM;
1736 attrs[num].type = DGRAM_PACKET;
1737 attrs[num].broadcast = false;
1738 num += 1;
1740 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1742 if (subrec->nmb_sock != -1) {
1743 attrs[num].fd = subrec->nmb_sock;
1744 attrs[num].type = NMB_PACKET;
1745 attrs[num].broadcast = false;
1746 num += 1;
1749 if (subrec->nmb_bcast != -1) {
1750 attrs[num].fd = subrec->nmb_bcast;
1751 attrs[num].type = NMB_PACKET;
1752 attrs[num].broadcast = true;
1753 num += 1;
1756 if (subrec->dgram_sock != -1) {
1757 attrs[num].fd = subrec->dgram_sock;
1758 attrs[num].type = DGRAM_PACKET;
1759 attrs[num].broadcast = false;
1760 num += 1;
1763 if (subrec->dgram_bcast != -1) {
1764 attrs[num].fd = subrec->dgram_bcast;
1765 attrs[num].type = DGRAM_PACKET;
1766 attrs[num].broadcast = true;
1767 num += 1;
1771 TALLOC_FREE(*pattrs);
1772 *pattrs = attrs;
1774 *pnum_sockets = count;
1776 return False;
1779 /****************************************************************************
1780 List of packets we're processing this select.
1781 ***************************************************************************/
1783 struct processed_packet {
1784 struct processed_packet *next;
1785 struct processed_packet *prev;
1786 enum packet_type packet_type;
1787 struct in_addr ip;
1788 int packet_id;
1791 /****************************************************************************
1792 Have we seen this before ?
1793 ***************************************************************************/
1795 static bool is_processed_packet(struct processed_packet *processed_packet_list,
1796 struct packet_struct *packet)
1798 struct processed_packet *p = NULL;
1800 for (p = processed_packet_list; p; p = p->next) {
1801 if (ip_equal_v4(p->ip, packet->ip) && p->packet_type == packet->packet_type) {
1802 if ((p->packet_type == NMB_PACKET) &&
1803 (p->packet_id ==
1804 packet->packet.nmb.header.name_trn_id)) {
1805 return true;
1806 } else if ((p->packet_type == DGRAM_PACKET) &&
1807 (p->packet_id ==
1808 packet->packet.dgram.header.dgm_id)) {
1809 return true;
1813 return false;
1816 /****************************************************************************
1817 Keep a list of what we've seen before.
1818 ***************************************************************************/
1820 static bool store_processed_packet(struct processed_packet **pp_processed_packet_list,
1821 struct packet_struct *packet)
1823 struct processed_packet *p = SMB_MALLOC_P(struct processed_packet);
1824 if (!p) {
1825 return false;
1827 p->packet_type = packet->packet_type;
1828 p->ip = packet->ip;
1829 if (packet->packet_type == NMB_PACKET) {
1830 p->packet_id = packet->packet.nmb.header.name_trn_id;
1831 } else if (packet->packet_type == DGRAM_PACKET) {
1832 p->packet_id = packet->packet.dgram.header.dgm_id;
1833 } else {
1834 SAFE_FREE(p);
1835 return false;
1838 DLIST_ADD(*pp_processed_packet_list, p);
1839 return true;
1842 /****************************************************************************
1843 Throw away what we've seen before.
1844 ***************************************************************************/
1846 static void free_processed_packet_list(struct processed_packet **pp_processed_packet_list)
1848 struct processed_packet *p = NULL, *next = NULL;
1850 for (p = *pp_processed_packet_list; p; p = next) {
1851 next = p->next;
1852 DLIST_REMOVE(*pp_processed_packet_list, p);
1853 SAFE_FREE(p);
1857 /****************************************************************************
1858 Timeout callback - just notice we timed out.
1859 ***************************************************************************/
1861 static void nmbd_timeout_handler(struct tevent_context *ev,
1862 struct tevent_timer *te,
1863 struct timeval current_time,
1864 void *private_data)
1866 bool *got_timeout = private_data;
1867 *got_timeout = true;
1870 /****************************************************************************
1871 fd callback - remember the fd that triggered.
1872 ***************************************************************************/
1874 static void nmbd_fd_handler(struct tevent_context *ev,
1875 struct tevent_fd *fde,
1876 uint16_t flags,
1877 void *private_data)
1879 struct socket_attributes *attr = private_data;
1880 attr->triggered = true;
1883 /****************************************************************************
1884 Read from a socket.
1885 ****************************************************************************/
1887 static ssize_t read_udp_v4_socket(
1888 int fd,
1889 char *buf,
1890 size_t len,
1891 struct sockaddr_storage *psa)
1893 ssize_t ret;
1894 socklen_t socklen = sizeof(*psa);
1895 struct sockaddr_in *si = (struct sockaddr_in *)psa;
1897 memset((char *)psa,'\0',socklen);
1899 ret = (ssize_t)sys_recvfrom(fd,buf,len,0,
1900 (struct sockaddr *)psa,&socklen);
1901 if (ret <= 0) {
1902 /* Don't print a low debug error for a non-blocking socket. */
1903 if (errno == EAGAIN) {
1904 DEBUG(10,("read_udp_v4_socket: returned EAGAIN\n"));
1905 } else {
1906 DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
1907 strerror(errno)));
1909 return 0;
1912 if (psa->ss_family != AF_INET) {
1913 DEBUG(2,("read_udp_v4_socket: invalid address family %d "
1914 "(not IPv4)\n", (int)psa->ss_family));
1915 return 0;
1918 DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
1919 inet_ntoa(si->sin_addr),
1920 si->sin_port,
1921 (unsigned long)ret));
1923 return ret;
1926 /*******************************************************************
1927 Read a packet from a socket and parse it, returning a packet ready
1928 to be used or put on the queue. This assumes a UDP socket.
1929 ******************************************************************/
1931 static struct packet_struct *read_packet(int fd,enum packet_type packet_type)
1933 struct packet_struct *packet;
1934 struct sockaddr_storage sa;
1935 struct sockaddr_in *si = (struct sockaddr_in *)&sa;
1936 char buf[MAX_DGRAM_SIZE];
1937 int length;
1939 length = read_udp_v4_socket(fd,buf,sizeof(buf),&sa);
1940 if (length < MIN_DGRAM_SIZE || sa.ss_family != AF_INET) {
1941 return NULL;
1944 packet = parse_packet(buf,
1945 length,
1946 packet_type,
1947 si->sin_addr,
1948 ntohs(si->sin_port));
1949 if (!packet)
1950 return NULL;
1952 packet->recv_fd = fd;
1953 packet->send_fd = -1;
1955 DEBUG(5,("Received a packet of len %d from (%s) port %d\n",
1956 length, inet_ntoa(packet->ip), packet->port ) );
1958 return(packet);
1961 /****************************************************************************
1962 Listens for NMB or DGRAM packets, and queues them.
1963 return True if the socket is dead
1964 ***************************************************************************/
1966 bool listen_for_packets(struct messaging_context *msg, bool run_election)
1968 static struct socket_attributes *attrs = NULL;
1969 static int listen_number = 0;
1970 int num_sockets;
1971 int i;
1972 int loop_rtn;
1973 int timeout_secs;
1975 #ifndef SYNC_DNS
1976 int dns_fd;
1977 int dns_pollidx = -1;
1978 #endif
1979 struct processed_packet *processed_packet_list = NULL;
1980 struct tevent_timer *te = NULL;
1981 bool got_timeout = false;
1982 TALLOC_CTX *frame = talloc_stackframe();
1984 if ((attrs == NULL) || rescan_listen_set) {
1985 if (create_listen_array(&attrs, &listen_number)) {
1986 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1987 TALLOC_FREE(frame);
1988 return True;
1990 rescan_listen_set = False;
1993 num_sockets = listen_number;
1995 #ifndef SYNC_DNS
1996 dns_fd = asyncdns_fd();
1997 if (dns_fd != -1) {
1998 attrs = talloc_realloc(NULL,
1999 attrs,
2000 struct socket_attributes,
2001 num_sockets + 1);
2002 if (attrs == NULL) {
2003 TALLOC_FREE(frame);
2004 return true;
2006 dns_pollidx = num_sockets;
2007 attrs[dns_pollidx].fd = dns_fd;
2009 * dummy values, we only need
2010 * fd and triggered.
2012 attrs[dns_pollidx].type = NMB_PACKET;
2013 attrs[dns_pollidx].broadcast = false;
2014 num_sockets += 1;
2016 #endif
2018 for (i=0; i<num_sockets; i++) {
2019 struct tevent_fd *tfd = tevent_add_fd(nmbd_event_context(),
2020 frame,
2021 attrs[i].fd,
2022 TEVENT_FD_READ,
2023 nmbd_fd_handler,
2024 &attrs[i]);
2025 if (tfd == NULL) {
2026 TALLOC_FREE(frame);
2027 return true;
2029 attrs[i].triggered = false;
2033 * During elections and when expecting a netbios response packet we
2034 * need to send election packets at tighter intervals.
2035 * Ideally it needs to be the interval (in ms) between time now and
2036 * the time we are expecting the next netbios packet.
2039 if (run_election||num_response_packets) {
2040 timeout_secs = 1;
2041 } else {
2042 timeout_secs = NMBD_SELECT_LOOP;
2045 te = tevent_add_timer(nmbd_event_context(),
2046 frame,
2047 tevent_timeval_current_ofs(timeout_secs, 0),
2048 nmbd_timeout_handler,
2049 &got_timeout);
2050 if (te == NULL) {
2051 TALLOC_FREE(frame);
2052 return true;
2055 loop_rtn = tevent_loop_once(nmbd_event_context());
2057 if (loop_rtn == -1) {
2058 TALLOC_FREE(frame);
2059 return true;
2062 if (got_timeout) {
2063 TALLOC_FREE(frame);
2064 return false;
2067 #ifndef SYNC_DNS
2068 if ((dns_fd != -1) && (dns_pollidx != -1) &&
2069 attrs[dns_pollidx].triggered){
2070 run_dns_queue(msg);
2071 TALLOC_FREE(frame);
2072 return false;
2074 #endif
2076 for(i = 0; i < listen_number; i++) {
2077 enum packet_type packet_type;
2078 struct packet_struct *packet;
2079 const char *packet_name;
2080 int client_fd;
2081 int client_port;
2083 if (!attrs[i].triggered) {
2084 continue;
2087 if (attrs[i].type == NMB_PACKET) {
2088 /* Port 137 */
2089 packet_type = NMB_PACKET;
2090 packet_name = "nmb";
2091 client_fd = ClientNMB;
2092 client_port = global_nmb_port;
2093 } else {
2094 /* Port 138 */
2095 packet_type = DGRAM_PACKET;
2096 packet_name = "dgram";
2097 client_fd = ClientDGRAM;
2098 client_port = DGRAM_PORT;
2101 packet = read_packet(attrs[i].fd, packet_type);
2102 if (!packet) {
2103 continue;
2107 * If we got a packet on the broadcast socket and interfaces
2108 * only is set then check it came from one of our local nets.
2110 if (lp_bind_interfaces_only() &&
2111 (attrs[i].fd == client_fd) &&
2112 (!is_local_net_v4(packet->ip))) {
2113 DEBUG(7,("discarding %s packet sent to broadcast socket from %s:%d\n",
2114 packet_name, inet_ntoa(packet->ip), packet->port));
2115 free_packet(packet);
2116 continue;
2119 if (!IS_DC) {
2120 if ((is_loopback_ip_v4(packet->ip) || ismyip_v4(packet->ip)) &&
2121 packet->port == client_port)
2123 if (client_port == DGRAM_PORT) {
2124 DEBUG(7,("discarding own dgram packet from %s:%d\n",
2125 inet_ntoa(packet->ip),packet->port));
2126 free_packet(packet);
2127 continue;
2130 if (packet->packet.nmb.header.nm_flags.bcast) {
2131 DEBUG(7,("discarding own nmb bcast packet from %s:%d\n",
2132 inet_ntoa(packet->ip),packet->port));
2133 free_packet(packet);
2134 continue;
2139 if (is_processed_packet(processed_packet_list, packet)) {
2140 DEBUG(7,("discarding duplicate packet from %s:%d\n",
2141 inet_ntoa(packet->ip),packet->port));
2142 free_packet(packet);
2143 continue;
2146 store_processed_packet(&processed_packet_list, packet);
2148 if (attrs[i].broadcast) {
2149 /* this is a broadcast socket */
2150 packet->send_fd = attrs[i-1].fd;
2151 } else {
2152 /* this is already a unicast socket */
2153 packet->send_fd = attrs[i].fd;
2156 queue_packet(packet);
2159 free_processed_packet_list(&processed_packet_list);
2160 TALLOC_FREE(frame);
2161 return False;
2164 /****************************************************************************
2165 Construct and send a netbios DGRAM.
2166 **************************************************************************/
2168 bool send_mailslot(bool unique, const char *mailslot,char *buf, size_t len,
2169 const char *srcname, int src_type,
2170 const char *dstname, int dest_type,
2171 struct in_addr dest_ip,struct in_addr src_ip,
2172 int dest_port)
2174 bool loopback_this_packet = False;
2175 struct packet_struct p;
2176 struct dgram_packet *dgram = &p.packet.dgram;
2177 char *ptr,*p2;
2178 char tmp[4];
2180 memset((char *)&p,'\0',sizeof(p));
2182 if(ismyip_v4(dest_ip) && (dest_port == DGRAM_PORT)) /* Only if to DGRAM_PORT */
2183 loopback_this_packet = True;
2185 /* generate_name_trn_id(); */ /* Not used, so gone, RJS */
2187 /* DIRECT GROUP or UNIQUE datagram. */
2188 dgram->header.msg_type = unique ? 0x10 : 0x11;
2189 dgram->header.flags.node_type = M_NODE;
2190 dgram->header.flags.first = True;
2191 dgram->header.flags.more = False;
2192 dgram->header.dgm_id = generate_name_trn_id();
2193 dgram->header.source_ip = src_ip;
2194 dgram->header.source_port = DGRAM_PORT;
2195 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
2196 dgram->header.packet_offset = 0;
2198 make_nmb_name(&dgram->source_name,srcname,src_type);
2199 make_nmb_name(&dgram->dest_name,dstname,dest_type);
2201 ptr = &dgram->data[0];
2203 /* Setup the smb part. */
2204 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
2205 memcpy(tmp,ptr,4);
2207 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
2208 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2209 return false;
2212 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
2213 memcpy(ptr,tmp,4);
2215 SCVAL(ptr,smb_com,SMBtrans);
2216 SSVAL(ptr,smb_vwv1,len);
2217 SSVAL(ptr,smb_vwv11,len);
2218 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
2219 SSVAL(ptr,smb_vwv13,3);
2220 SSVAL(ptr,smb_vwv14,1);
2221 SSVAL(ptr,smb_vwv15,1);
2222 SSVAL(ptr,smb_vwv16,2);
2223 p2 = smb_buf(ptr);
2224 strlcpy_base(p2, mailslot, dgram->data, sizeof(dgram->data));
2225 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
2227 if (((p2+len) > dgram->data+sizeof(dgram->data)) || ((p2+len) < p2)) {
2228 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2229 return False;
2230 } else {
2231 if (len) {
2232 memcpy(p2,buf,len);
2234 p2 += len;
2237 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
2239 p.ip = dest_ip;
2240 p.port = dest_port;
2241 p.recv_fd = -1;
2242 p.send_fd = find_subnet_mailslot_fd_for_address( src_ip );
2243 p.timestamp = time(NULL);
2244 p.packet_type = DGRAM_PACKET;
2246 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
2247 nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
2248 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
2250 debug_browse_data(buf, len);
2252 if(loopback_this_packet) {
2253 struct packet_struct *lo_packet = NULL;
2254 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
2255 if((lo_packet = copy_packet(&p)) == NULL)
2256 return False;
2257 queue_packet(lo_packet);
2258 return True;
2259 } else {
2260 return(send_packet(&p));