From 50ae248b84defb4d399fa9794783fd51ab32f696 Mon Sep 17 00:00:00 2001 From: James R Date: Fri, 26 Apr 2019 12:59:21 -0700 Subject: [PATCH 1/4] Unfuck MS connecting and error reporting Reconnect if the socket is closed. Report the proper error from SO_ERROR and report an error from getsockopt. --- src/mserv.c | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/mserv.c b/src/mserv.c index f5c4fa889..eb1e6302e 100644 --- a/src/mserv.c +++ b/src/mserv.c @@ -661,11 +661,19 @@ FUNCMATH static const char *int2str(INT32 n) #ifndef NONET static INT32 ConnectionFailed(void) { + time(&MSLastPing); con_state = MSCS_FAILED; CONS_Alert(CONS_ERROR, M_GetText("Connection to Master Server failed\n")); CloseConnection(); return MS_CONNECT_ERROR; } + +static INT32 ConnectionFailedwerrno(int no) +{ + CONS_Alert(CONS_ERROR, M_GetText("Master Server socket error: %s\n"), + strerror(no)); + return ConnectionFailed(); +} #endif /** Tries to register the local game server on the master server. @@ -682,44 +690,41 @@ static INT32 AddToMasterServer(boolean firstadd) msg_server_t *info = (msg_server_t *)msg.buffer; INT32 room = -1; fd_set tset; - time_t timestamp = time(NULL); UINT32 signature, tmp; const char *insname; + if (socket_fd == ERRSOCKET)/* Woah, our socket was closed! */ + { + if (MS_Connect(GetMasterServerIP(), GetMasterServerPort(), 0)) + return ConnectionFailedwerrno(errno); + } + M_Memcpy(&tset, &wset, sizeof (tset)); res = select(255, NULL, &tset, NULL, &select_timeout); - if (res != ERRSOCKET && !res) + if (res == 0)/* nothing selected */ { - if (retry++ > 30) // an about 30 second timeout + /* + Timeout next call because SendPingToMasterServer + (our calling function) already calls this once + every two minutes. + */ + if (retry++ == 1) { retry = 0; CONS_Alert(CONS_ERROR, M_GetText("Master Server timed out\n")); - MSLastPing = timestamp; return ConnectionFailed(); } return MS_CONNECT_ERROR; } retry = 0; - if (res == ERRSOCKET) - { - if (MS_Connect(GetMasterServerIP(), GetMasterServerPort(), 0)) - { - CONS_Alert(CONS_ERROR, M_GetText("Master Server socket error #%u: %s\n"), errno, strerror(errno)); - MSLastPing = timestamp; - return ConnectionFailed(); - } - } // so, the socket is writable, but what does that mean, that the connection is // ok, or bad... let see that! j = (socklen_t)sizeof (i); - getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, (char *)&i, &j); + if (getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, (char *)&i, &j) == ERRSOCKET) + return ConnectionFailedwerrno(errno); if (i) // it was bad - { - CONS_Alert(CONS_ERROR, M_GetText("Master Server socket error #%u: %s\n"), errno, strerror(errno)); - MSLastPing = timestamp; - return ConnectionFailed(); - } + return ConnectionFailedwerrno(i); #ifdef PARANOIA if (ms_RoomId <= 0) @@ -752,15 +757,12 @@ static INT32 AddToMasterServer(boolean firstadd) msg.length = (UINT32)sizeof (msg_server_t); msg.room = 0; if (MS_Write(&msg) < 0) - { - MSLastPing = timestamp; return ConnectionFailed(); - } if(con_state != MSCS_REGISTERED) CONS_Printf(M_GetText("Master Server update successful.\n")); - MSLastPing = timestamp; + time(&MSLastPing); con_state = MSCS_REGISTERED; CloseConnection(); #endif From 1d77716d7ebeea2bf279d690a6c0542ff155cb22 Mon Sep 17 00:00:00 2001 From: James R Date: Fri, 26 Apr 2019 13:06:26 -0700 Subject: [PATCH 2/4] Check error on select --- src/mserv.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mserv.c b/src/mserv.c index eb1e6302e..1b0b41be3 100644 --- a/src/mserv.c +++ b/src/mserv.c @@ -701,6 +701,8 @@ static INT32 AddToMasterServer(boolean firstadd) M_Memcpy(&tset, &wset, sizeof (tset)); res = select(255, NULL, &tset, NULL, &select_timeout); + if (res == -1) + return ConnectionFailedwerrno(errno); if (res == 0)/* nothing selected */ { /* From 9f0887fafca1388561c535714c60d84016f2f447 Mon Sep 17 00:00:00 2001 From: James R Date: Fri, 26 Apr 2019 13:08:35 -0700 Subject: [PATCH 3/4] Force of habit --- src/mserv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mserv.c b/src/mserv.c index 1b0b41be3..5d10aa3ca 100644 --- a/src/mserv.c +++ b/src/mserv.c @@ -701,7 +701,7 @@ static INT32 AddToMasterServer(boolean firstadd) M_Memcpy(&tset, &wset, sizeof (tset)); res = select(255, NULL, &tset, NULL, &select_timeout); - if (res == -1) + if (res == ERRSOCKET) return ConnectionFailedwerrno(errno); if (res == 0)/* nothing selected */ { From 70566c808886eb5d0d9406c452ca2b2be3862f32 Mon Sep 17 00:00:00 2001 From: James R Date: Tue, 30 Apr 2019 13:03:13 -0700 Subject: [PATCH 4/4] Fix differing signedness compare --- src/mserv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mserv.c b/src/mserv.c index 5d10aa3ca..29aa99fae 100644 --- a/src/mserv.c +++ b/src/mserv.c @@ -693,7 +693,7 @@ static INT32 AddToMasterServer(boolean firstadd) UINT32 signature, tmp; const char *insname; - if (socket_fd == ERRSOCKET)/* Woah, our socket was closed! */ + if (socket_fd == (SOCKET_TYPE)ERRSOCKET)/* Woah, our socket was closed! */ { if (MS_Connect(GetMasterServerIP(), GetMasterServerPort(), 0)) return ConnectionFailedwerrno(errno);