aboutsummaryrefslogtreecommitdiff
path: root/network.cpp
blob: e5cc70575a7c9adcfb03a28e0ce3ad6d138ce2e0 (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

HIGHLY MODIFIED FROM THE ORIGINAL CODE
*/

#include <string>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#include "network.h"

uint16_t m_peer_id;

int sockfd = 0;

struct sockaddr_in server;

uint16_t seqnum_reliable[3];
uint16_t seqnum_split;

int Resolve(char *address, char *port, in_addr_t & ip)
{
	int success;
	struct addrinfo hints, *servinfo, *p;

	bzero(&hints, sizeof(hints));

	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_DGRAM;

	success = getaddrinfo(address, port, &hints, &servinfo);

	if (success != 0) {
		printf("Resolve hostname: failed.\n");
		return 1;
	} else {
		printf("Resolve hostname: success.\n");
	}

	for (p = servinfo; p != NULL; p = p->ai_next) {
		if (((struct sockaddr_in *)p->ai_addr)->sin_addr.s_addr !=
		    INADDR_ANY) {
			ip = ((struct sockaddr_in *)p->ai_addr)->
			    sin_addr.s_addr;
			break;
		}
	}

	return 0;
};

int CreateClient(char *address, char *port)
{
	int success, i;

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd == -1) {
		printf("Error while creating socket!\n");
		return 1;
	}

	struct timeval timeout;
	timeout.tv_sec = 30;
	timeout.tv_usec = 0;

	setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

	bzero(&server, sizeof(server));

	in_addr_t ip;

	success = inet_pton(AF_INET, address, &ip);

	printf("Address: %s\n", address);

	if (success != 1) {
		printf("Direct conversion: failed.\n");
		success = Resolve(address, port, ip);
	} else {
		printf("Direct conversion: success.\n");
		success = 0;
	}

	if (success != 0) {
		printf("Unable to proccess address: %s\n", address);
		return 2;
	}

	uint16_t iport;
	sscanf(port, "%hd", &iport);

	server.sin_addr.s_addr = ip;
	server.sin_family = AF_INET;
	server.sin_port = htons(iport);

	connect(sockfd, (sockaddr *) & server, sizeof(server));

	m_peer_id = PEER_ID_NONEXISTENT;

	for (i = 0; i < CHANNEL_COUNT; i++) {
		seqnum_reliable[i] = SEQNUM_INITIAL;
	}
	seqnum_split = SEQNUM_INITIAL;

	return 0;
};

int Send(NetworkPacket & pkt)
{
	int len_sent = send(sockfd, pkt.get_data(), pkt.get_size(), 0);
	if (len_sent >= 1) {
		return 0;
	} else {
		return 1;
	}
};

int Recv(NetworkPacket & pkt)
{
	int len;
	char buf[512];

	len = recv(sockfd, buf, 512, 0);

	while (len == 0)
		len = recv(sockfd, buf, 512, 0);
	if (len < 0) {
		printf("%04X\n", errno);
		return 1;
	}

	bool success = pkt.set_data(buf, len);

	if (!success)
		return 2;

	return 0;
};

int Create_Packet(NetworkPacket & pkt, uint8_t channel, bool reliable)
{
	if (channel >= CHANNEL_COUNT)
		return 1;

	pkt << (uint32_t) PROTOCOL_ID << m_peer_id << channel;

	if (!reliable)
		return 0;

	pkt << (uint8_t) TYPE_RELIABLE << seqnum_reliable[channel];
	seqnum_reliable[channel]++;

	return 0;
};

int Disconnect()
{
	NetworkPacket pkt;
	Create_Packet(pkt, 0, false);
	pkt << (uint8_t) TYPE_CONTROL << (uint8_t) CONTROLTYPE_DISCO;
	Send(pkt);
	close(sockfd);
	return 0;
};