findname.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. int
  15. nbnsfindname(uint8_t *serveripaddr, NbName name, uint8_t *ipaddr,
  16. uint32_t *ttlp)
  17. {
  18. NbnsMessage *nq;
  19. Alt aa[3];
  20. int tries = NbnsRetryBroadcast;
  21. NbnsAlarm *a;
  22. int rv;
  23. NbnsMessage *response;
  24. nq = nbnsmessagenamequeryrequestnew(0, serveripaddr == nil, name);
  25. if (nq == nil)
  26. return -1;
  27. a = nbnsalarmnew();
  28. if (a == nil) {
  29. free(nq);
  30. return -1;
  31. }
  32. aa[0].c = a->c;
  33. aa[0].v = nil;
  34. aa[0].op = CHANRCV;
  35. aa[1].op = CHANRCV;
  36. aa[2].op = CHANEND;
  37. while (tries > 0) {
  38. NbnsTransaction *t;
  39. nq->id = nbnsnextid();
  40. t = nbnstransactionnew(nq, serveripaddr);
  41. aa[1].c = t->c;
  42. aa[1].v = &response;
  43. nbnsalarmset(a, NbnsTimeoutBroadcast);
  44. for (;;) {
  45. int i;
  46. i = alt(aa);
  47. if (i == 0) {
  48. tries--;
  49. break;
  50. }
  51. else if (i == 1) {
  52. if (response->opcode == NbnsOpQuery) {
  53. nbnstransactionfree(&t);
  54. goto done;
  55. }
  56. nbnsmessagefree(&response);
  57. }
  58. }
  59. nbnstransactionfree(&t);
  60. }
  61. done:
  62. if (tries == 0)
  63. rv = -1;
  64. else {
  65. if (response->rcode != 0)
  66. rv = response->rcode;
  67. else if (response->an == nil)
  68. rv = -1;
  69. else {
  70. rv = 0;
  71. v4tov6(ipaddr, response->an->rdata + 2);
  72. if (ttlp)
  73. *ttlp = response->an->ttl;
  74. }
  75. nbnsmessagefree(&response);
  76. }
  77. nbnsalarmfree(&a);
  78. nbnsmessagefree(&nq);
  79. return rv;
  80. }