From 70f5f8754f26cb6a6db43281f8e7475386d0c7a4 Mon Sep 17 00:00:00 2001 From: Michael Blizek Date: Sat, 9 Jul 2022 10:53:15 +0200 Subject: [PATCH] addr fixed to 8 bytes --- Makefile | 20 +- src/benchmark/client.c | 13 +- src/cor.h | 2 +- src/demo/connect2.c | 3 +- src/demo/invalidconnect.c | 5 +- src/demo/libcor.c | 129 ++---- src/demo/libcor.h | 28 +- src/demo/neigh.c | 19 +- src/demo/perf.c | 12 +- src/demo/route.c | 58 +-- src/demo/routed.c | 121 ++---- src/demo/routed2.c | 981 ++-------------------------------------------- src/routed/conf.c | 110 ++---- src/routed/forward.c | 27 +- src/routed/libcor.c | 126 ++---- src/routed/libcor.h | 22 +- src/routed/main.c | 18 +- src/routed/nodedb.c | 240 +++--------- src/routed/routed.h | 41 +- src/routed/util.c | 47 --- src/utils.h | 95 ++--- 21 files changed, 391 insertions(+), 1726 deletions(-) diff --git a/Makefile b/Makefile index 49b3be2..f4e2550 100644 --- a/Makefile +++ b/Makefile @@ -43,20 +43,20 @@ bin/demo_route: src/demo/route.c src/demo/libcor.c src/demo/libcor.h \ $(CC) $(CFLAGS) -o bin/demo_route src/demo/route.c src/demo/libcor.c bin/demo_routed: src/demo/routed.c src/demo/libcor.c src/demo/libcor.h \ - src/cor.h src/list.h src/utils.h - $(CC) $(CFLAGS) -o bin/demo_routed src/demo/routed.c src/demo/libcor.c + src/cor.h src/list.h src/utils.h src/utils.c + $(CC) $(CFLAGS) -o bin/demo_routed src/demo/routed.c src/demo/libcor.c src/utils.c bin/demo_routed2: src/demo/routed2.c \ - src/cor.h src/list.h src/utils.h - $(CC) $(CFLAGS) -o bin/demo_routed2 src/demo/routed2.c src/demo/libcor.c + src/cor.h src/list.h src/utils.h src/utils.c + $(CC) $(CFLAGS) -o bin/demo_routed2 src/demo/routed2.c src/demo/libcor.c src/utils.c bin/demo_echo: src/demo/echo.c \ - src/cor.h src/list.h src/utils.h + src/cor.h src/list.h src/utils.h $(CC) $(CFLAGS) -o bin/demo_echo src/demo/echo.c bin/demo_perf: src/demo/perf.c \ - src/cor.h src/list.h src/utils.h - $(CC) $(CFLAGS) -o bin/demo_perf src/demo/perf.c + src/cor.h src/list.h src/utils.h src/utils.c + $(CC) $(CFLAGS) -o bin/demo_perf src/demo/perf.c src/utils.c bin/cor_perfs: src/benchmark/server.c \ @@ -65,12 +65,12 @@ bin/cor_perfs: src/benchmark/server.c \ bin/cor_perfc: src/benchmark/client.c \ src/cor.h src/list.h src/utils.h - $(CC) $(CFLAGS) -o bin/cor_perfc src/benchmark/client.c + $(CC) $(CFLAGS) -o bin/cor_perfc src/benchmark/client.c src/utils.c bin/cor_routed: src/routed/routed.h src/routed/main.c \ src/routed/util.c src/routed/conf.c src/routed/nodedb.c \ src/routed/forward.c src/routed/libcor.c src/routed/libcor.h \ - src/cor.h src/list.h src/utils.h + src/cor.h src/list.h src/utils.h src/utils.c $(CC) $(CFLAGS) -o bin/cor_routed src/routed/main.c \ src/routed/util.c src/routed/conf.c src/routed/nodedb.c \ - src/routed/forward.c src/routed/libcor.c + src/routed/forward.c src/routed/libcor.c src/utils.c diff --git a/src/benchmark/client.c b/src/benchmark/client.c index 9e47ae7..c66d611 100644 --- a/src/benchmark/client.c +++ b/src/benchmark/client.c @@ -849,19 +849,16 @@ static int parse_addr_inet(char *arg) static int parse_addr_cor(char *arg) { - size_t arg_len = strlen(arg); + __be64 addr; + + if (parse_cor_addr(&addr, arg, 0) != 0) + goto out_err; bzero(&config.addr.addr_cor, sizeof(struct cor_sockaddr)); config.af = AF_COR; config.addr.addr_cor.sin_family = AF_COR; config.addr.addr_cor.port = htonl(2372); - - if ((arg_len % 2) != 0 || arg_len < 2 || - arg_len/2 > sizeof(config.addr.addr_cor.addr)) - goto out_err; - - if (parse_hex(&(config.addr.addr_cor.addr[0]), arg, arg_len) != 0) - goto out_err; + config.addr.addr_cor.addr = htonll(addr); return 0; diff --git a/src/cor.h b/src/cor.h index 92d75f3..91e3048 100644 --- a/src/cor.h +++ b/src/cor.h @@ -36,7 +36,7 @@ struct cor_sockaddr { __u16 sin_family; __be32 port; - char addr[64]; + __be64 addr; }; diff --git a/src/demo/connect2.c b/src/demo/connect2.c index 7fd1893..8ecb1eb 100644 --- a/src/demo/connect2.c +++ b/src/demo/connect2.c @@ -29,6 +29,7 @@ #include #include "../cor.h" +#include "../utils.h" int main(void) { @@ -42,7 +43,7 @@ int main(void) addr.sin_family = AF_COR; - addr.addr[0] = 1; + addr.addr = htonll(1); addr.port = htonl(1); fd = socket(PF_COR, SOCK_STREAM, 0); diff --git a/src/demo/invalidconnect.c b/src/demo/invalidconnect.c index 4214245..3c6c0a0 100644 --- a/src/demo/invalidconnect.c +++ b/src/demo/invalidconnect.c @@ -24,14 +24,13 @@ #include #include "../cor.h" +#include "../utils.h" #include "libcor.h" int main(void) { int fd, rc; - char *addr = "ahugpykehegh"; - fd = socket(PF_COR, 0, 0); if (fd < 0) { perror("socket"); @@ -44,7 +43,7 @@ int main(void) } while (1) { - rc = send_connect_neigh(fd, strlen(addr), addr); + rc = send_connect_neigh(fd, htonll(565)); printf("send_connect_neigh rc = %d\n", rc); diff --git a/src/demo/libcor.c b/src/demo/libcor.c index fd6f251..0e6d505 100644 --- a/src/demo/libcor.c +++ b/src/demo/libcor.c @@ -29,6 +29,7 @@ #include #include "../cor.h" +#include "../utils.h" #include "libcor.h" #define likely(x) __builtin_expect((x), 1) @@ -81,54 +82,6 @@ static int bzero_nr_iffinished(struct libcor_nonblock_resumeinfo *nr, int rc) } while(0) \ -static __u64 htonll(__u64 in) -{ - __u64 out = 0; - char *p_out = (char *) &out; - - p_out[7] = in & 255; - in = in >> 8; - p_out[6] = in & 255; - in = in >> 8; - p_out[5] = in & 255; - in = in >> 8; - p_out[4] = in & 255; - in = in >> 8; - p_out[3] = in & 255; - in = in >> 8; - p_out[2] = in & 255; - in = in >> 8; - p_out[1] = in & 255; - in = in >> 8; - p_out[0] = in & 255; - - return out; -} - -static __u64 ntohll(__u64 in) -{ - __u64 out = 0; - char *p_in = (char *) ∈ - - out += p_in[7]; - out << 8; - out += p_in[6]; - out << 8; - out += p_in[5]; - out << 8; - out += p_in[4]; - out << 8; - out += p_in[3]; - out << 8; - out += p_in[2]; - out << 8; - out += p_in[1]; - out << 8; - out += p_in[0]; - - return out; -} - #warning todo commands are sent via multiple packets int resume_send(int fd, struct libcor_nonblock_resumeinfo *nr) { @@ -575,9 +528,9 @@ int read_resp(int fd, int expect_bindata) return rc; } - +#warning todo replace LIBCOR_ASSERT_ERR int send_connect_neigh_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, - __u32 addrlen, char *addr) + __be64 addr) { __u32 hdrlen = 0; int rc; @@ -585,10 +538,6 @@ int send_connect_neigh_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, if (unlikely(nr->type != RESUME_TYPE_NONE)) LIBCOR_ASSERT_ERR(); - if (unlikely(addrlen > (1024 * 1024 * 1024) || - addrlen + 4 > WRITE_BUF_SIZE)) - LIBCOR_ASSERT_ERR(); - bzero(nr, sizeof(struct libcor_nonblock_resumeinfo)); nr->type = RESUME_TYPE_WRITE; nr->data.write.fd = fd; @@ -596,11 +545,11 @@ int send_connect_neigh_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, if (sizeof(nr->data.write.buf) != WRITE_BUF_SIZE) LIBCOR_ASSERT_ERR(); - if (unlikely(WRITE_BUF_SIZE < addrlen)) + if (unlikely(WRITE_BUF_SIZE < 8)) LIBCOR_ASSERT_ERR(); - memcpy(&(nr->data.write.buf[nr->data.write.len]), addr, addrlen); - nr->data.write.len += addrlen; + memcpy(&(nr->data.write.buf[0]), (char *) &addr, 8); + nr->data.write.len = 8; if (unlikely(nr->data.write.len > WRITE_BUF_SIZE)) LIBCOR_ASSERT_ERR(); @@ -608,13 +557,13 @@ int send_connect_neigh_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, return _send_cmd(fd, nr, CD_CONNECT_NB); } -int send_connect_neigh(int fd, __u32 addrlen, char *addr) +int send_connect_neigh(int fd, __be64 addr) { int rc = RC_WOULDBLOCK; struct libcor_nonblock_resumeinfo nr; bzero(&nr, sizeof(struct libcor_nonblock_resumeinfo)); - rc = send_connect_neigh_nonblock(fd, &nr, addrlen, addr); + rc = send_connect_neigh_nonblock(fd, &nr, addr); if (unlikely(rc == RC_WOULDBLOCK)) { LIBCOR_ASSERT_ERR(); return bzero_nr_iffinished(&nr, RC_CONNBROKEN); @@ -788,7 +737,7 @@ static int field_present(struct listneigh_field *fields, __u32 numfields, int read_neigh_list_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, void *ptr, void (*init)(void *ptr, __u32 numneigh), - void (*next_neigh)(void *ptr, __u32 addrlen, char *addr)) + void (*next_neigh)(void *ptr, __be64 addr)) { int rc; @@ -887,8 +836,7 @@ state_4: init(ptr, nr_nl->numneighs); for (nr_nl->u=0; nr_nl->u < nr_nl->numneighs; nr_nl->u++) { - bzero(&(nr_nl->addr[0]), ADDR_MAX_LEN); - nr_nl->addrlen = 0; + nr_nl->addr = 0; nr_nl->latency = 0; for(nr_nl->v=0; nr_nl->v < nr_nl->numfields; nr_nl->v++) { @@ -912,22 +860,15 @@ state_5: goto discard_field; } else if (nr_nl->fields[nr_nl->v].field == LIST_NEIGH_FIELD_ADDR) { - - nr_nl->addrlen = nr_nl->fieldlen; - - if (unlikely(nr_nl->addrlen > ADDR_MAX_LEN)) + if (unlikely(nr_nl->fieldlen != 8)) goto discard_field; - if (sizeof(nr_nl->addr) != ADDR_MAX_LEN) - LIBCOR_ASSERT_ERR(); - - bzero(&(nr_nl->addr[0]), ADDR_MAX_LEN); + ASSERT(sizeof(nr_nl->addr) == 8); nr_nl->state = 6; state_6: - rc = read_fully(fd, nr, &(nr_nl->addr[0]), - nr_nl->addrlen, - &(nr_nl->fieldlen)); + rc = read_fully(fd, nr, (char *) &(nr_nl->addr), + 8, &(nr_nl->fieldlen)); if (rc != RC_OK) return bzero_nr_iffinished(nr, rc); } else if (nr_nl->fields[nr_nl->v].field == @@ -956,12 +897,8 @@ state_8: nr_nl->fieldlen = 0; } - if (nr_nl->addrlen > ADDR_MAX_LEN) - LIBCOR_ASSERT_ERR(); - - next_neigh(ptr, nr_nl->addrlen, &(nr_nl->addr[0])); - bzero(&(nr_nl->addr[0]), ADDR_MAX_LEN); - nr_nl->addrlen = 0; + next_neigh(ptr, nr_nl->addr); + nr_nl->addr = 0; nr_nl->latency = 0; } @@ -970,7 +907,7 @@ state_8: int read_neigh_list(int fd, void *ptr, void (*init)(void *ptr, __u32 numneighs), - void (*next_neigh)(void *ptr, __u32 addrlen, char *addr)) + void (*next_neigh)(void *ptr, __be64 addr)) { int rc = RC_WOULDBLOCK; struct libcor_nonblock_resumeinfo nr; @@ -1054,38 +991,42 @@ int send_rdsock_version(int fd, __u32 version) } int send_rdsock_up_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, - char *addr, __u32 addrlen) + int has_addr, __be64 addr) { - char data[72]; + char data[16]; __u64 flags = 0; __u32 addrlenbe; - if (addr == 0 && addrlen != 0) + if (has_addr == 0 && addr != 0) return RC_CONNBROKEN; - if (addrlen > 64) - return RC_CONNBROKEN; + if (has_addr) { + flags |= CRD_UTK_UP_FLAGS_ADDR; + + flags = htonll(flags); - flags = htonll(flags); - addrlenbe = htonl(addrlen); + memcpy(&(data[0]), (char *) &flags, 8); + memcpy(&(data[8]), (char *) &addr, 8); - memcpy(&(data[0]), (char *) &flags, 8); - memcpy(&(data[8]), (char *) &addrlenbe, 4); - if (addrlen > 0) - memcpy(&(data[12]), addr, addrlen); + return send_rdsock_cmd(fd, nr, CRD_UTK_UP, &(data[0]), 16); + } else { + flags = htonll(flags); - return send_rdsock_cmd(fd, nr, CRD_UTK_UP, &(data[0]), 12 + addrlen); + memcpy(&(data[0]), (char *) &flags, 8); + + return send_rdsock_cmd(fd, nr, CRD_UTK_UP, &(data[0]), 8); + } } -int send_rdsock_up(int fd, char *addr, __u32 addrlen) +int send_rdsock_up(int fd, int has_addr, __be64 addr) { int rc = RC_WOULDBLOCK; struct libcor_nonblock_resumeinfo nr; bzero(&nr, sizeof(struct libcor_nonblock_resumeinfo)); - rc = send_rdsock_up_nonblock(fd, &nr, addr, addrlen); + rc = send_rdsock_up_nonblock(fd, &nr, has_addr, addr); if (unlikely(rc == RC_WOULDBLOCK)) { LIBCOR_ASSERT_ERR(); return bzero_nr_iffinished(&nr, RC_CONNBROKEN); diff --git a/src/demo/libcor.h b/src/demo/libcor.h index 180a732..44dbacd 100644 --- a/src/demo/libcor.h +++ b/src/demo/libcor.h @@ -34,11 +34,16 @@ */ #define CRD_UTK_UP 2 +#define CRD_UTK_UP_FLAGS_ADDR 1 +#define CRD_UTK_UP_FLAGS_INTERFACES 2 /* - * CRD_UTK_UP[4] length[4] flags[8] addrlen[4] addr[addrlen] + * CRD_UTK_UP[4] length[4] flags[8] + * if CRD_UTK_UP_FLAGS_ADDR + * addr[8] + * if CRD_UTK_UP_FLAGS_INTERFACES: + * num_interfaces[4] (length[4] interface[length])[num_interfaces] */ - #define CRD_UTK_CONNECTERROR 3 /* * CRD_UTK_CONNECTERROR[4] length[4] cookie[8] error[4] @@ -56,7 +61,6 @@ #define RESUME_READ_FUNC_RDSOCK_CMD 4 #define NEIGHLIST_MAX_FIELDS 256 -#define ADDR_MAX_LEN 1024 #define WRITE_BUF_SIZE 1024 struct listneigh_field { @@ -79,7 +83,7 @@ struct libcor_nonblock_resumeinfo_neighlist{ void *ptr; void (*init)(void *ptr, __u32 numneighs); - void (*next_neigh)(void *ptr, __u32 addrlen, char *addr); + void (*next_neigh)(void *ptr, __be64 addr); __u8 state; @@ -95,8 +99,7 @@ struct libcor_nonblock_resumeinfo_neighlist{ __u32 fieldlen; - char addr[ADDR_MAX_LEN]; - __u32 addrlen; + __be64 addr; char latency; }; @@ -210,10 +213,9 @@ extern int read_resp_nonblock(int fd, int expect_bindata, extern int read_resp(int fd, int expect_bindata); extern int send_connect_neigh_nonblock(int fd, - struct libcor_nonblock_resumeinfo *nr, - __u32 addrlen, char *addr); + struct libcor_nonblock_resumeinfo *nr, __be64 addr); -extern int send_connect_neigh(int fd, __u32 addrlen, char *addr); +extern int send_connect_neigh(int fd, __be64 addr); extern int send_connect_port_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, @@ -244,11 +246,11 @@ extern int read_neigh_list_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, void *ptr, void (*init)(void *ptr, __u32 numneighs), - void (*next_neigh)(void *ptr, __u32 addrlen, char *addr)); + void (*next_neigh)(void *ptr, __be64 addr)); extern int read_neigh_list(int fd, void *ptr, void (*init)(void *ptr, __u32 numneighs), - void (*next_neigh)(void *ptr, __u32 addrlen, char *addr)); + void (*next_neigh)(void *ptr, __be64 addr)); @@ -261,9 +263,9 @@ extern int send_rdsock_version(int fd, __u32 version); extern int send_rdsock_up_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, - char *addr, __u32 addrlen); + int has_addr, __be64 addrlen); -extern int send_rdsock_up(int fd, char *addr, __u32 addrlen); +extern int send_rdsock_up(int fd, int has_addr, __be64 addr); extern int send_rdsock_connecterror_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, diff --git a/src/demo/neigh.c b/src/demo/neigh.c index 3f6e3bd..e044d71 100644 --- a/src/demo/neigh.c +++ b/src/demo/neigh.c @@ -23,6 +23,7 @@ #include #include +#include "../utils.h" #include "../cor.h" #include "libcor.h" @@ -31,23 +32,9 @@ void init_neighlist(void *ptr, __u32 numneighs) printf("numneighs = %u\n", numneighs); } -static void print_hex(char *buf, int len) +void add_neigh(void *ptr, __be64 addr) { - int u; - - for(u=0;u sizeof(config.addr.addr_cor.addr)) - goto out_err; - - if (parse_hex(&(config.addr.addr_cor.addr[0]), arg, arg_len) != 0) - goto out_err; return 0; diff --git a/src/demo/route.c b/src/demo/route.c index 09be433..6cc7a6f 100644 --- a/src/demo/route.c +++ b/src/demo/route.c @@ -25,6 +25,7 @@ #include #include +#include "../utils.h" #include "../cor.h" #include "libcor.h" @@ -45,11 +46,10 @@ struct route_list{ struct route_list localneighs; struct node{ - char *addr; + __be64 addr; struct route_list routes; - __u16 addrlen; __u16 minhops; __u8 neighs_queried; }; @@ -67,10 +67,7 @@ struct e2e_route{ struct search_query{ int type; union { - struct { - __u16 addrlen; - char *addr; - }nodebyaddress; + __be64 addr; }query; }; @@ -79,12 +76,7 @@ static int node_matches_searchquery(struct node *n, struct search_query *q) if (q->type == SEARCHQUERY_NEIGHSNOTQUERIED) { return n->neighs_queried == 0; } else if (q->type == SEARCHQUERY_NODEBYADDRESS) { - if (n->addrlen != q->query.nodebyaddress.addrlen) - return 0; - if (memcmp(n->addr, q->query.nodebyaddress.addr, - n->addrlen) != 0) - return 0; - return 1; + return n->addr == q->query.addr; } else { assert(0); return 0; @@ -121,14 +113,12 @@ static int try_find_neigh(struct e2e_route *route, struct search_query *q, return 0; } -static int try_find_neigh_byaddr(struct e2e_route *route, __u16 addrlen, - char *addr) +static int try_find_neigh_byaddr(struct e2e_route *route, __be64 addr) { struct search_query q; bzero(&q, sizeof(struct search_query)); q.type = SEARCHQUERY_NODEBYADDRESS; - q.query.nodebyaddress.addrlen = addrlen; - q.query.nodebyaddress.addr = addr; + q.query.addr = addr; return try_find_neigh(route, &q, &localneighs); } @@ -150,7 +140,7 @@ static int connect_to_host_send(int fd, struct e2e_route *route) int u; for (u=0;u<=route->pos;u++) { struct node *n = routes->routes[route->routes[u]].dst; - int rc = send_connect_neigh(fd, n->addrlen, n->addr); + int rc = send_connect_neigh(fd, n->addr); if (rc != RC_OK) return RC_CONNBROKEN; routes = &(n->routes); @@ -159,13 +149,13 @@ static int connect_to_host_send(int fd, struct e2e_route *route) return RC_OK; } -static int connect_to_host(int fd, __u16 addrlen, char *addr) +static int connect_to_host(int fd, __be64 addr) { int rc; struct e2e_route route; bzero(&route, sizeof(struct e2e_route)); - if (try_find_neigh_byaddr(&route, addrlen, addr) == 0) + if (try_find_neigh_byaddr(&route, addr) == 0) return RC_CONNBROKEN; rc = connect_to_host_send(fd, &route); @@ -178,13 +168,13 @@ out: return rc; } -void add_neigh(void *ptr, __u32 addrlen, char *addr) +void add_neigh(void *ptr, __be64 addr) { struct route_list *list = (struct route_list *) ptr; struct node *node; struct e2e_route route; - if (addrlen > MAX_ADDRLEN) + if (addr == 0) return; if (list->numroutes >= list->rows_alloc) @@ -193,12 +183,9 @@ void add_neigh(void *ptr, __u32 addrlen, char *addr) assert(list->routes[list->numroutes].dst == 0); bzero(&route, sizeof(struct e2e_route)); - if (try_find_neigh_byaddr(&route, addrlen, addr) == 0) { + if (try_find_neigh_byaddr(&route, addr) == 0) { node = calloc(1, sizeof(struct node)); - - node->addr = malloc(((int) addrlen)); - node->addrlen = addrlen; - memcpy(node->addr, addr, addrlen); + node->addr = addr; } else { node = route.result; } @@ -217,21 +204,6 @@ void init_neighlist(void *ptr, __u32 numneighs) list->routes = calloc(numneighs, sizeof(struct route)); } -static void print_hex(char *buf, int len) -{ - int u; - for(u=0;uresult->addrlen, - route->result->addr); + printf("addr: %llx\n", ntohll(route->result->addr)); + list = &(route->result->routes); rc = connect_to_host_send(fd, route); diff --git a/src/demo/routed.c b/src/demo/routed.c index 93b4c1c..480ebc1 100644 --- a/src/demo/routed.c +++ b/src/demo/routed.c @@ -52,13 +52,12 @@ struct service_list{ }; struct node{ - char *addr; + __be64 addr; struct route_list routes; struct service_list services; - __u16 addrlen; __u16 minhops; __u8 neighs_queried; }; @@ -76,16 +75,13 @@ struct e2e_route{ struct search_query{ int type; union { - struct { - __u16 addrlen; - char *addr; - }nodebyaddress; + __be64 addr }query; }; -char *localaddr; -__u32 localaddrlen; +int has_addr = 0; +__be64 localaddr; struct route_list localneighs; struct service_list localservices; @@ -96,33 +92,7 @@ static int node_matches_searchquery(struct node *n, struct search_query *q) if (q->type == SEARCHQUERY_NEIGHSNOTQUERIED) { return n->neighs_queried == 0; } else if (q->type == SEARCHQUERY_NODEBYADDRESS) { - /* if (n->addrlen != q->query.nodebyaddress.addrlen) - return 0; - if (memcmp(n->addr, q->query.nodebyaddress.addr, - n->addrlen) != 0) - return 0; - return 1; */ - - __u16 cmplen = n->addrlen; - __u32 u; - - if (q->query.nodebyaddress.addrlen < cmplen) - cmplen = q->query.nodebyaddress.addrlen; - - if (memcmp(n->addr, q->query.nodebyaddress.addr, cmplen) != 0) - return 0; - - for (u=cmplen;uaddrlen;u++) { - if (n->addr[u] != 0) { - return 0; - } - } - for (u=cmplen;uquery.nodebyaddress.addrlen;u++) { - if (q->query.nodebyaddress.addr[u] != 0) { - return 0; - } - } - return 1; + return n->addr == q->query.addr; } else { assert(0); return 0; @@ -159,14 +129,12 @@ static int try_find_neigh(struct e2e_route *route, struct search_query *q, return 0; } -static int try_find_neigh_byaddr(struct e2e_route *route, __u16 addrlen, - char *addr) +static int try_find_neigh_byaddr(struct e2e_route *route, __be64 addr) { struct search_query q; bzero(&q, sizeof(struct search_query)); q.type = SEARCHQUERY_NODEBYADDRESS; - q.query.nodebyaddress.addrlen = addrlen; - q.query.nodebyaddress.addr = addr; + q.query.addr = addr; return try_find_neigh(route, &q, &localneighs); } @@ -189,7 +157,7 @@ static int connect_to_host_send(int fd, struct e2e_route *route) int u; for (u=0;u<=route->pos;u++) { struct node *n = routes->routes[route->routes[u]].dst; - int rc = send_connect_neigh(fd, n->addrlen, n->addr); + int rc = send_connect_neigh(fd, n->addr); if (rc != RC_OK) return RC_CONNBROKEN; routes = &(n->routes); @@ -198,13 +166,13 @@ static int connect_to_host_send(int fd, struct e2e_route *route) return RC_OK; } -static int connect_to_host(int fd, __u16 addrlen, char *addr) +static int connect_to_host(int fd, __be64 addr) { int rc; struct e2e_route route; bzero(&route, sizeof(struct e2e_route)); - if (try_find_neigh_byaddr(&route, addrlen, addr) == 0) + if (try_find_neigh_byaddr(&route, addr) == 0) return RC_CONNBROKEN; rc = connect_to_host_send(fd, &route); @@ -217,13 +185,13 @@ out: return rc; } -void add_neigh(void *ptr, __u32 addrlen, char *addr) +void add_neigh(void *ptr, __be64 addr) { struct route_list *list = (struct route_list *) ptr; struct node *node; struct e2e_route route; - if (addrlen > MAX_ADDRLEN) + if (addr == 0) return; if (list->numroutes >= list->rows_alloc) @@ -232,12 +200,9 @@ void add_neigh(void *ptr, __u32 addrlen, char *addr) assert(list->routes[list->numroutes].dst == 0); bzero(&route, sizeof(struct e2e_route)); - if (try_find_neigh_byaddr(&route, addrlen, addr) == 0) { + if (try_find_neigh_byaddr(&route, addr) == 0) { node = calloc(1, sizeof(struct node)); - - node->addr = malloc(((int) addrlen)); - node->addrlen = addrlen; - memcpy(node->addr, addr, addrlen); + node->addr = addr; } else { node = route.result; } @@ -281,21 +246,6 @@ void init_servicelist(void *ptr, __u32 numservices) list->ports = calloc(numservices, 4); } -static void print_hex(char *buf, int len) -{ - int u; - for(u=0;uresult->addrlen, - route->result->addr); + printf("addr %llx\n", ntohll(route->result->addr)); list = &(route->result->routes); service = &(route->result->services); rc = connect_to_host_send(fd, route); @@ -422,11 +371,9 @@ static int rdscmd_connect(void *ptr, __u64 cookie, } - printf("rds_connect addr: "); - print_hex(&(addr->addr[0]), sizeof(addr->addr)); - printf(" port: %u\n", ntohs(addr->port)); + printf("rds_connect addr: %llx port %u\n", ntohll(addr->addr), ntohs(addr->port)); - rc = connect_to_host(fd, sizeof(addr->addr), &(addr->addr[0])); + rc = connect_to_host(fd, addr->addr); if (rc != RC_OK) { printf("connect_to_host error\n"); @@ -485,33 +432,17 @@ static int proc_rdsock_cmd(int fd, struct rdsock_cmd *cmd) int parse_addr(char *argv_addr) { - int rc; - int len = strlen(argv_addr); - char *noaddr = "noaddr"; - - if (strncmp(argv_addr, noaddr, len) == 0) { + if (strcmp(argv_addr, "noaddr") == 0) { + has_addr = 0; localaddr = 0; - localaddrlen = 0; return 0; + } else if (parse_cor_addr(&localaddr, argv_addr, 0) == 0) { + has_addr = 1; + return 0; + } else { + fprintf(stderr, "error: \"%s\" is not a valid address (expected noaddr or 16 hex digits)\n", argv_addr); + return -1; } - - if ((len < 2) || (len%2) != 0) - goto parseerror; - - localaddr = malloc(len/2); - localaddrlen = len/2; - - rc = parse_hex(localaddr, argv_addr, len); - if (rc != 0) - goto parseerror; - - return 0; - -parseerror: - fprintf(stderr, "error: argv_addr is not a valid address (contains " - "non-hex characters or number of hex characters is not " - "a multiple of 2)\n"); - return -1; } int main(int argc, char *argv[]) @@ -538,7 +469,7 @@ int main(int argc, char *argv[]) } send_rdsock_version(rdsock, 0); - send_rdsock_up(rdsock, localaddr, localaddrlen); + send_rdsock_up(rdsock, has_addr, localaddr); sleep(30); diff --git a/src/demo/routed2.c b/src/demo/routed2.c index 733d9bd..e8f96a9 100644 --- a/src/demo/routed2.c +++ b/src/demo/routed2.c @@ -61,13 +61,12 @@ struct service_list{ #define ROUTE_MAXHOPS 7 struct node{ - char *addr; + __be64 addr; struct route_list routes; struct service_list services; - __u32 addrlen; __u16 minhops; __u8 neighs_queried; @@ -83,18 +82,14 @@ struct node{ struct search_query{ int type; union { - struct { - __u32 addrlen; - char *addr; - }nodebyaddress; - + __be64 addr; __be32 serviceport; }query; }; -char *localaddr; -__u32 localaddrlen; +int has_localaddr = 0; +__be64 localaddr; __u32 sleep_before_discover = 5; __u8 export_servicelist_enabled = 0; @@ -114,9 +109,6 @@ int rdsock_fd = 0; #define EPOLLDATA_DISCOVERNETWORK 1 #define EPOLLDATA_RDSCONNECT 2 #define EPOLLDATA_RDSOCKCMD 3 -#define EPOLLDATA_FWD_TO_R 4 -#define EPOLLDATA_FWD_TO_LSERVICE 5 -#define EPOLLDATA_FORWARD 6 struct nonblock_resumeinfo_connect_to_host_send{ int fd; @@ -175,33 +167,6 @@ struct nonblock_resumeinfo{ struct{ struct libcor_nonblock_resumeinfo lnr; }rdsock_cmd; - - struct{ - __u8 fwd_to_host; - char *addr; - __u32 addrlen; - __be32 targetport; - }fwd_to_r; - - struct{ - int af; - struct sockaddr *saddr; - socklen_t saddrlen; - }fwd_to_lservice; - - struct{ - /** - * buf gets filled by reversedir and written into - * this fd - */ - char *buf; - __u32 buffill; - __u32 bufwritten; - - __u8 is_connected; - - struct nonblock_resumeinfo *reversedir; - }forward; }data; }; @@ -255,37 +220,6 @@ static void set_nonblock(int fd, int value) } } -static int addr_eq(char *addr1, __u32 addrlen1, - char *addr2, __u32 addrlen2) -{ - while (addrlen1>0) { - if (addr1[addrlen1-1] == 0) { - addrlen1--; - } else { - break; - } - } - - while (addrlen2>0) { - if (addr2[addrlen2-1] == 0) { - addrlen2--; - } else { - break; - } - } - - if (addrlen1 != addrlen2) - return 0; - - if (addrlen1 > MAX_ADDRLEN) - return 0; - - if (addrlen1 != 0 && memcmp(addr1, addr2, addrlen1) != 0) - return 0; - - return 1; -} - static int service_list_contains(struct service_list *services, __be32 port) { __u32 u; @@ -302,13 +236,10 @@ static int service_list_contains(struct service_list *services, __be32 port) static int node_matches_searchquery(struct node *n, struct search_query *q) { if (q->type == SEARCHQUERY_NEIGHSNOTQUERIED) { - return n->neighs_queried == 0 && - addr_eq(n->addr, n->addrlen, localaddr, - localaddrlen) == 0; + return n->neighs_queried == 0 && n->addr != 0 && + n->addr != localaddr; } else if (q->type == SEARCHQUERY_NODEBYADDRESS) { - return addr_eq(n->addr, n->addrlen, - q->query.nodebyaddress.addr, - q->query.nodebyaddress.addrlen); + return n->addr == q->query.addr; } else if (q->type == SEARCHQUERY_NODEBYSERVICEPORT) { return service_list_contains(&(n->services), q->query.serviceport); @@ -336,13 +267,12 @@ static struct node * try_find_neigh(struct search_query *q) return 0; } -static struct node * try_find_neigh_byaddr(__u16 addrlen, char *addr) +static struct node * try_find_neigh_byaddr(__be64 addr) { struct search_query q; bzero(&q, sizeof(struct search_query)); q.type = SEARCHQUERY_NODEBYADDRESS; - q.query.nodebyaddress.addrlen = addrlen; - q.query.nodebyaddress.addr = addr; + q.query.addr = addr; return try_find_neigh(&q); } @@ -411,8 +341,7 @@ static int connect_to_host_send(int fd, for (;cth->u < node->hopcount;) { struct node *n = cth->routes->routes[node->route[cth->u]].dst; - int rc = send_connect_neigh_nonblock(fd, lnr, n->addrlen, - n->addr); + int rc = send_connect_neigh_nonblock(fd, lnr, n->addr); cth->u++; if (rc != RC_OK) return rc; @@ -422,15 +351,12 @@ static int connect_to_host_send(int fd, return RC_OK; } -static void add_neigh(void *ptr, __u32 addrlen, char *addr) +static void add_neigh(void *ptr, __be64 addr) { struct route_list *list = (struct route_list *) ptr; struct node *node; - if (addrlen > MAX_ADDRLEN) - return; - - if (addr_eq(addr, addrlen, 0, 0)) + if (addr == 0) return; if (list->numroutes >= list->rows_alloc) @@ -438,14 +364,12 @@ static void add_neigh(void *ptr, __u32 addrlen, char *addr) ASSERT(list->routes[list->numroutes].dst == 0); - node = try_find_neigh_byaddr(addrlen, addr); + node = try_find_neigh_byaddr(addr); if (node == 0) { node = calloc(1, sizeof(struct node)); - node->addr = malloc(((int) addrlen)); - node->addrlen = addrlen; - memcpy(node->addr, addr, addrlen); + node->addr = addr; list_add_tail(&(node->node_list), &node_list); } @@ -490,81 +414,18 @@ static void init_servicelist(void *ptr, __u32 numservices) list->ports = calloc(numservices, 4); } -static void print_hex(char *buf, __u32 len) -{ - __u32 u; - - ASSERT(len != U32_MAX); - - for(u=0;unumports;u++) { - __u32 v; - - ASSERT(addrlen != U32_MAX); + int rc = dprintf(fd, "%llx:%u:%u\n", ntohll(addr), + ntohl(services->ports[u]), cost); - for(v=0;v 0); - ASSERT(rc < 20); - - rc = __export_servicelist(fd, &(buf[0]), rc); - if (rc != 0) - return 1; - } - - rc = snprintf(&(buf[0]), 20, ":%hu:%u\n", - ntohs(services->ports[u]), cost); - ASSERT(rc > 0); - ASSERT(rc < 20); - - rc = __export_servicelist(fd, &(buf[0]), rc); - if (rc != 0) + if (rc < 0) { return 1; + } } return 0; @@ -587,7 +448,7 @@ static void export_servicelist(void) return; } - if (_export_servicelist(fd, 0, 0, 0, &localservices) != 0) + if (_export_servicelist(fd, 0, 0, &localservices) != 0) goto err; curr = node_list.next; @@ -597,11 +458,13 @@ static void export_servicelist(void) curr = curr->next; - if (addr_eq(currnode->addr, currnode->addrlen, localaddr, - localaddrlen) != 0) + ASSERT(has_localaddr != 0 || localaddr == 0); + if (currnode->addr == 0) + continue; + if (currnode->addr == localaddr) continue; - if (_export_servicelist(fd, currnode->addr, currnode->addrlen, + if (_export_servicelist(fd, currnode->addr, currnode->hopcount, &(currnode->services)) != 0) goto err; } @@ -714,11 +577,6 @@ static void recalc_routes(void) list_del(&(currnode->node_list)); - if (currnode->addr != 0) { - free(currnode->addr); - currnode->addr = 0; - } - free(currnode); } @@ -792,8 +650,8 @@ static int load_neigh_list(int fd, struct nonblock_resumeinfo *nr, nr->data.discover_network.list = &localneighs; nr->data.discover_network.service = &localservices; } else { - neigh_printaddr(node->addrlen, - node->addr); + printf("addr %llx\n", ntohll(node->addr)); + nr->data.discover_network.list = &(node->routes); nr->data.discover_network.service = &(node->services); @@ -1000,16 +858,11 @@ static int rdscmd_connect(void *ptr, __u64 cookie, goto out_noclose; } - printf("rds_connect cookie: %llu, addr: ", cookie); - print_hex(&(addr->addr[0]), sizeof(addr->addr)); - printf(" port: %u\n", ntohs(addr->port)); + printf("rds_connect cookie: %llu, addr: %llx port %u\n", cookie, ntohll(addr->addr), ntohs(addr->port)); - if (addr_eq(addr->addr, sizeof(addr->addr), 0, 0) != 0 || - addr_eq(addr->addr, sizeof(addr->addr), localaddr, - localaddrlen) != 0) { + if (addr->addr == 0) { node = 0; - } else if ((node = try_find_neigh_byaddr(sizeof(addr->addr), - &(addr->addr[0]))) == 0) { + } else if ((node = try_find_neigh_byaddr(addr->addr)) == 0) { printf("connect_to_host host not found\n"); goto out_noclose; } @@ -1065,298 +918,6 @@ static int proc_rdsock_cmd(int fd, struct rdsock_cmd *cmd) } } -static struct nonblock_resumeinfo *_init_fwd(int fd, __u8 is_connected) -{ - struct nonblock_resumeinfo *nr = - malloc(sizeof(struct nonblock_resumeinfo)); - - struct epoll_event epe; - - bzero(nr, sizeof(struct nonblock_resumeinfo)); - nr->fd = fd; - nr->type = EPOLLDATA_FORWARD; - nr->data.forward.buf = malloc(FORWARD_BUF_SIZE); - nr->data.forward.is_connected = is_connected; - bzero(&epe, sizeof(struct epoll_event)); - epe.events = (EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLERR | EPOLLET); - epe.data.ptr = nr; - - epoll_ctl(epoll_fd, EPOLL_CTL_ADD, nr->fd, &epe); - - return nr; -} - -static void init_fwd(int fd0, __u8 fd0connected, int fd1, __u8 fd1connected) -{ - struct nonblock_resumeinfo *dir0 = _init_fwd(fd0, fd0connected); - struct nonblock_resumeinfo *dir1 = _init_fwd(fd1, fd1connected); - - dir0->data.forward.reversedir = dir1; - dir1->data.forward.reversedir = dir0; -} - -static void fwd_to_r_accept(int listenerfd, __u8 fwd_to_host, char *hostaddr, - __u32 hostaddrlen, __be32 targetport) -{ - while (1) { - int localfd; - struct node *node; - struct cor_sockaddr addr; - int remotefd; - __u8 remote_fd_isconnected = 0; - - #warning todo remote_fd_isconnected = 1 bug - - localfd = accept(listenerfd, 0, 0); - if (localfd < 0) - return; - - set_nonblock(localfd, 1); - - if (fwd_to_host != 0) { - if (addr_eq(hostaddr, hostaddrlen, 0, 0) != 0 || - addr_eq(hostaddr, hostaddrlen, - localaddr, localaddrlen) != 0) { - node = 0; - } else if ((node = try_find_neigh_byaddr(hostaddrlen, - hostaddr)) == 0) { - printf("fwd_to_r_accept host not found\n"); - goto failed; - } - } else { - if (service_list_contains(&(localservices), targetport) - != 0) { - node = 0; - } else if ((node = try_find_neigh_byservice(targetport)) - == 0) { - printf("fwd_to_r_accept no host with service found\n"); - goto failed; - } - } - - bzero(&addr, sizeof(struct cor_sockaddr)); - addr.sin_family = AF_COR; - addr.port = targetport; - - if (node != 0) { - if (node->addrlen > MAX_ADDRLEN) - goto failed; - memcpy(&(addr.addr[0]), node->addr, node->addrlen); - } - - - remotefd = socket(PF_COR, SOCK_STREAM, 0); - if (remotefd == -1) { - perror("socket"); - goto failed; - } - - set_nonblock(remotefd, 1); - - if (connect(remotefd, (struct sockaddr *) &addr, - sizeof(struct cor_sockaddr)) != 0) { - if (errno == EINPROGRESS) { - remote_fd_isconnected = 0; - } else { - close(remotefd); - perror("connect"); - goto failed; - } - } - - init_fwd(localfd, 1, remotefd, remote_fd_isconnected); - - if (0) { -failed: - close(localfd); - } - } -} - -static void fwd_to_lservice_accept(int listenerfd, int af, - struct sockaddr *saddr, socklen_t saddrlen) -{ - while (1) { - int localfd; - int remotefd; - __u8 remote_fd_isconnected = 1; - - localfd = accept(listenerfd, 0, 0); - - if (localfd < 0) - return; - - set_nonblock(localfd, 1); - - remotefd = socket(af, SOCK_STREAM, 0); - if (remotefd == -1) { - perror("socket"); - goto failed; - } - - set_nonblock(remotefd, 1); - - if (connect(remotefd, saddr, saddrlen) != 0) { - if (errno == EINPROGRESS) { - remote_fd_isconnected = 0; - } else { - perror("connect"); - close(remotefd); - goto failed; - } - } - - init_fwd(localfd, 1, remotefd, remote_fd_isconnected); - - if (0) { -failed: - close(localfd); - } - } -} - - - -#define RC_FORWARD_OK 1 -#define RC_FORWARD_WOULDBLOCK 2 -#define RC_FORWARD_ERROR 3 - -static int forward_write(struct nonblock_resumeinfo *nr) -{ - __u32 buffill = nr->data.forward.buffill; - int more = 0; - - ASSERT(buffill <= FORWARD_BUF_SIZE); - ASSERT(nr->data.forward.bufwritten <= buffill); - - if (buffill > 1 && FORWARD_BUF_SIZE > 1) { - buffill -= 1; - more = 1; - } - - while (nr->data.forward.bufwritten < buffill) { - int rc = send(nr->fd, nr->data.forward.buf + - nr->data.forward.bufwritten, - buffill - nr->data.forward.bufwritten, - more == 0 ? 0 : MSG_MORE); - - if (rc < 0 && (errno == EAGAIN || - errno == EWOULDBLOCK)) { - return RC_FORWARD_WOULDBLOCK; - } else if (rc <= 0) { - if (errno == EINTR) - continue; - - perror("forward_write"); - return RC_FORWARD_ERROR; - } else { - nr->data.forward.bufwritten += rc; - ASSERT(nr->data.forward.bufwritten <= buffill); - } - } - - return RC_FORWARD_OK; -} - -static int forward_read(struct nonblock_resumeinfo *nr) -{ - int readfd = nr->data.forward.reversedir->fd; - int rc; - - ASSERT(nr->data.forward.bufwritten <= nr->data.forward.buffill); - - if (nr->data.forward.bufwritten == nr->data.forward.buffill) { - nr->data.forward.buffill = 0; - nr->data.forward.bufwritten = 0; - } else if (nr->data.forward.bufwritten * 3 > nr->data.forward.buffill) { - memmove(nr->data.forward.buf, nr->data.forward.buf + - nr->data.forward.bufwritten, - nr->data.forward.buffill - - nr->data.forward.bufwritten); - nr->data.forward.buffill -= nr->data.forward.bufwritten; - nr->data.forward.bufwritten = 0; - } - - if (nr->data.forward.buffill == FORWARD_BUF_SIZE) - return RC_FORWARD_OK; - - while (nr->data.forward.buffill < FORWARD_BUF_SIZE) { - rc = recv(readfd, nr->data.forward.buf + - nr->data.forward.buffill, - FORWARD_BUF_SIZE - nr->data.forward.buffill, 0); - - if (rc == 0) { - #warning todo eof - return RC_FORWARD_OK; - } else if (rc < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - return RC_FORWARD_WOULDBLOCK; - } else if (errno == EINTR) { - continue; - } else { - perror("forward_read"); - return RC_FORWARD_ERROR; - } - } else { - nr->data.forward.buffill += rc; - ASSERT(nr->data.forward.buffill <= FORWARD_BUF_SIZE); - } - } - - return RC_FORWARD_OK; -} - -static void _forward(struct nonblock_resumeinfo *nr) -{ - while (1) { - int rc; - - rc = forward_read(nr); - if (rc == RC_FORWARD_ERROR) - goto err; - if (nr->data.forward.bufwritten == nr->data.forward.buffill) - break; - - rc = forward_write(nr); - if (rc == RC_FORWARD_ERROR) - goto err; - else if (rc == RC_FORWARD_WOULDBLOCK) - return; - } - - #warning todo call EPOLL_CTL_DEL + free where needed (some places calling close do not) - if (0) { -err: - epoll_ctl(epoll_fd, EPOLL_CTL_DEL, - nr->data.forward.reversedir->fd, 0); - close(nr->data.forward.reversedir->fd); - epoll_ctl(epoll_fd, EPOLL_CTL_DEL, nr->fd, 0); - close(nr->fd); - - #warning todo free after all events are processed - /* free(nr->data.forward.reversedir); - free(nr); */ - } - -} - -static void forward(struct nonblock_resumeinfo *nr, __u32 eventflags) -{ - ASSERT(nr->type == EPOLLDATA_FORWARD); - ASSERT(nr->data.forward.reversedir->type == EPOLLDATA_FORWARD); - - if ((eventflags & EPOLLOUT) != 0) - nr->data.forward.is_connected = 1; - - if (nr->data.forward.is_connected == 0 || - nr->data.forward.reversedir->data.forward.is_connected - == 0) - return; - - _forward(nr); - _forward(nr->data.forward.reversedir); -} - static void nonblock_resume(struct nonblock_resumeinfo *nr, __u32 eventflags) { if (nr->type == EPOLLDATA_DISCOVERNETWORK) { @@ -1395,17 +956,6 @@ static void nonblock_resume(struct nonblock_resumeinfo *nr, __u32 eventflags) rc = proc_rdsock_cmd(rdsock_fd, &cmd); free_rdsockcmd_data(&cmd); ASSERT(rc == 0); - } else if (nr->type == EPOLLDATA_FWD_TO_R) { - fwd_to_r_accept(nr->fd, nr->data.fwd_to_r.fwd_to_host, - nr->data.fwd_to_r.addr, - nr->data.fwd_to_r.addrlen, - nr->data.fwd_to_r.targetport); - } else if (nr->type == EPOLLDATA_FWD_TO_LSERVICE) { - fwd_to_lservice_accept(nr->fd, nr->data.fwd_to_lservice.af, - nr->data.fwd_to_lservice.saddr, - nr->data.fwd_to_lservice.saddrlen); - } else if (nr->type == EPOLLDATA_FORWARD) { - forward(nr, eventflags); } else { ASSERT(0); } @@ -1452,394 +1002,19 @@ static int rdsock_negotiate_version(void) return 0; } -static void epoll_add_fwd_to_r(struct list_head *r_fwds) +int parse_addr(char *argv_addr) { - while (list_empty(r_fwds) == 0) { - struct fwd_to_r_item *item = container_of(r_fwds->next, - struct fwd_to_r_item, allfwds); - - struct nonblock_resumeinfo *nr; - - struct epoll_event epe; - - set_nonblock(item->bindfd, 1); - - nr = malloc(sizeof(struct nonblock_resumeinfo)); - bzero(nr, sizeof(struct nonblock_resumeinfo)); - nr->fd = item->bindfd; - nr->type = EPOLLDATA_FWD_TO_R; - nr->data.fwd_to_r.fwd_to_host = item->fwd_to_host; - nr->data.fwd_to_r.addr = item->addr; - nr->data.fwd_to_r.addrlen = item->addrlen; - nr->data.fwd_to_r.targetport = item->targetport; - bzero(&epe, sizeof(struct epoll_event)); - epe.events = (EPOLLIN | EPOLLERR); - epe.data.ptr = nr; - - epoll_ctl(epoll_fd, EPOLL_CTL_ADD, item->bindfd, &epe); - - list_del(&(item->allfwds)); - free(item); - } -} - -static void epoll_add_fwd_to_lservice(struct list_head *lservice_fwds) -{ - while (list_empty(lservice_fwds) == 0) { - struct fwd_to_lservice_item *item = container_of( - lservice_fwds->next, - struct fwd_to_lservice_item, - allfwds); - - struct nonblock_resumeinfo *nr; - - struct epoll_event epe; - - set_nonblock(item->bindfd, 1); - - nr = malloc(sizeof(struct nonblock_resumeinfo)); - bzero(nr, sizeof(struct nonblock_resumeinfo)); - nr->fd = item->bindfd; - nr->type = EPOLLDATA_FWD_TO_LSERVICE; - nr->data.fwd_to_lservice.af = item->af; - nr->data.fwd_to_lservice.saddr = item->saddr; - nr->data.fwd_to_lservice.saddrlen = item->saddrlen; - bzero(&epe, sizeof(struct epoll_event)); - epe.events = (EPOLLIN | EPOLLERR); - epe.data.ptr = nr; - - epoll_ctl(epoll_fd, EPOLL_CTL_ADD, item->bindfd, &epe); - - list_del(&(item->allfwds)); - free(item); - } -} - -static int _parse_addr(char *arg, size_t arg_len, char **addr, __u32 *addrlen, - int noaddr_allowed) -{ - char *noaddr = "noaddr"; - - if (arg_len > 1048576) - return 1; - - if (noaddr_allowed != 0 && arg_len == 6 && memcmp(arg, noaddr, 6) == 0){ - *addr = 0; - *addrlen = 0; + if (strcmp(argv_addr, "noaddr") == 0) { + has_localaddr = 0; + localaddr = 0; return 0; - } - - if ((arg_len < 2) || (arg_len%2) != 0) - goto parseerror; - - *addr = malloc(arg_len/2); - *addrlen = arg_len/2; - - if (parse_hex(*addr, arg, arg_len) != 0) - goto parseerror; - - return 0; - -parseerror: - fprintf(stderr, "error: argv_addr is not a valid address (contains " - "non-hex characters or number of hex characters is not " - "a multiple of 2)\n"); - return 1; -} - -static size_t _parse_addr_lastarglen(char *arg, size_t len) -{ - size_t lastarglen = 0; - - while (lastarglen < len) { - if (arg[len-lastarglen-1] == ':') - break; - lastarglen++; - } - return lastarglen; -} - -static int parse_addr(char *arg, size_t arg_len, size_t *arg_addrlen, - char **addr, __u32 *addrlen) -{ - size_t lastarglen = _parse_addr_lastarglen(arg, arg_len); - - if (lastarglen == 0 || lastarglen >= arg_len) - return 1; - - *arg_addrlen = lastarglen + 1; // +1 ... ':' - - return _parse_addr(arg + arg_len - lastarglen, lastarglen, - addr, addrlen, 0); -} - -static int _parse_port(char *arg, size_t len, __u16 *port) -{ - char *tmpbuf; - char *endptr = 0; - - long int tmpport = 0; - - tmpbuf = malloc(len+1); - memcpy(tmpbuf, arg, len); - tmpbuf[len] = 0; - - tmpport = strtol(tmpbuf, &endptr, 10); - if (tmpbuf[0] == 0 || endptr == 0 || endptr != tmpbuf + len) { - free(tmpbuf); - tmpbuf = 0; - return 1; - } - free(tmpbuf); - tmpbuf = 0; - - if (tmpport <= 0 || tmpport >= 65536) - return 1; - - *port = (__u16) tmpport; - - return 0; -} - -static int parse_port_firstarg(char *arg, size_t len, size_t *p_portlen, - __u16 *port) -{ - size_t portlen = 0; - - while (portlen < len) { - if (arg[portlen] == ':') - break; - portlen++; - } - - if (portlen == 0 || portlen >= len) - return 1; - - if (_parse_port(arg, portlen, port) != 0) - return 1; - - *p_portlen = portlen + 1;// +1 ... ':' - return 0; -} - -static int parse_port_lastarg(char *arg, size_t len, size_t *p_portlen, - __u16 *port) -{ - size_t portlen = 0; - - portlen = _parse_addr_lastarglen(arg, len); - - if (portlen == 0 || portlen >= len) - return 1; - - if (_parse_port(arg + len - portlen, portlen, port) != 0) - return 1; - - *p_portlen = portlen + 1;// +1 ... ':' - return 0; -} - -static int parse_inetaddr(char *addr, size_t len, int *af, - struct sockaddr **saddr, socklen_t *saddrlen) -{ - size_t portlen = 0; - - __u16 port; - - char *host; - - struct in_addr inaddr; - struct in6_addr in6addr; - - if (parse_port_lastarg(addr, len, &portlen, &port) != 0) - return 1; - - if (portlen >= len) - return 1; - - host = malloc(len - portlen + 1); - memcpy(host, addr, len - portlen); - host[len - portlen] = 0; - - if (inet_pton(AF_INET, host, &inaddr) == 1) { - struct sockaddr_in *ret; - - free(host); - host = 0; - - ret = malloc(sizeof(struct sockaddr_in)); - bzero(ret, sizeof(struct sockaddr_in)); - - ret->sin_family = AF_INET; - ret->sin_port = htons(port); - memcpy(&(ret->sin_addr), &inaddr, sizeof(inaddr)); - - *af = AF_INET; - *saddr = (struct sockaddr *) ret; - *saddrlen = sizeof(struct sockaddr_in); - + } else if (parse_cor_addr(&localaddr, argv_addr, 0) == 0) { + has_localaddr = 1; return 0; - } else if (inet_pton(AF_INET6, host, &in6addr) == 1) { - free(host); - host = 0; - - printf("sorry IPv6 support is not implemented yet\n"); - - return 1; } else { - free(host); - host = 0; - return 1; - } -} - -static int parse_fwd_to_r(char *argv_fwd, - struct list_head *r_fwds, __u8 fwd_to_host) -{ - struct fwd_to_r_item *item; - int af; - struct sockaddr *saddr; - socklen_t saddrlen; - - size_t portlen = 0; - - char *addr = 0; - __u32 addrlen = 0; - __u16 serviceport; - - size_t len = strlen(argv_fwd); - - int optval; - - if (parse_port_lastarg(argv_fwd, len, &portlen, &serviceport) != 0) - return 1; - - len -= portlen; - - if (fwd_to_host != 0) { - size_t arg_addrlen = 0; - if (parse_addr(argv_fwd, len, &arg_addrlen, - &addr, &addrlen) != 0) - return 1; - len -= arg_addrlen; + fprintf(stderr, "error: \"%s\" is not a valid address (expected noaddr or 16 hex digits)\n", argv_addr); + return -1; } - - if (parse_inetaddr(argv_fwd, len, &af, &saddr, &saddrlen) != 0) - return 1; - - item = malloc(sizeof(struct fwd_to_r_item)); - item->bindfd = socket(af, SOCK_STREAM, 0); - if (item->bindfd < 0) { - perror("socket"); - goto err_noclose; - } - optval = 1; - if (setsockopt(item->bindfd, SOL_SOCKET, SO_REUSEADDR, &optval, - sizeof(optval)) != 0) { - perror("setsockopt"); - goto err; - } - if (bind(item->bindfd, saddr, saddrlen) != 0) { - perror("bind"); - goto err; - } - if (listen(item->bindfd, 100) != 0) { - perror("listen"); - goto err; - } - item->fwd_to_host = fwd_to_host; - if (fwd_to_host) { - item->addr = addr; - item->addrlen = addrlen; - } - item->targetport = htonl(serviceport); - - list_add_tail(&(item->allfwds), r_fwds); - - free(saddr); - - return 0; - -err: - close(item->bindfd); -err_noclose: - free(item); - free(saddr); - - return 1; -} - -static int parse_fwd_to_lservice(char *argv_fwd, - struct list_head *lservice_fwds, __u8 publish) -{ - size_t portlen = 0; - - __u16 serviceport; - - struct cor_sockaddr bind_saddr; - - int optval; - - struct fwd_to_lservice_item *item; - - int af; - struct sockaddr *saddr; - socklen_t saddrlen; - - size_t len = strlen(argv_fwd); - - if (parse_port_firstarg(argv_fwd, len, &portlen, &serviceport) != 0) - return 1; - - argv_fwd += portlen; - len -= portlen; - - if (parse_inetaddr(argv_fwd, len, &af, &saddr, &saddrlen) != 0) - return 1; - - bzero(&bind_saddr, sizeof(struct cor_sockaddr)); - bind_saddr.sin_family = AF_COR; - bind_saddr.port = htonl(serviceport); - - #warning todo - - item = malloc(sizeof(struct fwd_to_lservice_item)); - item->bindfd = socket(PF_COR, SOCK_STREAM, 0); - if (item->bindfd < 0) { - perror("socket"); - goto err; - } - optval = publish == 0 ? 0 : 1; - if (setsockopt(item->bindfd, SOL_COR, COR_PUBLISH_SERVICE, &optval, - sizeof(optval)) != 0) { - perror("setsockopt"); - goto err; - } - if (bind(item->bindfd, (struct sockaddr *) &bind_saddr, - sizeof(bind_saddr)) != 0) { - perror("bind"); - goto err; - } - if (listen(item->bindfd, 100) != 0) { - perror("listen"); - goto err; - } - - item->af = af; - item->saddr = saddr; - item->saddrlen = saddrlen; - - list_add_tail(&(item->allfwds), lservice_fwds); - - return 0; - -err: - close(item->bindfd); -err_noclose: - free(item); - free(saddr); - - return 1; } static int parse_args(int argc, char *argv[], struct list_head *r_fwds, @@ -1856,35 +1031,14 @@ static int parse_args(int argc, char *argv[], struct list_head *r_fwds, if (argc <= argsconsumed + 1) break; - if (strcmp(argv[argsconsumed + 1], - "--sleep_before_discover") == 0) { - long int l_sleep_before_discover; - char *endptr; - - if (argc <= argsconsumed + 2) - goto usage; - - l_sleep_before_discover = strtol(argv[argsconsumed + 1], - &endptr, 10); - if (*endptr != 0) - goto usage; - if (l_sleep_before_discover < 0 || - l_sleep_before_discover > 3600) - goto usage; - - sleep_before_discover = (__u32) l_sleep_before_discover; - - argsconsumed += 2; - } else if (strcmp(argv[argsconsumed + 1], "--addr") == 0) { + if (strcmp(argv[argsconsumed + 1], "--addr") == 0) { if (argc <= argsconsumed + 2) goto usage; if (addrfound != 0) goto usage; - rc = _parse_addr(argv[argsconsumed + 2], - strlen(argv[argsconsumed + 2]), - &localaddr, &localaddrlen, 1); + rc = parse_addr(argv[argsconsumed + 2]); if (rc != 0) goto usage; @@ -1896,49 +1050,6 @@ static int parse_args(int argc, char *argv[], struct list_head *r_fwds, export_servicelist_enabled = 1; argsconsumed += 1; - } else if (strcmp(argv[argsconsumed + 1], - "--fwd-to-rservice") == 0) { - if (argc <= argsconsumed + 2) - goto usage; - - rc = parse_fwd_to_r(argv[argsconsumed + 2], - r_fwds, 0); - if (rc != 0) - goto usage; - - argsconsumed += 2; - } else if (strcmp(argv[argsconsumed + 1], - "--fwd-to-rhost") == 0) { - if (argc <= argsconsumed + 2) - goto usage; - - rc = parse_fwd_to_r(argv[argsconsumed + 2], - r_fwds, 1); - if (rc != 0) - goto usage; - - argsconsumed += 2; - } else if (strcmp(argv[argsconsumed + 1], - "--fwd-to-lservice") == 0) { - if (argc <= argsconsumed + 2) - goto usage; - - rc = parse_fwd_to_lservice(argv[argsconsumed + 2], - lservice_fwds, 0); - if (rc != 0) - goto usage; - argsconsumed += 2; - } else if (strcmp(argv[argsconsumed + 1], - "--fwd-to-lservice-pub") == 0) { - if (argc <= argsconsumed + 2) - goto usage; - - rc = parse_fwd_to_lservice(argv[argsconsumed + 2], - lservice_fwds, 1); - if (rc != 0) - goto usage; - - argsconsumed += 2; } else { goto usage; } @@ -1952,11 +1063,7 @@ static int parse_args(int argc, char *argv[], struct list_head *r_fwds, usage: fprintf(stderr, "usage: test_routed2" " [--export-servicelist]" - " [--fwd-to-rservice bindaddr:bindport:remoteport]" - " [--fwd-to-rhost bindaddr:bindport:remotaaddr:remoteport]" - " [--fwd-to-lservice bindport:localaddr:localport]" - " [--fwd-to-lservice-pub bindport:localaddr:localport]" - " --addr addr (even number of hex digits or noaddr)\n"); + " --addr addr (noaddr or 16 hex digits)\n"); return 1; } @@ -2007,13 +1114,11 @@ int main(int argc, char *argv[]) if (rc != 0) goto out; - rc = send_rdsock_up(rdsock_fd, localaddr, localaddrlen); + rc = send_rdsock_up(rdsock_fd, has_localaddr, localaddr); ASSERT(rc == RC_OK); set_nonblock(rdsock_fd, 1); - epoll_add_fwd_to_lservice(&lservice_fwds); - sleep(sleep_before_discover); discover_network(0, 0); @@ -2035,8 +1140,6 @@ int main(int argc, char *argv[]) epoll_ctl(epoll_fd, EPOLL_CTL_ADD, rdsock_fd, &rds_epe); - epoll_add_fwd_to_r(&r_fwds); - discover_finished_executed = 1; printf("discover finished\n"); } diff --git a/src/routed/conf.c b/src/routed/conf.c index 750264f..273942d 100644 --- a/src/routed/conf.c +++ b/src/routed/conf.c @@ -23,8 +23,8 @@ #include "routed.h" -char *localaddr; -__u16 localaddrlen; +int has_localaddr = 0; +__be64 localaddr; char **interfaces = 0; __u32 num_interfaces = 0; @@ -56,66 +56,6 @@ static int parse_interface(char *arg) return 0; } -static int _parse_addr(char *arg, size_t arg_len, char **addr, __u16 *addrlen, - int noaddr_allowed) -{ - char *noaddr = "noaddr"; - - /* addrlen overflow */ - if (arg_len >= 65536) - return 1; - - if (noaddr_allowed != 0 && arg_len == 6 && memcmp(arg, noaddr, 6) == 0){ - *addr = 0; - *addrlen = 0; - return 0; - } - - if ((arg_len < 2) || (arg_len%2) != 0) - goto parseerror; - - *addr = malloc(arg_len/2); - bzero(*addr, arg_len/2); - *addrlen = (__u16) arg_len/2; - - if (parse_hex(*addr, arg, arg_len) != 0) - goto parseerror; - - return 0; - -parseerror: - fprintf(stderr, "error: argv_addr is not a valid address (contains " - "non-hex characters or number of hex characters is not " - "a multiple of 2)\n"); - return 1; -} - -static size_t _parse_addr_lastarglen(char *arg, size_t len) -{ - size_t lastarglen = 0; - - while (lastarglen < len) { - if (arg[len-lastarglen-1] == ':') - break; - lastarglen++; - } - return lastarglen; -} - -static int parse_addr(char *arg, size_t arg_len, size_t *arg_addrlen, - char **addr, __u16 *addrlen) -{ - size_t lastarglen = _parse_addr_lastarglen(arg, arg_len); - - if (lastarglen == 0 || lastarglen >= arg_len) - return 1; - - *arg_addrlen = lastarglen + 1; // +1 ... ':' - - return _parse_addr(arg + arg_len - lastarglen, lastarglen, - addr, addrlen, 0); -} - static int _parse_port(char *arg, size_t len, __u16 *port) { char *tmpbuf; @@ -165,6 +105,17 @@ static int parse_port_firstarg(char *arg, size_t len, size_t *p_portlen, *p_portlen = portlen + 1;// +1 ... ':' return 0; } +static size_t _parse_addr_lastarglen(char *arg, size_t len) +{ + size_t lastarglen = 0; + + while (lastarglen < len) { + if (arg[len-lastarglen-1] == ':') + break; + lastarglen++; + } + return lastarglen; +} static int parse_port_lastarg(char *arg, size_t len, size_t *p_portlen, __u16 *port) @@ -248,8 +199,7 @@ static int parse_fwd_to_r(char *argv_fwd, size_t portlen = 0; - char *addr = 0; - __u16 addrlen = 0; + __be64 addr; __u16 serviceport; size_t len = strlen(argv_fwd); @@ -262,11 +212,13 @@ static int parse_fwd_to_r(char *argv_fwd, len -= portlen; if (fwd_to_host != 0) { - size_t arg_addrlen = 0; - if (parse_addr(argv_fwd, len, &arg_addrlen, - &addr, &addrlen) != 0) + if (len < 17) return 1; - len -= arg_addrlen; + + if (parse_cor_addr(&addr, argv_fwd + len - 17, ':') != 0) + return 1; + + len -= 18; /* 16 for addr + 2x ':' */ } if (parse_inetaddr(argv_fwd, len, &af, &saddr, &saddrlen) != 0) @@ -296,7 +248,6 @@ static int parse_fwd_to_r(char *argv_fwd, item->fwd_to_host = fwd_to_host; if (fwd_to_host) { item->addr = addr; - item->addrlen = addrlen; } item->targetport = htonl(serviceport); @@ -388,6 +339,21 @@ err_noclose: return 1; } +int parse_localaddr(char *argv_addr) +{ + if (strcmp(argv_addr, "noaddr") == 0) { + has_localaddr = 0; + localaddr = 0; + return 0; + } else if (parse_cor_addr(&localaddr, argv_addr, 0) == 0) { + has_localaddr = 1; + return 0; + } else { + fprintf(stderr, "error: \"%s\" is not a valid address (expected noaddr or 16 hex digits)\n", argv_addr); + return -1; + } +} + int parse_args(int argc, char *argv[], struct list_head *r_fwds, struct list_head *lservice_fwds) { @@ -435,9 +401,7 @@ int parse_args(int argc, char *argv[], struct list_head *r_fwds, if (addrfound != 0) goto usage; - rc = _parse_addr(argv[argsconsumed + 2], - strlen(argv[argsconsumed + 2]), - &localaddr, &localaddrlen, 1); + rc = parse_localaddr(argv[argsconsumed + 2]); if (rc != 0) goto usage; @@ -519,6 +483,6 @@ usage: " [--fwd-to-lservice bindport:localaddr:localport]\n" " [--fwd-to-lservice-pub bindport:localaddr:localport]\n" " [--intf intf|*] (only localhost connectivity if omitted)" - " --addr addr (even number of hex digits or noaddr)\n"); + " --addr addr (noaddr or 16 hex digits)\n"); return 1; } diff --git a/src/routed/forward.c b/src/routed/forward.c index cf6f070..4618f9d 100644 --- a/src/routed/forward.c +++ b/src/routed/forward.c @@ -54,8 +54,8 @@ static void init_fwd(int fd0, __u8 fd0connected, int fd1, __u8 fd1connected) dir1->data.forward.reversedir = dir0; } -void fwd_to_r_accept(int listenerfd, __u8 fwd_to_host, char *hostaddr, - __u32 hostaddrlen, __be32 targetport) +void fwd_to_r_accept(int listenerfd, __u8 fwd_to_host, __be64 hostaddr, + __be32 targetport) { while (1) { int localfd; @@ -72,13 +72,14 @@ void fwd_to_r_accept(int listenerfd, __u8 fwd_to_host, char *hostaddr, set_nonblock(localfd, 1); + #warning todo noaddr should disable localhost connectivity (other places too) + if (fwd_to_host != 0) { - if (addr_eq(hostaddr, hostaddrlen, 0, 0) != 0 || - addr_eq(hostaddr, hostaddrlen, - localaddr, localaddrlen) != 0) { - node = 0; - } else if ((node = try_find_neigh_byaddr(hostaddrlen, - hostaddr)) == 0) { + if (hostaddr == 0 || + (has_localaddr != 0 && + hostaddr == localaddr)) { + node = 0; + } else if ((node = try_find_neigh_byaddr(hostaddr)) == 0) { if (verbosity >= 1) printf("fwd_to_r_accept host not found\n"); goto failed; @@ -99,11 +100,10 @@ void fwd_to_r_accept(int listenerfd, __u8 fwd_to_host, char *hostaddr, addr.sin_family = AF_COR; addr.port = targetport; - if (node != 0) { - if (node->addrlen > MAX_ADDRLEN) - goto failed; - memcpy(&(addr.addr[0]), node->addr, node->addrlen); - } + if (node == 0) + addr.addr = 0; + else + addr.addr = node->addr; remotefd = socket(PF_COR, SOCK_STREAM, 0); @@ -336,7 +336,6 @@ void epoll_add_fwd_to_r(struct list_head *r_fwds) nr->type = EPOLLDATA_FWD_TO_R; nr->data.fwd_to_r.fwd_to_host = item->fwd_to_host; nr->data.fwd_to_r.addr = item->addr; - nr->data.fwd_to_r.addrlen = item->addrlen; nr->data.fwd_to_r.targetport = item->targetport; bzero(&epe, sizeof(struct epoll_event)); epe.events = (EPOLLIN | EPOLLERR); diff --git a/src/routed/libcor.c b/src/routed/libcor.c index 09c4bca..3cf3b4d 100644 --- a/src/routed/libcor.c +++ b/src/routed/libcor.c @@ -70,54 +70,6 @@ static int bzero_nr_iffinished(struct libcor_nonblock_resumeinfo *nr, int rc) } -static __u64 htonll(__u64 in) -{ - __u64 out = 0; - char *p_out = (char *) &out; - - p_out[7] = in & 255; - in = in >> 8; - p_out[6] = in & 255; - in = in >> 8; - p_out[5] = in & 255; - in = in >> 8; - p_out[4] = in & 255; - in = in >> 8; - p_out[3] = in & 255; - in = in >> 8; - p_out[2] = in & 255; - in = in >> 8; - p_out[1] = in & 255; - in = in >> 8; - p_out[0] = in & 255; - - return out; -} - -static __u64 ntohll(__u64 in) -{ - __u64 out = 0; - char *p_in = (char *) ∈ - - out += p_in[7]; - out << 8; - out += p_in[6]; - out << 8; - out += p_in[5]; - out << 8; - out += p_in[4]; - out << 8; - out += p_in[3]; - out << 8; - out += p_in[2]; - out << 8; - out += p_in[1]; - out << 8; - out += p_in[0]; - - return out; -} - static const __u32 log_64_11_table[] = {0, 64, 68, 73, 77, 82, 88, 93, 99, 106, 113, 120, 128, 136, 145, 155, 165, 175, 187, 199, 212, 226, 240, @@ -685,7 +637,7 @@ int read_resp_bin(int fd, char **resp_buf, __u32 *resp_len) int send_connect_neigh_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, - __u32 addrlen, char *addr) + __be64 addr) { __u32 hdrlen = 0; int rc; @@ -693,10 +645,6 @@ int send_connect_neigh_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, if (unlikely(nr->type != RESUME_TYPE_NONE)) ASSERT_ERR(); - if (unlikely(addrlen > (1024 * 1024 * 1024) || - addrlen + 4 > WRITE_BUF_SIZE)) - ASSERT_ERR(); - bzero(nr, sizeof(struct libcor_nonblock_resumeinfo)); nr->type = RESUME_TYPE_WRITE; nr->data.write.fd = fd; @@ -704,11 +652,11 @@ int send_connect_neigh_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, if (sizeof(nr->data.write.buf) != WRITE_BUF_SIZE) ASSERT_ERR(); - if (unlikely(WRITE_BUF_SIZE < addrlen)) + if (unlikely(WRITE_BUF_SIZE < 8)) ASSERT_ERR(); - memcpy(&(nr->data.write.buf[nr->data.write.len]), addr, addrlen); - nr->data.write.len += addrlen; + memcpy(&(nr->data.write.buf[0]), (char *) &addr, 8); + nr->data.write.len = 8; if (unlikely(nr->data.write.len > WRITE_BUF_SIZE)) ASSERT_ERR(); @@ -716,13 +664,13 @@ int send_connect_neigh_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, return _send_cmd(fd, nr, CD_CONNECT_NB); } -int send_connect_neigh(int fd, __u32 addrlen, char *addr) +int send_connect_neigh(int fd, __be64 addr) { int rc = RC_WOULDBLOCK; struct libcor_nonblock_resumeinfo nr; bzero(&nr, sizeof(struct libcor_nonblock_resumeinfo)); - rc = send_connect_neigh_nonblock(fd, &nr, addrlen, addr); + rc = send_connect_neigh_nonblock(fd, &nr, addr); if (unlikely(rc == RC_WOULDBLOCK)) { ASSERT_ERR(); return bzero_nr_iffinished(&nr, RC_CONNBROKEN); @@ -866,8 +814,7 @@ static int field_present(struct listneigh_field *fields, __u32 numfields, static int _parse_neigh_list(char *buf, __u32 len, __u32 default_latency_us, void *ptr, void (*init)(void *ptr, __u32 numneigh), - void (*next_neigh)(void *ptr, __u32 addrlen, char *addr, - __u32 latency_us)) + void (*next_neigh)(void *ptr, __be64 addr, __u32 latency_us)) { int rc; __u32 u; @@ -931,8 +878,7 @@ static int _parse_neigh_list(char *buf, __u32 len, __u32 default_latency_us, init(ptr, numneighs); for (;numneighs>0;numneighs--) { - char *addr = 0; - __u32 addrlen = 0; + __be64 addr = 0; __u32 latency_us = 0; __u32 v; @@ -958,8 +904,10 @@ static int _parse_neigh_list(char *buf, __u32 len, __u32 default_latency_us, if (unlikely(field_present(fields, v, fields[v].field))) { } else if (fields[v].field == LIST_NEIGH_FIELD_ADDR) { - addr = buf; - addrlen = fieldlen; + if (unlikely(fieldlen != 8)) + return RC_CONNBROKEN; + + memcpy((char *) &addr, buf, 8); } else if (fields[v].field == LIST_NEIGH_FIELD_LATENCY) { latency_us = cor_dec_log_64_11(*((__u8 *) buf)); @@ -970,7 +918,7 @@ static int _parse_neigh_list(char *buf, __u32 len, __u32 default_latency_us, } if (next_neigh != 0) - next_neigh(ptr, addrlen, addr, latency_us); + next_neigh(ptr, addr, latency_us); } return RC_OK; @@ -979,8 +927,7 @@ static int _parse_neigh_list(char *buf, __u32 len, __u32 default_latency_us, int parse_neigh_list(char *resp, __u32 len, __u32 default_latency_us, void *ptr, void (*init)(void *ptr, __u32 numneigh), - void (*next_neigh)(void *ptr, __u32 addrlen, char *addr, - __u32 latency_us)) + void (*next_neigh)(void *ptr, __be64 addr, __u32 latency_us)) { int rc; @@ -1109,7 +1056,7 @@ static __u32 _send_rdsock_up_interfaces(char **interfaces, int send_rdsock_up_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, - char *addr, __u32 addrlen, + int has_addr, __be64 addr, char **interfaces, __u32 num_interfaces, int all_interfaces) { int rc; @@ -1118,13 +1065,19 @@ int send_rdsock_up_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, __u32 interface_len2; char *buf; + __u32 buflen; + __u32 offset = 0; __u64 flags = 0; - __u32 addrlenbe; + if (has_addr == 0 && addr != 0) + return RC_CONNBROKEN; + + if (has_addr != 0) + flags |= CRD_UTK_UP_FLAGS_ADDR; if (!all_interfaces) { - flags = CRD_UTK_UP_FLAGS_INTERFACES; + flags |= CRD_UTK_UP_FLAGS_INTERFACES; interface_len = _send_rdsock_up_interfacelen(interfaces, num_interfaces); @@ -1132,46 +1085,45 @@ int send_rdsock_up_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, ASSERT(interface_len <= 1024*1024*1024); } - if (addr == 0 && addrlen != 0) - return RC_CONNBROKEN; - - if (addrlen > 64) - return RC_CONNBROKEN; - flags = htonll(flags); - addrlenbe = htonl(addrlen); + buflen = 8 + 8 + interface_len; + buf = malloc(8 + 8 + interface_len); - buf = malloc(12 + addrlen + interface_len); + memcpy(buf + offset, (char *) &flags, 8); + offset += 8; - memcpy(buf, (char *) &flags, 8); - memcpy(buf + 8, (char *) &addrlenbe, 4); - if (addrlen > 0) - memcpy(buf + 12, addr, addrlen); + if (has_addr != 0) { + memcpy(buf + offset, (char *) &addr, 8); + offset += 8; + } if (!all_interfaces) { interface_len2 = _send_rdsock_up_interfaces(interfaces, num_interfaces, - buf + 12 + addrlen, interface_len); + buf + offset, interface_len); ASSERT(interface_len == interface_len2); + offset += interface_len2; } - rc = send_rdsock_cmd(fd, nr, CRD_UTK_UP, buf, - 12 + addrlen + interface_len); + ASSERT(offset <= buflen); + + rc = send_rdsock_cmd(fd, nr, CRD_UTK_UP, buf, offset); free(buf); buf = 0; return rc; } -int send_rdsock_up(int fd, char *addr, __u32 addrlen, +int send_rdsock_up(int fd, + int has_addr, __be64 addr, char **interfaces, __u32 num_interfaces, int all_interfaces) { int rc = RC_WOULDBLOCK; struct libcor_nonblock_resumeinfo nr; bzero(&nr, sizeof(struct libcor_nonblock_resumeinfo)); - rc = send_rdsock_up_nonblock(fd, &nr, addr, addrlen, + rc = send_rdsock_up_nonblock(fd, &nr, has_addr, addr, interfaces, num_interfaces, all_interfaces); if (unlikely(rc == RC_WOULDBLOCK)) { ASSERT_ERR(); diff --git a/src/routed/libcor.h b/src/routed/libcor.h index a576289..19973d9 100644 --- a/src/routed/libcor.h +++ b/src/routed/libcor.h @@ -34,14 +34,16 @@ */ #define CRD_UTK_UP 2 -#define CRD_UTK_UP_FLAGS_INTERFACES 1 +#define CRD_UTK_UP_FLAGS_ADDR 1 +#define CRD_UTK_UP_FLAGS_INTERFACES 2 /* - * CRD_UTK_UP[4] length[4] flags[8] addrlen[4] addr[addrlen] + * CRD_UTK_UP[4] length[4] flags[8] + * if CRD_UTK_UP_FLAGS_ADDR + * addr[8] * if CRD_UTK_UP_FLAGS_INTERFACES: * num_interfaces[4] (length[4] interface[length])[num_interfaces] */ - #define CRD_UTK_CONNECTERROR 3 /* * CRD_UTK_CONNECTERROR[4] length[4] cookie[8] error[4] @@ -60,7 +62,6 @@ #define RESUME_READ_FUNC_RDSOCK_CMD 5 #define NEIGHLIST_MAX_FIELDS 256 -#define ADDR_MAX_LEN 1024 #define WRITE_BUF_SIZE 1024 struct listneigh_field { @@ -183,10 +184,9 @@ extern int read_resp_bin_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, extern int read_resp_bin(int fd, char **resp_buf, __u32 *resp_len); extern int send_connect_neigh_nonblock(int fd, - struct libcor_nonblock_resumeinfo *nr, - __u32 addrlen, char *addr); + struct libcor_nonblock_resumeinfo *nr, __be64 addr); -extern int send_connect_neigh(int fd, __u32 addrlen, char *addr); +extern int send_connect_neigh(int fd, __be64 addr); extern int send_connect_port_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, @@ -212,8 +212,7 @@ extern int send_list_neigh(int fd); extern int parse_neigh_list(char *buf, __u32 len, __u32 default_latency_us, void *ptr, void (*init)(void *ptr, __u32 numneigh), - void (*next_neigh)(void *ptr, __u32 addrlen, char *addr, - __u32 latency_us)); + void (*next_neigh)(void *ptr, __be64 addr, __u32 latency_us)); extern int pass_socket(int fd, __u64 cookie); @@ -225,10 +224,11 @@ extern int send_rdsock_version(int fd, __u32 version); extern int send_rdsock_up_nonblock(int fd, struct libcor_nonblock_resumeinfo *nr, - char *addr, __u32 addrlen, + int has_addr, __be64 addr, char **interfaces, __u32 num_interfaces, int all_interfaces); -extern int send_rdsock_up(int fd, char *addr, __u32 addrlen, +extern int send_rdsock_up(int fd, + int has_addr, __be64 addr, char **interfaces, __u32 num_interfaces, int all_interfaces); extern int send_rdsock_connecterror_nonblock(int fd, diff --git a/src/routed/main.c b/src/routed/main.c index 9fae544..41851be 100644 --- a/src/routed/main.c +++ b/src/routed/main.c @@ -176,18 +176,12 @@ static int rdscmd_connect(void *ptr, __u64 cookie, goto out_error; } - if (verbosity >= 1) { - printf("rds_connect cookie: %llu, addr: ", cookie); - print_hex(&(addr->addr[0]), sizeof(addr->addr)); - printf(" port: %u\n", ntohs(addr->port)); - } + if (verbosity >= 1) + printf("rds_connect cookie: %llu, addr: %llu port: %u\n", cookie, addr->addr, ntohs(addr->port)); - if (addr_eq(addr->addr, sizeof(addr->addr), 0, 0) != 0 || - addr_eq(addr->addr, sizeof(addr->addr), localaddr, - localaddrlen) != 0) { + if (addr->addr == 0) { node = 0; - } else if ((node = try_find_neigh_byaddr(sizeof(addr->addr), - &(addr->addr[0]))) == 0) { + } else if ((node = try_find_neigh_byaddr(addr->addr)) == 0) { if (verbosity >= 1) printf("connect_to_host host not found\n"); goto out_error; @@ -262,7 +256,6 @@ static void nonblock_resume(struct nonblock_resumeinfo *nr, __u32 eventflags) } else if (nr->type == EPOLLDATA_FWD_TO_R) { fwd_to_r_accept(nr->fd, nr->data.fwd_to_r.fwd_to_host, nr->data.fwd_to_r.addr, - nr->data.fwd_to_r.addrlen, nr->data.fwd_to_r.targetport); } else if (nr->type == EPOLLDATA_FWD_TO_LSERVICE) { fwd_to_lservice_accept(nr->fd, nr->data.fwd_to_lservice.af, @@ -318,6 +311,7 @@ static int open_timerfd() return 0; } +#warning todo move this to libcor (in demo/ too) static int rdsock_negotiate_version(void) { struct rdsock_cmd cmd; @@ -454,7 +448,7 @@ int main(int argc, char *argv[]) if (rc != 0) goto out; - rc = send_rdsock_up(rdsock_fd, localaddr, localaddrlen, + rc = send_rdsock_up(rdsock_fd, has_localaddr, localaddr, interfaces, num_interfaces, all_interfaces); ASSERT(rc == RC_OK); diff --git a/src/routed/nodedb.c b/src/routed/nodedb.c index c349c2e..a8f3f88 100644 --- a/src/routed/nodedb.c +++ b/src/routed/nodedb.c @@ -49,7 +49,7 @@ int service_list_contains(struct service_list *services, __be32 port) return 0; } -struct node * try_find_neigh_byaddr(__u16 addrlen, char *addr) +struct node * try_find_neigh_byaddr(__be64 addr) { struct list_head *curr = node_list_reachable.next; @@ -57,9 +57,8 @@ struct node * try_find_neigh_byaddr(__u16 addrlen, char *addr) struct node *currnode = container_of(curr, struct node, node_list); - if (addr_eq(currnode->addr, currnode->addrlen, addr, addrlen)) { + if (currnode->addr == addr) return currnode; - } curr = curr->next; } @@ -85,116 +84,77 @@ struct node * try_find_neigh_byservice(__be32 serviceport) return 0; } -struct e2eroute * get_e2eroute_localhost(void) +struct e2eroute get_e2eroute_localhost(void) { - struct e2eroute *e2er = malloc(sizeof(struct e2eroute)); - e2er->numhops = 0; - return e2er; + struct e2eroute ret; + bzero(&ret, sizeof(struct e2eroute)); + ret.numhops = 0; + return ret; } - -struct e2eroute_gen{ - char *buf; - __u32 alloclen; - - __u32 addrlen_offset; - __u32 addr_offset; -}; - -void _get_e2eroute(struct node *node, struct e2eroute_gen *gen) +struct e2eroute get_e2eroute(struct node *node) { - if (node->prevhop != 0) - _get_e2eroute(node->prevhop, gen); + struct e2eroute ret; - ASSERT(gen->addr_offset + node->addrlen <= gen->alloclen); - memcpy(gen->buf + gen->addr_offset, node->addr, node->addrlen); - gen->addr_offset += node->addrlen; - - ASSERT(gen->addrlen_offset + 2 <= gen->alloclen); - *((__u16 *) (gen->buf + gen->addrlen_offset)) = node->addrlen; - gen->addrlen_offset += 2; -} - -struct e2eroute * get_e2eroute(struct node *node) -{ struct node *currnode; + __u32 u; - __u32 addrlen_sum = 0; - __u32 hopcount = 0; - - struct e2eroute_gen gen; - - currnode = node; - while (1) { - addrlen_sum += currnode->addrlen; - hopcount += 1; + ASSERT(node != 0); - if (currnode->prevhop == 0) { - ASSERT(currnode->is_neighbor == 1); - break; - } + bzero(&ret, sizeof(struct e2eroute)); + ret.numhops = 0; - currnode = currnode->prevhop; + for (currnode = node;currnode != 0;currnode = currnode->prevhop) { + ret.numhops += 1; + ASSERT(currnode->prevhop != 0 || currnode->is_neighbor == 1); } - bzero(&gen, sizeof(struct e2eroute_gen)); - - gen.alloclen = 2 + hopcount*2 + addrlen_sum; - gen.buf = malloc(gen.alloclen); - bzero(gen.buf, gen.alloclen); - - gen.addrlen_offset = 2; - gen.addr_offset = 2 + hopcount*2; - - ASSERT(2 <= gen.alloclen); - *((__u16 *) (gen.buf + 0)) = hopcount; + ret.addr = calloc(8, ret.numhops); - _get_e2eroute(node, &gen); + u = ret.numhops; + for (currnode = node;currnode != 0;currnode = currnode->prevhop) { + ASSERT(u > 0); + ret.addr[u-1] = node->addr; + u--; - ASSERT(gen.addrlen_offset == 2 + hopcount*2); - ASSERT(gen.addr_offset == gen.alloclen); - - - ASSERT(((struct e2eroute *) gen.buf)->numhops == hopcount); - - return (struct e2eroute *) gen.buf; -} + ASSERT(currnode->prevhop != 0 || currnode->is_neighbor == 1); + } + ASSERT(u == 0); -static __u16 e2eroute_get_addrlen(struct e2eroute *e2er, __u32 idx) -{ - ASSERT(idx < 65536); - ASSERT(idx < e2er->numhops); - return *((__u16 *) (((char *) e2er) + 2 + idx*2)); + return ret; } -static char * e2eroute_get_addr(struct e2eroute *e2er, __u32 idx) +#warning todo call from rds_connect +void free_e2eroute_addr(struct e2eroute *e2er) { - __u32 addrlen_before = 0; - - __u32 u; + if (e2er->numhops == 0) { + ASSERT(e2er->addr == 0); + } else { + ASSERT(e2er->addr != 0); + free(e2er->addr); + e2er->addr = 0; - for (u=0;unumhops = 0; } - return (((char *) e2er) + 2 + e2er->numhops*2 + addrlen_before); + bzero(e2er, sizeof(struct e2eroute)); } int connect_to_host_recv(int fd, struct libcor_nonblock_resumeinfo *lnr, struct nonblock_resumeinfo_connect_to_host_recv *rcr, - struct e2eroute *e2er) + struct e2eroute e2er) { if (rcr->state == 0) { rcr->fd = fd; - rcr->numhops = e2er->numhops; + rcr->numhops = e2er.numhops; rcr->state = 1; rcr->u=0; } ASSERT(rcr->fd == fd); - ASSERT(rcr->numhops == e2er->numhops); + ASSERT(rcr->numhops == e2er.numhops); for (;rcr->u < rcr->numhops;rcr->u++) { int rc = read_resp_nonblock(fd, 0, lnr); @@ -208,7 +168,7 @@ int connect_to_host_recv(int fd, struct libcor_nonblock_resumeinfo *lnr, int connect_to_host_send(int fd, struct libcor_nonblock_resumeinfo *lnr, struct nonblock_resumeinfo_connect_to_host_send *cth, - struct e2eroute *e2er) + struct e2eroute e2er) { if (cth->state == 0) { cth->fd = fd; @@ -220,12 +180,11 @@ int connect_to_host_send(int fd, } ASSERT(cth->fd == fd); - ASSERT(cth->e2er == e2er); + ASSERT(cth->e2er.numhops == e2er.numhops); - for (;cth->u < e2er->numhops;) { + for (;cth->u < e2er.numhops;) { int rc = send_connect_neigh_nonblock(fd, lnr, - e2eroute_get_addrlen(e2er, cth->u), - e2eroute_get_addr(e2er, cth->u)); + e2er.addr[cth->u]); cth->u++; if (rc != RC_OK) return rc; @@ -234,26 +193,15 @@ int connect_to_host_send(int fd, return RC_OK; } -static void add_neigh(void *ptr, __u32 addrlen, char *addr, __u32 latency_us) +static void add_neigh(void *ptr, __be64 addr, __u32 latency_us) { struct route_list *list = (struct route_list *) ptr; struct node *node; - if (verbosity >= 2) { - printf("add neighbor "); - neigh_printaddr(addrlen, addr); - printf("latency %u\n", latency_us); - } - - ASSERT(MAX_ADDRLEN <= 65535); - - if (addrlen > MAX_ADDRLEN) - return; - - if (addr_eq(addr, addrlen, 0, 0)) - return; + if (verbosity >= 2) + printf("add neighbor %llx latency %u\n", ntohll(addr), latency_us); - if (addr_eq(addr, addrlen, localaddr, localaddrlen)) + if (addr == 0) return; if (list->numroutes >= list->rows_alloc) @@ -261,16 +209,11 @@ static void add_neigh(void *ptr, __u32 addrlen, char *addr, __u32 latency_us) ASSERT(list->routes[list->numroutes].dst == 0); - node = try_find_neigh_byaddr(addrlen, addr); + node = try_find_neigh_byaddr(addr); if (node == 0) { node = calloc(1, sizeof(struct node)); - - node->addr = malloc(((int) addrlen)); - bzero(node->addr, ((int) addrlen)); - node->addrlen = (__u16) addrlen; - memcpy(node->addr, addr, addrlen); - + node->addr = addr; list_add_tail(&(node->node_list), &node_list_reachable); } @@ -331,62 +274,18 @@ static void init_servicelist(void *ptr, __u32 numservices) } -static int __export_servicelist(int fd, char *data, __u32 len) -{ - __u32 totalwritten = 0; - - ASSERT(len != U32_MAX); - - while (totalwritten < len) { - int written = write(fd, data + totalwritten, len -totalwritten); - - if (written <= 0) { - if (errno == EINTR) - continue; - - perror("export servicelist"); - return 1; - } - - totalwritten += written; - } - - return 0; -} - -static int _export_servicelist(int fd, char *addr, __u32 addrlen, __u32 cost, +static int _export_servicelist(int fd, __be64 addr, __u32 cost, struct service_list *services) { __u32 u; - char buf[24]; - int rc; for (u=0;unumports;u++) { - __u32 v; - - ASSERT(addrlen != U32_MAX); - - for(v=0;v 0); - ASSERT(rc < 20); - - rc = __export_servicelist(fd, &(buf[0]), rc); - if (rc != 0) - return 1; - } - - rc = snprintf(&(buf[0]), 20, ":%u:%u\n", + int rc = dprintf(fd, "%llx:%u:%u\n", ntohll(addr), ntohl(services->ports[u]), cost); - ASSERT(rc > 0); - ASSERT(rc < 20); - rc = __export_servicelist(fd, &(buf[0]), rc); - if (rc != 0) + if (rc < 0) { return 1; + } } return 0; @@ -410,7 +309,7 @@ static void export_servicelist(void) return; } - if (_export_servicelist(fd, 0, 0, 0, &localservices) != 0) + if (_export_servicelist(fd, 0, 0, &localservices) != 0) goto err; curr = node_list_reachable.next; @@ -420,11 +319,13 @@ static void export_servicelist(void) curr = curr->next; - if (addr_eq(currnode->addr, currnode->addrlen, localaddr, - localaddrlen) != 0) + ASSERT(has_localaddr != 0 || localaddr == 0); + if (currnode->addr == 0) + continue; + if (currnode->addr == localaddr) continue; - if (_export_servicelist(fd, currnode->addr, currnode->addrlen, + if (_export_servicelist(fd, currnode->addr, currnode->metric, &(currnode->services)) != 0) goto err; } @@ -685,11 +586,8 @@ void _discover_network(int fd, struct nonblock_resumeinfo *nr) ASSERT(0); } - if (verbosity >= 2) { - printf("discover resume neighbor "); - neigh_printaddr(node->addrlen, node->addr); - printf("\n"); - } + if (verbosity >= 2) + printf("discover resume neighbor %llx\n", ntohll(node->addr)); nr->data.discover_network.e2er = get_e2eroute(node); @@ -720,8 +618,7 @@ state_3: if (check_rc(rc, "load_neigh_list: connect_to_host_recv error")) goto out; - free(nr->data.discover_network.e2er); - nr->data.discover_network.e2er = 0; + free_e2eroute_addr(&nr->data.discover_network.e2er); nr->data.discover_network.state = 4; state_4: @@ -774,11 +671,7 @@ state_7: out: //printf("load_neigh_list state %d\n", nr->data.discover_network.state); if (rc != RC_WOULDBLOCK) { - - if (nr->data.discover_network.e2er != 0) { - free(nr->data.discover_network.e2er); - nr->data.discover_network.e2er = 0; - } + free_e2eroute_addr(&nr->data.discover_network.e2er); //printf("load_neigh_list rc %d\n", rc); close(fd); @@ -799,11 +692,8 @@ static int _discover_network_start_load_node(struct node *node) struct epoll_event epe; - if (verbosity >= 2) { - printf("discover start neighbor "); - neigh_printaddr(node->addrlen, node->addr); - printf("\n"); - } + if (verbosity >= 2) + printf("discover start neighbor %llx\n", ntohll(node->addr)); ASSERT(node->query_in_progress == 0); node->query_in_progress = 1; diff --git a/src/routed/routed.h b/src/routed/routed.h index 75877e0..2303cdf 100644 --- a/src/routed/routed.h +++ b/src/routed/routed.h @@ -67,7 +67,7 @@ struct service_list{ #define QUERY_STATUS_ERROR 2 struct node{ - char *addr; + __be64 addr; struct route_list routes; @@ -103,8 +103,7 @@ struct search_query{ struct e2eroute{ __u16 numhops; - /* __u16 addrlen[numhops] */ - /* char addr[][] */ + __be64 *addr; /* array of size numhops */ }; @@ -118,7 +117,7 @@ struct e2eroute{ struct nonblock_resumeinfo_connect_to_host_send{ int fd; - struct e2eroute *e2er; + struct e2eroute e2er; __u8 state; @@ -147,7 +146,7 @@ struct nonblock_resumeinfo{ struct node *node; - struct e2eroute *e2er; + struct e2eroute e2er; struct nonblock_resumeinfo_connect_to_host_send connect_send; struct nonblock_resumeinfo_connect_to_host_recv connect_recv; @@ -159,7 +158,7 @@ struct nonblock_resumeinfo{ __u8 state; __u64 cookie; - struct e2eroute *e2er; + struct e2eroute e2er; __be32 port; struct nonblock_resumeinfo_connect_to_host_send connect_send; @@ -174,8 +173,7 @@ struct nonblock_resumeinfo{ struct{ __u8 fwd_to_host; - char *addr; - __u16 addrlen; + __be64 addr; __be32 targetport; }fwd_to_r; @@ -207,8 +205,7 @@ struct fwd_to_r_item{ int bindfd; __u8 fwd_to_host; - char *addr; - __u16 addrlen; + __be64 addr; __be32 targetport; }; @@ -249,16 +246,10 @@ extern int rdsock_fd; /* util.c */ extern void set_nonblock(int fd, int value); -extern int addr_eq(char *addr1, __u32 addrlen1, char *addr2, __u32 addrlen2); - -extern void print_hex(char *buf, __u32 len); - -extern void neigh_printaddr(__u16 addrlen, char *addr); - /* conf.c */ -extern char *localaddr; -extern __u16 localaddrlen; +extern int has_localaddr; +extern __be64 localaddr; extern char **interfaces; extern __u32 num_interfaces; @@ -282,22 +273,22 @@ extern struct list_head node_list; extern int service_list_contains(struct service_list *services, __be32 port); -extern struct node * try_find_neigh_byaddr(__u16 addrlen, char *addr); +extern struct node * try_find_neigh_byaddr(__be64 addr); extern struct node * try_find_neigh_byservice(__be32 serviceport); -extern struct e2eroute * get_e2eroute_localhost(void); +extern struct e2eroute get_e2eroute_localhost(void); -extern struct e2eroute * get_e2eroute(struct node *node); +extern struct e2eroute get_e2eroute(struct node *node); extern int connect_to_host_recv(int fd, struct libcor_nonblock_resumeinfo *lnr, struct nonblock_resumeinfo_connect_to_host_recv *rcr, - struct e2eroute *e2er); + struct e2eroute e2er); extern int connect_to_host_send(int fd, struct libcor_nonblock_resumeinfo *lnr, struct nonblock_resumeinfo_connect_to_host_send *cth, - struct e2eroute *e2er); + struct e2eroute e2er); extern void _discover_network(int fd, struct nonblock_resumeinfo *nr); @@ -308,8 +299,8 @@ extern int initial_discovery_inprogress(void); extern void init_nodedb(void); /* forward.c */ -extern void fwd_to_r_accept(int listenerfd, __u8 fwd_to_host, char *hostaddr, - __u32 hostaddrlen, __be32 targetport); +extern void fwd_to_r_accept(int listenerfd, __u8 fwd_to_host, __be64 hostaddr, + __be32 targetport); extern void fwd_to_lservice_accept(int listenerfd, int af, struct sockaddr *saddr, socklen_t saddrlen); diff --git a/src/routed/util.c b/src/routed/util.c index 719f4b8..d35a63c 100644 --- a/src/routed/util.c +++ b/src/routed/util.c @@ -42,50 +42,3 @@ void set_nonblock(int fd, int value) exit(1); } } - -int addr_eq(char *addr1, __u32 addrlen1, char *addr2, __u32 addrlen2) -{ - while (addrlen1>0) { - if (addr1[addrlen1-1] == 0) { - addrlen1--; - } else { - break; - } - } - - while (addrlen2>0) { - if (addr2[addrlen2-1] == 0) { - addrlen2--; - } else { - break; - } - } - - if (addrlen1 != addrlen2) - return 0; - - if (addrlen1 > MAX_ADDRLEN) - return 0; - - if (addrlen1 != 0 && memcmp(addr1, addr2, addrlen1) != 0) - return 0; - - return 1; -} - -void print_hex(char *buf, __u32 len) -{ - __u32 u; - - ASSERT(len != U32_MAX); - - for(u=0;u #include #include @@ -41,62 +42,52 @@ } while(0) -static inline int parse_hexchar(char c) +static __u64 htonll(__u64 in) { - if (c == '0') - return 0; - else if (c == '1') - return 1; - else if (c == '2') - return 2; - else if (c == '3') - return 3; - else if (c == '4') - return 4; - else if (c == '5') - return 5; - else if (c == '6') - return 6; - else if (c == '7') - return 7; - else if (c == '8') - return 8; - else if (c == '9') - return 9; - else if (c == 'A' || c == 'a') - return 10; - else if (c == 'B' || c == 'b') - return 11; - else if (c == 'C' || c == 'c') - return 12; - else if (c == 'D' || c == 'd') - return 13; - else if (c == 'E' || c == 'e') - return 14; - else if (c == 'F' || c == 'f') - return 15; - else - return -1; -} - -static inline int parse_hex(char *target, char *src, int srclen) -{ - int u; + __u64 out = 0; + char *p_out = (char *) &out; - ASSERT(src != 0); - ASSERT(target != 0); - ASSERT(srclen > 0); - ASSERT((srclen%2) == 0); + p_out[7] = in & 255; + in = in >> 8; + p_out[6] = in & 255; + in = in >> 8; + p_out[5] = in & 255; + in = in >> 8; + p_out[4] = in & 255; + in = in >> 8; + p_out[3] = in & 255; + in = in >> 8; + p_out[2] = in & 255; + in = in >> 8; + p_out[1] = in & 255; + in = in >> 8; + p_out[0] = in & 255; - for(u=0;u<(srclen/2);u++) { - int high = parse_hexchar(src[u*2]); - int low = parse_hexchar(src[u*2+1]); + return out; +} - if (high == -1 || low == -1) - return -1; +static __u64 ntohll(__u64 in) +{ + __u64 out = 0; + char *p_in = (char *) ∈ - target[u] = (char) ((high << 4) + low); - } + out += p_in[7]; + out << 8; + out += p_in[6]; + out << 8; + out += p_in[5]; + out << 8; + out += p_in[4]; + out << 8; + out += p_in[3]; + out << 8; + out += p_in[2]; + out << 8; + out += p_in[1]; + out << 8; + out += p_in[0]; - return 0; + return out; } + +extern int parse_cor_addr(__be64 *addr, char *saddr, char terminator); -- 2.11.4.GIT