1 /* ClientSocket.c - NeonServ v5.2
2 * Copyright (C) 2011 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "ClientSocket.h"
19 #include "IRCParser.h"
22 #include "WHOHandler.h"
23 #include "HandleInfoHandler.h"
25 #include "ConfigParser.h"
28 struct ClientSocket *data;
33 static struct socket_list *sockets = NULL;
34 static char buffer[BUF_SIZ];
36 static void init_sockets() {
37 sockets = malloc(sizeof(*sockets));
40 perror("malloc() failed");
47 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
48 if(sockets == NULL) init_sockets();
49 struct ClientSocket *client = malloc(sizeof(*client));
52 perror("malloc() failed");
55 client->host = strdup(host);
57 client->bind = (bindto ? strdup(bindto) : NULL);
58 client->pass = (pass == NULL ? NULL : strdup(pass));
59 client->nick = strdup(nick);
60 client->ident = strdup(ident);
61 client->realname = strdup(realname);
64 client->bufferpos = 0;
65 client->traffic_in = 0;
66 client->traffic_out = 0;
67 client->connection_time = 0;
71 client->whoqueue_first = NULL;
72 client->whoqueue_last = NULL;
73 client->handleinfo_first = NULL;
74 client->handleinfo_last = NULL;
75 client->next = sockets->data;
76 sockets->data = client;
81 int connect_socket(struct ClientSocket *client) {
82 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
85 struct addrinfo hints, *res;
86 struct sockaddr_in *ip4 = NULL;
87 struct sockaddr_in6 *ip6 = NULL;
88 memset (&hints, 0, sizeof (hints));
89 hints.ai_family = PF_UNSPEC;
90 hints.ai_socktype = SOCK_STREAM;
91 hints.ai_flags |= AI_CANONNAME;
92 if (getaddrinfo (client->host, NULL, &hints, &res)) {
96 switch (res->ai_family) {
98 ip4 = (struct sockaddr_in *) res->ai_addr;
101 ip6 = (struct sockaddr_in6 *) res->ai_addr;
108 sock = socket(AF_INET6, SOCK_STREAM, 0);
110 perror("socket() failed");
114 ip6->sin6_family = AF_INET6;
115 ip6->sin6_port = htons(client->port);
117 struct sockaddr_in6 *ip6vhost = NULL;
118 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
120 switch (res->ai_family) {
122 ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
129 ip6vhost->sin6_family = AF_INET6;
130 ip6vhost->sin6_port = htons(0);
131 bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
134 if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
135 perror("connect() failed");
140 sock = socket(AF_INET, SOCK_STREAM, 0);
142 perror("socket() failed");
146 ip4->sin_family = AF_INET;
147 ip4->sin_port = htons(client->port);
149 struct sockaddr_in *ip4vhost = NULL;
150 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
152 switch (res->ai_family) {
154 ip4vhost = (struct sockaddr_in *) res->ai_addr;
161 ip4vhost->sin_family = AF_INET;
162 ip4vhost->sin_port = htons(0);
163 bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
166 if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
167 perror("connect() failed");
174 if(get_int_field("Sockets.NoDelay")) {
176 if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) == -1) {
177 perror("setsockopt() failed");
183 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
184 client->connection_time = time(0);
186 if(client->flags & SOCKET_FLAG_SSL) {
190 //send the IRC Headers
194 if(client->pass && strcmp(client->pass, "")) {
195 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
196 write_socket(client, sendBuf, len);
198 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
199 write_socket(client, sendBuf, len);
200 len = sprintf(sendBuf, "NICK %s\n", client->nick);
201 write_socket(client, sendBuf, len);
206 int connect_socket(struct ClientSocket *client) {
207 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
208 struct hostent *host;
209 struct sockaddr_in addr;
211 addr.sin_addr.s_addr = inet_addr(client->host);
212 if (addr.sin_addr.s_addr == INADDR_NONE) {
213 host = gethostbyname(client->host);
217 memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
219 sock = socket(PF_INET, SOCK_STREAM, 0);
222 perror("socket() failed");
226 addr.sin_port = htons(client->port);
227 addr.sin_family = AF_INET;
229 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
231 perror("connect() failed");
236 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
237 client->connection_time = time(0);
240 if(client->flags & SOCKET_FLAG_SSL) {
245 //send the IRC Headers
249 if(client->pass && strcmp(client->pass, "")) {
250 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
251 write_socket(client, sendBuf, len);
253 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
254 write_socket(client, sendBuf, len);
255 len = sprintf(sendBuf, "NICK %s\n", client->nick);
256 write_socket(client, sendBuf, len);
262 int close_socket(struct ClientSocket *client) {
263 if(client == NULL) return 0;
264 if((client->flags & SOCKET_FLAG_CONNECTED))
266 if(client->flags & SOCKET_FLAG_SSL)
267 ssl_disconnect(client);
268 struct ClientSocket *sock, *last_sock = NULL;
269 for (sock = sockets->data; sock; sock = sock->next) {
272 last_sock->next = sock->next;
274 sockets->data = sock->next;
280 queue_destroy(client);
281 if(client->whoqueue_first)
282 clear_whoqueue(client);
283 if(client->handleinfo_first)
284 clear_handleinfoqueue(client);
294 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
295 printf("[send %d] %s", len, msg);
296 if(!(client->flags & SOCKET_FLAG_SSL) || ssl_write(client, msg, len) == -2) {
298 send(client->sock, msg, len, 0);
300 write(client->sock, msg, len);
303 client->traffic_out += len;
307 int write_socket(struct ClientSocket *client, char* msg, int len) {
308 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
309 if(client->flags & SOCKET_FLAG_USE_QUEUE)
310 return queue_add(client, msg, len);
312 return write_socket_force(client, msg, len);
315 void socket_loop(int timeout_seconds) {
316 if(sockets == NULL) return;
318 struct timeval timeout;
319 struct ClientSocket *sock;
320 int ret = 0, bytes, i;
323 for (sock = sockets->data; sock; sock = sock->next) {
324 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
325 FD_SET(sock->sock, &fds);
329 timeout.tv_sec = timeout_seconds;
331 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
333 for (sock = sockets->data; sock; sock = sock->next) {
334 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
335 if(sock->bufferpos != 0) {
336 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
338 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
340 bytes = read(sock->sock, buffer, sizeof(buffer));
344 for(i = 0; i < bytes; i++) {
345 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
346 sock->buffer[sock->bufferpos + i] = buffer[i];
348 sock->bufferpos += i;
351 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
353 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
355 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
359 sock->bufferpos = bytes;
363 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
364 bot_disconnect(sock);
368 if(sock->flags & SOCKET_FLAG_SSL)
369 ssl_disconnect(sock);
371 sock->traffic_in += bytes;
372 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
373 if(used == sock->bufferpos + 1) {
374 //used all bytes so just reset the bufferpos
377 for(i = 0; i < sock->bufferpos - used; i++) {
378 sock->buffer[i] = sock->buffer[i+used];
380 sock->bufferpos -= used;
383 } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
384 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
385 connect_socket(sock);
392 putsock(struct ClientSocket *client, const char *text, ...)
395 char sendBuf[MAXLEN];
397 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
399 va_start(arg_list, text);
400 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
402 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
404 sendBuf[pos+1] = '\0';
405 write_socket(client, sendBuf, pos+1);
408 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
409 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
410 if(sock == NULL) return NULL;
411 for (; sock; sock = sock->next) {
412 if(!flags || (sock->flags & flags) == flags)
418 void free_sockets() {
420 struct ClientSocket *client, *next;
421 for (client = sockets->data; client; client = next) {
423 if((client->flags & SOCKET_FLAG_CONNECTED))
425 if(client->flags & SOCKET_FLAG_SSL)
426 ssl_disconnect(client);
428 queue_destroy(client);