-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.c
179 lines (162 loc) · 4.2 KB
/
socket.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
/*
* This file is part of Aquarium Control Center (aquacc).
*
* Copyright (C) 2012-2013 Cluytens David <david.cluytens@gmail.com>
*
* Aquarium Control Center (aquacc) is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Aquarium Control Center (aquacc) 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Aquarium Control Center (aquacc). If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <syslog.h>
#include <time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <termios.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "aquacc.h"
#include "socket.h"
#include "fd_list.h"
#include "fd_event.h"
aq_socket_t socks[MAX_SOCKETS];
extern int errno;
int makeSocket (int16_t port) {
int on = 1;
int sock;
struct sockaddr_in name;
/* Create the socket. */
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
/* Give the socket a name. */
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind (sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
return sock;
}
/* Connection request on original socket. */
int acceptSocket(int sock) {
int new;
socklen_t size;
struct sockaddr_in clientname;
char *host;
uint16_t port;
size = sizeof(clientname);
new = accept(sock, (struct sockaddr *)&clientname, &size);
if (new < 0) {
perror("accept");
return -1;
}
host = inet_ntoa(clientname.sin_addr);
port = ntohs(clientname.sin_port);
fprintf(stderr, "Server: connect from host %s, port %u.\n", host, port);
return new;
}
void initSocket(void) {
memset(socks, 0, sizeof(socks));
return;
}
int addSocket(int fd, socket_type_t socket_type) {
size_t i;
for (i = 0; i < MAX_SOCKETS; i++) {
if (!socks[i].free) {
socks[i].id = i;
socks[i].socket_type = socket_type;
socks[i].free = 1;
socks[i].fd = fd;
socks[i].has_w_data = 0;
socks[i].has_r_data = 0;
socks[i].r_len = 0;
socks[i].w_len = 0;
return i;
}
}
fprintf(stderr, "Error: No free socket found\n");
return -1;
}
int freeSocket(int fd) {
size_t i;
for (i = 0; i < MAX_SOCKETS; i++) {
if (socks[i].fd == fd) {
socks[i].free = 0;
socks[i].fd = 0;
socks[i].has_w_data = 0;
socks[i].has_r_data = 0;
socks[i].r_len = 0;
socks[i].w_len = 0;
return i;
}
}
return -1;
}
int writeSocket(int fd) {
int ret;
size_t i;
for (i = 0; i < MAX_SOCKETS; i++) {
if ((socks[i].free) && (socks[i].fd == fd)) {
ret = writen_ni(socks[i].fd, socks[i].w_buffer, socks[i].w_len);
socks[i].has_w_data = 0;
socks[i].w_len = 0;
return ret;
}
}
return -1;
}
int readSocket(int fd) {
int nbytes = 0;
size_t i;
for (i = 0; i < MAX_SOCKETS; i++) {
if ((socks[i].free) && (socks[i].fd == fd)) {
do {
if (socks[i].has_r_data) {
nbytes = read(socks[i].fd,
socks[i].r_buffer + socks[i].r_len,
sizeof(socks[i].r_buffer) - socks[i].r_len);
socks[i].r_len += nbytes;
} else {
nbytes = read(socks[i].fd, socks[i].r_buffer, sizeof(socks[i].r_buffer));
socks[i].r_len = nbytes;
}
} while (nbytes < 0 && errno == EINTR);
socks[i].r_buffer[nbytes] = '\0';
socks[i].has_r_data = 1;
return nbytes;
}
}
return -1;
}
int listenSocket(int sockfd, int backlog) {
return(listen(sockfd, backlog));
}
int closeSocket(int fd) {
return(close(fd));
}