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) {
188 client->flags |= SOCKET_FLAG_HAVE_SSL;
191 //send the IRC Headers
195 if(client->pass && strcmp(client->pass, "")) {
196 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
197 write_socket(client, sendBuf, len);
199 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
200 write_socket(client, sendBuf, len);
201 len = sprintf(sendBuf, "NICK %s\n", client->nick);
202 write_socket(client, sendBuf, len);
207 int connect_socket(struct ClientSocket *client) {
208 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
209 struct hostent *host;
210 struct sockaddr_in addr;
212 addr.sin_addr.s_addr = inet_addr(client->host);
213 if (addr.sin_addr.s_addr == INADDR_NONE) {
214 host = gethostbyname(client->host);
218 memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
220 sock = socket(PF_INET, SOCK_STREAM, 0);
223 perror("socket() failed");
227 addr.sin_port = htons(client->port);
228 addr.sin_family = AF_INET;
230 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
232 perror("connect() failed");
237 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
238 client->connection_time = time(0);
241 if(client->flags & SOCKET_FLAG_SSL) {
243 client->flags |= SOCKET_FLAG_HAVE_SSL;
247 //send the IRC Headers
251 if(client->pass && strcmp(client->pass, "")) {
252 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
253 write_socket(client, sendBuf, len);
255 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
256 write_socket(client, sendBuf, len);
257 len = sprintf(sendBuf, "NICK %s\n", client->nick);
258 write_socket(client, sendBuf, len);
264 int close_socket(struct ClientSocket *client) {
265 if(client == NULL) return 0;
266 if((client->flags & SOCKET_FLAG_CONNECTED))
268 if(client->flags & SOCKET_FLAG_HAVE_SSL)
269 ssl_disconnect(client);
270 struct ClientSocket *sock, *last_sock = NULL;
271 for (sock = sockets->data; sock; sock = sock->next) {
274 last_sock->next = sock->next;
276 sockets->data = sock->next;
282 queue_destroy(client);
283 if(client->whoqueue_first)
284 clear_whoqueue(client);
285 if(client->handleinfo_first)
286 clear_handleinfoqueue(client);
296 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
297 printf("[send %d] %s", len, msg);
298 if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) {
300 send(client->sock, msg, len, 0);
302 write(client->sock, msg, len);
305 client->traffic_out += len;
309 int write_socket(struct ClientSocket *client, char* msg, int len) {
310 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
311 if(client->flags & SOCKET_FLAG_USE_QUEUE)
312 return queue_add(client, msg, len);
314 return write_socket_force(client, msg, len);
317 void socket_loop(int timeout_seconds) {
318 if(sockets == NULL) return;
320 struct timeval timeout;
321 struct ClientSocket *sock;
322 int ret = 0, bytes, i;
325 for (sock = sockets->data; sock; sock = sock->next) {
326 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
327 FD_SET(sock->sock, &fds);
331 timeout.tv_sec = timeout_seconds;
333 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
335 for (sock = sockets->data; sock; sock = sock->next) {
336 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
337 if(sock->bufferpos != 0) {
338 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
340 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
342 bytes = read(sock->sock, buffer, sizeof(buffer));
346 for(i = 0; i < bytes; i++) {
347 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
348 sock->buffer[sock->bufferpos + i] = buffer[i];
350 sock->bufferpos += i;
353 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
355 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
357 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
361 sock->bufferpos = bytes;
365 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
366 bot_disconnect(sock);
370 if(sock->flags & SOCKET_FLAG_HAVE_SSL)
371 ssl_disconnect(sock);
373 sock->traffic_in += bytes;
374 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
375 if(used == sock->bufferpos + 1) {
376 //used all bytes so just reset the bufferpos
379 for(i = 0; i < sock->bufferpos - used; i++) {
380 sock->buffer[i] = sock->buffer[i+used];
382 sock->bufferpos -= used;
385 } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
386 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
387 connect_socket(sock);
394 putsock(struct ClientSocket *client, const char *text, ...)
397 char sendBuf[MAXLEN];
399 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
401 va_start(arg_list, text);
402 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
404 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
406 sendBuf[pos+1] = '\0';
407 write_socket(client, sendBuf, pos+1);
410 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
411 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
412 if(sock == NULL) return NULL;
413 for (; sock; sock = sock->next) {
414 if(!flags || (sock->flags & flags) == flags)
420 void free_sockets() {
422 struct ClientSocket *client, *next;
423 for (client = sockets->data; client; client = next) {
425 if((client->flags & SOCKET_FLAG_CONNECTED))
427 if(client->flags & SOCKET_FLAG_HAVE_SSL)
428 ssl_disconnect(client);
430 queue_destroy(client);