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)
98 for (i = 0; i < len; i+= 16)
100 DEBUG(4, ("%3x char ", i));
102 for (j = 0; j < 16; j++)
104 unsigned char x = outbuf[i+j];
105 if (x < 32 || x > 127)
113 DEBUG(4, (" hex ", i));
115 for (j = 0; j < 16; j++)
119 DEBUG(4, (" %02x", (unsigned char)outbuf[i+j]));
126 /***************************************************************************
127 Generates the unique transaction identifier
128 **************************************************************************/
130 static uint16 name_trn_id=0;
132 static uint16 generate_name_trn_id(void)
137 name_trn_id = (time(NULL)%(unsigned)0x7FFF) + (getpid()%(unsigned)100);
139 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
143 /***************************************************************************
144 Either loops back or sends out a completed NetBIOS packet.
145 **************************************************************************/
147 static BOOL send_netbios_packet(struct packet_struct *p)
149 BOOL loopback_this_packet = False;
151 /* Check if we are sending to or from ourselves as a WINS server. */
152 if(ismyip(p->ip) && (p->port == global_nmb_port))
153 loopback_this_packet = True;
155 if(loopback_this_packet)
157 struct packet_struct *lo_packet = NULL;
158 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
159 if((lo_packet = copy_packet(p)) == NULL)
161 queue_packet(lo_packet);
163 else if (!send_packet(p))
165 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
166 inet_ntoa(p->ip),p->port));
173 /***************************************************************************
174 Sets up the common elements of an outgoing NetBIOS packet.
175 **************************************************************************/
177 static struct packet_struct *create_and_init_netbios_packet(struct nmb_name *nmbname,
179 struct in_addr to_ip)
181 struct packet_struct *packet = NULL;
182 struct nmb_packet *nmb = NULL;
184 /* Allocate the packet_struct we will return. */
185 if((packet = (struct packet_struct *)malloc(sizeof(*packet))) == NULL)
187 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
191 bzero((char *)packet,sizeof(*packet));
193 nmb = &packet->packet.nmb;
195 nmb->header.name_trn_id = generate_name_trn_id();
196 nmb->header.response = False;
197 nmb->header.nm_flags.recursion_desired = False;
198 nmb->header.nm_flags.recursion_available = False;
199 nmb->header.nm_flags.trunc = False;
200 nmb->header.nm_flags.authoritative = False;
201 nmb->header.nm_flags.bcast = bcast;
203 nmb->header.rcode = 0;
204 nmb->header.qdcount = 1;
205 nmb->header.ancount = 0;
206 nmb->header.nscount = 0;
208 nmb->question.question_name = *nmbname;
209 nmb->question.question_type = QUESTION_TYPE_NB_QUERY;
210 nmb->question.question_class = QUESTION_CLASS_IN;
213 packet->port = NMB_PORT;
214 packet->fd = ClientNMB;
215 packet->timestamp = time(NULL);
216 packet->packet_type = NMB_PACKET;
217 packet->locked = False;
219 return packet; /* Caller must free. */
222 /***************************************************************************
223 Sets up the common elements of register, refresh or release packet.
224 **************************************************************************/
226 static BOOL create_and_init_additional_record(struct packet_struct *packet,
228 struct in_addr *register_ip)
230 struct nmb_packet *nmb = &packet->packet.nmb;
232 if((nmb->additional = (struct res_rec *)malloc(sizeof(struct res_rec))) == NULL)
234 DEBUG(0,("initiate_name_register_packet: malloc fail for additional record.\n"));
238 bzero((char *)nmb->additional,sizeof(struct res_rec));
240 nmb->additional->rr_name = nmb->question.question_name;
241 nmb->additional->rr_type = RR_TYPE_NB;
242 nmb->additional->rr_class = RR_CLASS_IN;
244 nmb->additional->ttl = lp_max_ttl();
246 nmb->additional->rdlength = 6;
248 set_nb_flags(nmb->additional->rdata,nb_flags);
250 /* Set the address for the name we are registering. */
251 putip(&nmb->additional->rdata[2], register_ip);
253 /* Ensure that we send out the file descriptor to give us the
254 the specific source address we are registering as our
255 IP source address. */
257 packet->fd = find_subnet_fd_for_address( *register_ip );
262 /***************************************************************************
263 Sends out a name query.
264 **************************************************************************/
266 static BOOL initiate_name_query_packet( struct packet_struct *packet)
268 struct nmb_packet *nmb = NULL;
270 nmb = &packet->packet.nmb;
272 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
273 nmb->header.arcount = 0;
275 nmb->header.nm_flags.recursion_desired = True;
277 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
278 namestr(&nmb->question.question_name),
279 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
281 return send_netbios_packet( packet );
284 /***************************************************************************
285 Sends out a name register.
286 **************************************************************************/
288 static BOOL initiate_name_register_packet( struct packet_struct *packet,
289 uint16 nb_flags, struct in_addr *register_ip)
291 struct nmb_packet *nmb = &packet->packet.nmb;
293 nmb->header.opcode = NMB_NAME_REG_OPCODE;
294 nmb->header.arcount = 1;
296 nmb->header.nm_flags.recursion_desired = True;
298 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
301 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
302 namestr(&nmb->additional->rr_name),
303 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
305 return send_netbios_packet( packet );
308 /***************************************************************************
309 Sends out a multihomed name register.
310 **************************************************************************/
312 static BOOL initiate_multihomed_name_register_packet( struct packet_struct *packet,
313 uint16 nb_flags, struct in_addr *register_ip)
315 struct nmb_packet *nmb = &packet->packet.nmb;
316 char second_ip_buf[25];
318 strcpy(second_ip_buf, inet_ntoa(packet->ip));
320 nmb->header.opcode = NMB_NAME_MULTIHOMED_REG_OPCODE;
321 nmb->header.arcount = 1;
323 nmb->header.nm_flags.recursion_desired = True;
325 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
328 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
329 for name %s IP %s (bcast=%s) to IP %s\n",
330 namestr(&nmb->additional->rr_name), inet_ntoa(*register_ip),
331 BOOLSTR(nmb->header.nm_flags.bcast), second_ip_buf ));
333 return send_netbios_packet( packet );
336 /***************************************************************************
337 Sends out a name refresh.
338 **************************************************************************/
340 static BOOL initiate_name_refresh_packet( struct packet_struct *packet,
341 uint16 nb_flags, struct in_addr *refresh_ip)
343 struct nmb_packet *nmb = &packet->packet.nmb;
345 nmb->header.opcode = NMB_NAME_REFRESH_OPCODE_8;
346 nmb->header.arcount = 1;
348 nmb->header.nm_flags.recursion_desired = False;
350 if(create_and_init_additional_record(packet, nb_flags, refresh_ip) == False)
353 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
354 namestr(&nmb->additional->rr_name),
355 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
357 return send_netbios_packet( packet );
360 /***************************************************************************
361 Sends out a name release.
362 **************************************************************************/
364 static BOOL initiate_name_release_packet( struct packet_struct *packet,
365 uint16 nb_flags, struct in_addr *release_ip)
367 struct nmb_packet *nmb = &packet->packet.nmb;
369 nmb->header.opcode = NMB_NAME_RELEASE_OPCODE;
370 nmb->header.arcount = 1;
372 nmb->header.nm_flags.recursion_desired = False;
374 if(create_and_init_additional_record(packet, nb_flags, release_ip) == False)
377 DEBUG(4,("initiate_name_release_packet: sending release 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 node status.
386 **************************************************************************/
388 static BOOL initiate_node_status_packet( struct packet_struct *packet )
390 struct nmb_packet *nmb = &packet->packet.nmb;
392 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
393 nmb->header.arcount = 0;
395 nmb->header.nm_flags.recursion_desired = False;
397 nmb->question.question_type = QUESTION_TYPE_NB_STATUS;
399 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
400 namestr(&nmb->question.question_name),
401 inet_ntoa(packet->ip)));
403 return send_netbios_packet( packet );
406 /****************************************************************************
407 Simplification functions for queuing standard packets.
408 These should be the only publicly callable functions for sending
410 ****************************************************************************/
412 /****************************************************************************
413 Assertion - we should never be sending nmbd packets on the remote
415 ****************************************************************************/
417 static BOOL assert_check_subnet(struct subnet_record *subrec)
419 if( subrec == remote_broadcast_subnet)
421 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
428 /****************************************************************************
429 Queue a register name packet to the broadcast address of a subnet.
430 ****************************************************************************/
432 struct response_record *queue_register_name( struct subnet_record *subrec,
433 response_function resp_fn,
434 timeout_response_function timeout_fn,
435 register_name_success_function success_fn,
436 register_name_fail_function fail_fn,
437 struct userdata_struct *userdata,
438 struct nmb_name *nmbname,
441 struct packet_struct *p;
442 struct response_record *rrec;
443 BOOL bcast = (subrec == unicast_subnet) ? False : True;
445 if(assert_check_subnet(subrec))
448 if(( p = create_and_init_netbios_packet(nmbname, bcast,
449 subrec->bcast_ip)) == NULL)
452 if(initiate_name_register_packet( p, nb_flags,
453 iface_ip(subrec->bcast_ip)) == False)
460 if((rrec = make_response_record(subrec, /* subnet record. */
461 p, /* packet we sent. */
462 resp_fn, /* function to call on response. */
463 timeout_fn, /* function to call on timeout. */
464 (success_function)success_fn, /* function to call on operation success. */
465 (fail_function)fail_fn, /* function to call on operation fail. */
476 /****************************************************************************
477 Queue a multihomed register name packet to the broadcast address of a subnet.
478 ****************************************************************************/
480 struct response_record *queue_register_multihomed_name( struct subnet_record *subrec,
481 response_function resp_fn,
482 timeout_response_function timeout_fn,
483 register_name_success_function success_fn,
484 register_name_fail_function fail_fn,
485 struct userdata_struct *userdata,
486 struct nmb_name *nmbname,
488 struct in_addr register_ip)
490 struct packet_struct *p;
491 struct response_record *rrec;
496 if(subrec != unicast_subnet)
498 DEBUG(0,("queue_register_multihomed_name: should only be done on \
499 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
503 if(assert_check_subnet(subrec))
506 if(( p = create_and_init_netbios_packet(nmbname, bcast,
507 subrec->bcast_ip)) == NULL)
510 if (nb_flags & NB_GROUP)
511 ret = initiate_name_register_packet( p, nb_flags, ®ister_ip);
513 ret = initiate_multihomed_name_register_packet( p, nb_flags, ®ister_ip);
522 if((rrec = make_response_record(subrec, /* subnet record. */
523 p, /* packet we sent. */
524 resp_fn, /* function to call on response. */
525 timeout_fn, /* function to call on timeout. */
526 (success_function)success_fn, /* function to call on operation success. */
527 (fail_function)fail_fn, /* function to call on operation fail. */
538 /****************************************************************************
539 Queue a release name packet to the broadcast address of a subnet.
540 ****************************************************************************/
542 struct response_record *queue_release_name( struct subnet_record *subrec,
543 response_function resp_fn,
544 timeout_response_function timeout_fn,
545 release_name_success_function success_fn,
546 release_name_fail_function fail_fn,
547 struct userdata_struct *userdata,
548 struct nmb_name *nmbname,
550 struct in_addr release_ip)
552 BOOL bcast = (subrec == unicast_subnet) ? False : True;
553 struct packet_struct *p;
554 struct response_record *rrec;
556 if(assert_check_subnet(subrec))
559 if(( p = create_and_init_netbios_packet(nmbname, bcast,
560 subrec->bcast_ip)) == NULL)
563 if(initiate_name_release_packet( p, nb_flags, &release_ip) == False)
570 if((rrec = make_response_record(subrec, /* subnet record. */
571 p, /* packet we sent. */
572 resp_fn, /* function to call on response. */
573 timeout_fn, /* function to call on timeout. */
574 (success_function)success_fn, /* function to call on operation success. */
575 (fail_function)fail_fn, /* function to call on operation fail. */
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->nb_flags, &refresh_ip) == False)
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 node status packet to a given name and address.
684 ****************************************************************************/
686 struct response_record *queue_node_status( struct subnet_record *subrec,
687 response_function resp_fn,
688 timeout_response_function timeout_fn,
689 node_status_success_function success_fn,
690 node_status_fail_function fail_fn,
691 struct userdata_struct *userdata,
692 struct nmb_name *nmbname,
693 struct in_addr send_ip)
695 struct packet_struct *p;
696 struct response_record *rrec;
700 if(subrec != unicast_subnet)
702 DEBUG(0,("queue_register_multihomed_name: should only be done on \
703 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
707 if(assert_check_subnet(subrec))
710 if(( p = create_and_init_netbios_packet(nmbname, bcast,
714 if(initiate_node_status_packet(p) == False)
721 if((rrec = make_response_record(subrec, /* subnet record. */
722 p, /* packet we sent. */
723 resp_fn, /* function to call on response. */
724 timeout_fn, /* function to call on timeout. */
725 (success_function)success_fn, /* function to call on operation success. */
726 (fail_function)fail_fn, /* function to call on operation fail. */
737 /****************************************************************************
738 Reply to a netbios name packet. see rfc1002.txt
739 ****************************************************************************/
741 void reply_netbios_packet(struct packet_struct *orig_packet,
742 int rcode, enum netbios_reply_type_code rcv_code, int opcode,
743 int ttl, char *data,int len)
745 struct packet_struct packet;
746 struct nmb_packet *nmb = NULL;
747 struct res_rec answers;
748 struct nmb_packet *orig_nmb = &orig_packet->packet.nmb;
749 BOOL loopback_this_packet = False;
750 char *packet_type = "unknown";
752 /* Check if we are sending to or from ourselves. */
753 if(ismyip(orig_packet->ip) && (orig_packet->port == global_nmb_port))
754 loopback_this_packet = True;
756 nmb = &packet.packet.nmb;
758 /* Do a partial copy of the packet. We clear the locked flag and
759 the resource record pointers. */
760 packet = *orig_packet; /* Full structure copy. */
761 packet.locked = False;
764 nmb->additional = NULL;
770 packet_type = "nmb_status";
771 nmb->header.nm_flags.recursion_desired = False;
772 nmb->header.nm_flags.recursion_available = False;
777 packet_type = "nmb_query";
778 nmb->header.nm_flags.recursion_desired = True;
779 nmb->header.nm_flags.recursion_available = True;
783 case NMB_REG_REFRESH:
785 packet_type = "nmb_reg";
786 nmb->header.nm_flags.recursion_desired = True;
787 nmb->header.nm_flags.recursion_available = True;
792 packet_type = "nmb_rel";
793 nmb->header.nm_flags.recursion_desired = False;
794 nmb->header.nm_flags.recursion_available = False;
799 packet_type = "nmb_wack";
800 nmb->header.nm_flags.recursion_desired = False;
801 nmb->header.nm_flags.recursion_available = False;
806 packet_type = "wins_reg";
807 nmb->header.nm_flags.recursion_desired = True;
808 nmb->header.nm_flags.recursion_available = True;
813 packet_type = "wins_query";
814 nmb->header.nm_flags.recursion_desired = True;
815 nmb->header.nm_flags.recursion_available = True;
821 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
822 packet_type, namestr(&orig_nmb->question.question_name),
823 inet_ntoa(packet.ip)));
829 DEBUG(4,("reply_netbios_packet: sending a reply of packet type: %s %s to ip %s \
831 packet_type, namestr(&orig_nmb->question.question_name),
832 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
834 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
835 nmb->header.opcode = opcode;
836 nmb->header.response = True;
837 nmb->header.nm_flags.bcast = False;
838 nmb->header.nm_flags.trunc = False;
839 nmb->header.nm_flags.authoritative = True;
841 nmb->header.rcode = rcode;
842 nmb->header.qdcount = 0;
843 nmb->header.ancount = 1;
844 nmb->header.nscount = 0;
845 nmb->header.arcount = 0;
847 bzero((char*)&nmb->question,sizeof(nmb->question));
849 nmb->answers = &answers;
850 bzero((char*)nmb->answers,sizeof(*nmb->answers));
852 nmb->answers->rr_name = orig_nmb->question.question_name;
853 nmb->answers->rr_type = orig_nmb->question.question_type;
854 nmb->answers->rr_class = orig_nmb->question.question_class;
855 nmb->answers->ttl = ttl;
859 nmb->answers->rdlength = len;
860 memcpy(nmb->answers->rdata, data, len);
863 packet.packet_type = NMB_PACKET;
864 /* Ensure we send out on the same fd that the original
865 packet came in on to give the correct source IP address. */
866 packet.fd = orig_packet->fd;
867 packet.timestamp = time(NULL);
869 debug_nmb_packet(&packet);
871 if(loopback_this_packet)
873 struct packet_struct *lo_packet;
874 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
875 if((lo_packet = copy_packet(&packet)) == NULL)
877 queue_packet(lo_packet);
879 else if (!send_packet(&packet))
881 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
882 inet_ntoa(packet.ip),packet.port));
886 /*******************************************************************
887 Queue a packet into a packet queue
888 ******************************************************************/
890 void queue_packet(struct packet_struct *packet)
892 struct packet_struct *p;
898 packet_queue = packet;
902 /* find the bottom */
903 for (p=packet_queue;p->next;p=p->next)
911 /****************************************************************************
912 Try and find a matching subnet record for a datagram port 138 packet.
913 ****************************************************************************/
915 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
917 struct subnet_record *subrec;
919 /* Go through all the broadcast subnets and see if the mask matches. */
920 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
922 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
926 /* If the subnet record is the remote announce broadcast subnet,
927 hack it here to be the first subnet. This is really gross and
928 is needed due to people turning on port 137/138 broadcast
929 forwarding on their routers. May fire and brimstone rain
936 /****************************************************************************
937 Dispatch a browse frame from port 138 to the correct processing function.
938 ****************************************************************************/
940 void process_browse_packet(struct packet_struct *p, char *buf,int len)
942 struct dgram_packet *dgram = &p->packet.dgram;
943 int command = CVAL(buf,0);
944 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
946 /* Drop the packet if it's a different NetBIOS scope, or
947 the source is from one of our names. */
949 if (!strequal(dgram->dest_name.scope,scope ))
951 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
952 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
956 if (is_myname(dgram->source_name.name))
958 DEBUG(0,("process_browse_packet: Discarding datagram from IP %s. Source name \
959 %s is one of our names !\n", inet_ntoa(p->ip), namestr(&dgram->source_name)));
965 case ANN_HostAnnouncement:
967 debug_browse_data(buf, len);
968 process_host_announce(subrec, p, buf+1);
971 case ANN_DomainAnnouncement:
973 debug_browse_data(buf, len);
974 process_workgroup_announce(subrec, p, buf+1);
977 case ANN_LocalMasterAnnouncement:
979 debug_browse_data(buf, len);
980 process_local_master_announce(subrec, p, buf+1);
983 case ANN_AnnouncementRequest:
985 debug_browse_data(buf, len);
986 process_announce_request(subrec, p, buf+1);
991 debug_browse_data(buf, len);
992 process_election(subrec, p, buf+1);
995 case ANN_GetBackupListReq:
997 debug_browse_data(buf, len);
999 /* This is one occasion where we change a subnet that is
1000 given to us. If the packet was sent to WORKGROUP<1b> instead
1001 of WORKGROUP<1d> then it was unicast to us a domain master
1002 browser. Change subrec to unicast.
1004 if(dgram->dest_name.name_type == 0x1b)
1005 subrec = unicast_subnet;
1007 process_get_backup_list_request(subrec, p, buf+1);
1010 case ANN_GetBackupListResp:
1012 debug_browse_data(buf, len);
1013 /* We never send ANN_GetBackupListReq so we
1014 should never get these. */
1015 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1016 packet from %s IP %s\n", namestr(&dgram->source_name), inet_ntoa(p->ip)));
1019 case ANN_ResetBrowserState:
1021 debug_browse_data(buf, len);
1022 process_reset_browser(subrec, p, buf+1);
1025 case ANN_MasterAnnouncement:
1027 /* Master browser datagrams must be processed
1028 on the unicast subnet. */
1029 subrec = unicast_subnet;
1031 debug_browse_data(buf, len);
1032 process_master_browser_announce(subrec, p, buf+1);
1035 case ANN_BecomeBackup:
1038 * We don't currently implement this. Log it just in case.
1040 debug_browse_data(buf, len);
1041 DEBUG(10,("process_browse_packet: On subnet %s ignoring browse packet \
1042 command ANN_BecomeBackup from %s IP %s to %s\n",
1043 subrec->subnet_name, namestr(&dgram->source_name),
1044 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1049 debug_browse_data(buf, len);
1050 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1051 command code %d from %s IP %s to %s\n",
1052 subrec->subnet_name, command, namestr(&dgram->source_name),
1053 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1058 /****************************************************************************
1059 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1060 ****************************************************************************/
1062 void process_lanman_packet(struct packet_struct *p, char *buf,int len)
1064 struct dgram_packet *dgram = &p->packet.dgram;
1065 int command = SVAL(buf,0);
1066 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1068 /* Drop the packet if it's a different NetBIOS scope, or
1069 the source is from one of our names. */
1071 if (!strequal(dgram->dest_name.scope,scope ))
1073 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1074 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
1078 if (is_myname(dgram->source_name.name))
1080 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1081 %s is one of our names !\n", inet_ntoa(p->ip), namestr(&dgram->source_name)));
1087 case ANN_HostAnnouncement:
1089 debug_browse_data(buf, len);
1090 process_lm_host_announce(subrec, p, buf+1);
1093 case ANN_AnnouncementRequest:
1095 process_lm_announce_request(subrec, p, buf+1);
1100 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1101 command code %d from %s IP %s to %s\n",
1102 subrec->subnet_name, command, namestr(&dgram->source_name),
1103 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1108 /****************************************************************************
1109 Determine if a packet is for us on port 138. Note that to have any chance of
1110 being efficient we need to drop as many packets as possible at this
1111 stage as subsequent processing is expensive.
1112 ****************************************************************************/
1114 static BOOL listening(struct packet_struct *p,struct nmb_name *nbname)
1116 struct subnet_record *subrec = NULL;
1118 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1120 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1125 subrec = unicast_subnet;
1127 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1130 /****************************************************************************
1131 Process udp 138 datagrams
1132 ****************************************************************************/
1134 static void process_dgram(struct packet_struct *p)
1139 struct dgram_packet *dgram = &p->packet.dgram;
1141 /* If we aren't listening to the destination name then ignore the packet */
1142 if (!listening(p,&dgram->dest_name))
1144 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1145 namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1149 if (dgram->header.msg_type != 0x10 &&
1150 dgram->header.msg_type != 0x11 &&
1151 dgram->header.msg_type != 0x12)
1153 /* Don't process error packets etc yet */
1154 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1155 an error packet of type %x\n",
1156 namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1160 buf = &dgram->data[0];
1161 buf -= 4; /* XXXX for the pseudo tcp length -
1162 someday I need to get rid of this */
1164 if (CVAL(buf,smb_com) != SMBtrans)
1167 len = SVAL(buf,smb_vwv11);
1168 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1170 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1171 namestr(&dgram->source_name),namestr(&dgram->dest_name),
1172 inet_ntoa(p->ip), smb_buf(buf),CVAL(buf2,0),len));
1178 /* Datagram packet received for the browser mailslot */
1179 if (strequal(smb_buf(buf),BROWSE_MAILSLOT))
1181 process_browse_packet(p,buf2,len);
1185 /* Datagram packet received for the LAN Manager mailslot */
1186 if (strequal(smb_buf(buf),LANMAN_MAILSLOT)) {
1187 process_lanman_packet(p,buf2,len);
1191 /* Datagram packet received for the domain logon mailslot */
1192 if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT))
1194 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1198 /* Datagram packet received for the NT domain logon mailslot */
1199 if (strequal(smb_buf(buf),NT_LOGON_MAILSLOT))
1201 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1206 /****************************************************************************
1207 Validate a response nmb packet.
1208 ****************************************************************************/
1210 BOOL validate_nmb_response_packet( struct nmb_packet *nmb )
1212 BOOL ignore = False;
1214 switch (nmb->header.opcode)
1216 case NMB_NAME_REG_OPCODE:
1217 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1218 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1219 if (nmb->header.ancount == 0)
1221 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1226 case NMB_NAME_QUERY_OPCODE:
1227 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1))
1229 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1233 case NMB_NAME_RELEASE_OPCODE:
1234 if (nmb->header.ancount == 0)
1236 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1240 case NMB_WACK_OPCODE:
1241 /* Check WACK response here. */
1242 if (nmb->header.ancount != 1)
1244 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1249 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1250 nmb->header.opcode));
1255 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1260 /****************************************************************************
1261 Validate a request nmb packet.
1262 ****************************************************************************/
1264 BOOL validate_nmb_packet( struct nmb_packet *nmb )
1266 BOOL ignore = False;
1268 switch (nmb->header.opcode)
1270 case NMB_NAME_REG_OPCODE:
1271 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1272 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1273 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1274 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1276 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1281 case NMB_NAME_QUERY_OPCODE:
1282 if ((nmb->header.qdcount == 0) ||
1283 ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1284 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS)))
1286 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1291 case NMB_NAME_RELEASE_OPCODE:
1292 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1294 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1299 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1300 nmb->header.opcode));
1305 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1310 /****************************************************************************
1311 Find a subnet (and potentially a response record) for a packet.
1312 ****************************************************************************/
1314 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1315 struct response_record **pprrec)
1317 struct nmb_packet *nmb = &p->packet.nmb;
1318 struct response_record *rrec = NULL;
1319 struct subnet_record *subrec = NULL;
1324 if(nmb->header.response)
1326 /* It's a response packet. Find a record for it or it's an error. */
1328 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1331 DEBUG(0,("find_subnet_for_nmb_packet: response record not found for response id %hu\n",
1332 nmb->header.name_trn_id));
1338 DEBUG(0,("find_subnet_for_nmb_packet: subnet record not found for response id %hu\n",
1339 nmb->header.name_trn_id));
1348 /* Try and see what subnet this packet belongs to. */
1351 if(packet_is_for_wins_server(p))
1352 return wins_server_subnet;
1354 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1355 if(nmb->header.nm_flags.bcast == False)
1356 return unicast_subnet;
1358 /* Go through all the broadcast subnets and see if the mask matches. */
1359 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1361 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1365 /* If none match it must have been a directed broadcast - assign
1366 the remote_broadcast_subnet. */
1367 return remote_broadcast_subnet;
1370 /****************************************************************************
1371 Process a nmb request packet - validate the packet and route it.
1372 ****************************************************************************/
1374 static void process_nmb_request(struct packet_struct *p)
1376 struct nmb_packet *nmb = &p->packet.nmb;
1377 struct subnet_record *subrec = NULL;
1379 debug_nmb_packet(p);
1381 /* Ensure we have a good packet. */
1382 if(validate_nmb_packet(nmb))
1385 /* Allocate a subnet to this packet - if we cannot - fail. */
1386 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1389 switch (nmb->header.opcode)
1391 case NMB_NAME_REG_OPCODE:
1392 if(subrec == wins_server_subnet)
1393 wins_process_name_registration_request(subrec, p);
1395 process_name_registration_request(subrec, p);
1398 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1399 case NMB_NAME_REFRESH_OPCODE_9:
1400 if(subrec == wins_server_subnet)
1401 wins_process_name_refresh_request(subrec, p);
1403 process_name_refresh_request(subrec, p);
1406 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1407 if(subrec == wins_server_subnet)
1408 wins_process_multihomed_name_registration_request(subrec, p);
1411 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1412 directed at a WINS server.\n"));
1416 case NMB_NAME_QUERY_OPCODE:
1417 switch (nmb->question.question_type)
1419 case QUESTION_TYPE_NB_QUERY:
1421 if(subrec == wins_server_subnet)
1422 wins_process_name_query_request(subrec, p);
1424 process_name_query_request(subrec, p);
1427 case QUESTION_TYPE_NB_STATUS:
1429 if(subrec == wins_server_subnet)
1431 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1436 process_node_status_request(subrec, p);
1442 case NMB_NAME_RELEASE_OPCODE:
1443 if(subrec == wins_server_subnet)
1444 wins_process_name_release_request(subrec, p);
1446 process_name_release_request(subrec, p);
1451 /****************************************************************************
1452 Process a nmb response packet - validate the packet and route it.
1453 to either the WINS server or a normal response.
1454 ****************************************************************************/
1456 static void process_nmb_response(struct packet_struct *p)
1458 struct nmb_packet *nmb = &p->packet.nmb;
1459 struct subnet_record *subrec = NULL;
1460 struct response_record *rrec = NULL;
1462 debug_nmb_packet(p);
1464 if(validate_nmb_response_packet(nmb))
1467 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1472 DEBUG(0,("process_nmb_response: response packet received but no response record \
1473 found for id = %hu. Ignoring packet.\n", nmb->header.name_trn_id));
1477 /* Increment the number of responses received for this record. */
1479 /* Ensure we don't re-send the request. */
1480 rrec->repeat_count = 0;
1482 /* Call the response received function for this packet. */
1483 (*rrec->resp_fn)(subrec, rrec, p);
1487 /*******************************************************************
1488 Run elements off the packet queue till its empty
1489 ******************************************************************/
1491 void run_packet_queue()
1493 struct packet_struct *p;
1495 while ((p = packet_queue))
1497 packet_queue = p->next;
1499 packet_queue->prev = NULL;
1500 p->next = p->prev = NULL;
1502 switch (p->packet_type)
1505 if(p->packet.nmb.header.response)
1506 process_nmb_response(p);
1508 process_nmb_request(p);
1519 /*******************************************************************
1520 Retransmit or timeout elements from all the outgoing subnet response
1521 record queues. NOTE that this code must also check the WINS server
1522 subnet for response records to timeout as the WINS server code
1523 can send requests to check if a client still owns a name.
1524 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1525 ******************************************************************/
1527 void retransmit_or_expire_response_records(time_t t)
1529 struct subnet_record *subrec;
1531 for (subrec = FIRST_SUBNET; subrec;
1532 subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec))
1534 struct response_record *rrec, *nextrrec;
1536 for (rrec = subrec->responselist; rrec; rrec = nextrrec)
1538 nextrrec = rrec->next;
1540 if (rrec->repeat_time <= t)
1542 if (rrec->repeat_count > 0)
1544 /* Resend while we have a non-zero repeat_count. */
1545 if(!send_packet(rrec->packet))
1547 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1548 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1549 subrec->subnet_name));
1551 rrec->repeat_time += rrec->repeat_interval;
1552 rrec->repeat_count--;
1556 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1557 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1558 subrec->subnet_name));
1560 /* Call the timeout function. This will deal with removing the
1561 timed out packet. */
1562 if(rrec->timeout_fn)
1563 (*rrec->timeout_fn)(subrec, rrec);
1566 /* We must remove the record ourself if there is
1567 no timeout function. */
1568 remove_response_record(subrec, rrec);
1570 } /* rrec->repeat_count > 0 */
1571 } /* rrec->repeat_time <= t */
1572 } /* end for rrec */
1573 } /* end for subnet */
1576 /****************************************************************************
1577 Create an fd_set containing all the sockets in the subnet structures,
1578 plus the broadcast sockets.
1579 ***************************************************************************/
1581 static BOOL create_listen_fdset(fd_set **ppset, int **psock_array, int *listen_number)
1583 int *sock_array = NULL;
1584 struct subnet_record *subrec = NULL;
1587 fd_set *pset = (fd_set *)malloc(sizeof(fd_set));
1591 DEBUG(0,("create_listen_fdset: malloc fail !\n"));
1595 /* Check that we can add all the fd's we need. */
1596 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1599 if((count*2) + 2 > FD_SETSIZE)
1601 DEBUG(0,("create_listen_fdset: Too many file descriptors needed (%d). We can \
1602 only use %d.\n", (count*2) + 2, FD_SETSIZE));
1606 if((sock_array = (int *)malloc(((count*2) + 2)*sizeof(int))) == NULL)
1608 DEBUG(0,("create_listen_fdset: malloc fail for socket array.\n"));
1614 /* Add in the broadcast socket on 137. */
1615 FD_SET(ClientNMB,pset);
1616 sock_array[num++] = ClientNMB;
1618 /* Add in the 137 sockets on all the interfaces. */
1619 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1621 FD_SET(subrec->nmb_sock,pset);
1622 sock_array[num++] = subrec->nmb_sock;
1625 /* Add in the broadcast socket on 138. */
1626 FD_SET(ClientDGRAM,pset);
1627 sock_array[num++] = ClientDGRAM;
1629 /* Add in the 138 sockets on all the interfaces. */
1630 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1632 FD_SET(subrec->dgram_sock,pset);
1633 sock_array[num++] = subrec->dgram_sock;
1636 *listen_number = (count*2) + 2;
1638 *psock_array = sock_array;
1643 /****************************************************************************
1644 Listens for NMB or DGRAM packets, and queues them.
1645 ***************************************************************************/
1647 BOOL listen_for_packets(BOOL run_election)
1649 static fd_set *listen_set = NULL;
1650 static int listen_number = 0;
1651 static int *sock_array = NULL;
1655 struct timeval timeout;
1660 if(listen_set == NULL)
1662 if(create_listen_fdset(&listen_set, &sock_array, &listen_number))
1664 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1669 memcpy((char *)&fds, (char *)listen_set, sizeof(fd_set));
1672 dns_fd = asyncdns_fd();
1674 FD_SET(dns_fd, &fds);
1680 * During elections and when expecting a netbios response packet we
1681 * need to send election packets at tighter intervals.
1682 * Ideally it needs to be the interval (in ms) between time now and
1683 * the time we are expecting the next netbios packet.
1686 timeout.tv_sec = (run_election||num_response_packets) ? 1 : NMBD_SELECT_LOOP;
1687 timeout.tv_usec = 0;
1689 /* Prepare for the select - allow certain signals. */
1691 BlockSignals(False, SIGTERM);
1692 #if defined(SIGUSR1)
1693 BlockSignals(False, SIGUSR1);
1694 #endif /* SIGUSR1 */
1695 #if defined(SIGUSR2)
1696 BlockSignals(False, SIGUSR2);
1697 #endif /* SIGUSR2 */
1699 selrtn = sys_select(&fds,&timeout);
1701 /* We can only take signals when we are in the select - block them again here. */
1703 BlockSignals(True, SIGTERM);
1704 #if defined(SIGUSR1)
1705 BlockSignals(True, SIGUSR1);
1706 #endif /* SIGUSR1 */
1707 #if defined(SIGUSR2)
1708 BlockSignals(True, SIGUSR2);
1709 #endif /* SIGUSR2 */
1716 if (dns_fd != -1 && FD_ISSET(dns_fd,&fds)) {
1721 for(i = 0; i < listen_number; i++)
1723 if(i < (listen_number/2))
1725 /* Processing a 137 socket. */
1726 if (FD_ISSET(sock_array[i],&fds))
1728 struct packet_struct *packet = read_packet(sock_array[i], NMB_PACKET);
1732 * If we got a packet on the broadcast socket and interfaces
1733 * only is set then check it came from one of our local nets.
1735 if(lp_bind_interfaces_only() && (sock_array[i] == ClientNMB) &&
1736 (!is_local_net(packet->ip)))
1738 DEBUG(7,("discarding nmb packet sent to broadcast socket from %s:%d\n",
1739 inet_ntoa(packet->ip),packet->port));
1740 free_packet(packet);
1742 else if ((ip_equal(loopback_ip, packet->ip) ||
1743 ismyip(packet->ip)) && packet->port == global_nmb_port)
1745 DEBUG(7,("discarding own packet from %s:%d\n",
1746 inet_ntoa(packet->ip),packet->port));
1747 free_packet(packet);
1751 /* Save the file descriptor this packet came in on. */
1752 packet->fd = sock_array[i];
1753 queue_packet(packet);
1760 /* Processing a 138 socket. */
1762 if (FD_ISSET(sock_array[i],&fds))
1764 struct packet_struct *packet = read_packet(sock_array[i], DGRAM_PACKET);
1768 * If we got a packet on the broadcast socket and interfaces
1769 * only is set then check it came from one of our local nets.
1771 if(lp_bind_interfaces_only() && (sock_array[i] == ClientDGRAM) &&
1772 (!is_local_net(packet->ip)))
1774 DEBUG(7,("discarding dgram packet sent to broadcast socket from %s:%d\n",
1775 inet_ntoa(packet->ip),packet->port));
1776 free_packet(packet);
1778 else if ((ip_equal(loopback_ip, packet->ip) ||
1779 ismyip(packet->ip)) && packet->port == DGRAM_PORT)
1781 DEBUG(7,("discarding own packet from %s:%d\n",
1782 inet_ntoa(packet->ip),packet->port));
1783 free_packet(packet);
1787 /* Save the file descriptor this packet came in on. */
1788 packet->fd = sock_array[i];
1789 queue_packet(packet);
1793 } /* end processing 138 socket. */
1795 } /* end if selret > 0 */
1799 /****************************************************************************
1800 Construct and send a netbios DGRAM.
1801 Note that this currently sends all packets to port 138.
1802 **************************************************************************/
1804 BOOL send_mailslot(BOOL unique, char *mailslot,char *buf,int len,
1805 char *srcname, int src_type,
1806 char *dstname, int dest_type,
1807 struct in_addr dest_ip,struct in_addr src_ip)
1809 BOOL loopback_this_packet = False;
1810 struct packet_struct p;
1811 struct dgram_packet *dgram = &p.packet.dgram;
1815 bzero((char *)&p,sizeof(p));
1818 loopback_this_packet = True;
1820 generate_name_trn_id();
1822 /* DIRECT GROUP or UNIQUE datagram. */
1823 dgram->header.msg_type = unique ? 0x10 : 0x11;
1824 dgram->header.flags.node_type = M_NODE;
1825 dgram->header.flags.first = True;
1826 dgram->header.flags.more = False;
1827 dgram->header.dgm_id = name_trn_id;
1828 dgram->header.source_ip = src_ip;
1829 dgram->header.source_port = DGRAM_PORT;
1830 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
1831 dgram->header.packet_offset = 0;
1833 make_nmb_name(&dgram->source_name,srcname,src_type,scope);
1834 make_nmb_name(&dgram->dest_name,dstname,dest_type,scope);
1836 ptr = &dgram->data[0];
1838 /* Setup the smb part. */
1839 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
1841 set_message(ptr,17,17 + len,True);
1844 CVAL(ptr,smb_com) = SMBtrans;
1845 SSVAL(ptr,smb_vwv1,len);
1846 SSVAL(ptr,smb_vwv11,len);
1847 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
1848 SSVAL(ptr,smb_vwv13,3);
1849 SSVAL(ptr,smb_vwv14,1);
1850 SSVAL(ptr,smb_vwv15,1);
1851 SSVAL(ptr,smb_vwv16,2);
1853 strcpy(p2,mailslot);
1854 p2 = skip_string(p2,1);
1859 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
1862 p.port = DGRAM_PORT;
1863 p.fd = find_subnet_mailslot_fd_for_address( src_ip );
1864 p.timestamp = time(NULL);
1865 p.packet_type = DGRAM_PACKET;
1867 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
1868 namestr(&dgram->source_name), inet_ntoa(src_ip)));
1869 DEBUG(4,("to %s IP %s\n", namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
1871 debug_browse_data(buf, len);
1873 if(loopback_this_packet)
1875 struct packet_struct *lo_packet = NULL;
1876 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
1877 if((lo_packet = copy_packet(&p)) == NULL)
1879 queue_packet(lo_packet);
1883 return(send_packet(&p));