Skip to content

Commit

Permalink
Replace ioctl with fcntl, fix ioctlsocket calls (64-bit fix)
Browse files Browse the repository at this point in the history
`ioctl` and `ioctlsocket` take two different types of arguments but worked in the past due to
long being 32-bit. However, ioctl functionality is non-standard and should not be used in
code written after **1997** in order to make sockets non-blocking. This functionality was
standardized as part of fcntl.
  • Loading branch information
GravisZro committed May 18, 2024
1 parent fd3f171 commit aaf52fd
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 34 deletions.
22 changes: 12 additions & 10 deletions Descent3/dedicated_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#include <stdlib.h>

#ifndef __LINUX__
#include <fcntl.h>
typedef int socklen_t;
#endif

Expand Down Expand Up @@ -859,12 +860,12 @@ void InitDedicatedSocket(ushort port) {
}
// Make the socket non-blocking

#if defined(WIN32)
u_long argp = 1;
ioctlsocket(dedicated_listen_socket, FIONBIO, &argp);
#ifdef WIN32
unsigned long arg = 1;
ioctlsocket(dedicated_listen_socket, FIONBIO, &arg);
#elif defined(__LINUX__)
int argp = 1;
ioctl(dedicated_listen_socket, FIONBIO, &argp);
fcntl(dedicated_listen_socket, F_SETFL,
fcntl(dedicated_listen_socket, F_GETFL, 0) | O_NONBLOCK);
#endif
}

Expand All @@ -877,13 +878,14 @@ void ListenDedicatedSocket(void) {
if (INVALID_SOCKET != incoming_socket) {
// Make the socket non-blocking

#if defined(WIN32)
u_long argp = 1;
ioctlsocket(incoming_socket, FIONBIO, &argp);
#ifdef WIN32
unsigned long arg = 1;
ioctlsocket(incoming_socket, FIONBIO, &arg);
#elif defined(__LINUX__)
int argp = 1;
ioctl(incoming_socket, FIONBIO, &argp);
fcntl(incoming_socket, F_SETFL,
fcntl(incoming_socket, F_GETFL, 0) | O_NONBLOCK);
#endif

if (!Dedicated_allow_remote) {
// Check to see if this came in from the local address
unsigned long localhost = inet_addr("127.0.0.1");
Expand Down
1 change: 0 additions & 1 deletion ddebug/lnxmono.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>

#define MAX_TCPLOG_LEN 2000
Expand Down
13 changes: 7 additions & 6 deletions legacy/inetfile/Chttpget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
#endif

#ifdef __LINUX__
#include <fcntl.h>

inline void Sleep(int millis)
{
Expand Down Expand Up @@ -201,14 +202,14 @@ void ChttpGet::PrepSocket(char *URL)
{
m_State = HTTP_STATE_SOCKET_ERROR;
return;
}
unsigned long arg;
}

arg = true;
#if defined(WIN32)
ioctlsocket( m_DataSock, FIONBIO, &arg );
#ifdef WIN32
unsigned long arg = 1;
ioctlsocket(m_DataSock, FIONBIO, &arg);
#elif defined(__LINUX__)
ioctl( m_DataSock, FIONBIO, &arg );
fcntl(m_DataSock, F_SETFL,
fcntl(m_DataSock, F_GETFL, 0) | O_NONBLOCK);
#endif

char *pURL = URL;
Expand Down
10 changes: 6 additions & 4 deletions legacy/mtclient/chat_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "networking.h"
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#endif

#include "chat_api.h"
Expand Down Expand Up @@ -133,8 +134,7 @@ int ConnectToChatServer(char *serveraddr,char *nickname,char *trackerid)
{
short chat_port;
char chat_server[50];
char *p;
unsigned long argp = 1;
char *p;
char signon_str[100];

//if(Socket_connected && ) return -2;
Expand Down Expand Up @@ -187,9 +187,11 @@ int ConnectToChatServer(char *serveraddr,char *nickname,char *trackerid)
}

#ifdef WIN32
ioctlsocket( Chatsock, FIONBIO, &argp );
unsigned long arg = 1;
ioctlsocket(Chatsock, FIONBIO, &arg);
#elif defined(__LINUX__)
ioctl(Chatsock,FIONBIO,&argp);
fcntl(Chatsock, F_SETFL,
fcntl(Chatsock, F_GETFL, 0) | O_NONBLOCK);
#endif


Expand Down
1 change: 0 additions & 1 deletion lib/networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ static inline void INADDR_GET_SUN_SUNB(struct in_addr *st, unsigned char *s_b1,
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <errno.h>
#include <pthread.h>
Expand Down
1 change: 0 additions & 1 deletion netcon/includes/inetgetfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <sys/time.h>
#include <unistd.h>
Expand Down
14 changes: 7 additions & 7 deletions netcon/inetfile/Chttpget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
#endif

#ifdef __LINUX__

#include <fcntl.h>
#include "SDL_thread.h"

inline void Sleep(int millis) {
Expand Down Expand Up @@ -210,13 +210,13 @@ void ChttpGet::PrepSocket(char *URL) {
m_State = HTTP_STATE_SOCKET_ERROR;
return;
}
unsigned int arg = 1;


#ifdef WIN32
u_long argWin = static_cast<u_long>(arg);
ioctlsocket(m_DataSock, FIONBIO, &argWin);
#elif defined(__linux__)
ioctl(m_DataSock, FIONBIO, &arg);
unsigned long arg = 1;
ioctlsocket(m_DataSock, FIONBIO, &arg);
#elif defined(__LINUX__)
fcntl(m_DataSock, F_SETFL,
fcntl(m_DataSock, F_GETFL, 0) | O_NONBLOCK);
#endif

char *pURL = URL;
Expand Down
7 changes: 3 additions & 4 deletions networking/networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,12 @@ void nw_SetSocketOptions(SOCKET sock) {
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (LPSTR)&broadcast, sizeof(broadcast));

int error;
unsigned long arg;

arg = TRUE;
#ifdef WIN32
unsigned long arg = 1;
error = ioctlsocket(sock, FIONBIO, &arg);
#elif defined(__LINUX__)
error = ioctl(sock, FIONBIO, &arg);
fcntl(sock, F_SETFL,
fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
#endif
if (error == SOCKET_ERROR) {
mprintf((0, "Unable to make socket non-blocking -- %d", WSAGetLastError()));
Expand Down

0 comments on commit aaf52fd

Please sign in to comment.