diff --git a/include/re_stun.h b/include/re_stun.h index e60dc18..cc645cc 100644 --- a/include/re_stun.h +++ b/include/re_stun.h @@ -24,6 +24,13 @@ enum stun_af { STUN_AF_IPv6 = 0x02 /**< IPv6 Address Family */ }; +/** STUN Transport */ +enum stun_transp { + STUN_TRANSP_UDP = IPPROTO_UDP, /**< UDP-transport (struct udp_sock) */ + STUN_TRANSP_TCP = IPPROTO_TCP, /**< TCP-transport (struct tcp_conn) */ + STUN_TRANSP_DTLS, /**< DTLS-transport (struct tls_conn) */ +}; + /** STUN Methods */ enum stun_method { STUN_METHOD_BINDING = 0x001, diff --git a/include/re_tls.h b/include/re_tls.h index 350aa3e..d9d63e4 100644 --- a/include/re_tls.h +++ b/include/re_tls.h @@ -69,3 +69,5 @@ int dtls_accept(struct tls_conn **ptc, struct tls *tls, dtls_estab_h *estabh, dtls_recv_h *recvh, dtls_close_h *closeh, void *arg); int dtls_send(struct tls_conn *tc, struct mbuf *mb); +void dtls_set_handlers(struct tls_conn *tc, dtls_estab_h *estabh, + dtls_recv_h *recvh, dtls_close_h *closeh, void *arg); diff --git a/src/stun/ctrans.c b/src/stun/ctrans.c index 54643fd..03a6b78 100644 --- a/src/stun/ctrans.c +++ b/src/stun/ctrans.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -85,7 +87,8 @@ static void timeout_handler(void *arg) goto error; ct->mb->pos = ct->pos; - err = udp_send(ct->sock, &ct->dst, ct->mb); + + err = stun_send(ct->proto, ct->sock, &ct->dst, ct->mb); if (err) goto error; @@ -292,6 +295,19 @@ int stun_ctrans_request(struct stun_ctrans **ctp, struct stun *stun, int proto, tcp_close_handler, ct); break; + case STUN_TRANSP_DTLS: + if (!sock) { + err = EINVAL; + break; + } + + ct->ival = stun_conf(stun)->rto; + tmr_start(&ct->tmr, ct->ival, timeout_handler, ct); + + ct->txc = 1; + err = dtls_send(ct->sock, mb); + break; + default: err = EPROTONOSUPPORT; break; diff --git a/src/stun/stun.c b/src/stun/stun.c index 4eb3562..9ec738e 100644 --- a/src/stun/stun.c +++ b/src/stun/stun.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #include #include @@ -108,6 +110,10 @@ int stun_send(int proto, void *sock, const struct sa *dst, struct mbuf *mb) err = tcp_send(sock, mb); break; + case STUN_TRANSP_DTLS: + err = dtls_send(sock, mb); + break; + default: err = EPROTONOSUPPORT; break; diff --git a/src/tls/openssl/tls_udp.c b/src/tls/openssl/tls_udp.c index bc8732e..6d72120 100644 --- a/src/tls/openssl/tls_udp.c +++ b/src/tls/openssl/tls_udp.c @@ -577,6 +577,28 @@ int dtls_send(struct tls_conn *tc, struct mbuf *mb) } +/** + * Set handlers on a DTLS Connection + * + * @param tc DTLS Connection + * @param estabh DTLS Connection Established handler + * @param recvh DTLS Connection Receive data handler + * @param closeh DTLS Connection Close handler + * @param arg Handler argument + */ +void dtls_set_handlers(struct tls_conn *tc, dtls_estab_h *estabh, + dtls_recv_h *recvh, dtls_close_h *closeh, void *arg) +{ + if (!tc) + return; + + tc->estabh = estabh; + tc->recvh = recvh; + tc->closeh = closeh; + tc->arg = arg; +} + + static void sock_destructor(void *arg) { struct dtls_sock *sock = arg; diff --git a/src/turn/turnc.c b/src/turn/turnc.c index e06c0b9..1a17b09 100644 --- a/src/turn/turnc.c +++ b/src/turn/turnc.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include #include #include "turnc.h" @@ -535,6 +537,10 @@ int turnc_send(struct turnc *turnc, const struct sa *dst, struct mbuf *mb) err = tcp_send(turnc->sock, mb); break; + case STUN_TRANSP_DTLS: + err = dtls_send(turnc->sock, mb); + break; + default: err = EPROTONOSUPPORT; break;