From 5465b8c85efda20a79d23db28fde7ee403aec8f3 Mon Sep 17 00:00:00 2001 From: Pavel Roskin Date: Wed, 31 Aug 2016 11:15:25 -0700 Subject: [PATCH] Add socklen_t check with substitution, use socklen_t as needed --- common/os_calls.c | 75 +++++++---------------------------------- configure.ac | 1 + m4/ax_type_socklen_t.m4 | 61 +++++++++++++++++++++++++++++++++ tests/gtcp_proxy/gtcp.c | 30 +++-------------- tests/tcp_proxy/main.c | 6 ++-- 5 files changed, 80 insertions(+), 93 deletions(-) create mode 100644 m4/ax_type_socklen_t.m4 diff --git a/common/os_calls.c b/common/os_calls.c index 4608f6b0..8a294971 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -332,13 +332,8 @@ int APP_CC g_tcp_set_no_delay(int sck) { int ret = 1; /* error */ -#if defined(_WIN32) - int option_value; - int option_len; -#else int option_value; - unsigned int option_len; -#endif + socklen_t option_len; option_len = sizeof(option_value); @@ -376,13 +371,8 @@ int APP_CC g_tcp_set_keepalive(int sck) { int ret = 1; /* error */ -#if defined(_WIN32) int option_value; - int option_len; -#else - int option_value; - unsigned int option_len; -#endif + socklen_t option_len; option_len = sizeof(option_value); @@ -422,11 +412,7 @@ g_tcp_socket(void) { int rv; int option_value; -#if defined(_WIN32) - int option_len; -#else - unsigned int option_len; -#endif + socklen_t option_len; #if defined(XRDP_ENABLE_IPV6) rv = (int)socket(AF_INET6, SOCK_STREAM, 0); @@ -500,11 +486,7 @@ int APP_CC g_sck_set_send_buffer_bytes(int sck, int bytes) { int option_value; -#if defined(_WIN32) - int option_len; -#else - unsigned int option_len; -#endif + socklen_t option_len; option_value = bytes; option_len = sizeof(option_value); @@ -522,11 +504,7 @@ int APP_CC g_sck_get_send_buffer_bytes(int sck, int *bytes) { int option_value; -#if defined(_WIN32) - int option_len; -#else - unsigned int option_len; -#endif + socklen_t option_len; option_value = 0; option_len = sizeof(option_value); @@ -545,11 +523,7 @@ int APP_CC g_sck_set_recv_buffer_bytes(int sck, int bytes) { int option_value; -#if defined(_WIN32) - int option_len; -#else - unsigned int option_len; -#endif + socklen_t option_len; option_value = bytes; option_len = sizeof(option_value); @@ -567,11 +541,7 @@ int APP_CC g_sck_get_recv_buffer_bytes(int sck, int *bytes) { int option_value; -#if defined(_WIN32) - int option_len; -#else - unsigned int option_len; -#endif + socklen_t option_len; option_value = 0; option_len = sizeof(option_value); @@ -601,11 +571,7 @@ int APP_CC g_sck_get_peer_cred(int sck, int *pid, int *uid, int *gid) { #if defined(SO_PEERCRED) -#if defined(_WIN32) - int ucred_length; -#else - unsigned int ucred_length; -#endif + socklen_t ucred_length; struct myucred { pid_t pid; @@ -1056,11 +1022,7 @@ g_tcp_accept(int sck) int ret ; char ipAddr[256] ; struct sockaddr_in s; -#if defined(_WIN32) - signed int i; -#else - unsigned int i; -#endif + socklen_t i; i = sizeof(struct sockaddr_in); memset(&s, 0, i); @@ -1081,11 +1043,7 @@ g_sck_accept(int sck, char *addr, int addr_bytes, char *port, int port_bytes) int ret; char ipAddr[256]; struct sockaddr_in s; -#if defined(_WIN32) - signed int i; -#else - unsigned int i; -#endif + socklen_t i; i = sizeof(struct sockaddr_in); memset(&s, 0, i); @@ -1115,11 +1073,7 @@ g_write_ip_address(int rcv_sck, char *ip_address, int bytes) { struct sockaddr_in s; struct in_addr in; -#if defined(_WIN32) - int len; -#else - unsigned int len; -#endif + socklen_t len; int ip_port; int ok; @@ -1196,13 +1150,8 @@ g_sck_send(int sck, const void *ptr, int len, int flags) int APP_CC g_sck_socket_ok(int sck) { -#if defined(_WIN32) int opt; - int opt_len; -#else - int opt; - unsigned int opt_len; -#endif + socklen_t opt_len; opt_len = sizeof(opt); diff --git a/configure.ac b/configure.ac index d2a7938d..27719990 100644 --- a/configure.ac +++ b/configure.ac @@ -16,6 +16,7 @@ m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) AX_CFLAGS_WARN_ALL AX_APPEND_COMPILE_FLAGS([-Wwrite-strings]) AX_GCC_FUNC_ATTRIBUTE([format]) +AX_TYPE_SOCKLEN_T case $host_os in *linux*) diff --git a/m4/ax_type_socklen_t.m4 b/m4/ax_type_socklen_t.m4 new file mode 100644 index 00000000..834c4cfa --- /dev/null +++ b/m4/ax_type_socklen_t.m4 @@ -0,0 +1,61 @@ +# =========================================================================== +# http://www.gnu.org/software/autoconf-archive/ax_type_socklen_t.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_TYPE_SOCKLEN_T +# +# DESCRIPTION +# +# Check whether sys/socket.h defines type socklen_t. Please note that some +# systems require sys/types.h to be included before sys/socket.h can be +# compiled. +# +# LICENSE +# +# Copyright (c) 2008 Lars Brinkhoff +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +# +# As a special exception, the respective Autoconf Macro's copyright owner +# gives unlimited permission to copy, distribute and modify the configure +# scripts that are the output of Autoconf when processing the Macro. You +# need not follow the terms of the GNU General Public License when using +# or distributing such scripts, even though portions of the text of the +# Macro appear in them. The GNU General Public License (GPL) does govern +# all other use of the material that constitutes the Autoconf Macro. +# +# This special exception to the GPL applies to versions of the Autoconf +# Macro released by the Autoconf Archive. When you make and distribute a +# modified version of the Autoconf Macro, you may extend this special +# exception to the GPL to apply to your modified version as well. + +#serial 5 + +AU_ALIAS([TYPE_SOCKLEN_T], [AX_TYPE_SOCKLEN_T]) +AC_DEFUN([AX_TYPE_SOCKLEN_T], +[AC_CACHE_CHECK([for socklen_t], ac_cv_ax_type_socklen_t, +[ + AC_TRY_COMPILE( + [#include + #include ], + [socklen_t len = 42; return 0;], + ac_cv_ax_type_socklen_t=yes, + ac_cv_ax_type_socklen_t=no) +]) + if test $ac_cv_ax_type_socklen_t != yes; then + AC_DEFINE(socklen_t, int, [Substitute for socklen_t]) + fi +]) diff --git a/tests/gtcp_proxy/gtcp.c b/tests/gtcp_proxy/gtcp.c index 504138bf..a27f2ea6 100644 --- a/tests/gtcp_proxy/gtcp.c +++ b/tests/gtcp_proxy/gtcp.c @@ -26,12 +26,7 @@ int tcp_socket_create(void) { int rv; int option_value; - -#if defined(_WIN32) - int option_len; -#else - unsigned int option_len; -#endif + socklen_t option_len; /* in win32 a socket is an unsigned int, in linux, it's an int */ if ((rv = (int) socket(PF_INET, SOCK_STREAM, 0)) < 0) @@ -133,12 +128,7 @@ int tcp_accept(int skt) int ret ; char ipAddr[256] ; struct sockaddr_in s; - -#if defined(_WIN32) - int i; -#else - unsigned int i; -#endif + socklen_t i; i = sizeof(struct sockaddr_in); memset(&s, 0, i); @@ -186,12 +176,7 @@ int tcp_socket(void) { int rv; int option_value; - -#if defined(_WIN32) - int option_len; -#else - unsigned int option_len; -#endif + socklen_t option_len; /* in win32 a socket is an unsigned int, in linux, it's an int */ if ((rv = (int) socket(PF_INET, SOCK_STREAM, 0)) < 0) @@ -305,14 +290,7 @@ int tcp_can_send(int skt, int millis) int tcp_socket_ok(int skt) { int opt; - -#if defined(_WIN32) - int opt_len; -#else - unsigned int opt_len; -#endif - - opt_len = sizeof(opt); + socklen_t opt_len = sizeof(opt); if (getsockopt(skt, SOL_SOCKET, SO_ERROR, (char *) (&opt), &opt_len) == 0) { diff --git a/tests/tcp_proxy/main.c b/tests/tcp_proxy/main.c index 3a62c98c..5ea53f5f 100644 --- a/tests/tcp_proxy/main.c +++ b/tests/tcp_proxy/main.c @@ -121,7 +121,7 @@ g_tcp_socket(void) { int rv; int option_value; - unsigned int option_len; + socklen_t option_len; rv = (int)socket(AF_INET, SOCK_STREAM, 0); if (rv < 0) @@ -336,9 +336,7 @@ static int APP_CC g_tcp_socket_ok(int sck) { int opt; - unsigned int opt_len; - - opt_len = sizeof(opt); + socklen_t opt_len = sizeof(opt); if (getsockopt(sck, SOL_SOCKET, SO_ERROR, (char *)(&opt), &opt_len) == 0) {