nbname.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <ip.h>
  12. #include <thread.h>
  13. #include "netbios.h"
  14. static int
  15. decodehex(char c)
  16. {
  17. if (c >= '0' && c <= '9')
  18. return c - '0';
  19. else if (c >= 'A' && c <= 'F')
  20. return c - 'A' + 10;
  21. else if (c >= 'a' && c <= 'f')
  22. return c - 'a' + 10;
  23. return 0;
  24. }
  25. static char
  26. encodehex(int n)
  27. {
  28. if (n >= 0 && n <= 9)
  29. return '0' + n;
  30. if (n >= 10 && n <= 15)
  31. return 'a' + (n - 10);
  32. return '?';
  33. }
  34. static int
  35. _nameextract(uint8_t *base, uint8_t *p, uint8_t *ep, int k,
  36. uint8_t *outbuf, int outbufmaxlen, int *outbuflenp)
  37. {
  38. uint8_t *op, *oep, *savep;
  39. savep = p;
  40. op = outbuf;
  41. oep = outbuf + outbufmaxlen;
  42. for (;;) {
  43. uint8_t b;
  44. int n;
  45. if (p >= ep)
  46. return 0;
  47. b = *p++;
  48. if (b == 0)
  49. break;
  50. if (k) {
  51. if (op >= oep)
  52. return 0;
  53. *op++ = '.';
  54. }
  55. if ((b & 0xc0) == 0xc0) {
  56. uint16_t off;
  57. if (ep - p < 2)
  58. return 0;
  59. off = nhgets(p - 1) & 0x3fff; p++;
  60. if (_nameextract(base, base + off, p, k, op, oep - ep, &n) == 0)
  61. return 0;
  62. op += n;
  63. }
  64. else if ((b & 0xc0) != 0x00)
  65. return 0;
  66. else if (b != 0x20)
  67. return 0;
  68. else {
  69. int x;
  70. if (p + b > ep)
  71. return 0;
  72. if (op + b / 2 > oep)
  73. return 0;
  74. for (x = 0; x < b; x += 2) {
  75. uint8_t hn, ln;
  76. if (*p < 'A' || *p >= 'A' + 16)
  77. return 0;
  78. hn = *p++ - 'A';
  79. if (*p < 'A' || *p >= 'A' + 16)
  80. return 0;
  81. ln = *p++ - 'A';
  82. *op++ = (hn << 4) | ln;
  83. }
  84. }
  85. k++;
  86. }
  87. *outbuflenp = op - outbuf;
  88. return p - savep;
  89. }
  90. int
  91. nbnamedecode(uint8_t *base, uint8_t *p, uint8_t *ep, NbName nbname)
  92. {
  93. int n;
  94. int rv = _nameextract(base, p, ep, 0, nbname, NbNameLen, &n);
  95. if (rv == 0)
  96. return rv;
  97. if (n != NbNameLen)
  98. return 0;
  99. return rv;
  100. }
  101. int
  102. nbnameencode(uint8_t *ap, uint8_t *ep, NbName name)
  103. {
  104. uint8_t *p = ap;
  105. int i;
  106. if (p + 1 + 2 * NbNameLen + 1 > ep)
  107. return 0;
  108. *p++ = NbNameLen * 2;
  109. for (i = 0; i < NbNameLen; i++) {
  110. *p++ = 'A' + ((name[i] >> 4) & 0xf);
  111. *p++ = 'A' + (name[i] & 0xf);
  112. }
  113. *p++ = 0;
  114. return p - ap;
  115. }
  116. void
  117. nbnamecpy(NbName n1, NbName n2)
  118. {
  119. memcpy(n1, n2, NbNameLen);
  120. }
  121. void
  122. nbmknamefromstring(NbName nbname, char *s)
  123. {
  124. int i;
  125. memset(nbname, ' ', NbNameLen - 1);
  126. nbname[NbNameLen - 1] = 0;
  127. i = 0;
  128. while (*s) {
  129. if (*s == '\\' && *(s + 1) == 'x') {
  130. s += 2;
  131. if (*s == 0 || *(s + 1) == 0)
  132. return;
  133. nbname[NbNameLen - 1] = (decodehex(s[0]) << 4) | decodehex(s[1]);
  134. return;
  135. } else {
  136. if (i < NbNameLen - 1)
  137. nbname[i++] = toupper(*s);
  138. s++;
  139. }
  140. }
  141. }
  142. void
  143. nbmknamefromstringandtype(NbName nbname, char *s, uint8_t type)
  144. {
  145. nbmknamefromstring(nbname, s);
  146. nbname[NbNameLen - 1] = type;
  147. }
  148. void
  149. nbmkstringfromname(char *buf, int buflen, NbName name)
  150. {
  151. int x;
  152. for (x = 0; x < NbNameLen - 1; x++) {
  153. if (name[x] == ' ')
  154. break;
  155. if (buflen > 1) {
  156. *buf++ = tolower(name[x]);
  157. buflen--;
  158. }
  159. }
  160. if (name[NbNameLen - 1] != 0) {
  161. if (buflen > 1) {
  162. *buf++ = '\\';
  163. buflen--;
  164. }
  165. if (buflen > 1) {
  166. *buf++ = 'x';
  167. buflen--;
  168. }
  169. if (buflen > 1) {
  170. *buf++ = encodehex(name[NbNameLen - 1] >> 4);
  171. buflen--;
  172. }
  173. if (buflen > 1)
  174. *buf++ = encodehex(name[NbNameLen - 1] & 0x0f);
  175. }
  176. *buf = 0;
  177. }
  178. int
  179. nbnameisany(NbName name)
  180. {
  181. return name[0] == '*';
  182. }
  183. int
  184. nbnameequal(NbName name1, NbName name2)
  185. {
  186. if (name1[NbNameLen - 1] != name2[NbNameLen - 1])
  187. return 0;
  188. if (nbnameisany(name1))
  189. return 1;
  190. if (nbnameisany(name2))
  191. return 1;
  192. return memcmp(name1, name2, NbNameLen - 1) == 0;
  193. }
  194. int
  195. nbnamefmt(Fmt *f)
  196. {
  197. uint8_t *n;
  198. char buf[100];
  199. n = va_arg(f->args, uint8_t *);
  200. nbmkstringfromname(buf, sizeof(buf), n);
  201. return fmtstrcpy(f, buf);
  202. }
  203. typedef struct NbNameTableEntry NbNameTableEntry;
  204. struct NbNameTableEntry {
  205. NbName name;
  206. NbNameTableEntry *next;
  207. };
  208. static struct {
  209. QLock;
  210. NbNameTableEntry *head;
  211. } nbnametable;
  212. int
  213. nbnametablefind(NbName name, int add)
  214. {
  215. int rv;
  216. NbNameTableEntry *p;
  217. qlock(&nbnametable);
  218. for (p = nbnametable.head; p; p = p->next) {
  219. if (nbnameequal(p->name, name)) {
  220. qunlock(&nbnametable);
  221. return 1;
  222. }
  223. }
  224. if (add) {
  225. p = nbemalloc(sizeof(*p));
  226. nbnamecpy(p->name, name);
  227. p->next = nbnametable.head;
  228. nbnametable.head = p;
  229. rv = 1;
  230. }
  231. else
  232. rv = 0;
  233. qunlock(&nbnametable);
  234. return rv;
  235. }
  236. typedef struct NbRemoteNameTableEntry NbRemoteNameTableEntry;
  237. struct NbRemoteNameTableEntry {
  238. NbName name;
  239. char ipaddr[IPaddrlen];
  240. int32_t expire;
  241. NbRemoteNameTableEntry *next;
  242. };
  243. static struct {
  244. QLock;
  245. NbRemoteNameTableEntry *head;
  246. } nbremotenametable;
  247. int
  248. nbremotenametablefind(NbName name, uint8_t *ipaddr)
  249. {
  250. NbRemoteNameTableEntry *p, **pp;
  251. int32_t now = time(nil);
  252. qlock(&nbremotenametable);
  253. for (pp = &nbremotenametable.head; (p = *pp) != nil;) {
  254. if (p->expire <= now) {
  255. //print("nbremotenametablefind: expired %B\n", p->name);
  256. *pp = p->next;
  257. free(p);
  258. continue;
  259. }
  260. if (nbnameequal(p->name, name)) {
  261. ipmove(ipaddr, p->ipaddr);
  262. qunlock(&nbremotenametable);
  263. return 1;
  264. }
  265. pp = &p->next;
  266. }
  267. qunlock(&nbremotenametable);
  268. return 0;
  269. }
  270. int
  271. nbremotenametableadd(NbName name, uint8_t *ipaddr, uint32_t ttl)
  272. {
  273. NbRemoteNameTableEntry *p;
  274. qlock(&nbremotenametable);
  275. for (p = nbremotenametable.head; p; p = p->next)
  276. if (nbnameequal(p->name, name))
  277. break;
  278. if (p == nil) {
  279. p = nbemalloc(sizeof(*p));
  280. p->next = nbremotenametable.head;
  281. nbremotenametable.head = p;
  282. nbnamecpy(p->name, name);
  283. }
  284. ipmove(p->ipaddr, ipaddr);
  285. p->expire = time(nil) + ttl;
  286. //print("nbremotenametableadd: %B ttl %lud expire %ld\n", p->name, ttl, p->expire);
  287. qunlock(&nbremotenametable);
  288. return 1;
  289. }