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 *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->pass = (pass == NULL ? NULL : strdup(pass));
57 client->nick = strdup(nick);
58 client->ident = strdup(ident);
59 client->realname = strdup(realname);
62 client->bufferpos = 0;
63 client->traffic_in = 0;
64 client->traffic_out = 0;
65 client->connection_time = 0;
69 client->whoqueue_first = NULL;
70 client->whoqueue_last = NULL;
71 client->handleinfo_first = NULL;
72 client->handleinfo_last = NULL;
73 client->next = sockets->data;
74 sockets->data = client;
78 int connect_socket(struct ClientSocket *client) {
79 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
82 struct addrinfo hints, *res;
83 struct sockaddr_in *ip4 = NULL;
84 struct sockaddr_in6 *ip6 = NULL;
85 memset (&hints, 0, sizeof (hints));
86 hints.ai_family = PF_UNSPEC;
87 hints.ai_socktype = SOCK_STREAM;
88 hints.ai_flags |= AI_CANONNAME;
89 if (getaddrinfo (client->host, NULL, &hints, &res)) {
93 switch (res->ai_family) {
95 ip4 = (struct sockaddr_in *) res->ai_addr;
98 ip6 = (struct sockaddr_in6 *) res->ai_addr;
105 sock = socket(AF_INET6, SOCK_STREAM, 0);
107 perror("socket() failed");
111 ip6->sin6_family = AF_INET6;
112 ip6->sin6_port = htons(client->port);
114 if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
115 perror("connect() failed");
120 sock = socket(AF_INET, SOCK_STREAM, 0);
122 perror("socket() failed");
126 ip4->sin_family = AF_INET;
127 ip4->sin_port = htons(client->port);
129 if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
130 perror("connect() failed");
138 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
139 client->connection_time = time(0);
141 if(client->flags & SOCKET_FLAG_SSL) {
145 //send the IRC Headers
149 if(client->pass && strcmp(client->pass, "")) {
150 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
151 write_socket(client, sendBuf, len);
153 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
154 write_socket(client, sendBuf, len);
155 len = sprintf(sendBuf, "NICK %s\n", client->nick);
156 write_socket(client, sendBuf, len);
161 int close_socket(struct ClientSocket *client) {
162 if(client == NULL) return 0;
163 if((client->flags & SOCKET_FLAG_CONNECTED))
165 if(client->flags & SOCKET_FLAG_SSL)
166 ssl_disconnect(client);
167 struct ClientSocket *sock, *last_sock = NULL;
168 for (sock = sockets->data; sock; sock = sock->next) {
171 last_sock->next = sock->next;
173 sockets->data = sock->next;
179 queue_destroy(client);
180 if(client->whoqueue_first)
181 clear_whoqueue(client);
182 if(client->handleinfo_first)
183 clear_handleinfoqueue(client);
190 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
191 printf("[send %d] %s", len, msg);
192 if(!(client->flags & SOCKET_FLAG_SSL) || ssl_write(client, msg, len) == -2) {
194 send(client->sock, msg, len, 0);
196 write(client->sock, msg, len);
199 client->traffic_out += len;
203 int write_socket(struct ClientSocket *client, char* msg, int len) {
204 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
205 if(client->flags & SOCKET_FLAG_USE_QUEUE)
206 return queue_add(client, msg, len);
208 return write_socket_force(client, msg, len);
211 void socket_loop(int timeout_seconds) {
212 if(sockets == NULL) return;
214 struct timeval timeout;
215 struct ClientSocket *sock;
216 int ret = 0, bytes, i;
219 for (sock = sockets->data; sock; sock = sock->next) {
220 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
221 FD_SET(sock->sock, &fds);
225 timeout.tv_sec = timeout_seconds;
227 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
229 for (sock = sockets->data; sock; sock = sock->next) {
230 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
231 if(sock->bufferpos != 0) {
232 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
234 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
236 bytes = read(sock->sock, buffer, sizeof(buffer));
240 for(i = 0; i < bytes; i++) {
241 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
242 sock->buffer[sock->bufferpos + i] = buffer[i];
244 sock->bufferpos += i;
247 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
249 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
251 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
255 sock->bufferpos = bytes;
259 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
260 bot_disconnect(sock);
264 if(sock->flags & SOCKET_FLAG_SSL)
265 ssl_disconnect(sock);
267 sock->traffic_in += bytes;
268 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
269 if(used == sock->bufferpos + 1) {
270 //used all bytes so just reset the bufferpos
273 for(i = 0; i < sock->bufferpos - used; i++) {
274 sock->buffer[i] = sock->buffer[i+used];
276 sock->bufferpos -= used;
279 } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
280 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
281 connect_socket(sock);
288 putsock(struct ClientSocket *client, const char *text, ...)
291 char sendBuf[MAXLEN];
293 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
295 va_start(arg_list, text);
296 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
298 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
300 sendBuf[pos+1] = '\0';
301 write_socket(client, sendBuf, pos+1);
304 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
305 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
306 if(sock == NULL) return NULL;
307 for (; sock; sock = sock->next) {
308 if(!flags || (sock->flags & flags) == flags)
314 void free_sockets() {
316 struct ClientSocket *client, *next;
317 for (client = sockets->data; client; client = next) {
319 if((client->flags & SOCKET_FLAG_CONNECTED))
321 if(client->flags & SOCKET_FLAG_SSL)
322 ssl_disconnect(client);
324 queue_destroy(client);