X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=netconn.c;h=66129781f8c69c305d018c264ce3ad9443f103e5;hb=7d08a8ba9d7925d79b903a087b97357968e011de;hp=2f50f4d1d14dcb3d46d7fe3c7411443123b398f2;hpb=9cc276c2e36a6fc6cfafd8a0fb6df85e2f644c07;p=xonotic%2Fdarkplaces.git diff --git a/netconn.c b/netconn.c index 2f50f4d1..66129781 100755 --- a/netconn.c +++ b/netconn.c @@ -432,180 +432,177 @@ int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnet return NetConn_Write(mysocket, string, (int)strlen(string), peeraddress); } -int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data) +int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol) { - unsigned int packetLen; - unsigned int dataLen; - unsigned int eom; - unsigned int *header; - -//#ifdef DEBUG - if (data->cursize == 0) + if (protocol == PROTOCOL_QUAKEWORLD) { - Con_Printf ("Datagram_SendMessage: zero length message\n"); - return -1; - } + int packetLen; + qboolean sendreliable; + + // note that it is ok to send empty messages to the qw server, + // otherwise it won't respond to us at all + + sendreliable = false; + // if the remote side dropped the last reliable message, resend it + if (conn->qw.incoming_acknowledged > conn->qw.last_reliable_sequence && conn->qw.incoming_reliable_acknowledged != conn->qw.reliable_sequence) + sendreliable = true; + // if the reliable transmit buffer is empty, copy the current message out + if (!conn->sendMessageLength && conn->message.cursize) + { + memcpy (conn->sendMessage, conn->message.data, conn->message.cursize); + conn->sendMessageLength = conn->message.cursize; + SZ_Clear(&conn->message); // clear the message buffer + conn->qw.reliable_sequence ^= 1; + sendreliable = true; + } + // outgoing unreliable packet number, and outgoing reliable packet number (0 or 1) + *((int *)(sendbuffer + 0)) = LittleLong((unsigned int)conn->qw.outgoing_sequence | ((unsigned int)sendreliable<<31)); + // last received unreliable packet number, and last received reliable packet number (0 or 1) + *((int *)(sendbuffer + 4)) = LittleLong((unsigned int)conn->qw.incoming_sequence | ((unsigned int)conn->qw.incoming_reliable_sequence<<31)); + packetLen = 8; + conn->qw.outgoing_sequence++; + // client sends qport in every packet + if (conn == cls.netcon) + { + *((short *)(sendbuffer + 8)) = LittleShort(cls.qw_qport); + packetLen += 2; + } + if (packetLen + (sendreliable ? conn->sendMessageLength : 0) > 1400) + { + Con_Printf ("NetConn_SendUnreliableMessage: reliable message too big %u\n", data->cursize); + return -1; + } + // add the reliable message if there is one + if (sendreliable) + { + memcpy(sendbuffer + packetLen, conn->sendMessage, conn->sendMessageLength); + packetLen += conn->sendMessageLength; + conn->qw.last_reliable_sequence = conn->qw.outgoing_sequence; + } + // add the unreliable message if possible + if (packetLen + data->cursize <= 1400) + { + memcpy(sendbuffer + packetLen, data->data, data->cursize); + packetLen += data->cursize; + } - if (data->cursize > (int)sizeof(conn->sendMessage)) - { - Con_Printf ("Datagram_SendMessage: message too big (%u > %u)\n", data->cursize, sizeof(conn->sendMessage)); - return -1; - } + NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress); - if (conn->canSend == false) - { - Con_Printf ("SendMessage: called with canSend == false\n"); - return -1; - } -//#endif + packetsSent++; + unreliableMessagesSent++; - memcpy(conn->sendMessage, data->data, data->cursize); - conn->sendMessageLength = data->cursize; + // delay later packets to obey rate limit + conn->qw.cleartime = max(conn->qw.cleartime, realtime) + packetLen * conn->qw.rate; - if (conn->sendMessageLength <= MAX_PACKETFRAGMENT) - { - dataLen = conn->sendMessageLength; - eom = NETFLAG_EOM; + return 0; } else { - dataLen = MAX_PACKETFRAGMENT; - eom = 0; - } - - packetLen = NET_HEADERSIZE + dataLen; - - header = (unsigned int *)sendbuffer; - header[0] = BigLong(packetLen | (NETFLAG_DATA | eom)); - header[1] = BigLong(conn->sendSequence); - memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen); + unsigned int packetLen; + unsigned int dataLen; + unsigned int eom; + unsigned int *header; - conn->sendSequence++; - conn->canSend = false; - - if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen) - return -1; - - conn->lastSendTime = realtime; - packetsSent++; - reliableMessagesSent++; - return 1; -} - -static void NetConn_SendMessageNext(netconn_t *conn) -{ - unsigned int packetLen; - unsigned int dataLen; - unsigned int eom; - unsigned int *header; - - if (conn->sendMessageLength && !conn->canSend && conn->sendNext) - { - if (conn->sendMessageLength <= MAX_PACKETFRAGMENT) + // if a reliable message fragment has been lost, send it again + if (conn->sendMessageLength && (realtime - conn->lastSendTime) > 1.0) { - dataLen = conn->sendMessageLength; - eom = NETFLAG_EOM; - } - else - { - dataLen = MAX_PACKETFRAGMENT; - eom = 0; - } - - packetLen = NET_HEADERSIZE + dataLen; - - header = (unsigned int *)sendbuffer; - header[0] = BigLong(packetLen | (NETFLAG_DATA | eom)); - header[1] = BigLong(conn->sendSequence); - memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen); + if (conn->sendMessageLength <= MAX_PACKETFRAGMENT) + { + dataLen = conn->sendMessageLength; + eom = NETFLAG_EOM; + } + else + { + dataLen = MAX_PACKETFRAGMENT; + eom = 0; + } - conn->sendSequence++; - conn->sendNext = false; + packetLen = NET_HEADERSIZE + dataLen; - if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen) - return; + header = (unsigned int *)sendbuffer; + header[0] = BigLong(packetLen | (NETFLAG_DATA | eom)); + header[1] = BigLong(conn->nq.sendSequence - 1); + memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen); - conn->lastSendTime = realtime; - packetsSent++; - } -} - -static void NetConn_ReSendMessage(netconn_t *conn) -{ - unsigned int packetLen; - unsigned int dataLen; - unsigned int eom; - unsigned int *header; - - if (conn->sendMessageLength && !conn->canSend && (realtime - conn->lastSendTime) > 1.0) - { - if (conn->sendMessageLength <= MAX_PACKETFRAGMENT) - { - dataLen = conn->sendMessageLength; - eom = NETFLAG_EOM; + if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen) + { + conn->lastSendTime = realtime; + packetsReSent++; + } } - else + + // if we have a new reliable message to send, do so + if (!conn->sendMessageLength && conn->message.cursize) { - dataLen = MAX_PACKETFRAGMENT; - eom = 0; - } + if (conn->message.cursize > (int)sizeof(conn->sendMessage)) + { + Con_Printf("NetConn_SendUnreliableMessage: reliable message too big (%u > %u)\n", conn->message.cursize, sizeof(conn->sendMessage)); + conn->message.overflowed = true; + return -1; + } - packetLen = NET_HEADERSIZE + dataLen; + if (developer_networking.integer && conn == cls.netcon) + { + Con_Print("client sending reliable message to server:\n"); + SZ_HexDumpToConsole(&conn->message); + } - header = (unsigned int *)sendbuffer; - header[0] = BigLong(packetLen | (NETFLAG_DATA | eom)); - header[1] = BigLong(conn->sendSequence - 1); - memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen); + memcpy(conn->sendMessage, conn->message.data, conn->message.cursize); + conn->sendMessageLength = conn->message.cursize; + SZ_Clear(&conn->message); - conn->sendNext = false; + if (conn->sendMessageLength <= MAX_PACKETFRAGMENT) + { + dataLen = conn->sendMessageLength; + eom = NETFLAG_EOM; + } + else + { + dataLen = MAX_PACKETFRAGMENT; + eom = 0; + } - if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen) - return; + packetLen = NET_HEADERSIZE + dataLen; - conn->lastSendTime = realtime; - packetsReSent++; - } -} + header = (unsigned int *)sendbuffer; + header[0] = BigLong(packetLen | (NETFLAG_DATA | eom)); + header[1] = BigLong(conn->nq.sendSequence); + memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen); -qboolean NetConn_CanSendMessage(netconn_t *conn) -{ - return conn->canSend; -} + conn->nq.sendSequence++; -int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data) -{ - int packetLen; - unsigned int *header; + NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress); - packetLen = NET_HEADERSIZE + data->cursize; + conn->lastSendTime = realtime; + packetsSent++; + reliableMessagesSent++; + } -//#ifdef DEBUG - if (data->cursize == 0) - { - Con_Printf ("Datagram_SendUnreliableMessage: zero length message\n"); - return -1; - } + // if we have an unreliable message to send, do so + if (data->cursize) + { + packetLen = NET_HEADERSIZE + data->cursize; - if (packetLen > (int)sizeof(sendbuffer)) - { - Con_Printf ("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize); - return -1; - } -//#endif + if (packetLen > (int)sizeof(sendbuffer)) + { + Con_Printf("NetConn_SendUnreliableMessage: message too big %u\n", data->cursize); + return -1; + } - header = (unsigned int *)sendbuffer; - header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE); - header[1] = BigLong(conn->unreliableSendSequence); - memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize); + header = (unsigned int *)sendbuffer; + header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE); + header[1] = BigLong(conn->nq.unreliableSendSequence); + memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize); - conn->unreliableSendSequence++; + conn->nq.unreliableSendSequence++; - if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen) - return -1; + NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress); - packetsSent++; - unreliableMessagesSent++; - return 1; + packetsSent++; + unreliableMessagesSent++; + } + return 0; + } } void NetConn_CloseClientPorts(void) @@ -691,12 +688,11 @@ void NetConn_OpenServerPort(const char *addressstring, int defaultport) } } -static void NetConn_UpdateServerStuff(void); void NetConn_OpenServerPorts(int opennetports) { int port; NetConn_CloseServerPorts(); - NetConn_UpdateServerStuff(); + NetConn_UpdateSockets(); port = bound(0, sv_netport.integer, 65535); if (port == 0) port = 26000; @@ -738,7 +734,6 @@ netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress) conn = (netconn_t *)Mem_Alloc(netconn_mempool, sizeof(*conn)); conn->mysocket = mysocket; conn->peeraddress = *peeraddress; - conn->canSend = true; conn->lastMessageTime = realtime; conn->message.data = conn->messagedata; conn->message.maxsize = sizeof(conn->messagedata); @@ -779,7 +774,7 @@ void NetConn_Close(netconn_t *conn) static int clientport = -1; static int clientport2 = -1; static int hostport = -1; -static void NetConn_UpdateServerStuff(void) +void NetConn_UpdateSockets(void) { if (cls.state != ca_dedicated) { @@ -806,15 +801,76 @@ static void NetConn_UpdateServerStuff(void) } } -int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length) +static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length, protocolversion_t protocol) { - unsigned int count; - unsigned int flags; - unsigned int sequence; - int qlength; + if (length < 8) + return 0; + + if (protocol == PROTOCOL_QUAKEWORLD) + { + int sequence, sequence_ack; + int reliable_ack, reliable_message; + int count; + int qport; + + sequence = LittleLong(*((int *)(data + 0))); + sequence_ack = LittleLong(*((int *)(data + 4))); + data += 8; + length -= 8; + + if (conn != cls.netcon) + { + // server only + if (length < 2) + return 0; + // TODO: use qport to identify that this client really is who they say they are? (and elsewhere in the code to identify the connection without a port match?) + qport = LittleShort(*((int *)(data + 8))); + data += 2; + length -= 2; + } - if (length >= 8) + packetsReceived++; + reliable_message = (sequence >> 31) & 1; + reliable_ack = (sequence_ack >> 31) & 1; + sequence &= ~(1<<31); + sequence_ack &= ~(1<<31); + if (sequence <= conn->qw.incoming_sequence) + { + Con_DPrint("Got a stale datagram\n"); + return 0; + } + count = sequence - (conn->qw.incoming_sequence + 1); + if (count > 0) + { + droppedDatagrams += count; + Con_DPrintf("Dropped %u datagram(s)\n", count); + } + if (reliable_ack == conn->qw.reliable_sequence) + { + // received, now we will be able to send another reliable message + conn->sendMessageLength = 0; + reliableMessagesReceived++; + } + conn->qw.incoming_sequence = sequence; + conn->qw.incoming_acknowledged = sequence_ack; + conn->qw.incoming_reliable_acknowledged = reliable_ack; + if (reliable_message) + conn->qw.incoming_reliable_sequence ^= 1; + conn->lastMessageTime = realtime; + conn->timeout = realtime + net_messagetimeout.value; + unreliableMessagesReceived++; + SZ_Clear(&net_message); + SZ_Write(&net_message, data, length); + MSG_BeginReading(); + return 2; + } + else { + unsigned int count; + unsigned int flags; + unsigned int sequence; + int qlength; + qlength = (unsigned int)BigLong(((int *)data)[0]); flags = qlength & ~NETFLAG_LENGTH_MASK; qlength &= NETFLAG_LENGTH_MASK; @@ -827,15 +883,15 @@ int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length) length -= 8; if (flags & NETFLAG_UNRELIABLE) { - if (sequence >= conn->unreliableReceiveSequence) + if (sequence >= conn->nq.unreliableReceiveSequence) { - if (sequence > conn->unreliableReceiveSequence) + if (sequence > conn->nq.unreliableReceiveSequence) { - count = sequence - conn->unreliableReceiveSequence; + count = sequence - conn->nq.unreliableReceiveSequence; droppedDatagrams += count; Con_DPrintf("Dropped %u datagram(s)\n", count); } - conn->unreliableReceiveSequence = sequence + 1; + conn->nq.unreliableReceiveSequence = sequence + 1; conn->lastMessageTime = realtime; conn->timeout = realtime + net_messagetimeout.value; unreliableMessagesReceived++; @@ -853,27 +909,53 @@ int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length) } else if (flags & NETFLAG_ACK) { - if (sequence == (conn->sendSequence - 1)) + if (sequence == (conn->nq.sendSequence - 1)) { - if (sequence == conn->ackSequence) + if (sequence == conn->nq.ackSequence) { - conn->ackSequence++; - if (conn->ackSequence != conn->sendSequence) + conn->nq.ackSequence++; + if (conn->nq.ackSequence != conn->nq.sendSequence) Con_DPrint("ack sequencing error\n"); conn->lastMessageTime = realtime; conn->timeout = realtime + net_messagetimeout.value; - conn->sendMessageLength -= MAX_PACKETFRAGMENT; - if (conn->sendMessageLength > 0) + if (conn->sendMessageLength > MAX_PACKETFRAGMENT) { + unsigned int packetLen; + unsigned int dataLen; + unsigned int eom; + unsigned int *header; + + conn->sendMessageLength -= MAX_PACKETFRAGMENT; memcpy(conn->sendMessage, conn->sendMessage+MAX_PACKETFRAGMENT, conn->sendMessageLength); - conn->sendNext = true; - NetConn_SendMessageNext(conn); + + if (conn->sendMessageLength <= MAX_PACKETFRAGMENT) + { + dataLen = conn->sendMessageLength; + eom = NETFLAG_EOM; + } + else + { + dataLen = MAX_PACKETFRAGMENT; + eom = 0; + } + + packetLen = NET_HEADERSIZE + dataLen; + + header = (unsigned int *)sendbuffer; + header[0] = BigLong(packetLen | (NETFLAG_DATA | eom)); + header[1] = BigLong(conn->nq.sendSequence); + memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen); + + conn->nq.sendSequence++; + + if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen) + { + conn->lastSendTime = realtime; + packetsSent++; + } } else - { conn->sendMessageLength = 0; - conn->canSend = true; - } } else Con_DPrint("Duplicate ACK received\n"); @@ -888,11 +970,11 @@ int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length) temppacket[0] = BigLong(8 | NETFLAG_ACK); temppacket[1] = BigLong(sequence); NetConn_Write(conn->mysocket, (unsigned char *)temppacket, 8, &conn->peeraddress); - if (sequence == conn->receiveSequence) + if (sequence == conn->nq.receiveSequence) { conn->lastMessageTime = realtime; conn->timeout = realtime + net_messagetimeout.value; - conn->receiveSequence++; + conn->nq.receiveSequence++; if( conn->receiveMessageLength + length <= (int)sizeof( conn->receiveMessage ) ) { memcpy(conn->receiveMessage + conn->receiveMessageLength, data, length); conn->receiveMessageLength += length; @@ -925,7 +1007,7 @@ int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length) return 0; } -void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress) +void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress, protocolversion_t initialprotocol) { cls.connect_trying = false; M_Update_Return_Reason(""); @@ -933,7 +1015,7 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer CL_Disconnect(); // if we're connecting to a remote server, shut down any local server if (LHNETADDRESS_GetAddressType(peeraddress) != LHNETADDRESSTYPE_LOOP && sv.active) - Host_ShutdownServer (false); + Host_ShutdownServer (); // allocate a net connection to keep track of things cls.netcon = NetConn_Open(mysocket, peeraddress); Con_Printf("Connection accepted to %s\n", cls.netcon->address); @@ -942,6 +1024,9 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer cls.demonum = -1; // not in the demo loop now cls.state = ca_connected; cls.signon = 0; // need all the signon messages before playing + cls.protocol = initialprotocol; + if (cls.protocol == PROTOCOL_QUAKEWORLD) + Cmd_ForwardStringToServer("new"); } int NetConn_IsLocalGame(void) @@ -951,13 +1036,17 @@ int NetConn_IsLocalGame(void) return false; } -int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress) +static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress) { + qboolean fromserver; int ret, c, control; const char *s; char *string, addressstring2[128], cname[128], ipstring[32]; char stringbuf[16384]; + // quakeworld ingame packet + fromserver = cls.netcon && mysocket == cls.netcon->mysocket && !LHNETADDRESS_Compare(&cls.netcon->peeraddress, peeraddress); + if (length >= 5 && data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255) { // received a command string - strip off the packaging and put it @@ -969,27 +1058,45 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int stringbuf[length] = 0; string = stringbuf; + LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true); + if (developer.integer) { - LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true); Con_Printf("NetConn_ClientParsePacket: %s sent us a command:\n", addressstring2); Com_HexDumpToConsole(data, length); } if (length > 10 && !memcmp(string, "challenge ", 10) && cls.connect_trying) { + // darkplaces or quake3 char protocolnames[1400]; Protocol_Names(protocolnames, sizeof(protocolnames)); - LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true); Con_Printf("\"%s\" received, sending connect request back to %s\n", string, addressstring2); M_Update_Return_Reason("Got challenge response"); NetConn_WriteString(mysocket, va("\377\377\377\377connect\\protocol\\darkplaces 3\\protocols\\%s\\challenge\\%s", protocolnames, string + 10), peeraddress); return true; } + if (length > 1 && string[0] == 'c' && (string[1] == '-' || (string[1] >= '0' && string[1] <= '9'))) + { + // quakeworld + LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true); + Con_Printf("\"%s\" received, sending QuakeWorld connect request back to %s\n", string, addressstring2); + M_Update_Return_Reason("Got QuakeWorld challenge response"); + cls.qw_qport = qport.integer; + NetConn_WriteString(mysocket, va("\377\377\377\377connect 28 %i %i \"%s\"\n", cls.qw_qport, atoi(string + 1), cls.userinfo), peeraddress); + } if (length == 6 && !memcmp(string, "accept", 6) && cls.connect_trying) { + // darkplaces or quake3 M_Update_Return_Reason("Accepted"); - NetConn_ConnectionEstablished(mysocket, peeraddress); + NetConn_ConnectionEstablished(mysocket, peeraddress, PROTOCOL_DARKPLACES3); + return true; + } + if (length > 1 && string[0] == 'j' && cls.connect_trying) + { + // quakeworld + M_Update_Return_Reason("QuakeWorld Accepted"); + NetConn_ConnectionEstablished(mysocket, peeraddress, PROTOCOL_QUAKEWORLD); return true; } if (length > 7 && !memcmp(string, "reject ", 7) && cls.connect_trying) @@ -1125,10 +1232,47 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int if (!strncmp(string, "ack", 3)) return true; */ + // QuakeWorld compatibility + if (length >= 1 && string[0] == 'j' && cls.connect_trying) + { + // accept message + M_Update_Return_Reason("Accepted"); + NetConn_ConnectionEstablished(mysocket, peeraddress, PROTOCOL_QUAKEWORLD); + return true; + } + if (length > 1 && string[0] == 'c' && string[1] >= '0' && string[1] <= '9' && cls.connect_trying) + { + // challenge message + LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true); + Con_Printf("challenge %s received, sending connect request back to %s\n", string + 1, addressstring2); + M_Update_Return_Reason("Got challenge response"); + cls.qw_qport = qport.integer; + InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "*ip", addressstring2); + InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "name", cl_name.string); + InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "topcolor", va("%i", (cl_color.integer >> 4) & 15)); + InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "bottomcolor", va("%i", (cl_color.integer) & 15)); + InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "rate", va("%i", cl_rate.integer)); + InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "msg", "1"); + InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "*ver", engineversion); + NetConn_WriteString(mysocket, va("\377\377\377\377connect %i %i %i \"%s\"\n", 28, cls.qw_qport, atoi(string + 1), cls.userinfo), peeraddress); + return true; + } + if (string[0] == 'n') + { + // qw print command + Con_Printf("QW print command from server at %s:\n", addressstring2, string + 1); + } // we may not have liked the packet, but it was a command packet, so // we're done processing this packet now return true; } + // quakeworld ingame packet + if (fromserver && cls.protocol == PROTOCOL_QUAKEWORLD && length >= 8 && (ret = NetConn_ReceivedMessage(cls.netcon, data, length, cls.protocol)) == 2) + { + ret = 0; + CL_ParseServerMessage(); + return ret; + } // netquake control packets, supported for compatibility only if (length >= 5 && (control = BigLong(*((int *)data))) && (control & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (control & NETFLAG_LENGTH_MASK) == length) { @@ -1153,7 +1297,7 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int LHNETADDRESS_SetPort(&clientportaddress, port); } M_Update_Return_Reason("Accepted"); - NetConn_ConnectionEstablished(mysocket, &clientportaddress); + NetConn_ConnectionEstablished(mysocket, &clientportaddress, PROTOCOL_QUAKE); } break; case CCREP_REJECT: @@ -1216,7 +1360,7 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int return true; } ret = 0; - if (length >= (int)NET_HEADERSIZE && cls.netcon && mysocket == cls.netcon->mysocket && !LHNETADDRESS_Compare(&cls.netcon->peeraddress, peeraddress) && (ret = NetConn_ReceivedMessage(cls.netcon, data, length)) == 2) + if (fromserver && length >= (int)NET_HEADERSIZE && (ret = NetConn_ReceivedMessage(cls.netcon, data, length, cls.protocol)) == 2) CL_ParseServerMessage(); return ret; } @@ -1290,8 +1434,7 @@ void NetConn_ClientFrame(void) { int i, length; lhnetaddress_t peeraddress; - netconn_t *conn; - NetConn_UpdateServerStuff(); + NetConn_UpdateSockets(); if (cls.connect_trying && cls.connect_nextsendtime < realtime) { if (cls.connect_remainingtries == 0) @@ -1304,7 +1447,7 @@ void NetConn_ClientFrame(void) M_Update_Return_Reason("Connect: Failed"); return; } - // try challenge first (newer server) + // try challenge first (newer DP server or QW) NetConn_WriteString(cls.connect_mysocket, "\377\377\377\377getchallenge", &cls.connect_address); // then try netquake as a fallback (old server, or netquake) SZ_Clear(&net_message); @@ -1317,20 +1460,16 @@ void NetConn_ClientFrame(void) NetConn_Write(cls.connect_mysocket, net_message.data, net_message.cursize, &cls.connect_address); SZ_Clear(&net_message); } - for (i = 0;i < cl_numsockets;i++) { - while (cl_sockets[i] && (length = NetConn_Read(cl_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0) { + for (i = 0;i < cl_numsockets;i++) + while (cl_sockets[i] && (length = NetConn_Read(cl_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0) NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress); - } - } NetConn_QueryQueueFrame(); if (cls.netcon && realtime > cls.netcon->timeout) { Con_Print("Connection timed out\n"); CL_Disconnect(); - Host_ShutdownServer (false); + Host_ShutdownServer (); } - for (conn = netconn_list;conn;conn = conn->next) - NetConn_ReSendMessage(conn); } #define MAX_CHALLENGES 128 @@ -1431,13 +1570,22 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg } extern void SV_SendServerinfo (client_t *client); -int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress) +static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress) { int i, ret, clientnum, best; double besttime; client_t *client; netconn_t *conn; - char *s, *string, response[512], addressstring2[128], stringbuf[16384]; + char *s, *string, response[1400], addressstring2[128], stringbuf[16384]; + + // see if we can identify the sender as a local player + // (this is necessary for rcon to send a reliable reply if the client is + // actually on the server, not sending remotely) + for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++) + if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress)) + break; + if (i == svs.maxclients) + host_client = NULL; if (sv.active) { @@ -1544,7 +1692,9 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int Con_Printf("Datagram_ParseConnectionless: sending \"accept\" to %s.\n", conn->address); NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress); // now set up the client + SV_VM_Begin(); SV_ConnectClient(clientnum, conn); + SV_VM_End(); NetConn_Heartbeat(1); } } @@ -1593,6 +1743,46 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int } return true; } + if (length >= 5 && !memcmp(string, "rcon ", 5)) + { + int i; + char *s = string + 5; + char password[64]; + for (i = 0;*s > ' ';s++) + if (i < (int)sizeof(password) - 1) + password[i++] = *s; + password[i] = 0; + if (password[0] > ' ' && !strcmp(rcon_password.string, password)) + { + // looks like a legitimate rcon command with the correct password + Con_Printf("server received rcon command from %s:\n%s\n", host_client ? host_client->name : addressstring2, s); + rcon_redirect = true; + rcon_redirect_bufferpos = 0; + Cmd_ExecuteString(s, src_command); + rcon_redirect_buffer[rcon_redirect_bufferpos] = 0; + rcon_redirect = false; + // print resulting text to client + // if client is playing, send a reliable reply instead of + // a command packet + if (host_client) + { + // if the netconnection is loop, then this is the + // local player on a listen mode server, and it would + // result in duplicate printing to the console + // (not that the local player should be using rcon + // when they have the console) + if (host_client->netconnection && LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP) + SV_ClientPrintf("%s", rcon_redirect_buffer); + } + else + { + // qw print command + dpsnprintf(response, sizeof(response), "\377\377\377\377n%s", rcon_redirect_buffer); + NetConn_WriteString(mysocket, response, peeraddress); + } + } + return true; + } /* if (!strncmp(string, "ping", 4)) { @@ -1698,7 +1888,9 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress); SZ_Clear(&net_message); // now set up the client struct + SV_VM_Begin(); SV_ConnectClient(clientnum, conn); + SV_VM_End(); NetConn_Heartbeat(1); } else @@ -1811,12 +2003,13 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int } } #endif - for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++) + if (host_client) { - if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress)) + if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length, sv.protocol)) == 2) { - if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length)) == 2) - SV_ReadClientMessage(); + SV_VM_Begin(); + SV_ReadClientMessage(); + SV_VM_End(); return ret; } } @@ -1828,8 +2021,7 @@ void NetConn_ServerFrame(void) { int i, length; lhnetaddress_t peeraddress; - netconn_t *conn; - NetConn_UpdateServerStuff(); + NetConn_UpdateSockets(); for (i = 0;i < sv_numsockets;i++) while (sv_sockets[i] && (length = NetConn_Read(sv_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0) NetConn_ServerParsePacket(sv_sockets[i], readbuffer, length, &peeraddress); @@ -1842,8 +2034,6 @@ void NetConn_ServerFrame(void) SV_DropClient(false); } } - for (conn = netconn_list;conn;conn = conn->next) - NetConn_ReSendMessage(conn); } void NetConn_QueryMasters(void) @@ -1932,43 +2122,6 @@ void NetConn_Heartbeat(int priority) } } -int NetConn_SendToAll(sizebuf_t *data, double blocktime) -{ - int i, count = 0; - unsigned char sent[MAX_SCOREBOARD]; - - memset(sent, 0, sizeof(sent)); - - // simultaneously wait for the first CanSendMessage and send the message, - // then wait for a second CanSendMessage (verifying it was received), or - // the client drops and is no longer counted - // the loop aborts when either it runs out of clients to send to, or a - // timeout expires - blocktime += Sys_DoubleTime(); - do - { - count = 0; - // run a network frame to check for packets - NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End(); - for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++) - { - if (host_client->netconnection) - { - if (NetConn_CanSendMessage(host_client->netconnection)) - { - if (!sent[i]) - NetConn_SendReliableMessage(host_client->netconnection, data); - sent[i] = true; - } - if (!NetConn_CanSendMessage(host_client->netconnection)) - count++; - } - } - } - while (count && Sys_DoubleTime() < blocktime); - return count; -} - static void Net_Heartbeat_f(void) { if (sv.active) @@ -1979,7 +2132,10 @@ static void Net_Heartbeat_f(void) void PrintStats(netconn_t *conn) { - Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, conn->canSend, conn->sendSequence, conn->receiveSequence); + if ((cls.state == ca_connected && cls.protocol == PROTOCOL_QUAKEWORLD) || (sv.active && sv.protocol == PROTOCOL_QUAKEWORLD)) + Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, !conn->sendMessageLength, conn->qw.outgoing_sequence, conn->qw.incoming_sequence); + else + Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, !conn->sendMessageLength, conn->nq.sendSequence, conn->nq.receiveSequence); } void Net_Stats_f(void)