Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use struct sockaddr_storage and socklen_t rather than ad-hoc types #106

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ Working version
- #13504, #13625: Add `Thread.set_current_thread_name`.
(Romain Beauxis, review by Gabriel Scherer and Antonin Décimo)

- #13???: Deprecate union sock_addr_union for struct sockaddr_storage
and socklen_param_type for socklen_t.
(Antonin Décimo, review by ?)

### Tools:

- #12019: ocamlc: add `align_double` and `align_int64` to `ocamlc -config`
Expand Down
8 changes: 6 additions & 2 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2044,10 +2044,8 @@ AS_IF([$sockets], [AC_DEFINE([HAS_SOCKETS], [1])])

AS_CASE([$target],
[*-*-mingw32*|*-pc-windows],
[AC_CHECK_TYPE([socklen_t], [AC_DEFINE([HAS_SOCKLEN_T], [1])], [],
[#include <ws2tcpip.h>])],
[AC_CHECK_TYPE([socklen_t], [AC_DEFINE([HAS_SOCKLEN_T], [1])], [],
[#include <sys/socket.h>])])
[AC_CHECK_TYPES([socklen_t], [], [], [#include <ws2tcpip.h>])],
[AC_CHECK_TYPES([socklen_t], [], [], [#include <sys/socket.h>])])

AC_CHECK_FUNC([inet_aton], [AC_DEFINE([HAS_INET_ATON], [1])])

Expand Down
8 changes: 4 additions & 4 deletions otherlibs/unix/accept_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ CAMLprim value caml_unix_accept(value cloexec, value sock)
CAMLlocal1(a);
int retcode;
value res;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;
int clo = caml_unix_cloexec_p(cloexec);

addr_len = sizeof(addr);
caml_enter_blocking_section();
#if defined(HAS_ACCEPT4) && defined(SOCK_CLOEXEC)
retcode = accept4(Int_val(sock), &addr.s_gen, &addr_len,
retcode = accept4(Int_val(sock), (struct sockaddr *) &addr, &addr_len,
clo ? SOCK_CLOEXEC : 0);
#else
retcode = accept(Int_val(sock), &addr.s_gen, &addr_len);
retcode = accept(Int_val(sock), (struct sockaddr *) &addr, &addr_len);
#endif
caml_leave_blocking_section();
if (retcode == -1) caml_uerror("accept", Nothing);
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/accept_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ CAMLprim value caml_unix_accept(value cloexec, value sock)
SOCKET sconn = Socket_val(sock);
SOCKET snew;
value res;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;
DWORD err = 0;

addr_len = sizeof(addr);
caml_enter_blocking_section();
snew = accept(sconn, &addr.s_gen, &addr_len);
snew = accept(sconn, (struct sockaddr *) &addr, &addr_len);
if (snew == INVALID_SOCKET) err = WSAGetLastError ();
caml_leave_blocking_section();
if (snew == INVALID_SOCKET) {
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/bind_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
CAMLprim value caml_unix_bind(value socket, value address)
{
int ret;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;

caml_unix_get_sockaddr(address, &addr, &addr_len);
ret = bind(Int_val(socket), &addr.s_gen, addr_len);
ret = bind(Int_val(socket), (struct sockaddr *) &addr, addr_len);
if (ret == -1) caml_uerror("bind", Nothing);
return Val_unit;
}
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/bind_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
CAMLprim value caml_unix_bind(value socket, value address)
{
int ret;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;

caml_unix_get_sockaddr(address, &addr, &addr_len);
ret = bind(Socket_val(socket), &addr.s_gen, addr_len);
ret = bind(Socket_val(socket), (struct sockaddr *) &addr, addr_len);
if (ret == -1) {
caml_win32_maperr(WSAGetLastError());
caml_uerror("bind", Nothing);
Expand Down
36 changes: 27 additions & 9 deletions otherlibs/unix/caml/socketaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct sockaddr_un {
#include <arpa/inet.h>
#endif

/* Deprecated: use struct sockaddr_storage. */
union sock_addr_union {
struct sockaddr s_gen;
struct sockaddr_un s_unix;
Expand All @@ -54,11 +55,11 @@ union sock_addr_union {
#endif
};

#ifdef HAS_SOCKLEN_T
typedef socklen_t socklen_param_type;
#else
typedef int socklen_param_type;
#ifndef HAVE_SOCKLEN_T
typedef int socklen_t;
#endif
/* Deprecated: use socklen_t */
typedef socklen_t socklen_param_type;

#ifdef __cplusplus
extern "C" {
Expand All @@ -71,15 +72,32 @@ extern "C" {
#define alloc_inet_addr caml_unix_alloc_inet_addr
#endif /* CAML_BUILDING_UNIX */

extern void caml_unix_get_sockaddr (value mladdr,
union sock_addr_union * addr /*out*/,
socklen_param_type * addr_len /*out*/);
extern value caml_unix_alloc_sockaddr (union sock_addr_union * addr /*in*/,
socklen_param_type addr_len,
extern void caml_unix_get_sockaddr (value vaddr,
struct sockaddr_storage * addr /*out*/,
socklen_t * addr_len /*out*/);
extern value caml_unix_alloc_sockaddr (struct sockaddr_storage * addr /*in*/,
socklen_t addr_len,
int close_on_error);
extern value caml_unix_alloc_inet_addr (struct in_addr * inaddr);
#define GET_INET_ADDR(v) (*((struct in_addr *) (v)))

/* API compatibility between union sock_addr_union and
struct sockaddr_storage. */
#define caml_unix_get_sockaddr(vaddr, addr, addr_len) \
caml_unix_get_sockaddr( \
(vaddr), \
_Generic((addr), \
struct sockaddr_storage *: (addr), \
union sock_addr_union *: ((struct sockaddr_storage *)(addr))), \
(addr_len))
#define caml_unix_alloc_sockaddr(addr, addr_len, close_on_error) \
caml_unix_alloc_sockaddr( \
_Generic((addr), \
struct sockaddr_storage *: (addr), \
union sock_addr_union *: (struct sockaddr_storage *)(addr)), \
(addr_len), \
(close_on_error))

#ifdef HAS_IPV6
extern value caml_unix_alloc_inet6_addr (struct in6_addr * inaddr);
#define GET_INET6_ADDR(v) (*((struct in6_addr *) (v)))
Expand Down
2 changes: 1 addition & 1 deletion otherlibs/unix/channels_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static int caml_unix_check_stream_semantics(int fd)
#ifdef HAS_SOCKETS
case S_IFSOCK: {
int so_type;
socklen_param_type so_type_len = sizeof(so_type);
socklen_t so_type_len = sizeof(so_type);
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &so_type_len) == -1)
return errno;
switch (so_type) {
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/connect_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
CAMLprim value caml_unix_connect(value socket, value address)
{
int retcode;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;

caml_unix_get_sockaddr(address, &addr, &addr_len);
caml_enter_blocking_section();
retcode = connect(Int_val(socket), &addr.s_gen, addr_len);
retcode = connect(Int_val(socket), (struct sockaddr *) &addr, addr_len);
caml_leave_blocking_section();
if (retcode == -1) caml_uerror("connect", Nothing);
return Val_unit;
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/connect_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
CAMLprim value caml_unix_connect(value socket, value address)
{
SOCKET s = Socket_val(socket);
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;
DWORD err = 0;

caml_unix_get_sockaddr(address, &addr, &addr_len);
caml_enter_blocking_section();
if (connect(s, &addr.s_gen, addr_len) == -1)
if (connect(s, (struct sockaddr *) &addr, addr_len) == -1)
err = WSAGetLastError();
caml_leave_blocking_section();
if (err) {
Expand Down
12 changes: 6 additions & 6 deletions otherlibs/unix/getaddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
extern const int caml_unix_socket_domain_table[]; /* from socket.c */
extern const int caml_unix_socket_type_table[]; /* from socket.c */

static value convert_addrinfo(struct addrinfo * a)
static value convert_addrinfo(const struct addrinfo * a)
{
CAMLparam0();
CAMLlocal3(vres,vaddr,vcanonname);
union sock_addr_union sa;
socklen_param_type len;
struct sockaddr_storage addr;
socklen_t len;

len = a->ai_addrlen;
if (len > sizeof(sa)) len = sizeof(sa);
memcpy(&sa.s_gen, a->ai_addr, len);
vaddr = caml_unix_alloc_sockaddr(&sa, len, -1);
if (len > sizeof(addr)) len = sizeof(addr);
memcpy(&addr, a->ai_addr, len);
vaddr = caml_unix_alloc_sockaddr(&addr, len, -1);
vcanonname = caml_copy_string(a->ai_canonname == NULL ? "" : a->ai_canonname);
vres = caml_alloc_small(5, 0);
Field(vres, 0) =
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/getnameinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ CAMLprim value caml_unix_getnameinfo(value vaddr, value vopts)
{
CAMLparam0();
CAMLlocal3(vhost, vserv, vres);
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;
char host[4096];
char serv[1024];
int opts, retcode;
Expand All @@ -47,7 +47,7 @@ CAMLprim value caml_unix_getnameinfo(value vaddr, value vopts)
opts = caml_convert_flag_list(vopts, getnameinfo_flag_table);
caml_enter_blocking_section();
retcode =
getnameinfo((const struct sockaddr *) &addr.s_gen, addr_len,
getnameinfo((struct sockaddr *) &addr, addr_len,
host, sizeof(host), serv, sizeof(serv), opts);
caml_leave_blocking_section();
/* TODO: detailed error reporting? */
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/getpeername_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
CAMLprim value caml_unix_getpeername(value sock)
{
int retcode;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;

addr_len = sizeof(addr);
retcode = getpeername(Int_val(sock), &addr.s_gen, &addr_len);
retcode = getpeername(Int_val(sock), (struct sockaddr *) &addr, &addr_len);
if (retcode == -1) caml_uerror("getpeername", Nothing);
return caml_unix_alloc_sockaddr(&addr, addr_len, -1);
}
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/getpeername_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
CAMLprim value caml_unix_getpeername(value sock)
{
int retcode;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;

addr_len = sizeof(addr);
retcode = getpeername(Socket_val(sock), &addr.s_gen, &addr_len);
retcode = getpeername(Socket_val(sock), (struct sockaddr *) &addr, &addr_len);
if (retcode == -1) {
caml_win32_maperr(WSAGetLastError());
caml_uerror("getpeername", Nothing);
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/getsockname_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
CAMLprim value caml_unix_getsockname(value sock)
{
int retcode;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;

addr_len = sizeof(addr);
retcode = getsockname(Int_val(sock), &addr.s_gen, &addr_len);
retcode = getsockname(Int_val(sock), (struct sockaddr *) &addr, &addr_len);
if (retcode == -1) caml_uerror("getsockname", Nothing);
return caml_unix_alloc_sockaddr(&addr, addr_len, -1);
}
Expand Down
6 changes: 3 additions & 3 deletions otherlibs/unix/getsockname_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
CAMLprim value caml_unix_getsockname(value sock)
{
int retcode;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;

addr_len = sizeof(addr);
retcode = getsockname(Socket_val(sock), &addr.s_gen, &addr_len);
retcode = getsockname(Socket_val(sock), (struct sockaddr *) &addr, &addr_len);
if (retcode == -1) {
caml_win32_maperr(WSAGetLastError());
caml_uerror("getsockname", Nothing);
Expand Down
12 changes: 6 additions & 6 deletions otherlibs/unix/sendrecv_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ CAMLprim value caml_unix_recvfrom(value sock, value buff, value ofs, value len,
long numbytes;
char iobuf[UNIX_BUFFER_SIZE];
value res;
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;

cv_flags = caml_convert_flag_list(flags, msg_flag_table);
numbytes = Long_val(len);
if (numbytes > UNIX_BUFFER_SIZE) numbytes = UNIX_BUFFER_SIZE;
addr_len = sizeof(addr);
caml_enter_blocking_section();
ret = recvfrom(Int_val(sock), iobuf, (int) numbytes, cv_flags,
&addr.s_gen, &addr_len);
(struct sockaddr *) &addr, &addr_len);
caml_leave_blocking_section();
if (ret == -1) caml_uerror("recvfrom", Nothing);
memmove (&Byte(buff, Long_val(ofs)), iobuf, ret);
Expand Down Expand Up @@ -100,8 +100,8 @@ CAMLprim value caml_unix_sendto_native(value sock, value buff, value ofs,
int ret, cv_flags;
long numbytes;
char iobuf[UNIX_BUFFER_SIZE];
union sock_addr_union addr;
socklen_param_type addr_len;
struct sockaddr_storage addr;
socklen_t addr_len;

cv_flags = caml_convert_flag_list(flags, msg_flag_table);
caml_unix_get_sockaddr(dest, &addr, &addr_len);
Expand All @@ -110,7 +110,7 @@ CAMLprim value caml_unix_sendto_native(value sock, value buff, value ofs,
memmove (iobuf, &Byte(buff, Long_val(ofs)), numbytes);
caml_enter_blocking_section();
ret = sendto(Int_val(sock), iobuf, (int) numbytes, cv_flags,
&addr.s_gen, addr_len);
(struct sockaddr *) &addr, addr_len);
caml_leave_blocking_section();
if (ret == -1) caml_uerror("sendto", Nothing);
return Val_int(ret);
Expand Down
Loading
Loading