Skip to content

Commit 33a0d77

Browse files
committed
CDRIVER-2015 getpeername and getsockopt params, part 2
1 parent 287d036 commit 33a0d77

File tree

5 files changed

+22
-59
lines changed

5 files changed

+22
-59
lines changed

CMakeLists.txt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ if (MINGW)
166166
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_RAND_S")
167167
endif ()
168168

169+
# Reasonable default. CheckCompiler.m4 checks thoroughly for HP-UX's sake.
170+
set (MONGOC_SOCKET_ARG2 "struct sockaddr")
171+
169172
include(CheckTypeSize)
170173
if (MSVC)
171174
SET(CMAKE_EXTRA_INCLUDE_FILES "ws2tcpip.h")
@@ -177,19 +180,12 @@ SET(CMAKE_EXTRA_INCLUDE_FILES)
177180

178181
if (HAVE_SOCKLEN)
179182
set(MONGOC_HAVE_SOCKLEN 1)
183+
set (MONGOC_SOCKET_ARG3 "socklen_t")
180184
else()
181185
set(MONGOC_HAVE_SOCKLEN 0)
186+
set (MONGOC_SOCKET_ARG3 "int")
182187
endif()
183188

184-
# Reasonable defaults. CheckCompiler.m4 checks thoroughly for HP-UX's sake.
185-
set (ACCEPT_ARG2 "struct sockaddr")
186-
set (ACCEPT_ARG3 "socklen_t")
187-
set (GETPEERNAME_ARG2 "struct sockaddr")
188-
set (GETPEERNAME_ARG3 "socklen_t")
189-
set (GETSOCKNAME_ARG2 "struct sockaddr")
190-
set (GETSOCKNAME_ARG3 "socklen_t")
191-
set (GETSOCKOPT_ARG5 "socklen_t")
192-
193189
set (SOURCE_DIR "${PROJECT_SOURCE_DIR}/")
194190

195191
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/build/cmake)

build/autotools/CheckCompiler.m4

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
4343
#endif
4444
])], [c_compiler="sun"], [])
4545

