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;
80 int connect_socket(struct ClientSocket *client) {
81 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
84 struct addrinfo hints, *res;
85 struct sockaddr_in *ip4 = NULL;
86 struct sockaddr_in6 *ip6 = NULL;
87 memset (&hints, 0, sizeof (hints));
88 hints.ai_family = PF_UNSPEC;
89 hints.ai_socktype = SOCK_STREAM;
90 hints.ai_flags |= AI_CANONNAME;
91 if (getaddrinfo (client->host, NULL, &hints, &res)) {
95 switch (res->ai_family) {
97 ip4 = (struct sockaddr_in *) res->ai_addr;
100 ip6 = (struct sockaddr_in6 *) res->ai_addr;
107 sock = socket(AF_INET6, SOCK_STREAM, 0);
109 perror("socket() failed");
113 ip6->sin6_family = AF_INET6;
114 ip6->sin6_port = htons(client->port);
116 struct sockaddr_in6 *ip6vhost = NULL;
117 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
119 switch (res->ai_family) {
121 ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
128 ip6vhost->sin6_family = AF_INET6;
129 ip6vhost->sin6_port = htons(0);
130 bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
133 if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
134 perror("connect() failed");
139 sock = socket(AF_INET, SOCK_STREAM, 0);
141 perror("socket() failed");
145 ip4->sin_family = AF_INET;
146 ip4->sin_port = htons(client->port);
148 struct sockaddr_in *ip4vhost = NULL;
149 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
151 switch (res->ai_family) {
153 ip4vhost = (struct sockaddr_in *) res->ai_addr;
160 ip4vhost->sin_family = AF_INET;
161 ip4vhost->sin_port = htons(0);
162 bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
165 if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
166 perror("connect() failed");
173 if(get_int_field("Sockets.NoDelay")) {
175 if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) == -1) {
176 perror("setsockopt() failed");
182 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
183 client->connection_time = time(0);
185 if(client->flags & SOCKET_FLAG_SSL) {
189 //send the IRC Headers
193 if(client->pass && strcmp(client->pass, "")) {
194 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
195 write_socket(client, sendBuf, len);
197 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
198 write_socket(client, sendBuf, len);
199 len = sprintf(sendBuf, "NICK %s\n", client->nick);
200 write_socket(client, sendBuf, len);
205 int close_socket(struct ClientSocket *client) {
206 if(client == NULL) return 0;
207 if((client->flags & SOCKET_FLAG_CONNECTED))
209 if(client->flags & SOCKET_FLAG_SSL)
210 ssl_disconnect(client);
211 struct ClientSocket *sock, *last_sock = NULL;
212 for (sock = sockets->data; sock; sock = sock->next) {
215 last_sock->next = sock->next;
217 sockets->data = sock->next;
223 queue_destroy(client);
224 if(client->whoqueue_first)
225 clear_whoqueue(client);
226 if(client->handleinfo_first)
227 clear_handleinfoqueue(client);
237 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
238 printf("[send %d] %s", len, msg);
239 if(!(client->flags & SOCKET_FLAG_SSL) || ssl_write(client, msg, len) == -2) {
241 send(client->sock, msg, len, 0);
243 write(client->sock, msg, len);
246 client->traffic_out += len;
250 int write_socket(struct ClientSocket *client, char* msg, int len) {
251 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
252 if(client->flags & SOCKET_FLAG_USE_QUEUE)
253 return queue_add(client, msg, len);
255 return write_socket_force(client, msg, len);
258 void socket_loop(int timeout_seconds) {
259 if(sockets == NULL) return;
261 struct timeval timeout;
262 struct ClientSocket *sock;
263 int ret = 0, bytes, i;
266 for (sock = sockets->data; sock; sock = sock->next) {
267 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
268 FD_SET(sock->sock, &fds);
272 timeout.tv_sec = timeout_seconds;
274 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
276 for (sock = sockets->data; sock; sock = sock->next) {
277 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
278 if(sock->bufferpos != 0) {
279 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
281 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
283 bytes = read(sock->sock, buffer, sizeof(buffer));
287 for(i = 0; i < bytes; i++) {
288 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
289 sock->buffer[sock->bufferpos + i] = buffer[i];
291 sock->bufferpos += i;
294 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
296 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
298 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
302 sock->bufferpos = bytes;
306 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
307 bot_disconnect(sock);
311 if(sock->flags & SOCKET_FLAG_SSL)
312 ssl_disconnect(sock);
314 sock->traffic_in += bytes;
315 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
316 if(used == sock->bufferpos + 1) {
317 //used all bytes so just reset the bufferpos
320 for(i = 0; i < sock->bufferpos - used; i++) {
321 sock->buffer[i] = sock->buffer[i+used];
323 sock->bufferpos -= used;
326 } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
327 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
328 connect_socket(sock);
335 putsock(struct ClientSocket *client, const char *text, ...)
338 char sendBuf[MAXLEN];
340 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
342 va_start(arg_list, text);
343 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
345 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
347 sendBuf[pos+1] = '\0';
348 write_socket(client, sendBuf, pos+1);
351 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
352 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
353 if(sock == NULL) return NULL;
354 for (; sock; sock = sock->next) {
355 if(!flags || (sock->flags & flags) == flags)
361 void free_sockets() {
363 struct ClientSocket *client, *next;
364 for (client = sockets->data; client; client = next) {
366 if((client->flags & SOCKET_FLAG_CONNECTED))
368 if(client->flags & SOCKET_FLAG_SSL)
369 ssl_disconnect(client);
371 queue_destroy(client);