aboutsummaryrefslogtreecommitdiff
path: root/src/parsec_server/net_util_sv.cpp
blob: 88c0468830312fda237cc67bf9f1a1d01804bec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * PARSEC - Utility Functions - server
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/15 12:26:06 $
 *
 * Orginally written by:
 *   Copyright (c) Clemens Beer        <cbx@parsec.org>   2001-2002
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   1996-2000
 *
 *  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
 */ 

// C library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

// compilation flags/debug support
#include "config.h"
#include "debug.h"

// general definitions
#include "general.h"
#include "objstruc.h"

// network subsystem & server headers 
#include "net_defs.h"
#include "e_defs.h"

// network code config
//#include "net_conf.h"

// UNP header
#include "net_wrap.h"

// local module header
#include "net_util.h"


// flags ----------------------------------------------------------------------
//

// copy one node over another -------------------------------------------------
//
void NODE_Copy( node_t* dst, node_t* src )
{
	ASSERT( dst != NULL );
	ASSERT( src != NULL );

	memcpy( dst, src, sizeof( node_t ) );
}


// store port number into node address structure ------------------------------
//
void NODE_StorePort( node_t *node, word port )
{
	ASSERT( node != NULL );
	
	node->address[ 4 ] = port >> 8;
	node->address[ 5 ] = port & 0xff;
}


// fetch port number from node address structure ------------------------------
//
word NODE_GetPort( node_t *node )
{
	ASSERT( node != NULL );
	
	return ( ( (word)node->address[ 4 ] << 8 ) | node->address[ 5 ] );
}


// compare two node addresses (equal, less than, greater than) ----------------
//
int NODE_Compare( node_t *node1, node_t *node2 )
{
	byte *ip1 = (byte *) node1;
	byte *ip2 = (byte *) node2;
	
	// compare as many digits as necessary
	for ( int pos = 0; pos < NODE_ADR_LENGTH; pos++ ) {
		
		if ( ip1[ pos ] < ip2[ pos ] ) {
			return NODECMP_LESSTHAN;
		} else if ( ip1[ pos ] > ip2[ pos ] ) {
			return NODECMP_GREATERTHAN;
		}
	}
	
	return NODECMP_EQUAL;
}	

// compare two node addresses ( fast version ) --------------------------------
//
int NODE_AreSame( node_t *node1, node_t *node2 )
{
	return ( memcmp( (void*)node1, (void*)node2, sizeof( node_t ) ) == 0 );
}


// static print area for a node string ----------------------------------------
//
//FIXME: IPv6 ?
static char szNodePrintBuffer[ sizeof "255.255.255.255:65535" ];

// return a STATIC string that contains the node in printable form ------------
//
char* NODE_Print( node_t* node )
{
	ASSERT( node != NULL );

	// convert IP
	char ipstring[ MAX_IPADDR_LEN + 1 ];
	inet_ntop( AF_INET, node, ipstring, MAX_IPADDR_LEN + 1 );

	//NOTE: this function uses a STATIC string buffer that holds the printable
	//      form of the node. Do not store the pointer, as it may get overwritten
	//      by subsequent calls to NODE_Print

	sprintf( szNodePrintBuffer, "%s:%d", ipstring,  NODE_GetPort( node ) );
	return szNodePrintBuffer;
}