implemented ipv6 support
[NeonServV5.git] / src / ClientSocket.c
1 /* ClientSocket.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17
18 #include "ClientSocket.h"
19 #include "IRCParser.h"
20 #include "UserNode.h"
21 #include "IRCQueue.h"
22 #include "WHOHandler.h"
23 #include "HandleInfoHandler.h"
24 #include "ssl.h"
25
26 struct socket_list {
27     struct ClientSocket *data;
28     unsigned count;
29 };
30
31 //the magic list :P
32 static struct socket_list *sockets = NULL;
33 static char buffer[BUF_SIZ];
34
35 static void init_sockets() {
36     sockets = malloc(sizeof(*sockets));
37     if (!sockets)
38     {
39         perror("malloc() failed");
40         return;
41     }
42     sockets->data = NULL;
43     sockets->count = 0;
44 }
45
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));
49     if (!client)
50     {
51         perror("malloc() failed");
52         return NULL;
53     }
54     client->host = strdup(host);
55     client->port = port;
56     client->pass = (pass == NULL ? NULL : strdup(pass));
57     client->nick = strdup(nick);
58     client->ident = strdup(ident);
59     client->realname = strdup(realname);
60     client->user = NULL;
61     client->flags = 0;
62     client->bufferpos = 0;
63     client->traffic_in = 0;
64     client->traffic_out = 0;
65     client->connection_time = 0;
66         client->botid = 0;
67     client->clientid = 0;
68     client->queue = NULL;
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;
75     return client;
76 }
77
78 int connect_socket(struct ClientSocket *client) {
79     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
80     int sock;
81     
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)) {
90         return 0;
91     }
92     while (res) {
93         switch (res->ai_family) {
94         case AF_INET:
95             ip4 = (struct sockaddr_in *) res->ai_addr;
96             break;
97         case AF_INET6:
98             ip6 = (struct sockaddr_in6 *) res->ai_addr;
99             break;
100         }
101         res = res->ai_next;
102     }
103     
104     if(ip6) {
105         sock = socket(AF_INET6, SOCK_STREAM, 0);
106         if(sock == -1) {
107             perror("socket() failed");
108             return 0;
109         }
110         
111         ip6->sin6_family = AF_INET6;
112         ip6->sin6_port = htons(client->port);
113         
114         if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
115             perror("connect() failed");
116             return 0;
117         }
118         
119     } else if(ip4) {
120         sock = socket(AF_INET, SOCK_STREAM, 0);
121         if(sock == -1) {
122             perror("socket() failed");
123             return 0;
124         }
125         
126         ip4->sin_family = AF_INET;
127         ip4->sin_port = htons(client->port);
128         
129         if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
130             perror("connect() failed");
131             return 0;
132         }
133         
134     } else
135         return 0;
136     
137     client->sock = sock;
138     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
139     client->connection_time = time(0);
140     
141     if(client->flags & SOCKET_FLAG_SSL) {
142         ssl_connect(client);
143     }
144     
145     //send the IRC Headers
146     char sendBuf[512];
147     int len;
148     
149     if(client->pass && strcmp(client->pass, "")) {
150         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
151         write_socket(client, sendBuf, len);
152     }
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);
157     
158     return 1;
159 }
160
161 int close_socket(struct ClientSocket *client) {
162     if(client == NULL) return 0;
163     if((client->flags & SOCKET_FLAG_CONNECTED))
164         close(client->sock);
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) {
169         if(sock == client) {
170             if(last_sock)
171                 last_sock->next = sock->next;
172             else
173                 sockets->data = sock->next;
174             sockets->count--;
175         } else
176             last_sock = sock;
177     }
178     if(client->queue)
179         queue_destroy(client);
180     if(client->whoqueue_first)
181         clear_whoqueue(client);
182     if(client->handleinfo_first)
183         clear_handleinfoqueue(client);
184     free(client->host);
185     free(client->pass);
186     free(client);
187     return 1;
188 }
189
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) {
193         #ifdef WIN32
194         send(client->sock, msg, len, 0);
195         #else
196         write(client->sock, msg, len);
197         #endif
198     }
199     client->traffic_out += len;
200     return 1;
201 }
202
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);
207     else
208         return write_socket_force(client, msg, len);
209 }
210
211 void socket_loop(int timeout_seconds) {
212     if(sockets == NULL) return;
213     fd_set fds;
214     struct timeval timeout;
215     struct ClientSocket *sock;
216     int ret = 0, bytes, i;
217     
218     FD_ZERO(&fds);
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);
222         if(sock->sock > ret)
223             ret = sock->sock;
224     }
225     timeout.tv_sec = timeout_seconds;
226     timeout.tv_usec = 0;
227     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
228     if(ret == 0) return;
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) {
233                     #ifdef WIN32
234                     bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
235                     #else
236                     bytes = read(sock->sock, buffer, sizeof(buffer));
237                     #endif
238                 }
239                 if(bytes > 0) {
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];
243                     }
244                     sock->bufferpos += i;
245                 }
246             } else {
247                 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
248                     #ifdef WIN32
249                     bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
250                     #else
251                     bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
252                     #endif
253                 }
254                 if(bytes > 0)
255                     sock->bufferpos = bytes;
256             }
257             if(bytes <= 0) {
258                 //error
259                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
260                 bot_disconnect(sock);
261                 if(sock->queue)
262                     queue_destroy(sock);
263                 close(sock->sock);
264                 if(sock->flags & SOCKET_FLAG_SSL)
265                     ssl_disconnect(sock);
266             } else {
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
271                     sock->bufferpos = 0;
272                 } else {
273                     for(i = 0; i < sock->bufferpos - used; i++) {
274                         sock->buffer[i] = sock->buffer[i+used];
275                     }
276                     sock->bufferpos -= used;
277                 }
278             }
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);
282             }
283         }
284     }
285 }
286
287 void
288 putsock(struct ClientSocket *client, const char *text, ...)
289 {
290     va_list arg_list;
291     char sendBuf[MAXLEN];
292     int pos;
293     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
294     sendBuf[0] = '\0';
295     va_start(arg_list, text);
296     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
297     va_end(arg_list);
298     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
299     sendBuf[pos] = '\n';
300     sendBuf[pos+1] = '\0';
301     write_socket(client, sendBuf, pos+1);
302 }
303
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)
309             return sock;
310     }
311     return NULL;
312 }
313
314 void free_sockets() {
315     if(!sockets) return;
316     struct ClientSocket *client, *next;
317     for (client = sockets->data; client; client = next) {
318         next = client->next;
319         if((client->flags & SOCKET_FLAG_CONNECTED))
320             close(client->sock);
321         if(client->flags & SOCKET_FLAG_SSL)
322             ssl_disconnect(client);
323         if(client->queue)
324             queue_destroy(client);
325         free(client->host);
326         free(client->pass);
327         free(client);
328     }
329     free(sockets);
330     sockets = NULL;
331 }