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