Changes in uspace/srv/net/inet.c [a64c64d:aadf01e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/inet.c
ra64c64d raadf01e 45 45 #include "include/socket_codes.h" 46 46 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 size55 if(length < INET_ADDRSTRLEN){56 return ENOMEM;57 }58 // fill the buffer with the IPv4 address59 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 size63 if(length < INET6_ADDRSTRLEN){64 return ENOMEM;65 }66 // fill the buffer with the IPv6 address67 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 74 47 int inet_pton(uint16_t family, const char * address, uint8_t * data){ 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 85 48 const char * next; 86 49 char * last; 87 50 int index; 51 int count; 52 int base; 53 size_t bytes; 88 54 size_t shift; 89 55 unsigned long value; … … 92 58 return EINVAL; 93 59 } 94 95 // set the processing parameters96 60 switch(family){ 97 61 case AF_INET: … … 108 72 return ENOTSUP; 109 73 } 110 111 // erase if no address112 74 if(! address){ 113 75 bzero(data, count); 114 76 return ENOENT; 115 77 } 116 117 // process the string from the beginning118 78 next = address; 119 79 index = 0; 120 80 do{ 121 // if the actual character is set122 81 if(next && (*next)){ 123 124 // if not on the first character125 82 if(index){ 126 // move to the next character127 83 ++ next; 128 84 } 129 130 // parse the actual integral value131 85 value = strtoul(next, &last, base); 132 // remember the last problematic character133 // should be either '.' or ':' but is ignored to be more generic134 86 next = last; 135 136 // fill the address data byte by byte137 87 shift = bytes - 1; 138 88 do{ … … 141 91 value >>= 8; 142 92 }while(shift --); 143 144 93 index += bytes; 145 94 }else{ 146 // erase the rest of the address147 95 bzero(data + index, count - index); 148 96 return EOK; 149 97 } 150 98 }while(index < count); 99 return EOK; 100 } 151 101 152 return EOK; 102 int inet_ntop(uint16_t family, const uint8_t * data, char * address, size_t length){ 103 if((! data) || (! address)){ 104 return EINVAL; 105 } 106 switch(family){ 107 case AF_INET: 108 if(length < INET_ADDRSTRLEN){ 109 return ENOMEM; 110 } 111 snprintf(address, length, "%hhu.%hhu.%hhu.%hhu", data[0], data[1], data[2], data[3]); 112 return EOK; 113 case AF_INET6: 114 if(length < INET6_ADDRSTRLEN){ 115 return ENOMEM; 116 } 117 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]); 118 return EOK; 119 default: 120 return ENOTSUP; 121 } 153 122 } 154 123
Note:
See TracChangeset
for help on using the changeset viewer.