inet.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Lukas Mejdrech
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <net/socket_codes.h>
00038 #include <net/in.h>
00039 #include <net/in6.h>
00040 #include <net/inet.h>
00041 
00042 #include <errno.h>
00043 #include <mem.h>
00044 #include <stdio.h>
00045 #include <str.h>
00046 
00058 int
00059 inet_ntop(uint16_t family, const uint8_t *data, char *address, size_t length)
00060 {
00061         if ((!data) || (!address))
00062                 return EINVAL;
00063 
00064         switch (family) {
00065         case AF_INET:
00066                 /* Check output buffer size */
00067                 if (length < INET_ADDRSTRLEN)
00068                         return ENOMEM;
00069                         
00070                 /* Fill buffer with IPv4 address */
00071                 snprintf(address, length, "%hhu.%hhu.%hhu.%hhu",
00072                     data[0], data[1], data[2], data[3]);
00073 
00074                 return EOK;
00075 
00076         case AF_INET6:
00077                 /* Check output buffer size */
00078                 if (length < INET6_ADDRSTRLEN)
00079                         return ENOMEM;
00080                 
00081                 /* Fill buffer with IPv6 address */
00082                 snprintf(address, length,
00083                     "%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:"
00084                     "%hhx%hhx:%hhx%hhx",
00085                     data[0], data[1], data[2], data[3], data[4], data[5],
00086                     data[6], data[7], data[8], data[9], data[10], data[11],
00087                     data[12], data[13], data[14], data[15]);
00088                 
00089                 return EOK;
00090 
00091         default:
00092                 return ENOTSUP;
00093         }
00094 }
00095 
00108 int inet_pton(uint16_t family, const char *address, uint8_t *data)
00109 {
00111         int base;
00113         size_t bytes;
00115         int count;
00116 
00117         const char *next;
00118         char *last;
00119         int index;
00120         size_t shift;
00121         unsigned long value;
00122 
00123         if (!data)
00124                 return EINVAL;
00125 
00126         /* Set processing parameters */
00127         switch (family) {
00128         case AF_INET:
00129                 count = 4;
00130                 base = 10;
00131                 bytes = 1;
00132                 break;
00133 
00134         case AF_INET6:
00135                 count = 16;
00136                 base = 16;
00137                 bytes = 4;
00138                 break;
00139 
00140         default:
00141                 return ENOTSUP;
00142         }
00143 
00144         /* Erase if no address */
00145         if (!address) {
00146                 bzero(data, count);
00147                 return ENOENT;
00148         }
00149 
00150         /* Process string from the beginning */
00151         next = address;
00152         index = 0;
00153         do {
00154                 /* If the actual character is set */
00155                 if (next && *next) {
00156 
00157                         /* If not on the first character */
00158                         if (index) {
00159                                 /* Move to the next character */
00160                                 ++next;
00161                         }
00162 
00163                         /* Parse the actual integral value */
00164                         value = strtoul(next, &last, base);
00165                         /*
00166                          * Remember the last problematic character
00167                          * should be either '.' or ':' but is ignored to be
00168                          * more generic
00169                          */
00170                         next = last;
00171 
00172                         /* Fill the address data byte by byte */
00173                         shift = bytes - 1;
00174                         do {
00175                                 /* like little endian */
00176                                 data[index + shift] = value;
00177                                 value >>= 8;
00178                         } while(shift --);
00179 
00180                         index += bytes;
00181                 } else {
00182                         /* Erase the rest of the address */
00183                         bzero(data + index, count - index);
00184                         return EOK;
00185                 }
00186         } while (index < count);
00187 
00188         return EOK;
00189 }
00190 

Generated on Thu Jun 2 07:45:46 2011 for HelenOS/USB by  doxygen 1.4.7