Changes in uspace/lib/c/generic/net/inet.c [a2e3ee6:9d58539] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/net/inet.c
ra2e3ee6 r9d58539 39 39 #include <net/in6.h> 40 40 #include <net/inet.h> 41 #include <inet/addr.h> 41 42 42 #include <errno.h> 43 43 #include <mem.h> … … 94 94 } 95 95 96 static int inet_pton4(const char *address, uint8_t *data)97 {98 memset(data, 0, 4);99 100 const char *cur = address;101 size_t i = 0;102 103 while (i < 4) {104 int rc = str_uint8_t(cur, &cur, 10, false, &data[i]);105 if (rc != EOK)106 return rc;107 108 i++;109 110 if (*cur == 0)111 break;112 113 if (*cur != '.')114 return EINVAL;115 116 if (i < 4)117 cur++;118 }119 120 if ((i == 4) && (*cur != 0))121 return EINVAL;122 123 return EOK;124 }125 126 static int inet_pton6(const char *address, uint8_t *data)127 {128 // FIXME TODO129 return ENOTSUP;130 }131 132 96 /** Parses the character string into the address. 133 97 * 134 * @param[in] family The address family. 135 * @param[in] address The character buffer to be parsed. 136 * @param[out] data The address data to be filled. 98 * If the string is shorter than the full address, zero bytes are added. 137 99 * 138 * @return EOK on success. 139 * @return EINVAL if the data parameter is NULL. 140 * @return ENOENT if the address parameter is NULL. 141 * @return ENOTSUP if the address family is not supported. 142 * 100 * @param[in] family The address family. 101 * @param[in] address The character buffer to be parsed. 102 * @param[out] data The address data to be filled. 103 * @return EOK on success. 104 * @return EINVAL if the data parameter is NULL. 105 * @return ENOENT if the address parameter is NULL. 106 * @return ENOTSUP if the address family is not supported. 143 107 */ 144 108 int inet_pton(uint16_t family, const char *address, uint8_t *data) 145 109 { 110 /** The base number of the values. */ 111 int base; 112 /** The number of bytes per a section. */ 113 size_t bytes; 114 /** The number of bytes of the address data. */ 115 int count; 116 117 const char *next; 118 char *last; 119 int index; 120 size_t shift; 121 unsigned long value; 122 123 if (!data) 124 return EINVAL; 125 126 /* Set processing parameters */ 146 127 switch (family) { 147 128 case AF_INET: 148 return inet_pton4(address, data); 129 count = 4; 130 base = 10; 131 bytes = 1; 132 break; 133 149 134 case AF_INET6: 150 return inet_pton6(address, data); 135 count = 16; 136 base = 16; 137 bytes = 4; 138 break; 139 151 140 default: 152 /** Unknown address family */153 141 return ENOTSUP; 154 142 } 143 144 /* Erase if no address */ 145 if (!address) { 146 bzero(data, count); 147 return ENOENT; 148 } 149 150 /* Process string from the beginning */ 151 next = address; 152 index = 0; 153 do { 154 /* If the actual character is set */ 155 if (next && *next) { 156 157 /* If not on the first character */ 158 if (index) { 159 /* Move to the next character */ 160 ++next; 161 } 162 163 /* Parse the actual integral value */ 164 value = strtoul(next, &last, base); 165 /* 166 * Remember the last problematic character 167 * should be either '.' or ':' but is ignored to be 168 * more generic 169 */ 170 next = last; 171 172 /* Fill the address data byte by byte */ 173 shift = bytes - 1; 174 do { 175 /* like little endian */ 176 data[index + shift] = value; 177 value >>= 8; 178 } while(shift --); 179 180 index += bytes; 181 } else { 182 /* Erase the rest of the address */ 183 bzero(data + index, count - index); 184 return EOK; 185 } 186 } while (index < count); 187 188 return EOK; 155 189 } 156 190
Note:
See TracChangeset
for help on using the changeset viewer.