2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 extern int ClientDGRAM;
29 extern int global_nmb_port;
31 extern int DEBUGLEVEL;
33 extern int num_response_packets;
36 extern struct in_addr loopback_ip;
38 /*******************************************************************
39 The global packet linked-list. Incoming entries are
40 added to the end of this list. It is supposed to remain fairly
41 short so we won't bother with an end pointer.
42 ******************************************************************/
44 static struct packet_struct *packet_queue = NULL;
46 /***************************************************************************
47 Utility function to find the specific fd to send a packet out on.
48 **************************************************************************/
50 static int find_subnet_fd_for_address( struct in_addr local_ip )
52 struct subnet_record *subrec;
54 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
55 if(ip_equal(local_ip, subrec->myip))
56 return subrec->nmb_sock;
61 /***************************************************************************
62 Utility function to find the specific fd to send a mailslot packet out on.
63 **************************************************************************/
65 static int find_subnet_mailslot_fd_for_address( struct in_addr local_ip )
67 struct subnet_record *subrec;
69 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
70 if(ip_equal(local_ip, subrec->myip))
71 return subrec->dgram_sock;
76 /***************************************************************************
77 Get/Set problematic nb_flags as network byte order 16 bit int.
78 **************************************************************************/
80 uint16 get_nb_flags(char *buf)
82 return ((((uint16)*buf)&0xFFFF) & NB_FLGMSK);
85 void set_nb_flags(char *buf, uint16 nb_flags)
87 *buf++ = ((nb_flags & NB_FLGMSK) & 0xFF);
91 /***************************************************************************
92 Dumps out the browse packet data.
93 **************************************************************************/
95 static void debug_browse_data(char *outbuf, int len)
99 DEBUG( 4, ( "debug_browse_data():\n" ) );
100 for (i = 0; i < len; i+= 16)
102 DEBUGADD( 4, ( "%3x char ", i ) );
104 for (j = 0; j < 16; j++)
106 unsigned char x = outbuf[i+j];
107 if (x < 32 || x > 127)
112 DEBUGADD( 4, ( "%c", x ) );
115 DEBUGADD( 4, ( " hex ") );
117 for (j = 0; j < 16; j++)
121 DEBUGADD( 4, ( " %02x", (unsigned char)outbuf[i+j] ) );
124 DEBUGADD( 4, ("\n") );
128 /***************************************************************************
129 Generates the unique transaction identifier
130 **************************************************************************/
132 static uint16 name_trn_id=0;
134 static uint16 generate_name_trn_id(void)
139 name_trn_id = (time(NULL)%(unsigned)0x7FFF) + (getpid()%(unsigned)100);
141 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
145 /***************************************************************************
146 Either loops back or sends out a completed NetBIOS packet.
147 **************************************************************************/
149 static BOOL send_netbios_packet(struct packet_struct *p)
151 BOOL loopback_this_packet = False;
153 /* Check if we are sending to or from ourselves as a WINS server. */
154 if(ismyip(p->ip) && (p->port == global_nmb_port))
155 loopback_this_packet = True;
157 if(loopback_this_packet)
159 struct packet_struct *lo_packet = NULL;
160 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
161 if((lo_packet = copy_packet(p)) == NULL)
163 queue_packet(lo_packet);
165 else if (!send_packet(p))
167 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
168 inet_ntoa(p->ip),p->port));
175 /***************************************************************************
176 Sets up the common elements of an outgoing NetBIOS packet.
177 **************************************************************************/
179 static struct packet_struct *create_and_init_netbios_packet(struct nmb_name *nmbname,
181 struct in_addr to_ip)
183 struct packet_struct *packet = NULL;
184 struct nmb_packet *nmb = NULL;
186 /* Allocate the packet_struct we will return. */
187 if((packet = (struct packet_struct *)malloc(sizeof(*packet))) == NULL)
189 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
193 bzero((char *)packet,sizeof(*packet));
195 nmb = &packet->packet.nmb;
197 nmb->header.name_trn_id = generate_name_trn_id();
198 nmb->header.response = False;
199 nmb->header.nm_flags.recursion_desired = False;
200 nmb->header.nm_flags.recursion_available = False;
201 nmb->header.nm_flags.trunc = False;
202 nmb->header.nm_flags.authoritative = False;
203 nmb->header.nm_flags.bcast = bcast;
205 nmb->header.rcode = 0;
206 nmb->header.qdcount = 1;
207 nmb->header.ancount = 0;
208 nmb->header.nscount = 0;
210 nmb->question.question_name = *nmbname;
211 nmb->question.question_type = QUESTION_TYPE_NB_QUERY;
212 nmb->question.question_class = QUESTION_CLASS_IN;
215 packet->port = NMB_PORT;
216 packet->fd = ClientNMB;
217 packet->timestamp = time(NULL);
218 packet->packet_type = NMB_PACKET;
219 packet->locked = False;
221 return packet; /* Caller must free. */
224 /***************************************************************************
225 Sets up the common elements of register, refresh or release packet.
226 **************************************************************************/
228 static BOOL create_and_init_additional_record(struct packet_struct *packet,
230 struct in_addr *register_ip)
232 struct nmb_packet *nmb = &packet->packet.nmb;
234 if((nmb->additional = (struct res_rec *)malloc(sizeof(struct res_rec))) == NULL)
236 DEBUG(0,("initiate_name_register_packet: malloc fail for additional record.\n"));
240 bzero((char *)nmb->additional,sizeof(struct res_rec));
242 nmb->additional->rr_name = nmb->question.question_name;
243 nmb->additional->rr_type = RR_TYPE_NB;
244 nmb->additional->rr_class = RR_CLASS_IN;
246 nmb->additional->ttl = lp_max_ttl();
248 nmb->additional->rdlength = 6;
250 set_nb_flags(nmb->additional->rdata,nb_flags);
252 /* Set the address for the name we are registering. */
253 putip(&nmb->additional->rdata[2], register_ip);
255 /* Ensure that we send out the file descriptor to give us the
256 the specific source address we are registering as our
257 IP source address. */
259 packet->fd = find_subnet_fd_for_address( *register_ip );
264 /***************************************************************************
265 Sends out a name query.
266 **************************************************************************/
268 static BOOL initiate_name_query_packet( struct packet_struct *packet)
270 struct nmb_packet *nmb = NULL;
272 nmb = &packet->packet.nmb;
274 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
275 nmb->header.arcount = 0;
277 nmb->header.nm_flags.recursion_desired = True;
279 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
280 namestr(&nmb->question.question_name),
281 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
283 return send_netbios_packet( packet );
286 /***************************************************************************
287 Sends out a name query - from a WINS server.
288 **************************************************************************/
290 static BOOL initiate_name_query_packet_from_wins_server( struct packet_struct *packet)
292 struct nmb_packet *nmb = NULL;
294 nmb = &packet->packet.nmb;
296 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
297 nmb->header.arcount = 0;
299 nmb->header.nm_flags.recursion_desired = False;
301 DEBUG(4,("initiate_name_query_packet_from_wins_server: sending query for name %s (bcast=%s) to IP %s\n",
302 namestr(&nmb->question.question_name),
303 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
305 return send_netbios_packet( packet );
308 /***************************************************************************
309 Sends out a name register.
310 **************************************************************************/
312 static BOOL initiate_name_register_packet( struct packet_struct *packet,
313 uint16 nb_flags, struct in_addr *register_ip)
315 struct nmb_packet *nmb = &packet->packet.nmb;
317 nmb->header.opcode = NMB_NAME_REG_OPCODE;
318 nmb->header.arcount = 1;
320 nmb->header.nm_flags.recursion_desired = True;
322 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
325 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
326 namestr(&nmb->additional->rr_name),
327 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
329 return send_netbios_packet( packet );
332 /***************************************************************************
333 Sends out a multihomed name register.
334 **************************************************************************/
336 static BOOL initiate_multihomed_name_register_packet( struct packet_struct *packet,
337 uint16 nb_flags, struct in_addr *register_ip)
339 struct nmb_packet *nmb = &packet->packet.nmb;
340 fstring second_ip_buf;
342 fstrcpy(second_ip_buf, inet_ntoa(packet->ip));
344 nmb->header.opcode = NMB_NAME_MULTIHOMED_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)
352 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
353 for name %s IP %s (bcast=%s) to IP %s\n",
354 namestr(&nmb->additional->rr_name), inet_ntoa(*register_ip),
355 BOOLSTR(nmb->header.nm_flags.bcast), second_ip_buf ));
357 return send_netbios_packet( packet );
360 /***************************************************************************
361 Sends out a name refresh.
362 **************************************************************************/
364 static BOOL initiate_name_refresh_packet( struct packet_struct *packet,
365 uint16 nb_flags, struct in_addr *refresh_ip)
367 struct nmb_packet *nmb = &packet->packet.nmb;
369 nmb->header.opcode = NMB_NAME_REFRESH_OPCODE_8;
370 nmb->header.arcount = 1;
372 nmb->header.nm_flags.recursion_desired = False;
374 if(create_and_init_additional_record(packet, nb_flags, refresh_ip) == False)
377 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
378 namestr(&nmb->additional->rr_name),
379 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
381 return send_netbios_packet( packet );
384 /***************************************************************************
385 Sends out a name release.
386 **************************************************************************/
388 static BOOL initiate_name_release_packet( struct packet_struct *packet,
389 uint16 nb_flags, struct in_addr *release_ip)
391 struct nmb_packet *nmb = &packet->packet.nmb;
393 nmb->header.opcode = NMB_NAME_RELEASE_OPCODE;
394 nmb->header.arcount = 1;
396 nmb->header.nm_flags.recursion_desired = False;
398 if(create_and_init_additional_record(packet, nb_flags, release_ip) == False)
401 DEBUG(4,("initiate_name_release_packet: sending release for name %s (bcast=%s) to IP %s\n",
402 namestr(&nmb->additional->rr_name),
403 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
405 return send_netbios_packet( packet );
408 /***************************************************************************
409 Sends out a node status.
410 **************************************************************************/
412 static BOOL initiate_node_status_packet( struct packet_struct *packet )
414 struct nmb_packet *nmb = &packet->packet.nmb;
416 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
417 nmb->header.arcount = 0;
419 nmb->header.nm_flags.recursion_desired = False;
421 nmb->question.question_type = QUESTION_TYPE_NB_STATUS;
423 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
424 namestr(&nmb->question.question_name),
425 inet_ntoa(packet->ip)));
427 return send_netbios_packet( packet );
430 /****************************************************************************
431 Simplification functions for queuing standard packets.
432 These should be the only publicly callable functions for sending
434 ****************************************************************************/
436 /****************************************************************************
437 Assertion - we should never be sending nmbd packets on the remote
439 ****************************************************************************/
441 static BOOL assert_check_subnet(struct subnet_record *subrec)
443 if( subrec == remote_broadcast_subnet)
445 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
452 /****************************************************************************
453 Queue a register name packet to the broadcast address of a subnet.
454 ****************************************************************************/
456 struct response_record *queue_register_name( struct subnet_record *subrec,
457 response_function resp_fn,
458 timeout_response_function timeout_fn,
459 register_name_success_function success_fn,
460 register_name_fail_function fail_fn,
461 struct userdata_struct *userdata,
462 struct nmb_name *nmbname,
465 struct packet_struct *p;
466 struct response_record *rrec;
467 BOOL bcast = (subrec == unicast_subnet) ? False : True;
469 if(assert_check_subnet(subrec))
472 if(( p = create_and_init_netbios_packet(nmbname, bcast,
473 subrec->bcast_ip)) == NULL)
476 if(initiate_name_register_packet( p, nb_flags,
477 iface_ip(subrec->bcast_ip)) == False)
484 if((rrec = make_response_record(subrec, /* subnet record. */
485 p, /* packet we sent. */
486 resp_fn, /* function to call on response. */
487 timeout_fn, /* function to call on timeout. */
488 (success_function)success_fn, /* function to call on operation success. */
489 (fail_function)fail_fn, /* function to call on operation fail. */
500 /****************************************************************************
501 Queue a multihomed register name packet to the broadcast address of a subnet.
502 ****************************************************************************/
504 struct response_record *queue_register_multihomed_name( struct subnet_record *subrec,
505 response_function resp_fn,
506 timeout_response_function timeout_fn,
507 register_name_success_function success_fn,
508 register_name_fail_function fail_fn,
509 struct userdata_struct *userdata,
510 struct nmb_name *nmbname,
512 struct in_addr register_ip)
514 struct packet_struct *p;
515 struct response_record *rrec;
520 if(subrec != unicast_subnet)
522 DEBUG(0,("queue_register_multihomed_name: should only be done on \
523 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
527 if(assert_check_subnet(subrec))
530 if(( p = create_and_init_netbios_packet(nmbname, bcast,
531 subrec->bcast_ip)) == NULL)
534 if (nb_flags & NB_GROUP)
535 ret = initiate_name_register_packet( p, nb_flags, ®ister_ip);
537 ret = initiate_multihomed_name_register_packet( p, nb_flags, ®ister_ip);
546 if((rrec = make_response_record(subrec, /* subnet record. */
547 p, /* packet we sent. */
548 resp_fn, /* function to call on response. */
549 timeout_fn, /* function to call on timeout. */
550 (success_function)success_fn, /* function to call on operation success. */
551 (fail_function)fail_fn, /* function to call on operation fail. */
562 /****************************************************************************
563 Queue a release name packet to the broadcast address of a subnet.
564 ****************************************************************************/
566 struct response_record *queue_release_name( struct subnet_record *subrec,
567 response_function resp_fn,
568 timeout_response_function timeout_fn,
569 release_name_success_function success_fn,
570 release_name_fail_function fail_fn,
571 struct userdata_struct *userdata,
572 struct nmb_name *nmbname,
574 struct in_addr release_ip)
576 BOOL bcast = (subrec == unicast_subnet) ? False : True;
577 struct packet_struct *p;
578 struct response_record *rrec;
580 if(assert_check_subnet(subrec))
583 if(( p = create_and_init_netbios_packet(nmbname, bcast,
584 subrec->bcast_ip)) == NULL)
587 if(initiate_name_release_packet( p, nb_flags, &release_ip) == False)
594 if((rrec = make_response_record(subrec, /* subnet record. */
595 p, /* packet we sent. */
596 resp_fn, /* function to call on response. */
597 timeout_fn, /* function to call on timeout. */
598 (success_function)success_fn, /* function to call on operation success. */
599 (fail_function)fail_fn, /* function to call on operation fail. */
608 * For a broadcast release packet, only send once.
609 * This will cause us to remove the name asap. JRA.
614 rrec->repeat_count = 0;
615 rrec->repeat_time = 0;
621 /****************************************************************************
622 Queue a refresh name packet to the broadcast address of a subnet.
623 ****************************************************************************/
625 struct response_record *queue_refresh_name( struct subnet_record *subrec,
626 response_function resp_fn,
627 timeout_response_function timeout_fn,
628 refresh_name_success_function success_fn,
629 refresh_name_fail_function fail_fn,
630 struct userdata_struct *userdata,
631 struct name_record *namerec,
632 struct in_addr refresh_ip)
634 BOOL bcast = (subrec == unicast_subnet) ? False : True;
635 struct packet_struct *p;
636 struct response_record *rrec;
638 if(assert_check_subnet(subrec))
641 if(( p = create_and_init_netbios_packet(&namerec->name, bcast,
642 subrec->bcast_ip)) == NULL)
645 if( !initiate_name_refresh_packet( p, namerec->data.nb_flags, &refresh_ip ) )
652 if((rrec = make_response_record(subrec, /* subnet record. */
653 p, /* packet we sent. */
654 resp_fn, /* function to call on response. */
655 timeout_fn, /* function to call on timeout. */
656 (success_function)success_fn, /* function to call on operation success. */
657 (fail_function)fail_fn, /* function to call on operation fail. */
668 /****************************************************************************
669 Queue a query name packet to the broadcast address of a subnet.
670 ****************************************************************************/
672 struct response_record *queue_query_name( struct subnet_record *subrec,
673 response_function resp_fn,
674 timeout_response_function timeout_fn,
675 query_name_success_function success_fn,
676 query_name_fail_function fail_fn,
677 struct userdata_struct *userdata,
678 struct nmb_name *nmbname)
680 struct packet_struct *p;
681 struct response_record *rrec;
684 if ((subrec == unicast_subnet) || (subrec == wins_server_subnet))
687 if(assert_check_subnet(subrec))
690 if(( p = create_and_init_netbios_packet(nmbname, bcast,
691 subrec->bcast_ip)) == NULL)
694 if(initiate_name_query_packet( p ) == False)
701 if((rrec = make_response_record(subrec, /* subnet record. */
702 p, /* packet we sent. */
703 resp_fn, /* function to call on response. */
704 timeout_fn, /* function to call on timeout. */
705 (success_function)success_fn, /* function to call on operation success. */
706 (fail_function)fail_fn, /* function to call on operation fail. */
717 /****************************************************************************
718 Queue a query name packet to a given address from the WINS subnet.
719 ****************************************************************************/
721 struct response_record *queue_query_name_from_wins_server( struct in_addr to_ip,
722 response_function resp_fn,
723 timeout_response_function timeout_fn,
724 query_name_success_function success_fn,
725 query_name_fail_function fail_fn,
726 struct userdata_struct *userdata,
727 struct nmb_name *nmbname)
729 struct packet_struct *p;
730 struct response_record *rrec;
733 if(( p = create_and_init_netbios_packet(nmbname, bcast, to_ip)) == NULL)
736 if(initiate_name_query_packet_from_wins_server( p ) == False)
743 if((rrec = make_response_record(wins_server_subnet, /* subnet record. */
744 p, /* packet we sent. */
745 resp_fn, /* function to call on response. */
746 timeout_fn, /* function to call on timeout. */
747 (success_function)success_fn, /* function to call on operation success. */
748 (fail_function)fail_fn, /* function to call on operation fail. */
759 /****************************************************************************
760 Queue a node status packet to a given name and address.
761 ****************************************************************************/
763 struct response_record *queue_node_status( struct subnet_record *subrec,
764 response_function resp_fn,
765 timeout_response_function timeout_fn,
766 node_status_success_function success_fn,
767 node_status_fail_function fail_fn,
768 struct userdata_struct *userdata,
769 struct nmb_name *nmbname,
770 struct in_addr send_ip)
772 struct packet_struct *p;
773 struct response_record *rrec;
777 if(subrec != unicast_subnet)
779 DEBUG(0,("queue_register_multihomed_name: should only be done on \
780 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
784 if(assert_check_subnet(subrec))
787 if(( p = create_and_init_netbios_packet(nmbname, bcast,
791 if(initiate_node_status_packet(p) == False)
798 if((rrec = make_response_record(subrec, /* subnet record. */
799 p, /* packet we sent. */
800 resp_fn, /* function to call on response. */
801 timeout_fn, /* function to call on timeout. */
802 (success_function)success_fn, /* function to call on operation success. */
803 (fail_function)fail_fn, /* function to call on operation fail. */
814 /****************************************************************************
815 Reply to a netbios name packet. see rfc1002.txt
816 ****************************************************************************/
818 void reply_netbios_packet(struct packet_struct *orig_packet,
819 int rcode, enum netbios_reply_type_code rcv_code, int opcode,
820 int ttl, char *data,int len)
822 struct packet_struct packet;
823 struct nmb_packet *nmb = NULL;
824 struct res_rec answers;
825 struct nmb_packet *orig_nmb = &orig_packet->packet.nmb;
826 BOOL loopback_this_packet = False;
827 char *packet_type = "unknown";
829 /* Check if we are sending to or from ourselves. */
830 if(ismyip(orig_packet->ip) && (orig_packet->port == global_nmb_port))
831 loopback_this_packet = True;
833 nmb = &packet.packet.nmb;
835 /* Do a partial copy of the packet. We clear the locked flag and
836 the resource record pointers. */
837 packet = *orig_packet; /* Full structure copy. */
838 packet.locked = False;
841 nmb->additional = NULL;
847 packet_type = "nmb_status";
848 nmb->header.nm_flags.recursion_desired = False;
849 nmb->header.nm_flags.recursion_available = False;
854 packet_type = "nmb_query";
855 nmb->header.nm_flags.recursion_desired = True;
856 nmb->header.nm_flags.recursion_available = True;
860 case NMB_REG_REFRESH:
862 packet_type = "nmb_reg";
863 nmb->header.nm_flags.recursion_desired = True;
864 nmb->header.nm_flags.recursion_available = True;
869 packet_type = "nmb_rel";
870 nmb->header.nm_flags.recursion_desired = False;
871 nmb->header.nm_flags.recursion_available = False;
876 packet_type = "nmb_wack";
877 nmb->header.nm_flags.recursion_desired = False;
878 nmb->header.nm_flags.recursion_available = False;
883 packet_type = "wins_reg";
884 nmb->header.nm_flags.recursion_desired = True;
885 nmb->header.nm_flags.recursion_available = True;
890 packet_type = "wins_query";
891 nmb->header.nm_flags.recursion_desired = True;
892 nmb->header.nm_flags.recursion_available = True;
898 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
899 packet_type, namestr(&orig_nmb->question.question_name),
900 inet_ntoa(packet.ip)));
906 DEBUG(4,("reply_netbios_packet: sending a reply of packet type: %s %s to ip %s \
908 packet_type, namestr(&orig_nmb->question.question_name),
909 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
911 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
912 nmb->header.opcode = opcode;
913 nmb->header.response = True;
914 nmb->header.nm_flags.bcast = False;
915 nmb->header.nm_flags.trunc = False;
916 nmb->header.nm_flags.authoritative = True;
918 nmb->header.rcode = rcode;
919 nmb->header.qdcount = 0;
920 nmb->header.ancount = 1;
921 nmb->header.nscount = 0;
922 nmb->header.arcount = 0;
924 bzero((char*)&nmb->question,sizeof(nmb->question));
926 nmb->answers = &answers;
927 bzero((char*)nmb->answers,sizeof(*nmb->answers));
929 nmb->answers->rr_name = orig_nmb->question.question_name;
930 nmb->answers->rr_type = orig_nmb->question.question_type;
931 nmb->answers->rr_class = orig_nmb->question.question_class;
932 nmb->answers->ttl = ttl;
936 nmb->answers->rdlength = len;
937 memcpy(nmb->answers->rdata, data, len);
940 packet.packet_type = NMB_PACKET;
941 /* Ensure we send out on the same fd that the original
942 packet came in on to give the correct source IP address. */
943 packet.fd = orig_packet->fd;
944 packet.timestamp = time(NULL);
946 debug_nmb_packet(&packet);
948 if(loopback_this_packet)
950 struct packet_struct *lo_packet;
951 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
952 if((lo_packet = copy_packet(&packet)) == NULL)
954 queue_packet(lo_packet);
956 else if (!send_packet(&packet))
958 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
959 inet_ntoa(packet.ip),packet.port));
963 /*******************************************************************
964 Queue a packet into a packet queue
965 ******************************************************************/
967 void queue_packet(struct packet_struct *packet)
969 struct packet_struct *p;
975 packet_queue = packet;
979 /* find the bottom */
980 for (p=packet_queue;p->next;p=p->next)
988 /****************************************************************************
989 Try and find a matching subnet record for a datagram port 138 packet.
990 ****************************************************************************/
992 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
994 struct subnet_record *subrec;
996 /* Go through all the broadcast subnets and see if the mask matches. */
997 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
999 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1003 /* If the subnet record is the remote announce broadcast subnet,
1004 hack it here to be the first subnet. This is really gross and
1005 is needed due to people turning on port 137/138 broadcast
1006 forwarding on their routers. May fire and brimstone rain
1010 return FIRST_SUBNET;
1013 /****************************************************************************
1014 Dispatch a browse frame from port 138 to the correct processing function.
1015 ****************************************************************************/
1017 void process_browse_packet(struct packet_struct *p, char *buf,int len)
1019 struct dgram_packet *dgram = &p->packet.dgram;
1020 int command = CVAL(buf,0);
1021 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1023 /* Drop the packet if it's a different NetBIOS scope, or
1024 the source is from one of our names. */
1026 if (!strequal(dgram->dest_name.scope,scope ))
1028 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
1029 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
1033 if (is_myname(dgram->source_name.name))
1035 DEBUG(0,("process_browse_packet: Discarding datagram from IP %s. Source name \
1036 %s is one of our names !\n", inet_ntoa(p->ip), namestr(&dgram->source_name)));
1042 case ANN_HostAnnouncement:
1044 debug_browse_data(buf, len);
1045 process_host_announce(subrec, p, buf+1);
1048 case ANN_DomainAnnouncement:
1050 debug_browse_data(buf, len);
1051 process_workgroup_announce(subrec, p, buf+1);
1054 case ANN_LocalMasterAnnouncement:
1056 debug_browse_data(buf, len);
1057 process_local_master_announce(subrec, p, buf+1);
1060 case ANN_AnnouncementRequest:
1062 debug_browse_data(buf, len);
1063 process_announce_request(subrec, p, buf+1);
1068 debug_browse_data(buf, len);
1069 process_election(subrec, p, buf+1);
1072 case ANN_GetBackupListReq:
1074 debug_browse_data(buf, len);
1076 /* This is one occasion where we change a subnet that is
1077 given to us. If the packet was sent to WORKGROUP<1b> instead
1078 of WORKGROUP<1d> then it was unicast to us a domain master
1079 browser. Change subrec to unicast.
1081 if(dgram->dest_name.name_type == 0x1b)
1082 subrec = unicast_subnet;
1084 process_get_backup_list_request(subrec, p, buf+1);
1087 case ANN_GetBackupListResp:
1089 debug_browse_data(buf, len);
1090 /* We never send ANN_GetBackupListReq so we
1091 should never get these. */
1092 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1093 packet from %s IP %s\n", namestr(&dgram->source_name), inet_ntoa(p->ip)));
1096 case ANN_ResetBrowserState:
1098 debug_browse_data(buf, len);
1099 process_reset_browser(subrec, p, buf+1);
1102 case ANN_MasterAnnouncement:
1104 /* Master browser datagrams must be processed
1105 on the unicast subnet. */
1106 subrec = unicast_subnet;
1108 debug_browse_data(buf, len);
1109 process_master_browser_announce(subrec, p, buf+1);
1112 case ANN_BecomeBackup:
1115 * We don't currently implement this. Log it just in case.
1117 debug_browse_data(buf, len);
1118 DEBUG(10,("process_browse_packet: On subnet %s ignoring browse packet \
1119 command ANN_BecomeBackup from %s IP %s to %s\n",
1120 subrec->subnet_name, namestr(&dgram->source_name),
1121 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1126 debug_browse_data(buf, len);
1127 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1128 command code %d from %s IP %s to %s\n",
1129 subrec->subnet_name, command, namestr(&dgram->source_name),
1130 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1135 /****************************************************************************
1136 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1137 ****************************************************************************/
1139 void process_lanman_packet(struct packet_struct *p, char *buf,int len)
1141 struct dgram_packet *dgram = &p->packet.dgram;
1142 int command = SVAL(buf,0);
1143 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1145 /* Drop the packet if it's a different NetBIOS scope, or
1146 the source is from one of our names. */
1148 if (!strequal(dgram->dest_name.scope,scope ))
1150 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1151 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
1155 if (is_myname(dgram->source_name.name))
1157 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1158 %s is one of our names !\n", inet_ntoa(p->ip), namestr(&dgram->source_name)));
1164 case ANN_HostAnnouncement:
1166 debug_browse_data(buf, len);
1167 process_lm_host_announce(subrec, p, buf+1);
1170 case ANN_AnnouncementRequest:
1172 process_lm_announce_request(subrec, p, buf+1);
1177 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1178 command code %d from %s IP %s to %s\n",
1179 subrec->subnet_name, command, namestr(&dgram->source_name),
1180 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1185 /****************************************************************************
1186 Determine if a packet is for us on port 138. Note that to have any chance of
1187 being efficient we need to drop as many packets as possible at this
1188 stage as subsequent processing is expensive.
1189 ****************************************************************************/
1191 static BOOL listening(struct packet_struct *p,struct nmb_name *nbname)
1193 struct subnet_record *subrec = NULL;
1195 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1197 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1202 subrec = unicast_subnet;
1204 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1207 /****************************************************************************
1208 Process udp 138 datagrams
1209 ****************************************************************************/
1211 static void process_dgram(struct packet_struct *p)
1216 struct dgram_packet *dgram = &p->packet.dgram;
1218 /* If we aren't listening to the destination name then ignore the packet */
1219 if (!listening(p,&dgram->dest_name))
1221 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1222 namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1226 if (dgram->header.msg_type != 0x10 &&
1227 dgram->header.msg_type != 0x11 &&
1228 dgram->header.msg_type != 0x12)
1230 /* Don't process error packets etc yet */
1231 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1232 an error packet of type %x\n",
1233 namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1237 buf = &dgram->data[0];
1238 buf -= 4; /* XXXX for the pseudo tcp length -
1239 someday I need to get rid of this */
1241 if (CVAL(buf,smb_com) != SMBtrans)
1244 len = SVAL(buf,smb_vwv11);
1245 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1247 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1248 namestr(&dgram->source_name),namestr(&dgram->dest_name),
1249 inet_ntoa(p->ip), smb_buf(buf),CVAL(buf2,0),len));
1255 /* Datagram packet received for the browser mailslot */
1256 if (strequal(smb_buf(buf),BROWSE_MAILSLOT))
1258 process_browse_packet(p,buf2,len);
1262 /* Datagram packet received for the LAN Manager mailslot */
1263 if (strequal(smb_buf(buf),LANMAN_MAILSLOT)) {
1264 process_lanman_packet(p,buf2,len);
1268 /* Datagram packet received for the domain logon mailslot */
1269 if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT))
1271 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1275 /* Datagram packet received for the NT domain logon mailslot */
1276 if (strequal(smb_buf(buf),NT_LOGON_MAILSLOT))
1278 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1283 /****************************************************************************
1284 Validate a response nmb packet.
1285 ****************************************************************************/
1287 BOOL validate_nmb_response_packet( struct nmb_packet *nmb )
1289 BOOL ignore = False;
1291 switch (nmb->header.opcode)
1293 case NMB_NAME_REG_OPCODE:
1294 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1295 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1296 if (nmb->header.ancount == 0)
1298 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1303 case NMB_NAME_QUERY_OPCODE:
1304 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1))
1306 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1310 case NMB_NAME_RELEASE_OPCODE:
1311 if (nmb->header.ancount == 0)
1313 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1317 case NMB_WACK_OPCODE:
1318 /* Check WACK response here. */
1319 if (nmb->header.ancount != 1)
1321 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1326 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1327 nmb->header.opcode));
1332 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1337 /****************************************************************************
1338 Validate a request nmb packet.
1339 ****************************************************************************/
1341 BOOL validate_nmb_packet( struct nmb_packet *nmb )
1343 BOOL ignore = False;
1345 switch (nmb->header.opcode)
1347 case NMB_NAME_REG_OPCODE:
1348 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1349 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1350 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1351 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1353 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1358 case NMB_NAME_QUERY_OPCODE:
1359 if ((nmb->header.qdcount == 0) ||
1360 ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1361 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS)))
1363 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1368 case NMB_NAME_RELEASE_OPCODE:
1369 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1371 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1376 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1377 nmb->header.opcode));
1382 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1387 /****************************************************************************
1388 Find a subnet (and potentially a response record) for a packet.
1389 ****************************************************************************/
1391 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1392 struct response_record **pprrec)
1394 struct nmb_packet *nmb = &p->packet.nmb;
1395 struct response_record *rrec = NULL;
1396 struct subnet_record *subrec = NULL;
1401 if(nmb->header.response)
1403 /* It's a response packet. Find a record for it or it's an error. */
1405 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1408 DEBUG(0,("find_subnet_for_nmb_packet: response record not found for response id %hu\n",
1409 nmb->header.name_trn_id));
1415 DEBUG(0,("find_subnet_for_nmb_packet: subnet record not found for response id %hu\n",
1416 nmb->header.name_trn_id));
1425 /* Try and see what subnet this packet belongs to. */
1428 if(packet_is_for_wins_server(p))
1429 return wins_server_subnet;
1431 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1432 if(nmb->header.nm_flags.bcast == False)
1433 return unicast_subnet;
1435 /* Go through all the broadcast subnets and see if the mask matches. */
1436 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1438 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1442 /* If none match it must have been a directed broadcast - assign
1443 the remote_broadcast_subnet. */
1444 return remote_broadcast_subnet;
1447 /****************************************************************************
1448 Process a nmb request packet - validate the packet and route it.
1449 ****************************************************************************/
1451 static void process_nmb_request(struct packet_struct *p)
1453 struct nmb_packet *nmb = &p->packet.nmb;
1454 struct subnet_record *subrec = NULL;
1456 debug_nmb_packet(p);
1458 /* Ensure we have a good packet. */
1459 if(validate_nmb_packet(nmb))
1462 /* Allocate a subnet to this packet - if we cannot - fail. */
1463 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1466 switch (nmb->header.opcode)
1468 case NMB_NAME_REG_OPCODE:
1469 if(subrec == wins_server_subnet)
1470 wins_process_name_registration_request(subrec, p);
1472 process_name_registration_request(subrec, p);
1475 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1476 case NMB_NAME_REFRESH_OPCODE_9:
1477 if(subrec == wins_server_subnet)
1478 wins_process_name_refresh_request(subrec, p);
1480 process_name_refresh_request(subrec, p);
1483 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1484 if(subrec == wins_server_subnet)
1485 wins_process_multihomed_name_registration_request(subrec, p);
1488 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1489 directed at a WINS server.\n"));
1493 case NMB_NAME_QUERY_OPCODE:
1494 switch (nmb->question.question_type)
1496 case QUESTION_TYPE_NB_QUERY:
1498 if(subrec == wins_server_subnet)
1499 wins_process_name_query_request(subrec, p);
1501 process_name_query_request(subrec, p);
1504 case QUESTION_TYPE_NB_STATUS:
1506 if(subrec == wins_server_subnet)
1508 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1513 process_node_status_request(subrec, p);
1519 case NMB_NAME_RELEASE_OPCODE:
1520 if(subrec == wins_server_subnet)
1521 wins_process_name_release_request(subrec, p);
1523 process_name_release_request(subrec, p);
1528 /****************************************************************************
1529 Process a nmb response packet - validate the packet and route it.
1530 to either the WINS server or a normal response.
1531 ****************************************************************************/
1533 static void process_nmb_response(struct packet_struct *p)
1535 struct nmb_packet *nmb = &p->packet.nmb;
1536 struct subnet_record *subrec = NULL;
1537 struct response_record *rrec = NULL;
1539 debug_nmb_packet(p);
1541 if(validate_nmb_response_packet(nmb))
1544 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1549 DEBUG(0,("process_nmb_response: response packet received but no response record \
1550 found for id = %hu. Ignoring packet.\n", nmb->header.name_trn_id));
1554 /* Increment the number of responses received for this record. */
1556 /* Ensure we don't re-send the request. */
1557 rrec->repeat_count = 0;
1559 /* Call the response received function for this packet. */
1560 (*rrec->resp_fn)(subrec, rrec, p);
1564 /*******************************************************************
1565 Run elements off the packet queue till its empty
1566 ******************************************************************/
1568 void run_packet_queue(void)
1570 struct packet_struct *p;
1572 while ((p = packet_queue))
1574 packet_queue = p->next;
1576 packet_queue->prev = NULL;
1577 p->next = p->prev = NULL;
1579 switch (p->packet_type)
1582 if(p->packet.nmb.header.response)
1583 process_nmb_response(p);
1585 process_nmb_request(p);
1596 /*******************************************************************
1597 Retransmit or timeout elements from all the outgoing subnet response
1598 record queues. NOTE that this code must also check the WINS server
1599 subnet for response records to timeout as the WINS server code
1600 can send requests to check if a client still owns a name.
1601 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1602 ******************************************************************/
1604 void retransmit_or_expire_response_records(time_t t)
1606 struct subnet_record *subrec;
1608 for (subrec = FIRST_SUBNET; subrec;
1609 subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec))
1611 struct response_record *rrec, *nextrrec;
1613 for (rrec = subrec->responselist; rrec; rrec = nextrrec)
1615 nextrrec = rrec->next;
1617 if (rrec->repeat_time <= t)
1619 if (rrec->repeat_count > 0)
1621 /* Resend while we have a non-zero repeat_count. */
1622 if(!send_packet(rrec->packet))
1624 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1625 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1626 subrec->subnet_name));
1628 rrec->repeat_time += rrec->repeat_interval;
1629 rrec->repeat_count--;
1633 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1634 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1635 subrec->subnet_name));
1637 /* Call the timeout function. This will deal with removing the
1638 timed out packet. */
1639 if(rrec->timeout_fn)
1640 (*rrec->timeout_fn)(subrec, rrec);
1643 /* We must remove the record ourself if there is
1644 no timeout function. */
1645 remove_response_record(subrec, rrec);
1647 } /* rrec->repeat_count > 0 */
1648 } /* rrec->repeat_time <= t */
1649 } /* end for rrec */
1650 } /* end for subnet */
1653 /****************************************************************************
1654 Create an fd_set containing all the sockets in the subnet structures,
1655 plus the broadcast sockets.
1656 ***************************************************************************/
1658 static BOOL create_listen_fdset(fd_set **ppset, int **psock_array, int *listen_number)
1660 int *sock_array = NULL;
1661 struct subnet_record *subrec = NULL;
1664 fd_set *pset = (fd_set *)malloc(sizeof(fd_set));
1668 DEBUG(0,("create_listen_fdset: malloc fail !\n"));
1672 /* Check that we can add all the fd's we need. */
1673 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1676 if((count*2) + 2 > FD_SETSIZE)
1678 DEBUG(0,("create_listen_fdset: Too many file descriptors needed (%d). We can \
1679 only use %d.\n", (count*2) + 2, FD_SETSIZE));
1683 if((sock_array = (int *)malloc(((count*2) + 2)*sizeof(int))) == NULL)
1685 DEBUG(0,("create_listen_fdset: malloc fail for socket array.\n"));
1691 /* Add in the broadcast socket on 137. */
1692 FD_SET(ClientNMB,pset);
1693 sock_array[num++] = ClientNMB;
1695 /* Add in the 137 sockets on all the interfaces. */
1696 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1698 FD_SET(subrec->nmb_sock,pset);
1699 sock_array[num++] = subrec->nmb_sock;
1702 /* Add in the broadcast socket on 138. */
1703 FD_SET(ClientDGRAM,pset);
1704 sock_array[num++] = ClientDGRAM;
1706 /* Add in the 138 sockets on all the interfaces. */
1707 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1709 FD_SET(subrec->dgram_sock,pset);
1710 sock_array[num++] = subrec->dgram_sock;
1713 *listen_number = (count*2) + 2;
1715 *psock_array = sock_array;
1720 /****************************************************************************
1721 Listens for NMB or DGRAM packets, and queues them.
1722 ***************************************************************************/
1724 BOOL listen_for_packets(BOOL run_election)
1726 static fd_set *listen_set = NULL;
1727 static int listen_number = 0;
1728 static int *sock_array = NULL;
1732 struct timeval timeout;
1737 if(listen_set == NULL)
1739 if(create_listen_fdset(&listen_set, &sock_array, &listen_number))
1741 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1746 memcpy((char *)&fds, (char *)listen_set, sizeof(fd_set));
1749 dns_fd = asyncdns_fd();
1751 FD_SET(dns_fd, &fds);
1757 * During elections and when expecting a netbios response packet we
1758 * need to send election packets at tighter intervals.
1759 * Ideally it needs to be the interval (in ms) between time now and
1760 * the time we are expecting the next netbios packet.
1763 timeout.tv_sec = (run_election||num_response_packets) ? 1 : NMBD_SELECT_LOOP;
1764 timeout.tv_usec = 0;
1766 /* Prepare for the select - allow certain signals. */
1768 BlockSignals(False, SIGTERM);
1769 #if defined(SIGUSR1)
1770 BlockSignals(False, SIGUSR1);
1771 #endif /* SIGUSR1 */
1772 #if defined(SIGUSR2)
1773 BlockSignals(False, SIGUSR2);
1774 #endif /* SIGUSR2 */
1776 selrtn = sys_select(256,&fds,&timeout);
1778 /* We can only take signals when we are in the select - block them again here. */
1780 BlockSignals(True, SIGTERM);
1781 #if defined(SIGUSR1)
1782 BlockSignals(True, SIGUSR1);
1783 #endif /* SIGUSR1 */
1784 #if defined(SIGUSR2)
1785 BlockSignals(True, SIGUSR2);
1786 #endif /* SIGUSR2 */
1793 if (dns_fd != -1 && FD_ISSET(dns_fd,&fds)) {
1798 for(i = 0; i < listen_number; i++)
1800 if(i < (listen_number/2))
1802 /* Processing a 137 socket. */
1803 if (FD_ISSET(sock_array[i],&fds))
1805 struct packet_struct *packet = read_packet(sock_array[i], NMB_PACKET);
1809 * If we got a packet on the broadcast socket and interfaces
1810 * only is set then check it came from one of our local nets.
1812 if(lp_bind_interfaces_only() && (sock_array[i] == ClientNMB) &&
1813 (!is_local_net(packet->ip)))
1815 DEBUG(7,("discarding nmb packet sent to broadcast socket from %s:%d\n",
1816 inet_ntoa(packet->ip),packet->port));
1817 free_packet(packet);
1819 else if ((ip_equal(loopback_ip, packet->ip) ||
1820 ismyip(packet->ip)) && packet->port == global_nmb_port)
1822 DEBUG(7,("discarding own packet from %s:%d\n",
1823 inet_ntoa(packet->ip),packet->port));
1824 free_packet(packet);
1828 /* Save the file descriptor this packet came in on. */
1829 packet->fd = sock_array[i];
1830 queue_packet(packet);
1837 /* Processing a 138 socket. */
1839 if (FD_ISSET(sock_array[i],&fds))
1841 struct packet_struct *packet = read_packet(sock_array[i], DGRAM_PACKET);
1845 * If we got a packet on the broadcast socket and interfaces
1846 * only is set then check it came from one of our local nets.
1848 if(lp_bind_interfaces_only() && (sock_array[i] == ClientDGRAM) &&
1849 (!is_local_net(packet->ip)))
1851 DEBUG(7,("discarding dgram packet sent to broadcast socket from %s:%d\n",
1852 inet_ntoa(packet->ip),packet->port));
1853 free_packet(packet);
1855 else if ((ip_equal(loopback_ip, packet->ip) ||
1856 ismyip(packet->ip)) && packet->port == DGRAM_PORT)
1858 DEBUG(7,("discarding own packet from %s:%d\n",
1859 inet_ntoa(packet->ip),packet->port));
1860 free_packet(packet);
1864 /* Save the file descriptor this packet came in on. */
1865 packet->fd = sock_array[i];
1866 queue_packet(packet);
1870 } /* end processing 138 socket. */
1872 } /* end if selret > 0 */
1876 /****************************************************************************
1877 Construct and send a netbios DGRAM.
1878 **************************************************************************/
1879 BOOL send_mailslot(BOOL unique, char *mailslot,char *buf,int len,
1880 char *srcname, int src_type,
1881 char *dstname, int dest_type,
1882 struct in_addr dest_ip,struct in_addr src_ip,
1885 BOOL loopback_this_packet = False;
1886 struct packet_struct p;
1887 struct dgram_packet *dgram = &p.packet.dgram;
1891 bzero((char *)&p,sizeof(p));
1894 loopback_this_packet = True;
1896 generate_name_trn_id();
1898 /* DIRECT GROUP or UNIQUE datagram. */
1899 dgram->header.msg_type = unique ? 0x10 : 0x11;
1900 dgram->header.flags.node_type = M_NODE;
1901 dgram->header.flags.first = True;
1902 dgram->header.flags.more = False;
1903 dgram->header.dgm_id = name_trn_id;
1904 dgram->header.source_ip = src_ip;
1905 dgram->header.source_port = DGRAM_PORT;
1906 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
1907 dgram->header.packet_offset = 0;
1909 make_nmb_name(&dgram->source_name,srcname,src_type,scope);
1910 make_nmb_name(&dgram->dest_name,dstname,dest_type,scope);
1912 ptr = &dgram->data[0];
1914 /* Setup the smb part. */
1915 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
1917 set_message(ptr,17,17 + len,True);
1920 CVAL(ptr,smb_com) = SMBtrans;
1921 SSVAL(ptr,smb_vwv1,len);
1922 SSVAL(ptr,smb_vwv11,len);
1923 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
1924 SSVAL(ptr,smb_vwv13,3);
1925 SSVAL(ptr,smb_vwv14,1);
1926 SSVAL(ptr,smb_vwv15,1);
1927 SSVAL(ptr,smb_vwv16,2);
1929 pstrcpy(p2,mailslot);
1930 p2 = skip_string(p2,1);
1935 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
1939 p.fd = find_subnet_mailslot_fd_for_address( src_ip );
1940 p.timestamp = time(NULL);
1941 p.packet_type = DGRAM_PACKET;
1943 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
1944 namestr(&dgram->source_name), inet_ntoa(src_ip)));
1945 DEBUG(4,("to %s IP %s\n", namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
1947 debug_browse_data(buf, len);
1949 if(loopback_this_packet)
1951 struct packet_struct *lo_packet = NULL;
1952 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
1953 if((lo_packet = copy_packet(&p)) == NULL)
1955 queue_packet(lo_packet);
1959 return(send_packet(&p));