Get the interface's external IP when using Rancidmeat configuration files if none of the peers have a LAN IP

git-svn-id: https://svn.eduke32.com/eduke32@136 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2006-05-02 02:57:11 +00:00
parent 34241c8514
commit bf48c4c4dc
2 changed files with 121 additions and 4 deletions

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
@ -648,3 +649,73 @@ long getpacket (long *retother, char *bufptr)
return(0);
}
char tempbuf[512],ipaddr[32];
const char *getexternaladdress(void)
{
int sockfd, bytes_sent, i=0, j=0;
struct sockaddr_in dest_addr;
struct hostent *h;
char *host = "checkip.dyndns.org";
char *req = "GET / HTTP/1.0\r\n\r\n";
#ifdef _WIN32
WSADATA ws;
if (WSAStartup(0x101,&ws) == SOCKET_ERROR) {
initprintf("winsock error %d\n",errno);
return(0);
}
#endif
if ((h=gethostbyname(host)) == NULL) {
initprintf("gethostbyname error %d\n",h_errno);
return(0);
}
dest_addr.sin_addr.s_addr = ((struct in_addr *)(h->h_addr))->s_addr;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(8245);
memset(&(dest_addr.sin_zero), '\0', 8);
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(sockfd == SOCKET_ERROR) {
initprintf("socket error %d\n",errno);
return(0);
}
if(connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == SOCKET_ERROR) {
initprintf("connect error %d\n",errno);
return(0);
}
bytes_sent = send(sockfd, req, strlen(req), 0);
if(bytes_sent == SOCKET_ERROR) {
initprintf("bytes_sent error %d\n",errno);
return(0);
}
// initprintf("sent %d bytes\n",bytes_sent);
recv(sockfd, (char *)&tempbuf, sizeof(tempbuf), 0);
closesocket(sockfd);
#ifdef _WIN32
WSACleanup();
#endif
for(i=0;(unsigned)i<strlen(tempbuf);i++)
{
if(isdigit(tempbuf[i]) && (isdigit(tempbuf[i+1]) || (tempbuf[i+1] == '.')) && (isdigit(tempbuf[i+2]) || (tempbuf[i+2] == '.')) && (isdigit(tempbuf[i+3]) || (tempbuf[i+3] == '.')))
{
while(isdigit(tempbuf[i]) || (tempbuf[i] == '.'))
{
ipaddr[j] = tempbuf[i];
i++, j++;
}
ipaddr[j] = '\0';
break;
}
}
return(ipaddr);
}

View file

@ -7494,10 +7494,11 @@ static int getatoken(scriptfile *sf, tokenlist *tl, int ntokens)
if (!Bstrcasecmp(tok, tl[i].text))
return tl[i].tokenid;
}
return T_ERROR;
}
extern const char *getexternaladdress(void);
static int parse_rancid_net(scriptfile *script)
{
int tokn;
@ -7509,6 +7510,7 @@ static int parse_rancid_net(scriptfile *script)
case T_INTERFACE:
{
char *ip;
if (scriptfile_getstring(script,&ip)) break;
Bstrcpy(rancid_ip_strings[MAXPLAYERS],ip);
Bstrcpy(rancid_ip_strings[rancid_players++],ip);
@ -7521,12 +7523,14 @@ static int parse_rancid_net(scriptfile *script)
case T_MODE:
{
char *mode;
if (scriptfile_getstring(script,&mode)) break;
}
break;
case T_ALLOW:
{
char *ip;
if (scriptfile_getstring(script,&ip)) break;
Bstrcpy(rancid_ip_strings[rancid_players++],ip);
}
@ -7594,12 +7598,15 @@ void checkcommandline(int argc,char **argv)
c = argv[i];
if (((*c == '/') || (*c == '-')) && (!firstnet))
{
if (!Bstrcasecmp(c+1,"rmnet")) {
if (argc > i+1) {
if (!Bstrcasecmp(c+1,"rmnet"))
{
if (argc > i+1)
{
CommandNet = argv[i+1];
i++;
}
if(CommandNet) {
if(CommandNet)
{
load_rancid_net(CommandNet);
qsort((char *)rancid_ip_strings, rancid_players, sizeof(rancid_ip_strings[0]), (int(*)(const void*,const void*))stringsort);
CommandNet = 0;
@ -7608,6 +7615,44 @@ void checkcommandline(int argc,char **argv)
netparamcount++;
netparam = (char **)calloc(netparamcount, sizeof(char **));
for(j=0;j<rancid_players;j++)
{
if(Bstrcmp(rancid_ip_strings[j],rancid_ip_strings[MAXPLAYERS]) != 0)
{
char tmp[16];
Bstrncpy(tempbuf,rancid_ip_strings[j], 8);
Bstrcpy(tmp,strtok(tempbuf,"."));
if(Bstrcmp(tmp,"10") == 0)
{
j = 0;
break;
}
if((Bstrcmp(tmp,"192") == 0) || (Bstrcmp(tmp,"172") == 0))
{
Bstrcpy(tmp,strtok(NULL,"."));
if((Bstrcmp(tmp,"168") == 0) || (Bstrcmp(tmp,"16") == 0))
{
j = 0;
break;
}
}
}
}
if(j == rancid_players)
{
Bsprintf(tempbuf, getexternaladdress());
if(tempbuf[0])
{
for(j=0;j<rancid_players;j++)
{
if(Bstrcmp(rancid_ip_strings[j],rancid_ip_strings[MAXPLAYERS]) == 0)
{
Bstrcpy(rancid_ip_strings[MAXPLAYERS],tempbuf);
Bstrcpy(rancid_ip_strings[j],tempbuf);
}
}
}
}
for(j=0;j<rancid_players;j++)
{
if(Bstrcmp(rancid_ip_strings[j],rancid_ip_strings[MAXPLAYERS]) == 0)
Bsprintf(rancid_ip_strings[j],"/n1");
@ -7615,6 +7660,7 @@ void checkcommandline(int argc,char **argv)
}
if(j != netparamcount)
netparam[j] = rancid_local_port_string;
initprintf("ip: %s\n",rancid_ip_strings[MAXPLAYERS]);
}
i++;
continue;