Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Windows: validate UDP socket options
Browse files Browse the repository at this point in the history
Makes test-udp-options pass on Windows.
  • Loading branch information
piscisaureus committed Apr 12, 2012
1 parent a41016e commit f3e3e5b
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/win/udp.c
Expand Up @@ -647,10 +647,15 @@ int uv_udp_set_broadcast(uv_udp_t* handle, int value) {
}


#define SOCKOPT_SETTER(name, option4, option6) \
#define SOCKOPT_SETTER(name, option4, option6, validate) \
int uv_udp_set_##name(uv_udp_t* handle, int value) { \
DWORD optval = (DWORD) value; \
\
if (!(validate(value))) { \
uv__set_artificial_error(handle->loop, UV_EINVAL); \
return -1; \
} \
\
/* If the socket is unbound, bind to inaddr_any. */ \
if (!(handle->flags & UV_HANDLE_BOUND) && \
uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) { \
Expand Down Expand Up @@ -681,8 +686,24 @@ int uv_udp_set_broadcast(uv_udp_t* handle, int value) {
return 0; \
}

SOCKOPT_SETTER(multicast_loop, IP_MULTICAST_LOOP, IPV6_MULTICAST_LOOP)
SOCKOPT_SETTER(multicast_ttl, IP_MULTICAST_TTL, IPV6_MULTICAST_HOPS)
SOCKOPT_SETTER(ttl, IP_TTL, IPV6_HOPLIMIT)
#define VALIDATE_TTL(value) ((value) >= 1 && (value) <= 255)
#define VALIDATE_MULTICAST_TTL(value) ((value) >= -1 && (value) <= 255)
#define VALIDATE_MULTICAST_LOOP(value) (1)

SOCKOPT_SETTER(ttl,
IP_TTL,
IPV6_HOPLIMIT,
VALIDATE_TTL)
SOCKOPT_SETTER(multicast_ttl,
IP_MULTICAST_TTL,
IPV6_MULTICAST_HOPS,
VALIDATE_MULTICAST_TTL)
SOCKOPT_SETTER(multicast_loop,
IP_MULTICAST_LOOP,
IPV6_MULTICAST_LOOP,
VALIDATE_MULTICAST_LOOP)

#undef SOCKOPT_SETTER
#undef VALIDATE_TTL
#undef VALIDATE_MULTICAST_TTL
#undef VALIDATE_MULTICAST_LOOP

1 comment on commit f3e3e5b

@igorzi
Copy link

@igorzi igorzi commented on f3e3e5b Apr 12, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Please sign in to comment.