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 static void queue_packet(struct packet_struct *packet);
40 /*******************************************************************
41 The global packet linked-list. Incoming entries are
42 added to the end of this list. It is supposed to remain fairly
43 short so we won't bother with an end pointer.
44 ******************************************************************/
46 static struct packet_struct *packet_queue = NULL;
48 /***************************************************************************
49 Utility function to find the specific fd to send a packet out on.
50 **************************************************************************/
52 static int find_subnet_fd_for_address( struct in_addr local_ip )
54 struct subnet_record *subrec;
56 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
57 if(ip_equal(local_ip, subrec->myip))
58 return subrec->nmb_sock;
63 /***************************************************************************
64 Utility function to find the specific fd to send a mailslot packet out on.
65 **************************************************************************/
67 static int find_subnet_mailslot_fd_for_address( struct in_addr local_ip )
69 struct subnet_record *subrec;
71 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
72 if(ip_equal(local_ip, subrec->myip))
73 return subrec->dgram_sock;
78 /***************************************************************************
79 Get/Set problematic nb_flags as network byte order 16 bit int.
80 **************************************************************************/
82 uint16 get_nb_flags(char *buf)
84 return ((((uint16)*buf)&0xFFFF) & NB_FLGMSK);
87 void set_nb_flags(char *buf, uint16 nb_flags)
89 *buf++ = ((nb_flags & NB_FLGMSK) & 0xFF);
93 /***************************************************************************
94 Generates the unique transaction identifier
95 **************************************************************************/
97 static uint16 name_trn_id=0;
99 static uint16 generate_name_trn_id(void)
104 name_trn_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)getpid()%(unsigned)100);
106 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
110 /***************************************************************************
111 Either loops back or sends out a completed NetBIOS packet.
112 **************************************************************************/
114 static BOOL send_netbios_packet(struct packet_struct *p)
116 BOOL loopback_this_packet = False;
118 /* Check if we are sending to or from ourselves as a WINS server. */
119 if(ismyip(p->ip) && (p->port == global_nmb_port))
120 loopback_this_packet = True;
122 if(loopback_this_packet)
124 struct packet_struct *lo_packet = NULL;
125 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
126 if((lo_packet = copy_packet(p)) == NULL)
128 queue_packet(lo_packet);
130 else if (!send_packet(p))
132 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
133 inet_ntoa(p->ip),p->port));
140 /***************************************************************************
141 Sets up the common elements of an outgoing NetBIOS packet.
142 **************************************************************************/
144 static struct packet_struct *create_and_init_netbios_packet(struct nmb_name *nmbname,
146 struct in_addr to_ip)
148 struct packet_struct *packet = NULL;
149 struct nmb_packet *nmb = NULL;
151 /* Allocate the packet_struct we will return. */
152 if((packet = (struct packet_struct *)malloc(sizeof(*packet))) == NULL)
154 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
158 bzero((char *)packet,sizeof(*packet));
160 nmb = &packet->packet.nmb;
162 nmb->header.name_trn_id = generate_name_trn_id();
163 nmb->header.response = False;
164 nmb->header.nm_flags.recursion_desired = False;
165 nmb->header.nm_flags.recursion_available = False;
166 nmb->header.nm_flags.trunc = False;
167 nmb->header.nm_flags.authoritative = False;
168 nmb->header.nm_flags.bcast = bcast;
170 nmb->header.rcode = 0;
171 nmb->header.qdcount = 1;
172 nmb->header.ancount = 0;
173 nmb->header.nscount = 0;
175 nmb->question.question_name = *nmbname;
176 nmb->question.question_type = QUESTION_TYPE_NB_QUERY;
177 nmb->question.question_class = QUESTION_CLASS_IN;
180 packet->port = NMB_PORT;
181 packet->fd = ClientNMB;
182 packet->timestamp = time(NULL);
183 packet->packet_type = NMB_PACKET;
184 packet->locked = False;
186 return packet; /* Caller must free. */
189 /***************************************************************************
190 Sets up the common elements of register, refresh or release packet.
191 **************************************************************************/
193 static BOOL create_and_init_additional_record(struct packet_struct *packet,
195 struct in_addr *register_ip)
197 struct nmb_packet *nmb = &packet->packet.nmb;
199 if((nmb->additional = (struct res_rec *)malloc(sizeof(struct res_rec))) == NULL)
201 DEBUG(0,("initiate_name_register_packet: malloc fail for additional record.\n"));
205 bzero((char *)nmb->additional,sizeof(struct res_rec));
207 nmb->additional->rr_name = nmb->question.question_name;
208 nmb->additional->rr_type = RR_TYPE_NB;
209 nmb->additional->rr_class = RR_CLASS_IN;
211 nmb->additional->ttl = lp_max_ttl();
213 nmb->additional->rdlength = 6;
215 set_nb_flags(nmb->additional->rdata,nb_flags);
217 /* Set the address for the name we are registering. */
218 putip(&nmb->additional->rdata[2], register_ip);
220 /* Ensure that we send out the file descriptor to give us the
221 the specific source address we are registering as our
222 IP source address. */
224 packet->fd = find_subnet_fd_for_address( *register_ip );
229 /***************************************************************************
230 Sends out a name query.
231 **************************************************************************/
233 static BOOL initiate_name_query_packet( struct packet_struct *packet)
235 struct nmb_packet *nmb = NULL;
237 nmb = &packet->packet.nmb;
239 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
240 nmb->header.arcount = 0;
242 nmb->header.nm_flags.recursion_desired = True;
244 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
245 nmb_namestr(&nmb->question.question_name),
246 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
248 return send_netbios_packet( packet );
251 /***************************************************************************
252 Sends out a name query - from a WINS server.
253 **************************************************************************/
255 static BOOL initiate_name_query_packet_from_wins_server( struct packet_struct *packet)
257 struct nmb_packet *nmb = NULL;
259 nmb = &packet->packet.nmb;
261 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
262 nmb->header.arcount = 0;
264 nmb->header.nm_flags.recursion_desired = False;
266 DEBUG(4,("initiate_name_query_packet_from_wins_server: sending query for name %s (bcast=%s) to IP %s\n",
267 nmb_namestr(&nmb->question.question_name),
268 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
270 return send_netbios_packet( packet );
273 /***************************************************************************
274 Sends out a name register.
275 **************************************************************************/
277 static BOOL initiate_name_register_packet( struct packet_struct *packet,
278 uint16 nb_flags, struct in_addr *register_ip)
280 struct nmb_packet *nmb = &packet->packet.nmb;
282 nmb->header.opcode = NMB_NAME_REG_OPCODE;
283 nmb->header.arcount = 1;
285 nmb->header.nm_flags.recursion_desired = True;
287 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
290 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
291 nmb_namestr(&nmb->additional->rr_name),
292 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
294 return send_netbios_packet( packet );
297 /***************************************************************************
298 Sends out a multihomed name register.
299 **************************************************************************/
301 static BOOL initiate_multihomed_name_register_packet( struct packet_struct *packet,
302 uint16 nb_flags, struct in_addr *register_ip)
304 struct nmb_packet *nmb = &packet->packet.nmb;
305 fstring second_ip_buf;
307 fstrcpy(second_ip_buf, inet_ntoa(packet->ip));
309 nmb->header.opcode = NMB_NAME_MULTIHOMED_REG_OPCODE;
310 nmb->header.arcount = 1;
312 nmb->header.nm_flags.recursion_desired = True;
314 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
317 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
318 for name %s IP %s (bcast=%s) to IP %s\n",
319 nmb_namestr(&nmb->additional->rr_name), inet_ntoa(*register_ip),
320 BOOLSTR(nmb->header.nm_flags.bcast), second_ip_buf ));
322 return send_netbios_packet( packet );
325 /***************************************************************************
326 Sends out a name refresh.
327 **************************************************************************/
329 static BOOL initiate_name_refresh_packet( struct packet_struct *packet,
330 uint16 nb_flags, struct in_addr *refresh_ip)
332 struct nmb_packet *nmb = &packet->packet.nmb;
334 nmb->header.opcode = NMB_NAME_REFRESH_OPCODE_8;
335 nmb->header.arcount = 1;
337 nmb->header.nm_flags.recursion_desired = False;
339 if(create_and_init_additional_record(packet, nb_flags, refresh_ip) == False)
342 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
343 nmb_namestr(&nmb->additional->rr_name),
344 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
346 return send_netbios_packet( packet );
349 /***************************************************************************
350 Sends out a name release.
351 **************************************************************************/
353 static BOOL initiate_name_release_packet( struct packet_struct *packet,
354 uint16 nb_flags, struct in_addr *release_ip)
356 struct nmb_packet *nmb = &packet->packet.nmb;
358 nmb->header.opcode = NMB_NAME_RELEASE_OPCODE;
359 nmb->header.arcount = 1;
361 nmb->header.nm_flags.recursion_desired = False;
363 if(create_and_init_additional_record(packet, nb_flags, release_ip) == False)
366 DEBUG(4,("initiate_name_release_packet: sending release for name %s (bcast=%s) to IP %s\n",
367 nmb_namestr(&nmb->additional->rr_name),
368 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
370 return send_netbios_packet( packet );
373 /***************************************************************************
374 Sends out a node status.
375 **************************************************************************/
377 static BOOL initiate_node_status_packet( struct packet_struct *packet )
379 struct nmb_packet *nmb = &packet->packet.nmb;
381 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
382 nmb->header.arcount = 0;
384 nmb->header.nm_flags.recursion_desired = False;
386 nmb->question.question_type = QUESTION_TYPE_NB_STATUS;
388 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
389 nmb_namestr(&nmb->question.question_name),
390 inet_ntoa(packet->ip)));
392 return send_netbios_packet( packet );
395 /****************************************************************************
396 Simplification functions for queuing standard packets.
397 These should be the only publicly callable functions for sending
399 ****************************************************************************/
401 /****************************************************************************
402 Assertion - we should never be sending nmbd packets on the remote
404 ****************************************************************************/
406 static BOOL assert_check_subnet(struct subnet_record *subrec)
408 if( subrec == remote_broadcast_subnet)
410 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
417 /****************************************************************************
418 Queue a register name packet to the broadcast address of a subnet.
419 ****************************************************************************/
421 struct response_record *queue_register_name( struct subnet_record *subrec,
422 response_function resp_fn,
423 timeout_response_function timeout_fn,
424 register_name_success_function success_fn,
425 register_name_fail_function fail_fn,
426 struct userdata_struct *userdata,
427 struct nmb_name *nmbname,
430 struct packet_struct *p;
431 struct response_record *rrec;
432 BOOL bcast = (subrec == unicast_subnet) ? False : True;
434 if(assert_check_subnet(subrec))
437 if(( p = create_and_init_netbios_packet(nmbname, bcast,
438 subrec->bcast_ip)) == NULL)
441 if(initiate_name_register_packet( p, nb_flags,
442 iface_ip(subrec->bcast_ip)) == False)
449 if((rrec = make_response_record(subrec, /* subnet record. */
450 p, /* packet we sent. */
451 resp_fn, /* function to call on response. */
452 timeout_fn, /* function to call on timeout. */
453 (success_function)success_fn, /* function to call on operation success. */
454 (fail_function)fail_fn, /* function to call on operation fail. */
465 /****************************************************************************
466 Queue a multihomed register name packet to the broadcast address of a subnet.
467 ****************************************************************************/
469 struct response_record *queue_register_multihomed_name( struct subnet_record *subrec,
470 response_function resp_fn,
471 timeout_response_function timeout_fn,
472 register_name_success_function success_fn,
473 register_name_fail_function fail_fn,
474 struct userdata_struct *userdata,
475 struct nmb_name *nmbname,
477 struct in_addr register_ip)
479 struct packet_struct *p;
480 struct response_record *rrec;
485 if(subrec != unicast_subnet)
487 DEBUG(0,("queue_register_multihomed_name: should only be done on \
488 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
492 if(assert_check_subnet(subrec))
495 if(( p = create_and_init_netbios_packet(nmbname, bcast,
496 subrec->bcast_ip)) == NULL)
499 if (nb_flags & NB_GROUP)
500 ret = initiate_name_register_packet( p, nb_flags, ®ister_ip);
502 ret = initiate_multihomed_name_register_packet( p, nb_flags, ®ister_ip);
511 if((rrec = make_response_record(subrec, /* subnet record. */
512 p, /* packet we sent. */
513 resp_fn, /* function to call on response. */
514 timeout_fn, /* function to call on timeout. */
515 (success_function)success_fn, /* function to call on operation success. */
516 (fail_function)fail_fn, /* function to call on operation fail. */
527 /****************************************************************************
528 Queue a release name packet to the broadcast address of a subnet.
529 ****************************************************************************/
531 struct response_record *queue_release_name( struct subnet_record *subrec,
532 response_function resp_fn,
533 timeout_response_function timeout_fn,
534 release_name_success_function success_fn,
535 release_name_fail_function fail_fn,
536 struct userdata_struct *userdata,
537 struct nmb_name *nmbname,
539 struct in_addr release_ip)
541 BOOL bcast = (subrec == unicast_subnet) ? False : True;
542 struct packet_struct *p;
543 struct response_record *rrec;
545 if(assert_check_subnet(subrec))
548 if(( p = create_and_init_netbios_packet(nmbname, bcast,
549 subrec->bcast_ip)) == NULL)
552 if(initiate_name_release_packet( p, nb_flags, &release_ip) == False)
559 if((rrec = make_response_record(subrec, /* subnet record. */
560 p, /* packet we sent. */
561 resp_fn, /* function to call on response. */
562 timeout_fn, /* function to call on timeout. */
563 (success_function)success_fn, /* function to call on operation success. */
564 (fail_function)fail_fn, /* function to call on operation fail. */
573 * For a broadcast release packet, only send once.
574 * This will cause us to remove the name asap. JRA.
579 rrec->repeat_count = 0;
580 rrec->repeat_time = 0;
586 /****************************************************************************
587 Queue a refresh name packet to the broadcast address of a subnet.
588 ****************************************************************************/
590 struct response_record *queue_refresh_name( struct subnet_record *subrec,
591 response_function resp_fn,
592 timeout_response_function timeout_fn,
593 refresh_name_success_function success_fn,
594 refresh_name_fail_function fail_fn,
595 struct userdata_struct *userdata,
596 struct name_record *namerec,
597 struct in_addr refresh_ip)
599 BOOL bcast = (subrec == unicast_subnet) ? False : True;
600 struct packet_struct *p;
601 struct response_record *rrec;
603 if(assert_check_subnet(subrec))
606 if(( p = create_and_init_netbios_packet(&namerec->name, bcast,
607 subrec->bcast_ip)) == NULL)
610 if( !initiate_name_refresh_packet( p, namerec->data.nb_flags, &refresh_ip ) )
617 if((rrec = make_response_record(subrec, /* subnet record. */
618 p, /* packet we sent. */
619 resp_fn, /* function to call on response. */
620 timeout_fn, /* function to call on timeout. */
621 (success_function)success_fn, /* function to call on operation success. */
622 (fail_function)fail_fn, /* function to call on operation fail. */
633 /****************************************************************************
634 Queue a query name packet to the broadcast address of a subnet.
635 ****************************************************************************/
637 struct response_record *queue_query_name( struct subnet_record *subrec,
638 response_function resp_fn,
639 timeout_response_function timeout_fn,
640 query_name_success_function success_fn,
641 query_name_fail_function fail_fn,
642 struct userdata_struct *userdata,
643 struct nmb_name *nmbname)
645 struct packet_struct *p;
646 struct response_record *rrec;
649 if ((subrec == unicast_subnet) || (subrec == wins_server_subnet))
652 if(assert_check_subnet(subrec))
655 if(( p = create_and_init_netbios_packet(nmbname, bcast,
656 subrec->bcast_ip)) == NULL)
659 if(initiate_name_query_packet( p ) == False)
666 if((rrec = make_response_record(subrec, /* subnet record. */
667 p, /* packet we sent. */
668 resp_fn, /* function to call on response. */
669 timeout_fn, /* function to call on timeout. */
670 (success_function)success_fn, /* function to call on operation success. */
671 (fail_function)fail_fn, /* function to call on operation fail. */
682 /****************************************************************************
683 Queue a query name packet to a given address from the WINS subnet.
684 ****************************************************************************/
686 struct response_record *queue_query_name_from_wins_server( struct in_addr to_ip,
687 response_function resp_fn,
688 timeout_response_function timeout_fn,
689 query_name_success_function success_fn,
690 query_name_fail_function fail_fn,
691 struct userdata_struct *userdata,
692 struct nmb_name *nmbname)
694 struct packet_struct *p;
695 struct response_record *rrec;
698 if(( p = create_and_init_netbios_packet(nmbname, bcast, to_ip)) == NULL)
701 if(initiate_name_query_packet_from_wins_server( p ) == False)
708 if((rrec = make_response_record(wins_server_subnet, /* subnet record. */
709 p, /* packet we sent. */
710 resp_fn, /* function to call on response. */
711 timeout_fn, /* function to call on timeout. */
712 (success_function)success_fn, /* function to call on operation success. */
713 (fail_function)fail_fn, /* function to call on operation fail. */
724 /****************************************************************************
725 Queue a node status packet to a given name and address.
726 ****************************************************************************/
728 struct response_record *queue_node_status( struct subnet_record *subrec,
729 response_function resp_fn,
730 timeout_response_function timeout_fn,
731 node_status_success_function success_fn,
732 node_status_fail_function fail_fn,
733 struct userdata_struct *userdata,
734 struct nmb_name *nmbname,
735 struct in_addr send_ip)
737 struct packet_struct *p;
738 struct response_record *rrec;
742 if(subrec != unicast_subnet)
744 DEBUG(0,("queue_register_multihomed_name: should only be done on \
745 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
749 if(assert_check_subnet(subrec))
752 if(( p = create_and_init_netbios_packet(nmbname, bcast,
756 if(initiate_node_status_packet(p) == False)
763 if((rrec = make_response_record(subrec, /* subnet record. */
764 p, /* packet we sent. */
765 resp_fn, /* function to call on response. */
766 timeout_fn, /* function to call on timeout. */
767 (success_function)success_fn, /* function to call on operation success. */
768 (fail_function)fail_fn, /* function to call on operation fail. */
779 /****************************************************************************
780 Reply to a netbios name packet. see rfc1002.txt
781 ****************************************************************************/
783 void reply_netbios_packet(struct packet_struct *orig_packet,
784 int rcode, enum netbios_reply_type_code rcv_code, int opcode,
785 int ttl, char *data,int len)
787 struct packet_struct packet;
788 struct nmb_packet *nmb = NULL;
789 struct res_rec answers;
790 struct nmb_packet *orig_nmb = &orig_packet->packet.nmb;
791 BOOL loopback_this_packet = False;
792 char *packet_type = "unknown";
794 /* Check if we are sending to or from ourselves. */
795 if(ismyip(orig_packet->ip) && (orig_packet->port == global_nmb_port))
796 loopback_this_packet = True;
798 nmb = &packet.packet.nmb;
800 /* Do a partial copy of the packet. We clear the locked flag and
801 the resource record pointers. */
802 packet = *orig_packet; /* Full structure copy. */
803 packet.locked = False;
806 nmb->additional = NULL;
812 packet_type = "nmb_status";
813 nmb->header.nm_flags.recursion_desired = False;
814 nmb->header.nm_flags.recursion_available = False;
819 packet_type = "nmb_query";
820 nmb->header.nm_flags.recursion_desired = True;
821 nmb->header.nm_flags.recursion_available = True;
825 case NMB_REG_REFRESH:
827 packet_type = "nmb_reg";
828 nmb->header.nm_flags.recursion_desired = True;
829 nmb->header.nm_flags.recursion_available = True;
834 packet_type = "nmb_rel";
835 nmb->header.nm_flags.recursion_desired = False;
836 nmb->header.nm_flags.recursion_available = False;
841 packet_type = "nmb_wack";
842 nmb->header.nm_flags.recursion_desired = False;
843 nmb->header.nm_flags.recursion_available = False;
848 packet_type = "wins_reg";
849 nmb->header.nm_flags.recursion_desired = True;
850 nmb->header.nm_flags.recursion_available = True;
855 packet_type = "wins_query";
856 nmb->header.nm_flags.recursion_desired = True;
857 nmb->header.nm_flags.recursion_available = True;
863 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
864 packet_type, nmb_namestr(&orig_nmb->question.question_name),
865 inet_ntoa(packet.ip)));
871 DEBUG(4,("reply_netbios_packet: sending a reply of packet type: %s %s to ip %s \
873 packet_type, nmb_namestr(&orig_nmb->question.question_name),
874 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
876 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
877 nmb->header.opcode = opcode;
878 nmb->header.response = True;
879 nmb->header.nm_flags.bcast = False;
880 nmb->header.nm_flags.trunc = False;
881 nmb->header.nm_flags.authoritative = True;
883 nmb->header.rcode = rcode;
884 nmb->header.qdcount = 0;
885 nmb->header.ancount = 1;
886 nmb->header.nscount = 0;
887 nmb->header.arcount = 0;
889 bzero((char*)&nmb->question,sizeof(nmb->question));
891 nmb->answers = &answers;
892 bzero((char*)nmb->answers,sizeof(*nmb->answers));
894 nmb->answers->rr_name = orig_nmb->question.question_name;
895 nmb->answers->rr_type = orig_nmb->question.question_type;
896 nmb->answers->rr_class = orig_nmb->question.question_class;
897 nmb->answers->ttl = ttl;
901 nmb->answers->rdlength = len;
902 memcpy(nmb->answers->rdata, data, len);
905 packet.packet_type = NMB_PACKET;
906 /* Ensure we send out on the same fd that the original
907 packet came in on to give the correct source IP address. */
908 packet.fd = orig_packet->fd;
909 packet.timestamp = time(NULL);
911 debug_nmb_packet(&packet);
913 if(loopback_this_packet)
915 struct packet_struct *lo_packet;
916 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
917 if((lo_packet = copy_packet(&packet)) == NULL)
919 queue_packet(lo_packet);
921 else if (!send_packet(&packet))
923 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
924 inet_ntoa(packet.ip),packet.port));
928 /*******************************************************************
929 Queue a packet into a packet queue
930 ******************************************************************/
931 static void queue_packet(struct packet_struct *packet)
933 struct packet_struct *p;
939 packet_queue = packet;
943 /* find the bottom */
944 for (p=packet_queue;p->next;p=p->next)
952 /****************************************************************************
953 Try and find a matching subnet record for a datagram port 138 packet.
954 ****************************************************************************/
956 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
958 struct subnet_record *subrec;
960 /* Go through all the broadcast subnets and see if the mask matches. */
961 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
963 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
967 /* If the subnet record is the remote announce broadcast subnet,
968 hack it here to be the first subnet. This is really gross and
969 is needed due to people turning on port 137/138 broadcast
970 forwarding on their routers. May fire and brimstone rain
977 /****************************************************************************
978 Dispatch a browse frame from port 138 to the correct processing function.
979 ****************************************************************************/
980 static void process_browse_packet(struct packet_struct *p, char *buf,int len)
982 struct dgram_packet *dgram = &p->packet.dgram;
983 int command = CVAL(buf,0);
984 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
986 /* Drop the packet if it's a different NetBIOS scope, or
987 the source is from one of our names. */
989 if (!strequal(dgram->dest_name.scope,scope ))
991 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
992 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
996 if (is_myname(dgram->source_name.name))
998 DEBUG(0,("process_browse_packet: Discarding datagram from IP %s. Source name \
999 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1005 case ANN_HostAnnouncement:
1007 process_host_announce(subrec, p, buf+1);
1010 case ANN_DomainAnnouncement:
1012 process_workgroup_announce(subrec, p, buf+1);
1015 case ANN_LocalMasterAnnouncement:
1017 process_local_master_announce(subrec, p, buf+1);
1020 case ANN_AnnouncementRequest:
1022 process_announce_request(subrec, p, buf+1);
1027 process_election(subrec, p, buf+1);
1030 case ANN_GetBackupListReq:
1033 /* This is one occasion where we change a subnet that is
1034 given to us. If the packet was sent to WORKGROUP<1b> instead
1035 of WORKGROUP<1d> then it was unicast to us a domain master
1036 browser. Change subrec to unicast.
1038 if(dgram->dest_name.name_type == 0x1b)
1039 subrec = unicast_subnet;
1041 process_get_backup_list_request(subrec, p, buf+1);
1044 case ANN_GetBackupListResp:
1046 /* We never send ANN_GetBackupListReq so we
1047 should never get these. */
1048 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1049 packet from %s IP %s\n", nmb_namestr(&dgram->source_name), inet_ntoa(p->ip)));
1052 case ANN_ResetBrowserState:
1054 process_reset_browser(subrec, p, buf+1);
1057 case ANN_MasterAnnouncement:
1059 /* Master browser datagrams must be processed
1060 on the unicast subnet. */
1061 subrec = unicast_subnet;
1063 process_master_browser_announce(subrec, p, buf+1);
1066 case ANN_BecomeBackup:
1069 * We don't currently implement this. Log it just in case.
1071 DEBUG(10,("process_browse_packet: On subnet %s ignoring browse packet \
1072 command ANN_BecomeBackup from %s IP %s to %s\n",
1073 subrec->subnet_name, nmb_namestr(&dgram->source_name),
1074 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1079 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1080 command code %d from %s IP %s to %s\n",
1081 subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1082 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1087 /****************************************************************************
1088 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1089 ****************************************************************************/
1090 static void process_lanman_packet(struct packet_struct *p, char *buf,int len)
1092 struct dgram_packet *dgram = &p->packet.dgram;
1093 int command = SVAL(buf,0);
1094 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1096 /* Drop the packet if it's a different NetBIOS scope, or
1097 the source is from one of our names. */
1099 if (!strequal(dgram->dest_name.scope,scope ))
1101 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1102 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
1106 if (is_myname(dgram->source_name.name))
1108 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1109 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1115 case ANN_HostAnnouncement:
1117 dump_data(4, buf, len);
1118 process_lm_host_announce(subrec, p, buf+1);
1121 case ANN_AnnouncementRequest:
1123 process_lm_announce_request(subrec, p, buf+1);
1128 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1129 command code %d from %s IP %s to %s\n",
1130 subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1131 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1136 /****************************************************************************
1137 Determine if a packet is for us on port 138. Note that to have any chance of
1138 being efficient we need to drop as many packets as possible at this
1139 stage as subsequent processing is expensive.
1140 ****************************************************************************/
1142 static BOOL listening(struct packet_struct *p,struct nmb_name *nbname)
1144 struct subnet_record *subrec = NULL;
1146 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1148 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1153 subrec = unicast_subnet;
1155 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1158 /****************************************************************************
1159 Process udp 138 datagrams
1160 ****************************************************************************/
1162 static void process_dgram(struct packet_struct *p)
1167 struct dgram_packet *dgram = &p->packet.dgram;
1169 /* If we aren't listening to the destination name then ignore the packet */
1170 if (!listening(p,&dgram->dest_name))
1172 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1173 nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1177 if (dgram->header.msg_type != 0x10 &&
1178 dgram->header.msg_type != 0x11 &&
1179 dgram->header.msg_type != 0x12)
1181 /* Don't process error packets etc yet */
1182 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1183 an error packet of type %x\n",
1184 nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1188 buf = &dgram->data[0];
1189 buf -= 4; /* XXXX for the pseudo tcp length -
1190 someday I need to get rid of this */
1192 if (CVAL(buf,smb_com) != SMBtrans)
1195 len = SVAL(buf,smb_vwv11);
1196 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1198 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1199 nmb_namestr(&dgram->source_name),nmb_namestr(&dgram->dest_name),
1200 inet_ntoa(p->ip), smb_buf(buf),CVAL(buf2,0),len));
1205 dump_data(100, buf2, len);
1207 /* Datagram packet received for the browser mailslot */
1208 if (strequal(smb_buf(buf),BROWSE_MAILSLOT))
1210 process_browse_packet(p,buf2,len);
1214 /* Datagram packet received for the LAN Manager mailslot */
1215 if (strequal(smb_buf(buf),LANMAN_MAILSLOT)) {
1216 process_lanman_packet(p,buf2,len);
1220 /* Datagram packet received for the domain logon mailslot */
1221 if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT))
1223 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1227 /* Datagram packet received for the NT domain logon mailslot */
1228 if (strequal(smb_buf(buf),NT_LOGON_MAILSLOT))
1230 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1235 /****************************************************************************
1236 Validate a response nmb packet.
1237 ****************************************************************************/
1239 static BOOL validate_nmb_response_packet( struct nmb_packet *nmb )
1241 BOOL ignore = False;
1243 switch (nmb->header.opcode)
1245 case NMB_NAME_REG_OPCODE:
1246 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1247 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1248 if (nmb->header.ancount == 0)
1250 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1255 case NMB_NAME_QUERY_OPCODE:
1256 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1))
1258 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1262 case NMB_NAME_RELEASE_OPCODE:
1263 if (nmb->header.ancount == 0)
1265 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1269 case NMB_WACK_OPCODE:
1270 /* Check WACK response here. */
1271 if (nmb->header.ancount != 1)
1273 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1278 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1279 nmb->header.opcode));
1284 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1289 /****************************************************************************
1290 Validate a request nmb packet.
1291 ****************************************************************************/
1293 static BOOL validate_nmb_packet( struct nmb_packet *nmb )
1295 BOOL ignore = False;
1297 switch (nmb->header.opcode)
1299 case NMB_NAME_REG_OPCODE:
1300 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1301 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1302 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1303 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1305 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1310 case NMB_NAME_QUERY_OPCODE:
1311 if ((nmb->header.qdcount == 0) ||
1312 ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1313 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS)))
1315 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1320 case NMB_NAME_RELEASE_OPCODE:
1321 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1323 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1328 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1329 nmb->header.opcode));
1334 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1339 /****************************************************************************
1340 Find a subnet (and potentially a response record) for a packet.
1341 ****************************************************************************/
1343 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1344 struct response_record **pprrec)
1346 struct nmb_packet *nmb = &p->packet.nmb;
1347 struct response_record *rrec = NULL;
1348 struct subnet_record *subrec = NULL;
1353 if(nmb->header.response)
1355 /* It's a response packet. Find a record for it or it's an error. */
1357 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1360 DEBUG(0,("find_subnet_for_nmb_packet: response record not found for response id %hu\n",
1361 nmb->header.name_trn_id));
1367 DEBUG(0,("find_subnet_for_nmb_packet: subnet record not found for response id %hu\n",
1368 nmb->header.name_trn_id));
1377 /* Try and see what subnet this packet belongs to. */
1380 if(packet_is_for_wins_server(p))
1381 return wins_server_subnet;
1383 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1384 if(nmb->header.nm_flags.bcast == False)
1385 return unicast_subnet;
1387 /* Go through all the broadcast subnets and see if the mask matches. */
1388 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1390 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1394 /* If none match it must have been a directed broadcast - assign
1395 the remote_broadcast_subnet. */
1396 return remote_broadcast_subnet;
1399 /****************************************************************************
1400 Process a nmb request packet - validate the packet and route it.
1401 ****************************************************************************/
1403 static void process_nmb_request(struct packet_struct *p)
1405 struct nmb_packet *nmb = &p->packet.nmb;
1406 struct subnet_record *subrec = NULL;
1408 debug_nmb_packet(p);
1410 /* Ensure we have a good packet. */
1411 if(validate_nmb_packet(nmb))
1414 /* Allocate a subnet to this packet - if we cannot - fail. */
1415 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1418 switch (nmb->header.opcode)
1420 case NMB_NAME_REG_OPCODE:
1421 if(subrec == wins_server_subnet)
1422 wins_process_name_registration_request(subrec, p);
1424 process_name_registration_request(subrec, p);
1427 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1428 case NMB_NAME_REFRESH_OPCODE_9:
1429 if(subrec == wins_server_subnet)
1430 wins_process_name_refresh_request(subrec, p);
1432 process_name_refresh_request(subrec, p);
1435 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1436 if(subrec == wins_server_subnet)
1437 wins_process_multihomed_name_registration_request(subrec, p);
1440 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1441 directed at a WINS server.\n"));
1445 case NMB_NAME_QUERY_OPCODE:
1446 switch (nmb->question.question_type)
1448 case QUESTION_TYPE_NB_QUERY:
1450 if(subrec == wins_server_subnet)
1451 wins_process_name_query_request(subrec, p);
1453 process_name_query_request(subrec, p);
1456 case QUESTION_TYPE_NB_STATUS:
1458 if(subrec == wins_server_subnet)
1460 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1465 process_node_status_request(subrec, p);
1471 case NMB_NAME_RELEASE_OPCODE:
1472 if(subrec == wins_server_subnet)
1473 wins_process_name_release_request(subrec, p);
1475 process_name_release_request(subrec, p);
1480 /****************************************************************************
1481 Process a nmb response packet - validate the packet and route it.
1482 to either the WINS server or a normal response.
1483 ****************************************************************************/
1485 static void process_nmb_response(struct packet_struct *p)
1487 struct nmb_packet *nmb = &p->packet.nmb;
1488 struct subnet_record *subrec = NULL;
1489 struct response_record *rrec = NULL;
1491 debug_nmb_packet(p);
1493 if(validate_nmb_response_packet(nmb))
1496 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1501 DEBUG(0,("process_nmb_response: response packet received but no response record \
1502 found for id = %hu. Ignoring packet.\n", nmb->header.name_trn_id));
1506 /* Increment the number of responses received for this record. */
1508 /* Ensure we don't re-send the request. */
1509 rrec->repeat_count = 0;
1511 /* Call the response received function for this packet. */
1512 (*rrec->resp_fn)(subrec, rrec, p);
1516 /*******************************************************************
1517 Run elements off the packet queue till its empty
1518 ******************************************************************/
1520 void run_packet_queue(void)
1522 struct packet_struct *p;
1524 while ((p = packet_queue))
1526 packet_queue = p->next;
1528 packet_queue->prev = NULL;
1529 p->next = p->prev = NULL;
1531 switch (p->packet_type)
1534 if(p->packet.nmb.header.response)
1535 process_nmb_response(p);
1537 process_nmb_request(p);
1548 /*******************************************************************
1549 Retransmit or timeout elements from all the outgoing subnet response
1550 record queues. NOTE that this code must also check the WINS server
1551 subnet for response records to timeout as the WINS server code
1552 can send requests to check if a client still owns a name.
1553 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1554 ******************************************************************/
1556 void retransmit_or_expire_response_records(time_t t)
1558 struct subnet_record *subrec;
1560 for (subrec = FIRST_SUBNET; subrec;
1561 subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec))
1563 struct response_record *rrec, *nextrrec;
1565 for (rrec = subrec->responselist; rrec; rrec = nextrrec)
1567 nextrrec = rrec->next;
1569 if (rrec->repeat_time <= t)
1571 if (rrec->repeat_count > 0)
1573 /* Resend while we have a non-zero repeat_count. */
1574 if(!send_packet(rrec->packet))
1576 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1577 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1578 subrec->subnet_name));
1580 rrec->repeat_time += rrec->repeat_interval;
1581 rrec->repeat_count--;
1585 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1586 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1587 subrec->subnet_name));
1590 * Check the flag in this record to prevent recursion if we end
1591 * up in this function again via the timeout function call.
1594 if(!rrec->in_expiration_processing)
1598 * Set the recursion protection flag in this record.
1601 rrec->in_expiration_processing = True;
1603 /* Call the timeout function. This will deal with removing the
1604 timed out packet. */
1605 if(rrec->timeout_fn)
1606 (*rrec->timeout_fn)(subrec, rrec);
1609 /* We must remove the record ourself if there is
1610 no timeout function. */
1611 remove_response_record(subrec, rrec);
1613 } /* !rrec->in_expitation_processing */
1614 } /* rrec->repeat_count > 0 */
1615 } /* rrec->repeat_time <= t */
1616 } /* end for rrec */
1617 } /* end for subnet */
1620 /****************************************************************************
1621 Create an fd_set containing all the sockets in the subnet structures,
1622 plus the broadcast sockets.
1623 ***************************************************************************/
1625 static BOOL create_listen_fdset(fd_set **ppset, int **psock_array, int *listen_number)
1627 int *sock_array = NULL;
1628 struct subnet_record *subrec = NULL;
1631 fd_set *pset = (fd_set *)malloc(sizeof(fd_set));
1635 DEBUG(0,("create_listen_fdset: malloc fail !\n"));
1639 /* Check that we can add all the fd's we need. */
1640 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1643 if((count*2) + 2 > FD_SETSIZE)
1645 DEBUG(0,("create_listen_fdset: Too many file descriptors needed (%d). We can \
1646 only use %d.\n", (count*2) + 2, FD_SETSIZE));
1650 if((sock_array = (int *)malloc(((count*2) + 2)*sizeof(int))) == NULL)
1652 DEBUG(0,("create_listen_fdset: malloc fail for socket array.\n"));
1658 /* Add in the broadcast socket on 137. */
1659 FD_SET(ClientNMB,pset);
1660 sock_array[num++] = ClientNMB;
1662 /* Add in the 137 sockets on all the interfaces. */
1663 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1665 FD_SET(subrec->nmb_sock,pset);
1666 sock_array[num++] = subrec->nmb_sock;
1669 /* Add in the broadcast socket on 138. */
1670 FD_SET(ClientDGRAM,pset);
1671 sock_array[num++] = ClientDGRAM;
1673 /* Add in the 138 sockets on all the interfaces. */
1674 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1676 FD_SET(subrec->dgram_sock,pset);
1677 sock_array[num++] = subrec->dgram_sock;
1680 *listen_number = (count*2) + 2;
1682 *psock_array = sock_array;
1687 /****************************************************************************
1688 Listens for NMB or DGRAM packets, and queues them.
1689 ***************************************************************************/
1691 BOOL listen_for_packets(BOOL run_election)
1693 static fd_set *listen_set = NULL;
1694 static int listen_number = 0;
1695 static int *sock_array = NULL;
1699 struct timeval timeout;
1704 if(listen_set == NULL)
1706 if(create_listen_fdset(&listen_set, &sock_array, &listen_number))
1708 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1713 memcpy((char *)&fds, (char *)listen_set, sizeof(fd_set));
1716 dns_fd = asyncdns_fd();
1718 FD_SET(dns_fd, &fds);
1724 * During elections and when expecting a netbios response packet we
1725 * need to send election packets at tighter intervals.
1726 * Ideally it needs to be the interval (in ms) between time now and
1727 * the time we are expecting the next netbios packet.
1730 timeout.tv_sec = (run_election||num_response_packets) ? 1 : NMBD_SELECT_LOOP;
1731 timeout.tv_usec = 0;
1733 /* Prepare for the select - allow certain signals. */
1735 BlockSignals(False, SIGTERM);
1736 #if defined(SIGUSR1)
1737 BlockSignals(False, SIGUSR1);
1738 #endif /* SIGUSR1 */
1739 #if defined(SIGUSR2)
1740 BlockSignals(False, SIGUSR2);
1741 #endif /* SIGUSR2 */
1743 selrtn = sys_select(256,&fds,&timeout);
1745 /* We can only take signals when we are in the select - block them again here. */
1747 BlockSignals(True, SIGTERM);
1748 #if defined(SIGUSR1)
1749 BlockSignals(True, SIGUSR1);
1750 #endif /* SIGUSR1 */
1751 #if defined(SIGUSR2)
1752 BlockSignals(True, SIGUSR2);
1753 #endif /* SIGUSR2 */
1760 if (dns_fd != -1 && FD_ISSET(dns_fd,&fds)) {
1765 for(i = 0; i < listen_number; i++)
1767 if(i < (listen_number/2))
1769 /* Processing a 137 socket. */
1770 if (FD_ISSET(sock_array[i],&fds))
1772 struct packet_struct *packet = read_packet(sock_array[i], NMB_PACKET);
1776 * If we got a packet on the broadcast socket and interfaces
1777 * only is set then check it came from one of our local nets.
1779 if(lp_bind_interfaces_only() && (sock_array[i] == ClientNMB) &&
1780 (!is_local_net(packet->ip)))
1782 DEBUG(7,("discarding nmb packet sent to broadcast socket from %s:%d\n",
1783 inet_ntoa(packet->ip),packet->port));
1784 free_packet(packet);
1786 else if ((ip_equal(loopback_ip, packet->ip) ||
1787 ismyip(packet->ip)) && packet->port == global_nmb_port)
1789 DEBUG(7,("discarding own packet from %s:%d\n",
1790 inet_ntoa(packet->ip),packet->port));
1791 free_packet(packet);
1795 /* Save the file descriptor this packet came in on. */
1796 packet->fd = sock_array[i];
1797 queue_packet(packet);
1804 /* Processing a 138 socket. */
1806 if (FD_ISSET(sock_array[i],&fds))
1808 struct packet_struct *packet = read_packet(sock_array[i], DGRAM_PACKET);
1812 * If we got a packet on the broadcast socket and interfaces
1813 * only is set then check it came from one of our local nets.
1815 if(lp_bind_interfaces_only() && (sock_array[i] == ClientDGRAM) &&
1816 (!is_local_net(packet->ip)))
1818 DEBUG(7,("discarding dgram packet sent to broadcast socket from %s:%d\n",
1819 inet_ntoa(packet->ip),packet->port));
1820 free_packet(packet);
1822 else if ((ip_equal(loopback_ip, packet->ip) ||
1823 ismyip(packet->ip)) && packet->port == DGRAM_PORT)
1825 DEBUG(7,("discarding own packet from %s:%d\n",
1826 inet_ntoa(packet->ip),packet->port));
1827 free_packet(packet);
1831 /* Save the file descriptor this packet came in on. */
1832 packet->fd = sock_array[i];
1833 queue_packet(packet);
1837 } /* end processing 138 socket. */
1839 } /* end if selret > 0 */
1843 /****************************************************************************
1844 Construct and send a netbios DGRAM.
1845 **************************************************************************/
1846 BOOL send_mailslot(BOOL unique, char *mailslot,char *buf,int len,
1847 char *srcname, int src_type,
1848 char *dstname, int dest_type,
1849 struct in_addr dest_ip,struct in_addr src_ip,
1852 BOOL loopback_this_packet = False;
1853 struct packet_struct p;
1854 struct dgram_packet *dgram = &p.packet.dgram;
1858 bzero((char *)&p,sizeof(p));
1861 loopback_this_packet = True;
1863 generate_name_trn_id();
1865 /* DIRECT GROUP or UNIQUE datagram. */
1866 dgram->header.msg_type = unique ? 0x10 : 0x11;
1867 dgram->header.flags.node_type = M_NODE | 0x40;
1868 dgram->header.flags.first = True;
1869 dgram->header.flags.more = False;
1870 dgram->header.dgm_id = name_trn_id;
1871 dgram->header.source_ip = src_ip;
1872 dgram->header.source_port = DGRAM_PORT;
1873 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
1874 dgram->header.packet_offset = 0;
1876 make_nmb_name(&dgram->source_name,srcname,src_type,scope);
1877 make_nmb_name(&dgram->dest_name,dstname,dest_type,scope);
1879 ptr = &dgram->data[0];
1881 /* Setup the smb part. */
1882 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
1884 set_message(ptr,17,17 + len,True);
1887 CVAL(ptr,smb_com) = SMBtrans;
1888 SSVAL(ptr,smb_vwv1,len);
1889 SSVAL(ptr,smb_vwv11,len);
1890 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
1891 SSVAL(ptr,smb_vwv13,3);
1892 SSVAL(ptr,smb_vwv14,1);
1893 SSVAL(ptr,smb_vwv15,1);
1894 SSVAL(ptr,smb_vwv16,2);
1896 pstrcpy(p2,mailslot);
1897 p2 = skip_string(p2,1);
1902 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
1906 p.fd = find_subnet_mailslot_fd_for_address( src_ip );
1907 p.timestamp = time(NULL);
1908 p.packet_type = DGRAM_PACKET;
1910 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
1911 nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
1912 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
1914 dump_data(4, buf, len);
1916 if(loopback_this_packet)
1918 struct packet_struct *lo_packet = NULL;
1919 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
1920 if((lo_packet = copy_packet(&p)) == NULL)
1922 queue_packet(lo_packet);
1926 return(send_packet(&p));