46+
# The type of parameters for accept, getpeername, getsockname, getsockopt
47+
# all vary the same way by platform.
4648
AX_PROTOTYPE(accept, [
4749
#include <sys/types.h>
4850
#include <sys/socket.h>
@@ -54,39 +56,10 @@ AX_PROTOTYPE(accept, [
5456
ARG2, [struct sockaddr, void],
5557
ARG3, [socklen_t, size_t, int])
5658

57-
AX_PROTOTYPE(getpeername, [
58-
#include <sys/types.h>
59-
#include <sys/socket.h>
60-
], [
61-
int a = 0;
62-
ARG2 *b = 0;
63-
ARG3 *c = 0;
64-
getpeername (a, b, c);],
65-
ARG2, [struct sockaddr, void],
66-
ARG3, [socklen_t, size_t, int])
67-
68-
AX_PROTOTYPE(getsockname, [
69-
#include <sys/types.h>
70-
#include <sys/socket.h>
71-
], [
72-
int a = 0;
73-
ARG2 *b = 0;
74-
ARG3 *c = 0;
75-
getsockname (a, b, c);],
76-
ARG2, [struct sockaddr, void],
77-
ARG3, [socklen_t, size_t, int])
78-
79-
AX_PROTOTYPE(getsockopt, [
80-
#include <sys/types.h>
81-
#include <sys/socket.h>
82-
], [
83-
int a = 0;
84-
int b = 0;
85-
int c = 0;
86-
void *d = 0;
87-
ARG5 *e = 0;
88-
getsockopt (a, b, c, d, e);],
89-
ARG5, [socklen_t, int])
59+
MONGOC_SOCKET_ARG2="$ACCEPT_ARG2"
60+
AC_SUBST(MONGOC_SOCKET_ARG2)
61+
MONGOC_SOCKET_ARG3="$ACCEPT_ARG3"
62+
AC_SUBST(MONGOC_SOCKET_ARG3)
9063

9164
AC_LANG_POP([C])
9265

src/mongoc/mongoc-config.h.in

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,8 @@
219219
* Set from configure, see
220220
* https://curl.haxx.se/mail/lib-2009-04/0287.html
221221
*/
222-
#define MONGOC_ACCEPT_ARG2 @ACCEPT_ARG2@
223-
#define MONGOC_ACCEPT_ARG3 @ACCEPT_ARG3@
224-
#define MONGOC_GETPEERNAME_ARG2 @GETPEERNAME_ARG2@
225-
#define MONGOC_GETPEERNAME_ARG3 @GETPEERNAME_ARG3@
226-
#define MONGOC_GETSOCKNAME_ARG2 @GETSOCKNAME_ARG2@
227-
#define MONGOC_GETSOCKNAME_ARG3 @GETSOCKNAME_ARG3@
228-
#define MONGOC_GETSOCKOPT_ARG5 @GETSOCKOPT_ARG5@
222+
#define MONGOC_SOCKET_ARG2 @MONGOC_SOCKET_ARG2@
223+
#define MONGOC_SOCKET_ARG3 @MONGOC_SOCKET_ARG3@
229224

230225
/*
231226
* NOTICE:

src/mongoc/mongoc-socket.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
((expire_at >= 0) && (expire_at < (bson_get_monotonic_time ())))
3434

3535

36+
/* either struct sockaddr or void, depending on platform */
37+
typedef MONGOC_SOCKET_ARG2 mongoc_sockaddr_t;
38+
3639
/*
3740
*--------------------------------------------------------------------------
3841
*
@@ -427,7 +430,7 @@ mongoc_socket_accept_ex (mongoc_socket_t *sock, /* IN */
427430

428431
again:
429432
errno = 0;
430-
sd = accept (sock->sd, (struct sockaddr *) &addr, &addrlen);
433+
sd = accept (sock->sd, (mongoc_sockaddr_t *) &addr, &addrlen);
431434

432435
_mongoc_socket_capture_errno (sock);
433436
#ifdef _WIN32
@@ -586,7 +589,7 @@ mongoc_socket_connect (mongoc_socket_t *sock, /* IN */
586589
int ret;
587590
int optval;
588591
/* getsockopt parameter types vary, we check in CheckCompiler.m4 */
589-
MONGOC_GETSOCKOPT_ARG5 optlen = (MONGOC_GETSOCKOPT_ARG5) sizeof optval;
592+
mongoc_socklen_t optlen = (mongoc_socklen_t) sizeof optval;
590593

591594
ENTRY;
592595

@@ -1186,7 +1189,7 @@ mongoc_socket_getsockname (mongoc_socket_t *sock, /* IN */
11861189

11871190
BSON_ASSERT (sock);
11881191

1189-
ret = getsockname (sock->sd, addr, addrlen);
1192+
ret = getsockname (sock->sd, (mongoc_sockaddr_t *) addr, addrlen);
11901193

11911194
_mongoc_socket_capture_errno (sock);
11921195

@@ -1198,8 +1201,8 @@ char *
11981201
mongoc_socket_getnameinfo (mongoc_socket_t *sock) /* IN */
11991202
{
12001203
/* getpeername parameter types vary, we check in CheckCompiler.m4 */
1201-
MONGOC_GETPEERNAME_ARG2 addr;
1202-
MONGOC_GETPEERNAME_ARG3 len = (MONGOC_GETPEERNAME_ARG3) sizeof addr;
1204+
mongoc_sockaddr_t addr;
1205+
mongoc_socklen_t len = (mongoc_socklen_t) sizeof addr;
12031206
char *ret;
12041207
char host[BSON_HOST_NAME_MAX + 1];
12051208

src/mongoc/mongoc-socket.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@
4545
BSON_BEGIN_DECLS
4646

4747

48-
#ifdef MONGOC_HAVE_SOCKLEN
49-
typedef socklen_t mongoc_socklen_t;
50-
#else
51-
typedef int mongoc_socklen_t;
52-
#endif
48+
typedef MONGOC_SOCKET_ARG3 mongoc_socklen_t;
5349

5450
typedef struct _mongoc_socket_t mongoc_socket_t;
5551

0 commit comments

Comments
 (0)