home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 29 Fixes_o / 29-Fixes_o.zip / netcsd.zip / MAPNAME.C < prev    next >
Text File  |  1991-02-22  |  8KB  |  261 lines

  1. /**********************************************************************
  2.  *                                                                    *
  3.  *  Licensed Materials - Property of IBM                              *
  4.  *  94F0534, 94F0535 (C) Copyright IBM Corp. 1991.                    *
  5.  *                   (C) Copyright Syntax, Inc. 1990.                 *
  6.  *  All rights reserved.                                              *
  7.  *  US Government Users Restricted Rights -                           *
  8.  *  Use, duplication or disclosure restricted by GSA ADP Schedule     *
  9.  *  Contract with IBM Corp.                                           *
  10.  *                                                                    *
  11.  **********************************************************************/
  12.  
  13.  
  14. /**********************************************************************
  15.  *
  16.  *  FILE NAME:  mapname.c
  17.  *
  18.  *  PURPOSE:    Display the mapping between NetBIOS names and
  19.  *              RFC-encoded names.  This source code is provided so
  20.  *              that a network administrator can compile this on the
  21.  *              machine that serves as a Domain Nameserver.  The program
  22.  *              can serve as a useful tool to convert NetBIOS names
  23.  *              when defining them in the name database.  
  24.  *
  25.  *  CHANGE HISTORY:
  26.  *
  27.  *  12/10/90 hj   Initial version
  28.  *  02/20/91 wtn  Added "E" for exiting
  29.  *  02/21/91 wtn  Added field lengths on the name entry fields.
  30.  *  02/22/91 wtn  Added "H" for on-line help
  31.  *
  32.  **********************************************************************/
  33.  
  34. static char copyright[] =
  35.    "\r\nCopyright (c) IBM Corp. 1991.\r\nCopyright (c) 1990 by Syntax, Inc\r\nAll rights reserved\r\n";
  36.  
  37. #include <stdio.h>
  38. #include <ctype.h>
  39.  
  40. #define NAMELEN 16
  41.  
  42. #define FIRST_ENC(c)  ((((c) >> 4) & 0xf) + 'A')
  43. #define SECOND_ENC(c) (((c) & 0xf) + 'A')
  44. #define DECODE(cp)    ((*(cp) - 'A') << 4 | (*((cp) + 1) - 'A'))
  45.  
  46. #define byte    unsigned char
  47.  
  48. static char inbuf[256];
  49.  
  50.  
  51. static int
  52. unhex(x)
  53. char x;
  54. {
  55.   if ((x >= 'a') && (x <= 'f')) return x - 'a' + 10;
  56.   if ((x >= 'A') && (x <= 'F')) return x - 'A' + 10;
  57.   if ((x >= '0') && (x <= '9')) return x - '0';
  58.   return -1;
  59. }
  60.  
  61. static
  62. getoct(s)
  63. char *s;
  64. {
  65. int n;
  66.  
  67.   if ((*s < '0') || (*s > '3')) return -1;
  68.   n = 64 * (*s++ - '0');
  69.   if ((*s < '0') || (*s > '7')) return -1;
  70.   n += 8 * (*s++ - '0');
  71.   if ((*s < '0') || (*s > '7')) return -1;
  72.   n += *s++ - '0';
  73.   return n;
  74. }
  75.  
  76. char *
  77. nbname(s)
  78. register char *s;
  79. {
  80. static byte buf[17];
  81. int i = 0,n;
  82.  
  83.   memset(buf,0,16);
  84.   while (*s && i < 16) {
  85.     if (*s == '\\') {
  86.       switch(*++s) {
  87.         case '\\' : buf[i++] = '\\'; ++s; break;
  88.         case 'a'  : buf[i++] = '\a'; ++s; break;
  89.         case 'b'  : buf[i++] = '\b'; ++s; break;
  90.         case 'n'  : buf[i++] = '\n'; ++s; break;
  91.         case 'r'  : buf[i++] = '\r'; ++s; break;
  92.         case 't'  : buf[i++] = '\t'; ++s; break;
  93.         case 'x'  :
  94.           if (!isxdigit(s[1]) || !isxdigit(s[2])) buf[i++] = '\\';
  95.           else {
  96.             s++;
  97.             buf[i++] = unhex(s[0]) * 16 + unhex(s[1]);
  98.             s += 2;
  99.           }
  100.           break;
  101.         case '0': case '1': case '2': case '3':
  102.           if ((n = getoct(s)) < 0) buf[i++] = '\\';
  103.           else {
  104.             buf[i++] = n;
  105.             s += 3;
  106.           }
  107.           break;
  108.         default: buf[i++] = *s++; break;
  109.       }
  110.     }
  111.     else buf[i++] = *s++;
  112.   }
  113.   buf[16] = 0;
  114.   return buf;
  115. }
  116.  
  117. static
  118. n_encode(crypt,clear)
  119. byte *crypt,*clear;
  120. {
  121. int i;
  122.  
  123.   for (i = 0; i < NAMELEN; i++) {
  124.     *crypt++ = FIRST_ENC(clear[i]);
  125.     *crypt++ = SECOND_ENC(clear[i]);
  126.   }
  127.  
  128.   *crypt = '\0';
  129. }
  130.  
  131. static
  132. n_decode(clear,crypt)
  133. byte *clear,*crypt;
  134. {
  135. int i;
  136.  
  137.   for (i = 0; i < NAMELEN; i++) {
  138.     *clear++ = DECODE(crypt);
  139.     crypt += 2;
  140.   }
  141.   *clear = '\0';
  142. }
  143.  
  144. static char *
  145. asc(s,l)
  146. byte *s;
  147. int l;
  148. {
  149. static byte buf[64],*bp;
  150.  
  151.   bp = buf;
  152.   while (l--) { *(bp++) = isprint(*s) ? *s : '.'; s++; }
  153.   *bp = 0;
  154.   return buf;
  155. }
  156.  
  157. static char *
  158. hexnm(nbn)
  159. byte *nbn;
  160. {
  161. int i;
  162. static char hex[64];
  163. register char *h;
  164.  
  165.   for (h=hex,i=0; i<16; i++) { sprintf(h,"%02.2x ",*(nbn++)); h += strlen(h); }
  166.   *(h-1) = 0;
  167.   return hex;
  168. }
  169.  
  170. static
  171. show(rfc,clear)
  172. char *rfc,*clear;
  173. {
  174.   printf("NetBIOS    : [%s] %s\n",asc(clear,16),hexnm(clear));
  175.   printf("RFC encoded: %s\n",rfc);
  176. }
  177.  
  178. main()
  179. {
  180. char rfcname[68],clearnm[68],*ascii,x[68];
  181. int i;
  182.  
  183.   printf("Name types:\n\n"
  184.          "R == RFC encoded (converted to NetBIOS)\n"
  185.          "N == NetBIOS     (converted to RFC encoded)\n"
  186.          "H == Help        \n"
  187.          "E == Exit        \n\n"
  188.          "RFC encoded names consist of letters A through P only.\n"
  189.          "NetBIOS names consist of any ASCII characters.\n\n"
  190.          "Enter non-printable ASCII characters using the \"C\" \"\\xxx\" conventions.\n\n");
  191.  
  192.   for (;;) {
  193.     printf("\nName type? R(FC), N(etBIOS), H(elp) or E(xit): ");
  194.     if (!gets(x)) break;
  195.     memset(rfcname,0,68);
  196.     memset(clearnm,0,68);
  197.     memset(inbuf,0,256);
  198.  
  199.     switch(x[0]) {
  200.  
  201.       case 'r' :
  202.       case 'R' :
  203.         printf("RFC encoded: [................................]");
  204.         printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
  205.         if (!gets(inbuf)) break;
  206.         strncpy(rfcname,inbuf,33);
  207.         rfcname[32] = 0;
  208.         for (i=0; i<strlen(rfcname); i++)
  209.           rfcname[i] = toupper(rfcname[i]);
  210.         if ((i=strspn(rfcname,"ABCDEFGHIJKLMNOP")) < strlen(rfcname)) {
  211.           printf("The '%c' in %s is illegal; legal characters are A - P\n",
  212.                  rfcname[i],rfcname);
  213.           break;
  214.         }
  215.         memset(rfcname+strlen(rfcname),'A',32-strlen(rfcname));
  216.         n_decode(clearnm,rfcname);
  217.         n_encode(rfcname,clearnm);
  218.         show(rfcname,clearnm);
  219.         break;
  220.  
  221.       case 'n' :
  222.       case 'N' :
  223.         printf("NetBIOS    : [................]\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
  224.         if (!gets(inbuf)) break;
  225.         strncpy(clearnm,inbuf,68);
  226.         clearnm[64] = 0;
  227.         n_encode(rfcname,ascii=nbname(clearnm));
  228.         n_decode(ascii,rfcname);
  229.         show(rfcname,ascii);
  230.         break;
  231.  
  232.       case 'h' :
  233.       case 'H' :
  234.         printf("\n");
  235.         printf("This tool is used to convert between NetBIOS names and RFC-encoded Domain\n");
  236.         printf("Names.  This tool is particularly useful when defining RFC-encoded Domain \n");
  237.         printf("Names in a Domain Nameserver.  The algorithm used for these conversions is\n");
  238.         printf("specified in the Network working Group Request for Comments (RFC) 1001 and \n");
  239.         printf("1002 (dated March 1987).  The conversion algorithm is described in Appendix A\n");
  240.         printf("of the User's Guide.  For more details please refer to RFC 1001 and 1002.\n");
  241.         printf("\n");
  242.         printf("N(etBIOS) - converts a 16 byte NetBIOS name into the 32 byte RFC-encoded name\n");
  243.         printf("            that may be used as a valid Domain Host Name.  For usage on an \n");
  244.         printf("            remote internet, the resulting RFC-encoded name should have a \n");
  245.         printf("            Domain Name appended to it.\n");
  246.         printf("\n");
  247.         printf("R(FC)     - converts a 32 byte RFC-encoded name back to a 16 byte NetBIOS name.\n");
  248.         printf("\n");
  249.         break;
  250.  
  251.       case 'e' :
  252.       case 'E' :
  253.         exit(0);
  254.  
  255.       default :
  256.         break;
  257.  
  258.     }
  259.   }
  260. }
  261.