nmblib.c 29 KB

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