/
bang-net.c
261 lines (226 loc) · 6.24 KB
/
bang-net.c
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* \file bang-net.c
* \author Nikhil Bysani
* \date December 20, 2008
*
* \brief Implementation of the networking behind the BANG project.
*/
#include"bang-net.h"
#include"bang-signals.h"
#include<assert.h>
#include<errno.h>
#include<netdb.h>
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
/**
* TODO: currently only one server can be run a time, good or bad?
*/
/**
* The server thread.
*/
static pthread_t *server_thread = NULL;
static char *port = DEFAULT_PORT;
static int sock = -1;
static pthread_mutex_t server_status_lock;
/*
* Not being used because cleanup doesn't pass warnigns for some reason.
static void free_server_addrinfo(void *result) {
freeaddrinfo((struct addrinfo*)result);
}
*/
void* BANG_server_thread(void *not_used) {
not_used = NULL;
assert(port != NULL);
assert(sock == -1);
struct addrinfo hints;
struct addrinfo *result, *rp;
BANG_sigargs args;
/*
* sets the hints of getaddrinfo so it know what kind of address we want
* basic template of code from "man 2 getaddrinfo" section
*
* getaddrinfo is NOT C99, annoying to say the least.
*/
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
//check to see if we got available addresses
if (getaddrinfo(NULL,port,&hints,&result) != 0) {
BANG_send_signal(BANG_GADDRINFO_FAIL,NULL,0);
return NULL;
}
/*
* Let me rant about cleanup push. First of all, it is a macro. WTF!?!?
* Second of all it must have a matching pair pthread_cleanup_pop
* Third of all, it means that anything between those two lines is
* in a big do{}while(0). Seriously. god damn.
*/
/*pthread_cleanup_push(free_server_addrinfo,result);*/
for (rp = result; rp != NULL; rp = rp->ai_next) {
sock = socket(rp->ai_family,rp->ai_socktype,rp->ai_protocol);
if (sock == -1) {
/* TODO: Send something more meaningful */
BANG_send_signal(BANG_BIND_FAIL,NULL,0);
} else if (bind(sock,rp->ai_addr,rp->ai_addrlen) == 0) {
/* TODO: Send something more meaningful */
BANG_send_signal(BANG_BIND_SUC,NULL,0);
break;
}
}
/* check to see if we could bind to a socket */
if (rp == NULL) {
close(sock);
/* TODO: Send something more meaningful */
BANG_send_signal(BANG_BIND_FAIL,NULL,0);
return NULL;
}
/* mark the socket for listening */
if (listen(sock,MAX_BACKLOG) != 0) {
close(sock);
/* TODO: Send something more meaningful */
BANG_send_signal(BANG_LISTEN_FAIL,NULL,0);
return NULL;
}
BANG_send_signal(BANG_SERVER_STARTED,NULL,0);
/* accepted client */
args.length = sizeof(int);
int accptsock;
while (1) {
accptsock = accept(sock,rp->ai_addr,&rp->ai_addrlen);
args.args = calloc(1,sizeof(int));
*((int*)args.args) = accptsock;
BANG_send_signal(BANG_PEER_CONNECTED,&args,1);
free(args.args);
}
/*pthread_cleanup_pop(1);*/
close(sock);
return NULL;
}
void* BANG_connect_thread(void *addr) {
struct addrinfo hints;
struct addrinfo *result, *rp;
BANG_sigargs args;
/*
* sets the hints of getaddrinfo so it know what kind of address we want
* basic template of code from "man 2 getaddrinfo" section
*/
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; //don't about ipv4 or ipv6
hints.ai_socktype = SOCK_STREAM;//tcp
hints.ai_flags = AI_PASSIVE; //for wildcard IP address
hints.ai_protocol = 0; //any protocol
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
/* check to see if we got available addresses */
if (getaddrinfo(NULL,(char*)addr,&hints,&result) != 0) {
args.args = addr;
args.length = strlen(addr) * sizeof(char);
/*
* TODO: Send something more meaningful
*/
BANG_send_signal(BANG_GADDRINFO_FAIL,&args,1);
free(args.args);
return NULL;
}
args.args = calloc(1,sizeof(int));
args.length = sizeof(int);
for (rp = result; rp != NULL; rp = rp->ai_next) {
*((int*)args.args) = socket(rp->ai_family,rp->ai_socktype,rp->ai_protocol);
if (*((int*)args.args) == -1) {
free(args.args);
args.args = addr;
args.length = strlen(addr) * sizeof(char);
/*
* TODO:Make this signal send out something more useful
*/
BANG_send_signal(BANG_CONNECT_FAIL,&args,1);
free(args.args);
} else if (connect(*((int*)args.args),rp->ai_addr,rp->ai_addrlen) == 0) {
/*
* Sends out a signal of the peer with its socket.
*/
BANG_send_signal(BANG_PEER_CONNECTED,&args,1);
free(args.args);
break;
}
}
freeaddrinfo(result);
return NULL;
}
void BANG_server_start(char *server_port) {
pthread_mutex_lock(&server_status_lock);
if (server_port != NULL)
port = server_port;
if (server_thread == NULL) {
#ifdef BDEBUG_1
fprintf(stderr,"Starting server.\n");
#endif
server_thread = (pthread_t*) calloc(1,sizeof(pthread_t));
pthread_create(server_thread,NULL,BANG_server_thread,NULL);
}
pthread_mutex_unlock(&server_status_lock);
}
void BANG_server_set_port(char *new_port) {
pthread_mutex_lock(&server_status_lock);
if (new_port != NULL && !strcmp(port,new_port)) {
port = new_port;
}
pthread_mutex_unlock(&server_status_lock);
}
void BANG_server_stop() {
pthread_mutex_lock(&server_status_lock);
if (server_thread != NULL) {
#ifdef BDEBUG_1
fprintf(stderr,"Stoping server.\n");
#endif
pthread_cancel(*server_thread);
pthread_join(*server_thread,NULL);
if (sock != -1) {
close(sock);
sock = -1;
}
free(server_thread);
server_thread = NULL;
BANG_send_signal(BANG_SERVER_STOPPED,NULL,0);
}
pthread_mutex_unlock(&server_status_lock);
}
char BANG_is_server_running(){
pthread_mutex_lock(&server_status_lock);
if (server_thread) {
pthread_mutex_unlock(&server_status_lock);
return 1;
}
pthread_mutex_unlock(&server_status_lock);
return 0;
}
void BANG_net_init(char *server_port ,char start_server) {
#ifdef BDEBUG_1
fprintf(stderr,"BANG net starting.\n");
#endif
if (server_port != NULL) {
port = server_port;
}
pthread_mutex_init(&server_status_lock,NULL);
if (start_server) {
BANG_server_start(NULL);
}
}
void BANG_net_close() {
#ifdef BDEBUG_1
fprintf(stderr,"BANG net closing.\n");
#endif
BANG_server_stop();
pthread_mutex_destroy(&server_status_lock);
}