forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
network_address.cc
68 lines (55 loc) · 1.07 KB
/
network_address.cc
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
#include "network_address.h"
#include "network_packet.h"
#include <stdlib.h>
#include <string.h>
net_address_t::net_address_t(uint32 ip_, uint32 mask_) : ip(ip_), mask(mask_)
{
ipstr[0] = '\0';
init_ipstr();
}
net_address_t::net_address_t(const char *text)
{
ipstr[0] = '\0';
uint32 offset = 0;
ip = 0;
mask = 0;
for(sint8 j=24; j>=0; j-=8) {
uint32 n = atoi(text);
ip |= (n & 0xff) << j;
mask |= 0xff << j;
text = strchr(text+offset, '.');
if (text) {
text++;
}
else {
break;
}
}
init_ipstr();
}
void net_address_t::rdwr(packet_t *packet)
{
packet->rdwr_long(ip);
packet->rdwr_long(mask);
}
void net_address_t::init_ipstr()
{
if ( ipstr[0] == '\0' ) {
sprintf( ipstr, "%i.%i.%i.%i", (ip >> 24) & 255, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255 );
}
}
const char* net_address_t::get_str () const
{
return ipstr;
}
void address_list_t::rdwr(packet_t *packet)
{
uint32 count = get_count();
packet->rdwr_long(count);
for(uint32 i=0; i<count; i++) {
if (packet->is_loading()) {
append(net_address_t());
}
(*this)[i].rdwr(packet);
}
}