2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 Copyright (C) Jeremy Allison 1994-2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 extern int ClientDGRAM;
27 extern int global_nmb_port;
29 extern int num_response_packets;
31 static void queue_packet(struct packet_struct *packet);
33 bool rescan_listen_set = False;
36 /*******************************************************************
37 The global packet linked-list. Incoming entries are
38 added to the end of this list. It is supposed to remain fairly
39 short so we won't bother with an end pointer.
40 ******************************************************************/
42 static struct packet_struct *packet_queue = NULL;
44 /***************************************************************************
45 Utility function to find the specific fd to send a packet out on.
46 **************************************************************************/
48 static int find_subnet_fd_for_address( struct in_addr local_ip )
50 struct subnet_record *subrec;
52 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
53 if(ip_equal(local_ip, subrec->myip))
54 return subrec->nmb_sock;
59 /***************************************************************************
60 Utility function to find the specific fd to send a mailslot packet out on.
61 **************************************************************************/
63 static int find_subnet_mailslot_fd_for_address( struct in_addr local_ip )
65 struct subnet_record *subrec;
67 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
68 if(ip_equal(local_ip, subrec->myip))
69 return subrec->dgram_sock;
74 /***************************************************************************
75 Get/Set problematic nb_flags as network byte order 16 bit int.
76 **************************************************************************/
78 uint16 get_nb_flags(char *buf)
80 return ((((uint16)*buf)&0xFFFF) & NB_FLGMSK);
83 void set_nb_flags(char *buf, uint16 nb_flags)
85 *buf++ = ((nb_flags & NB_FLGMSK) & 0xFF);
89 /***************************************************************************
90 Dumps out the browse packet data.
91 **************************************************************************/
93 static void debug_browse_data(char *outbuf, int len)
97 DEBUG( 4, ( "debug_browse_data():\n" ) );
98 for (i = 0; i < len; i+= 16) {
99 DEBUGADD( 4, ( "%3x char ", i ) );
101 for (j = 0; j < 16; j++) {
107 if (x < 32 || x > 127)
110 DEBUGADD( 4, ( "%c", x ) );
113 DEBUGADD( 4, ( "%*s hex", 16-j, "" ) );
115 for (j = 0; j < 16; j++) {
118 DEBUGADD( 4, ( " %02x", (unsigned char)outbuf[i+j] ) );
121 DEBUGADD( 4, ("\n") );
125 /***************************************************************************
126 Generates the unique transaction identifier
127 **************************************************************************/
129 static uint16 name_trn_id=0;
131 static uint16 generate_name_trn_id(void)
134 name_trn_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100);
136 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
140 /***************************************************************************
141 Either loops back or sends out a completed NetBIOS packet.
142 **************************************************************************/
144 static bool send_netbios_packet(struct packet_struct *p)
146 bool loopback_this_packet = False;
148 /* Check if we are sending to or from ourselves as a WINS server. */
149 if(ismyip_v4(p->ip) && (p->port == global_nmb_port))
150 loopback_this_packet = True;
152 if(loopback_this_packet) {
153 struct packet_struct *lo_packet = NULL;
154 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
155 if((lo_packet = copy_packet(p)) == NULL)
157 queue_packet(lo_packet);
158 } else if (!send_packet(p)) {
159 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
160 inet_ntoa(p->ip),p->port));
167 /***************************************************************************
168 Sets up the common elements of an outgoing NetBIOS packet.
170 Note: do not attempt to rationalise whether rec_des should be set or not
171 in a particular situation. Just follow rfc_1002 or look at examples from WinXX.
172 It does NOT follow the rule that requests to the wins server always have
173 rec_des true. See for example name releases and refreshes
174 **************************************************************************/
176 static struct packet_struct *create_and_init_netbios_packet(struct nmb_name *nmbname,
177 bool bcast, bool rec_des,
178 struct in_addr to_ip)
180 struct packet_struct *packet = NULL;
181 struct nmb_packet *nmb = NULL;
183 /* Allocate the packet_struct we will return. */
184 if((packet = SMB_MALLOC_P(struct packet_struct)) == NULL) {
185 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
189 memset((char *)packet,'\0',sizeof(*packet));
191 nmb = &packet->packet.nmb;
193 nmb->header.name_trn_id = generate_name_trn_id();
194 nmb->header.response = False;
195 nmb->header.nm_flags.recursion_desired = rec_des;
196 nmb->header.nm_flags.recursion_available = False;
197 nmb->header.nm_flags.trunc = False;
198 nmb->header.nm_flags.authoritative = False;
199 nmb->header.nm_flags.bcast = bcast;
201 nmb->header.rcode = 0;
202 nmb->header.qdcount = 1;
203 nmb->header.ancount = 0;
204 nmb->header.nscount = 0;
206 nmb->question.question_name = *nmbname;
207 nmb->question.question_type = QUESTION_TYPE_NB_QUERY;
208 nmb->question.question_class = QUESTION_CLASS_IN;
211 packet->port = NMB_PORT;
212 packet->fd = ClientNMB;
213 packet->timestamp = time(NULL);
214 packet->packet_type = NMB_PACKET;
215 packet->locked = False;
217 return packet; /* Caller must free. */
220 /***************************************************************************
221 Sets up the common elements of register, refresh or release packet.
222 **************************************************************************/
224 static bool create_and_init_additional_record(struct packet_struct *packet,
226 const struct in_addr *register_ip)
228 struct nmb_packet *nmb = &packet->packet.nmb;
230 if((nmb->additional = SMB_MALLOC_P(struct res_rec)) == NULL) {
231 DEBUG(0,("create_and_init_additional_record: malloc fail for additional record.\n"));
235 memset((char *)nmb->additional,'\0',sizeof(struct res_rec));
237 nmb->additional->rr_name = nmb->question.question_name;
238 nmb->additional->rr_type = RR_TYPE_NB;
239 nmb->additional->rr_class = RR_CLASS_IN;
241 /* See RFC 1002, sections 5.1.1.1, 5.1.1.2 and 5.1.1.3 */
242 if (nmb->header.nm_flags.bcast)
243 nmb->additional->ttl = PERMANENT_TTL;
245 nmb->additional->ttl = lp_max_ttl();
247 nmb->additional->rdlength = 6;
249 set_nb_flags(nmb->additional->rdata,nb_flags);
251 /* Set the address for the name we are registering. */
252 putip(&nmb->additional->rdata[2], register_ip);
255 it turns out that Jeremys code was correct, we are supposed
256 to send registrations from the IP we are registering. The
257 trick is what to do on timeouts! When we send on a
258 non-routable IP then the reply will timeout, and we should
259 treat this as success, not failure. That means we go into
260 our standard refresh cycle for that name which copes nicely
261 with disconnected networks.
263 packet->fd = find_subnet_fd_for_address(*register_ip);
268 /***************************************************************************
269 Sends out a name query.
270 **************************************************************************/
272 static bool initiate_name_query_packet( struct packet_struct *packet)
274 struct nmb_packet *nmb = NULL;
276 nmb = &packet->packet.nmb;
278 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
279 nmb->header.arcount = 0;
281 nmb->header.nm_flags.recursion_desired = True;
283 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
284 nmb_namestr(&nmb->question.question_name),
285 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
287 return send_netbios_packet( packet );
290 /***************************************************************************
291 Sends out a name query - from a WINS server.
292 **************************************************************************/
294 static bool initiate_name_query_packet_from_wins_server( struct packet_struct *packet)
296 struct nmb_packet *nmb = NULL;
298 nmb = &packet->packet.nmb;
300 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
301 nmb->header.arcount = 0;
303 nmb->header.nm_flags.recursion_desired = False;
305 DEBUG(4,("initiate_name_query_packet_from_wins_server: sending query for name %s (bcast=%s) to IP %s\n",
306 nmb_namestr(&nmb->question.question_name),
307 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
309 return send_netbios_packet( packet );
312 /***************************************************************************
313 Sends out a name register.
314 **************************************************************************/
316 static bool initiate_name_register_packet( struct packet_struct *packet,
317 uint16 nb_flags, const struct in_addr *register_ip)
319 struct nmb_packet *nmb = &packet->packet.nmb;
321 nmb->header.opcode = NMB_NAME_REG_OPCODE;
322 nmb->header.arcount = 1;
324 nmb->header.nm_flags.recursion_desired = True;
326 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
329 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
330 nmb_namestr(&nmb->additional->rr_name),
331 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
333 return send_netbios_packet( packet );
336 /***************************************************************************
337 Sends out a multihomed name register.
338 **************************************************************************/
340 static bool initiate_multihomed_name_register_packet(struct packet_struct *packet,
341 uint16 nb_flags, struct in_addr *register_ip)
343 struct nmb_packet *nmb = &packet->packet.nmb;
344 fstring second_ip_buf;
346 fstrcpy(second_ip_buf, inet_ntoa(packet->ip));
348 nmb->header.opcode = NMB_NAME_MULTIHOMED_REG_OPCODE;
349 nmb->header.arcount = 1;
351 nmb->header.nm_flags.recursion_desired = True;
353 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
356 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
357 for name %s IP %s (bcast=%s) to IP %s\n",
358 nmb_namestr(&nmb->additional->rr_name), inet_ntoa(*register_ip),
359 BOOLSTR(nmb->header.nm_flags.bcast), second_ip_buf ));
361 return send_netbios_packet( packet );
364 /***************************************************************************
365 Sends out a name refresh.
366 **************************************************************************/
368 static bool initiate_name_refresh_packet( struct packet_struct *packet,
369 uint16 nb_flags, struct in_addr *refresh_ip)
371 struct nmb_packet *nmb = &packet->packet.nmb;
373 nmb->header.opcode = NMB_NAME_REFRESH_OPCODE_8;
374 nmb->header.arcount = 1;
376 nmb->header.nm_flags.recursion_desired = False;
378 if(create_and_init_additional_record(packet, nb_flags, refresh_ip) == False)
381 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
382 nmb_namestr(&nmb->additional->rr_name),
383 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
385 return send_netbios_packet( packet );
388 /***************************************************************************
389 Sends out a name release.
390 **************************************************************************/
392 static bool initiate_name_release_packet( struct packet_struct *packet,
393 uint16 nb_flags, struct in_addr *release_ip)
395 struct nmb_packet *nmb = &packet->packet.nmb;
397 nmb->header.opcode = NMB_NAME_RELEASE_OPCODE;
398 nmb->header.arcount = 1;
400 nmb->header.nm_flags.recursion_desired = False;
402 if(create_and_init_additional_record(packet, nb_flags, release_ip) == False)
405 DEBUG(4,("initiate_name_release_packet: sending release for name %s (bcast=%s) to IP %s\n",
406 nmb_namestr(&nmb->additional->rr_name),
407 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
409 return send_netbios_packet( packet );
412 /***************************************************************************
413 Sends out a node status.
414 **************************************************************************/
416 static bool initiate_node_status_packet( struct packet_struct *packet )
418 struct nmb_packet *nmb = &packet->packet.nmb;
420 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
421 nmb->header.arcount = 0;
423 nmb->header.nm_flags.recursion_desired = False;
425 nmb->question.question_type = QUESTION_TYPE_NB_STATUS;
427 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
428 nmb_namestr(&nmb->question.question_name),
429 inet_ntoa(packet->ip)));
431 return send_netbios_packet( packet );
434 /****************************************************************************
435 Simplification functions for queuing standard packets.
436 These should be the only publicly callable functions for sending
438 ****************************************************************************/
440 /****************************************************************************
441 Assertion - we should never be sending nmbd packets on the remote
443 ****************************************************************************/
445 static bool assert_check_subnet(struct subnet_record *subrec)
447 if( subrec == remote_broadcast_subnet) {
448 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
455 /****************************************************************************
456 Queue a register name packet to the broadcast address of a subnet.
457 ****************************************************************************/
459 struct response_record *queue_register_name( struct subnet_record *subrec,
460 response_function resp_fn,
461 timeout_response_function timeout_fn,
462 register_name_success_function success_fn,
463 register_name_fail_function fail_fn,
464 struct userdata_struct *userdata,
465 struct nmb_name *nmbname,
468 struct packet_struct *p;
469 struct response_record *rrec;
470 struct sockaddr_storage ss;
471 const struct sockaddr_storage *pss = NULL;
472 if(assert_check_subnet(subrec))
475 /* note that all name registration requests have RD set (rfc1002 - section 4.2.2 */
476 if ((p = create_and_init_netbios_packet(nmbname, (subrec != unicast_subnet), True,
477 subrec->bcast_ip)) == NULL)
480 in_addr_to_sockaddr_storage(&ss, subrec->bcast_ip);
482 if (!pss || pss->ss_family != AF_INET) {
488 if(initiate_name_register_packet(p, nb_flags,
489 &((const struct sockaddr_in *)pss)->sin_addr) == False) {
495 if((rrec = make_response_record(subrec, /* subnet record. */
496 p, /* packet we sent. */
497 resp_fn, /* function to call on response. */
498 timeout_fn, /* function to call on timeout. */
499 (success_function)success_fn, /* function to call on operation success. */
500 (fail_function)fail_fn, /* function to call on operation fail. */
501 userdata)) == NULL) {
510 /****************************************************************************
511 Queue a refresh name packet to the broadcast address of a subnet.
512 ****************************************************************************/
514 void queue_wins_refresh(struct nmb_name *nmbname,
515 response_function resp_fn,
516 timeout_response_function timeout_fn,
518 struct in_addr refresh_ip,
521 struct packet_struct *p;
522 struct response_record *rrec;
523 struct in_addr wins_ip;
524 struct userdata_struct *userdata;
527 wins_ip = wins_srv_ip_tag(tag, refresh_ip);
529 if ((p = create_and_init_netbios_packet(nmbname, False, False, wins_ip)) == NULL) {
533 if (!initiate_name_refresh_packet(p, nb_flags, &refresh_ip)) {
539 fstrcpy(ip_str, inet_ntoa(refresh_ip));
541 DEBUG(6,("Refreshing name %s IP %s with WINS server %s using tag '%s'\n",
542 nmb_namestr(nmbname), ip_str, inet_ntoa(wins_ip), tag));
544 userdata = (struct userdata_struct *)SMB_MALLOC(sizeof(*userdata) + strlen(tag) + 1);
548 DEBUG(0,("Failed to allocate userdata structure!\n"));
551 ZERO_STRUCTP(userdata);
552 userdata->userdata_len = strlen(tag) + 1;
553 strlcpy(userdata->data, tag, userdata->userdata_len);
555 if ((rrec = make_response_record(unicast_subnet,
560 userdata)) == NULL) {
568 /* we don't want to repeat refresh packets */
569 rrec->repeat_count = 0;
573 /****************************************************************************
574 Queue a multihomed register name packet to a given WINS server IP
575 ****************************************************************************/
577 struct response_record *queue_register_multihomed_name( struct subnet_record *subrec,
578 response_function resp_fn,
579 timeout_response_function timeout_fn,
580 register_name_success_function success_fn,
581 register_name_fail_function fail_fn,
582 struct userdata_struct *userdata,
583 struct nmb_name *nmbname,
585 struct in_addr register_ip,
586 struct in_addr wins_ip)
588 struct packet_struct *p;
589 struct response_record *rrec;
593 if(subrec != unicast_subnet) {
594 DEBUG(0,("queue_register_multihomed_name: should only be done on \
595 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
599 if(assert_check_subnet(subrec))
602 if ((p = create_and_init_netbios_packet(nmbname, False, True, wins_ip)) == NULL)
605 if (nb_flags & NB_GROUP)
606 ret = initiate_name_register_packet( p, nb_flags, ®ister_ip);
608 ret = initiate_multihomed_name_register_packet(p, nb_flags, ®ister_ip);
616 if ((rrec = make_response_record(subrec, /* subnet record. */
617 p, /* packet we sent. */
618 resp_fn, /* function to call on response. */
619 timeout_fn, /* function to call on timeout. */
620 (success_function)success_fn, /* function to call on operation success. */
621 (fail_function)fail_fn, /* function to call on operation fail. */
622 userdata)) == NULL) {
631 /****************************************************************************
632 Queue a release name packet to the broadcast address of a subnet.
633 ****************************************************************************/
635 struct response_record *queue_release_name( struct subnet_record *subrec,
636 response_function resp_fn,
637 timeout_response_function timeout_fn,
638 release_name_success_function success_fn,
639 release_name_fail_function fail_fn,
640 struct userdata_struct *userdata,
641 struct nmb_name *nmbname,
643 struct in_addr release_ip,
644 struct in_addr dest_ip)
646 struct packet_struct *p;
647 struct response_record *rrec;
649 if(assert_check_subnet(subrec))
652 if ((p = create_and_init_netbios_packet(nmbname, (subrec != unicast_subnet), False, dest_ip)) == NULL)
655 if(initiate_name_release_packet( p, nb_flags, &release_ip) == False) {
661 if((rrec = make_response_record(subrec, /* subnet record. */
662 p, /* packet we sent. */
663 resp_fn, /* function to call on response. */
664 timeout_fn, /* function to call on timeout. */
665 (success_function)success_fn, /* function to call on operation success. */
666 (fail_function)fail_fn, /* function to call on operation fail. */
667 userdata)) == NULL) {
674 * For a broadcast release packet, only send once.
675 * This will cause us to remove the name asap. JRA.
678 if (subrec != unicast_subnet) {
679 rrec->repeat_count = 0;
680 rrec->repeat_time = 0;
686 /****************************************************************************
687 Queue a query name packet to the broadcast address of a subnet.
688 ****************************************************************************/
690 struct response_record *queue_query_name( struct subnet_record *subrec,
691 response_function resp_fn,
692 timeout_response_function timeout_fn,
693 query_name_success_function success_fn,
694 query_name_fail_function fail_fn,
695 struct userdata_struct *userdata,
696 struct nmb_name *nmbname)
698 struct packet_struct *p;
699 struct response_record *rrec;
700 struct in_addr to_ip;
702 if(assert_check_subnet(subrec))
705 to_ip = subrec->bcast_ip;
707 /* queries to the WINS server turn up here as queries to IP 0.0.0.0
708 These need to be handled a bit differently */
709 if (subrec->type == UNICAST_SUBNET && is_zero_ip_v4(to_ip)) {
710 /* What we really need to do is loop over each of our wins
711 * servers and wins server tags here, but that just doesn't
712 * fit our architecture at the moment (userdata may already
713 * be used when we get here). For now we just query the first
714 * active wins server on the first tag.
716 char **tags = wins_srv_tags();
720 to_ip = wins_srv_ip_tag(tags[0], to_ip);
721 wins_srv_tags_free(tags);
724 if(( p = create_and_init_netbios_packet(nmbname,
725 (subrec != unicast_subnet),
726 (subrec == unicast_subnet),
730 if(lp_bind_interfaces_only()) {
733 DEBUG(10,("queue_query_name: bind_interfaces_only is set, looking for suitable source IP\n"));
734 for(i = 0; i < iface_count(); i++) {
735 const struct in_addr *ifip = iface_n_ip_v4(i);
738 DEBUG(0,("queue_query_name: interface %d has NULL IP address !\n", i));
742 if (is_loopback_ip_v4(*ifip)) {
743 DEBUG(5,("queue_query_name: ignoring loopback interface (%d)\n", i));
747 DEBUG(10,("queue_query_name: using source IP %s\n",inet_ntoa(*ifip)));
748 p->fd = find_subnet_fd_for_address( *ifip );
753 if(initiate_name_query_packet( p ) == False) {
759 if((rrec = make_response_record(subrec, /* subnet record. */
760 p, /* packet we sent. */
761 resp_fn, /* function to call on response. */
762 timeout_fn, /* function to call on timeout. */
763 (success_function)success_fn, /* function to call on operation success. */
764 (fail_function)fail_fn, /* function to call on operation fail. */
765 userdata)) == NULL) {
774 /****************************************************************************
775 Queue a query name packet to a given address from the WINS subnet.
776 ****************************************************************************/
778 struct response_record *queue_query_name_from_wins_server( struct in_addr to_ip,
779 response_function resp_fn,
780 timeout_response_function timeout_fn,
781 query_name_success_function success_fn,
782 query_name_fail_function fail_fn,
783 struct userdata_struct *userdata,
784 struct nmb_name *nmbname)
786 struct packet_struct *p;
787 struct response_record *rrec;
789 if ((p = create_and_init_netbios_packet(nmbname, False, False, to_ip)) == NULL)
792 if(initiate_name_query_packet_from_wins_server( p ) == False) {
798 if((rrec = make_response_record(wins_server_subnet, /* subnet record. */
799 p, /* packet we sent. */
800 resp_fn, /* function to call on response. */
801 timeout_fn, /* function to call on timeout. */
802 (success_function)success_fn, /* function to call on operation success. */
803 (fail_function)fail_fn, /* function to call on operation fail. */
804 userdata)) == NULL) {
813 /****************************************************************************
814 Queue a node status packet to a given name and address.
815 ****************************************************************************/
817 struct response_record *queue_node_status( struct subnet_record *subrec,
818 response_function resp_fn,
819 timeout_response_function timeout_fn,
820 node_status_success_function success_fn,
821 node_status_fail_function fail_fn,
822 struct userdata_struct *userdata,
823 struct nmb_name *nmbname,
824 struct in_addr send_ip)
826 struct packet_struct *p;
827 struct response_record *rrec;
830 if(subrec != unicast_subnet) {
831 DEBUG(0,("queue_register_multihomed_name: should only be done on \
832 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
836 if(assert_check_subnet(subrec))
839 if(( p = create_and_init_netbios_packet(nmbname, False, False, send_ip)) == NULL)
842 if(initiate_node_status_packet(p) == False) {
848 if((rrec = make_response_record(subrec, /* subnet record. */
849 p, /* packet we sent. */
850 resp_fn, /* function to call on response. */
851 timeout_fn, /* function to call on timeout. */
852 (success_function)success_fn, /* function to call on operation success. */
853 (fail_function)fail_fn, /* function to call on operation fail. */
854 userdata)) == NULL) {
863 /****************************************************************************
864 Reply to a netbios name packet. see rfc1002.txt
865 ****************************************************************************/
867 void reply_netbios_packet(struct packet_struct *orig_packet,
868 int rcode, enum netbios_reply_type_code rcv_code, int opcode,
869 int ttl, char *data,int len)
871 struct packet_struct packet;
872 struct nmb_packet *nmb = NULL;
873 struct res_rec answers;
874 struct nmb_packet *orig_nmb = &orig_packet->packet.nmb;
875 bool loopback_this_packet = False;
876 int rr_type = RR_TYPE_NB;
877 const char *packet_type = "unknown";
879 /* Check if we are sending to or from ourselves. */
880 if(ismyip_v4(orig_packet->ip) && (orig_packet->port == global_nmb_port))
881 loopback_this_packet = True;
883 nmb = &packet.packet.nmb;
885 /* Do a partial copy of the packet. We clear the locked flag and
886 the resource record pointers. */
887 packet = *orig_packet; /* Full structure copy. */
888 packet.locked = False;
891 nmb->additional = NULL;
895 packet_type = "nmb_status";
896 nmb->header.nm_flags.recursion_desired = False;
897 nmb->header.nm_flags.recursion_available = False;
898 rr_type = RR_TYPE_NBSTAT;
901 packet_type = "nmb_query";
902 nmb->header.nm_flags.recursion_desired = True;
903 nmb->header.nm_flags.recursion_available = True;
905 rr_type = RR_TYPE_NULL;
909 case NMB_REG_REFRESH:
910 packet_type = "nmb_reg";
911 nmb->header.nm_flags.recursion_desired = True;
912 nmb->header.nm_flags.recursion_available = True;
915 packet_type = "nmb_rel";
916 nmb->header.nm_flags.recursion_desired = False;
917 nmb->header.nm_flags.recursion_available = False;
920 packet_type = "nmb_wack";
921 nmb->header.nm_flags.recursion_desired = False;
922 nmb->header.nm_flags.recursion_available = False;
923 rr_type = RR_TYPE_NULL;
926 packet_type = "wins_reg";
927 nmb->header.nm_flags.recursion_desired = True;
928 nmb->header.nm_flags.recursion_available = True;
931 packet_type = "wins_query";
932 nmb->header.nm_flags.recursion_desired = True;
933 nmb->header.nm_flags.recursion_available = True;
935 rr_type = RR_TYPE_NULL;
939 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
940 packet_type, nmb_namestr(&orig_nmb->question.question_name),
941 inet_ntoa(packet.ip)));
945 DEBUG(4,("reply_netbios_packet: sending a reply of packet type: %s %s to ip %s \
946 for id %hu\n", packet_type, nmb_namestr(&orig_nmb->question.question_name),
947 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
949 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
950 nmb->header.opcode = opcode;
951 nmb->header.response = True;
952 nmb->header.nm_flags.bcast = False;
953 nmb->header.nm_flags.trunc = False;
954 nmb->header.nm_flags.authoritative = True;
956 nmb->header.rcode = rcode;
957 nmb->header.qdcount = 0;
958 nmb->header.ancount = 1;
959 nmb->header.nscount = 0;
960 nmb->header.arcount = 0;
962 memset((char*)&nmb->question,'\0',sizeof(nmb->question));
964 nmb->answers = &answers;
965 memset((char*)nmb->answers,'\0',sizeof(*nmb->answers));
967 nmb->answers->rr_name = orig_nmb->question.question_name;
968 nmb->answers->rr_type = rr_type;
969 nmb->answers->rr_class = RR_CLASS_IN;
970 nmb->answers->ttl = ttl;
973 nmb->answers->rdlength = len;
974 memcpy(nmb->answers->rdata, data, len);
977 packet.packet_type = NMB_PACKET;
978 /* Ensure we send out on the same fd that the original
979 packet came in on to give the correct source IP address. */
980 packet.fd = orig_packet->fd;
981 packet.timestamp = time(NULL);
983 debug_nmb_packet(&packet);
985 if(loopback_this_packet) {
986 struct packet_struct *lo_packet;
987 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
988 if((lo_packet = copy_packet(&packet)) == NULL)
990 queue_packet(lo_packet);
991 } else if (!send_packet(&packet)) {
992 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
993 inet_ntoa(packet.ip),packet.port));
997 /*******************************************************************
998 Queue a packet into a packet queue
999 ******************************************************************/
1001 static void queue_packet(struct packet_struct *packet)
1003 struct packet_struct *p;
1005 if (!packet_queue) {
1006 packet->prev = NULL;
1007 packet->next = NULL;
1008 packet_queue = packet;
1012 /* find the bottom */
1013 for (p=packet_queue;p->next;p=p->next)
1017 packet->next = NULL;
1021 /****************************************************************************
1022 Try and find a matching subnet record for a datagram port 138 packet.
1023 ****************************************************************************/
1025 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
1027 struct subnet_record *subrec;
1029 /* Go through all the broadcast subnets and see if the mask matches. */
1030 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1031 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1035 /* If the subnet record is the remote announce broadcast subnet,
1036 hack it here to be the first subnet. This is really gross and
1037 is needed due to people turning on port 137/138 broadcast
1038 forwarding on their routers. May fire and brimstone rain
1042 return FIRST_SUBNET;
1045 /****************************************************************************
1046 Dispatch a browse frame from port 138 to the correct processing function.
1047 ****************************************************************************/
1049 static void process_browse_packet(struct packet_struct *p, char *buf,int len)
1051 struct dgram_packet *dgram = &p->packet.dgram;
1052 int command = CVAL(buf,0);
1053 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1057 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1058 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1059 if (!strequal(scope, global_scope())) {
1060 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
1061 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, global_scope()));
1065 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1066 if (is_myname(src_name)) {
1067 DEBUG(0,("process_browse_packet: Discarding datagram from IP %s. Source name \
1068 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1073 case ANN_HostAnnouncement:
1074 debug_browse_data(buf, len);
1075 process_host_announce(subrec, p, buf+1);
1077 case ANN_DomainAnnouncement:
1078 debug_browse_data(buf, len);
1079 process_workgroup_announce(subrec, p, buf+1);
1081 case ANN_LocalMasterAnnouncement:
1082 debug_browse_data(buf, len);
1083 process_local_master_announce(subrec, p, buf+1);
1085 case ANN_AnnouncementRequest:
1086 debug_browse_data(buf, len);
1087 process_announce_request(subrec, p, buf+1);
1090 debug_browse_data(buf, len);
1091 process_election(subrec, p, buf+1);
1093 case ANN_GetBackupListReq:
1094 debug_browse_data(buf, len);
1095 process_get_backup_list_request(subrec, p, buf+1);
1097 case ANN_GetBackupListResp:
1098 debug_browse_data(buf, len);
1099 /* We never send ANN_GetBackupListReq so we should never get these. */
1100 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1101 packet from %s IP %s\n", nmb_namestr(&dgram->source_name), inet_ntoa(p->ip)));
1103 case ANN_ResetBrowserState:
1104 debug_browse_data(buf, len);
1105 process_reset_browser(subrec, p, buf+1);
1107 case ANN_MasterAnnouncement:
1108 /* Master browser datagrams must be processed on the unicast subnet. */
1109 subrec = unicast_subnet;
1111 debug_browse_data(buf, len);
1112 process_master_browser_announce(subrec, p, buf+1);
1114 case ANN_BecomeBackup:
1116 * We don't currently implement this. Log it just in case.
1118 debug_browse_data(buf, len);
1119 DEBUG(10,("process_browse_packet: On subnet %s ignoring browse packet \
1120 command ANN_BecomeBackup from %s IP %s to %s\n", subrec->subnet_name, nmb_namestr(&dgram->source_name),
1121 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1124 debug_browse_data(buf, len);
1125 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1126 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1127 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1132 /****************************************************************************
1133 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1134 ****************************************************************************/
1136 static void process_lanman_packet(struct packet_struct *p, char *buf,int len)
1138 struct dgram_packet *dgram = &p->packet.dgram;
1139 int command = SVAL(buf,0);
1140 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1144 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1146 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1147 if (!strequal(scope, global_scope())) {
1148 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1149 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, global_scope()));
1153 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1154 if (is_myname(src_name)) {
1155 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1156 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1161 case ANN_HostAnnouncement:
1162 debug_browse_data(buf, len);
1163 process_lm_host_announce(subrec, p, buf+1, len > 1 ? len-1 : 0);
1165 case ANN_AnnouncementRequest:
1166 process_lm_announce_request(subrec, p, buf+1, len > 1 ? len-1 : 0);
1169 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1170 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1171 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1176 /****************************************************************************
1177 Determine if a packet is for us on port 138. Note that to have any chance of
1178 being efficient we need to drop as many packets as possible at this
1179 stage as subsequent processing is expensive.
1180 ****************************************************************************/
1182 static bool listening(struct packet_struct *p,struct nmb_name *nbname)
1184 struct subnet_record *subrec = NULL;
1186 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1187 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1192 subrec = unicast_subnet;
1194 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1197 /****************************************************************************
1198 Process udp 138 datagrams
1199 ****************************************************************************/
1201 static void process_dgram(struct packet_struct *p)
1206 struct dgram_packet *dgram = &p->packet.dgram;
1208 /* If we aren't listening to the destination name then ignore the packet */
1209 if (!listening(p,&dgram->dest_name)) {
1210 unexpected_packet(p);
1211 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1212 nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1216 if (dgram->header.msg_type != 0x10 && dgram->header.msg_type != 0x11 && dgram->header.msg_type != 0x12) {
1217 unexpected_packet(p);
1218 /* Don't process error packets etc yet */
1219 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1220 an error packet of type %x\n", nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1224 /* Ensure we have a large enough packet before looking inside. */
1225 if (dgram->datasize < (smb_vwv12 - 2)) {
1226 /* That's the offset minus the 4 byte length + 2 bytes of offset. */
1227 DEBUG(0,("process_dgram: ignoring too short dgram packet (%u) sent to name %s from IP %s\n",
1228 (unsigned int)dgram->datasize,
1229 nmb_namestr(&dgram->dest_name),
1230 inet_ntoa(p->ip) ));
1234 buf = &dgram->data[0];
1235 buf -= 4; /* XXXX for the pseudo tcp length - someday I need to get rid of this */
1237 if (CVAL(buf,smb_com) != SMBtrans)
1240 len = SVAL(buf,smb_vwv11);
1241 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1243 if (len <= 0 || len > dgram->datasize) {
1244 DEBUG(0,("process_dgram: ignoring malformed1 (datasize = %d, len = %d) datagram \
1245 packet sent to name %s from IP %s\n",
1248 nmb_namestr(&dgram->dest_name),
1249 inet_ntoa(p->ip) ));
1253 if (buf2 < dgram->data || (buf2 >= dgram->data + dgram->datasize)) {
1254 DEBUG(0,("process_dgram: ignoring malformed2 (datasize = %d, len=%d, off=%d) datagram \
1255 packet sent to name %s from IP %s\n",
1258 (int)PTR_DIFF(buf2, dgram->data),
1259 nmb_namestr(&dgram->dest_name),
1260 inet_ntoa(p->ip) ));
1264 if ((buf2 + len < dgram->data) || (buf2 + len > dgram->data + dgram->datasize)) {
1265 DEBUG(0,("process_dgram: ignoring malformed3 (datasize = %d, len=%d, off=%d) datagram \
1266 packet sent to name %s from IP %s\n",
1269 (int)PTR_DIFF(buf2, dgram->data),
1270 nmb_namestr(&dgram->dest_name),
1271 inet_ntoa(p->ip) ));
1275 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1276 nmb_namestr(&dgram->source_name),nmb_namestr(&dgram->dest_name),
1277 inet_ntoa(p->ip), smb_buf(buf),CVAL(buf2,0),len));
1279 /* Datagram packet received for the browser mailslot */
1280 if (strequal(smb_buf(buf),BROWSE_MAILSLOT)) {
1281 process_browse_packet(p,buf2,len);
1285 /* Datagram packet received for the LAN Manager mailslot */
1286 if (strequal(smb_buf(buf),LANMAN_MAILSLOT)) {
1287 process_lanman_packet(p,buf2,len);
1291 /* Datagram packet received for the domain logon mailslot */
1292 if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT)) {
1293 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1297 /* Datagram packet received for the NT domain logon mailslot */
1298 if (strequal(smb_buf(buf),NT_LOGON_MAILSLOT)) {
1299 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1303 unexpected_packet(p);
1306 /****************************************************************************
1307 Validate a response nmb packet.
1308 ****************************************************************************/
1310 static bool validate_nmb_response_packet( struct nmb_packet *nmb )
1312 bool ignore = False;
1314 switch (nmb->header.opcode) {
1315 case NMB_NAME_REG_OPCODE:
1316 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1317 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1318 if (nmb->header.ancount == 0) {
1319 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1324 case NMB_NAME_QUERY_OPCODE:
1325 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1)) {
1326 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1331 case NMB_NAME_RELEASE_OPCODE:
1332 if (nmb->header.ancount == 0) {
1333 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1338 case NMB_WACK_OPCODE:
1339 /* Check WACK response here. */
1340 if (nmb->header.ancount != 1) {
1341 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1346 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1347 nmb->header.opcode));
1352 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1357 /****************************************************************************
1358 Validate a request nmb packet.
1359 ****************************************************************************/
1361 static bool validate_nmb_packet( struct nmb_packet *nmb )
1363 bool ignore = False;
1365 switch (nmb->header.opcode) {
1366 case NMB_NAME_REG_OPCODE:
1367 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1368 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1369 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1370 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1371 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1376 case NMB_NAME_QUERY_OPCODE:
1377 if ((nmb->header.qdcount == 0) || ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1378 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS))) {
1379 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1384 case NMB_NAME_RELEASE_OPCODE:
1385 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1386 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1391 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1392 nmb->header.opcode));
1397 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1402 /****************************************************************************
1403 Find a subnet (and potentially a response record) for a packet.
1404 ****************************************************************************/
1406 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1407 struct response_record **pprrec)
1409 struct nmb_packet *nmb = &p->packet.nmb;
1410 struct response_record *rrec = NULL;
1411 struct subnet_record *subrec = NULL;
1416 if(nmb->header.response) {
1417 /* It's a response packet. Find a record for it or it's an error. */
1419 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1421 DEBUG(3,("find_subnet_for_nmb_packet: response record not found for response id %hu\n",
1422 nmb->header.name_trn_id));
1423 unexpected_packet(p);
1427 if(subrec == NULL) {
1428 DEBUG(0,("find_subnet_for_nmb_packet: subnet record not found for response id %hu\n",
1429 nmb->header.name_trn_id));
1438 /* Try and see what subnet this packet belongs to. */
1441 if(packet_is_for_wins_server(p))
1442 return wins_server_subnet;
1444 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1445 if(nmb->header.nm_flags.bcast == False)
1446 return unicast_subnet;
1448 /* Go through all the broadcast subnets and see if the mask matches. */
1449 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1450 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1454 /* If none match it must have been a directed broadcast - assign the remote_broadcast_subnet. */
1455 return remote_broadcast_subnet;
1458 /****************************************************************************
1459 Process a nmb request packet - validate the packet and route it.
1460 ****************************************************************************/
1462 static void process_nmb_request(struct packet_struct *p)
1464 struct nmb_packet *nmb = &p->packet.nmb;
1465 struct subnet_record *subrec = NULL;
1467 debug_nmb_packet(p);
1469 /* Ensure we have a good packet. */
1470 if(validate_nmb_packet(nmb))
1473 /* Allocate a subnet to this packet - if we cannot - fail. */
1474 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1477 switch (nmb->header.opcode) {
1478 case NMB_NAME_REG_OPCODE:
1479 if(subrec == wins_server_subnet)
1480 wins_process_name_registration_request(subrec, p);
1482 process_name_registration_request(subrec, p);
1485 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1486 case NMB_NAME_REFRESH_OPCODE_9:
1487 if(subrec == wins_server_subnet)
1488 wins_process_name_refresh_request(subrec, p);
1490 process_name_refresh_request(subrec, p);
1493 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1494 if(subrec == wins_server_subnet) {
1495 wins_process_multihomed_name_registration_request(subrec, p);
1497 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1498 directed at a WINS server.\n"));
1502 case NMB_NAME_QUERY_OPCODE:
1503 switch (nmb->question.question_type) {
1504 case QUESTION_TYPE_NB_QUERY:
1505 if(subrec == wins_server_subnet)
1506 wins_process_name_query_request(subrec, p);
1508 process_name_query_request(subrec, p);
1510 case QUESTION_TYPE_NB_STATUS:
1511 if(subrec == wins_server_subnet) {
1512 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1516 process_node_status_request(subrec, p);
1522 case NMB_NAME_RELEASE_OPCODE:
1523 if(subrec == wins_server_subnet)
1524 wins_process_name_release_request(subrec, p);
1526 process_name_release_request(subrec, p);
1531 /****************************************************************************
1532 Process a nmb response packet - validate the packet and route it.
1533 to either the WINS server or a normal response.
1534 ****************************************************************************/
1536 static void process_nmb_response(struct packet_struct *p)
1538 struct nmb_packet *nmb = &p->packet.nmb;
1539 struct subnet_record *subrec = NULL;
1540 struct response_record *rrec = NULL;
1542 debug_nmb_packet(p);
1544 if(validate_nmb_response_packet(nmb))
1547 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1551 DEBUG(0,("process_nmb_response: response packet received but no response record \
1552 found for id = %hu. Ignoring packet.\n", nmb->header.name_trn_id));
1556 /* Increment the number of responses received for this record. */
1558 /* Ensure we don't re-send the request. */
1559 rrec->repeat_count = 0;
1561 /* Call the response received function for this packet. */
1562 (*rrec->resp_fn)(subrec, rrec, p);
1565 /*******************************************************************
1566 Run elements off the packet queue till its empty
1567 ******************************************************************/
1569 void run_packet_queue(void)
1571 struct packet_struct *p;
1573 while ((p = packet_queue)) {
1574 packet_queue = p->next;
1576 packet_queue->prev = NULL;
1577 p->next = p->prev = NULL;
1579 switch (p->packet_type) {
1581 if(p->packet.nmb.header.response)
1582 process_nmb_response(p);
1584 process_nmb_request(p);
1595 /*******************************************************************
1596 Retransmit or timeout elements from all the outgoing subnet response
1597 record queues. NOTE that this code must also check the WINS server
1598 subnet for response records to timeout as the WINS server code
1599 can send requests to check if a client still owns a name.
1600 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1601 ******************************************************************/
1603 void retransmit_or_expire_response_records(time_t t)
1605 struct subnet_record *subrec;
1607 for (subrec = FIRST_SUBNET; subrec; subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec)) {
1608 struct response_record *rrec, *nextrrec;
1610 for (rrec = subrec->responselist; rrec; rrec = nextrrec) {
1611 nextrrec = rrec->next;
1613 if (rrec->repeat_time <= t) {
1614 if (rrec->repeat_count > 0) {
1615 /* Resend while we have a non-zero repeat_count. */
1616 if(!send_packet(rrec->packet)) {
1617 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1618 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1620 rrec->repeat_time = t + rrec->repeat_interval;
1621 rrec->repeat_count--;
1623 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1624 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1627 * Check the flag in this record to prevent recursion if we end
1628 * up in this function again via the timeout function call.
1631 if(!rrec->in_expiration_processing) {
1634 * Set the recursion protection flag in this record.
1637 rrec->in_expiration_processing = True;
1639 /* Call the timeout function. This will deal with removing the
1640 timed out packet. */
1641 if(rrec->timeout_fn) {
1642 (*rrec->timeout_fn)(subrec, rrec);
1644 /* We must remove the record ourself if there is
1645 no timeout function. */
1646 remove_response_record(subrec, rrec);
1648 } /* !rrec->in_expitation_processing */
1649 } /* rrec->repeat_count > 0 */
1650 } /* rrec->repeat_time <= t */
1651 } /* end for rrec */
1652 } /* end for subnet */
1655 /****************************************************************************
1656 Create an fd_set containing all the sockets in the subnet structures,
1657 plus the broadcast sockets.
1658 ***************************************************************************/
1660 static bool create_listen_fdset(fd_set **ppset, int **psock_array, int *listen_number, int *maxfd)
1662 int *sock_array = NULL;
1663 struct subnet_record *subrec = NULL;
1666 fd_set *pset = SMB_MALLOC_P(fd_set);
1669 DEBUG(0,("create_listen_fdset: malloc fail !\n"));
1673 /* Check that we can add all the fd's we need. */
1674 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1677 if((count*2) + 2 > FD_SETSIZE) {
1678 DEBUG(0,("create_listen_fdset: Too many file descriptors needed (%d). We can \
1679 only use %d.\n", (count*2) + 2, FD_SETSIZE));
1684 if((sock_array = SMB_MALLOC_ARRAY(int, (count*2) + 2)) == NULL) {
1685 DEBUG(0,("create_listen_fdset: malloc fail for socket array.\n"));
1692 /* Add in the broadcast socket on 137. */
1693 FD_SET(ClientNMB,pset);
1694 sock_array[num++] = ClientNMB;
1695 *maxfd = MAX( *maxfd, ClientNMB);
1697 /* Add in the 137 sockets on all the interfaces. */
1698 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1699 FD_SET(subrec->nmb_sock,pset);
1700 sock_array[num++] = subrec->nmb_sock;
1701 *maxfd = MAX( *maxfd, subrec->nmb_sock);
1704 /* Add in the broadcast socket on 138. */
1705 FD_SET(ClientDGRAM,pset);
1706 sock_array[num++] = ClientDGRAM;
1707 *maxfd = MAX( *maxfd, ClientDGRAM);
1709 /* Add in the 138 sockets on all the interfaces. */
1710 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1711 FD_SET(subrec->dgram_sock,pset);
1712 sock_array[num++] = subrec->dgram_sock;
1713 *maxfd = MAX( *maxfd, subrec->dgram_sock);
1716 *listen_number = (count*2) + 2;
1719 SAFE_FREE(*psock_array);
1722 *psock_array = sock_array;
1727 /****************************************************************************
1728 Listens for NMB or DGRAM packets, and queues them.
1729 return True if the socket is dead
1730 ***************************************************************************/
1732 bool listen_for_packets(bool run_election)
1734 static fd_set *listen_set = NULL;
1735 static int listen_number = 0;
1736 static int *sock_array = NULL;
1738 static int maxfd = 0;
1743 struct timeval timeout;
1748 if(listen_set == NULL || rescan_listen_set) {
1749 if(create_listen_fdset(&listen_set, &sock_array, &listen_number, &maxfd)) {
1750 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1753 rescan_listen_set = False;
1756 memcpy((char *)&r_fds, (char *)listen_set, sizeof(fd_set));
1760 dns_fd = asyncdns_fd();
1762 FD_SET(dns_fd, &r_fds);
1763 maxfd = MAX( maxfd, dns_fd);
1768 * During elections and when expecting a netbios response packet we
1769 * need to send election packets at tighter intervals.
1770 * Ideally it needs to be the interval (in ms) between time now and
1771 * the time we are expecting the next netbios packet.
1774 timeout.tv_sec = (run_election||num_response_packets) ? 1 : NMBD_SELECT_LOOP;
1775 timeout.tv_usec = 0;
1778 struct timeval now = timeval_current();
1779 event_add_to_select_args(nmbd_event_context(), &now,
1780 &r_fds, &w_fds, &timeout, &maxfd);
1783 if (timeval_is_zero(&timeout)) {
1784 /* Process a timed event now... */
1785 if (run_events(nmbd_event_context(), 0, NULL, NULL)) {
1790 /* Prepare for the select - allow certain signals. */
1792 BlockSignals(False, SIGTERM);
1794 selrtn = sys_select(maxfd+1,&r_fds,&w_fds,NULL,&timeout);
1796 /* We can only take signals when we are in the select - block them again here. */
1798 BlockSignals(True, SIGTERM);
1804 if (run_events(nmbd_event_context(), selrtn, &r_fds, &w_fds)) {
1809 if (dns_fd != -1 && FD_ISSET(dns_fd,&r_fds)) {
1814 for(i = 0; i < listen_number; i++) {
1815 if (i < (listen_number/2)) {
1816 /* Processing a 137 socket. */
1817 if (FD_ISSET(sock_array[i],&r_fds)) {
1818 struct packet_struct *packet = read_packet(sock_array[i], NMB_PACKET);
1821 * If we got a packet on the broadcast socket and interfaces
1822 * only is set then check it came from one of our local nets.
1824 if(lp_bind_interfaces_only() && (sock_array[i] == ClientNMB) &&
1825 (!is_local_net_v4(packet->ip))) {
1826 DEBUG(7,("discarding nmb packet sent to broadcast socket from %s:%d\n",
1827 inet_ntoa(packet->ip),packet->port));
1828 free_packet(packet);
1829 } else if ((is_loopback_ip_v4(packet->ip) ||
1830 ismyip_v4(packet->ip)) && packet->port == global_nmb_port &&
1831 packet->packet.nmb.header.nm_flags.bcast) {
1832 DEBUG(7,("discarding own bcast packet from %s:%d\n",
1833 inet_ntoa(packet->ip),packet->port));
1834 free_packet(packet);
1836 /* Save the file descriptor this packet came in on. */
1837 packet->fd = sock_array[i];
1838 queue_packet(packet);
1843 /* Processing a 138 socket. */
1844 if (FD_ISSET(sock_array[i],&r_fds)) {
1845 struct packet_struct *packet = read_packet(sock_array[i], DGRAM_PACKET);
1848 * If we got a packet on the broadcast socket and interfaces
1849 * only is set then check it came from one of our local nets.
1851 if(lp_bind_interfaces_only() && (sock_array[i] == ClientDGRAM) &&
1852 (!is_local_net_v4(packet->ip))) {
1853 DEBUG(7,("discarding dgram packet sent to broadcast socket from %s:%d\n",
1854 inet_ntoa(packet->ip),packet->port));
1855 free_packet(packet);
1856 } else if ((is_loopback_ip_v4(packet->ip) ||
1857 ismyip_v4(packet->ip)) && packet->port == DGRAM_PORT) {
1858 DEBUG(7,("discarding own dgram packet from %s:%d\n",
1859 inet_ntoa(packet->ip),packet->port));
1860 free_packet(packet);
1862 /* Save the file descriptor this packet came in on. */
1863 packet->fd = sock_array[i];
1864 queue_packet(packet);
1868 } /* end processing 138 socket. */
1873 /****************************************************************************
1874 Construct and send a netbios DGRAM.
1875 **************************************************************************/
1877 bool send_mailslot(bool unique, const char *mailslot,char *buf, size_t len,
1878 const char *srcname, int src_type,
1879 const char *dstname, int dest_type,
1880 struct in_addr dest_ip,struct in_addr src_ip,
1883 bool loopback_this_packet = False;
1884 struct packet_struct p;
1885 struct dgram_packet *dgram = &p.packet.dgram;
1889 memset((char *)&p,'\0',sizeof(p));
1891 if(ismyip_v4(dest_ip) && (dest_port == DGRAM_PORT)) /* Only if to DGRAM_PORT */
1892 loopback_this_packet = True;
1894 /* generate_name_trn_id(); */ /* Not used, so gone, RJS */
1896 /* DIRECT GROUP or UNIQUE datagram. */
1897 dgram->header.msg_type = unique ? 0x10 : 0x11;
1898 dgram->header.flags.node_type = M_NODE;
1899 dgram->header.flags.first = True;
1900 dgram->header.flags.more = False;
1901 dgram->header.dgm_id = generate_name_trn_id();
1902 dgram->header.source_ip = src_ip;
1903 dgram->header.source_port = DGRAM_PORT;
1904 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
1905 dgram->header.packet_offset = 0;
1907 make_nmb_name(&dgram->source_name,srcname,src_type);
1908 make_nmb_name(&dgram->dest_name,dstname,dest_type);
1910 ptr = &dgram->data[0];
1912 /* Setup the smb part. */
1913 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
1915 set_message(ptr,17,strlen(mailslot) + 1 + len,True);
1918 SCVAL(ptr,smb_com,SMBtrans);
1919 SSVAL(ptr,smb_vwv1,len);
1920 SSVAL(ptr,smb_vwv11,len);
1921 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
1922 SSVAL(ptr,smb_vwv13,3);
1923 SSVAL(ptr,smb_vwv14,1);
1924 SSVAL(ptr,smb_vwv15,1);
1925 SSVAL(ptr,smb_vwv16,2);
1927 safe_strcpy_base(p2, mailslot, dgram->data, sizeof(dgram->data));
1928 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
1930 if (((p2+len) > dgram->data+sizeof(dgram->data)) || ((p2+len) < p2)) {
1931 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
1938 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
1942 p.fd = find_subnet_mailslot_fd_for_address( src_ip );
1943 p.timestamp = time(NULL);
1944 p.packet_type = DGRAM_PACKET;
1946 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
1947 nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
1948 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
1950 debug_browse_data(buf, len);
1952 if(loopback_this_packet) {
1953 struct packet_struct *lo_packet = NULL;
1954 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
1955 if((lo_packet = copy_packet(&p)) == NULL)
1957 queue_packet(lo_packet);
1960 return(send_packet(&p));