006cb5f5eed4cd953bd12380dadc32f76dba8606
[kai/samba.git] / source3 / libsmb / nmblib.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios library routines
5    Copyright (C) Andrew Tridgell 1994-1998
6    
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 2 of the License, or
10    (at your option) any later version.
11    
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.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #include "includes.h"
24
25 extern int DEBUGLEVEL;
26
27 int num_good_sends = 0;
28 int num_good_receives = 0;
29
30 static struct opcode_names {
31         char *nmb_opcode_name;
32         int opcode;
33 } nmb_header_opcode_names[] = {
34       {"Query",           0 },
35       {"Registration",      5 },
36       {"Release",           6 },
37       {"WACK",              7 },
38       {"Refresh",           8 },
39       {"Refresh(altcode)",  9 },
40       {"Multi-homed Registration", 15 },
41       {0, -1 }
42 };
43
44 /****************************************************************************
45  * Lookup a nmb opcode name.
46  ****************************************************************************/
47 static char *lookup_opcode_name( int opcode )
48 {
49   struct opcode_names *op_namep;
50   int i;
51
52   for(i = 0; nmb_header_opcode_names[i].nmb_opcode_name != 0; i++) {
53     op_namep = &nmb_header_opcode_names[i];
54     if(opcode == op_namep->opcode)
55       return op_namep->nmb_opcode_name;
56   }
57   return "<unknown opcode>";
58 }
59
60 /****************************************************************************
61   print out a res_rec structure
62   ****************************************************************************/
63 static void debug_nmb_res_rec(struct res_rec *res, char *hdr)
64 {
65   int i, j;
66
67   DEBUGADD( 4, ( "    %s: nmb_name=%s rr_type=%d rr_class=%d ttl=%d\n",
68                  hdr,
69                  nmb_namestr(&res->rr_name),
70                  res->rr_type,
71                  res->rr_class,
72                  res->ttl ) );
73
74   if( res->rdlength == 0 || res->rdata == NULL )
75     return;
76
77   for (i = 0; i < res->rdlength; i+= 16)
78     {
79       DEBUGADD(4, ("    %s %3x char ", hdr, i));
80
81       for (j = 0; j < 16; j++)
82         {
83           unsigned char x = res->rdata[i+j];
84           if (x < 32 || x > 127) x = '.';
85           
86           if (i+j >= res->rdlength) break;
87           DEBUGADD(4, ("%c", x));
88         }
89       
90       DEBUGADD(4, ("   hex "));
91
92       for (j = 0; j < 16; j++)
93         {
94           if (i+j >= res->rdlength) break;
95           DEBUGADD(4, ("%02X", (unsigned char)res->rdata[i+j]));
96         }
97       
98       DEBUGADD(4, ("\n"));
99     }
100 }
101
102 /****************************************************************************
103   process a nmb packet
104   ****************************************************************************/
105 void debug_nmb_packet(struct packet_struct *p)
106 {
107   struct nmb_packet *nmb = &p->packet.nmb;
108
109   if( DEBUGLVL( 4 ) )
110     {
111     dbgtext( "nmb packet from %s(%d) header: id=%d 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),
115              nmb->header.opcode,
116              BOOLSTR(nmb->header.response) );
117     dbgtext( "    header: flags: bcast=%s rec_avail=%s rec_des=%s trunc=%s auth=%s\n",
118              BOOLSTR(nmb->header.nm_flags.bcast),
119              BOOLSTR(nmb->header.nm_flags.recursion_available),
120              BOOLSTR(nmb->header.nm_flags.recursion_desired),
121              BOOLSTR(nmb->header.nm_flags.trunc),
122              BOOLSTR(nmb->header.nm_flags.authoritative) );
123     dbgtext( "    header: rcode=%d qdcount=%d ancount=%d nscount=%d arcount=%d\n",
124              nmb->header.rcode,
125              nmb->header.qdcount,
126              nmb->header.ancount,
127              nmb->header.nscount,
128              nmb->header.arcount );
129     }
130
131   if (nmb->header.qdcount)
132     {
133       DEBUGADD( 4, ( "    question: q_name=%s q_type=%d q_class=%d\n",
134                      nmb_namestr(&nmb->question.question_name),
135                      nmb->question.question_type,
136                      nmb->question.question_class) );
137     }
138
139   if (nmb->answers && nmb->header.ancount)
140     {
141       debug_nmb_res_rec(nmb->answers,"answers");
142     }
143   if (nmb->nsrecs && nmb->header.nscount)
144     {
145       debug_nmb_res_rec(nmb->nsrecs,"nsrecs");
146     }
147   if (nmb->additional && nmb->header.arcount)
148     {
149       debug_nmb_res_rec(nmb->additional,"additional");
150     }
151 }
152
153 /*******************************************************************
154   handle "compressed" name pointers
155   ******************************************************************/
156 static BOOL handle_name_ptrs(unsigned char *ubuf,int *offset,int length,
157                              BOOL *got_pointer,int *ret)
158 {
159   int loop_count=0;
160   
161   while ((ubuf[*offset] & 0xC0) == 0xC0) {
162     if (!*got_pointer) (*ret) += 2;
163     (*got_pointer)=True;
164     (*offset) = ((ubuf[*offset] & ~0xC0)<<8) | ubuf[(*offset)+1];
165     if (loop_count++ == 10 || (*offset) < 0 || (*offset)>(length-2)) {
166       return(False);
167     }
168   }
169   return(True);
170 }
171
172 /*******************************************************************
173   parse a nmb name from "compressed" format to something readable
174   return the space taken by the name, or 0 if the name is invalid
175   ******************************************************************/
176 static int parse_nmb_name(char *inbuf,int offset,int length, struct nmb_name *name)
177 {
178   int m,n=0;
179   unsigned char *ubuf = (unsigned char *)inbuf;
180   int ret = 0;
181   BOOL got_pointer=False;
182
183   if (length - offset < 2) return(0);  
184
185   /* handle initial name pointers */
186   if (!handle_name_ptrs(ubuf,&offset,length,&got_pointer,&ret)) return(0);
187   
188   m = ubuf[offset];
189
190   if (!m) return(0);
191   if ((m & 0xC0) || offset+m+2 > length) return(0);
192
193   bzero((char *)name,sizeof(*name));
194
195   /* the "compressed" part */
196   if (!got_pointer) ret += m + 2;
197   offset++;
198   while (m) {
199     unsigned char c1,c2;
200     c1 = ubuf[offset++]-'A';
201     c2 = ubuf[offset++]-'A';
202     if ((c1 & 0xF0) || (c2 & 0xF0) || (n > sizeof(name->name)-1)) return(0);
203     name->name[n++] = (c1<<4) | c2;
204     m -= 2;
205   }
206   name->name[n] = 0;
207
208   if (n==16) {
209     /* parse out the name type, 
210        its always in the 16th byte of the name */
211     name->name_type = ((unsigned char)name->name[15]) & 0xff;
212   
213     /* remove trailing spaces */
214     name->name[15] = 0;
215     n = 14;
216     while (n && name->name[n]==' ') name->name[n--] = 0;  
217   }
218
219   /* now the domain parts (if any) */
220   n = 0;
221   while (ubuf[offset]) {
222     /* we can have pointers within the domain part as well */
223     if (!handle_name_ptrs(ubuf,&offset,length,&got_pointer,&ret)) return(0);
224
225     m = ubuf[offset];
226     if (!got_pointer) ret += m+1;
227     if (n) name->scope[n++] = '.';
228     if (m+2+offset>length || n+m+1>sizeof(name->scope)) return(0);
229     offset++;
230     while (m--) name->scope[n++] = (char)ubuf[offset++];
231   }
232   name->scope[n++] = 0;  
233
234   return(ret);
235 }
236
237
238 /*******************************************************************
239   put a compressed nmb name into a buffer. return the length of the
240   compressed name
241
242   compressed names are really weird. The "compression" doubles the
243   size. The idea is that it also means that compressed names conform
244   to the doman name system. See RFC1002.
245   ******************************************************************/
246 static int put_nmb_name(char *buf,int offset,struct nmb_name *name)
247 {
248   int ret,m;
249   fstring buf1;
250   char *p;
251
252   if (strcmp(name->name,"*") == 0) {
253     /* special case for wildcard name */
254     bzero(buf1,20);
255     buf1[0] = '*';
256     buf1[15] = name->name_type;
257   } else {
258     slprintf(buf1, sizeof(buf1) - 1,"%-15.15s%c",name->name,name->name_type);
259   }
260
261   buf[offset] = 0x20;
262
263   ret = 34;
264
265   for (m=0;m<16;m++) {
266     buf[offset+1+2*m] = 'A' + ((buf1[m]>>4)&0xF);
267     buf[offset+2+2*m] = 'A' + (buf1[m]&0xF);
268   }
269   offset += 33;
270
271   buf[offset] = 0;
272
273   if (name->scope[0]) {
274     /* XXXX this scope handling needs testing */
275     ret += strlen(name->scope) + 1;
276     pstrcpy(&buf[offset+1],name->scope);  
277   
278     p = &buf[offset+1];
279     while ((p = strchr(p,'.'))) {
280       buf[offset] = PTR_DIFF(p,&buf[offset]);
281       offset += buf[offset];
282       p = &buf[offset+1];
283     }
284     buf[offset] = strlen(&buf[offset+1]);
285   }
286
287   return(ret);
288 }
289
290
291 /*******************************************************************
292   useful for debugging messages
293   ******************************************************************/
294 char *nmb_namestr(struct nmb_name *n)
295 {
296   static int i=0;
297   static fstring ret[4];
298   char *p = ret[i];
299
300   nmb_safe_namestr(n, p, sizeof(fstring));
301
302   i = (i+1)%4;
303   return(p);
304 }
305
306 /*******************************************************************
307   useful for debugging messages
308   ******************************************************************/
309 void nmb_safe_namestr(struct nmb_name *n, char *str, size_t len)
310 {
311   if (!n->scope[0])
312     slprintf(str, len-1, "%s<%02x>",n->name,n->name_type);
313   else
314     slprintf(str, len-1, "%s<%02x>.%s",n->name,n->name_type,n->scope);
315 }
316
317 /*******************************************************************
318   allocate and parse some resource records
319   ******************************************************************/
320 static BOOL parse_alloc_res_rec(char *inbuf,int *offset,int length,
321                                 struct res_rec **recs, int count)
322 {
323   int i;
324   *recs = (struct res_rec *)malloc(sizeof(**recs)*count);
325   if (!*recs) return(False);
326
327   bzero(*recs,sizeof(**recs)*count);
328
329   for (i=0;i<count;i++) {
330     int l = parse_nmb_name(inbuf,*offset,length,&(*recs)[i].rr_name);
331     (*offset) += l;
332     if (!l || (*offset)+10 > length) {
333       free(*recs);
334       return(False);
335     }
336     (*recs)[i].rr_type = RSVAL(inbuf,(*offset));
337     (*recs)[i].rr_class = RSVAL(inbuf,(*offset)+2);
338     (*recs)[i].ttl = RIVAL(inbuf,(*offset)+4);
339     (*recs)[i].rdlength = RSVAL(inbuf,(*offset)+8);
340     (*offset) += 10;
341     if ((*recs)[i].rdlength>sizeof((*recs)[i].rdata) || 
342         (*offset)+(*recs)[i].rdlength > length) {
343       free(*recs);
344       return(False);
345     }
346     memcpy((*recs)[i].rdata,inbuf+(*offset),(*recs)[i].rdlength);
347     (*offset) += (*recs)[i].rdlength;    
348   }
349   return(True);
350 }
351
352 /*******************************************************************
353   put a resource record into a packet
354   ******************************************************************/
355 static int put_res_rec(char *buf,int offset,struct res_rec *recs,int count)
356 {
357   int ret=0;
358   int i;
359
360   for (i=0;i<count;i++) {
361     int l = put_nmb_name(buf,offset,&recs[i].rr_name);
362     offset += l;
363     ret += l;
364     RSSVAL(buf,offset,recs[i].rr_type);
365     RSSVAL(buf,offset+2,recs[i].rr_class);
366     RSIVAL(buf,offset+4,recs[i].ttl);
367     RSSVAL(buf,offset+8,recs[i].rdlength);
368     memcpy(buf+offset+10,recs[i].rdata,recs[i].rdlength);
369     offset += 10+recs[i].rdlength;
370     ret += 10+recs[i].rdlength;
371   }
372
373   return(ret);
374 }
375
376 /*******************************************************************
377   put a compressed name pointer record into a packet
378   ******************************************************************/
379 static int put_compressed_name_ptr(unsigned char *buf,int offset,struct res_rec *rec,int ptr_offset)
380 {  
381   int ret=0;
382   buf[offset] = (0xC0 | ((ptr_offset >> 8) & 0xFF));
383   buf[offset+1] = (ptr_offset & 0xFF);
384   offset += 2;
385   ret += 2;
386   RSSVAL(buf,offset,rec->rr_type);
387   RSSVAL(buf,offset+2,rec->rr_class);
388   RSIVAL(buf,offset+4,rec->ttl);
389   RSSVAL(buf,offset+8,rec->rdlength);
390   memcpy(buf+offset+10,rec->rdata,rec->rdlength);
391   offset += 10+rec->rdlength;
392   ret += 10+rec->rdlength;
393     
394   return(ret);
395 }
396
397 /*******************************************************************
398   parse a dgram packet. Return False if the packet can't be parsed 
399   or is invalid for some reason, True otherwise 
400
401   this is documented in section 4.4.1 of RFC1002
402   ******************************************************************/
403 static BOOL parse_dgram(char *inbuf,int length,struct dgram_packet *dgram)
404 {
405   int offset;
406   int flags;
407
408   bzero((char *)dgram,sizeof(*dgram));
409
410   if (length < 14) return(False);
411
412   dgram->header.msg_type = CVAL(inbuf,0);
413   flags = CVAL(inbuf,1);
414   dgram->header.flags.node_type = (enum node_type)((flags>>2)&3);
415   if (flags & 1) dgram->header.flags.more = True;
416   if (flags & 2) dgram->header.flags.first = True;
417   dgram->header.dgm_id = RSVAL(inbuf,2);
418   putip((char *)&dgram->header.source_ip,inbuf+4);
419   dgram->header.source_port = RSVAL(inbuf,8);
420   dgram->header.dgm_length = RSVAL(inbuf,10);
421   dgram->header.packet_offset = RSVAL(inbuf,12);
422
423   offset = 14;
424
425   if (dgram->header.msg_type == 0x10 ||
426       dgram->header.msg_type == 0x11 ||
427       dgram->header.msg_type == 0x12) {      
428     offset += parse_nmb_name(inbuf,offset,length,&dgram->source_name);
429     offset += parse_nmb_name(inbuf,offset,length,&dgram->dest_name);
430   }
431
432   if (offset >= length || (length-offset > sizeof(dgram->data))) 
433     return(False);
434
435   dgram->datasize = length-offset;
436   memcpy(dgram->data,inbuf+offset,dgram->datasize);
437
438   return(True);
439 }
440
441
442 /*******************************************************************
443   parse a nmb packet. Return False if the packet can't be parsed 
444   or is invalid for some reason, True otherwise 
445   ******************************************************************/
446 static BOOL parse_nmb(char *inbuf,int length,struct nmb_packet *nmb)
447 {
448   int nm_flags,offset;
449
450   bzero((char *)nmb,sizeof(*nmb));
451
452   if (length < 12) return(False);
453
454   /* parse the header */
455   nmb->header.name_trn_id = RSVAL(inbuf,0);
456
457   DEBUG(10,("parse_nmb: packet id = %d\n", nmb->header.name_trn_id));
458
459   nmb->header.opcode = (CVAL(inbuf,2) >> 3) & 0xF;
460   nmb->header.response = ((CVAL(inbuf,2)>>7)&1)?True:False;
461   nm_flags = ((CVAL(inbuf,2) & 0x7) << 4) + (CVAL(inbuf,3)>>4);
462   nmb->header.nm_flags.bcast = (nm_flags&1)?True:False;
463   nmb->header.nm_flags.recursion_available = (nm_flags&8)?True:False;
464   nmb->header.nm_flags.recursion_desired = (nm_flags&0x10)?True:False;
465   nmb->header.nm_flags.trunc = (nm_flags&0x20)?True:False;
466   nmb->header.nm_flags.authoritative = (nm_flags&0x40)?True:False;  
467   nmb->header.rcode = CVAL(inbuf,3) & 0xF;
468   nmb->header.qdcount = RSVAL(inbuf,4);
469   nmb->header.ancount = RSVAL(inbuf,6);
470   nmb->header.nscount = RSVAL(inbuf,8);
471   nmb->header.arcount = RSVAL(inbuf,10);
472   
473   if (nmb->header.qdcount) {
474     offset = parse_nmb_name(inbuf,12,length,&nmb->question.question_name);
475     if (!offset) return(False);
476
477     if (length - (12+offset) < 4) return(False);
478     nmb->question.question_type = RSVAL(inbuf,12+offset);
479     nmb->question.question_class = RSVAL(inbuf,12+offset+2);
480
481     offset += 12+4;
482   } else {
483     offset = 12;
484   }
485
486   /* and any resource records */
487   if (nmb->header.ancount && 
488       !parse_alloc_res_rec(inbuf,&offset,length,&nmb->answers,
489                            nmb->header.ancount))
490     return(False);
491
492   if (nmb->header.nscount && 
493       !parse_alloc_res_rec(inbuf,&offset,length,&nmb->nsrecs,
494                            nmb->header.nscount))
495     return(False);
496   
497   if (nmb->header.arcount && 
498       !parse_alloc_res_rec(inbuf,&offset,length,&nmb->additional,
499                            nmb->header.arcount))
500     return(False);
501
502   return(True);
503 }
504
505 /*******************************************************************
506   'Copy constructor' for an nmb packet
507   ******************************************************************/
508 static struct packet_struct *copy_nmb_packet(struct packet_struct *packet)
509 {  
510   struct nmb_packet *nmb;
511   struct nmb_packet *copy_nmb;
512   struct packet_struct *pkt_copy;
513
514   if(( pkt_copy = (struct packet_struct *)malloc(sizeof(*packet))) == NULL)
515   {
516     DEBUG(0,("copy_nmb_packet: malloc fail.\n"));
517     return NULL;
518   }
519
520   /* Structure copy of entire thing. */
521
522   *pkt_copy = *packet;
523
524   /* Ensure this copy is not locked. */
525   pkt_copy->locked = False;
526
527   /* Ensure this copy has no resource records. */
528   nmb = &packet->packet.nmb;
529   copy_nmb = &pkt_copy->packet.nmb;
530
531   copy_nmb->answers = NULL;
532   copy_nmb->nsrecs = NULL;
533   copy_nmb->additional = NULL;
534
535   /* Now copy any resource records. */
536
537   if (nmb->answers)
538   {
539     if((copy_nmb->answers = (struct res_rec *)
540                   malloc(nmb->header.ancount * sizeof(struct res_rec))) == NULL)
541       goto free_and_exit;
542     memcpy((char *)copy_nmb->answers, (char *)nmb->answers, 
543            nmb->header.ancount * sizeof(struct res_rec));
544   }
545   if (nmb->nsrecs)
546   {
547     if((copy_nmb->nsrecs = (struct res_rec *)
548                   malloc(nmb->header.nscount * sizeof(struct res_rec))) == NULL)
549       goto free_and_exit;
550     memcpy((char *)copy_nmb->nsrecs, (char *)nmb->nsrecs, 
551            nmb->header.nscount * sizeof(struct res_rec));
552   }
553   if (nmb->additional)
554   {
555     if((copy_nmb->additional = (struct res_rec *)
556                   malloc(nmb->header.arcount * sizeof(struct res_rec))) == NULL)
557       goto free_and_exit;
558     memcpy((char *)copy_nmb->additional, (char *)nmb->additional, 
559            nmb->header.arcount * sizeof(struct res_rec));
560   }
561
562   return pkt_copy;
563
564 free_and_exit:
565
566   if(copy_nmb->answers)
567     free((char *)copy_nmb->answers);
568   if(copy_nmb->nsrecs)
569     free((char *)copy_nmb->nsrecs);
570   if(copy_nmb->additional)
571     free((char *)copy_nmb->additional);
572   free((char *)pkt_copy);
573
574   DEBUG(0,("copy_nmb_packet: malloc fail in resource records.\n"));
575   return NULL;
576 }
577
578 /*******************************************************************
579   'Copy constructor' for a dgram packet
580   ******************************************************************/
581 static struct packet_struct *copy_dgram_packet(struct packet_struct *packet)
582
583   struct packet_struct *pkt_copy;
584
585   if(( pkt_copy = (struct packet_struct *)malloc(sizeof(*packet))) == NULL)
586   {
587     DEBUG(0,("copy_dgram_packet: malloc fail.\n"));
588     return NULL;
589   }
590
591   /* Structure copy of entire thing. */
592
593   *pkt_copy = *packet;
594
595   /* Ensure this copy is not locked. */
596   pkt_copy->locked = False;
597
598   /* There are no additional pointers in a dgram packet,
599      we are finished. */
600   return pkt_copy;
601 }
602
603 /*******************************************************************
604   'Copy constructor' for a generic packet
605   ******************************************************************/
606 struct packet_struct *copy_packet(struct packet_struct *packet)
607 {  
608   if(packet->packet_type == NMB_PACKET)
609     return copy_nmb_packet(packet);
610   else if (packet->packet_type == DGRAM_PACKET)
611     return copy_dgram_packet(packet);
612   return NULL;
613 }
614  
615 /*******************************************************************
616   free up any resources associated with an nmb packet
617   ******************************************************************/
618 static void free_nmb_packet(struct nmb_packet *nmb)
619 {  
620   if (nmb->answers) free(nmb->answers);
621   if (nmb->nsrecs) free(nmb->nsrecs);
622   if (nmb->additional) free(nmb->additional);
623 }
624
625 /*******************************************************************
626   free up any resources associated with a dgram packet
627   ******************************************************************/
628 static void free_dgram_packet(struct dgram_packet *nmb)
629 {  
630   /* We have nothing to do for a dgram packet. */
631 }
632
633 /*******************************************************************
634   free up any resources associated with a packet
635   ******************************************************************/
636 void free_packet(struct packet_struct *packet)
637 {  
638   if (packet->locked) 
639     return;
640   if (packet->packet_type == NMB_PACKET)
641     free_nmb_packet(&packet->packet.nmb);
642   else if (packet->packet_type == DGRAM_PACKET)
643     free_dgram_packet(&packet->packet.dgram);
644   free(packet);
645 }
646
647 /*******************************************************************
648   read a packet from a socket and parse it, returning a packet ready
649   to be used or put on the queue. This assumes a UDP socket
650   ******************************************************************/
651 struct packet_struct *read_packet(int fd,enum packet_type packet_type)
652 {
653   extern struct in_addr lastip;
654         struct nmb_state con;
655   extern int lastport;
656   struct packet_struct *packet;
657   char buf[MAX_DGRAM_SIZE];
658   int length;
659   BOOL ok=False;
660   
661         if (packet_type == NMB_SOCK_PACKET || packet_type == DGRAM_SOCK_PACKET)
662         {
663                 uint16 trn_id = 0;
664                 if (!read_nmb_sock(fd, &con))
665                 {
666                         return False;
667                 }
668                 if (write(fd, &trn_id, sizeof(trn_id)) != sizeof(trn_id))
669                 {
670                         return False;
671                 }
672         }
673         
674         length = read_udp_socket(fd,buf,sizeof(buf));
675         
676         dump_data(100, buf, length);
677
678         if (packet_type == NMB_SOCK_PACKET || packet_type == DGRAM_SOCK_PACKET)
679         {
680                 uint16 trn_id = 0;
681                 if (write(fd, &trn_id, sizeof(trn_id)) != sizeof(trn_id))
682                 {
683                         return False;
684                 }
685         }
686         
687   if (length < MIN_DGRAM_SIZE) return(NULL);
688
689         if (packet_type == NMB_SOCK_PACKET || packet_type == DGRAM_SOCK_PACKET)
690         {
691                 lastip = con.ip;
692                 lastport = con.port;
693         }
694
695   packet = (struct packet_struct *)malloc(sizeof(*packet));
696   if (!packet) return(NULL);
697
698   packet->next = NULL;
699   packet->prev = NULL;
700   packet->ip = lastip;
701   packet->port = lastport;
702   packet->fd = fd;
703   packet->locked = False;
704   packet->timestamp = time(NULL);
705   packet->packet_type = packet_type;
706   switch (packet_type) 
707     {
708     case NMB_PACKET:
709     case NMB_SOCK_PACKET:
710       ok = parse_nmb(buf,length,&packet->packet.nmb);
711       break;
712
713     case DGRAM_PACKET:
714     case DGRAM_SOCK_PACKET:
715       ok = parse_dgram(buf,length,&packet->packet.dgram);
716       break;
717     }
718   if (!ok) {
719     DEBUG(10,("read_packet: discarding packet id = %d\n", 
720                  packet->packet.nmb.header.name_trn_id));
721     free(packet);
722     return(NULL);
723   }
724
725   num_good_receives++;
726
727   DEBUG(5,("Received a packet of len %d from (%s) port %d\n",
728             length, inet_ntoa(packet->ip), packet->port ) );
729
730   return(packet);
731 }
732                                          
733
734 /*******************************************************************
735   send a udp packet on a already open socket
736   ******************************************************************/
737 static BOOL send_udp(int fd,char *buf,int len,struct in_addr ip,int port)
738 {
739   BOOL ret;
740   struct sockaddr_in sock_out;
741
742   /* set the address and port */
743   bzero((char *)&sock_out,sizeof(sock_out));
744   putip((char *)&sock_out.sin_addr,(char *)&ip);
745   sock_out.sin_port = htons( port );
746   sock_out.sin_family = AF_INET;
747   
748   DEBUG( 5, ( "Sending a packet of len %d to (%s) on port %d\n",
749               len, inet_ntoa(ip), port ) );
750         
751   ret = (sendto(fd,buf,len,0,(struct sockaddr *)&sock_out,
752                 sizeof(sock_out)) >= 0);
753
754   if (!ret)
755     DEBUG(0,("Packet send failed to %s(%d) ERRNO=%s\n",
756              inet_ntoa(ip),port,strerror(errno)));
757
758   if (ret)
759     num_good_sends++;
760
761   return(ret);
762 }
763
764 /*******************************************************************
765   build a dgram packet ready for sending
766
767   XXXX This currently doesn't handle packets too big for one
768   datagram. It should split them and use the packet_offset, more and
769   first flags to handle the fragmentation. Yuck.
770   ******************************************************************/
771 static int build_dgram(char *buf,struct packet_struct *p)
772 {
773   struct dgram_packet *dgram = &p->packet.dgram;
774   unsigned char *ubuf = (unsigned char *)buf;
775   int offset=0;
776
777   /* put in the header */
778   ubuf[0] = dgram->header.msg_type;
779   ubuf[1] = (((unsigned int)dgram->header.flags.node_type)<<2);
780   if (dgram->header.flags.more) ubuf[1] |= 1;
781   if (dgram->header.flags.first) ubuf[1] |= 2;
782   RSSVAL(ubuf,2,dgram->header.dgm_id);
783   putip(ubuf+4,(char *)&dgram->header.source_ip);
784   RSSVAL(ubuf,8,dgram->header.source_port);
785   RSSVAL(ubuf,12,dgram->header.packet_offset);
786
787   offset = 14;
788
789   if (dgram->header.msg_type == 0x10 ||
790       dgram->header.msg_type == 0x11 ||
791       dgram->header.msg_type == 0x12) {      
792     offset += put_nmb_name((char *)ubuf,offset,&dgram->source_name);
793     offset += put_nmb_name((char *)ubuf,offset,&dgram->dest_name);
794   }
795
796   memcpy(ubuf+offset,dgram->data,dgram->datasize);
797   offset += dgram->datasize;
798
799   /* automatically set the dgm_length */
800   dgram->header.dgm_length = offset;
801   RSSVAL(ubuf,10,dgram->header.dgm_length); 
802
803   return(offset);
804 }
805
806 /*******************************************************************
807   build a nmb name
808  *******************************************************************/
809 void make_nmb_name( struct nmb_name *n, const char *name, int type, const char *this_scope )
810 {
811         memset( (char *)n, '\0', sizeof(struct nmb_name) );
812         StrnCpy( n->name, name, 15 );
813         strupper( n->name );
814         n->name_type = (unsigned int)type & 0xFF;
815         StrnCpy( n->scope, this_scope, 63 );
816         strupper( n->scope );
817 }
818
819 /*******************************************************************
820   Compare two nmb names
821   ******************************************************************/
822
823 BOOL nmb_name_equal(struct nmb_name *n1, struct nmb_name *n2)
824 {
825   return ((n1->name_type == n2->name_type) &&
826          strequal(n1->name ,n2->name ) &&
827          strequal(n1->scope,n2->scope));
828 }
829
830 /*******************************************************************
831   build a nmb packet ready for sending
832
833   XXXX this currently relies on not being passed something that expands
834   to a packet too big for the buffer. Eventually this should be
835   changed to set the trunc bit so the receiver can request the rest
836   via tcp (when that becomes supported)
837   ******************************************************************/
838 static int build_nmb(char *buf,struct packet_struct *p)
839 {
840   struct nmb_packet *nmb = &p->packet.nmb;
841   unsigned char *ubuf = (unsigned char *)buf;
842   int offset=0;
843
844   /* put in the header */
845   RSSVAL(ubuf,offset,nmb->header.name_trn_id);
846   ubuf[offset+2] = (nmb->header.opcode & 0xF) << 3;
847   if (nmb->header.response) ubuf[offset+2] |= (1<<7);
848   if (nmb->header.nm_flags.authoritative && 
849       nmb->header.response) ubuf[offset+2] |= 0x4;
850   if (nmb->header.nm_flags.trunc) ubuf[offset+2] |= 0x2;
851   if (nmb->header.nm_flags.recursion_desired) ubuf[offset+2] |= 0x1;
852   if (nmb->header.nm_flags.recursion_available &&
853       nmb->header.response) ubuf[offset+3] |= 0x80;
854   if (nmb->header.nm_flags.bcast) ubuf[offset+3] |= 0x10;
855   ubuf[offset+3] |= (nmb->header.rcode & 0xF);
856
857   RSSVAL(ubuf,offset+4,nmb->header.qdcount);
858   RSSVAL(ubuf,offset+6,nmb->header.ancount);
859   RSSVAL(ubuf,offset+8,nmb->header.nscount);
860   RSSVAL(ubuf,offset+10,nmb->header.arcount);
861   
862   offset += 12;
863   if (nmb->header.qdcount) {
864     /* XXXX this doesn't handle a qdcount of > 1 */
865     offset += put_nmb_name((char *)ubuf,offset,&nmb->question.question_name);
866     RSSVAL(ubuf,offset,nmb->question.question_type);
867     RSSVAL(ubuf,offset+2,nmb->question.question_class);
868     offset += 4;
869   }
870
871   if (nmb->header.ancount)
872     offset += put_res_rec((char *)ubuf,offset,nmb->answers,
873                           nmb->header.ancount);
874
875   if (nmb->header.nscount)
876     offset += put_res_rec((char *)ubuf,offset,nmb->nsrecs,
877                           nmb->header.nscount);
878
879   /*
880    * The spec says we must put compressed name pointers
881    * in the following outgoing packets :
882    * NAME_REGISTRATION_REQUEST, NAME_REFRESH_REQUEST,
883    * NAME_RELEASE_REQUEST.
884    */
885
886   if((nmb->header.response == False) &&
887      ((nmb->header.opcode == NMB_NAME_REG_OPCODE) ||
888       (nmb->header.opcode == NMB_NAME_RELEASE_OPCODE) ||
889       (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_8) ||
890       (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_9) ||
891       (nmb->header.opcode == NMB_NAME_MULTIHOMED_REG_OPCODE)) &&
892      (nmb->header.arcount == 1)) {
893
894     offset += put_compressed_name_ptr(ubuf,offset,nmb->additional,12);
895
896   } else if (nmb->header.arcount) {
897     offset += put_res_rec((char *)ubuf,offset,nmb->additional,
898                           nmb->header.arcount);  
899   }
900   return(offset);
901 }
902
903
904 /*******************************************************************
905   send a packet_struct
906   ******************************************************************/
907 BOOL send_packet(struct packet_struct *p)
908 {
909   char buf[1024];
910   int len=0;
911
912         DEBUG(100,("send_packet: %d %d\n", p->fd, p->packet_type));
913
914   bzero(buf,sizeof(buf));
915
916   switch (p->packet_type) 
917     {
918     case NMB_PACKET:
919     case NMB_SOCK_PACKET:
920       len = build_nmb(buf,p);
921       debug_nmb_packet(p);
922       break;
923
924     case DGRAM_PACKET:
925     case DGRAM_SOCK_PACKET:
926       len = build_dgram(buf,p);
927       break;
928     }
929
930   if (!len) return(False);
931
932   switch (p->packet_type) 
933     {
934     case DGRAM_PACKET:
935     case NMB_PACKET:
936           return(send_udp(p->fd,buf,len,p->ip,p->port));
937       break;
938
939     case NMB_SOCK_PACKET:
940     case DGRAM_SOCK_PACKET:
941         {
942                 fstring qbuf;
943                 struct nmb_state nmb;
944                 int qlen;
945                 uint16 trn_id;
946                 char *q = qbuf + 4;
947
948                 nmb.ip = p->ip;
949                 nmb.port = p->port;
950
951                 SSVAL(q, 0, 0);
952                 q += 2;
953                 SSVAL(q, 0, 0);
954                 q += 2;
955                 memcpy(q, &nmb, sizeof(nmb));
956                 q += sizeof(nmb);
957
958                 qlen = PTR_DIFF(q, qbuf);
959                 SIVAL(qbuf, 0, qlen);
960
961                 dump_data(100, qbuf, qlen);
962
963                   if (write(p->fd,qbuf,qlen) != qlen)
964                 {
965                         DEBUG(0,("send_packet: write hdr failed\n"));
966                         return False;
967                 }
968                 if (read(p->fd, &trn_id, sizeof(trn_id)) != sizeof(trn_id))
969                 {
970                         DEBUG(0,("send_packet: 1st ack failed\n"));
971                         return False;
972                 }
973                 if (write(p->fd,buf,len) != len)
974                 {
975                         DEBUG(0,("send_packet: write packet failed\n"));
976                         return False;
977                 }
978                 if (read(p->fd, &trn_id, sizeof(trn_id)) != sizeof(trn_id))
979                 {
980                         DEBUG(0,("send_packet: 2nd ack failed\n"));
981                         return False;
982                 }
983                 return True;
984         }
985     }
986
987         return False;
988 }
989
990 /****************************************************************************
991   receive a packet with timeout on a open UDP filedescriptor
992   The timeout is in milliseconds
993   ***************************************************************************/
994 struct packet_struct *receive_packet(int fd,enum packet_type type,int t)
995 {
996   fd_set fds;
997   struct timeval timeout;
998
999   FD_ZERO(&fds);
1000   FD_SET(fd,&fds);
1001   timeout.tv_sec = t/1000;
1002   timeout.tv_usec = 1000*(t%1000);
1003
1004         DEBUG(100,("receive_packet: %d %d\n", fd, type));
1005   sys_select(fd+1,&fds,NULL, &timeout);
1006
1007   if (FD_ISSET(fd,&fds)) 
1008     return(read_packet(fd,type));
1009
1010   return(NULL);
1011 }
1012
1013
1014 /****************************************************************************
1015 return the number of bits that match between two 4 character buffers
1016   ***************************************************************************/
1017 static int matching_bits(uchar *p1, uchar *p2)
1018 {
1019         int i, j, ret = 0;
1020         for (i=0; i<4; i++) {
1021                 if (p1[i] != p2[i]) break;
1022                 ret += 8;
1023         }
1024
1025         if (i==4) return ret;
1026
1027         for (j=0; j<8; j++) {
1028                 if ((p1[i] & (1<<(7-j))) != (p2[i] & (1<<(7-j)))) break;
1029                 ret++;
1030         }       
1031         
1032         return ret;
1033 }
1034
1035
1036 static uchar sort_ip[4];
1037
1038 /****************************************************************************
1039 compare two query reply records
1040   ***************************************************************************/
1041 static int name_query_comp(uchar *p1, uchar *p2)
1042 {
1043         return matching_bits(p2+2, sort_ip) - matching_bits(p1+2, sort_ip);
1044 }
1045
1046 /****************************************************************************
1047 sort a set of 6 byte name query response records so that the IPs that
1048 have the most leading bits in common with the specified address come first
1049   ***************************************************************************/
1050 void sort_query_replies(char *data, int n, struct in_addr ip)
1051 {
1052         if (n <= 1) return;
1053
1054         putip(sort_ip, (char *)&ip);
1055
1056         qsort(data, n, 6, QSORT_CAST name_query_comp);
1057 }
1058
1059 BOOL read_nmb_sock(int c, struct nmb_state *con)
1060 {
1061         fstring buf;
1062         char *p = buf;
1063         int rl;
1064         uint32 len;
1065         uint16 version;
1066         uint16 command;
1067
1068         ZERO_STRUCTP(con);
1069
1070         rl = read(c, &buf, sizeof(len));
1071
1072         if (rl < 0)
1073         {
1074                 DEBUG(0,("read_nmb_sock: error\n"));
1075                 return False;
1076         }
1077         if (rl != sizeof(len))
1078         {
1079                 DEBUG(0,("Unable to read length\n"));
1080                 dump_data(0, buf, sizeof(len));
1081                 return False;
1082         }
1083
1084         len = IVAL(buf, 0);
1085
1086         if (len > sizeof(buf))
1087         {
1088                 DEBUG(0,("length %d too long\n", len));
1089                 return False;
1090         }
1091
1092         rl = read(c, buf, len);
1093
1094         if (rl < 0)
1095         {
1096                 DEBUG(0,("Unable to read from connection\n"));
1097                 return False;
1098         }
1099         
1100 #ifdef DEBUG_PASSWORD
1101         dump_data(100, buf, rl);
1102 #endif
1103         version = SVAL(p, 0);
1104         p += 2;
1105         command = SVAL(p, 0);
1106         p += 2;
1107
1108         memcpy(con, p, sizeof(*con));
1109         p += sizeof(*con);
1110
1111         DEBUG(10,("read_nmb_sock: ip %s port: %d\n",
1112                    inet_ntoa(con->ip), con->port));
1113
1114         if (PTR_DIFF(p, buf) != rl)
1115         {
1116                 DEBUG(0,("Buffer size %d %d!\n",
1117                         PTR_DIFF(p, buf), rl));
1118                 return False;
1119         }
1120
1121         return True;
1122 }
1123
1124 int get_nmb_sock(void)
1125 {
1126         fstring path;
1127         slprintf(path, sizeof(path)-1, "/tmp/.nmb/agent");
1128
1129         return open_pipe_sock(path);
1130 }