[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup net
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Internet protocol address conversion functions implementation.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <mem.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <string.h>
|
---|
| 41 |
|
---|
| 42 | #include "include/in.h"
|
---|
| 43 | #include "include/in6.h"
|
---|
| 44 | #include "include/inet.h"
|
---|
| 45 | #include "include/socket_codes.h"
|
---|
| 46 |
|
---|
[a64c64d] | 47 | int inet_ntop(uint16_t family, const uint8_t * data, char * address, size_t length){
|
---|
| 48 | if((! data) || (! address)){
|
---|
| 49 | return EINVAL;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | switch(family){
|
---|
| 53 | case AF_INET:
|
---|
| 54 | // check the output buffer size
|
---|
| 55 | if(length < INET_ADDRSTRLEN){
|
---|
| 56 | return ENOMEM;
|
---|
| 57 | }
|
---|
| 58 | // fill the buffer with the IPv4 address
|
---|
| 59 | snprintf(address, length, "%hhu.%hhu.%hhu.%hhu", data[0], data[1], data[2], data[3]);
|
---|
| 60 | return EOK;
|
---|
| 61 | case AF_INET6:
|
---|
| 62 | // check the output buffer size
|
---|
| 63 | if(length < INET6_ADDRSTRLEN){
|
---|
| 64 | return ENOMEM;
|
---|
| 65 | }
|
---|
| 66 | // fill the buffer with the IPv6 address
|
---|
| 67 | snprintf(address, length, "%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx", data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15]);
|
---|
| 68 | return EOK;
|
---|
| 69 | default:
|
---|
| 70 | return ENOTSUP;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[aadf01e] | 74 | int inet_pton(uint16_t family, const char * address, uint8_t * data){
|
---|
[a64c64d] | 75 | /** The base number of the values.
|
---|
| 76 | */
|
---|
| 77 | int base;
|
---|
| 78 | /** The number of bytes per a section.
|
---|
| 79 | */
|
---|
| 80 | size_t bytes;
|
---|
| 81 | /** The number of bytes of the address data.
|
---|
| 82 | */
|
---|
| 83 | int count;
|
---|
| 84 |
|
---|
[aadf01e] | 85 | const char * next;
|
---|
| 86 | char * last;
|
---|
| 87 | int index;
|
---|
| 88 | size_t shift;
|
---|
| 89 | unsigned long value;
|
---|
[21580dd] | 90 |
|
---|
[aadf01e] | 91 | if(! data){
|
---|
| 92 | return EINVAL;
|
---|
| 93 | }
|
---|
[a64c64d] | 94 |
|
---|
| 95 | // set the processing parameters
|
---|
[aadf01e] | 96 | switch(family){
|
---|
[21580dd] | 97 | case AF_INET:
|
---|
| 98 | count = 4;
|
---|
| 99 | base = 10;
|
---|
| 100 | bytes = 1;
|
---|
| 101 | break;
|
---|
| 102 | case AF_INET6:
|
---|
| 103 | count = 16;
|
---|
| 104 | base = 16;
|
---|
| 105 | bytes = 4;
|
---|
| 106 | break;
|
---|
| 107 | default:
|
---|
| 108 | return ENOTSUP;
|
---|
| 109 | }
|
---|
[a64c64d] | 110 |
|
---|
| 111 | // erase if no address
|
---|
[aadf01e] | 112 | if(! address){
|
---|
| 113 | bzero(data, count);
|
---|
[21580dd] | 114 | return ENOENT;
|
---|
| 115 | }
|
---|
[a64c64d] | 116 |
|
---|
| 117 | // process the string from the beginning
|
---|
[21580dd] | 118 | next = address;
|
---|
| 119 | index = 0;
|
---|
| 120 | do{
|
---|
[a64c64d] | 121 | // if the actual character is set
|
---|
[aadf01e] | 122 | if(next && (*next)){
|
---|
[a64c64d] | 123 |
|
---|
| 124 | // if not on the first character
|
---|
[aadf01e] | 125 | if(index){
|
---|
[a64c64d] | 126 | // move to the next character
|
---|
[aadf01e] | 127 | ++ next;
|
---|
| 128 | }
|
---|
[a64c64d] | 129 |
|
---|
| 130 | // parse the actual integral value
|
---|
[aadf01e] | 131 | value = strtoul(next, &last, base);
|
---|
[a64c64d] | 132 | // remember the last problematic character
|
---|
| 133 | // should be either '.' or ':' but is ignored to be more generic
|
---|
[21580dd] | 134 | next = last;
|
---|
[a64c64d] | 135 |
|
---|
| 136 | // fill the address data byte by byte
|
---|
[21580dd] | 137 | shift = bytes - 1;
|
---|
| 138 | do{
|
---|
| 139 | // like little endian
|
---|
[aadf01e] | 140 | data[index + shift] = value;
|
---|
[21580dd] | 141 | value >>= 8;
|
---|
[aadf01e] | 142 | }while(shift --);
|
---|
[a64c64d] | 143 |
|
---|
[21580dd] | 144 | index += bytes;
|
---|
| 145 | }else{
|
---|
[a64c64d] | 146 | // erase the rest of the address
|
---|
[aadf01e] | 147 | bzero(data + index, count - index);
|
---|
[21580dd] | 148 | return EOK;
|
---|
| 149 | }
|
---|
[aadf01e] | 150 | }while(index < count);
|
---|
[21580dd] | 151 |
|
---|
[a64c64d] | 152 | return EOK;
|
---|
[21580dd] | 153 | }
|
---|
| 154 |
|
---|
| 155 | /** @}
|
---|
| 156 | */
|
---|