From b8fb22fa715347f1012054c1e009b855f2a57c50 Mon Sep 17 00:00:00 2001 From: Richard Aas Date: Mon, 28 Feb 2011 06:55:25 +0000 Subject: [PATCH] tcp/udp: helper api cleanup --- docs/TODO | 2 -- include/re_tcp.h | 3 ++- include/re_udp.h | 2 +- src/ice/comp.c | 2 +- src/stun/keepalive.c | 2 +- src/tcp/symbian/tcp.cpp | 26 ++++++++++++++++++-------- src/tcp/tcp.c | 22 +++++++++++++++++----- src/tls/openssl/tls_tcp.c | 7 ++++--- src/turn/turnc.c | 2 +- src/udp/symbian/udp.cpp | 15 +++++---------- src/udp/udp.c | 7 ++----- 11 files changed, 52 insertions(+), 38 deletions(-) diff --git a/docs/TODO b/docs/TODO index bebd8f7..b1f506e 100644 --- a/docs/TODO +++ b/docs/TODO @@ -3,8 +3,6 @@ TODO ------------------------------------------------------------------------------- Version v0.2.0 - tcp_register_helper: remove int *fd, add int layer - udp_register_helper: remove int *fd ------------------------------------------------------------------------------- Version v0.x.y diff --git a/include/re_tcp.h b/include/re_tcp.h index a614421..01ebeb0 100644 --- a/include/re_tcp.h +++ b/include/re_tcp.h @@ -96,6 +96,7 @@ typedef bool (tcp_helper_recv_h)(int *err, struct mbuf *mb, bool *estab, struct tcp_helper; -int tcp_register_helper(struct tcp_helper **thp, struct tcp_conn *tc, int *fd, +int tcp_register_helper(struct tcp_helper **thp, struct tcp_conn *tc, + int layer, tcp_helper_estab_h *eh, tcp_helper_send_h *sh, tcp_helper_recv_h *rh, void *arg); diff --git a/include/re_udp.h b/include/re_udp.h index 73d2018..2dbf52d 100644 --- a/include/re_udp.h +++ b/include/re_udp.h @@ -46,7 +46,7 @@ struct udp_helper; int udp_register_helper(struct udp_helper **uhp, struct udp_sock *us, - int *fd, int layer, + int layer, udp_helper_send_h *sh, udp_helper_recv_h *rh, void *arg); diff --git a/src/ice/comp.c b/src/ice/comp.c index 3893743..ed75e57 100644 --- a/src/ice/comp.c +++ b/src/ice/comp.c @@ -156,7 +156,7 @@ int icem_comp_alloc(struct icem_comp **cp, struct icem *icem, int id, comp->sock = mem_ref(sock); comp->icem = icem; - err = udp_register_helper(&comp->uh, sock, NULL, icem->layer, + err = udp_register_helper(&comp->uh, sock, icem->layer, NULL, /*helper_send_handler*/ helper_recv_handler, comp); if (err) diff --git a/src/stun/keepalive.c b/src/stun/keepalive.c index 8163da4..0675932 100644 --- a/src/stun/keepalive.c +++ b/src/stun/keepalive.c @@ -201,7 +201,7 @@ int stun_keepalive_alloc(struct stun_keepalive **skap, switch (proto) { case IPPROTO_UDP: - err = udp_register_helper(&ska->uh, sock, NULL, layer, + err = udp_register_helper(&ska->uh, sock, layer, NULL, udp_recv_handler, ska); break; diff --git a/src/tcp/symbian/tcp.cpp b/src/tcp/symbian/tcp.cpp index af0d300..cbd929c 100644 --- a/src/tcp/symbian/tcp.cpp +++ b/src/tcp/symbian/tcp.cpp @@ -117,6 +117,7 @@ public: struct tcp_helper { struct le le; + int layer; tcp_helper_estab_h *estabh; tcp_helper_send_h *sendh; tcp_helper_recv_h *recvh; @@ -965,13 +966,24 @@ static bool helper_recv_handler(int *err, struct mbuf *mb, bool *estab, } -int tcp_register_helper(struct tcp_helper **thp, struct tcp_conn *tc, int *fd, +static bool sort_handler(struct le *le1, struct le *le2, void *arg) +{ + struct tcp_helper *th1 = (struct tcp_helper *)le1->data; + struct tcp_helper *th2 = (struct tcp_helper *)le2->data; + (void)arg; + + return th1->layer <= th2->layer; +} + + +int tcp_register_helper(struct tcp_helper **thp, struct tcp_conn *tc, + int layer, tcp_helper_estab_h *eh, tcp_helper_send_h *sh, tcp_helper_recv_h *rh, void *arg) { struct tcp_helper *th; - if (!thp || !tc) + if (!tc) return EINVAL; th = (struct tcp_helper *)mem_zalloc(sizeof(*th), helper_destructor); @@ -980,18 +992,16 @@ int tcp_register_helper(struct tcp_helper **thp, struct tcp_conn *tc, int *fd, list_append(&tc->helpers, &th->le, th); + th->layer = layer; th->estabh = eh ? eh : helper_estab_handler; th->sendh = sh ? sh : helper_send_handler; th->recvh = rh ? rh : helper_recv_handler; th->arg = arg; -#if 0 - if (fd) - *fd = tc->fdc; -#endif - (void)fd; + list_sort(&tc->helpers, sort_handler, NULL); - *thp = th; + if (thp) + *thp = th; return 0; } diff --git a/src/tcp/tcp.c b/src/tcp/tcp.c index cc01da3..1efca8e 100644 --- a/src/tcp/tcp.c +++ b/src/tcp/tcp.c @@ -84,6 +84,7 @@ struct tcp_conn { struct tcp_helper { struct le le; + int layer; tcp_helper_estab_h *estabh; tcp_helper_send_h *sendh; tcp_helper_recv_h *recvh; @@ -1191,13 +1192,23 @@ int tcp_conn_fd(const struct tcp_conn *tc) } -int tcp_register_helper(struct tcp_helper **thp, struct tcp_conn *tc, int *fd, +static bool sort_handler(struct le *le1, struct le *le2, void *arg) +{ + struct tcp_helper *th1 = le1->data, *th2 = le2->data; + (void)arg; + + return th1->layer <= th2->layer; +} + + +int tcp_register_helper(struct tcp_helper **thp, struct tcp_conn *tc, + int layer, tcp_helper_estab_h *eh, tcp_helper_send_h *sh, tcp_helper_recv_h *rh, void *arg) { struct tcp_helper *th; - if (!thp || !tc) + if (!tc) return EINVAL; th = mem_zalloc(sizeof(*th), helper_destructor); @@ -1206,15 +1217,16 @@ int tcp_register_helper(struct tcp_helper **thp, struct tcp_conn *tc, int *fd, list_append(&tc->helpers, &th->le, th); + th->layer = layer; th->estabh = eh ? eh : helper_estab_handler; th->sendh = sh ? sh : helper_send_handler; th->recvh = rh ? rh : helper_recv_handler; th->arg = arg; - if (fd) - *fd = tc->fdc; + list_sort(&tc->helpers, sort_handler, NULL); - *thp = th; + if (thp) + *thp = th; return 0; } diff --git a/src/tls/openssl/tls_tcp.c b/src/tls/openssl/tls_tcp.c index 9ee7266..2021b98 100644 --- a/src/tls/openssl/tls_tcp.c +++ b/src/tls/openssl/tls_tcp.c @@ -209,8 +209,9 @@ static bool send_handler(int *err, struct mbuf *mb, void *arg) int tls_start_tcp(struct tls_conn **ptc, struct tls *tls, struct tcp_conn *tcp) { - int err, fd; struct tls_conn *tc; + const int layer = 0; + int err; if (!ptc || !tls || !tcp) return EINVAL; @@ -219,7 +220,7 @@ int tls_start_tcp(struct tls_conn **ptc, struct tls *tls, struct tcp_conn *tcp) if (!tc) return ENOMEM; - err = tcp_register_helper(&tc->th, tcp, &fd, estab_handler, + err = tcp_register_helper(&tc->th, tcp, layer, estab_handler, send_handler, recv_handler, tc); if (err) goto out; @@ -241,7 +242,7 @@ int tls_start_tcp(struct tls_conn **ptc, struct tls *tls, struct tcp_conn *tcp) goto out; } - tc->sbio_out = BIO_new_socket(fd, BIO_NOCLOSE); + tc->sbio_out = BIO_new_socket(tcp_conn_fd(tcp), BIO_NOCLOSE); if (!tc->sbio_out) { DEBUG_WARNING("alloc: BIO_new_socket() failed\n"); goto out; diff --git a/src/turn/turnc.c b/src/turn/turnc.c index c4a2f06..5a4e203 100644 --- a/src/turn/turnc.c +++ b/src/turn/turnc.c @@ -416,7 +416,7 @@ int turnc_alloc(struct turnc **turncp, const struct stun_conf *conf, int proto, switch (proto) { case IPPROTO_UDP: - err = udp_register_helper(&turnc->uh, sock, NULL, layer, + err = udp_register_helper(&turnc->uh, sock, layer, udp_send_handler, udp_recv_handler, turnc); break; diff --git a/src/udp/symbian/udp.cpp b/src/udp/symbian/udp.cpp index e3d9836..f99abc6 100644 --- a/src/udp/symbian/udp.cpp +++ b/src/udp/symbian/udp.cpp @@ -536,18 +536,19 @@ static bool sort_handler(struct le *le1, struct le *le2, void *arg) struct udp_helper *uh1 = (struct udp_helper *)le1->data; struct udp_helper *uh2 = (struct udp_helper *)le2->data; (void)arg; + return uh1->layer <= uh2->layer; } int udp_register_helper(struct udp_helper **uhp, struct udp_sock *us, - int *fd, int layer, + int layer, udp_helper_send_h *sh, udp_helper_recv_h *rh, void *arg) { struct udp_helper *uh; - if (!uhp || !us) + if (!us) return EINVAL; uh = (struct udp_helper *)mem_zalloc(sizeof(*uh), helper_destructor); @@ -563,14 +564,8 @@ int udp_register_helper(struct udp_helper **uhp, struct udp_sock *us, list_sort(&us->helpers, sort_handler, NULL); -#if 0 - /* Symbian has no `fd' */ - if (fd) - *fd = us->fd; -#endif - (void)fd; - - *uhp = uh; + if (uhp) + *uhp = uh; return 0; } diff --git a/src/udp/udp.c b/src/udp/udp.c index c498206..ebf5566 100644 --- a/src/udp/udp.c +++ b/src/udp/udp.c @@ -720,6 +720,7 @@ static bool sort_handler(struct le *le1, struct le *le2, void *arg) { struct udp_helper *uh1 = le1->data, *uh2 = le2->data; (void)arg; + return uh1->layer <= uh2->layer; } @@ -729,7 +730,6 @@ static bool sort_handler(struct le *le1, struct le *le2, void *arg) * * @param uhp Pointer to allocated UDP helper object * @param us UDP socket - * @param fd Returned file-descriptor (optional) * @param layer Layer number; higher number means higher up in stack * @param sh Send handler * @param rh Receive handler @@ -738,7 +738,7 @@ static bool sort_handler(struct le *le1, struct le *le2, void *arg) * @return 0 if success, otherwise errorcode */ int udp_register_helper(struct udp_helper **uhp, struct udp_sock *us, - int *fd, int layer, + int layer, udp_helper_send_h *sh, udp_helper_recv_h *rh, void *arg) { @@ -760,9 +760,6 @@ int udp_register_helper(struct udp_helper **uhp, struct udp_sock *us, list_sort(&us->helpers, sort_handler, NULL); - if (fd) - *fd = us->fd; - if (uhp) *uhp = uh;