2006-04-16 21:37:00 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 04/2006 by Olaf Rempel *
|
|
|
|
* razzor@kopf-tisch.de *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
2006-04-16 21:02:41 +02:00
|
|
|
#include "gameentry.h"
|
2006-04-15 19:55:07 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2006-03-05 02:29:34 +01:00
|
|
|
#include "gameentry.h"
|
|
|
|
|
2006-04-17 13:59:54 +02:00
|
|
|
GameEntry::GameEntry(int gameid, struct in_addr *addr, int port1, int port2)
|
|
|
|
: port1(port1), port2(port2), gameid(gameid)
|
2006-04-15 19:55:07 +02:00
|
|
|
{
|
2006-04-17 13:59:54 +02:00
|
|
|
memcpy(&this->addr, addr, sizeof(this->addr));
|
2006-04-15 19:55:07 +02:00
|
|
|
|
|
|
|
if (port2 != 0) {
|
|
|
|
this->port2 = this->port1;
|
|
|
|
this->port1 = port2;
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2006-03-05 02:29:34 +01:00
|
|
|
int GameEntry::compare(const GameEntry* ge)
|
|
|
|
{
|
|
|
|
if (this->addr.s_addr > ge->addr.s_addr)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (this->addr.s_addr < ge->addr.s_addr)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (this->port1 > ge->port1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (this->port1 < ge->port1)
|
|
|
|
return 1;
|
|
|
|
|
2006-04-15 19:55:07 +02:00
|
|
|
// only compare IP:port
|
|
|
|
// gameid and port2 are useless
|
2006-03-05 02:29:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2006-04-15 19:55:07 +02:00
|
|
|
|
|
|
|
int GameEntry::hash(int max)
|
|
|
|
{
|
|
|
|
unsigned int hash = 0x12345678;
|
|
|
|
|
|
|
|
// IP
|
|
|
|
hash = ((hash<<5) ^ (hash>>27)) ^ ((addr.s_addr>>0) & 0xFF);
|
|
|
|
hash = ((hash<<5) ^ (hash>>27)) ^ ((addr.s_addr>>8) & 0xFF);
|
|
|
|
hash = ((hash<<5) ^ (hash>>27)) ^ ((addr.s_addr>>16) & 0xFF);
|
|
|
|
hash = ((hash<<5) ^ (hash>>27)) ^ ((addr.s_addr>>24) & 0xFF);
|
|
|
|
|
|
|
|
// port
|
|
|
|
hash = ((hash<<5) ^ (hash>>27)) ^ ((port1>>0) & 0xFF);
|
|
|
|
hash = ((hash<<5) ^ (hash>>27)) ^ ((port1>>8) & 0xFF);
|
|
|
|
|
|
|
|
return hash % max;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameEntry::update()
|
|
|
|
{
|
|
|
|
modtime = time(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GameEntry::show(char* buf, int size)
|
|
|
|
{
|
|
|
|
return snprintf(buf, size, "(%2d:%15s:%5d:%5d)",
|
|
|
|
this->gameid, inet_ntoa(this->addr),
|
|
|
|
this->port1, this->port2);
|
|
|
|
}
|