2 Unix SMB/CIFS implementation.
3 NBT netbios library routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libsmb/nmblib.h"
24 #include "lib/util/string_wrappers.h"
26 static const struct opcode_names
{
27 const char *nmb_opcode_name
;
29 } nmb_header_opcode_names
[] = {
35 {"Refresh(altcode)", 9 },
36 {"Multi-homed Registration", 15 },
40 /****************************************************************************
41 Lookup a nmb opcode name.
42 ****************************************************************************/
44 static const char *lookup_opcode_name( int opcode
)
46 const struct opcode_names
*op_namep
;
49 for(i
= 0; nmb_header_opcode_names
[i
].nmb_opcode_name
!= 0; i
++) {
50 op_namep
= &nmb_header_opcode_names
[i
];
51 if(opcode
== op_namep
->opcode
)
52 return op_namep
->nmb_opcode_name
;
54 return "<unknown opcode>";
57 /****************************************************************************
58 Print out a res_rec structure.
59 ****************************************************************************/
61 static void debug_nmb_res_rec(struct res_rec
*res
, const char *hdr
)
65 DEBUGADD( 4, ( " %s: nmb_name=%s rr_type=%d rr_class=%d ttl=%d\n",
67 nmb_namestr(&res
->rr_name
),
72 if (res
->rdlength
== 0) {
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 "
111 "opcode=%s(%d) response=%s\n",
112 inet_ntoa(p
->ip
), p
->port
,
113 nmb
->header
.name_trn_id
,
114 lookup_opcode_name(nmb
->header
.opcode
),
116 BOOLSTR(nmb
->header
.response
) );
117 dbgtext( " header: flags: bcast=%s rec_avail=%s "
118 "rec_des=%s trunc=%s auth=%s\n",
119 BOOLSTR(nmb
->header
.nm_flags
.bcast
),
120 BOOLSTR(nmb
->header
.nm_flags
.recursion_available
),
121 BOOLSTR(nmb
->header
.nm_flags
.recursion_desired
),
122 BOOLSTR(nmb
->header
.nm_flags
.trunc
),
123 BOOLSTR(nmb
->header
.nm_flags
.authoritative
) );
124 dbgtext( " header: rcode=%d qdcount=%d ancount=%d "
125 "nscount=%d arcount=%d\n",
130 nmb
->header
.arcount
);
133 if (nmb
->header
.qdcount
) {
134 DEBUGADD( 4, ( " question: q_name=%s q_type=%d q_class=%d\n",
135 nmb_namestr(&nmb
->question
.question_name
),
136 nmb
->question
.question_type
,
137 nmb
->question
.question_class
) );
140 if (nmb
->answers
&& nmb
->header
.ancount
) {
141 debug_nmb_res_rec(nmb
->answers
,"answers");
143 if (nmb
->nsrecs
&& nmb
->header
.nscount
) {
144 debug_nmb_res_rec(nmb
->nsrecs
,"nsrecs");
146 if (nmb
->additional
&& nmb
->header
.arcount
) {
147 debug_nmb_res_rec(nmb
->additional
,"additional");
151 /*******************************************************************
152 Handle "compressed" name pointers.
153 ******************************************************************/
155 static bool handle_name_ptrs(unsigned char *ubuf
,int *offset
,int length
,
156 bool *got_pointer
,int *ret
)
160 while ((ubuf
[*offset
] & 0xC0) == 0xC0) {
164 if (*offset
> length
- 2) {
167 (*offset
) = ((ubuf
[*offset
] & ~0xC0)<<8) | ubuf
[(*offset
)+1];
168 if (loop_count
++ == 10 ||
169 (*offset
) < 0 || (*offset
)>(length
-2)) {
176 /*******************************************************************
177 Parse a nmb name from "compressed" format to something readable
178 return the space taken by the name, or 0 if the name is invalid
179 ******************************************************************/
181 static int parse_nmb_name(char *inbuf
,int ofs
,int length
, struct nmb_name
*name
)
184 unsigned char *ubuf
= (unsigned char *)inbuf
;
186 bool got_pointer
=False
;
190 if (length
- offset
< 2)
193 /* handle initial name pointers */
194 if (!handle_name_ptrs(ubuf
,&offset
,length
,&got_pointer
,&ret
))
199 /* m must be 32 to exactly fill in the 16 bytes of the netbios name */
203 /* Cannot go past length. */
204 if (offset
+m
+2 > length
) {
208 memset((char *)name
,'\0',sizeof(*name
));
210 /* the "compressed" part */
216 c1
= ubuf
[offset
++]-'A';
217 c2
= ubuf
[offset
++]-'A';
218 if ((c1
& 0xF0) || (c2
& 0xF0)) {
221 if (n
>= sizeof(name
->name
)) {
224 name
->name
[n
++] = (c1
<<4) | c2
;
228 * RFC1002: For a valid NetBIOS name, exiting from the above,
229 * n *must* be MAX_NETBIOSNAME_LEN (16).
231 if (n
!= MAX_NETBIOSNAME_LEN
) {
235 /* parse out the name type, its always
236 * in the 16th byte of the name */
237 name
->name_type
= ((unsigned char)name
->name
[15]) & 0xff;
239 /* remove trailing spaces */
242 while (n
&& name
->name
[n
]==' ')
245 /* now the domain parts (if any) */
247 while (ubuf
[offset
]) {
248 /* we can have pointers within the domain part as well */
249 if (!handle_name_ptrs(ubuf
,&offset
,length
,&got_pointer
,&ret
))
254 * Don't allow null domain parts.
261 name
->scope
[n
++] = '.';
262 if (m
+2+offset
>length
|| n
+m
+1>sizeof(name
->scope
))
266 name
->scope
[n
++] = (char)ubuf
[offset
++];
269 * Watch for malicious loops.
271 if (loop_count
++ == 10)
274 name
->scope
[n
++] = 0;
279 /****************************************************************************
280 Put a netbios name, padding(s) and a name type into a 16 character buffer.
281 name is already in DOS charset.
282 [15 bytes name + padding][1 byte name type].
283 ****************************************************************************/
285 void put_name(char *dest
, const char *name
, int pad
, unsigned int name_type
)
287 size_t len
= strlen(name
);
289 memcpy(dest
, name
, (len
< MAX_NETBIOSNAME_LEN
) ?
290 len
: MAX_NETBIOSNAME_LEN
- 1);
291 if (len
< MAX_NETBIOSNAME_LEN
- 1) {
292 memset(dest
+ len
, pad
, MAX_NETBIOSNAME_LEN
- 1 - len
);
294 dest
[MAX_NETBIOSNAME_LEN
- 1] = name_type
;
297 /*******************************************************************
298 Put a compressed nmb name into a buffer. Return the length of the
301 Compressed names are really weird. The "compression" doubles the
302 size. The idea is that it also means that compressed names conform
303 to the doman name system. See RFC1002.
305 If buf == NULL this is a length calculation.
306 ******************************************************************/
308 static int put_nmb_name(char *buf
, size_t buflen
, int offset
,struct nmb_name
*name
)
314 if (strcmp(name
->name
,"*") == 0) {
315 /* special case for wildcard name */
316 put_name(buf1
, "*", '\0', name
->name_type
);
318 put_name(buf1
, name
->name
, ' ', name
->name_type
);
322 if (offset
>= buflen
) {
330 for (m
=0;m
<MAX_NETBIOSNAME_LEN
;m
++) {
332 if (offset
+2+2*m
>= buflen
) {
335 buf
[offset
+1+2*m
] = 'A' + ((buf1
[m
]>>4)&0xF);
336 buf
[offset
+2+2*m
] = 'A' + (buf1
[m
]&0xF);
342 if (offset
>= buflen
) {
348 if (name
->scope
[0]) {
349 /* XXXX this scope handling needs testing */
350 size_t scopenamelen
= strlen(name
->scope
) + 1;
353 if (offset
+1+scopenamelen
>= buflen
) {
356 strlcpy(&buf
[offset
+1],name
->scope
,
357 buflen
- (offset
+1));
360 while ((p
= strchr_m(p
,'.'))) {
361 buf
[offset
] = PTR_DIFF(p
,&buf
[offset
+1]);
362 offset
+= (buf
[offset
] + 1);
363 if (offset
+1 >= buflen
) {
368 buf
[offset
] = strlen(&buf
[offset
+1]);
375 /*******************************************************************
376 Useful for debugging messages.
377 ******************************************************************/
379 char *nmb_namestr(const struct nmb_name
*n
)
384 pull_ascii_fstring(name
, n
->name
);
386 result
= talloc_asprintf(talloc_tos(), "%s<%02x>", name
,
389 result
= talloc_asprintf(talloc_tos(), "%s<%02x>.%s", name
,
390 n
->name_type
, n
->scope
);
392 SMB_ASSERT(result
!= NULL
);
396 /*******************************************************************
397 Allocate and parse some resource records.
398 ******************************************************************/
400 static bool parse_alloc_res_rec(char *inbuf
,int *offset
,int length
,
401 struct res_rec
**recs
, int count
)
405 *recs
= SMB_MALLOC_ARRAY(struct res_rec
, count
);
409 memset((char *)*recs
,'\0',sizeof(**recs
)*count
);
411 for (i
=0;i
<count
;i
++) {
412 int l
= parse_nmb_name(inbuf
,*offset
,length
,
413 &(*recs
)[i
].rr_name
);
415 if (!l
|| (*offset
)+10 > length
) {
419 (*recs
)[i
].rr_type
= RSVAL(inbuf
,(*offset
));
420 (*recs
)[i
].rr_class
= RSVAL(inbuf
,(*offset
)+2);
421 (*recs
)[i
].ttl
= RIVAL(inbuf
,(*offset
)+4);
422 (*recs
)[i
].rdlength
= RSVAL(inbuf
,(*offset
)+8);
424 if ((*recs
)[i
].rdlength
>sizeof((*recs
)[i
].rdata
) ||
425 (*offset
)+(*recs
)[i
].rdlength
> length
) {
429 memcpy((*recs
)[i
].rdata
,inbuf
+(*offset
),(*recs
)[i
].rdlength
);
430 (*offset
) += (*recs
)[i
].rdlength
;
435 /*******************************************************************
436 Put a resource record into a packet.
437 If buf == NULL this is a length calculation.
438 ******************************************************************/
440 static int put_res_rec(char *buf
, size_t buflen
, int offset
,struct res_rec
*recs
,int count
)
445 for (i
=0;i
<count
;i
++) {
446 int l
= put_nmb_name(buf
,buflen
,offset
,&recs
[i
].rr_name
);
450 RSSVAL(buf
,offset
,recs
[i
].rr_type
);
451 RSSVAL(buf
,offset
+2,recs
[i
].rr_class
);
452 RSIVAL(buf
,offset
+4,(unsigned int)recs
[i
].ttl
);
453 RSSVAL(buf
,offset
+8,recs
[i
].rdlength
);
454 memcpy(buf
+offset
+10,recs
[i
].rdata
,recs
[i
].rdlength
);
456 offset
+= 10+recs
[i
].rdlength
;
457 ret
+= 10+recs
[i
].rdlength
;
463 /*******************************************************************
464 Put a compressed name pointer record into a packet.
465 If buf == NULL this is a length calculation.
466 ******************************************************************/
468 static int put_compressed_name_ptr(unsigned char *buf
,
475 buf
[offset
] = (0xC0 | ((ptr_offset
>> 8) & 0xFF));
476 buf
[offset
+1] = (ptr_offset
& 0xFF);
480 RSSVAL(buf
,offset
,rec
->rr_type
);
481 RSSVAL(buf
,offset
+2,rec
->rr_class
);
482 RSIVAL(buf
,offset
+4,rec
->ttl
);
483 RSSVAL(buf
,offset
+8,rec
->rdlength
);
484 memcpy(buf
+offset
+10,rec
->rdata
,rec
->rdlength
);
486 offset
+= 10+rec
->rdlength
;
487 ret
= (offset
- ret
);
492 /*******************************************************************
493 Parse a dgram packet. Return False if the packet can't be parsed
494 or is invalid for some reason, True otherwise.
496 This is documented in section 4.4.1 of RFC1002.
497 ******************************************************************/
499 static bool parse_dgram(char *inbuf
,int length
,struct dgram_packet
*dgram
)
504 memset((char *)dgram
,'\0',sizeof(*dgram
));
509 dgram
->header
.msg_type
= CVAL(inbuf
,0);
510 flags
= CVAL(inbuf
,1);
511 dgram
->header
.flags
.node_type
= (enum node_type
)((flags
>>2)&3);
513 dgram
->header
.flags
.more
= True
;
515 dgram
->header
.flags
.first
= True
;
516 dgram
->header
.dgm_id
= RSVAL(inbuf
,2);
517 putip((char *)&dgram
->header
.source_ip
,inbuf
+4);
518 dgram
->header
.source_port
= RSVAL(inbuf
,8);
519 dgram
->header
.dgm_length
= RSVAL(inbuf
,10);
520 dgram
->header
.packet_offset
= RSVAL(inbuf
,12);
524 if (dgram
->header
.msg_type
== 0x10 ||
525 dgram
->header
.msg_type
== 0x11 ||
526 dgram
->header
.msg_type
== 0x12) {
527 offset
+= parse_nmb_name(inbuf
,offset
,length
,
528 &dgram
->source_name
);
529 offset
+= parse_nmb_name(inbuf
,offset
,length
,
533 if (offset
>= length
|| (length
-offset
> sizeof(dgram
->data
)))
536 dgram
->datasize
= length
-offset
;
537 memcpy(dgram
->data
,inbuf
+offset
,dgram
->datasize
);
539 /* Paranioa. Ensure the last 2 bytes in the dgram buffer are
540 zero. This should be true anyway, just enforce it for
541 paranioa sake. JRA. */
542 SMB_ASSERT(dgram
->datasize
<= (sizeof(dgram
->data
)-2));
543 memset(&dgram
->data
[sizeof(dgram
->data
)-2], '\0', 2);
548 /*******************************************************************
549 Parse a nmb packet. Return False if the packet can't be parsed
550 or is invalid for some reason, True otherwise.
551 ******************************************************************/
553 static bool parse_nmb(char *inbuf
,int length
,struct nmb_packet
*nmb
)
557 memset((char *)nmb
,'\0',sizeof(*nmb
));
562 /* parse the header */
563 nmb
->header
.name_trn_id
= RSVAL(inbuf
,0);
565 DEBUG(10,("parse_nmb: packet id = %d\n", nmb
->header
.name_trn_id
));
567 nmb
->header
.opcode
= (CVAL(inbuf
,2) >> 3) & 0xF;
568 nmb
->header
.response
= ((CVAL(inbuf
,2)>>7)&1)?True
:False
;
569 nm_flags
= ((CVAL(inbuf
,2) & 0x7) << 4) + (CVAL(inbuf
,3)>>4);
570 nmb
->header
.nm_flags
.bcast
= (nm_flags
&1)?True
:False
;
571 nmb
->header
.nm_flags
.recursion_available
= (nm_flags
&8)?True
:False
;
572 nmb
->header
.nm_flags
.recursion_desired
= (nm_flags
&0x10)?True
:False
;
573 nmb
->header
.nm_flags
.trunc
= (nm_flags
&0x20)?True
:False
;
574 nmb
->header
.nm_flags
.authoritative
= (nm_flags
&0x40)?True
:False
;
575 nmb
->header
.rcode
= CVAL(inbuf
,3) & 0xF;
576 nmb
->header
.qdcount
= RSVAL(inbuf
,4);
577 nmb
->header
.ancount
= RSVAL(inbuf
,6);
578 nmb
->header
.nscount
= RSVAL(inbuf
,8);
579 nmb
->header
.arcount
= RSVAL(inbuf
,10);
581 if (nmb
->header
.qdcount
) {
582 offset
= parse_nmb_name(inbuf
,12,length
,
583 &nmb
->question
.question_name
);
587 if (length
- (12+offset
) < 4)
589 nmb
->question
.question_type
= RSVAL(inbuf
,12+offset
);
590 nmb
->question
.question_class
= RSVAL(inbuf
,12+offset
+2);
597 /* and any resource records */
598 if (nmb
->header
.ancount
&&
599 !parse_alloc_res_rec(inbuf
,&offset
,length
,&nmb
->answers
,
600 nmb
->header
.ancount
))
603 if (nmb
->header
.nscount
&&
604 !parse_alloc_res_rec(inbuf
,&offset
,length
,&nmb
->nsrecs
,
605 nmb
->header
.nscount
))
608 if (nmb
->header
.arcount
&&
609 !parse_alloc_res_rec(inbuf
,&offset
,length
,
610 &nmb
->additional
, nmb
->header
.arcount
))
616 /*******************************************************************
617 'Copy constructor' for an nmb packet.
618 ******************************************************************/
620 static struct packet_struct
*copy_nmb_packet(struct packet_struct
*packet
)
622 struct nmb_packet
*nmb
;
623 struct nmb_packet
*copy_nmb
;
624 struct packet_struct
*pkt_copy
;
626 if(( pkt_copy
= SMB_MALLOC_P(struct packet_struct
)) == NULL
) {
627 DEBUG(0,("copy_nmb_packet: malloc fail.\n"));
631 /* Structure copy of entire thing. */
635 /* Ensure this copy is not locked. */
636 pkt_copy
->locked
= False
;
637 pkt_copy
->recv_fd
= -1;
638 pkt_copy
->send_fd
= -1;
640 /* Ensure this copy has no resource records. */
641 nmb
= &packet
->packet
.nmb
;
642 copy_nmb
= &pkt_copy
->packet
.nmb
;
644 copy_nmb
->answers
= NULL
;
645 copy_nmb
->nsrecs
= NULL
;
646 copy_nmb
->additional
= NULL
;
648 /* Now copy any resource records. */
651 if((copy_nmb
->answers
= SMB_MALLOC_ARRAY(
652 struct res_rec
,nmb
->header
.ancount
)) == NULL
)
654 memcpy((char *)copy_nmb
->answers
, (char *)nmb
->answers
,
655 nmb
->header
.ancount
* sizeof(struct res_rec
));
658 if((copy_nmb
->nsrecs
= SMB_MALLOC_ARRAY(
659 struct res_rec
, nmb
->header
.nscount
)) == NULL
)
661 memcpy((char *)copy_nmb
->nsrecs
, (char *)nmb
->nsrecs
,
662 nmb
->header
.nscount
* sizeof(struct res_rec
));
664 if (nmb
->additional
) {
665 if((copy_nmb
->additional
= SMB_MALLOC_ARRAY(
666 struct res_rec
, nmb
->header
.arcount
)) == NULL
)
668 memcpy((char *)copy_nmb
->additional
, (char *)nmb
->additional
,
669 nmb
->header
.arcount
* sizeof(struct res_rec
));
676 SAFE_FREE(copy_nmb
->answers
);
677 SAFE_FREE(copy_nmb
->nsrecs
);
678 SAFE_FREE(copy_nmb
->additional
);
681 DEBUG(0,("copy_nmb_packet: malloc fail in resource records.\n"));
685 /*******************************************************************
686 'Copy constructor' for a dgram packet.
687 ******************************************************************/
689 static struct packet_struct
*copy_dgram_packet(struct packet_struct
*packet
)
691 struct packet_struct
*pkt_copy
;
693 if(( pkt_copy
= SMB_MALLOC_P(struct packet_struct
)) == NULL
) {
694 DEBUG(0,("copy_dgram_packet: malloc fail.\n"));
698 /* Structure copy of entire thing. */
702 /* Ensure this copy is not locked. */
703 pkt_copy
->locked
= False
;
704 pkt_copy
->recv_fd
= -1;
705 pkt_copy
->send_fd
= -1;
707 /* There are no additional pointers in a dgram packet,
712 /*******************************************************************
713 'Copy constructor' for a generic packet.
714 ******************************************************************/
716 struct packet_struct
*copy_packet(struct packet_struct
*packet
)
718 if(packet
->packet_type
== NMB_PACKET
)
719 return copy_nmb_packet(packet
);
720 else if (packet
->packet_type
== DGRAM_PACKET
)
721 return copy_dgram_packet(packet
);
725 /*******************************************************************
726 Free up any resources associated with an nmb packet.
727 ******************************************************************/
729 static void free_nmb_packet(struct nmb_packet
*nmb
)
731 SAFE_FREE(nmb
->answers
);
732 SAFE_FREE(nmb
->nsrecs
);
733 SAFE_FREE(nmb
->additional
);
736 /*******************************************************************
737 Free up any resources associated with a dgram packet.
738 ******************************************************************/
740 static void free_dgram_packet(struct dgram_packet
*nmb
)
742 /* We have nothing to do for a dgram packet. */
745 /*******************************************************************
746 Free up any resources associated with a packet.
747 ******************************************************************/
749 void free_packet(struct packet_struct
*packet
)
753 if (packet
->packet_type
== NMB_PACKET
)
754 free_nmb_packet(&packet
->packet
.nmb
);
755 else if (packet
->packet_type
== DGRAM_PACKET
)
756 free_dgram_packet(&packet
->packet
.dgram
);
757 ZERO_STRUCTPN(packet
);
761 int packet_trn_id(struct packet_struct
*p
)
764 switch (p
->packet_type
) {
766 result
= p
->packet
.nmb
.header
.name_trn_id
;
769 result
= p
->packet
.dgram
.header
.dgm_id
;
777 /*******************************************************************
778 Parse a packet buffer into a packet structure.
779 ******************************************************************/
781 struct packet_struct
*parse_packet(char *buf
,int length
,
782 enum packet_type packet_type
,
786 struct packet_struct
*p
;
789 p
= SMB_MALLOC_P(struct packet_struct
);
793 ZERO_STRUCTP(p
); /* initialize for possible padding */
800 p
->timestamp
= time(NULL
);
801 p
->packet_type
= packet_type
;
803 switch (packet_type
) {
805 ok
= parse_nmb(buf
,length
,&p
->packet
.nmb
);
809 ok
= parse_dgram(buf
,length
,&p
->packet
.dgram
);
821 static struct packet_struct
*copy_packet_talloc(
822 TALLOC_CTX
*mem_ctx
, const struct packet_struct
*src
)
824 struct packet_struct
*pkt
;
826 pkt
= talloc_memdup(mem_ctx
, src
, sizeof(struct packet_struct
));
834 if (src
->packet_type
== NMB_PACKET
) {
835 const struct nmb_packet
*nsrc
= &src
->packet
.nmb
;
836 struct nmb_packet
*ndst
= &pkt
->packet
.nmb
;
838 if (nsrc
->answers
!= NULL
) {
839 ndst
->answers
= talloc_memdup(
841 sizeof(struct res_rec
) * nsrc
->header
.ancount
);
842 if (ndst
->answers
== NULL
) {
846 if (nsrc
->nsrecs
!= NULL
) {
847 ndst
->nsrecs
= talloc_memdup(
849 sizeof(struct res_rec
) * nsrc
->header
.nscount
);
850 if (ndst
->nsrecs
== NULL
) {
854 if (nsrc
->additional
!= NULL
) {
855 ndst
->additional
= talloc_memdup(
856 pkt
, nsrc
->additional
,
857 sizeof(struct res_rec
) * nsrc
->header
.arcount
);
858 if (ndst
->additional
== NULL
) {
867 * DGRAM packets have no substructures
875 struct packet_struct
*parse_packet_talloc(TALLOC_CTX
*mem_ctx
,
876 char *buf
,int length
,
877 enum packet_type packet_type
,
881 struct packet_struct
*pkt
, *result
;
883 pkt
= parse_packet(buf
, length
, packet_type
, ip
, port
);
887 result
= copy_packet_talloc(mem_ctx
, pkt
);
892 /*******************************************************************
893 Send a udp packet on a already open socket.
894 ******************************************************************/
896 static bool send_udp(int fd
,char *buf
,int len
,struct in_addr ip
,int port
)
900 struct sockaddr_in sock_out
;
902 /* set the address and port */
903 memset((char *)&sock_out
,'\0',sizeof(sock_out
));
904 putip((char *)&sock_out
.sin_addr
,(char *)&ip
);
905 sock_out
.sin_port
= htons( port
);
906 sock_out
.sin_family
= AF_INET
;
908 DEBUG( 5, ( "Sending a packet of len %d to (%s) on port %d\n",
909 len
, inet_ntoa(ip
), port
) );
912 * Patch to fix asynch error notifications from Linux kernel.
915 for (i
= 0; i
< 5; i
++) {
916 ret
= (sendto(fd
,buf
,len
,0,(struct sockaddr
*)&sock_out
,
917 sizeof(sock_out
)) >= 0);
918 if (ret
|| errno
!= ECONNREFUSED
)
923 DEBUG(0,("Packet send failed to %s(%d) ERRNO=%s\n",
924 inet_ntoa(ip
),port
,strerror(errno
)));
929 /*******************************************************************
930 Build a dgram packet ready for sending.
931 If buf == NULL this is a length calculation.
932 ******************************************************************/
934 static int build_dgram(char *buf
, size_t len
, struct dgram_packet
*dgram
)
936 unsigned char *ubuf
= (unsigned char *)buf
;
939 /* put in the header */
941 ubuf
[0] = dgram
->header
.msg_type
;
942 ubuf
[1] = (((int)dgram
->header
.flags
.node_type
)<<2);
943 if (dgram
->header
.flags
.more
)
945 if (dgram
->header
.flags
.first
)
947 RSSVAL(ubuf
,2,dgram
->header
.dgm_id
);
948 putip(ubuf
+4,(char *)&dgram
->header
.source_ip
);
949 RSSVAL(ubuf
,8,dgram
->header
.source_port
);
950 RSSVAL(ubuf
,12,dgram
->header
.packet_offset
);
955 if (dgram
->header
.msg_type
== 0x10 ||
956 dgram
->header
.msg_type
== 0x11 ||
957 dgram
->header
.msg_type
== 0x12) {
958 offset
+= put_nmb_name((char *)ubuf
,len
,offset
,&dgram
->source_name
);
959 offset
+= put_nmb_name((char *)ubuf
,len
,offset
,&dgram
->dest_name
);
963 memcpy(ubuf
+offset
,dgram
->data
,dgram
->datasize
);
965 offset
+= dgram
->datasize
;
967 /* automatically set the dgm_length
968 * NOTE: RFC1002 says the dgm_length does *not*
969 * include the fourteen-byte header. crh
971 dgram
->header
.dgm_length
= (offset
- 14);
973 RSSVAL(ubuf
,10,dgram
->header
.dgm_length
);
979 /*******************************************************************
981 *******************************************************************/
983 void make_nmb_name( struct nmb_name
*n
, const char *name
, int type
)
986 memset( (char *)n
, '\0', sizeof(struct nmb_name
) );
987 fstrcpy(unix_name
, name
);
988 (void)strupper_m(unix_name
);
989 push_ascii(n
->name
, unix_name
, sizeof(n
->name
), STR_TERMINATE
);
990 n
->name_type
= (unsigned int)type
& 0xFF;
991 push_ascii(n
->scope
, lp_netbios_scope(), 64, STR_TERMINATE
);
994 /*******************************************************************
995 Compare two nmb names
996 ******************************************************************/
998 bool nmb_name_equal(struct nmb_name
*n1
, struct nmb_name
*n2
)
1000 return ((n1
->name_type
== n2
->name_type
) &&
1001 strequal(n1
->name
,n2
->name
) &&
1002 strequal(n1
->scope
,n2
->scope
));
1005 /*******************************************************************
1006 Build a nmb packet ready for sending.
1007 If buf == NULL this is a length calculation.
1008 ******************************************************************/
1010 static int build_nmb(char *buf
, size_t len
, struct nmb_packet
*nmb
)
1012 unsigned char *ubuf
= (unsigned char *)buf
;
1015 if (len
&& len
< 12) {
1019 /* put in the header */
1021 RSSVAL(ubuf
,offset
,nmb
->header
.name_trn_id
);
1022 ubuf
[offset
+2] = (nmb
->header
.opcode
& 0xF) << 3;
1023 if (nmb
->header
.response
)
1024 ubuf
[offset
+2] |= (1<<7);
1025 if (nmb
->header
.nm_flags
.authoritative
&&
1026 nmb
->header
.response
)
1027 ubuf
[offset
+2] |= 0x4;
1028 if (nmb
->header
.nm_flags
.trunc
)
1029 ubuf
[offset
+2] |= 0x2;
1030 if (nmb
->header
.nm_flags
.recursion_desired
)
1031 ubuf
[offset
+2] |= 0x1;
1032 if (nmb
->header
.nm_flags
.recursion_available
&&
1033 nmb
->header
.response
)
1034 ubuf
[offset
+3] |= 0x80;
1035 if (nmb
->header
.nm_flags
.bcast
)
1036 ubuf
[offset
+3] |= 0x10;
1037 ubuf
[offset
+3] |= (nmb
->header
.rcode
& 0xF);
1039 RSSVAL(ubuf
,offset
+4,nmb
->header
.qdcount
);
1040 RSSVAL(ubuf
,offset
+6,nmb
->header
.ancount
);
1041 RSSVAL(ubuf
,offset
+8,nmb
->header
.nscount
);
1042 RSSVAL(ubuf
,offset
+10,nmb
->header
.arcount
);
1046 if (nmb
->header
.qdcount
) {
1047 /* XXXX this doesn't handle a qdcount of > 1 */
1050 int extra
= put_nmb_name(NULL
,0,offset
,
1051 &nmb
->question
.question_name
);
1052 if (offset
+ extra
> len
) {
1056 offset
+= put_nmb_name((char *)ubuf
,len
,offset
,
1057 &nmb
->question
.question_name
);
1059 RSSVAL(ubuf
,offset
,nmb
->question
.question_type
);
1060 RSSVAL(ubuf
,offset
+2,nmb
->question
.question_class
);
1065 if (nmb
->header
.ancount
) {
1068 int extra
= put_res_rec(NULL
,0,offset
,nmb
->answers
,
1069 nmb
->header
.ancount
);
1070 if (offset
+ extra
> len
) {
1074 offset
+= put_res_rec((char *)ubuf
,len
,offset
,nmb
->answers
,
1075 nmb
->header
.ancount
);
1078 if (nmb
->header
.nscount
) {
1081 int extra
= put_res_rec(NULL
,0,offset
,nmb
->nsrecs
,
1082 nmb
->header
.nscount
);
1083 if (offset
+ extra
> len
) {
1087 offset
+= put_res_rec((char *)ubuf
,len
,offset
,nmb
->nsrecs
,
1088 nmb
->header
.nscount
);
1092 * The spec says we must put compressed name pointers
1093 * in the following outgoing packets :
1094 * NAME_REGISTRATION_REQUEST, NAME_REFRESH_REQUEST,
1095 * NAME_RELEASE_REQUEST.
1098 if((nmb
->header
.response
== False
) &&
1099 ((nmb
->header
.opcode
== NMB_NAME_REG_OPCODE
) ||
1100 (nmb
->header
.opcode
== NMB_NAME_RELEASE_OPCODE
) ||
1101 (nmb
->header
.opcode
== NMB_NAME_REFRESH_OPCODE_8
) ||
1102 (nmb
->header
.opcode
== NMB_NAME_REFRESH_OPCODE_9
) ||
1103 (nmb
->header
.opcode
== NMB_NAME_MULTIHOMED_REG_OPCODE
)) &&
1104 (nmb
->header
.arcount
== 1)) {
1108 int extra
= put_compressed_name_ptr(NULL
,offset
,
1109 nmb
->additional
,12);
1110 if (offset
+ extra
> len
) {
1114 offset
+= put_compressed_name_ptr(ubuf
,offset
,
1115 nmb
->additional
,12);
1116 } else if (nmb
->header
.arcount
) {
1119 int extra
= put_res_rec(NULL
,0,offset
,nmb
->additional
,
1120 nmb
->header
.arcount
);
1121 if (offset
+ extra
> len
) {
1125 offset
+= put_res_rec((char *)ubuf
,len
,offset
,nmb
->additional
,
1126 nmb
->header
.arcount
);
1131 /*******************************************************************
1133 ******************************************************************/
1135 int build_packet(char *buf
, size_t buflen
, struct packet_struct
*p
)
1139 switch (p
->packet_type
) {
1141 len
= build_nmb(buf
,buflen
,&p
->packet
.nmb
);
1145 len
= build_dgram(buf
,buflen
,&p
->packet
.dgram
);
1152 /*******************************************************************
1153 Send a packet_struct.
1154 ******************************************************************/
1156 bool send_packet(struct packet_struct
*p
)
1161 memset(buf
,'\0',sizeof(buf
));
1163 len
= build_packet(buf
, sizeof(buf
), p
);
1168 return(send_udp(p
->send_fd
,buf
,len
,p
->ip
,p
->port
));
1171 /****************************************************************************
1172 Receive a UDP/138 packet either via UDP or from the unexpected packet
1173 queue. The packet must be a reply packet and have the specified mailslot name
1174 The timeout is in milliseconds.
1175 ***************************************************************************/
1177 /****************************************************************************
1178 See if a datagram has the right mailslot name.
1179 ***************************************************************************/
1181 bool match_mailslot_name(struct packet_struct
*p
, const char *mailslot_name
)
1183 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
1186 buf
= &dgram
->data
[0];
1191 if (memcmp(buf
, mailslot_name
, strlen(mailslot_name
)+1) == 0) {
1198 /****************************************************************************
1199 Return the number of bits that match between two len character buffers
1200 ***************************************************************************/
1202 int matching_len_bits(const unsigned char *p1
, const unsigned char *p2
, size_t len
)
1206 for (i
=0; i
<len
; i
++) {
1215 for (j
=0; j
<8; j
++) {
1216 if ((p1
[i
] & (1<<(7-j
))) != (p2
[i
] & (1<<(7-j
))))
1224 static unsigned char sort_ip
[4];
1226 /****************************************************************************
1227 Compare two query reply records.
1228 ***************************************************************************/
1230 static int name_query_comp(unsigned char *p1
, unsigned char *p2
)
1232 return matching_len_bits(p2
+2, sort_ip
, 4) -
1233 matching_len_bits(p1
+2, sort_ip
, 4);
1236 /****************************************************************************
1237 Sort a set of 6 byte name query response records so that the IPs that
1238 have the most leading bits in common with the specified address come first.
1239 ***************************************************************************/
1241 void sort_query_replies(char *data
, int n
, struct in_addr ip
)
1246 putip(sort_ip
, (char *)&ip
);
1249 this can't use TYPESAFE_QSORT() as the types are wrong.
1250 It should be fixed to use a real type instead of char*
1252 qsort(data
, n
, 6, QSORT_CAST name_query_comp
);
1255 /****************************************************************************
1256 Interpret the weird netbios "name" into a unix fstring. Return the name type.
1257 Returns -1 on error.
1258 ****************************************************************************/
1260 static int name_interpret(unsigned char *buf
, size_t buf_len
,
1261 unsigned char *in
, fstring name
)
1263 unsigned char *end_ptr
= buf
+ buf_len
;
1267 unsigned char *out
= (unsigned char *)out_string
;
1271 if (in
>= end_ptr
) {
1281 if (&in
[1] >= end_ptr
) {
1284 if (in
[0] < 'A' || in
[0] > 'P' || in
[1] < 'A' || in
[1] > 'P') {
1288 *out
= ((in
[0]-'A')<<4) + (in
[1]-'A');
1291 if (PTR_DIFF(out
,out_string
) >= sizeof(fstring
)) {
1298 pull_ascii_fstring(name
, out_string
);
1303 /****************************************************************************
1304 Mangle a name into netbios format.
1305 Note: <Out> must be (33 + strlen(scope) + 2) bytes long, at minimum.
1306 ****************************************************************************/
1308 char *name_mangle(TALLOC_CTX
*mem_ctx
, const char *In
, char name_type
)
1316 result
= talloc_array(mem_ctx
, char, 33 + strlen(lp_netbios_scope()) + 2);
1317 if (result
== NULL
) {
1322 /* Safely copy the input string, In, into buf[]. */
1323 if (strcmp(In
,"*") == 0)
1324 put_name(buf
, "*", '\0', 0x00);
1326 /* We use an fstring here as mb dos names can expend x3 when
1331 pull_ascii_fstring(buf_unix
, In
);
1332 if (!strupper_m(buf_unix
)) {
1336 push_ascii_nstring(buf_dos
, buf_unix
);
1337 put_name(buf
, buf_dos
, ' ', name_type
);
1340 /* Place the length of the first field into the output buffer. */
1344 /* Now convert the name to the rfc1001/1002 format. */
1345 for( i
= 0; i
< MAX_NETBIOSNAME_LEN
; i
++ ) {
1346 p
[i
*2] = ( (buf
[i
] >> 4) & 0x000F ) + 'A';
1347 p
[(i
*2)+1] = (buf
[i
] & 0x000F) + 'A';
1352 /* Add the scope string. */
1353 for( i
= 0, len
= 0; *(lp_netbios_scope()) != '\0'; i
++, len
++ ) {
1354 switch( (lp_netbios_scope())[i
] ) {
1366 p
[len
+1] = (lp_netbios_scope())[i
];
1374 /****************************************************************************
1375 Find a pointer to a netbios name.
1376 ****************************************************************************/
1378 static unsigned char *name_ptr(unsigned char *buf
, size_t buf_len
, unsigned int ofs
)
1380 unsigned char c
= 0;
1382 if (ofs
> buf_len
|| buf_len
< 1) {
1386 c
= *(unsigned char *)(buf
+ofs
);
1387 if ((c
& 0xC0) == 0xC0) {
1390 if (ofs
> buf_len
- 1) {
1393 l
= RSVAL(buf
, ofs
) & 0x3FFF;
1397 DEBUG(5,("name ptr to pos %d from %d is %s\n",l
,ofs
,buf
+l
));
1404 /****************************************************************************
1405 Extract a netbios name from a buf (into a unix string) return name type.
1406 Returns -1 on error.
1407 ****************************************************************************/
1409 int name_extract(unsigned char *buf
, size_t buf_len
, unsigned int ofs
, fstring name
)
1411 unsigned char *p
= name_ptr(buf
,buf_len
,ofs
);
1417 return(name_interpret(buf
,buf_len
,p
,name
));
1420 /****************************************************************************
1421 Return the total storage length of a mangled name.
1422 Returns -1 on error.
1423 ****************************************************************************/
1425 int name_len(unsigned char *s1
, size_t buf_len
)
1427 /* NOTE: this argument _must_ be unsigned */
1428 unsigned char *s
= (unsigned char *)s1
;
1434 /* If the two high bits of the byte are set, return 2. */
1435 if (0xC0 == (*s
& 0xC0)) {
1442 /* Add up the length bytes. */
1443 for (len
= 1; (*s
); s
+= (*s
) + 1) {
1445 if (len
> buf_len
) {
1453 /*******************************************************************
1454 Setup the word count and byte count for a client smb message.
1455 ********************************************************************/
1457 int cli_set_message(char *buf
,int num_words
,int num_bytes
,bool zero
)
1459 if (zero
&& (num_words
|| num_bytes
)) {
1460 memset(buf
+ smb_size
,'\0',num_words
*2 + num_bytes
);
1462 SCVAL(buf
,smb_wct
,num_words
);
1463 SSVAL(buf
,smb_vwv
+ num_words
*SIZEOFWORD
,num_bytes
);
1464 smb_setlen(buf
,smb_size
+ num_words
*2 + num_bytes
- 4);
1465 return (smb_size
+ num_words
*2 + num_bytes
);