Back to index

glibc  2.9
nsap_addr.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1996-1999 by Internet Software Consortium.
00003  *
00004  * Permission to use, copy, modify, and distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00009  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00010  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00011  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00012  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00013  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00014  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00015  * SOFTWARE.
00016  */
00017 
00018 #if defined(LIBC_SCCS) && !defined(lint)
00019 static const char rcsid[] = "$BINDId: nsap_addr.c,v 8.10 1999/10/13 16:39:28 vixie Exp $";
00020 #endif /* LIBC_SCCS and not lint */
00021 
00022 #include <sys/types.h>
00023 #include <sys/param.h>
00024 #include <sys/socket.h>
00025 
00026 #include <netinet/in.h>
00027 #include <arpa/inet.h>
00028 #include <arpa/nameser.h>
00029 
00030 #include <ctype.h>
00031 #include <resolv.h>
00032 
00033 static char
00034 xtob(int c) {
00035        return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
00036 }
00037 
00038 u_int
00039 inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
00040        u_char c, nib;
00041        u_int len = 0;
00042 
00043        while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
00044               if (c == '.' || c == '+' || c == '/')
00045                      continue;
00046               if (!isascii(c))
00047                      return (0);
00048               c = toupper(c);
00049               if (isxdigit(c)) {
00050                      nib = xtob(c);
00051                      c = *ascii++;
00052                      if (c != '\0') {
00053                             c = toupper(c);
00054                             if (isxdigit(c)) {
00055                                    *binary++ = (nib << 4) | xtob(c);
00056                                    len++;
00057                             } else
00058                                    return (0);
00059                      }
00060                      else
00061                             return (0);
00062               }
00063               else
00064                      return (0);
00065        }
00066        return (len);
00067 }
00068 
00069 char *
00070 inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
00071        int nib;
00072        int i;
00073        static char tmpbuf[255*2 + 128];
00074        char *start;
00075 
00076        if (ascii)
00077               start = ascii;
00078        else {
00079               ascii = tmpbuf;
00080               start = tmpbuf;
00081        }
00082 
00083        if (binlen > 255)
00084               binlen = 255;
00085 
00086        for (i = 0; i < binlen; i++) {
00087               nib = *binary >> 4;
00088               *ascii++ = nib + (nib < 10 ? '0' : '7');
00089               nib = *binary++ & 0x0f;
00090               *ascii++ = nib + (nib < 10 ? '0' : '7');
00091               if (((i % 2) == 0 && (i + 1) < binlen))
00092                      *ascii++ = '.';
00093        }
00094        *ascii = '\0';
00095        return (start);
00096 }