nmblib.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. NBT netbios library routines
  5. 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.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include "includes.h"
  19. const char *unix_error_string (int error_num);
  20. extern int DEBUGLEVEL;
  21. int num_good_sends = 0;
  22. int num_good_receives = 0;
  23. static struct opcode_names {
  24. const char *nmb_opcode_name;
  25. int opcode;
  26. } const nmb_header_opcode_names[] = {
  27. {"Query", 0 },
  28. {"Registration", 5 },
  29. {"Release", 6 },
  30. {"WACK", 7 },
  31. {"Refresh", 8 },
  32. {"Refresh(altcode)", 9 },
  33. {"Multi-homed Registration", 15 },
  34. {0, -1 }
  35. };
  36. /****************************************************************************
  37. * Lookup a nmb opcode name.
  38. ****************************************************************************/
  39. static const char *lookup_opcode_name( int opcode )
  40. {
  41. const struct opcode_names *op_namep = nmb_header_opcode_names;
  42. while(op_namep->nmb_opcode_name) {
  43. if(opcode == op_namep->opcode)
  44. return op_namep->nmb_opcode_name;
  45. op_namep++;
  46. }
  47. return "<unknown opcode>";
  48. }
  49. /****************************************************************************
  50. print out a res_rec structure
  51. ****************************************************************************/
  52. static void debug_nmb_res_rec(struct res_rec *res, const char *hdr)
  53. {
  54. int i, j;
  55. DEBUGADD( 4, ( " %s: nmb_name=%s rr_type=%d rr_class=%d ttl=%d\n",
  56. hdr,
  57. nmb_namestr(&res->rr_name),
  58. res->rr_type,
  59. res->rr_class,
  60. res->ttl ) );
  61. if( res->rdlength == 0 || res->rdata == NULL )
  62. return;
  63. for (i = 0; i < res->rdlength; i+= 16)
  64. {
  65. DEBUGADD(4, (" %s %3x char ", hdr, i));
  66. for (j = 0; j < 16; j++)
  67. {
  68. unsigned char x = res->rdata[i+j];
  69. if (x < 32 || x > 127) x = '.';
  70. if (i+j >= res->rdlength) break;
  71. DEBUGADD(4, ("%c", x));
  72. }
  73. DEBUGADD(4, (" hex "));
  74. for (j = 0; j < 16; j++)
  75. {
  76. if (i+j >= res->rdlength) break;
  77. DEBUGADD(4, ("%02X", (unsigned char)res->rdata[i+j]));
  78. }
  79. DEBUGADD(4, ("\n"));
  80. }
  81. }
  82. /****************************************************************************
  83. process a nmb packet
  84. ****************************************************************************/
  85. void debug_nmb_packet(struct packet_struct *p)
  86. {
  87. struct nmb_packet *nmb = &p->packet.nmb;
  88. if( DEBUGLVL( 4 ) )
  89. {
  90. dbgtext( "nmb packet from %s(%d) header: id=%d opcode=%s(%d) response=%s\n",
  91. inet_ntoa(p->ip), p->port,
  92. nmb->header.name_trn_id,
  93. lookup_opcode_name(nmb->header.opcode),
  94. nmb->header.opcode,
  95. BOOLSTR(nmb->header.response) );
  96. dbgtext( " header: flags: bcast=%s rec_avail=%s rec_des=%s trunc=%s auth=%s\n",
  97. BOOLSTR(nmb->header.nm_flags.bcast),
  98. BOOLSTR(nmb->header.nm_flags.recursion_available),
  99. BOOLSTR(nmb->header.nm_flags.recursion_desired),
  100. BOOLSTR(nmb->header.nm_flags.trunc),
  101. BOOLSTR(nmb->header.nm_flags.authoritative) );
  102. dbgtext( " header: rcode=%d qdcount=%d ancount=%d nscount=%d arcount=%d\n",
  103. nmb->header.rcode,
  104. nmb->header.qdcount,
  105. nmb->header.ancount,
  106. nmb->header.nscount,
  107. nmb->header.arcount );
  108. }
  109. if (nmb->header.qdcount)
  110. {
  111. DEBUGADD( 4, ( " question: q_name=%s q_type=%d q_class=%d\n",
  112. nmb_namestr(&nmb->question.question_name),
  113. nmb->question.question_type,
  114. nmb->question.question_class) );
  115. }
  116. if (nmb->answers && nmb->header.ancount)
  117. {
  118. debug_nmb_res_rec(nmb->answers,"answers");
  119. }
  120. if (nmb->nsrecs && nmb->header.nscount)
  121. {
  122. debug_nmb_res_rec(nmb->nsrecs,"nsrecs");
  123. }
  124. if (nmb->additional && nmb->header.arcount)
  125. {
  126. debug_nmb_res_rec(nmb->additional,"additional");
  127. }
  128. }
  129. /*******************************************************************
  130. handle "compressed" name pointers
  131. ******************************************************************/
  132. static BOOL handle_name_ptrs(unsigned char *ubuf,int *offset,int length,
  133. BOOL *got_pointer,int *ret)
  134. {
  135. int loop_count=0;
  136. while ((ubuf[*offset] & 0xC0) == 0xC0) {
  137. if (!*got_pointer) (*ret) += 2;
  138. (*got_pointer)=True;
  139. (*offset) = ((ubuf[*offset] & ~0xC0)<<8) | ubuf[(*offset)+1];
  140. if (loop_count++ == 10 || (*offset) < 0 || (*offset)>(length-2)) {
  141. return(False);
  142. }
  143. }
  144. return(True);
  145. }
  146. /*******************************************************************
  147. parse a nmb name from "compressed" format to something readable
  148. return the space taken by the name, or 0 if the name is invalid
  149. ******************************************************************/
  150. static int parse_nmb_name(char *inbuf,int offset,int length, struct nmb_name *name)
  151. {
  152. int m,n=0;
  153. unsigned char *ubuf = (unsigned char *)inbuf;
  154. int ret = 0;
  155. BOOL got_pointer=False;
  156. if (length - offset < 2)
  157. return(0);
  158. /* handle initial name pointers */
  159. if (!handle_name_ptrs(ubuf,&offset,length,&got_pointer,&ret))
  160. return(0);
  161. m = ubuf[offset];
  162. if (!m)
  163. return(0);
  164. if ((m & 0xC0) || offset+m+2 > length)
  165. return(0);
  166. memset((char *)name,'\0',sizeof(*name));
  167. /* the "compressed" part */
  168. if (!got_pointer)
  169. ret += m + 2;
  170. offset++;
  171. while (m > 0) {
  172. unsigned char c1,c2;
  173. c1 = ubuf[offset++]-'A';
  174. c2 = ubuf[offset++]-'A';
  175. if ((c1 & 0xF0) || (c2 & 0xF0) || ((size_t) n > sizeof(name->name)-1))
  176. return(0);
  177. name->name[n++] = (c1<<4) | c2;
  178. m -= 2;
  179. }
  180. name->name[n] = 0;
  181. if (n==16) {
  182. /* parse out the name type,
  183. its always in the 16th byte of the name */
  184. name->name_type = ((unsigned char)name->name[15]) & 0xff;
  185. /* remove trailing spaces */
  186. name->name[15] = 0;
  187. n = 14;
  188. while (n && name->name[n]==' ')
  189. name->name[n--] = 0;
  190. }
  191. /* now the domain parts (if any) */
  192. n = 0;
  193. while (ubuf[offset]) {
  194. /* we can have pointers within the domain part as well */
  195. if (!handle_name_ptrs(ubuf,&offset,length,&got_pointer,&ret))
  196. return(0);
  197. m = ubuf[offset];
  198. if (!got_pointer)
  199. ret += m+1;
  200. if (n)
  201. name->scope[n++] = '.';
  202. if (m+2+offset>length || (size_t) n+m+1 >sizeof(name->scope))
  203. return(0);
  204. offset++;
  205. while (m--)
  206. name->scope[n++] = (char)ubuf[offset++];
  207. }
  208. name->scope[n++] = 0;
  209. return(ret);
  210. }
  211. /*******************************************************************
  212. put a compressed nmb name into a buffer. return the length of the
  213. compressed name
  214. compressed names are really weird. The "compression" doubles the
  215. size. The idea is that it also means that compressed names conform
  216. to the doman name system. See RFC1002.
  217. ******************************************************************/
  218. static int put_nmb_name(char *buf,int offset,struct nmb_name *name)
  219. {
  220. int ret,m;
  221. fstring buf1;
  222. char *p;
  223. if (strcmp(name->name,"*") == 0) {
  224. /* special case for wildcard name */
  225. memset(buf1,'\0',20);
  226. buf1[0] = '*';
  227. buf1[15] = name->name_type;
  228. } else {
  229. slprintf(buf1, sizeof(buf1) - 1,"%-15.15s%c",name->name,name->name_type);
  230. }
  231. buf[offset] = 0x20;
  232. ret = 34;
  233. for (m=0;m<16;m++) {
  234. buf[offset+1+2*m] = 'A' + ((buf1[m]>>4)&0xF);
  235. buf[offset+2+2*m] = 'A' + (buf1[m]&0xF);
  236. }
  237. offset += 33;
  238. buf[offset] = 0;
  239. if (name->scope[0]) {
  240. /* XXXX this scope handling needs testing */
  241. ret += strlen(name->scope) + 1;
  242. pstrcpy(&buf[offset+1],name->scope);
  243. p = &buf[offset+1];
  244. while ((p = strchr(p,'.'))) {
  245. buf[offset] = PTR_DIFF(p,&buf[offset+1]);
  246. offset += (buf[offset] + 1);
  247. p = &buf[offset+1];
  248. }
  249. buf[offset] = strlen(&buf[offset+1]);
  250. }
  251. return(ret);
  252. }
  253. /*******************************************************************
  254. useful for debugging messages
  255. ******************************************************************/
  256. char *nmb_namestr(struct nmb_name *n)
  257. {
  258. static int i=0;
  259. static fstring ret[4];
  260. char *p = ret[i];
  261. if (!n->scope[0])
  262. slprintf(p,sizeof(fstring)-1, "%s<%02x>",n->name,n->name_type);
  263. else
  264. slprintf(p,sizeof(fstring)-1, "%s<%02x>.%s",n->name,n->name_type,n->scope);
  265. i = (i+1)%4;
  266. return(p);
  267. }
  268. /*******************************************************************
  269. allocate and parse some resource records
  270. ******************************************************************/
  271. static BOOL parse_alloc_res_rec(char *inbuf,int *offset,int length,
  272. struct res_rec **recs, int count)
  273. {
  274. int i;
  275. *recs = (struct res_rec *)malloc(sizeof(**recs)*count);
  276. if (!*recs) return(False);
  277. memset((char *)*recs,'\0',sizeof(**recs)*count);
  278. for (i=0;i<count;i++) {
  279. int l = parse_nmb_name(inbuf,*offset,length,&(*recs)[i].rr_name);
  280. (*offset) += l;
  281. if (!l || (*offset)+10 > length) {
  282. free(*recs);
  283. return(False);
  284. }
  285. (*recs)[i].rr_type = RSVAL(inbuf,(*offset));
  286. (*recs)[i].rr_class = RSVAL(inbuf,(*offset)+2);
  287. (*recs)[i].ttl = RIVAL(inbuf,(*offset)+4);
  288. (*recs)[i].rdlength = RSVAL(inbuf,(*offset)+8);
  289. (*offset) += 10;
  290. if ((size_t)(*recs)[i].rdlength>sizeof((*recs)[i].rdata) ||
  291. (*offset)+(*recs)[i].rdlength > length) {
  292. free(*recs);
  293. return(False);
  294. }
  295. memcpy((*recs)[i].rdata,inbuf+(*offset),(*recs)[i].rdlength);
  296. (*offset) += (*recs)[i].rdlength;
  297. }
  298. return(True);
  299. }
  300. /*******************************************************************
  301. put a resource record into a packet
  302. ******************************************************************/
  303. static int put_res_rec(char *buf,int offset,struct res_rec *recs,int count)
  304. {
  305. int ret=0;
  306. int i;
  307. for (i=0;i<count;i++) {
  308. int l = put_nmb_name(buf,offset,&recs[i].rr_name);
  309. offset += l;
  310. ret += l;
  311. RSSVAL(buf,offset,recs[i].rr_type);
  312. RSSVAL(buf,offset+2,recs[i].rr_class);
  313. RSIVAL(buf,offset+4,recs[i].ttl);
  314. RSSVAL(buf,offset+8,recs[i].rdlength);
  315. memcpy(buf+offset+10,recs[i].rdata,recs[i].rdlength);
  316. offset += 10+recs[i].rdlength;
  317. ret += 10+recs[i].rdlength;
  318. }
  319. return(ret);
  320. }
  321. /*******************************************************************
  322. put a compressed name pointer record into a packet
  323. ******************************************************************/
  324. static int put_compressed_name_ptr(unsigned char *buf,int offset,struct res_rec *rec,int ptr_offset)
  325. {
  326. int ret=0;
  327. buf[offset] = (0xC0 | ((ptr_offset >> 8) & 0xFF));
  328. buf[offset+1] = (ptr_offset & 0xFF);
  329. offset += 2;
  330. ret += 2;
  331. RSSVAL(buf,offset,rec->rr_type);
  332. RSSVAL(buf,offset+2,rec->rr_class);
  333. RSIVAL(buf,offset+4,rec->ttl);
  334. RSSVAL(buf,offset+8,rec->rdlength);
  335. memcpy(buf+offset+10,rec->rdata,rec->rdlength);
  336. offset += 10+rec->rdlength;
  337. ret += 10+rec->rdlength;
  338. return(ret);
  339. }
  340. /*******************************************************************
  341. parse a dgram packet. Return False if the packet can't be parsed
  342. or is invalid for some reason, True otherwise
  343. this is documented in section 4.4.1 of RFC1002
  344. ******************************************************************/
  345. static BOOL parse_dgram(char *inbuf,int length,struct dgram_packet *dgram)
  346. {
  347. int offset;
  348. int flags;
  349. memset((char *)dgram,'\0',sizeof(*dgram));
  350. if (length < 14) return(False);
  351. dgram->header.msg_type = CVAL(inbuf,0);
  352. flags = CVAL(inbuf,1);
  353. dgram->header.flags.node_type = (enum node_type)((flags>>2)&3);
  354. if (flags & 1) dgram->header.flags.more = True;
  355. if (flags & 2) dgram->header.flags.first = True;
  356. dgram->header.dgm_id = RSVAL(inbuf,2);
  357. putip((char *)&dgram->header.source_ip,inbuf+4);
  358. dgram->header.source_port = RSVAL(inbuf,8);
  359. dgram->header.dgm_length = RSVAL(inbuf,10);
  360. dgram->header.packet_offset = RSVAL(inbuf,12);
  361. offset = 14;
  362. if (dgram->header.msg_type == 0x10 ||
  363. dgram->header.msg_type == 0x11 ||
  364. dgram->header.msg_type == 0x12) {
  365. offset += parse_nmb_name(inbuf,offset,length,&dgram->source_name);
  366. offset += parse_nmb_name(inbuf,offset,length,&dgram->dest_name);
  367. }
  368. if (offset >= length || ((size_t)length-offset > sizeof(dgram->data)))
  369. return(False);
  370. dgram->datasize = length-offset;
  371. memcpy(dgram->data,inbuf+offset,dgram->datasize);
  372. return(True);
  373. }
  374. /*******************************************************************
  375. parse a nmb packet. Return False if the packet can't be parsed
  376. or is invalid for some reason, True otherwise
  377. ******************************************************************/
  378. static BOOL parse_nmb(char *inbuf,int length,struct nmb_packet *nmb)
  379. {
  380. int nm_flags,offset;
  381. memset((char *)nmb,'\0',sizeof(*nmb));
  382. if (length < 12) return(False);
  383. /* parse the header */
  384. nmb->header.name_trn_id = RSVAL(inbuf,0);
  385. DEBUG(10,("parse_nmb: packet id = %d\n", nmb->header.name_trn_id));
  386. nmb->header.opcode = (CVAL(inbuf,2) >> 3) & 0xF;
  387. nmb->header.response = ((CVAL(inbuf,2)>>7)&1)?True:False;
  388. nm_flags = ((CVAL(inbuf,2) & 0x7) << 4) + (CVAL(inbuf,3)>>4);
  389. nmb->header.nm_flags.bcast = (nm_flags&1)?True:False;
  390. nmb->header.nm_flags.recursion_available = (nm_flags&8)?True:False;
  391. nmb->header.nm_flags.recursion_desired = (nm_flags&0x10)?True:False;
  392. nmb->header.nm_flags.trunc = (nm_flags&0x20)?True:False;
  393. nmb->header.nm_flags.authoritative = (nm_flags&0x40)?True:False;
  394. nmb->header.rcode = CVAL(inbuf,3) & 0xF;
  395. nmb->header.qdcount = RSVAL(inbuf,4);
  396. nmb->header.ancount = RSVAL(inbuf,6);
  397. nmb->header.nscount = RSVAL(inbuf,8);
  398. nmb->header.arcount = RSVAL(inbuf,10);
  399. if (nmb->header.qdcount) {
  400. offset = parse_nmb_name(inbuf,12,length,&nmb->question.question_name);
  401. if (!offset) return(False);
  402. if (length - (12+offset) < 4) return(False);
  403. nmb->question.question_type = RSVAL(inbuf,12+offset);
  404. nmb->question.question_class = RSVAL(inbuf,12+offset+2);
  405. offset += 12+4;
  406. } else {
  407. offset = 12;
  408. }
  409. /* and any resource records */
  410. if (nmb->header.ancount &&
  411. !parse_alloc_res_rec(inbuf,&offset,length,&nmb->answers,
  412. nmb->header.ancount))
  413. return(False);
  414. if (nmb->header.nscount &&
  415. !parse_alloc_res_rec(inbuf,&offset,length,&nmb->nsrecs,
  416. nmb->header.nscount))
  417. return(False);
  418. if (nmb->header.arcount &&
  419. !parse_alloc_res_rec(inbuf,&offset,length,&nmb->additional,
  420. nmb->header.arcount))
  421. return(False);
  422. return(True);
  423. }
  424. #if 0
  425. /*******************************************************************
  426. 'Copy constructor' for an nmb packet
  427. ******************************************************************/
  428. static struct packet_struct *copy_nmb_packet(struct packet_struct *packet)
  429. {
  430. struct nmb_packet *nmb;
  431. struct nmb_packet *copy_nmb;
  432. struct packet_struct *pkt_copy;
  433. if(( pkt_copy = (struct packet_struct *)malloc(sizeof(*packet))) == NULL)
  434. {
  435. DEBUG(0,("copy_nmb_packet: malloc fail.\n"));
  436. return NULL;
  437. }
  438. /* Structure copy of entire thing. */
  439. *pkt_copy = *packet;
  440. /* Ensure this copy is not locked. */
  441. pkt_copy->locked = False;
  442. /* Ensure this copy has no resource records. */
  443. nmb = &packet->packet.nmb;
  444. copy_nmb = &pkt_copy->packet.nmb;
  445. copy_nmb->answers = NULL;
  446. copy_nmb->nsrecs = NULL;
  447. copy_nmb->additional = NULL;
  448. /* Now copy any resource records. */
  449. if (nmb->answers)
  450. {
  451. if((copy_nmb->answers = (struct res_rec *)
  452. malloc(nmb->header.ancount * sizeof(struct res_rec))) == NULL)
  453. goto free_and_exit;
  454. memcpy((char *)copy_nmb->answers, (char *)nmb->answers,
  455. nmb->header.ancount * sizeof(struct res_rec));
  456. }
  457. if (nmb->nsrecs)
  458. {
  459. if((copy_nmb->nsrecs = (struct res_rec *)
  460. malloc(nmb->header.nscount * sizeof(struct res_rec))) == NULL)
  461. goto free_and_exit;
  462. memcpy((char *)copy_nmb->nsrecs, (char *)nmb->nsrecs,
  463. nmb->header.nscount * sizeof(struct res_rec));
  464. }
  465. if (nmb->additional)
  466. {
  467. if((copy_nmb->additional = (struct res_rec *)
  468. malloc(nmb->header.arcount * sizeof(struct res_rec))) == NULL)
  469. goto free_and_exit;
  470. memcpy((char *)copy_nmb->additional, (char *)nmb->additional,
  471. nmb->header.arcount * sizeof(struct res_rec));
  472. }
  473. return pkt_copy;
  474. free_and_exit:
  475. if(copy_nmb->answers)
  476. free((char *)copy_nmb->answers);
  477. if(copy_nmb->nsrecs)
  478. free((char *)copy_nmb->nsrecs);
  479. if(copy_nmb->additional)
  480. free((char *)copy_nmb->additional);
  481. free((char *)pkt_copy);
  482. DEBUG(0,("copy_nmb_packet: malloc fail in resource records.\n"));
  483. return NULL;
  484. }
  485. /*******************************************************************
  486. 'Copy constructor' for a dgram packet
  487. ******************************************************************/
  488. static struct packet_struct *copy_dgram_packet(struct packet_struct *packet)
  489. {
  490. struct packet_struct *pkt_copy;
  491. if(( pkt_copy = (struct packet_struct *)malloc(sizeof(*packet))) == NULL)
  492. {
  493. DEBUG(0,("copy_dgram_packet: malloc fail.\n"));
  494. return NULL;
  495. }
  496. /* Structure copy of entire thing. */
  497. *pkt_copy = *packet;
  498. /* Ensure this copy is not locked. */
  499. pkt_copy->locked = False;
  500. /* There are no additional pointers in a dgram packet,
  501. we are finished. */
  502. return pkt_copy;
  503. }
  504. /*******************************************************************
  505. 'Copy constructor' for a generic packet
  506. ******************************************************************/
  507. struct packet_struct *copy_packet(struct packet_struct *packet)
  508. {
  509. if(packet->packet_type == NMB_PACKET)
  510. return copy_nmb_packet(packet);
  511. else if (packet->packet_type == DGRAM_PACKET)
  512. return copy_dgram_packet(packet);
  513. return NULL;
  514. }
  515. #endif /* 0 */
  516. /*******************************************************************
  517. free up any resources associated with an nmb packet
  518. ******************************************************************/
  519. static void free_nmb_packet(struct nmb_packet *nmb)
  520. {
  521. if (nmb->answers) free(nmb->answers);
  522. if (nmb->nsrecs) free(nmb->nsrecs);
  523. if (nmb->additional) free(nmb->additional);
  524. }
  525. /*******************************************************************
  526. free up any resources associated with a dgram packet
  527. ******************************************************************/
  528. static void free_dgram_packet(struct dgram_packet *nmb)
  529. {
  530. /* We have nothing to do for a dgram packet. */
  531. (void) nmb;
  532. }
  533. /*******************************************************************
  534. free up any resources associated with a packet
  535. ******************************************************************/
  536. void free_packet(struct packet_struct *packet)
  537. {
  538. if (packet->locked)
  539. return;
  540. if (packet->packet_type == NMB_PACKET)
  541. free_nmb_packet(&packet->packet.nmb);
  542. else if (packet->packet_type == DGRAM_PACKET)
  543. free_dgram_packet(&packet->packet.dgram);
  544. free(packet);
  545. }
  546. /*******************************************************************
  547. read a packet from a socket and parse it, returning a packet ready
  548. to be used or put on the queue. This assumes a UDP socket
  549. ******************************************************************/
  550. struct packet_struct *read_packet(int fd,enum packet_type packet_type)
  551. {
  552. extern struct in_addr lastip;
  553. extern int lastport;
  554. struct packet_struct *packet;
  555. char buf[MAX_DGRAM_SIZE];
  556. int length;
  557. BOOL ok=False;
  558. length = read_udp_socket(fd,buf,sizeof(buf));
  559. if (length < MIN_DGRAM_SIZE) return(NULL);
  560. packet = (struct packet_struct *)malloc(sizeof(*packet));
  561. if (!packet) return(NULL);
  562. packet->next = NULL;
  563. packet->prev = NULL;
  564. packet->ip = lastip;
  565. packet->port = lastport;
  566. packet->fd = fd;
  567. packet->locked = False;
  568. packet->timestamp = time(NULL);
  569. packet->packet_type = packet_type;
  570. switch (packet_type)
  571. {
  572. case NMB_PACKET:
  573. ok = parse_nmb(buf,length,&packet->packet.nmb);
  574. break;
  575. case DGRAM_PACKET:
  576. ok = parse_dgram(buf,length,&packet->packet.dgram);
  577. break;
  578. }
  579. if (!ok) {
  580. DEBUG(10,("read_packet: discarding packet id = %d\n",
  581. packet->packet.nmb.header.name_trn_id));
  582. free_packet(packet);
  583. return(NULL);
  584. }
  585. num_good_receives++;
  586. DEBUG(5,("Received a packet of len %d from (%s) port %d\n",
  587. length, inet_ntoa(packet->ip), packet->port ) );
  588. return(packet);
  589. }
  590. /*******************************************************************
  591. send a udp packet on a already open socket
  592. ******************************************************************/
  593. static BOOL send_udp(int fd,char *buf,int len,struct in_addr ip,int port)
  594. {
  595. BOOL ret;
  596. struct sockaddr_in sock_out;
  597. /* set the address and port */
  598. memset((char *)&sock_out,'\0',sizeof(sock_out));
  599. putip((char *)&sock_out.sin_addr,(char *)&ip);
  600. sock_out.sin_port = htons( port );
  601. sock_out.sin_family = AF_INET;
  602. DEBUG( 5, ( "Sending a packet of len %d to (%s) on port %d\n",
  603. len, inet_ntoa(ip), port ) );
  604. ret = (sendto(fd,buf,len,0,(struct sockaddr *)&sock_out,
  605. sizeof(sock_out)) >= 0);
  606. if (!ret)
  607. DEBUG(0,("Packet send failed to %s(%d) ERRNO=%s\n",
  608. inet_ntoa(ip),port, unix_error_string (errno)));
  609. if (ret)
  610. num_good_sends++;
  611. return(ret);
  612. }
  613. /*******************************************************************
  614. build a dgram packet ready for sending
  615. XXXX This currently doesn't handle packets too big for one
  616. datagram. It should split them and use the packet_offset, more and
  617. first flags to handle the fragmentation. Yuck.
  618. ******************************************************************/
  619. static int build_dgram(char *buf,struct packet_struct *p)
  620. {
  621. struct dgram_packet *dgram = &p->packet.dgram;
  622. unsigned char *ubuf = (unsigned char *)buf;
  623. int offset=0;
  624. /* put in the header */
  625. ubuf[0] = dgram->header.msg_type;
  626. ubuf[1] = (((int)dgram->header.flags.node_type)<<2);
  627. if (dgram->header.flags.more) ubuf[1] |= 1;
  628. if (dgram->header.flags.first) ubuf[1] |= 2;
  629. RSSVAL(ubuf,2,dgram->header.dgm_id);
  630. putip(ubuf+4,(char *)&dgram->header.source_ip);
  631. RSSVAL(ubuf,8,dgram->header.source_port);
  632. RSSVAL(ubuf,12,dgram->header.packet_offset);
  633. offset = 14;
  634. if (dgram->header.msg_type == 0x10 ||
  635. dgram->header.msg_type == 0x11 ||
  636. dgram->header.msg_type == 0x12) {
  637. offset += put_nmb_name((char *)ubuf,offset,&dgram->source_name);
  638. offset += put_nmb_name((char *)ubuf,offset,&dgram->dest_name);
  639. }
  640. memcpy(ubuf+offset,dgram->data,dgram->datasize);
  641. offset += dgram->datasize;
  642. /* automatically set the dgm_length */
  643. dgram->header.dgm_length = offset;
  644. RSSVAL(ubuf,10,dgram->header.dgm_length);
  645. return(offset);
  646. }
  647. /*******************************************************************
  648. build a nmb name
  649. *******************************************************************/
  650. void make_nmb_name( struct nmb_name *n, const char *name, int type )
  651. {
  652. extern pstring global_scope;
  653. memset( (char *)n, '\0', sizeof(struct nmb_name) );
  654. StrnCpy( n->name, name, 15 );
  655. strupper( n->name );
  656. n->name_type = (unsigned int)type & 0xFF;
  657. StrnCpy( n->scope, global_scope, 63 );
  658. strupper( n->scope );
  659. }
  660. /*******************************************************************
  661. Compare two nmb names
  662. ******************************************************************/
  663. #if 0
  664. BOOL nmb_name_equal(struct nmb_name *n1, struct nmb_name *n2)
  665. {
  666. return ((n1->name_type == n2->name_type) &&
  667. strequal(n1->name ,n2->name ) &&
  668. strequal(n1->scope,n2->scope));
  669. }
  670. #endif /* 0 */
  671. /*******************************************************************
  672. build a nmb packet ready for sending
  673. XXXX this currently relies on not being passed something that expands
  674. to a packet too big for the buffer. Eventually this should be
  675. changed to set the trunc bit so the receiver can request the rest
  676. via tcp (when that becomes supported)
  677. ******************************************************************/
  678. static int build_nmb(char *buf,struct packet_struct *p)
  679. {
  680. struct nmb_packet *nmb = &p->packet.nmb;
  681. unsigned char *ubuf = (unsigned char *)buf;
  682. int offset=0;
  683. /* put in the header */
  684. RSSVAL(ubuf,offset,nmb->header.name_trn_id);
  685. ubuf[offset+2] = (nmb->header.opcode & 0xF) << 3;
  686. if (nmb->header.response) ubuf[offset+2] |= (1<<7);
  687. if (nmb->header.nm_flags.authoritative &&
  688. nmb->header.response) ubuf[offset+2] |= 0x4;
  689. if (nmb->header.nm_flags.trunc) ubuf[offset+2] |= 0x2;
  690. if (nmb->header.nm_flags.recursion_desired) ubuf[offset+2] |= 0x1;
  691. if (nmb->header.nm_flags.recursion_available &&
  692. nmb->header.response) ubuf[offset+3] |= 0x80;
  693. if (nmb->header.nm_flags.bcast) ubuf[offset+3] |= 0x10;
  694. ubuf[offset+3] |= (nmb->header.rcode & 0xF);
  695. RSSVAL(ubuf,offset+4,nmb->header.qdcount);
  696. RSSVAL(ubuf,offset+6,nmb->header.ancount);
  697. RSSVAL(ubuf,offset+8,nmb->header.nscount);
  698. RSSVAL(ubuf,offset+10,nmb->header.arcount);
  699. offset += 12;
  700. if (nmb->header.qdcount) {
  701. /* XXXX this doesn't handle a qdcount of > 1 */
  702. offset += put_nmb_name((char *)ubuf,offset,&nmb->question.question_name);
  703. RSSVAL(ubuf,offset,nmb->question.question_type);
  704. RSSVAL(ubuf,offset+2,nmb->question.question_class);
  705. offset += 4;
  706. }
  707. if (nmb->header.ancount)
  708. offset += put_res_rec((char *)ubuf,offset,nmb->answers,
  709. nmb->header.ancount);
  710. if (nmb->header.nscount)
  711. offset += put_res_rec((char *)ubuf,offset,nmb->nsrecs,
  712. nmb->header.nscount);
  713. /*
  714. * The spec says we must put compressed name pointers
  715. * in the following outgoing packets :
  716. * NAME_REGISTRATION_REQUEST, NAME_REFRESH_REQUEST,
  717. * NAME_RELEASE_REQUEST.
  718. */
  719. if((nmb->header.response == False) &&
  720. ((nmb->header.opcode == NMB_NAME_REG_OPCODE) ||
  721. (nmb->header.opcode == NMB_NAME_RELEASE_OPCODE) ||
  722. (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_8) ||
  723. (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_9) ||
  724. (nmb->header.opcode == NMB_NAME_MULTIHOMED_REG_OPCODE)) &&
  725. (nmb->header.arcount == 1)) {
  726. offset += put_compressed_name_ptr(ubuf,offset,nmb->additional,12);
  727. } else if (nmb->header.arcount) {
  728. offset += put_res_rec((char *)ubuf,offset,nmb->additional,
  729. nmb->header.arcount);
  730. }
  731. return(offset);
  732. }
  733. /*******************************************************************
  734. send a packet_struct
  735. ******************************************************************/
  736. BOOL send_packet(struct packet_struct *p)
  737. {
  738. char buf[1024];
  739. int len=0;
  740. memset(buf,'\0',sizeof(buf));
  741. switch (p->packet_type)
  742. {
  743. case NMB_PACKET:
  744. len = build_nmb(buf,p);
  745. debug_nmb_packet(p);
  746. break;
  747. case DGRAM_PACKET:
  748. len = build_dgram(buf,p);
  749. break;
  750. }
  751. if (!len) return(False);
  752. return(send_udp(p->fd,buf,len,p->ip,p->port));
  753. }
  754. /****************************************************************************
  755. receive a packet with timeout on a open UDP filedescriptor
  756. The timeout is in milliseconds
  757. ***************************************************************************/
  758. struct packet_struct *receive_packet(int fd,enum packet_type type,int t)
  759. {
  760. fd_set fds;
  761. struct timeval timeout;
  762. FD_ZERO(&fds);
  763. FD_SET(fd,&fds);
  764. timeout.tv_sec = t/1000;
  765. timeout.tv_usec = 1000*(t%1000);
  766. sys_select(fd+1,&fds,&timeout);
  767. if (FD_ISSET(fd,&fds))
  768. return(read_packet(fd,type));
  769. return(NULL);
  770. }
  771. #if 0
  772. /****************************************************************************
  773. return the number of bits that match between two 4 character buffers
  774. ***************************************************************************/
  775. static int matching_bits(uchar *p1, uchar *p2)
  776. {
  777. int i, j, ret = 0;
  778. for (i=0; i<4; i++) {
  779. if (p1[i] != p2[i]) break;
  780. ret += 8;
  781. }
  782. if (i==4) return ret;
  783. for (j=0; j<8; j++) {
  784. if ((p1[i] & (1<<(7-j))) != (p2[i] & (1<<(7-j)))) break;
  785. ret++;
  786. }
  787. return ret;
  788. }
  789. static uchar sort_ip[4];
  790. /****************************************************************************
  791. compare two query reply records
  792. ***************************************************************************/
  793. static int name_query_comp(uchar *p1, uchar *p2)
  794. {
  795. return matching_bits(p2+2, sort_ip) - matching_bits(p1+2, sort_ip);
  796. }
  797. /****************************************************************************
  798. sort a set of 6 byte name query response records so that the IPs that
  799. have the most leading bits in common with the specified address come first
  800. ***************************************************************************/
  801. void sort_query_replies(char *data, int n, struct in_addr ip)
  802. {
  803. if (n <= 1) return;
  804. putip(sort_ip, (char *)&ip);
  805. qsort(data, n, 6, QSORT_CAST name_query_comp);
  806. }
  807. #endif /*0 */