summaryrefslogtreecommitdiff
path: root/src/net_server.c
blob: a0dc0e2dbb6a36dc7482e24c8ffb25a4995c9d0e (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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "net.h"
#include "player.h"

/**
 * Server side function; start server on specified port
 * @param[in] port Port on which server should listen
 * @return Listening socket
 */
int server_start(const char* port)
{
	assert(port != NULL);

	int status;
	int serversock;
	struct addrinfo hints, *result, *tmp;

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;		// IPv4 or IPv6
	hints.ai_socktype = SOCK_STREAM;	// TCP socket
	hints.ai_flags = AI_PASSIVE;		// wildcard IP
	hints.ai_protocol = 0;			// any protocol

	status = getaddrinfo(NULL, port, &hints, &result);
	if(status != 0)
	{
		printf("getaddrinfo: %s\n", gai_strerror(status));
		exit(EXIT_FAILURE);
	}

	// iterate over linked addrinfo list and use first useable entry
	for(tmp = result; tmp != NULL; tmp = tmp->ai_next)
	{
		int yes=1;

		// create socket
		serversock = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol);
		if(serversock == -1)
			continue;

		// try to reuse still open sockets in TIME_WAIT state
		setsockopt(serversock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

		// try to bind to address/port
		if(bind(serversock, tmp->ai_addr, tmp->ai_addrlen) == 0)
			break; // Success!

		close(serversock);
	}

	if(tmp == NULL)
	{
		printf("failed to bind\n");
		exit(EXIT_FAILURE);
	}
	freeaddrinfo(result);

	// start listening
	status = listen(serversock, 1);
	if(status == -1)
	{
		printf("listen: %s\n", strerror(status));
		exit(EXIT_FAILURE);
	}

	return serversock;
}

/**
 * Server side function; accepts connections from clients
 * @param[in] serversock Socket on which server is listening
 * @param[in] count Number of clients that should connect
 * @return List of $count open sockets with connections to clients
 */
int* server_get_players(int serversock, const uint8_t count)
{
	int* clientsocks;
	int i;

	assert(count < MAX_PLAYERS && count > 0);

	clientsocks = malloc(count*sizeof(int));
	if(clientsocks == NULL)
	{
		printf("server_get_players: Out of memory\n");
		exit(EXIT_FAILURE);
	}

	// accept connections
	for(i=0; i<count; i++)
	{
		int sock;
		struct sockaddr_storage addr;
		socklen_t addrlen = sizeof(addr);
		sock = accept(serversock, (struct sockaddr*) &addr, &addrlen);
		if(sock == -1)
		{
			printf("accept: %s\n", strerror(sock));
			exit(EXIT_FAILURE);
		}
		//printf("new client connected: %s\n", inet_ntop(sock.ss_family, get_in_addr((struct sockaddr*)&sock), INET6_ADDRSTRLEN));
		printf("new client connected (%d/%d)\n", i+1, count);
		clientsocks[i] = sock;
	}

	return clientsocks;
}

/**
 * Server side function; notifies players of game start and send list of all players
 * @param[in] clients List of sockets with connection to clients
 * @param[in] clientcount Number of clients
 * @param[in] players List of all players (including self) to send
 */
void server_start_game(int* clients, const uint8_t clientcount, const player_list* players)
{
	assert(clients != NULL);
	assert(players != NULL);

	uint8_t* buf;
	uint8_t usercount = players->count;
	uint32_t pos;
	uint32_t buflen = 3 + usercount; // type + packetlen + usercount + (usercount * len)

	for(int i=0; i<usercount; i++)
		buflen += strlen(players->names[i]);

	buf = malloc(buflen);
	if(buf == NULL)
	{
		printf("server_start_game: Out of memory\n");
		exit(EXIT_FAILURE);
	}
	buf[NET_MSG_OFFSET_TYPE] = msg_type_start_game;
	buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = buflen - 2;
	pos = NET_MSG_OFFSET_PAYLOAD;
	buf[pos++] = players->count;
	// copy usernames with length to buffer
	for(int i=0; i<usercount; i++)
	{
		uint8_t len = strlen(players->names[i]);
		buf[pos++] = len;
		memcpy(buf+pos, players->names[i], len);
		pos += len;
	}

	// send to all users
	for(int i=0; i<clientcount; i++)
		send(clients[i], buf, buflen, 0);

	free(buf);
}

/**
  * Server side function; receive hello message from client and read username
  * @param[in] sock Socket to use
  * @return Username of client
  */
char* server_recv_hello(const uint8_t* payload, const uint8_t payload_len)
{
	assert(payload != NULL);

	char* name;

	name = malloc(payload_len+1);
	if(name == NULL)
	{
		printf("sender_recv_hello: Out of memory\n");
		exit(EXIT_FAILURE);
	}

	memcpy(name, payload, payload_len);
	name[payload_len] = '\0';

	return name;
}

/**
 * Server side function; deal cards to a client (send hand)
 * @param[in] sock Socket to use
 * @param[in] h Hand to send
 */
void server_deal_cards(int sock, const hand_t *h)
{
	assert(h != NULL);

	uint8_t buf[2+MAX_HAND_CARDS];

	buf[NET_MSG_OFFSET_TYPE] = msg_type_deal_cards;
	buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = MAX_HAND_CARDS;
	for(int i=0; i<MAX_HAND_CARDS; i++)
		buf[NET_MSG_OFFSET_PAYLOAD+i] = h->cards[i];

	send(sock, buf, 2+MAX_HAND_CARDS, 0);
}