nmblib.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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) || (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 || 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 ((*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 || (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. }
  531. /*******************************************************************
  532. free up any resources associated with a packet
  533. ******************************************************************/
  534. void free_packet(struct packet_struct *packet)
  535. {
  536. if (packet->locked)
  537. return;
  538. if (packet->packet_type == NMB_PACKET)
  539. free_nmb_packet(&packet->packet.nmb);
  540. else if (packet->packet_type == DGRAM_PACKET)
  541. free_dgram_packet(&packet->packet.dgram);
  542. free(packet);
  543. }
  544. /*******************************************************************
  545. read a packet from a socket and parse it, returning a packet ready
  546. to be used or put on the queue. This assumes a UDP socket
  547. ******************************************************************/
  548. struct packet_struct *read_packet(int fd,enum packet_type packet_type)
  549. {
  550. extern struct in_addr lastip;
  551. extern int lastport;
  552. struct packet_struct *packet;
  553. char buf[MAX_DGRAM_SIZE];
  554. int length;
  555. BOOL ok=False;
  556. length = read_udp_socket(fd,buf,sizeof(buf));
  557. if (length < MIN_DGRAM_SIZE) return(NULL);
  558. packet = (struct packet_struct *)malloc(sizeof(*packet));
  559. if (!packet) return(NULL);
  560. packet->next = NULL;
  561. packet->prev = NULL;
  562. packet->ip = lastip;
  563. packet->port = lastport;
  564. packet->fd = fd;
  565. packet->locked = False;
  566. packet->timestamp = time(NULL);
  567. packet->packet_type = packet_type;
  568. switch (packet_type)
  569. {
  570. case NMB_PACKET:
  571. ok = parse_nmb(buf,length,&packet->packet.nmb);
  572. break;
  573. case DGRAM_PACKET:
  574. ok = parse_dgram(buf,length,&packet->packet.dgram);
  575. break;
  576. }
  577. if (!ok) {
  578. DEBUG(10,("read_packet: discarding packet id = %d\n",
  579. packet->packet.nmb.header.name_trn_id));
  580. free_packet(packet);
  581. return(NULL);
  582. }
  583. num_good_receives++;
  584. DEBUG(5,("Received a packet of len %d from (%s) port %d\n",
  585. length, inet_ntoa(packet->ip), packet->port ) );
  586. return(packet);
  587. }
  588. /*******************************************************************
  589. send a udp packet on a already open socket
  590. ******************************************************************/
  591. static BOOL send_udp(int fd,char *buf,int len,struct in_addr ip,int port)
  592. {
  593. BOOL ret;
  594. struct sockaddr_in sock_out;
  595. /* set the address and port */
  596. memset((char *)&sock_out,'\0',sizeof(sock_out));
  597. putip((char *)&sock_out.sin_addr,(char *)&ip);
  598. sock_out.sin_port = htons( port );
  599. sock_out.sin_family = AF_INET;
  600. DEBUG( 5, ( "Sending a packet of len %d to (%s) on port %d\n",
  601. len, inet_ntoa(ip), port ) );
  602. ret = (sendto(fd,buf,len,0,(struct sockaddr *)&sock_out,
  603. sizeof(sock_out)) >= 0);
  604. if (!ret)
  605. DEBUG(0,("Packet send failed to %s(%d) ERRNO=%s\n",
  606. inet_ntoa(ip),port,strerror(errno)));
  607. if (ret)
  608. num_good_sends++;
  609. return(ret);
  610. }
  611. /*******************************************************************
  612. build a dgram packet ready for sending
  613. XXXX This currently doesn't handle packets too big for one
  614. datagram. It should split them and use the packet_offset, more and
  615. first flags to handle the fragmentation. Yuck.
  616. ******************************************************************/
  617. static int build_dgram(char *buf,struct packet_struct *p)
  618. {
  619. struct dgram_packet *dgram = &p->packet.dgram;
  620. unsigned char *ubuf = (unsigned char *)buf;
  621. int offset=0;
  622. /* put in the header */
  623. ubuf[0] = dgram->header.msg_type;
  624. ubuf[1] = (((int)dgram->header.flags.node_type)<<2);
  625. if (dgram->header.flags.more) ubuf[1] |= 1;
  626. if (dgram->header.flags.first) ubuf[1] |= 2;
  627. RSSVAL(ubuf,2,dgram->header.dgm_id);
  628. putip(ubuf+4,(char *)&dgram->header.source_ip);
  629. RSSVAL(ubuf,8,dgram->header.source_port);
  630. RSSVAL(ubuf,12,dgram->header.packet_offset);
  631. offset = 14;
  632. if (dgram->header.msg_type == 0x10 ||
  633. dgram->header.msg_type == 0x11 ||
  634. dgram->header.msg_type == 0x12) {
  635. offset += put_nmb_name((char *)ubuf,offset,&dgram->source_name);
  636. offset += put_nmb_name((char *)ubuf,offset,&dgram->dest_name);
  637. }
  638. memcpy(ubuf+offset,dgram->data,dgram->datasize);
  639. offset += dgram->datasize;
  640. /* automatically set the dgm_length */
  641. dgram->header.dgm_length = offset;
  642. RSSVAL(ubuf,10,dgram->header.dgm_length);
  643. return(offset);
  644. }
  645. /*******************************************************************
  646. build a nmb name
  647. *******************************************************************/
  648. void make_nmb_name( struct nmb_name *n, const char *name, int type )
  649. {
  650. extern pstring global_scope;
  651. memset( (char *)n, '\0', sizeof(struct nmb_name) );
  652. StrnCpy( n->name, name, 15 );
  653. strupper( n->name );
  654. n->name_type = (unsigned int)type & 0xFF;
  655. StrnCpy( n->scope, global_scope, 63 );
  656. strupper( n->scope );
  657. }
  658. /*******************************************************************
  659. Compare two nmb names
  660. ******************************************************************/
  661. #if 0
  662. BOOL nmb_name_equal(struct nmb_name *n1, struct nmb_name *n2)
  663. {
  664. return ((n1->name_type == n2->name_type) &&
  665. strequal(n1->name ,n2->name ) &&
  666. strequal(n1->scope,n2->scope));
  667. }
  668. #endif /* 0 */
  669. /*******************************************************************
  670. build a nmb packet ready for sending
  671. XXXX this currently relies on not being passed something that expands
  672. to a packet too big for the buffer. Eventually this should be
  673. changed to set the trunc bit so the receiver can request the rest
  674. via tcp (when that becomes supported)
  675. ******************************************************************/
  676. static int build_nmb(char *buf,struct packet_struct *p)
  677. {
  678. struct nmb_packet *nmb = &p->packet.nmb;
  679. unsigned char *ubuf = (unsigned char *)buf;
  680. int offset=0;
  681. /* put in the header */
  682. RSSVAL(ubuf,offset,nmb->header.name_trn_id);
  683. ubuf[offset+2] = (nmb->header.opcode & 0xF) << 3;
  684. if (nmb->header.response) ubuf[offset+2] |= (1<<7);
  685. if (nmb->header.nm_flags.authoritative &&
  686. nmb->header.response) ubuf[offset+2] |= 0x4;
  687. if (nmb->header.nm_flags.trunc) ubuf[offset+2] |= 0x2;
  688. if (nmb->header.nm_flags.recursion_desired) ubuf[offset+2] |= 0x1;
  689. if (nmb->header.nm_flags.recursion_available &&
  690. nmb->header.response) ubuf[offset+3] |= 0x80;
  691. if (nmb->header.nm_flags.bcast) ubuf[offset+3] |= 0x10;
  692. ubuf[offset+3] |= (nmb->header.rcode & 0xF);
  693. RSSVAL(ubuf,offset+4,nmb->header.qdcount);
  694. RSSVAL(ubuf,offset+6,nmb->header.ancount);
  695. RSSVAL(ubuf,offset+8,nmb->header.nscount);
  696. RSSVAL(ubuf,offset+10,nmb->header.arcount);
  697. offset += 12;
  698. if (nmb->header.qdcount) {
  699. /* XXXX this doesn't handle a qdcount of > 1 */
  700. offset += put_nmb_name((char *)ubuf,offset,&nmb->question.question_name);
  701. RSSVAL(ubuf,offset,nmb->question.question_type);
  702. RSSVAL(ubuf,offset+2,nmb->question.question_class);
  703. offset += 4;
  704. }
  705. if (nmb->header.ancount)
  706. offset += put_res_rec((char *)ubuf,offset,nmb->answers,
  707. nmb->header.ancount);
  708. if (nmb->header.nscount)
  709. offset += put_res_rec((char *)ubuf,offset,nmb->nsrecs,
  710. nmb->header.nscount);
  711. /*
  712. * The spec says we must put compressed name pointers
  713. * in the following outgoing packets :
  714. * NAME_REGISTRATION_REQUEST, NAME_REFRESH_REQUEST,
  715. * NAME_RELEASE_REQUEST.
  716. */
  717. if((nmb->header.response == False) &&
  718. ((nmb->header.opcode == NMB_NAME_REG_OPCODE) ||
  719. (nmb->header.opcode == NMB_NAME_RELEASE_OPCODE) ||
  720. (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_8) ||
  721. (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_9) ||
  722. (nmb->header.opcode == NMB_NAME_MULTIHOMED_REG_OPCODE)) &&
  723. (nmb->header.arcount == 1)) {
  724. offset += put_compressed_name_ptr(ubuf,offset,nmb->additional,12);
  725. } else if (nmb->header.arcount) {
  726. offset += put_res_rec((char *)ubuf,offset,nmb->additional,
  727. nmb->header.arcount);
  728. }
  729. return(offset);
  730. }
  731. /*******************************************************************
  732. send a packet_struct
  733. ******************************************************************/
  734. BOOL send_packet(struct packet_struct *p)
  735. {
  736. char buf[1024];
  737. int len=0;
  738. memset(buf,'\0',sizeof(buf));
  739. switch (p->packet_type)
  740. {
  741. case NMB_PACKET:
  742. len = build_nmb(buf,p);
  743. debug_nmb_packet(p);
  744. break;
  745. case DGRAM_PACKET:
  746. len = build_dgram(buf,p);
  747. break;
  748. }
  749. if (!len) return(False);
  750. return(send_udp(p->fd,buf,len,p->ip,p->port));
  751. }
  752. /****************************************************************************
  753. receive a packet with timeout on a open UDP filedescriptor
  754. The timeout is in milliseconds
  755. ***************************************************************************/
  756. struct packet_struct *receive_packet(int fd,enum packet_type type,int t)
  757. {
  758. fd_set fds;
  759. struct timeval timeout;
  760. FD_ZERO(&fds);
  761. FD_SET(fd,&fds);
  762. timeout.tv_sec = t/1000;
  763. timeout.tv_usec = 1000*(t%1000);
  764. sys_select(fd+1,&fds,&timeout);
  765. if (FD_ISSET(fd,&fds))
  766. return(read_packet(fd,type));
  767. return(NULL);
  768. }
  769. #if 0
  770. /****************************************************************************
  771. return the number of bits that match between two 4 character buffers
  772. ***************************************************************************/
  773. static int matching_bits(uchar *p1, uchar *p2)
  774. {
  775. int i, j, ret = 0;
  776. for (i=0; i<4; i++) {
  777. if (p1[i] != p2[i]) break;
  778. ret += 8;
  779. }
  780. if (i==4) return ret;
  781. for (j=0; j<8; j++) {
  782. if ((p1[i] & (1<<(7-j))) != (p2[i] & (1<<(7-j)))) break;
  783. ret++;
  784. }
  785. return ret;
  786. }
  787. static uchar sort_ip[4];
  788. /****************************************************************************
  789. compare two query reply records
  790. ***************************************************************************/
  791. static int name_query_comp(uchar *p1, uchar *p2)
  792. {
  793. return matching_bits(p2+2, sort_ip) - matching_bits(p1+2, sort_ip);
  794. }
  795. /****************************************************************************
  796. sort a set of 6 byte name query response records so that the IPs that
  797. have the most leading bits in common with the specified address come first
  798. ***************************************************************************/
  799. void sort_query_replies(char *data, int n, struct in_addr ip)
  800. {
  801. if (n <= 1) return;
  802. putip(sort_ip, (char *)&ip);
  803. qsort(data, n, 6, QSORT_CAST name_query_comp);
  804. }
  805. #endif /*0 */