client.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 char *hmsg = "headers";
  15. int chatty = 1;
  16. void
  17. warning(char *fmt, ...)
  18. {
  19. char err[128];
  20. va_list arg;
  21. va_start(arg, fmt);
  22. vseprint(err, err+sizeof(err), fmt, arg);
  23. va_end(arg);
  24. syslog(1, "netbios-ns", err);
  25. if (chatty)
  26. print("%s\n", err);
  27. }
  28. static int
  29. udpannounce(void)
  30. {
  31. int data, ctl;
  32. char dir[64];
  33. char datafile[64+6];
  34. /* get a udp port */
  35. ctl = announce("udp!*!netbios-ns", dir);
  36. if(ctl < 0){
  37. warning("can't announce on netbios-ns udp port");
  38. return -1;
  39. }
  40. snprint(datafile, sizeof(datafile), "%s/data", dir);
  41. /* turn on header style interface */
  42. if(write(ctl, hmsg, strlen(hmsg)) , 0)
  43. abort(); /* hmsg */;
  44. data = open(datafile, ORDWR);
  45. if(data < 0){
  46. close(ctl);
  47. warning("can't announce on dns udp port");
  48. return -1;
  49. }
  50. close(ctl);
  51. return data;
  52. }
  53. #define BROADCAST 1
  54. void
  55. listen137(void *)
  56. {
  57. for (;;) {
  58. uint8_t msg[Udphdrsize + 576];
  59. int len = read(fd137, msg, sizeof(msg));
  60. if (len < 0)
  61. break;
  62. if (len >= Udphdrsize) {
  63. NbnsMessage *s;
  64. Udphdr *uh;
  65. uint8_t *p;
  66. uh = (Udphdr*)msg;
  67. p = msg + Udphdrsize;
  68. len -= Udphdrsize;
  69. s = nbnsconvM2S(p, len);
  70. if (s) {
  71. print("%I:%d -> %I:%d\n", uh->raddr, nhgets(uh->rport), uh->laddr, nhgets(uh->lport));
  72. nbnsdumpmessage(s);
  73. if (s->response) {
  74. NbnsTransaction *t;
  75. qlock(&transactionlist);
  76. for (t = transactionlist.head; t; t = t->next)
  77. if (t->id == s->id)
  78. break;
  79. if (t) {
  80. sendp(t->c, s);
  81. }
  82. else
  83. nbnsmessagefree(&s);
  84. qunlock(&transactionlist);
  85. }
  86. else
  87. nbnsmessagefree(&s);
  88. }
  89. }
  90. }
  91. }
  92. void
  93. usage(void)
  94. {
  95. fprint(2, "usage: %s [-u ipaddr] name\n", argv0);
  96. exits("usage");
  97. }
  98. void
  99. threadmain(int argc, char **argv)
  100. {
  101. int broadcast = 1, i, listen137thread, rv;
  102. char *ip;
  103. char ipaddr[IPaddrlen], serveripaddr[IPaddrlen];
  104. NbName nbname;
  105. ARGBEGIN {
  106. case 'u':
  107. broadcast = 0;
  108. ip = EARGF(usage());
  109. if (parseip(serveripaddr, ip) == -1)
  110. sysfatal("bad ip address %s", ip);
  111. break;
  112. default:
  113. usage();
  114. } ARGEND;
  115. if (argc == 0)
  116. usage();
  117. nbmknamefromstring(nbname, argv[0]);
  118. ipifc = readipifc("/net", nil, 0);
  119. if (ipifc == nil || ipifc->lifc == nil)
  120. sysfatal("no network interface");
  121. fmtinstall('I', eipfmt);
  122. ipmove(bcastaddr, ipifc->lifc->ip);
  123. for (i = 0; i < IPaddrlen; i++)
  124. bcastaddr[i] |= ~ipifc->lifc->mask[i];
  125. print("broadcasting to %I\n", bcastaddr);
  126. // setnetmtpt("/net");
  127. fd137 = udpannounce();
  128. listen137thread = proccreate(listen137, nil, 16384);
  129. rv = nbnsaddname(broadcast ? nil : serveripaddr, nbname, 3000, ipifc->lifc->ip);
  130. if (rv != 0)
  131. print("error code %d\n", rv);
  132. else
  133. print("%I\n", ipaddr);
  134. nbnsalarmend();
  135. threadint(listen137thread);
  136. }