wins.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2004 by Jean Pierre Gauthier. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY <YOUR NAME> AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <YOUR NAME>
  21. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /* ********************************************************* */
  31. /*
  32. Netbios WINS (RFC 1002) Name Query.
  33. Only Query Request Client Routine sending/Positive Name Query Response receiving
  34. are implemented.
  35. When the Netbios Name Query request UDP datagram is on the ethernet network, asking
  36. "Who is 'name'?", NutWinsNameQuery answers with the specified 'ipaddr' Ethernut IP address.
  37. Answer to Microsoft Windows/Internet Explorer calls by "http://name" command line
  38. (and even directly "name" as command line if "name" is not a shared folder).
  39. Launch for example :
  40. THREAD(wins_deamon, arg)
  41. {
  42. NutWinsNameQuery ( "myboard", inet_addr(MYIP) ) ;
  43. }
  44. */
  45. /* ********************************************************* */
  46. #include <cfg/os.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <sys/heap.h>
  50. #include <sys/socket.h>
  51. #include <sys/sock_var.h>
  52. #include <sys/types.h>
  53. #include <pro/wins.h>
  54. #include <netinet/in.h>
  55. #ifdef NUTDEBUG
  56. #include <stdio.h>
  57. #endif
  58. extern int toupper(int);
  59. /* ********************************************************* */
  60. /*!
  61. * \addtogroup xgWins
  62. */
  63. /*@{*/
  64. typedef struct {
  65. uint16_t id;
  66. uint16_t flags;
  67. uint16_t quests;
  68. uint16_t answers;
  69. uint16_t authrr;
  70. uint16_t addrr;
  71. uint8_t namelen;
  72. uint8_t name[33];
  73. uint16_t type;
  74. uint16_t class; /* end of request */
  75. uint32_t ttl;
  76. uint16_t len_rep;
  77. uint8_t node_flags;
  78. uint8_t node_type;
  79. uint32_t ip_addr; /* end of answer */
  80. } WINSHEADER;
  81. /* ********************************************************* */
  82. /* name : netbios label (15 chars max), ipaddr : network ordered IP address bytes */
  83. int NutWinsNameQuery(char * name, uint32_t ipaddr)
  84. {
  85. WINSHEADER *pkt = NULL;
  86. uint8_t *encoded = NULL;
  87. UDPSOCKET *sock;
  88. uint32_t raddr;
  89. uint16_t rport;
  90. uint8_t car;
  91. int i, j;
  92. if (strlen(name) > 15)
  93. return -1;
  94. if (((pkt = calloc(1, sizeof(WINSHEADER))) == NULL) || /* */
  95. ((encoded = calloc(33, 1)) == NULL) || /* */
  96. ((sock = NutUdpCreateSocket(137)) == 0) /* NETBIOS UDP port */
  97. ) {
  98. if (pkt != NULL)
  99. free(pkt);
  100. if (encoded != NULL)
  101. free(encoded);
  102. return -1;
  103. }
  104. j = 0;
  105. for (i = 0; i < 16; i++) { /* label 'compression' */
  106. car = toupper(i < strlen(name) ? name[i] : ' ');
  107. if (i == 15)
  108. car = 0;
  109. encoded[j] = (car >> 4) + 'A';
  110. encoded[j + 1] = (car & 0xf) + 'A';
  111. j += 2;
  112. }
  113. encoded[j] = 0;
  114. /* printf("local compressed name=\n%s \n", encoded); */
  115. for (;;) { /* infinite loop / Netbios deamon */
  116. NutUdpReceiveFrom(sock, &raddr, &rport, pkt, sizeof(WINSHEADER), 0);
  117. /* RFC1002 Name Query Request verification */
  118. if (((ntohs(pkt->flags) & 0xf800) != 0) || /* */
  119. (ntohs(pkt->quests) != 1) || /* */
  120. (pkt->namelen != 0x20) || /* */
  121. (ntohs(pkt->type) != 32) || /* */
  122. (ntohs(pkt->class) != 1) || /* */
  123. (strcmp((char *)pkt->name, (char *)encoded)))
  124. continue; /* bad request, try again */
  125. /* printf("Name=%s recognized\r\n", name); */
  126. /* build RFC1002 Positive Name Query Response */
  127. pkt->flags = htons(0x8580); /* Response flags */
  128. pkt->answers = htons(1);
  129. pkt->ttl = htonl((uint32_t) 60); /* 60 seconds validity */
  130. pkt->len_rep = htons(6);
  131. pkt->node_flags = pkt->node_type = pkt->quests = 0; /* B-type node, etc... */
  132. pkt->ip_addr = ipaddr; /* Returned IP Address, end of answer */
  133. NutUdpSendTo(sock, raddr, 137, pkt, sizeof(WINSHEADER)); /* send to netbios port */
  134. memset(pkt, 0, sizeof(WINSHEADER));
  135. }
  136. }
  137. /*@}*/