net: do strtok on a duplicated string containing the address in Net_Connect().

Also,
 - don't strip const when passing the char ptr to it and a couple more
   instances in game.c
 - use g_netPort when -connect parameter doesn't have a port suffix (":XXXX"),
   so that e.g.
    eduke32 -port 1700 -connect localhost
   is the same as
    eduke32 -connect localhost:1700
   (-port must come before -connect, unfortunately.)

git-svn-id: https://svn.eduke32.com/eduke32@2737 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-06-03 19:20:41 +00:00
parent 9b40534c8f
commit 3fbf46ce3a
2 changed files with 14 additions and 8 deletions

View file

@ -8532,7 +8532,7 @@ static int32_t loaddefinitions_game(const char *fn, int32_t preload)
scriptfile *script; scriptfile *script;
int32_t i; int32_t i;
script = scriptfile_fromfile((char *)fn); script = scriptfile_fromfile(fn);
if (!script) return -1; if (!script) return -1;
parsedefinitions_game(script, preload); parsedefinitions_game(script, preload);
@ -8743,7 +8743,7 @@ static void G_CheckCommandLine(int32_t argc, const char **argv)
{ {
if (argc > i+1) if (argc > i+1)
{ {
Net_Connect((char *)argv[i+1]); Net_Connect(argv[i+1]);
g_noSetup = g_noLogo = TRUE; g_noSetup = g_noLogo = TRUE;
i++; i++;
} }
@ -8840,7 +8840,7 @@ static void G_CheckCommandLine(int32_t argc, const char **argv)
{ {
if (argc > i+1) if (argc > i+1)
{ {
uint32_t j = Batol((char *)argv[i+1]); uint32_t j = Batol(argv[i+1]);
if (j>=10000000 && j<=99999999) if (j>=10000000 && j<=99999999)
{ {
g_scriptDateVersion = j; g_scriptDateVersion = j;
@ -8871,7 +8871,7 @@ static void G_CheckCommandLine(int32_t argc, const char **argv)
{ {
if (argc > i+1) if (argc > i+1)
{ {
uint32_t j = Batol((char *)argv[i+1]); uint32_t j = Batol(argv[i+1]);
MAXCACHE1DSIZE = j<<10; MAXCACHE1DSIZE = j<<10;
initprintf("Cache size: %dkB\n",j); initprintf("Cache size: %dkB\n",j);
i++; i++;

View file

@ -312,6 +312,9 @@ void Net_Connect(const char *srvaddr)
char *addrstr = NULL; char *addrstr = NULL;
int32_t i; int32_t i;
char *oursrvaddr = Bstrdup(srvaddr);
if (!oursrvaddr) G_GameExit("OUT OF MEMORY");
Net_Disconnect(); Net_Disconnect();
g_netClient = enet_host_create(NULL, 1, CHAN_MAX, 0, 0); g_netClient = enet_host_create(NULL, 1, CHAN_MAX, 0, 0);
@ -322,9 +325,10 @@ void Net_Connect(const char *srvaddr)
return; return;
} }
addrstr = strtok((char *)srvaddr, ":"); addrstr = strtok(oursrvaddr, ":");
enet_address_set_host(&address, addrstr); enet_address_set_host(&address, addrstr);
address.port = atoi((addrstr = strtok(NULL, ":")) == NULL ? "23513" : addrstr); addrstr = strtok(NULL, ":");
address.port = addrstr==NULL ? g_netPort : Batoi(addrstr);
g_netClientPeer = enet_host_connect(g_netClient, &address, CHAN_MAX, 0); g_netClientPeer = enet_host_connect(g_netClient, &address, CHAN_MAX, 0);
@ -340,7 +344,8 @@ void Net_Connect(const char *srvaddr)
if (enet_host_service(g_netClient, & event, 5000) > 0 && if (enet_host_service(g_netClient, & event, 5000) > 0 &&
event.type == ENET_EVENT_TYPE_CONNECT) event.type == ENET_EVENT_TYPE_CONNECT)
{ {
initprintf("Connection to %s:%d succeeded.\n", (char *)srvaddr, address.port); initprintf("Connection to %s:%d succeeded.\n", oursrvaddr, address.port);
Bfree(oursrvaddr);
return; return;
} }
else else
@ -349,11 +354,12 @@ void Net_Connect(const char *srvaddr)
/* received. Reset the peer in the event the 5 seconds */ /* received. Reset the peer in the event the 5 seconds */
/* had run out without any significant event. */ /* had run out without any significant event. */
enet_peer_reset(g_netClientPeer); enet_peer_reset(g_netClientPeer);
initprintf("Connection to %s:%d failed.\n",(char *)srvaddr,address.port); initprintf("Connection to %s:%d failed.\n", oursrvaddr, address.port);
} }
initprintf(i ? "Retrying...\n" : "Giving up connection attempt.\n"); initprintf(i ? "Retrying...\n" : "Giving up connection attempt.\n");
} }
Bfree(oursrvaddr);
Net_Disconnect(); Net_Disconnect();
} }