2 Unix SMB/CIFS implementation.
3 NBT netbios library routines
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 int num_good_sends
= 0;
25 int num_good_receives
= 0;
27 static const struct opcode_names
{
28 const char *nmb_opcode_name
;
30 } nmb_header_opcode_names
[] = {
36 {"Refresh(altcode)", 9 },
37 {"Multi-homed Registration", 15 },
41 /****************************************************************************
42 Lookup a nmb opcode name.
43 ****************************************************************************/
45 static const char *lookup_opcode_name( int opcode
)
47 const struct opcode_names
*op_namep
;
50 for(i
= 0; nmb_header_opcode_names
[i
].nmb_opcode_name
!= 0; i
++) {
51 op_namep
= &nmb_header_opcode_names
[i
];
52 if(opcode
== op_namep
->opcode
)
53 return op_namep
->nmb_opcode_name
;
55 return "<unknown opcode>";
58 /****************************************************************************
59 Print out a res_rec structure.
60 ****************************************************************************/
62 static void debug_nmb_res_rec(struct res_rec
*res
, const char *hdr
)
66 DEBUGADD( 4, ( " %s: nmb_name=%s rr_type=%d rr_class=%d ttl=%d\n",
68 nmb_namestr(&res
->rr_name
),
73 if( res
->rdlength
== 0 || res
->rdata
== NULL
)
76 for (i
= 0; i
< res
->rdlength
; i
+= MAX_NETBIOSNAME_LEN
) {
77 DEBUGADD(4, (" %s %3x char ", hdr
, i
));
79 for (j
= 0; j
< MAX_NETBIOSNAME_LEN
; j
++) {
80 unsigned char x
= res
->rdata
[i
+j
];
81 if (x
< 32 || x
> 127)
84 if (i
+j
>= res
->rdlength
)
86 DEBUGADD(4, ("%c", x
));
89 DEBUGADD(4, (" hex "));
91 for (j
= 0; j
< MAX_NETBIOSNAME_LEN
; j
++) {
92 if (i
+j
>= res
->rdlength
)
94 DEBUGADD(4, ("%02X", (unsigned char)res
->rdata
[i
+j
]));
101 /****************************************************************************
102 Process a nmb packet.
103 ****************************************************************************/
105 void debug_nmb_packet(struct packet_struct
*p
)
107 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
109 if( DEBUGLVL( 4 ) ) {
110 dbgtext( "nmb packet from %s(%d) header: id=%d opcode=%s(%d) response=%s\n",
111 inet_ntoa(p
->ip
), p
->port
,
112 nmb
->header
.name_trn_id
,
113 lookup_opcode_name(nmb
->header
.opcode
),
115 BOOLSTR(nmb
->header
.response
) );
116 dbgtext( " header: flags: bcast=%s rec_avail=%s rec_des=%s trunc=%s auth=%s\n",
117 BOOLSTR(nmb
->header
.nm_flags
.bcast
),
118 BOOLSTR(nmb
->header
.nm_flags
.recursion_available
),
119 BOOLSTR(nmb
->header
.nm_flags
.recursion_desired
),
120 BOOLSTR(nmb
->header
.nm_flags
.trunc
),
121 BOOLSTR(nmb
->header
.nm_flags
.authoritative
) );
122 dbgtext( " header: rcode=%d qdcount=%d ancount=%d nscount=%d arcount=%d\n",
127 nmb
->header
.arcount
);
130 if (nmb
->header
.qdcount
) {
131 DEBUGADD( 4, ( " question: q_name=%s q_type=%d q_class=%d\n",
132 nmb_namestr(&nmb
->question
.question_name
),
133 nmb
->question
.question_type
,
134 nmb
->question
.question_class
) );
137 if (nmb
->answers
&& nmb
->header
.ancount
) {
138 debug_nmb_res_rec(nmb
->answers
,"answers");
140 if (nmb
->nsrecs
&& nmb
->header
.nscount
) {
141 debug_nmb_res_rec(nmb
->nsrecs
,"nsrecs");
143 if (nmb
->additional
&& nmb
->header
.arcount
) {
144 debug_nmb_res_rec(nmb
->additional
,"additional");
148 /*******************************************************************
149 Handle "compressed" name pointers.
150 ******************************************************************/
152 static BOOL
handle_name_ptrs(unsigned char *ubuf
,int *offset
,int length
,
153 BOOL
*got_pointer
,int *ret
)
157 while ((ubuf
[*offset
] & 0xC0) == 0xC0) {
161 (*offset
) = ((ubuf
[*offset
] & ~0xC0)<<8) | ubuf
[(*offset
)+1];
162 if (loop_count
++ == 10 || (*offset
) < 0 || (*offset
)>(length
-2)) {
169 /*******************************************************************
170 Parse a nmb name from "compressed" format to something readable
171 return the space taken by the name, or 0 if the name is invalid
172 ******************************************************************/
174 static int parse_nmb_name(char *inbuf
,int ofs
,int length
, struct nmb_name
*name
)
177 unsigned char *ubuf
= (unsigned char *)inbuf
;
179 BOOL got_pointer
=False
;
183 if (length
- offset
< 2)
186 /* handle initial name pointers */
187 if (!handle_name_ptrs(ubuf
,&offset
,length
,&got_pointer
,&ret
))
194 if ((m
& 0xC0) || offset
+m
+2 > length
)
197 memset((char *)name
,'\0',sizeof(*name
));
199 /* the "compressed" part */
205 c1
= ubuf
[offset
++]-'A';
206 c2
= ubuf
[offset
++]-'A';
207 if ((c1
& 0xF0) || (c2
& 0xF0) || (n
> sizeof(name
->name
)-1))
209 name
->name
[n
++] = (c1
<<4) | c2
;
214 if (n
==MAX_NETBIOSNAME_LEN
) {
215 /* parse out the name type, its always in the 16th byte of the name */
216 name
->name_type
= ((unsigned char)name
->name
[15]) & 0xff;
218 /* remove trailing spaces */
221 while (n
&& name
->name
[n
]==' ')
225 /* now the domain parts (if any) */
227 while (ubuf
[offset
]) {
228 /* we can have pointers within the domain part as well */
229 if (!handle_name_ptrs(ubuf
,&offset
,length
,&got_pointer
,&ret
))
234 * Don't allow null domain parts.
241 name
->scope
[n
++] = '.';
242 if (m
+2+offset
>length
|| n
+m
+1>sizeof(name
->scope
))
246 name
->scope
[n
++] = (char)ubuf
[offset
++];
249 * Watch for malicious loops.
251 if (loop_count
++ == 10)
254 name
->scope
[n
++] = 0;
259 /****************************************************************************
260 Put a netbios name, padding(s) and a name type into a 16 character buffer.
261 name is already in DOS charset.
262 [15 bytes name + padding][1 byte name type].
263 ****************************************************************************/
265 static void put_name(char *dest
, const char *name
, int pad
, unsigned int name_type
)
267 size_t len
= strlen(name
);
269 memcpy(dest
, name
, (len
< MAX_NETBIOSNAME_LEN
) ? len
: MAX_NETBIOSNAME_LEN
- 1);
270 if (len
< MAX_NETBIOSNAME_LEN
- 1) {
271 memset(dest
+ len
, pad
, MAX_NETBIOSNAME_LEN
- 1 - len
);
273 dest
[MAX_NETBIOSNAME_LEN
- 1] = name_type
;
276 /*******************************************************************
277 Put a compressed nmb name into a buffer. Return the length of the
280 Compressed names are really weird. The "compression" doubles the
281 size. The idea is that it also means that compressed names conform
282 to the doman name system. See RFC1002.
283 ******************************************************************/
285 static int put_nmb_name(char *buf
,int offset
,struct nmb_name
*name
)
291 if (strcmp(name
->name
,"*") == 0) {
292 /* special case for wildcard name */
293 put_name(buf1
, "*", '\0', name
->name_type
);
295 put_name(buf1
, name
->name
, ' ', name
->name_type
);
302 for (m
=0;m
<MAX_NETBIOSNAME_LEN
;m
++) {
303 buf
[offset
+1+2*m
] = 'A' + ((buf1
[m
]>>4)&0xF);
304 buf
[offset
+2+2*m
] = 'A' + (buf1
[m
]&0xF);
310 if (name
->scope
[0]) {
311 /* XXXX this scope handling needs testing */
312 ret
+= strlen(name
->scope
) + 1;
313 safe_strcpy(&buf
[offset
+1],name
->scope
,sizeof(name
->scope
));
316 while ((p
= strchr_m(p
,'.'))) {
317 buf
[offset
] = PTR_DIFF(p
,&buf
[offset
+1]);
318 offset
+= (buf
[offset
] + 1);
321 buf
[offset
] = strlen(&buf
[offset
+1]);
327 /*******************************************************************
328 Useful for debugging messages.
329 ******************************************************************/
331 char *nmb_namestr(struct nmb_name
*n
)
334 static fstring ret
[4];
338 pull_ascii_fstring(name
, n
->name
);
340 slprintf(p
,sizeof(fstring
)-1, "%s<%02x>",name
,n
->name_type
);
342 slprintf(p
,sizeof(fstring
)-1, "%s<%02x>.%s",name
,n
->name_type
,n
->scope
);
348 /*******************************************************************
349 Allocate and parse some resource records.
350 ******************************************************************/
352 static BOOL
parse_alloc_res_rec(char *inbuf
,int *offset
,int length
,
353 struct res_rec
**recs
, int count
)
357 *recs
= (struct res_rec
*)malloc(sizeof(**recs
)*count
);
361 memset((char *)*recs
,'\0',sizeof(**recs
)*count
);
363 for (i
=0;i
<count
;i
++) {
364 int l
= parse_nmb_name(inbuf
,*offset
,length
,&(*recs
)[i
].rr_name
);
366 if (!l
|| (*offset
)+10 > length
) {
370 (*recs
)[i
].rr_type
= RSVAL(inbuf
,(*offset
));
371 (*recs
)[i
].rr_class
= RSVAL(inbuf
,(*offset
)+2);
372 (*recs
)[i
].ttl
= RIVAL(inbuf
,(*offset
)+4);
373 (*recs
)[i
].rdlength
= RSVAL(inbuf
,(*offset
)+8);
375 if ((*recs
)[i
].rdlength
>sizeof((*recs
)[i
].rdata
) ||
376 (*offset
)+(*recs
)[i
].rdlength
> length
) {
380 memcpy((*recs
)[i
].rdata
,inbuf
+(*offset
),(*recs
)[i
].rdlength
);
381 (*offset
) += (*recs
)[i
].rdlength
;
386 /*******************************************************************
387 Put a resource record into a packet.
388 ******************************************************************/
390 static int put_res_rec(char *buf
,int offset
,struct res_rec
*recs
,int count
)
395 for (i
=0;i
<count
;i
++) {
396 int l
= put_nmb_name(buf
,offset
,&recs
[i
].rr_name
);
399 RSSVAL(buf
,offset
,recs
[i
].rr_type
);
400 RSSVAL(buf
,offset
+2,recs
[i
].rr_class
);
401 RSIVAL(buf
,offset
+4,recs
[i
].ttl
);
402 RSSVAL(buf
,offset
+8,recs
[i
].rdlength
);
403 memcpy(buf
+offset
+10,recs
[i
].rdata
,recs
[i
].rdlength
);
404 offset
+= 10+recs
[i
].rdlength
;
405 ret
+= 10+recs
[i
].rdlength
;
411 /*******************************************************************
412 Put a compressed name pointer record into a packet.
413 ******************************************************************/
415 static int put_compressed_name_ptr(unsigned char *buf
,int offset
,struct res_rec
*rec
,int ptr_offset
)
418 buf
[offset
] = (0xC0 | ((ptr_offset
>> 8) & 0xFF));
419 buf
[offset
+1] = (ptr_offset
& 0xFF);
422 RSSVAL(buf
,offset
,rec
->rr_type
);
423 RSSVAL(buf
,offset
+2,rec
->rr_class
);
424 RSIVAL(buf
,offset
+4,rec
->ttl
);
425 RSSVAL(buf
,offset
+8,rec
->rdlength
);
426 memcpy(buf
+offset
+10,rec
->rdata
,rec
->rdlength
);
427 offset
+= 10+rec
->rdlength
;
428 ret
+= 10+rec
->rdlength
;
433 /*******************************************************************
434 Parse a dgram packet. Return False if the packet can't be parsed
435 or is invalid for some reason, True otherwise.
437 This is documented in section 4.4.1 of RFC1002.
438 ******************************************************************/
440 static BOOL
parse_dgram(char *inbuf
,int length
,struct dgram_packet
*dgram
)
445 memset((char *)dgram
,'\0',sizeof(*dgram
));
450 dgram
->header
.msg_type
= CVAL(inbuf
,0);
451 flags
= CVAL(inbuf
,1);
452 dgram
->header
.flags
.node_type
= (enum node_type
)((flags
>>2)&3);
454 dgram
->header
.flags
.more
= True
;
456 dgram
->header
.flags
.first
= True
;
457 dgram
->header
.dgm_id
= RSVAL(inbuf
,2);
458 putip((char *)&dgram
->header
.source_ip
,inbuf
+4);
459 dgram
->header
.source_port
= RSVAL(inbuf
,8);
460 dgram
->header
.dgm_length
= RSVAL(inbuf
,10);
461 dgram
->header
.packet_offset
= RSVAL(inbuf
,12);
465 if (dgram
->header
.msg_type
== 0x10 ||
466 dgram
->header
.msg_type
== 0x11 ||
467 dgram
->header
.msg_type
== 0x12) {
468 offset
+= parse_nmb_name(inbuf
,offset
,length
,&dgram
->source_name
);
469 offset
+= parse_nmb_name(inbuf
,offset
,length
,&dgram
->dest_name
);
472 if (offset
>= length
|| (length
-offset
> sizeof(dgram
->data
)))
475 dgram
->datasize
= length
-offset
;
476 memcpy(dgram
->data
,inbuf
+offset
,dgram
->datasize
);
478 /* Paranioa. Ensure the last 2 bytes in the dgram buffer are
479 zero. This should be true anyway, just enforce it for paranioa sake. JRA. */
480 SMB_ASSERT(dgram
->datasize
<= (sizeof(dgram
->data
)-2));
481 memset(&dgram
->data
[sizeof(dgram
->data
)-2], '\0', 2);
486 /*******************************************************************
487 Parse a nmb packet. Return False if the packet can't be parsed
488 or is invalid for some reason, True otherwise.
489 ******************************************************************/
491 static BOOL
parse_nmb(char *inbuf
,int length
,struct nmb_packet
*nmb
)
495 memset((char *)nmb
,'\0',sizeof(*nmb
));
500 /* parse the header */
501 nmb
->header
.name_trn_id
= RSVAL(inbuf
,0);
503 DEBUG(10,("parse_nmb: packet id = %d\n", nmb
->header
.name_trn_id
));
505 nmb
->header
.opcode
= (CVAL(inbuf
,2) >> 3) & 0xF;
506 nmb
->header
.response
= ((CVAL(inbuf
,2)>>7)&1)?True
:False
;
507 nm_flags
= ((CVAL(inbuf
,2) & 0x7) << 4) + (CVAL(inbuf
,3)>>4);
508 nmb
->header
.nm_flags
.bcast
= (nm_flags
&1)?True
:False
;
509 nmb
->header
.nm_flags
.recursion_available
= (nm_flags
&8)?True
:False
;
510 nmb
->header
.nm_flags
.recursion_desired
= (nm_flags
&0x10)?True
:False
;
511 nmb
->header
.nm_flags
.trunc
= (nm_flags
&0x20)?True
:False
;
512 nmb
->header
.nm_flags
.authoritative
= (nm_flags
&0x40)?True
:False
;
513 nmb
->header
.rcode
= CVAL(inbuf
,3) & 0xF;
514 nmb
->header
.qdcount
= RSVAL(inbuf
,4);
515 nmb
->header
.ancount
= RSVAL(inbuf
,6);
516 nmb
->header
.nscount
= RSVAL(inbuf
,8);
517 nmb
->header
.arcount
= RSVAL(inbuf
,10);
519 if (nmb
->header
.qdcount
) {
520 offset
= parse_nmb_name(inbuf
,12,length
,&nmb
->question
.question_name
);
524 if (length
- (12+offset
) < 4)
526 nmb
->question
.question_type
= RSVAL(inbuf
,12+offset
);
527 nmb
->question
.question_class
= RSVAL(inbuf
,12+offset
+2);
534 /* and any resource records */
535 if (nmb
->header
.ancount
&& !parse_alloc_res_rec(inbuf
,&offset
,length
,&nmb
->answers
,
536 nmb
->header
.ancount
))
539 if (nmb
->header
.nscount
&& !parse_alloc_res_rec(inbuf
,&offset
,length
,&nmb
->nsrecs
,
540 nmb
->header
.nscount
))
543 if (nmb
->header
.arcount
&& !parse_alloc_res_rec(inbuf
,&offset
,length
,&nmb
->additional
,
544 nmb
->header
.arcount
))
550 /*******************************************************************
551 'Copy constructor' for an nmb packet.
552 ******************************************************************/
554 static struct packet_struct
*copy_nmb_packet(struct packet_struct
*packet
)
556 struct nmb_packet
*nmb
;
557 struct nmb_packet
*copy_nmb
;
558 struct packet_struct
*pkt_copy
;
560 if(( pkt_copy
= (struct packet_struct
*)malloc(sizeof(*packet
))) == NULL
) {
561 DEBUG(0,("copy_nmb_packet: malloc fail.\n"));
565 /* Structure copy of entire thing. */
569 /* Ensure this copy is not locked. */
570 pkt_copy
->locked
= False
;
572 /* Ensure this copy has no resource records. */
573 nmb
= &packet
->packet
.nmb
;
574 copy_nmb
= &pkt_copy
->packet
.nmb
;
576 copy_nmb
->answers
= NULL
;
577 copy_nmb
->nsrecs
= NULL
;
578 copy_nmb
->additional
= NULL
;
580 /* Now copy any resource records. */
583 if((copy_nmb
->answers
= (struct res_rec
*)
584 malloc(nmb
->header
.ancount
* sizeof(struct res_rec
))) == NULL
)
586 memcpy((char *)copy_nmb
->answers
, (char *)nmb
->answers
,
587 nmb
->header
.ancount
* sizeof(struct res_rec
));
590 if((copy_nmb
->nsrecs
= (struct res_rec
*)
591 malloc(nmb
->header
.nscount
* sizeof(struct res_rec
))) == NULL
)
593 memcpy((char *)copy_nmb
->nsrecs
, (char *)nmb
->nsrecs
,
594 nmb
->header
.nscount
* sizeof(struct res_rec
));
596 if (nmb
->additional
) {
597 if((copy_nmb
->additional
= (struct res_rec
*)
598 malloc(nmb
->header
.arcount
* sizeof(struct res_rec
))) == NULL
)
600 memcpy((char *)copy_nmb
->additional
, (char *)nmb
->additional
,
601 nmb
->header
.arcount
* sizeof(struct res_rec
));
608 SAFE_FREE(copy_nmb
->answers
);
609 SAFE_FREE(copy_nmb
->nsrecs
);
610 SAFE_FREE(copy_nmb
->additional
);
613 DEBUG(0,("copy_nmb_packet: malloc fail in resource records.\n"));
617 /*******************************************************************
618 'Copy constructor' for a dgram packet.
619 ******************************************************************/
621 static struct packet_struct
*copy_dgram_packet(struct packet_struct
*packet
)
623 struct packet_struct
*pkt_copy
;
625 if(( pkt_copy
= (struct packet_struct
*)malloc(sizeof(*packet
))) == NULL
) {
626 DEBUG(0,("copy_dgram_packet: malloc fail.\n"));
630 /* Structure copy of entire thing. */
634 /* Ensure this copy is not locked. */
635 pkt_copy
->locked
= False
;
637 /* There are no additional pointers in a dgram packet,
642 /*******************************************************************
643 'Copy constructor' for a generic packet.
644 ******************************************************************/
646 struct packet_struct
*copy_packet(struct packet_struct
*packet
)
648 if(packet
->packet_type
== NMB_PACKET
)
649 return copy_nmb_packet(packet
);
650 else if (packet
->packet_type
== DGRAM_PACKET
)
651 return copy_dgram_packet(packet
);
655 /*******************************************************************
656 Free up any resources associated with an nmb packet.
657 ******************************************************************/
659 static void free_nmb_packet(struct nmb_packet
*nmb
)
661 SAFE_FREE(nmb
->answers
);
662 SAFE_FREE(nmb
->nsrecs
);
663 SAFE_FREE(nmb
->additional
);
666 /*******************************************************************
667 Free up any resources associated with a dgram packet.
668 ******************************************************************/
670 static void free_dgram_packet(struct dgram_packet
*nmb
)
672 /* We have nothing to do for a dgram packet. */
675 /*******************************************************************
676 Free up any resources associated with a packet.
677 ******************************************************************/
679 void free_packet(struct packet_struct
*packet
)
683 if (packet
->packet_type
== NMB_PACKET
)
684 free_nmb_packet(&packet
->packet
.nmb
);
685 else if (packet
->packet_type
== DGRAM_PACKET
)
686 free_dgram_packet(&packet
->packet
.dgram
);
687 ZERO_STRUCTPN(packet
);
691 /*******************************************************************
692 Parse a packet buffer into a packet structure.
693 ******************************************************************/
695 struct packet_struct
*parse_packet(char *buf
,int length
,
696 enum packet_type packet_type
)
698 extern struct in_addr lastip
;
700 struct packet_struct
*p
;
703 p
= (struct packet_struct
*)malloc(sizeof(*p
));
712 p
->timestamp
= time(NULL
);
713 p
->packet_type
= packet_type
;
715 switch (packet_type
) {
717 ok
= parse_nmb(buf
,length
,&p
->packet
.nmb
);
721 ok
= parse_dgram(buf
,length
,&p
->packet
.dgram
);
733 /*******************************************************************
734 Read a packet from a socket and parse it, returning a packet ready
735 to be used or put on the queue. This assumes a UDP socket.
736 ******************************************************************/
738 struct packet_struct
*read_packet(int fd
,enum packet_type packet_type
)
740 struct packet_struct
*packet
;
741 char buf
[MAX_DGRAM_SIZE
];
744 length
= read_udp_socket(fd
,buf
,sizeof(buf
));
745 if (length
< MIN_DGRAM_SIZE
)
748 packet
= parse_packet(buf
, length
, packet_type
);
756 DEBUG(5,("Received a packet of len %d from (%s) port %d\n",
757 length
, inet_ntoa(packet
->ip
), packet
->port
) );
762 /*******************************************************************
763 Send a udp packet on a already open socket.
764 ******************************************************************/
766 static BOOL
send_udp(int fd
,char *buf
,int len
,struct in_addr ip
,int port
)
770 struct sockaddr_in sock_out
;
772 /* set the address and port */
773 memset((char *)&sock_out
,'\0',sizeof(sock_out
));
774 putip((char *)&sock_out
.sin_addr
,(char *)&ip
);
775 sock_out
.sin_port
= htons( port
);
776 sock_out
.sin_family
= AF_INET
;
778 DEBUG( 5, ( "Sending a packet of len %d to (%s) on port %d\n",
779 len
, inet_ntoa(ip
), port
) );
782 * Patch to fix asynch error notifications from Linux kernel.
785 for (i
= 0; i
< 5; i
++) {
786 ret
= (sendto(fd
,buf
,len
,0,(struct sockaddr
*)&sock_out
, sizeof(sock_out
)) >= 0);
787 if (ret
|| errno
!= ECONNREFUSED
)
792 DEBUG(0,("Packet send failed to %s(%d) ERRNO=%s\n",
793 inet_ntoa(ip
),port
,strerror(errno
)));
801 /*******************************************************************
802 Build a dgram packet ready for sending.
804 XXXX This currently doesn't handle packets too big for one
805 datagram. It should split them and use the packet_offset, more and
806 first flags to handle the fragmentation. Yuck.
808 [...but it isn't clear that we would ever need to send a
809 a fragmented NBT Datagram. The IP layer does its own
810 fragmentation to ensure that messages can fit into the path
811 MTU. It *is* important to be able to receive and rebuild
812 fragmented NBT datagrams, just in case someone out there
813 really has implemented this 'feature'. crh -)------ ]
815 ******************************************************************/
817 static int build_dgram(char *buf
,struct packet_struct
*p
)
819 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
820 unsigned char *ubuf
= (unsigned char *)buf
;
823 /* put in the header */
824 ubuf
[0] = dgram
->header
.msg_type
;
825 ubuf
[1] = (((int)dgram
->header
.flags
.node_type
)<<2);
826 if (dgram
->header
.flags
.more
)
828 if (dgram
->header
.flags
.first
)
830 RSSVAL(ubuf
,2,dgram
->header
.dgm_id
);
831 putip(ubuf
+4,(char *)&dgram
->header
.source_ip
);
832 RSSVAL(ubuf
,8,dgram
->header
.source_port
);
833 RSSVAL(ubuf
,12,dgram
->header
.packet_offset
);
837 if (dgram
->header
.msg_type
== 0x10 ||
838 dgram
->header
.msg_type
== 0x11 ||
839 dgram
->header
.msg_type
== 0x12) {
840 offset
+= put_nmb_name((char *)ubuf
,offset
,&dgram
->source_name
);
841 offset
+= put_nmb_name((char *)ubuf
,offset
,&dgram
->dest_name
);
844 memcpy(ubuf
+offset
,dgram
->data
,dgram
->datasize
);
845 offset
+= dgram
->datasize
;
847 /* automatically set the dgm_length
848 * NOTE: RFC1002 says the dgm_length does *not*
849 * include the fourteen-byte header. crh
851 dgram
->header
.dgm_length
= (offset
- 14);
852 RSSVAL(ubuf
,10,dgram
->header
.dgm_length
);
857 /*******************************************************************
859 *******************************************************************/
861 void make_nmb_name( struct nmb_name
*n
, const char *name
, int type
)
864 memset( (char *)n
, '\0', sizeof(struct nmb_name
) );
865 fstrcpy(unix_name
, name
);
866 strupper_m(unix_name
);
867 push_ascii(n
->name
, unix_name
, sizeof(n
->name
), STR_TERMINATE
);
868 n
->name_type
= (unsigned int)type
& 0xFF;
869 push_ascii(n
->scope
, global_scope(), 64, STR_TERMINATE
);
872 /*******************************************************************
873 Compare two nmb names
874 ******************************************************************/
876 BOOL
nmb_name_equal(struct nmb_name
*n1
, struct nmb_name
*n2
)
878 return ((n1
->name_type
== n2
->name_type
) &&
879 strequal(n1
->name
,n2
->name
) &&
880 strequal(n1
->scope
,n2
->scope
));
883 /*******************************************************************
884 Build a nmb packet ready for sending.
886 XXXX this currently relies on not being passed something that expands
887 to a packet too big for the buffer. Eventually this should be
888 changed to set the trunc bit so the receiver can request the rest
889 via tcp (when that becomes supported)
890 ******************************************************************/
892 static int build_nmb(char *buf
,struct packet_struct
*p
)
894 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
895 unsigned char *ubuf
= (unsigned char *)buf
;
898 /* put in the header */
899 RSSVAL(ubuf
,offset
,nmb
->header
.name_trn_id
);
900 ubuf
[offset
+2] = (nmb
->header
.opcode
& 0xF) << 3;
901 if (nmb
->header
.response
)
902 ubuf
[offset
+2] |= (1<<7);
903 if (nmb
->header
.nm_flags
.authoritative
&&
904 nmb
->header
.response
)
905 ubuf
[offset
+2] |= 0x4;
906 if (nmb
->header
.nm_flags
.trunc
)
907 ubuf
[offset
+2] |= 0x2;
908 if (nmb
->header
.nm_flags
.recursion_desired
)
909 ubuf
[offset
+2] |= 0x1;
910 if (nmb
->header
.nm_flags
.recursion_available
&&
911 nmb
->header
.response
)
912 ubuf
[offset
+3] |= 0x80;
913 if (nmb
->header
.nm_flags
.bcast
)
914 ubuf
[offset
+3] |= 0x10;
915 ubuf
[offset
+3] |= (nmb
->header
.rcode
& 0xF);
917 RSSVAL(ubuf
,offset
+4,nmb
->header
.qdcount
);
918 RSSVAL(ubuf
,offset
+6,nmb
->header
.ancount
);
919 RSSVAL(ubuf
,offset
+8,nmb
->header
.nscount
);
920 RSSVAL(ubuf
,offset
+10,nmb
->header
.arcount
);
923 if (nmb
->header
.qdcount
) {
924 /* XXXX this doesn't handle a qdcount of > 1 */
925 offset
+= put_nmb_name((char *)ubuf
,offset
,&nmb
->question
.question_name
);
926 RSSVAL(ubuf
,offset
,nmb
->question
.question_type
);
927 RSSVAL(ubuf
,offset
+2,nmb
->question
.question_class
);
931 if (nmb
->header
.ancount
)
932 offset
+= put_res_rec((char *)ubuf
,offset
,nmb
->answers
,
933 nmb
->header
.ancount
);
935 if (nmb
->header
.nscount
)
936 offset
+= put_res_rec((char *)ubuf
,offset
,nmb
->nsrecs
,
937 nmb
->header
.nscount
);
940 * The spec says we must put compressed name pointers
941 * in the following outgoing packets :
942 * NAME_REGISTRATION_REQUEST, NAME_REFRESH_REQUEST,
943 * NAME_RELEASE_REQUEST.
946 if((nmb
->header
.response
== False
) &&
947 ((nmb
->header
.opcode
== NMB_NAME_REG_OPCODE
) ||
948 (nmb
->header
.opcode
== NMB_NAME_RELEASE_OPCODE
) ||
949 (nmb
->header
.opcode
== NMB_NAME_REFRESH_OPCODE_8
) ||
950 (nmb
->header
.opcode
== NMB_NAME_REFRESH_OPCODE_9
) ||
951 (nmb
->header
.opcode
== NMB_NAME_MULTIHOMED_REG_OPCODE
)) &&
952 (nmb
->header
.arcount
== 1)) {
954 offset
+= put_compressed_name_ptr(ubuf
,offset
,nmb
->additional
,12);
956 } else if (nmb
->header
.arcount
) {
957 offset
+= put_res_rec((char *)ubuf
,offset
,nmb
->additional
,
958 nmb
->header
.arcount
);
963 /*******************************************************************
965 ******************************************************************/
967 int build_packet(char *buf
, struct packet_struct
*p
)
971 switch (p
->packet_type
) {
973 len
= build_nmb(buf
,p
);
977 len
= build_dgram(buf
,p
);
984 /*******************************************************************
985 Send a packet_struct.
986 ******************************************************************/
988 BOOL
send_packet(struct packet_struct
*p
)
993 memset(buf
,'\0',sizeof(buf
));
995 len
= build_packet(buf
, p
);
1000 return(send_udp(p
->fd
,buf
,len
,p
->ip
,p
->port
));
1003 /****************************************************************************
1004 Receive a packet with timeout on a open UDP filedescriptor.
1005 The timeout is in milliseconds
1006 ***************************************************************************/
1008 struct packet_struct
*receive_packet(int fd
,enum packet_type type
,int t
)
1011 struct timeval timeout
;
1016 timeout
.tv_sec
= t
/1000;
1017 timeout
.tv_usec
= 1000*(t
%1000);
1019 if ((ret
= sys_select_intr(fd
+1,&fds
,NULL
,NULL
,&timeout
)) == -1) {
1020 /* errno should be EBADF or EINVAL. */
1021 DEBUG(0,("select returned -1, errno = %s (%d)\n", strerror(errno
), errno
));
1025 if (ret
== 0) /* timeout */
1028 if (FD_ISSET(fd
,&fds
))
1029 return(read_packet(fd
,type
));
1034 /****************************************************************************
1035 Receive a UDP/137 packet either via UDP or from the unexpected packet
1036 queue. The packet must be a reply packet and have the specified trn_id.
1037 The timeout is in milliseconds.
1038 ***************************************************************************/
1040 struct packet_struct
*receive_nmb_packet(int fd
, int t
, int trn_id
)
1042 struct packet_struct
*p
;
1044 p
= receive_packet(fd
, NMB_PACKET
, t
);
1046 if (p
&& p
->packet
.nmb
.header
.response
&&
1047 p
->packet
.nmb
.header
.name_trn_id
== trn_id
) {
1053 /* try the unexpected packet queue */
1054 return receive_unexpected(NMB_PACKET
, trn_id
, NULL
);
1057 /****************************************************************************
1058 Receive a UDP/138 packet either via UDP or from the unexpected packet
1059 queue. The packet must be a reply packet and have the specified mailslot name
1060 The timeout is in milliseconds.
1061 ***************************************************************************/
1063 struct packet_struct
*receive_dgram_packet(int fd
, int t
, const char *mailslot_name
)
1065 struct packet_struct
*p
;
1067 p
= receive_packet(fd
, DGRAM_PACKET
, t
);
1069 if (p
&& match_mailslot_name(p
, mailslot_name
)) {
1075 /* try the unexpected packet queue */
1076 return receive_unexpected(DGRAM_PACKET
, 0, mailslot_name
);
1079 /****************************************************************************
1080 See if a datagram has the right mailslot name.
1081 ***************************************************************************/
1083 BOOL
match_mailslot_name(struct packet_struct
*p
, const char *mailslot_name
)
1085 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
1088 buf
= &dgram
->data
[0];
1093 if (memcmp(buf
, mailslot_name
, strlen(mailslot_name
)+1) == 0) {
1100 /****************************************************************************
1101 Return the number of bits that match between two 4 character buffers
1102 ***************************************************************************/
1104 int matching_quad_bits(unsigned char *p1
, unsigned char *p2
)
1107 for (i
=0; i
<4; i
++) {
1116 for (j
=0; j
<8; j
++) {
1117 if ((p1
[i
] & (1<<(7-j
))) != (p2
[i
] & (1<<(7-j
))))
1125 static unsigned char sort_ip
[4];
1127 /****************************************************************************
1128 Compare two query reply records.
1129 ***************************************************************************/
1131 static int name_query_comp(unsigned char *p1
, unsigned char *p2
)
1133 return matching_quad_bits(p2
+2, sort_ip
) - matching_quad_bits(p1
+2, sort_ip
);
1136 /****************************************************************************
1137 Sort a set of 6 byte name query response records so that the IPs that
1138 have the most leading bits in common with the specified address come first.
1139 ***************************************************************************/
1141 void sort_query_replies(char *data
, int n
, struct in_addr ip
)
1146 putip(sort_ip
, (char *)&ip
);
1148 qsort(data
, n
, 6, QSORT_CAST name_query_comp
);
1151 /*******************************************************************
1152 Convert, possibly using a stupid microsoft-ism which has destroyed
1153 the transport independence of netbios (for CIFS vendors that usually
1154 use the Win95-type methods, not for NT to NT communication, which uses
1155 DCE/RPC and therefore full-length unicode strings...) a dns name into
1158 The netbios name (NOT necessarily null-terminated) is truncated to 15
1161 ******************************************************************/
1163 char *dns_to_netbios_name(const char *dns_name
)
1165 static nstring netbios_name
;
1167 StrnCpy(netbios_name
, dns_name
, MAX_NETBIOSNAME_LEN
-1);
1168 netbios_name
[15] = 0;
1170 /* ok. this is because of a stupid microsoft-ism. if the called host
1171 name contains a '.', microsoft clients expect you to truncate the
1172 netbios name up to and including the '.' this even applies, by
1173 mistake, to workgroup (domain) names, which is _really_ daft.
1175 for (i
= 0; i
< 15; i
++) {
1176 if (netbios_name
[i
] == '.') {
1177 netbios_name
[i
] = 0;
1182 return netbios_name
;
1185 /****************************************************************************
1186 Interpret the weird netbios "name" into a unix fstring. Return the name type.
1187 ****************************************************************************/
1189 static int name_interpret(char *in
, fstring name
)
1192 int len
= (*in
++) / 2;
1194 char *out
= out_string
;
1198 if (len
> 30 || len
<1)
1202 if (in
[0] < 'A' || in
[0] > 'P' || in
[1] < 'A' || in
[1] > 'P') {
1206 *out
= ((in
[0]-'A')<<4) + (in
[1]-'A');
1213 #ifdef NETBIOS_SCOPE
1214 /* Handle any scope names */
1216 *out
++ = '.'; /* Scope names are separated by periods */
1217 len
= *(unsigned char *)in
++;
1218 StrnCpy(out
, in
, len
);
1224 pull_ascii_fstring(name
, out_string
);
1229 /****************************************************************************
1230 Mangle a name into netbios format.
1231 Note: <Out> must be (33 + strlen(scope) + 2) bytes long, at minimum.
1232 ****************************************************************************/
1234 int name_mangle( char *In
, char *Out
, char name_type
)
1241 /* Safely copy the input string, In, into buf[]. */
1242 if (strcmp(In
,"*") == 0)
1243 put_name(buf
, "*", '\0', 0x00);
1245 /* We use an fstring here as mb dos names can expend x3 when
1250 pull_ascii_fstring(buf_unix
, In
);
1251 strupper_m(buf_unix
);
1253 push_ascii_nstring(buf_dos
, buf_unix
);
1254 put_name(buf
, buf_dos
, ' ', name_type
);
1257 /* Place the length of the first field into the output buffer. */
1261 /* Now convert the name to the rfc1001/1002 format. */
1262 for( i
= 0; i
< MAX_NETBIOSNAME_LEN
; i
++ ) {
1263 p
[i
*2] = ( (buf
[i
] >> 4) & 0x000F ) + 'A';
1264 p
[(i
*2)+1] = (buf
[i
] & 0x000F) + 'A';
1269 /* Add the scope string. */
1270 for( i
= 0, len
= 0; *(global_scope()) != '\0'; i
++, len
++ ) {
1271 switch( (global_scope())[i
] ) {
1276 return( name_len(Out
) );
1283 p
[len
+1] = (global_scope())[i
];
1288 return( name_len(Out
) );
1291 /****************************************************************************
1292 Find a pointer to a netbios name.
1293 ****************************************************************************/
1295 static char *name_ptr(char *buf
,int ofs
)
1297 unsigned char c
= *(unsigned char *)(buf
+ofs
);
1299 if ((c
& 0xC0) == 0xC0) {
1300 uint16 l
= RSVAL(buf
, ofs
) & 0x3FFF;
1301 DEBUG(5,("name ptr to pos %d from %d is %s\n",l
,ofs
,buf
+l
));
1308 /****************************************************************************
1309 Extract a netbios name from a buf (into a unix string) return name type.
1310 ****************************************************************************/
1312 int name_extract(char *buf
,int ofs
, fstring name
)
1314 char *p
= name_ptr(buf
,ofs
);
1315 int d
= PTR_DIFF(p
,buf
+ofs
);
1318 if (d
< -50 || d
> 50)
1320 return(name_interpret(p
,name
));
1323 /****************************************************************************
1324 Return the total storage length of a mangled name.
1325 ****************************************************************************/
1327 int name_len(char *s1
)
1329 /* NOTE: this argument _must_ be unsigned */
1330 unsigned char *s
= (unsigned char *)s1
;
1333 /* If the two high bits of the byte are set, return 2. */
1334 if (0xC0 == (*s
& 0xC0))
1337 /* Add up the length bytes. */
1338 for (len
= 1; (*s
); s
+= (*s
) + 1) {
1340 SMB_ASSERT(len
< 80);