Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Apr 7, 2022
1 parent 58b375f commit b42df7d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion include/re_udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void udp_handler_set(struct udp_sock *us, udp_recv_h *rh, void *arg);
void udp_error_handler_set(struct udp_sock *us, udp_error_h *eh);
int udp_thread_attach(struct udp_sock *us);
void udp_thread_detach(struct udp_sock *us);
int udp_sock_fd(const struct udp_sock *us, int af);
re_sock_t udp_sock_fd(const struct udp_sock *us, int af);

int udp_multicast_join(struct udp_sock *us, const struct sa *group);
int udp_multicast_leave(struct udp_sock *us, const struct sa *group);
Expand Down
94 changes: 47 additions & 47 deletions src/udp/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ typedef UINT32 QOS_FLOWID, *PQOS_FLOWID;
#ifndef QOS_NON_ADAPTIVE_FLOW
#define QOS_NON_ADAPTIVE_FLOW 0x00000002
#endif
#endif
#include <winsock2.h>
#endif /*!_MSC_VER*/
#include <qos2.h>
#endif
#endif /*WIN32*/

#define DEBUG_MODULE "udp"
#define DEBUG_LEVEL 5
Expand Down Expand Up @@ -71,8 +70,8 @@ struct udp_sock {
udp_recv_h *rh; /**< Receive handler */
udp_error_h *eh; /**< Error handler */
void *arg; /**< Handler argument */
RE_SOCKET fd; /**< Socket file descriptor */
RE_SOCKET fd6; /**< IPv6 socket file descriptor */
re_sock_t fd; /**< Socket file descriptor */
re_sock_t fd6; /**< IPv6 socket file descriptor */
bool conn; /**< Connected socket flag */
size_t rxsz; /**< Maximum receive chunk size */
size_t rx_presz; /**< Preallocated rx buffer size */
Expand Down Expand Up @@ -138,12 +137,12 @@ static void udp_destructor(void *data)
(void)QOSCloseHandle(us->qos);
#endif

if (-1 != us->fd) {
if (BAD_SOCK != us->fd) {
fd_close(us->fd);
(void)close(us->fd);
}

if (-1 != us->fd6) {
if (BAD_SOCK != us->fd6) {
fd_close(us->fd6);
(void)close(us->fd6);
}
Expand All @@ -166,7 +165,7 @@ static void udp_read(struct udp_sock *us, int fd)
SIZ_CAST (mb->size - us->rx_presz), 0,
&src.u.sa, &src.len);
if (n < 0) {
err = ERRNO_SOCKET;
err = ERRNO_SOCK;

if (EAGAIN == err)
goto out;
Expand All @@ -191,13 +190,13 @@ static void udp_read(struct udp_sock *us, int fd)
if (err)
goto out;

if (-1 != us->fd) {
if (BAD_SOCK != us->fd) {
fd_close(us->fd);
(void)close(us->fd);
us->fd = -1;
}

if (-1 != us->fd6) {
if (BAD_SOCK != us->fd6) {
fd_close(us->fd6);
(void)close(us->fd6);
us->fd6 = -1;
Expand All @@ -210,8 +209,8 @@ static void udp_read(struct udp_sock *us, int fd)
us->fd = us_new->fd;
us->fd6 = us_new->fd6;

us_new->fd = -1;
us_new->fd6 = -1;
us_new->fd = BAD_SOCK;
us_new->fd6 = BAD_SOCK;

mem_deref(us_new);

Expand Down Expand Up @@ -286,7 +285,7 @@ int udp_listen(struct udp_sock **usp, const struct sa *local,
{
struct addrinfo hints, *res = NULL, *r;
struct udp_sock *us = NULL;
char addr[64];
char addr[64] = {0};
char serv[6] = "0";
int af, error, err = 0;

Expand All @@ -299,8 +298,8 @@ int udp_listen(struct udp_sock **usp, const struct sa *local,

list_init(&us->helpers);

us->fd = -1;
us->fd6 = -1;
us->fd = BAD_SOCK;
us->fd6 = BAD_SOCK;

if (local) {
af = sa_af(local);
Expand Down Expand Up @@ -336,17 +335,17 @@ int udp_listen(struct udp_sock **usp, const struct sa *local,
}

for (r = res; r; r = r->ai_next) {
RE_SOCKET fd;
re_sock_t fd;

if (us->fd > 0)
if (us->fd != BAD_SOCK)
continue;

DEBUG_INFO("listen: for: af=%d addr=%j\n",
r->ai_family, r->ai_addr);

fd = socket(r->ai_family, SOCK_DGRAM, IPPROTO_UDP);
if (fd == BAD_SOCKET) {
err = ERRNO_SOCKET;
if (fd == BAD_SOCK) {
err = ERRNO_SOCK;
continue;
}

Expand All @@ -358,7 +357,7 @@ int udp_listen(struct udp_sock **usp, const struct sa *local,
}

if (bind(fd, r->ai_addr, SIZ_CAST r->ai_addrlen) < 0) {
err = ERRNO_SOCKET;
err = ERRNO_SOCK;
DEBUG_INFO("listen: bind(): %m (%J)\n", err, local);
(void)close(fd);
continue;
Expand All @@ -380,7 +379,7 @@ int udp_listen(struct udp_sock **usp, const struct sa *local,
if (0==sa_set_sa(&sa, r->ai_addr) && sa_is_any(&sa)) {
on = 1;
}
DEBUG_INFO("socket %d: IPV6_V6ONLY is %d\n", fd, on);
DEBUG_INFO("socket %k: IPV6_V6ONLY is %d\n", fd, on);
if (on) {
us->fd6 = fd;
continue;
Expand All @@ -395,7 +394,7 @@ int udp_listen(struct udp_sock **usp, const struct sa *local,
freeaddrinfo(res);

/* We must have at least one socket */
if (-1 == us->fd && -1 == us->fd6) {
if (BAD_SOCK == us->fd && BAD_SOCK == us->fd6) {
if (0 == err)
err = EADDRNOTAVAIL;
goto out;
Expand Down Expand Up @@ -431,7 +430,7 @@ int udp_open(struct udp_sock **usp, int af)
{
struct udp_sock *us = NULL;
int err = 0;
RE_SOCKET fd;
re_sock_t fd;

if (!usp)
return EINVAL;
Expand All @@ -440,12 +439,12 @@ int udp_open(struct udp_sock **usp, int af)
if (!us)
return ENOMEM;

us->fd = -1;
us->fd6 = -1;
us->fd = BAD_SOCK;
us->fd6 = BAD_SOCK;

fd = socket(af, SOCK_DGRAM, IPPROTO_UDP);
if (fd == BAD_SOCKET) {
err = ERRNO_SOCKET;
if (fd == BAD_SOCK) {
err = ERRNO_SOCK;
goto out;
}

Expand Down Expand Up @@ -475,19 +474,19 @@ int udp_open(struct udp_sock **usp, int af)
*/
int udp_connect(struct udp_sock *us, const struct sa *peer)
{
int fd;
re_sock_t fd;

if (!us || !peer)
return EINVAL;

/* choose a socket */
if (AF_INET6 == sa_af(peer) && -1 != us->fd6)
if (AF_INET6 == sa_af(peer) && BAD_SOCK != us->fd6)
fd = us->fd6;
else
fd = us->fd;

if (0 != connect(fd, &peer->u.sa, peer->len))
return errno;
return ERRNO_SOCK;

us->conn = true;

Expand All @@ -499,10 +498,11 @@ static int udp_send_internal(struct udp_sock *us, const struct sa *dst,
struct mbuf *mb, struct le *le)
{
struct sa hdst;
int err = 0, fd;
int err = 0;
re_sock_t fd;

/* choose a socket */
if (AF_INET6 == sa_af(dst) && -1 != us->fd6)
if (AF_INET6 == sa_af(dst) && BAD_SOCK != us->fd6)
fd = us->fd6;
else
fd = us->fd;
Expand Down Expand Up @@ -608,7 +608,7 @@ int udp_local_get(const struct udp_sock *us, struct sa *local)
if (0 == getsockname(us->fd6, &local->u.sa, &local->len))
return 0;

return errno;
return ERRNO_SOCK;
}


Expand All @@ -631,13 +631,13 @@ int udp_setsockopt(struct udp_sock *us, int level, int optname,
if (!us)
return EINVAL;

if (-1 != us->fd) {
if (BAD_SOCK != us->fd) {
if (0 != setsockopt(us->fd, level, optname,
BUF_CAST optval, optlen))
err |= errno;
}

if (-1 != us->fd6) {
if (BAD_SOCK != us->fd6) {
if (0 != setsockopt(us->fd6, level, optname,
BUF_CAST optval, optlen))
err |= errno;
Expand Down Expand Up @@ -696,7 +696,7 @@ int udp_settos(struct udp_sock *us, uint8_t tos)
return GetLastError();

us->qos_id = 0;
if (-1 != us->fd) {
if (BAD_SOCK != us->fd) {
err = QOSAddSocketToFlow(us->qos, us->fd, NULL,
qos_type,
QOS_NON_ADAPTIVE_FLOW,
Expand All @@ -706,7 +706,7 @@ int udp_settos(struct udp_sock *us, uint8_t tos)
}

us->qos_id6 = 0;
if (-1 != us->fd6) {
if (BAD_SOCK != us->fd6) {
err = QOSAddSocketToFlow(us->qos, us->fd6, NULL,
qos_type,
QOS_NON_ADAPTIVE_FLOW,
Expand Down Expand Up @@ -788,18 +788,18 @@ void udp_error_handler_set(struct udp_sock *us, udp_error_h *eh)
* @param us UDP Socket
* @param af Address Family
*
* @return File Descriptor, or -1 for errors
* @return File Descriptor, or BAD_SOCK for errors
*/
int udp_sock_fd(const struct udp_sock *us, int af)
re_sock_t udp_sock_fd(const struct udp_sock *us, int af)
{
if (!us)
return -1;
return BAD_SOCK;

switch (af) {

default:
case AF_INET: return us->fd;
case AF_INET6: return (us->fd6 != -1) ? us->fd6 : us->fd;
case AF_INET6: return (us->fd6 != BAD_SOCK) ? us->fd6 : us->fd;
}
}

Expand All @@ -818,13 +818,13 @@ int udp_thread_attach(struct udp_sock *us)
if (!us)
return EINVAL;

if (-1 != us->fd) {
if (BAD_SOCK != us->fd) {
err = fd_listen(us->fd, FD_READ, udp_read_handler, us);
if (err)
goto out;
}

if (-1 != us->fd6) {
if (BAD_SOCK != us->fd6) {
err = fd_listen(us->fd6, FD_READ, udp_read_handler6, us);
if (err)
goto out;
Expand All @@ -848,10 +848,10 @@ void udp_thread_detach(struct udp_sock *us)
if (!us)
return;

if (-1 != us->fd)
if (BAD_SOCK != us->fd)
fd_close(us->fd);

if (-1 != us->fd6)
if (BAD_SOCK != us->fd6)
fd_close(us->fd6);
}

Expand Down Expand Up @@ -1003,15 +1003,15 @@ void udp_flush(const struct udp_sock *us)
if (!us)
return;

if (-1 != us->fd) {
if (BAD_SOCK != us->fd) {
uint8_t buf[4096];

while (recvfrom(us->fd, BUF_CAST buf, sizeof(buf),
0, NULL, 0) > 0)
;
}

if (-1 != us->fd6) {
if (BAD_SOCK != us->fd6) {
uint8_t buf[4096];

while (recvfrom(us->fd6, BUF_CAST buf, sizeof(buf),
Expand Down

0 comments on commit b42df7d

Please sign in to comment.