net: fix parsing of :port syntax

This commit is contained in:
Thomas Schoebel-Theuer 2013-04-10 09:54:04 +02:00
parent 05f202a9d4
commit 4d1bbdef5a
2 changed files with 23 additions and 6 deletions

View File

@ -57,8 +57,8 @@ void _check(int status)
int mars_create_sockaddr(struct sockaddr_storage *addr, const char *spec) int mars_create_sockaddr(struct sockaddr_storage *addr, const char *spec)
{ {
struct sockaddr_in *sockaddr = (void*)addr; struct sockaddr_in *sockaddr = (void*)addr;
char *new_spec; const char *new_spec;
char *tmp_spec; const char *tmp_spec;
int status = 0; int status = 0;
memset(addr, 0, sizeof(*addr)); memset(addr, 0, sizeof(*addr));
@ -83,20 +83,26 @@ int mars_create_sockaddr(struct sockaddr_storage *addr, const char *spec)
unsigned char u0 = 0, u1 = 0, u2 = 0, u3 = 0; unsigned char u0 = 0, u1 = 0, u2 = 0, u3 = 0;
status = sscanf(tmp_spec, "%hhu.%hhu.%hhu.%hhu", &u0, &u1, &u2, &u3); status = sscanf(tmp_spec, "%hhu.%hhu.%hhu.%hhu", &u0, &u1, &u2, &u3);
if (status != 4) { if (status != 4) {
MARS_ERR("invalid sockaddr IP syntax '%s', status = %d\n", tmp_spec, status);
status = -EINVAL; status = -EINVAL;
goto done; goto done;
} }
MARS_DBG("decoded IP = %u.%u.%u.%u\n", u0, u1, u2, u3);
sockaddr->sin_addr.s_addr = (__be32)u0 | (__be32)u1 << 8 | (__be32)u2 << 16 | (__be32)u3 << 24; sockaddr->sin_addr.s_addr = (__be32)u0 | (__be32)u1 << 8 | (__be32)u2 << 16 | (__be32)u3 << 24;
} }
// deocde port number (when present)
tmp_spec = spec;
while (*tmp_spec && *tmp_spec++ != ':') while (*tmp_spec && *tmp_spec++ != ':')
/*empty*/; /*empty*/;
if (*tmp_spec) { if (*tmp_spec) {
int port = 0; int port = 0;
status = sscanf(tmp_spec, "%d", &port); status = sscanf(tmp_spec, "%d", &port);
if (status != 1) { if (status != 1) {
MARS_ERR("invalid sockaddr PORT syntax '%s', status = %d\n", tmp_spec, status);
status = -EINVAL; status = -EINVAL;
goto done; goto done;
} }
MARS_DBG("decoded PORT = %d\n", port);
sockaddr->sin_port = htons(port); sockaddr->sin_port = htons(port);
} }
status = 0; status = 0;

View File

@ -15,14 +15,22 @@ static
char *_mars_translate_hostname(const char *name) char *_mars_translate_hostname(const char *name)
{ {
struct mars_global *global = mars_global; struct mars_global *global = mars_global;
const char *res = name; char *res = brick_strdup(name);
struct mars_dent *test; struct mars_dent *test;
char *tmp; char *tmp;
if (unlikely(!global)) { if (unlikely(!global)) {
goto done; goto done;
} }
tmp = path_make("/mars/ips/ip-%s", name);
for (tmp = res; *tmp; tmp++) {
if (*tmp == ':') {
*tmp = '\0';
break;
}
}
tmp = path_make("/mars/ips/ip-%s", res);
if (unlikely(!tmp)) { if (unlikely(!tmp)) {
goto done; goto done;
} }
@ -30,12 +38,15 @@ char *_mars_translate_hostname(const char *name)
test = mars_find_dent(global, tmp); test = mars_find_dent(global, tmp);
if (test && test->new_link) { if (test && test->new_link) {
MARS_DBG("'%s' => '%s'\n", tmp, test->new_link); MARS_DBG("'%s' => '%s'\n", tmp, test->new_link);
res = test->new_link; brick_string_free(res);
res = brick_strdup(test->new_link);
} else {
MARS_DBG("no translation for '%s'\n", tmp);
} }
brick_string_free(tmp); brick_string_free(tmp);
done: done:
return brick_strdup(res); return res;
} }
int mars_send_dent_list(struct mars_socket *sock, struct list_head *anchor) int mars_send_dent_list(struct mars_socket *sock, struct list_head *anchor)