You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nmb.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "resolv.h"
  2. #include "string.h"
  3. #include "dns.h"
  4. #include "nic.h"
  5. #include "nmb.h"
  6. /*
  7. * Convert a standard NUL-terminated string to an NBNS query name.
  8. *
  9. * Returns a pointer to the character following the constructed NBNS
  10. * query name.
  11. *
  12. */
  13. static inline char * nbns_make_name ( char *dest, const char *name ) {
  14. char nb_name[16];
  15. char c;
  16. int i;
  17. uint16_t *d;
  18. *(dest++) = 32; /* Length is always 32 */
  19. /* Name encoding is as follows: pad the name with spaces to
  20. * length 15, and add a NUL. Take this 16-byte string, split
  21. * it into nibbles and add 0x41 to each nibble to form a byte
  22. * of the resulting name string.
  23. */
  24. memset ( nb_name, ' ', 15 );
  25. nb_name[15] = '\0';
  26. memcpy ( nb_name, name, strlen ( name ) ); /* Do not copy NUL */
  27. d = ( uint16_t * ) dest;
  28. for ( i = 0 ; i < 16 ; i++ ) {
  29. c = nb_name[i];
  30. *( d++ ) = htons ( ( ( c | ( c << 4 ) ) & 0x0f0f ) + 0x4141 );
  31. }
  32. dest = ( char * ) d;
  33. *(dest++) = 0; /* Terminating 0-length name component */
  34. return dest;
  35. }
  36. /*
  37. * Resolve a name using NMB
  38. *
  39. */
  40. static int nmb_resolv ( struct in_addr *addr, const char *name ) {
  41. struct dns_query query;
  42. struct dns_query_info *query_info;
  43. struct dns_header *reply;
  44. struct dns_rr_info *rr_info;
  45. struct dns_rr_info_nb *rr_info_nb;
  46. struct sockaddr_in nameserver;
  47. DBG ( "NMB resolving %s\n", name );
  48. /* Set up the query data */
  49. nameserver.sin_addr.s_addr = INADDR_BROADCAST;
  50. nameserver.sin_port = NBNS_UDP_PORT;
  51. memset ( &query, 0, sizeof ( query ) );
  52. query.dns.id = htons ( 1 );
  53. query.dns.flags = htons ( DNS_FLAG_QUERY | DNS_FLAG_OPCODE_QUERY |
  54. DNS_FLAG_RD | DNS_FLAG_BROADCAST );
  55. query.dns.qdcount = htons ( 1 );
  56. query_info = ( void * ) nbns_make_name ( query.payload, name );
  57. query_info->qtype = htons ( DNS_TYPE_NB );
  58. query_info->qclass = htons ( DNS_CLASS_IN );
  59. /* Issue query, wait for reply */
  60. reply = dns_query ( &query,
  61. ( ( ( char * ) query_info )
  62. + sizeof ( *query_info )
  63. - ( ( char * ) &query ) ),
  64. &nameserver );
  65. if ( ! reply ) {
  66. DBG ( "NMB got no response via %@ (port %d)\n",
  67. nameserver.sin_addr.s_addr,
  68. nameserver.sin_port );
  69. return 0;
  70. }
  71. /* Search through response for useful answers. */
  72. rr_info = dns_find_rr ( &query, reply );
  73. if ( ! rr_info ) {
  74. DBG ( "NMB got invalid response\n" );
  75. return 0;
  76. }
  77. /* Check type of response */
  78. if ( ntohs ( rr_info->type ) != DNS_TYPE_NB ) {
  79. DBG ( "NMB got answer type %hx (wanted %hx)\n",
  80. ntohs ( rr_info->type ), DNS_TYPE_NB );
  81. return 0;
  82. }
  83. /* Read response */
  84. rr_info_nb = ( struct dns_rr_info_nb * ) rr_info;
  85. *addr = rr_info_nb->nb_address;
  86. DBG ( "NMB found address %@\n", addr->s_addr );
  87. return 1;
  88. }
  89. struct resolver nmb_resolver __resolver = {
  90. .name = "NMB",
  91. .resolv = nmb_resolv,
  92. };