Skip to content

Commit

Permalink
udp: Add function udp_open for socket without bind
Browse files Browse the repository at this point in the history
For (multicast) RTP transmission feature we need a function that simply creates
an UDP socket without bind and attaching a thread for incomming data.
  • Loading branch information
cspiel1 authored and sreimers committed Feb 27, 2021
1 parent 9b2bce3 commit 1cfda99
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/re_udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
45 changes: 45 additions & 0 deletions src/udp/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1cfda99

Please sign in to comment.