mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-08 16:11:18 +00:00
IPv6 related refactoring
This commit is contained in:
1 parent
d7abd53062
commit
29534218d7
11 files changed
+193
-89
No files matched your search
@@ -1,5 +1,5 @@
|
||||
#include "net.h"
|
||||
#include "log.h"
|
||||
#include "net.h"
|
||||
#include "util.h"
|
||||
|
||||
int mg_vprintf(struct mg_connection *c, const char *fmt, va_list ap) {
|
||||
@@ -25,3 +25,41 @@ char *mg_straddr(struct mg_connection *c, char *buf, size_t len) {
|
||||
mg_ntohs(c->peer.port));
|
||||
return buf;
|
||||
}
|
||||
|
||||
char *mg_ntoa(const struct mg_addr *addr, char *buf, size_t len) {
|
||||
uint8_t p[4];
|
||||
memcpy(p, &addr->ip, sizeof(p));
|
||||
snprintf(buf, len, "%hhu.%hhu.%hhu.%hhu", p[0], p[1], p[2], p[3]);
|
||||
return buf;
|
||||
}
|
||||
|
||||
bool mg_aton(struct mg_str str, struct mg_addr *addr) {
|
||||
uint8_t data[4] = {0, 0, 0, 0};
|
||||
// LOG(LL_INFO, ("[%.*s]", (int) str.len, str.ptr));
|
||||
if (!mg_casecmp(str.ptr, "localhost")) {
|
||||
addr->ip = mg_htonl(0x7f000001);
|
||||
return true;
|
||||
} else if (addr->is_ip6) {
|
||||
return false;
|
||||
} else {
|
||||
size_t i, num_octets = 0;
|
||||
// LOG(LL_DEBUG, ("[%.*s]", (int) str.len, str.ptr));
|
||||
for (i = 0; i < str.len; i++) {
|
||||
// LOG(LL_DEBUG,
|
||||
//(" %c %zu %hhu %zu", str.ptr[i], i, data[num_octets], num_octets));
|
||||
if (str.ptr[i] >= '0' && str.ptr[i] <= '9') {
|
||||
int octet = data[num_octets] * 10 + (str.ptr[i] - '0');
|
||||
if (octet > 255) return false;
|
||||
data[num_octets] = octet;
|
||||
} else if (str.ptr[i] == '.') {
|
||||
if (num_octets >= 3 || i == 0 || str.ptr[i - 1] == '.') return false;
|
||||
num_octets++;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (num_octets != 3 || str.ptr[i - 1] == '.') return false;
|
||||
memcpy(&addr->ip, data, sizeof(data));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user