diff --git a/include/re_rtp.h b/include/re_rtp.h index 06c7ff3b7..9b63ecfac 100644 --- a/include/re_rtp.h +++ b/include/re_rtp.h @@ -202,6 +202,7 @@ int rtp_alloc(struct rtp_sock **rsp); int rtp_listen(struct rtp_sock **rsp, int proto, const struct sa *ip, uint16_t min_port, uint16_t max_port, bool enable_rtcp, rtp_recv_h *recvh, rtcp_recv_h *rtcph, void *arg); +int rtp_open(struct rtp_sock **rsp, int af); int rtp_hdr_encode(struct mbuf *mb, const struct rtp_header *hdr); int rtp_hdr_decode(struct rtp_header *hdr, struct mbuf *mb); int rtp_encode(struct rtp_sock *rs, bool ext, bool marker, uint8_t pt, diff --git a/include/re_udp.h b/include/re_udp.h index f744f2f9b..f796138bd 100644 --- a/include/re_udp.h +++ b/include/re_udp.h @@ -23,6 +23,7 @@ typedef void (udp_error_h)(int err, void *arg); int udp_listen(struct udp_sock **usp, const struct sa *local, udp_recv_h *rh, void *arg); int udp_connect(struct udp_sock *us, const struct sa *peer); +int udp_open(struct udp_sock **usp, int af); int udp_send(struct udp_sock *us, const struct sa *dst, struct mbuf *mb); int udp_send_anon(const struct sa *dst, struct mbuf *mb); int udp_local_get(const struct udp_sock *us, struct sa *local); diff --git a/src/rtp/rtp.c b/src/rtp/rtp.c index 066199d57..dcd74b048 100644 --- a/src/rtp/rtp.c +++ b/src/rtp/rtp.c @@ -345,6 +345,39 @@ int rtp_listen(struct rtp_sock **rsp, int proto, const struct sa *ip, } +/** + * Open RTP Socket without bind. + * + * @param rsp Pointer to returned RTP socket + * @param af Address family AF_INET or AF_INET6 + * + * @return 0 for success, otherwise errorcode + */ +int rtp_open(struct rtp_sock **rsp, int af) +{ + struct rtp_sock *rs; + struct udp_sock *us_rtp; + int err; + + err = rtp_alloc(&rs); + if (err) + return err; + + rs->proto = IPPROTO_UDP; + + us_rtp = NULL; + err = udp_open(&us_rtp, af); + rs->sock_rtp = us_rtp; + + if (err) + mem_deref(rs); + else + *rsp = rs; + + return err; +} + + /** * Encode a new RTP header into the beginning of the buffer * diff --git a/src/udp/udp.c b/src/udp/udp.c index f6312ab19..4f29ce9a3 100644 --- a/src/udp/udp.c +++ b/src/udp/udp.c @@ -391,6 +391,51 @@ int udp_listen(struct udp_sock **usp, const struct sa *local, } +/** + * Create an UDP socket with specified address family. + * + * @param usp Pointer to returned UDP Socket + * @param af Address family AF_INET or AF_INET6 + * + * @return 0 if success, otherwise errorcode + */ +int udp_open(struct udp_sock **usp, int af) +{ + struct udp_sock *us = NULL; + int err = 0; + int fd = -1; + + if (!usp) + return EINVAL; + + us = mem_zalloc(sizeof(*us), udp_destructor); + if (!us) + return ENOMEM; + + us->fd = -1; + us->fd6 = -1; + + fd = SOK_CAST socket(af, SOCK_DGRAM, IPPROTO_UDP); + if (fd < 0) { + err = errno; + goto out; + } + + if (af == AF_INET) + us->fd = fd; + else + us->fd6 = fd; + + out: + if (err) + mem_deref(us); + else + *usp = us; + + return err; +} + + /** * Connect a UDP Socket to a specific peer. * When connected, this UDP Socket will only receive data from that peer.