-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsockets.c
More file actions
89 lines (70 loc) · 2.27 KB
/
sockets.c
File metadata and controls
89 lines (70 loc) · 2.27 KB
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <poll.h>
#include "sockets.h"
#include "utility.h"
// Get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
// Return a listening socket
int get_listener_socket(void)
{
int listener; // Listening socket descriptor
int yes=1; // For setsockopt() SO_REUSEADDR, below
int rv;
struct addrinfo hints, *ai, *p;
// Get us a socket and bind it
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL, PORT, &hints, &ai)) != 0) {
fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
exit_with_error(1);
}
for(p = ai; p != NULL; p = p->ai_next) {
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener < 0) {
continue;
}
// Lose the pesky "address already in use" error message
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) {
close(listener);
continue;
}
break;
}
// If we got here, it means we didn't get bound
if (p == NULL) {
return -1;
}
freeaddrinfo(ai); // All done with this
// Listen
if (listen(listener, CONNECTION_QUEUE) == -1) {
return -1;
}
return listener;
}
void add_connection(struct pollfd pfds[], int newfd, int *connection_count)
{
pfds[*connection_count].fd = newfd;
pfds[*connection_count].events = POLLIN; // Check ready-to-read
(*connection_count)++;
}
void remove_connection(struct pollfd pfds[], char client_buffer[][CLIENT_BUFFER_SIZE], int i, int *connection_count)
{
// Copy the one from the end over this one
pfds[i] = pfds[*connection_count-1];
memcpy(&client_buffer[i], &client_buffer[*connection_count], sizeof(*client_buffer));
// clean the buffer for the new empty slot
memset(&client_buffer[*connection_count-1], 0, sizeof(*client_buffer));
(*connection_count)--;
}