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"
27 struct ClientSocket *data;
32 static struct socket_list *sockets = NULL;
33 static char buffer[BUF_SIZ];
35 static void init_sockets() {
36 sockets = malloc(sizeof(*sockets));
39 perror("malloc() failed");
46 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
47 if(sockets == NULL) init_sockets();
48 struct ClientSocket *client = malloc(sizeof(*client));
51 perror("malloc() failed");
54 client->host = strdup(host);
56 client->bind = (bindto ? strdup(bindto) : NULL);
57 client->pass = (pass == NULL ? NULL : strdup(pass));
58 client->nick = strdup(nick);
59 client->ident = strdup(ident);
60 client->realname = strdup(realname);
63 client->bufferpos = 0;
64 client->traffic_in = 0;
65 client->traffic_out = 0;
66 client->connection_time = 0;
70 client->whoqueue_first = NULL;
71 client->whoqueue_last = NULL;
72 client->handleinfo_first = NULL;
73 client->handleinfo_last = NULL;
74 client->next = sockets->data;
75 sockets->data = client;
79 int connect_socket(struct ClientSocket *client) {
80 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
83 struct addrinfo hints, *res;
84 struct sockaddr_in *ip4 = NULL;
85 struct sockaddr_in6 *ip6 = NULL;
86 memset (&hints, 0, sizeof (hints));
87 hints.ai_family = PF_UNSPEC;
88 hints.ai_socktype = SOCK_STREAM;
89 hints.ai_flags |= AI_CANONNAME;
90 if (getaddrinfo (client->host, NULL, &hints, &res)) {
94 switch (res->ai_family) {
96 ip4 = (struct sockaddr_in *) res->ai_addr;
99 ip6 = (struct sockaddr_in6 *) res->ai_addr;
106 sock = socket(AF_INET6, SOCK_STREAM, 0);
108 perror("socket() failed");
112 ip6->sin6_family = AF_INET6;
113 ip6->sin6_port = htons(client->port);
115 struct sockaddr_in6 *ip6vhost = NULL;
116 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
118 switch (res->ai_family) {
120 ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
127 ip6vhost->sin6_family = AF_INET6;
128 ip6vhost->sin6_port = htons(0);
129 bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
132 if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
133 perror("connect() failed");
138 sock = socket(AF_INET, SOCK_STREAM, 0);
140 perror("socket() failed");
144 ip4->sin_family = AF_INET;
145 ip4->sin_port = htons(client->port);
147 struct sockaddr_in *ip4vhost = NULL;
148 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
150 switch (res->ai_family) {
152 ip4vhost = (struct sockaddr_in *) res->ai_addr;
159 ip4vhost->sin_family = AF_INET;
160 ip4vhost->sin_port = htons(0);
161 bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
164 if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
165 perror("connect() failed");
173 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
174 client->connection_time = time(0);
176 if(client->flags & SOCKET_FLAG_SSL) {
180 //send the IRC Headers
184 if(client->pass && strcmp(client->pass, "")) {
185 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
186 write_socket(client, sendBuf, len);
188 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
189 write_socket(client, sendBuf, len);
190 len = sprintf(sendBuf, "NICK %s\n", client->nick);
191 write_socket(client, sendBuf, len);
196 int close_socket(struct ClientSocket *client) {
197 if(client == NULL) return 0;
198 if((client->flags & SOCKET_FLAG_CONNECTED))
200 if(client->flags & SOCKET_FLAG_SSL)
201 ssl_disconnect(client);
202 struct ClientSocket *sock, *last_sock = NULL;
203 for (sock = sockets->data; sock; sock = sock->next) {
206 last_sock->next = sock->next;
208 sockets->data = sock->next;
214 queue_destroy(client);
215 if(client->whoqueue_first)
216 clear_whoqueue(client);
217 if(client->handleinfo_first)
218 clear_handleinfoqueue(client);
228 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
229 printf("[send %d] %s", len, msg);
230 if(!(client->flags & SOCKET_FLAG_SSL) || ssl_write(client, msg, len) == -2) {
232 send(client->sock, msg, len, 0);
234 write(client->sock, msg, len);
237 client->traffic_out += len;
241 int write_socket(struct ClientSocket *client, char* msg, int len) {
242 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
243 if(client->flags & SOCKET_FLAG_USE_QUEUE)
244 return queue_add(client, msg, len);
246 return write_socket_force(client, msg, len);
249 void socket_loop(int timeout_seconds) {
250 if(sockets == NULL) return;
252 struct timeval timeout;
253 struct ClientSocket *sock;
254 int ret = 0, bytes, i;
257 for (sock = sockets->data; sock; sock = sock->next) {
258 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
259 FD_SET(sock->sock, &fds);
263 timeout.tv_sec = timeout_seconds;
265 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
267 for (sock = sockets->data; sock; sock = sock->next) {
268 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
269 if(sock->bufferpos != 0) {
270 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
272 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
274 bytes = read(sock->sock, buffer, sizeof(buffer));
278 for(i = 0; i < bytes; i++) {
279 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
280 sock->buffer[sock->bufferpos + i] = buffer[i];
282 sock->bufferpos += i;
285 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
287 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
289 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
293 sock->bufferpos = bytes;
297 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
298 bot_disconnect(sock);
302 if(sock->flags & SOCKET_FLAG_SSL)
303 ssl_disconnect(sock);
305 sock->traffic_in += bytes;
306 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
307 if(used == sock->bufferpos + 1) {
308 //used all bytes so just reset the bufferpos
311 for(i = 0; i < sock->bufferpos - used; i++) {
312 sock->buffer[i] = sock->buffer[i+used];
314 sock->bufferpos -= used;
317 } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
318 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
319 connect_socket(sock);
326 putsock(struct ClientSocket *client, const char *text, ...)
329 char sendBuf[MAXLEN];
331 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
333 va_start(arg_list, text);
334 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
336 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
338 sendBuf[pos+1] = '\0';
339 write_socket(client, sendBuf, pos+1);
342 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
343 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
344 if(sock == NULL) return NULL;
345 for (; sock; sock = sock->next) {
346 if(!flags || (sock->flags & flags) == flags)
352 void free_sockets() {
354 struct ClientSocket *client, *next;
355 for (client = sockets->data; client; client = next) {
357 if((client->flags & SOCKET_FLAG_CONNECTED))
359 if(client->flags & SOCKET_FLAG_SSL)
360 ssl_disconnect(client);
362 queue_destroy(client);