src/network.c

Mon, 21 Mar 2016 20:35:58 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 21 Mar 2016 20:35:58 +0100
changeset 56
11b57a15d11a
parent 55
54ea19938d57
child 59
3fa1de896666
permissions
-rw-r--r--

fixed hypthetical bug, where the client address is not correctly retrieved by accept()

1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
55
54ea19938d57 updated copyright and version info
Mike Becker <universe@uap-core.de>
parents: 48
diff changeset
4 * Copyright 2016 Mike Becker. All rights reserved.
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include <stdlib.h>
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include <string.h>
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include "network.h"
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
34 #define new_socket() socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
35
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
36 int net_create(Server *server, char* port) {
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
37 server->info = NULL;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
38
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
39 struct sockaddr_in addr;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
40 addr.sin_family = AF_INET;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
41 addr.sin_addr.s_addr = INADDR_ANY;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
42 addr.sin_port = htons(atoi(port));
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
43
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
44 server->fd = new_socket();
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 if (server->fd > -1) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 int true = 1;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 setsockopt(server->fd, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int));
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
48 if (bind(server->fd, (struct sockaddr*)&addr, sizeof(addr))) {
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 server->fd = -1;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 return 1;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 } else {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 return 0;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 } else {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 return 1;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58
48
0cedda2544da added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
59 static int getaddrinfo_intrnl(char *host, char *port, struct addrinfo **info) {
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 struct addrinfo hints;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 memset(&hints, 0, sizeof(hints));
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 hints.ai_socktype = SOCK_STREAM;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 hints.ai_protocol = IPPROTO_TCP;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 return getaddrinfo(host, port, &hints, info);
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 int net_find(Server *server, char *host, char* port) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 memset(server, 0, sizeof(Server));
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 server->fd = -1;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 return getaddrinfo_intrnl(host, port, &(server->info));
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 int net_listen(Server *server) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 listen(server->fd, 1);
56
11b57a15d11a fixed hypthetical bug, where the client address is not correctly retrieved by accept()
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
77 Client* client = malloc(sizeof(Client));
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 client->fd = -1;
56
11b57a15d11a fixed hypthetical bug, where the client address is not correctly retrieved by accept()
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
79 client->address_len = sizeof(client->address);
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 server->client = client;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 client->fd = accept(server->fd,
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 &(client->address), &(client->address_len));
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 return client->fd == -1;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 int net_connect(Server *server) {
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
89
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
90 Client* client = calloc(1, sizeof(Client));
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
91 client->fd = -1;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
92 server->fd = new_socket();
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
93 server->client = client;
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
94
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
95 if (server->fd == -1) {
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 return 1;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
99 return connect(server->fd, server->info->ai_addr, server->info->ai_addrlen);
1
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 int net_destroy(Server *server) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 if (server->info) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 freeaddrinfo(server->info);
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 if (server->client) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 shutdown(server->client->fd, SHUT_RDWR);
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 free(server->client);
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 if (server->fd > -1) {
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 return shutdown(server->fd, SHUT_RDWR);
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 }
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 return EXIT_SUCCESS;
e5fbb8f9edbe added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 }
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
116
22
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
117 void net_send_code(int socket, uint8_t code) {
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
118 send(socket, &code, sizeof(uint8_t), 0);
3
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
119 }
3693fd2eb0e9 changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
120
22
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
121 void net_send_data(int socket, uint8_t code, void *data, size_t len) {
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
122 uint8_t pkg[len+1];
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
123 pkg[0] = code;
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
124 memcpy(pkg+1, data, len);
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
125 send(socket, pkg, len+1, 0);
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
126 }
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
127
22
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
128 uint8_t net_recieve_code(int socket) {
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
129 uint8_t code;
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
130 if (recv(socket, &code, sizeof(code), 0) == sizeof(code)) {
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
131 return code;
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
132 } else {
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
133 return NETCODE_CONNLOST;
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
134 }
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
135 }
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
136
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
137 void net_recieve_data(int socket, void *data, size_t len) {
45
e14a1d9aa91d removed nonblocking sockets for causing problems and not being necessary
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
138 recv(socket, data, len, MSG_WAITALL);
2
0a08f79c320d fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
139 }

mercurial