Changeset 60ab6c3 in mainline for uspace/srv/net/inet.c
- Timestamp:
- 2010-03-10T05:46:54Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5782081
- Parents:
- 71b00dcc (diff), b48ebd19 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/inet.c
r71b00dcc r60ab6c3 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 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 47 74 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 48 85 const char * next; 49 86 char * last; 50 87 int index; 51 int count;52 int base;53 size_t bytes;54 88 size_t shift; 55 89 unsigned long value; … … 58 92 return EINVAL; 59 93 } 94 95 // set the processing parameters 60 96 switch(family){ 61 97 case AF_INET: … … 72 108 return ENOTSUP; 73 109 } 110 111 // erase if no address 74 112 if(! address){ 75 113 bzero(data, count); 76 114 return ENOENT; 77 115 } 116 117 // process the string from the beginning 78 118 next = address; 79 119 index = 0; 80 120 do{ 121 // if the actual character is set 81 122 if(next && (*next)){ 123 124 // if not on the first character 82 125 if(index){ 126 // move to the next character 83 127 ++ next; 84 128 } 129 130 // parse the actual integral value 85 131 value = strtoul(next, &last, base); 132 // remember the last problematic character 133 // should be either '.' or ':' but is ignored to be more generic 86 134 next = last; 135 136 // fill the address data byte by byte 87 137 shift = bytes - 1; 88 138 do{ … … 91 141 value >>= 8; 92 142 }while(shift --); 143 93 144 index += bytes; 94 145 }else{ 146 // erase the rest of the address 95 147 bzero(data + index, count - index); 96 148 return EOK; 97 149 } 98 150 }while(index < count); 151 99 152 return EOK; 100 }101 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 }122 153 } 123 154
Note:
See TracChangeset
for help on using the changeset viewer.