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 Get/Set problematic nb_flags as network byte order 16 bit int.
63 **************************************************************************/
65 uint16 get_nb_flags(char *buf)
67 return ((((uint16)*buf)&0xFFFF) & NB_FLGMSK);
70 void set_nb_flags(char *buf, uint16 nb_flags)
72 *buf++ = ((nb_flags & NB_FLGMSK) & 0xFF);
76 /***************************************************************************
77 Dumps out the browse packet data.
78 **************************************************************************/
80 static void debug_browse_data(char *outbuf, int len)
83 for (i = 0; i < len; i+= 16)
85 DEBUG(4, ("%3x char ", i));
87 for (j = 0; j < 16; j++)
89 unsigned char x = outbuf[i+j];
90 if (x < 32 || x > 127)
98 DEBUG(4, (" hex ", i));
100 for (j = 0; j < 16; j++)
104 DEBUG(4, (" %02x", (unsigned char)outbuf[i+j]));
111 /***************************************************************************
112 Generates the unique transaction identifier
113 **************************************************************************/
115 static uint16 name_trn_id=0;
117 static uint16 generate_name_trn_id(void)
122 name_trn_id = (time(NULL)%(unsigned)0x7FFF) + (getpid()%(unsigned)100);
124 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
128 /***************************************************************************
129 Either loops back or sends out a completed NetBIOS packet.
130 **************************************************************************/
132 static BOOL send_netbios_packet(struct packet_struct *p)
134 BOOL loopback_this_packet = False;
136 /* Check if we are sending to or from ourselves as a WINS server. */
137 if(ismyip(p->ip) && (p->port == global_nmb_port))
138 loopback_this_packet = True;
140 if(loopback_this_packet)
142 struct packet_struct *lo_packet = NULL;
143 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
144 if((lo_packet = copy_packet(p)) == NULL)
146 queue_packet(lo_packet);
148 else if (!send_packet(p))
150 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
151 inet_ntoa(p->ip),p->port));
158 /***************************************************************************
159 Sets up the common elements of an outgoing NetBIOS packet.
160 **************************************************************************/
162 static struct packet_struct *create_and_init_netbios_packet(struct nmb_name *nmbname,
164 struct in_addr to_ip)
166 struct packet_struct *packet = NULL;
167 struct nmb_packet *nmb = NULL;
169 /* Allocate the packet_struct we will return. */
170 if((packet = (struct packet_struct *)malloc(sizeof(*packet))) == NULL)
172 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
176 bzero((char *)packet,sizeof(*packet));
178 nmb = &packet->packet.nmb;
180 nmb->header.name_trn_id = generate_name_trn_id();
181 nmb->header.response = False;
182 nmb->header.nm_flags.recursion_desired = False;
183 nmb->header.nm_flags.recursion_available = False;
184 nmb->header.nm_flags.trunc = False;
185 nmb->header.nm_flags.authoritative = False;
186 nmb->header.nm_flags.bcast = bcast;
188 nmb->header.rcode = 0;
189 nmb->header.qdcount = 1;
190 nmb->header.ancount = 0;
191 nmb->header.nscount = 0;
193 nmb->question.question_name = *nmbname;
194 nmb->question.question_type = QUESTION_TYPE_NB_QUERY;
195 nmb->question.question_class = QUESTION_CLASS_IN;
198 packet->port = NMB_PORT;
199 packet->fd = ClientNMB;
200 packet->timestamp = time(NULL);
201 packet->packet_type = NMB_PACKET;
202 packet->locked = False;
204 return packet; /* Caller must free. */
207 /***************************************************************************
208 Sets up the common elements of register, refresh or release packet.
209 **************************************************************************/
211 static BOOL create_and_init_additional_record(struct packet_struct *packet,
213 struct in_addr *register_ip)
215 struct nmb_packet *nmb = &packet->packet.nmb;
217 if((nmb->additional = (struct res_rec *)malloc(sizeof(struct res_rec))) == NULL)
219 DEBUG(0,("initiate_name_register_packet: malloc fail for additional record.\n"));
223 bzero((char *)nmb->additional,sizeof(struct res_rec));
225 nmb->additional->rr_name = nmb->question.question_name;
226 nmb->additional->rr_type = RR_TYPE_NB;
227 nmb->additional->rr_class = RR_CLASS_IN;
229 nmb->additional->ttl = lp_max_ttl();
231 nmb->additional->rdlength = 6;
233 set_nb_flags(nmb->additional->rdata,nb_flags);
235 /* Set the address for the name we are registering. */
236 putip(&nmb->additional->rdata[2], register_ip);
238 /* Ensure that we send out the file descriptor to give us the
239 the specific source address we are registering as our
240 IP source address. */
242 packet->fd = find_subnet_fd_for_address( *register_ip );
247 /***************************************************************************
248 Sends out a name query.
249 **************************************************************************/
251 static BOOL initiate_name_query_packet( struct packet_struct *packet)
253 struct nmb_packet *nmb = NULL;
255 nmb = &packet->packet.nmb;
257 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
258 nmb->header.arcount = 0;
260 nmb->header.nm_flags.recursion_desired = True;
262 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
263 namestr(&nmb->question.question_name),
264 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
266 return send_netbios_packet( packet );
269 /***************************************************************************
270 Sends out a name register.
271 **************************************************************************/
273 static BOOL initiate_name_register_packet( struct packet_struct *packet,
274 uint16 nb_flags, struct in_addr *register_ip)
276 struct nmb_packet *nmb = &packet->packet.nmb;
278 nmb->header.opcode = NMB_NAME_REG_OPCODE;
279 nmb->header.arcount = 1;
281 nmb->header.nm_flags.recursion_desired = True;
283 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
286 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
287 namestr(&nmb->additional->rr_name),
288 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
290 return send_netbios_packet( packet );
293 /***************************************************************************
294 Sends out a multihomed name register.
295 **************************************************************************/
297 static BOOL initiate_multihomed_name_register_packet( struct packet_struct *packet,
298 uint16 nb_flags, struct in_addr *register_ip)
300 struct nmb_packet *nmb = &packet->packet.nmb;
301 char second_ip_buf[25];
303 strcpy(second_ip_buf, inet_ntoa(packet->ip));
305 nmb->header.opcode = NMB_NAME_MULTIHOMED_REG_OPCODE;
306 nmb->header.arcount = 1;
308 nmb->header.nm_flags.recursion_desired = True;
310 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
313 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
314 for name %s IP %s (bcast=%s) to IP %s\n",
315 namestr(&nmb->additional->rr_name), inet_ntoa(*register_ip),
316 BOOLSTR(nmb->header.nm_flags.bcast), second_ip_buf ));
318 return send_netbios_packet( packet );
321 /***************************************************************************
322 Sends out a name refresh.
323 **************************************************************************/
325 static BOOL initiate_name_refresh_packet( struct packet_struct *packet,
326 uint16 nb_flags, struct in_addr *refresh_ip)
328 struct nmb_packet *nmb = &packet->packet.nmb;
330 nmb->header.opcode = NMB_NAME_REFRESH_OPCODE_8;
331 nmb->header.arcount = 1;
333 nmb->header.nm_flags.recursion_desired = False;
335 if(create_and_init_additional_record(packet, nb_flags, refresh_ip) == False)
338 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
339 namestr(&nmb->additional->rr_name),
340 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
342 return send_netbios_packet( packet );
345 /***************************************************************************
346 Sends out a name release.
347 **************************************************************************/
349 static BOOL initiate_name_release_packet( struct packet_struct *packet,
350 uint16 nb_flags, struct in_addr *release_ip)
352 struct nmb_packet *nmb = &packet->packet.nmb;
354 nmb->header.opcode = NMB_NAME_RELEASE_OPCODE;
355 nmb->header.arcount = 1;
357 nmb->header.nm_flags.recursion_desired = False;
359 if(create_and_init_additional_record(packet, nb_flags, release_ip) == False)
362 DEBUG(4,("initiate_name_release_packet: sending release for name %s (bcast=%s) to IP %s\n",
363 namestr(&nmb->additional->rr_name),
364 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
366 return send_netbios_packet( packet );
369 /***************************************************************************
370 Sends out a node status.
371 **************************************************************************/
373 static BOOL initiate_node_status_packet( struct packet_struct *packet )
375 struct nmb_packet *nmb = &packet->packet.nmb;
377 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
378 nmb->header.arcount = 0;
380 nmb->header.nm_flags.recursion_desired = False;
382 nmb->question.question_type = QUESTION_TYPE_NB_STATUS;
384 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
385 namestr(&nmb->question.question_name),
386 inet_ntoa(packet->ip)));
388 return send_netbios_packet( packet );
391 /****************************************************************************
392 Simplification functions for queuing standard packets.
393 These should be the only publicly callable functions for sending
395 ****************************************************************************/
397 /****************************************************************************
398 Assertion - we should never be sending nmbd packets on the remote
400 ****************************************************************************/
402 static BOOL assert_check_subnet(struct subnet_record *subrec)
404 if( subrec == remote_broadcast_subnet)
406 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
413 /****************************************************************************
414 Queue a register name packet to the broadcast address of a subnet.
415 ****************************************************************************/
417 struct response_record *queue_register_name( struct subnet_record *subrec,
418 response_function resp_fn,
419 timeout_response_function timeout_fn,
420 register_name_success_function success_fn,
421 register_name_fail_function fail_fn,
422 struct userdata_struct *userdata,
423 struct nmb_name *nmbname,
426 struct packet_struct *p;
427 struct response_record *rrec;
428 BOOL bcast = (subrec == unicast_subnet) ? False : True;
430 if(assert_check_subnet(subrec))
433 if(( p = create_and_init_netbios_packet(nmbname, bcast,
434 subrec->bcast_ip)) == NULL)
437 if(initiate_name_register_packet( p, nb_flags,
438 iface_ip(subrec->bcast_ip)) == False)
445 if((rrec = make_response_record(subrec, /* subnet record. */
446 p, /* packet we sent. */
447 resp_fn, /* function to call on response. */
448 timeout_fn, /* function to call on timeout. */
449 (success_function)success_fn, /* function to call on operation success. */
450 (fail_function)fail_fn, /* function to call on operation fail. */
461 /****************************************************************************
462 Queue a multihomed register name packet to the broadcast address of a subnet.
463 ****************************************************************************/
465 struct response_record *queue_register_multihomed_name( struct subnet_record *subrec,
466 response_function resp_fn,
467 timeout_response_function timeout_fn,
468 register_name_success_function success_fn,
469 register_name_fail_function fail_fn,
470 struct userdata_struct *userdata,
471 struct nmb_name *nmbname,
473 struct in_addr register_ip)
475 struct packet_struct *p;
476 struct response_record *rrec;
481 if(subrec != unicast_subnet)
483 DEBUG(0,("queue_register_multihomed_name: should only be done on \
484 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
488 if(assert_check_subnet(subrec))
491 if(( p = create_and_init_netbios_packet(nmbname, bcast,
492 subrec->bcast_ip)) == NULL)
495 if (nb_flags & NB_GROUP)
496 ret = initiate_name_register_packet( p, nb_flags, ®ister_ip);
498 ret = initiate_multihomed_name_register_packet( p, nb_flags, ®ister_ip);
507 if((rrec = make_response_record(subrec, /* subnet record. */
508 p, /* packet we sent. */
509 resp_fn, /* function to call on response. */
510 timeout_fn, /* function to call on timeout. */
511 (success_function)success_fn, /* function to call on operation success. */
512 (fail_function)fail_fn, /* function to call on operation fail. */
523 /****************************************************************************
524 Queue a release name packet to the broadcast address of a subnet.
525 ****************************************************************************/
527 struct response_record *queue_release_name( struct subnet_record *subrec,
528 response_function resp_fn,
529 timeout_response_function timeout_fn,
530 release_name_success_function success_fn,
531 release_name_fail_function fail_fn,
532 struct userdata_struct *userdata,
533 struct nmb_name *nmbname,
535 struct in_addr release_ip)
537 BOOL bcast = (subrec == unicast_subnet) ? False : True;
538 struct packet_struct *p;
539 struct response_record *rrec;
541 if(assert_check_subnet(subrec))
544 if(( p = create_and_init_netbios_packet(nmbname, bcast,
545 subrec->bcast_ip)) == NULL)
548 if(initiate_name_release_packet( p, nb_flags, &release_ip) == False)
555 if((rrec = make_response_record(subrec, /* subnet record. */
556 p, /* packet we sent. */
557 resp_fn, /* function to call on response. */
558 timeout_fn, /* function to call on timeout. */
559 (success_function)success_fn, /* function to call on operation success. */
560 (fail_function)fail_fn, /* function to call on operation fail. */
571 /****************************************************************************
572 Queue a refresh name packet to the broadcast address of a subnet.
573 ****************************************************************************/
575 struct response_record *queue_refresh_name( struct subnet_record *subrec,
576 response_function resp_fn,
577 timeout_response_function timeout_fn,
578 refresh_name_success_function success_fn,
579 refresh_name_fail_function fail_fn,
580 struct userdata_struct *userdata,
581 struct name_record *namerec,
582 struct in_addr refresh_ip)
584 BOOL bcast = (subrec == unicast_subnet) ? False : True;
585 struct packet_struct *p;
586 struct response_record *rrec;
588 if(assert_check_subnet(subrec))
591 if(( p = create_and_init_netbios_packet(&namerec->name, bcast,
592 subrec->bcast_ip)) == NULL)
595 if(initiate_name_refresh_packet( p, namerec->nb_flags, &refresh_ip) == False)
602 if((rrec = make_response_record(subrec, /* subnet record. */
603 p, /* packet we sent. */
604 resp_fn, /* function to call on response. */
605 timeout_fn, /* function to call on timeout. */
606 (success_function)success_fn, /* function to call on operation success. */
607 (fail_function)fail_fn, /* function to call on operation fail. */
618 /****************************************************************************
619 Queue a query name packet to the broadcast address of a subnet.
620 ****************************************************************************/
622 struct response_record *queue_query_name( struct subnet_record *subrec,
623 response_function resp_fn,
624 timeout_response_function timeout_fn,
625 query_name_success_function success_fn,
626 query_name_fail_function fail_fn,
627 struct userdata_struct *userdata,
628 struct nmb_name *nmbname)
630 struct packet_struct *p;
631 struct response_record *rrec;
634 if ((subrec == unicast_subnet) || (subrec == wins_server_subnet))
637 if(assert_check_subnet(subrec))
640 if(( p = create_and_init_netbios_packet(nmbname, bcast,
641 subrec->bcast_ip)) == NULL)
644 if(initiate_name_query_packet( p ) == False)
651 if((rrec = make_response_record(subrec, /* subnet record. */
652 p, /* packet we sent. */
653 resp_fn, /* function to call on response. */
654 timeout_fn, /* function to call on timeout. */
655 (success_function)success_fn, /* function to call on operation success. */
656 (fail_function)fail_fn, /* function to call on operation fail. */
667 /****************************************************************************
668 Queue a node status packet to a given name and address.
669 ****************************************************************************/
671 struct response_record *queue_node_status( struct subnet_record *subrec,
672 response_function resp_fn,
673 timeout_response_function timeout_fn,
674 node_status_success_function success_fn,
675 node_status_fail_function fail_fn,
676 struct userdata_struct *userdata,
677 struct nmb_name *nmbname,
678 struct in_addr send_ip)
680 struct packet_struct *p;
681 struct response_record *rrec;
685 if(subrec != unicast_subnet)
687 DEBUG(0,("queue_register_multihomed_name: should only be done on \
688 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
692 if(assert_check_subnet(subrec))
695 if(( p = create_and_init_netbios_packet(nmbname, bcast,
699 if(initiate_node_status_packet(p) == False)
706 if((rrec = make_response_record(subrec, /* subnet record. */
707 p, /* packet we sent. */
708 resp_fn, /* function to call on response. */
709 timeout_fn, /* function to call on timeout. */
710 (success_function)success_fn, /* function to call on operation success. */
711 (fail_function)fail_fn, /* function to call on operation fail. */
722 /****************************************************************************
723 Reply to a netbios name packet. see rfc1002.txt
724 ****************************************************************************/
726 void reply_netbios_packet(struct packet_struct *orig_packet,
727 int rcode, enum netbios_reply_type_code rcv_code, int opcode,
728 int ttl, char *data,int len)
730 struct packet_struct packet;
731 struct nmb_packet *nmb = NULL;
732 struct res_rec answers;
733 struct nmb_packet *orig_nmb = &orig_packet->packet.nmb;
734 BOOL loopback_this_packet = False;
735 char *packet_type = "unknown";
737 /* Check if we are sending to or from ourselves. */
738 if(ismyip(orig_packet->ip) && (orig_packet->port == global_nmb_port))
739 loopback_this_packet = True;
741 nmb = &packet.packet.nmb;
743 /* Do a partial copy of the packet. We clear the locked flag and
744 the resource record pointers. */
745 packet = *orig_packet; /* Full structure copy. */
746 packet.locked = False;
749 nmb->additional = NULL;
755 packet_type = "nmb_status";
756 nmb->header.nm_flags.recursion_desired = False;
757 nmb->header.nm_flags.recursion_available = False;
762 packet_type = "nmb_query";
763 nmb->header.nm_flags.recursion_desired = True;
764 nmb->header.nm_flags.recursion_available = True;
768 case NMB_REG_REFRESH:
770 packet_type = "nmb_reg";
771 nmb->header.nm_flags.recursion_desired = True;
772 nmb->header.nm_flags.recursion_available = True;
777 packet_type = "nmb_rel";
778 nmb->header.nm_flags.recursion_desired = False;
779 nmb->header.nm_flags.recursion_available = False;
784 packet_type = "nmb_wack";
785 nmb->header.nm_flags.recursion_desired = False;
786 nmb->header.nm_flags.recursion_available = False;
791 packet_type = "wins_reg";
792 nmb->header.nm_flags.recursion_desired = True;
793 nmb->header.nm_flags.recursion_available = True;
798 packet_type = "wins_query";
799 nmb->header.nm_flags.recursion_desired = True;
800 nmb->header.nm_flags.recursion_available = True;
806 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
807 packet_type, namestr(&orig_nmb->question.question_name),
808 inet_ntoa(packet.ip)));
814 DEBUG(4,("reply_netbios_packet: sending a reply of packet type: %s %s to ip %s \
816 packet_type, namestr(&orig_nmb->question.question_name),
817 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
819 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
820 nmb->header.opcode = opcode;
821 nmb->header.response = True;
822 nmb->header.nm_flags.bcast = False;
823 nmb->header.nm_flags.trunc = False;
824 nmb->header.nm_flags.authoritative = True;
826 nmb->header.rcode = rcode;
827 nmb->header.qdcount = 0;
828 nmb->header.ancount = 1;
829 nmb->header.nscount = 0;
830 nmb->header.arcount = 0;
832 bzero((char*)&nmb->question,sizeof(nmb->question));
834 nmb->answers = &answers;
835 bzero((char*)nmb->answers,sizeof(*nmb->answers));
837 nmb->answers->rr_name = orig_nmb->question.question_name;
838 nmb->answers->rr_type = orig_nmb->question.question_type;
839 nmb->answers->rr_class = orig_nmb->question.question_class;
840 nmb->answers->ttl = ttl;
844 nmb->answers->rdlength = len;
845 memcpy(nmb->answers->rdata, data, len);
848 packet.packet_type = NMB_PACKET;
849 /* Ensure we send out on the same fd that the original
850 packet came in on to give the correct source IP address. */
851 packet.fd = orig_packet->fd;
852 packet.timestamp = time(NULL);
854 debug_nmb_packet(&packet);
856 if(loopback_this_packet)
858 struct packet_struct *lo_packet;
859 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
860 if((lo_packet = copy_packet(&packet)) == NULL)
862 queue_packet(lo_packet);
864 else if (!send_packet(&packet))
866 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
867 inet_ntoa(packet.ip),packet.port));
871 /*******************************************************************
872 Queue a packet into a packet queue
873 ******************************************************************/
875 void queue_packet(struct packet_struct *packet)
877 struct packet_struct *p;
883 packet_queue = packet;
887 /* find the bottom */
888 for (p=packet_queue;p->next;p=p->next)
896 /****************************************************************************
897 Try and find a matching subnet record for a datagram port 138 packet.
898 ****************************************************************************/
900 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
902 struct subnet_record *subrec;
904 /* Go through all the broadcast subnets and see if the mask matches. */
905 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
907 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
911 /* If the subnet record is the remote announce broadcast subnet,
912 hack it here to be the first subnet. This is really gross and
913 is needed due to people turning on port 137/138 broadcast
914 forwarding on their routers. May fire and brimstone rain
921 /****************************************************************************
922 Dispatch a browse frame from port 138 to the correct processing function.
923 ****************************************************************************/
925 void process_browse_packet(struct packet_struct *p, char *buf,int len)
927 struct dgram_packet *dgram = &p->packet.dgram;
928 int command = CVAL(buf,0);
929 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
931 /* Drop the packet if it's a different NetBIOS scope, or
932 the source is from one of our names. */
934 if (!strequal(dgram->dest_name.scope,scope ))
936 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
937 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
941 if (is_myname(dgram->source_name.name))
943 DEBUG(0,("process_browse_packet: Discarding datagram from IP %s. Source name \
944 %s is one of our names !\n", inet_ntoa(p->ip), namestr(&dgram->source_name)));
950 case ANN_HostAnnouncement:
952 debug_browse_data(buf, len);
953 process_host_announce(subrec, p, buf+1);
956 case ANN_DomainAnnouncement:
958 debug_browse_data(buf, len);
959 process_workgroup_announce(subrec, p, buf+1);
962 case ANN_LocalMasterAnnouncement:
964 debug_browse_data(buf, len);
965 process_local_master_announce(subrec, p, buf+1);
968 case ANN_AnnouncementRequest:
970 process_announce_request(subrec, p, buf+1);
975 process_election(subrec, p, buf+1);
978 case ANN_GetBackupListReq:
980 debug_browse_data(buf, len);
982 /* This is one occasion where we change a subnet that is
983 given to us. If the packet was sent to WORKGROUP<1b> instead
984 of WORKGROUP<1d> then it was unicast to us a domain master
985 browser. Change subrec to unicast.
987 if(dgram->dest_name.name_type == 0x1b)
988 subrec = unicast_subnet;
990 process_get_backup_list_request(subrec, p, buf+1);
993 case ANN_GetBackupListResp:
995 debug_browse_data(buf, len);
996 /* We never send ANN_GetBackupListReq so we
997 should never get these. */
998 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
999 packet from %s IP %s\n", namestr(&dgram->source_name), inet_ntoa(p->ip)));
1002 case ANN_ResetBrowserState:
1004 process_reset_browser(subrec, p, buf+1);
1007 case ANN_MasterAnnouncement:
1009 /* Master browser datagrams must be processed
1010 on the unicast subnet. */
1011 subrec = unicast_subnet;
1013 process_master_browser_announce(subrec, p, buf+1);
1018 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1019 command code %d from %s IP %s to %s\n",
1020 subrec->subnet_name, command, namestr(&dgram->source_name),
1021 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1026 /****************************************************************************
1027 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1028 ****************************************************************************/
1030 void process_lanman_packet(struct packet_struct *p, char *buf,int len)
1032 struct dgram_packet *dgram = &p->packet.dgram;
1033 int command = SVAL(buf,0);
1034 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1036 /* Drop the packet if it's a different NetBIOS scope, or
1037 the source is from one of our names. */
1039 if (!strequal(dgram->dest_name.scope,scope ))
1041 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1042 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
1046 if (is_myname(dgram->source_name.name))
1048 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1049 %s is one of our names !\n", inet_ntoa(p->ip), namestr(&dgram->source_name)));
1055 case ANN_HostAnnouncement:
1057 debug_browse_data(buf, len);
1058 process_lm_host_announce(subrec, p, buf+1);
1061 case ANN_AnnouncementRequest:
1063 process_lm_announce_request(subrec, p, buf+1);
1068 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1069 command code %d from %s IP %s to %s\n",
1070 subrec->subnet_name, command, namestr(&dgram->source_name),
1071 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1076 /****************************************************************************
1077 Determine if a packet is for us on port 138. Note that to have any chance of
1078 being efficient we need to drop as many packets as possible at this
1079 stage as subsequent processing is expensive.
1080 ****************************************************************************/
1082 static BOOL listening(struct packet_struct *p,struct nmb_name *nbname)
1084 struct subnet_record *subrec = NULL;
1086 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1088 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1093 subrec = unicast_subnet;
1095 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1098 /****************************************************************************
1099 Process udp 138 datagrams
1100 ****************************************************************************/
1102 static void process_dgram(struct packet_struct *p)
1107 struct dgram_packet *dgram = &p->packet.dgram;
1109 /* If we aren't listening to the destination name then ignore the packet */
1110 if (!listening(p,&dgram->dest_name))
1112 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1113 namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1117 if (dgram->header.msg_type != 0x10 &&
1118 dgram->header.msg_type != 0x11 &&
1119 dgram->header.msg_type != 0x12)
1121 /* Don't process error packets etc yet */
1122 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1123 an error packet of type %x\n",
1124 namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1128 buf = &dgram->data[0];
1129 buf -= 4; /* XXXX for the pseudo tcp length -
1130 someday I need to get rid of this */
1132 if (CVAL(buf,smb_com) != SMBtrans)
1135 len = SVAL(buf,smb_vwv11);
1136 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1138 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1139 namestr(&dgram->source_name),namestr(&dgram->dest_name),
1140 inet_ntoa(p->ip), smb_buf(buf),CVAL(buf2,0),len));
1146 /* Datagram packet received for the browser mailslot */
1147 if (strequal(smb_buf(buf),BROWSE_MAILSLOT))
1149 process_browse_packet(p,buf2,len);
1153 /* Datagram packet received for the LAN Manager mailslot */
1154 if (strequal(smb_buf(buf),LANMAN_MAILSLOT)) {
1155 process_lanman_packet(p,buf2,len);
1159 /* Datagram packet received for the domain logon mailslot */
1160 if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT))
1162 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1166 /* Datagram packet received for the NT domain logon mailslot */
1167 if (strequal(smb_buf(buf),NT_LOGON_MAILSLOT))
1169 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1174 /****************************************************************************
1175 Validate a response nmb packet.
1176 ****************************************************************************/
1178 BOOL validate_nmb_response_packet( struct nmb_packet *nmb )
1180 BOOL ignore = False;
1182 switch (nmb->header.opcode)
1184 case NMB_NAME_REG_OPCODE:
1185 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1186 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1187 if (nmb->header.ancount == 0)
1189 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1194 case NMB_NAME_QUERY_OPCODE:
1195 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1))
1197 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1201 case NMB_NAME_RELEASE_OPCODE:
1202 if (nmb->header.ancount == 0)
1204 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1208 case NMB_WACK_OPCODE:
1209 /* Check WACK response here. */
1210 if (nmb->header.ancount != 1)
1212 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1217 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1218 nmb->header.opcode));
1223 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1228 /****************************************************************************
1229 Validate a request nmb packet.
1230 ****************************************************************************/
1232 BOOL validate_nmb_packet( struct nmb_packet *nmb )
1234 BOOL ignore = False;
1236 switch (nmb->header.opcode)
1238 case NMB_NAME_REG_OPCODE:
1239 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1240 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1241 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1242 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1244 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1249 case NMB_NAME_QUERY_OPCODE:
1250 if ((nmb->header.qdcount == 0) ||
1251 ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1252 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS)))
1254 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1259 case NMB_NAME_RELEASE_OPCODE:
1260 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1262 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1267 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1268 nmb->header.opcode));
1273 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1278 /****************************************************************************
1279 Find a subnet (and potentially a response record) for a packet.
1280 ****************************************************************************/
1282 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1283 struct response_record **pprrec)
1285 struct nmb_packet *nmb = &p->packet.nmb;
1286 struct response_record *rrec = NULL;
1287 struct subnet_record *subrec = NULL;
1292 if(nmb->header.response)
1294 /* It's a response packet. Find a record for it or it's an error. */
1296 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1299 DEBUG(0,("find_subnet_for_nmb_packet: response record not found for response id %hu\n",
1300 nmb->header.name_trn_id));
1306 DEBUG(0,("find_subnet_for_nmb_packet: subnet record not found for response id %hu\n",
1307 nmb->header.name_trn_id));
1316 /* Try and see what subnet this packet belongs to. */
1319 if(packet_is_for_wins_server(p))
1320 return wins_server_subnet;
1322 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1323 if(nmb->header.nm_flags.bcast == False)
1324 return unicast_subnet;
1326 /* Go through all the broadcast subnets and see if the mask matches. */
1327 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1329 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1333 /* If none match it must have been a directed broadcast - assign
1334 the remote_broadcast_subnet. */
1335 return remote_broadcast_subnet;
1338 /****************************************************************************
1339 Process a nmb request packet - validate the packet and route it.
1340 ****************************************************************************/
1342 static void process_nmb_request(struct packet_struct *p)
1344 struct nmb_packet *nmb = &p->packet.nmb;
1345 struct subnet_record *subrec = NULL;
1347 debug_nmb_packet(p);
1349 /* Ensure we have a good packet. */
1350 if(validate_nmb_packet(nmb))
1353 /* Allocate a subnet to this packet - if we cannot - fail. */
1354 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1357 switch (nmb->header.opcode)
1359 case NMB_NAME_REG_OPCODE:
1360 if(subrec == wins_server_subnet)
1361 wins_process_name_registration_request(subrec, p);
1363 process_name_registration_request(subrec, p);
1366 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1367 case NMB_NAME_REFRESH_OPCODE_9:
1368 if(subrec == wins_server_subnet)
1369 wins_process_name_refresh_request(subrec, p);
1371 process_name_refresh_request(subrec, p);
1374 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1375 if(subrec == wins_server_subnet)
1376 wins_process_multihomed_name_registration_request(subrec, p);
1379 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1380 directed at a WINS server.\n"));
1384 case NMB_NAME_QUERY_OPCODE:
1385 switch (nmb->question.question_type)
1387 case QUESTION_TYPE_NB_QUERY:
1389 if(subrec == wins_server_subnet)
1390 wins_process_name_query_request(subrec, p);
1392 process_name_query_request(subrec, p);
1395 case QUESTION_TYPE_NB_STATUS:
1397 if(subrec == wins_server_subnet)
1399 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1404 process_node_status_request(subrec, p);
1410 case NMB_NAME_RELEASE_OPCODE:
1411 if(subrec == wins_server_subnet)
1412 wins_process_name_release_request(subrec, p);
1414 process_name_release_request(subrec, p);
1419 /****************************************************************************
1420 Process a nmb response packet - validate the packet and route it.
1421 to either the WINS server or a normal response.
1422 ****************************************************************************/
1424 static void process_nmb_response(struct packet_struct *p)
1426 struct nmb_packet *nmb = &p->packet.nmb;
1427 struct subnet_record *subrec = NULL;
1428 struct response_record *rrec = NULL;
1430 debug_nmb_packet(p);
1432 if(validate_nmb_response_packet(nmb))
1435 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1440 DEBUG(0,("process_nmb_response: response packet received but no response record \
1441 found for id = %hu. Ignoring packet.\n", nmb->header.name_trn_id));
1445 /* Increment the number of responses received for this record. */
1447 /* Ensure we don't re-send the request. */
1448 rrec->repeat_count = 0;
1450 /* Call the response received function for this packet. */
1451 (*rrec->resp_fn)(subrec, rrec, p);
1455 /*******************************************************************
1456 Run elements off the packet queue till its empty
1457 ******************************************************************/
1459 void run_packet_queue()
1461 struct packet_struct *p;
1463 while ((p = packet_queue))
1465 packet_queue = p->next;
1467 packet_queue->prev = NULL;
1468 p->next = p->prev = NULL;
1470 switch (p->packet_type)
1473 if(p->packet.nmb.header.response)
1474 process_nmb_response(p);
1476 process_nmb_request(p);
1487 /*******************************************************************
1488 Retransmit or timeout elements from all the outgoing subnet response
1489 record queues. NOTE that this code must also check the WINS server
1490 subnet for response records to timeout as the WINS server code
1491 can send requests to check if a client still owns a name.
1492 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1493 ******************************************************************/
1495 void retransmit_or_expire_response_records(time_t t)
1497 struct subnet_record *subrec;
1499 for (subrec = FIRST_SUBNET; subrec;
1500 subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec))
1502 struct response_record *rrec, *nextrrec;
1504 for (rrec = subrec->responselist; rrec; rrec = nextrrec)
1506 nextrrec = rrec->next;
1508 if (rrec->repeat_time <= t)
1510 if (rrec->repeat_count > 0)
1512 /* Resend while we have a non-zero repeat_count. */
1513 if(!send_packet(rrec->packet))
1515 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1516 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1517 subrec->subnet_name));
1519 rrec->repeat_time += rrec->repeat_interval;
1520 rrec->repeat_count--;
1524 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1525 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1526 subrec->subnet_name));
1528 /* Call the timeout function. This will deal with removing the
1529 timed out packet. */
1530 if(rrec->timeout_fn)
1531 (*rrec->timeout_fn)(subrec, rrec);
1534 /* We must remove the record ourself if there is
1535 no timeout function. */
1536 remove_response_record(subrec, rrec);
1538 } /* rrec->repeat_count > 0 */
1539 } /* rrec->repeat_time <= t */
1540 } /* end for rrec */
1541 } /* end for subnet */
1544 /****************************************************************************
1545 Create an fd_set containing all the sockets in the subnet structures,
1546 plus the broadcast sockets.
1547 ***************************************************************************/
1549 static BOOL create_listen_fdset(fd_set **ppset, int **psock_array, int *listen_number)
1551 int *sock_array = NULL;
1552 struct subnet_record *subrec = NULL;
1555 fd_set *pset = (fd_set *)malloc(sizeof(fd_set));
1559 DEBUG(0,("create_listen_fdset: malloc fail !\n"));
1563 /* Check that we can add all the fd's we need. */
1564 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1567 if((count*2) + 2 > FD_SETSIZE)
1569 DEBUG(0,("create_listen_fdset: Too many file descriptors needed (%d). We can \
1570 only use %d.\n", (count*2) + 2, FD_SETSIZE));
1574 if((sock_array = (int *)malloc(((count*2) + 2)*sizeof(int))) == NULL)
1576 DEBUG(0,("create_listen_fdset: malloc fail for socket array.\n"));
1582 /* Add in the broadcast socket on 137. */
1583 FD_SET(ClientNMB,pset);
1584 sock_array[num++] = ClientNMB;
1586 /* Add in the 137 sockets on all the interfaces. */
1587 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1589 FD_SET(subrec->nmb_sock,pset);
1590 sock_array[num++] = subrec->nmb_sock;
1593 /* Add in the broadcast socket on 138. */
1594 FD_SET(ClientDGRAM,pset);
1595 sock_array[num++] = ClientDGRAM;
1597 /* Add in the 138 sockets on all the interfaces. */
1598 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1600 FD_SET(subrec->dgram_sock,pset);
1601 sock_array[num++] = subrec->dgram_sock;
1604 *listen_number = (count*2) + 2;
1606 *psock_array = sock_array;
1611 /****************************************************************************
1612 Listens for NMB or DGRAM packets, and queues them.
1613 ***************************************************************************/
1615 BOOL listen_for_packets(BOOL run_election)
1617 static fd_set *listen_set = NULL;
1618 static int listen_number = 0;
1619 static int *sock_array = NULL;
1623 struct timeval timeout;
1628 if(listen_set == NULL)
1630 if(create_listen_fdset(&listen_set, &sock_array, &listen_number))
1632 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1637 memcpy((char *)&fds, (char *)listen_set, sizeof(fd_set));
1640 dns_fd = asyncdns_fd();
1642 FD_SET(dns_fd, &fds);
1648 * During elections and when expecting a netbios response packet we
1649 * need to send election packets at tighter intervals.
1650 * Ideally it needs to be the interval (in ms) between time now and
1651 * the time we are expecting the next netbios packet.
1654 timeout.tv_sec = (run_election||num_response_packets) ? 1 : NMBD_SELECT_LOOP;
1655 timeout.tv_usec = 0;
1657 /* Prepare for the select - allow certain signals. */
1659 BlockSignals(False, SIGTERM);
1660 #if defined(SIGUSR1)
1661 BlockSignals(False, SIGUSR1);
1662 #endif /* SIGUSR1 */
1663 #if defined(SIGUSR2)
1664 BlockSignals(False, SIGUSR2);
1665 #endif /* SIGUSR2 */
1667 selrtn = sys_select(&fds,&timeout);
1669 /* We can only take signals when we are in the select - block them again here. */
1671 BlockSignals(True, SIGTERM);
1672 #if defined(SIGUSR1)
1673 BlockSignals(True, SIGUSR1);
1674 #endif /* SIGUSR1 */
1675 #if defined(SIGUSR2)
1676 BlockSignals(True, SIGUSR2);
1677 #endif /* SIGUSR2 */
1684 if (dns_fd != -1 && FD_ISSET(dns_fd,&fds)) {
1689 for(i = 0; i < listen_number; i++)
1691 if(i < (listen_number/2))
1693 /* Processing a 137 socket. */
1694 if (FD_ISSET(sock_array[i],&fds))
1696 struct packet_struct *packet = read_packet(sock_array[i], NMB_PACKET);
1700 * If we got a packet on the broadcast socket and interfaces
1701 * only is set then check it came from one of our local nets.
1703 if(lp_bind_interfaces_only() && (sock_array[i] == ClientNMB) &&
1704 (!is_local_net(packet->ip)))
1706 DEBUG(7,("discarding nmb packet sent to broadcast socket from %s:%d\n",
1707 inet_ntoa(packet->ip),packet->port));
1708 free_packet(packet);
1710 else if ((ip_equal(loopback_ip, packet->ip) ||
1711 ismyip(packet->ip)) && packet->port == global_nmb_port)
1713 DEBUG(7,("discarding own packet from %s:%d\n",
1714 inet_ntoa(packet->ip),packet->port));
1715 free_packet(packet);
1719 /* Save the file descriptor this packet came in on. */
1720 packet->fd = sock_array[i];
1721 queue_packet(packet);
1728 /* Processing a 138 socket. */
1730 if (FD_ISSET(sock_array[i],&fds))
1732 struct packet_struct *packet = read_packet(sock_array[i], DGRAM_PACKET);
1736 * If we got a packet on the broadcast socket and interfaces
1737 * only is set then check it came from one of our local nets.
1739 if(lp_bind_interfaces_only() && (sock_array[i] == ClientDGRAM) &&
1740 (!is_local_net(packet->ip)))
1742 DEBUG(7,("discarding dgram packet sent to broadcast socket from %s:%d\n",
1743 inet_ntoa(packet->ip),packet->port));
1744 free_packet(packet);
1746 else if ((ip_equal(loopback_ip, packet->ip) ||
1747 ismyip(packet->ip)) && packet->port == DGRAM_PORT)
1749 DEBUG(7,("discarding own packet from %s:%d\n",
1750 inet_ntoa(packet->ip),packet->port));
1751 free_packet(packet);
1755 /* Save the file descriptor this packet came in on. */
1756 packet->fd = sock_array[i];
1757 queue_packet(packet);
1761 } /* end processing 138 socket. */
1763 } /* end if selret > 0 */
1767 /****************************************************************************
1768 Construct and send a netbios DGRAM.
1769 Note that this currently sends all packets to port 138.
1770 **************************************************************************/
1772 BOOL send_mailslot(BOOL unique, char *mailslot,char *buf,int len,
1773 char *srcname, int src_type,
1774 char *dstname, int dest_type,
1775 struct in_addr dest_ip,struct in_addr src_ip)
1777 BOOL loopback_this_packet = False;
1778 struct packet_struct p;
1779 struct dgram_packet *dgram = &p.packet.dgram;
1783 bzero((char *)&p,sizeof(p));
1786 loopback_this_packet = True;
1788 generate_name_trn_id();
1790 /* DIRECT GROUP or UNIQUE datagram. */
1791 dgram->header.msg_type = unique ? 0x10 : 0x11;
1792 dgram->header.flags.node_type = M_NODE;
1793 dgram->header.flags.first = True;
1794 dgram->header.flags.more = False;
1795 dgram->header.dgm_id = name_trn_id;
1796 dgram->header.source_ip = src_ip;
1797 dgram->header.source_port = DGRAM_PORT;
1798 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
1799 dgram->header.packet_offset = 0;
1801 make_nmb_name(&dgram->source_name,srcname,src_type,scope);
1802 make_nmb_name(&dgram->dest_name,dstname,dest_type,scope);
1804 ptr = &dgram->data[0];
1806 /* Setup the smb part. */
1807 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
1809 set_message(ptr,17,17 + len,True);
1812 CVAL(ptr,smb_com) = SMBtrans;
1813 SSVAL(ptr,smb_vwv1,len);
1814 SSVAL(ptr,smb_vwv11,len);
1815 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
1816 SSVAL(ptr,smb_vwv13,3);
1817 SSVAL(ptr,smb_vwv14,1);
1818 SSVAL(ptr,smb_vwv15,1);
1819 SSVAL(ptr,smb_vwv16,2);
1821 strcpy(p2,mailslot);
1822 p2 = skip_string(p2,1);
1827 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
1830 p.port = DGRAM_PORT;
1832 p.timestamp = time(NULL);
1833 p.packet_type = DGRAM_PACKET;
1835 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
1836 namestr(&dgram->source_name), inet_ntoa(src_ip)));
1837 DEBUG(4,("to %s IP %s\n", namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
1839 debug_browse_data(buf, len);
1841 if(loopback_this_packet)
1843 struct packet_struct *lo_packet = NULL;
1844 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
1845 if((lo_packet = copy_packet(&p)) == NULL)
1847 queue_packet(lo_packet);
1851 return(send_packet(&p));