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 process_announce_request(subrec, p, buf+1);
990 process_election(subrec, p, buf+1);
993 case ANN_GetBackupListReq:
995 debug_browse_data(buf, len);
997 /* This is one occasion where we change a subnet that is
998 given to us. If the packet was sent to WORKGROUP<1b> instead
999 of WORKGROUP<1d> then it was unicast to us a domain master
1000 browser. Change subrec to unicast.
1002 if(dgram->dest_name.name_type == 0x1b)
1003 subrec = unicast_subnet;
1005 process_get_backup_list_request(subrec, p, buf+1);
1008 case ANN_GetBackupListResp:
1010 debug_browse_data(buf, len);
1011 /* We never send ANN_GetBackupListReq so we
1012 should never get these. */
1013 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1014 packet from %s IP %s\n", namestr(&dgram->source_name), inet_ntoa(p->ip)));
1017 case ANN_ResetBrowserState:
1019 process_reset_browser(subrec, p, buf+1);
1022 case ANN_MasterAnnouncement:
1024 /* Master browser datagrams must be processed
1025 on the unicast subnet. */
1026 subrec = unicast_subnet;
1028 process_master_browser_announce(subrec, p, buf+1);
1033 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1034 command code %d from %s IP %s to %s\n",
1035 subrec->subnet_name, command, namestr(&dgram->source_name),
1036 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1041 /****************************************************************************
1042 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1043 ****************************************************************************/
1045 void process_lanman_packet(struct packet_struct *p, char *buf,int len)
1047 struct dgram_packet *dgram = &p->packet.dgram;
1048 int command = SVAL(buf,0);
1049 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1051 /* Drop the packet if it's a different NetBIOS scope, or
1052 the source is from one of our names. */
1054 if (!strequal(dgram->dest_name.scope,scope ))
1056 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1057 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
1061 if (is_myname(dgram->source_name.name))
1063 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1064 %s is one of our names !\n", inet_ntoa(p->ip), namestr(&dgram->source_name)));
1070 case ANN_HostAnnouncement:
1072 debug_browse_data(buf, len);
1073 process_lm_host_announce(subrec, p, buf+1);
1076 case ANN_AnnouncementRequest:
1078 process_lm_announce_request(subrec, p, buf+1);
1083 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1084 command code %d from %s IP %s to %s\n",
1085 subrec->subnet_name, command, namestr(&dgram->source_name),
1086 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1091 /****************************************************************************
1092 Determine if a packet is for us on port 138. Note that to have any chance of
1093 being efficient we need to drop as many packets as possible at this
1094 stage as subsequent processing is expensive.
1095 ****************************************************************************/
1097 static BOOL listening(struct packet_struct *p,struct nmb_name *nbname)
1099 struct subnet_record *subrec = NULL;
1101 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1103 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1108 subrec = unicast_subnet;
1110 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1113 /****************************************************************************
1114 Process udp 138 datagrams
1115 ****************************************************************************/
1117 static void process_dgram(struct packet_struct *p)
1122 struct dgram_packet *dgram = &p->packet.dgram;
1124 /* If we aren't listening to the destination name then ignore the packet */
1125 if (!listening(p,&dgram->dest_name))
1127 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1128 namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1132 if (dgram->header.msg_type != 0x10 &&
1133 dgram->header.msg_type != 0x11 &&
1134 dgram->header.msg_type != 0x12)
1136 /* Don't process error packets etc yet */
1137 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1138 an error packet of type %x\n",
1139 namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1143 buf = &dgram->data[0];
1144 buf -= 4; /* XXXX for the pseudo tcp length -
1145 someday I need to get rid of this */
1147 if (CVAL(buf,smb_com) != SMBtrans)
1150 len = SVAL(buf,smb_vwv11);
1151 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1153 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1154 namestr(&dgram->source_name),namestr(&dgram->dest_name),
1155 inet_ntoa(p->ip), smb_buf(buf),CVAL(buf2,0),len));
1161 /* Datagram packet received for the browser mailslot */
1162 if (strequal(smb_buf(buf),BROWSE_MAILSLOT))
1164 process_browse_packet(p,buf2,len);
1168 /* Datagram packet received for the LAN Manager mailslot */
1169 if (strequal(smb_buf(buf),LANMAN_MAILSLOT)) {
1170 process_lanman_packet(p,buf2,len);
1174 /* Datagram packet received for the domain logon mailslot */
1175 if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT))
1177 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1181 /* Datagram packet received for the NT domain logon mailslot */
1182 if (strequal(smb_buf(buf),NT_LOGON_MAILSLOT))
1184 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1189 /****************************************************************************
1190 Validate a response nmb packet.
1191 ****************************************************************************/
1193 BOOL validate_nmb_response_packet( struct nmb_packet *nmb )
1195 BOOL ignore = False;
1197 switch (nmb->header.opcode)
1199 case NMB_NAME_REG_OPCODE:
1200 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1201 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1202 if (nmb->header.ancount == 0)
1204 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1209 case NMB_NAME_QUERY_OPCODE:
1210 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1))
1212 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1216 case NMB_NAME_RELEASE_OPCODE:
1217 if (nmb->header.ancount == 0)
1219 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1223 case NMB_WACK_OPCODE:
1224 /* Check WACK response here. */
1225 if (nmb->header.ancount != 1)
1227 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1232 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1233 nmb->header.opcode));
1238 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1243 /****************************************************************************
1244 Validate a request nmb packet.
1245 ****************************************************************************/
1247 BOOL validate_nmb_packet( struct nmb_packet *nmb )
1249 BOOL ignore = False;
1251 switch (nmb->header.opcode)
1253 case NMB_NAME_REG_OPCODE:
1254 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1255 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1256 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1257 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1259 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1264 case NMB_NAME_QUERY_OPCODE:
1265 if ((nmb->header.qdcount == 0) ||
1266 ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1267 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS)))
1269 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1274 case NMB_NAME_RELEASE_OPCODE:
1275 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1277 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1282 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1283 nmb->header.opcode));
1288 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1293 /****************************************************************************
1294 Find a subnet (and potentially a response record) for a packet.
1295 ****************************************************************************/
1297 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1298 struct response_record **pprrec)
1300 struct nmb_packet *nmb = &p->packet.nmb;
1301 struct response_record *rrec = NULL;
1302 struct subnet_record *subrec = NULL;
1307 if(nmb->header.response)
1309 /* It's a response packet. Find a record for it or it's an error. */
1311 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1314 DEBUG(0,("find_subnet_for_nmb_packet: response record not found for response id %hu\n",
1315 nmb->header.name_trn_id));
1321 DEBUG(0,("find_subnet_for_nmb_packet: subnet record not found for response id %hu\n",
1322 nmb->header.name_trn_id));
1331 /* Try and see what subnet this packet belongs to. */
1334 if(packet_is_for_wins_server(p))
1335 return wins_server_subnet;
1337 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1338 if(nmb->header.nm_flags.bcast == False)
1339 return unicast_subnet;
1341 /* Go through all the broadcast subnets and see if the mask matches. */
1342 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1344 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1348 /* If none match it must have been a directed broadcast - assign
1349 the remote_broadcast_subnet. */
1350 return remote_broadcast_subnet;
1353 /****************************************************************************
1354 Process a nmb request packet - validate the packet and route it.
1355 ****************************************************************************/
1357 static void process_nmb_request(struct packet_struct *p)
1359 struct nmb_packet *nmb = &p->packet.nmb;
1360 struct subnet_record *subrec = NULL;
1362 debug_nmb_packet(p);
1364 /* Ensure we have a good packet. */
1365 if(validate_nmb_packet(nmb))
1368 /* Allocate a subnet to this packet - if we cannot - fail. */
1369 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1372 switch (nmb->header.opcode)
1374 case NMB_NAME_REG_OPCODE:
1375 if(subrec == wins_server_subnet)
1376 wins_process_name_registration_request(subrec, p);
1378 process_name_registration_request(subrec, p);
1381 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1382 case NMB_NAME_REFRESH_OPCODE_9:
1383 if(subrec == wins_server_subnet)
1384 wins_process_name_refresh_request(subrec, p);
1386 process_name_refresh_request(subrec, p);
1389 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1390 if(subrec == wins_server_subnet)
1391 wins_process_multihomed_name_registration_request(subrec, p);
1394 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1395 directed at a WINS server.\n"));
1399 case NMB_NAME_QUERY_OPCODE:
1400 switch (nmb->question.question_type)
1402 case QUESTION_TYPE_NB_QUERY:
1404 if(subrec == wins_server_subnet)
1405 wins_process_name_query_request(subrec, p);
1407 process_name_query_request(subrec, p);
1410 case QUESTION_TYPE_NB_STATUS:
1412 if(subrec == wins_server_subnet)
1414 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1419 process_node_status_request(subrec, p);
1425 case NMB_NAME_RELEASE_OPCODE:
1426 if(subrec == wins_server_subnet)
1427 wins_process_name_release_request(subrec, p);
1429 process_name_release_request(subrec, p);
1434 /****************************************************************************
1435 Process a nmb response packet - validate the packet and route it.
1436 to either the WINS server or a normal response.
1437 ****************************************************************************/
1439 static void process_nmb_response(struct packet_struct *p)
1441 struct nmb_packet *nmb = &p->packet.nmb;
1442 struct subnet_record *subrec = NULL;
1443 struct response_record *rrec = NULL;
1445 debug_nmb_packet(p);
1447 if(validate_nmb_response_packet(nmb))
1450 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1455 DEBUG(0,("process_nmb_response: response packet received but no response record \
1456 found for id = %hu. Ignoring packet.\n", nmb->header.name_trn_id));
1460 /* Increment the number of responses received for this record. */
1462 /* Ensure we don't re-send the request. */
1463 rrec->repeat_count = 0;
1465 /* Call the response received function for this packet. */
1466 (*rrec->resp_fn)(subrec, rrec, p);
1470 /*******************************************************************
1471 Run elements off the packet queue till its empty
1472 ******************************************************************/
1474 void run_packet_queue()
1476 struct packet_struct *p;
1478 while ((p = packet_queue))
1480 packet_queue = p->next;
1482 packet_queue->prev = NULL;
1483 p->next = p->prev = NULL;
1485 switch (p->packet_type)
1488 if(p->packet.nmb.header.response)
1489 process_nmb_response(p);
1491 process_nmb_request(p);
1502 /*******************************************************************
1503 Retransmit or timeout elements from all the outgoing subnet response
1504 record queues. NOTE that this code must also check the WINS server
1505 subnet for response records to timeout as the WINS server code
1506 can send requests to check if a client still owns a name.
1507 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1508 ******************************************************************/
1510 void retransmit_or_expire_response_records(time_t t)
1512 struct subnet_record *subrec;
1514 for (subrec = FIRST_SUBNET; subrec;
1515 subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec))
1517 struct response_record *rrec, *nextrrec;
1519 for (rrec = subrec->responselist; rrec; rrec = nextrrec)
1521 nextrrec = rrec->next;
1523 if (rrec->repeat_time <= t)
1525 if (rrec->repeat_count > 0)
1527 /* Resend while we have a non-zero repeat_count. */
1528 if(!send_packet(rrec->packet))
1530 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1531 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1532 subrec->subnet_name));
1534 rrec->repeat_time += rrec->repeat_interval;
1535 rrec->repeat_count--;
1539 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1540 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1541 subrec->subnet_name));
1543 /* Call the timeout function. This will deal with removing the
1544 timed out packet. */
1545 if(rrec->timeout_fn)
1546 (*rrec->timeout_fn)(subrec, rrec);
1549 /* We must remove the record ourself if there is
1550 no timeout function. */
1551 remove_response_record(subrec, rrec);
1553 } /* rrec->repeat_count > 0 */
1554 } /* rrec->repeat_time <= t */
1555 } /* end for rrec */
1556 } /* end for subnet */
1559 /****************************************************************************
1560 Create an fd_set containing all the sockets in the subnet structures,
1561 plus the broadcast sockets.
1562 ***************************************************************************/
1564 static BOOL create_listen_fdset(fd_set **ppset, int **psock_array, int *listen_number)
1566 int *sock_array = NULL;
1567 struct subnet_record *subrec = NULL;
1570 fd_set *pset = (fd_set *)malloc(sizeof(fd_set));
1574 DEBUG(0,("create_listen_fdset: malloc fail !\n"));
1578 /* Check that we can add all the fd's we need. */
1579 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1582 if((count*2) + 2 > FD_SETSIZE)
1584 DEBUG(0,("create_listen_fdset: Too many file descriptors needed (%d). We can \
1585 only use %d.\n", (count*2) + 2, FD_SETSIZE));
1589 if((sock_array = (int *)malloc(((count*2) + 2)*sizeof(int))) == NULL)
1591 DEBUG(0,("create_listen_fdset: malloc fail for socket array.\n"));
1597 /* Add in the broadcast socket on 137. */
1598 FD_SET(ClientNMB,pset);
1599 sock_array[num++] = ClientNMB;
1601 /* Add in the 137 sockets on all the interfaces. */
1602 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1604 FD_SET(subrec->nmb_sock,pset);
1605 sock_array[num++] = subrec->nmb_sock;
1608 /* Add in the broadcast socket on 138. */
1609 FD_SET(ClientDGRAM,pset);
1610 sock_array[num++] = ClientDGRAM;
1612 /* Add in the 138 sockets on all the interfaces. */
1613 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1615 FD_SET(subrec->dgram_sock,pset);
1616 sock_array[num++] = subrec->dgram_sock;
1619 *listen_number = (count*2) + 2;
1621 *psock_array = sock_array;
1626 /****************************************************************************
1627 Listens for NMB or DGRAM packets, and queues them.
1628 ***************************************************************************/
1630 BOOL listen_for_packets(BOOL run_election)
1632 static fd_set *listen_set = NULL;
1633 static int listen_number = 0;
1634 static int *sock_array = NULL;
1638 struct timeval timeout;
1643 if(listen_set == NULL)
1645 if(create_listen_fdset(&listen_set, &sock_array, &listen_number))
1647 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1652 memcpy((char *)&fds, (char *)listen_set, sizeof(fd_set));
1655 dns_fd = asyncdns_fd();
1657 FD_SET(dns_fd, &fds);
1663 * During elections and when expecting a netbios response packet we
1664 * need to send election packets at tighter intervals.
1665 * Ideally it needs to be the interval (in ms) between time now and
1666 * the time we are expecting the next netbios packet.
1669 timeout.tv_sec = (run_election||num_response_packets) ? 1 : NMBD_SELECT_LOOP;
1670 timeout.tv_usec = 0;
1672 /* Prepare for the select - allow certain signals. */
1674 BlockSignals(False, SIGTERM);
1675 #if defined(SIGUSR1)
1676 BlockSignals(False, SIGUSR1);
1677 #endif /* SIGUSR1 */
1678 #if defined(SIGUSR2)
1679 BlockSignals(False, SIGUSR2);
1680 #endif /* SIGUSR2 */
1682 selrtn = sys_select(&fds,&timeout);
1684 /* We can only take signals when we are in the select - block them again here. */
1686 BlockSignals(True, SIGTERM);
1687 #if defined(SIGUSR1)
1688 BlockSignals(True, SIGUSR1);
1689 #endif /* SIGUSR1 */
1690 #if defined(SIGUSR2)
1691 BlockSignals(True, SIGUSR2);
1692 #endif /* SIGUSR2 */
1699 if (dns_fd != -1 && FD_ISSET(dns_fd,&fds)) {
1704 for(i = 0; i < listen_number; i++)
1706 if(i < (listen_number/2))
1708 /* Processing a 137 socket. */
1709 if (FD_ISSET(sock_array[i],&fds))
1711 struct packet_struct *packet = read_packet(sock_array[i], NMB_PACKET);
1715 * If we got a packet on the broadcast socket and interfaces
1716 * only is set then check it came from one of our local nets.
1718 if(lp_bind_interfaces_only() && (sock_array[i] == ClientNMB) &&
1719 (!is_local_net(packet->ip)))
1721 DEBUG(7,("discarding nmb packet sent to broadcast socket from %s:%d\n",
1722 inet_ntoa(packet->ip),packet->port));
1723 free_packet(packet);
1725 else if ((ip_equal(loopback_ip, packet->ip) ||
1726 ismyip(packet->ip)) && packet->port == global_nmb_port)
1728 DEBUG(7,("discarding own packet from %s:%d\n",
1729 inet_ntoa(packet->ip),packet->port));
1730 free_packet(packet);
1734 /* Save the file descriptor this packet came in on. */
1735 packet->fd = sock_array[i];
1736 queue_packet(packet);
1743 /* Processing a 138 socket. */
1745 if (FD_ISSET(sock_array[i],&fds))
1747 struct packet_struct *packet = read_packet(sock_array[i], DGRAM_PACKET);
1751 * If we got a packet on the broadcast socket and interfaces
1752 * only is set then check it came from one of our local nets.
1754 if(lp_bind_interfaces_only() && (sock_array[i] == ClientDGRAM) &&
1755 (!is_local_net(packet->ip)))
1757 DEBUG(7,("discarding dgram packet sent to broadcast socket from %s:%d\n",
1758 inet_ntoa(packet->ip),packet->port));
1759 free_packet(packet);
1761 else if ((ip_equal(loopback_ip, packet->ip) ||
1762 ismyip(packet->ip)) && packet->port == DGRAM_PORT)
1764 DEBUG(7,("discarding own packet from %s:%d\n",
1765 inet_ntoa(packet->ip),packet->port));
1766 free_packet(packet);
1770 /* Save the file descriptor this packet came in on. */
1771 packet->fd = sock_array[i];
1772 queue_packet(packet);
1776 } /* end processing 138 socket. */
1778 } /* end if selret > 0 */
1782 /****************************************************************************
1783 Construct and send a netbios DGRAM.
1784 Note that this currently sends all packets to port 138.
1785 **************************************************************************/
1787 BOOL send_mailslot(BOOL unique, char *mailslot,char *buf,int len,
1788 char *srcname, int src_type,
1789 char *dstname, int dest_type,
1790 struct in_addr dest_ip,struct in_addr src_ip)
1792 BOOL loopback_this_packet = False;
1793 struct packet_struct p;
1794 struct dgram_packet *dgram = &p.packet.dgram;
1798 bzero((char *)&p,sizeof(p));
1801 loopback_this_packet = True;
1803 generate_name_trn_id();
1805 /* DIRECT GROUP or UNIQUE datagram. */
1806 dgram->header.msg_type = unique ? 0x10 : 0x11;
1807 dgram->header.flags.node_type = M_NODE;
1808 dgram->header.flags.first = True;
1809 dgram->header.flags.more = False;
1810 dgram->header.dgm_id = name_trn_id;
1811 dgram->header.source_ip = src_ip;
1812 dgram->header.source_port = DGRAM_PORT;
1813 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
1814 dgram->header.packet_offset = 0;
1816 make_nmb_name(&dgram->source_name,srcname,src_type,scope);
1817 make_nmb_name(&dgram->dest_name,dstname,dest_type,scope);
1819 ptr = &dgram->data[0];
1821 /* Setup the smb part. */
1822 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
1824 set_message(ptr,17,17 + len,True);
1827 CVAL(ptr,smb_com) = SMBtrans;
1828 SSVAL(ptr,smb_vwv1,len);
1829 SSVAL(ptr,smb_vwv11,len);
1830 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
1831 SSVAL(ptr,smb_vwv13,3);
1832 SSVAL(ptr,smb_vwv14,1);
1833 SSVAL(ptr,smb_vwv15,1);
1834 SSVAL(ptr,smb_vwv16,2);
1836 strcpy(p2,mailslot);
1837 p2 = skip_string(p2,1);
1842 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
1845 p.port = DGRAM_PORT;
1846 p.fd = find_subnet_mailslot_fd_for_address( src_ip );
1847 p.timestamp = time(NULL);
1848 p.packet_type = DGRAM_PACKET;
1850 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
1851 namestr(&dgram->source_name), inet_ntoa(src_ip)));
1852 DEBUG(4,("to %s IP %s\n", namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
1854 debug_browse_data(buf, len);
1856 if(loopback_this_packet)
1858 struct packet_struct *lo_packet = NULL;
1859 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
1860 if((lo_packet = copy_packet(&p)) == NULL)
1862 queue_packet(lo_packet);
1866 return(send_packet(&p));