implemented bind support (vhost)
[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 *bindto, 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->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);
61     client->user = NULL;
62     client->flags = 0;
63     client->bufferpos = 0;
64     client->traffic_in = 0;
65     client->traffic_out = 0;
66     client->connection_time = 0;
67         client->botid = 0;
68     client->clientid = 0;
69     client->queue = NULL;
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;
76     return client;
77 }
78
79 int connect_socket(struct ClientSocket *client) {
80     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
81     int sock;
82     
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)) {
91         return 0;
92     }
93     while (res) {
94         switch (res->ai_family) {
95         case AF_INET:
96             ip4 = (struct sockaddr_in *) res->ai_addr;
97             break;
98         case AF_INET6:
99             ip6 = (struct sockaddr_in6 *) res->ai_addr;
100             break;
101         }
102         res = res->ai_next;
103     }
104     
105     if(ip6) {
106         sock = socket(AF_INET6, SOCK_STREAM, 0);
107         if(sock == -1) {
108             perror("socket() failed");
109             return 0;
110         }
111         
112         ip6->sin6_family = AF_INET6;
113         ip6->sin6_port = htons(client->port);
114         
115         struct sockaddr_in6 *ip6vhost = NULL;
116         if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
117             while (res) {
118                 switch (res->ai_family) {
119                 case AF_INET6:
120                     ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
121                     break;
122                 }
123                 res = res->ai_next;
124             }
125         }
126         if(ip6vhost) {
127             ip6vhost->sin6_family = AF_INET6;
128             ip6vhost->sin6_port = htons(0);
129             bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
130         }
131         
132         if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
133             perror("connect() failed");
134             return 0;
135         }
136         
137     } else if(ip4) {
138         sock = socket(AF_INET, SOCK_STREAM, 0);
139         if(sock == -1) {
140             perror("socket() failed");
141             return 0;
142         }
143         
144         ip4->sin_family = AF_INET;
145         ip4->sin_port = htons(client->port);
146         
147         struct sockaddr_in *ip4vhost = NULL;
148         if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
149             while (res) {
150                 switch (res->ai_family) {
151                 case AF_INET:
152                     ip4vhost = (struct sockaddr_in *) res->ai_addr;
153                     break;
154                 }
155                 res = res->ai_next;
156             }
157         }
158         if(ip4vhost) {
159             ip4vhost->sin_family = AF_INET;
160             ip4vhost->sin_port = htons(0);
161             bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
162         }
163         
164         if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
165             perror("connect() failed");
166             return 0;
167         }
168         
169     } else
170         return 0;
171     
172     client->sock = sock;
173     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
174     client->connection_time = time(0);
175     
176     if(client->flags & SOCKET_FLAG_SSL) {
177         ssl_connect(client);
178     }
179     
180     //send the IRC Headers
181     char sendBuf[512];
182     int len;
183     
184     if(client->pass && strcmp(client->pass, "")) {
185         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
186         write_socket(client, sendBuf, len);
187     }
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);
192     
193     return 1;
194 }
195
196 int close_socket(struct ClientSocket *client) {
197     if(client == NULL) return 0;
198     if((client->flags & SOCKET_FLAG_CONNECTED))
199         close(client->sock);
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) {
204         if(sock == client) {
205             if(last_sock)
206                 last_sock->next = sock->next;
207             else
208                 sockets->data = sock->next;
209             sockets->count--;
210         } else
211             last_sock = sock;
212     }
213     if(client->queue)
214         queue_destroy(client);
215     if(client->whoqueue_first)
216         clear_whoqueue(client);
217     if(client->handleinfo_first)
218         clear_handleinfoqueue(client);
219     free(client->host);
220     if(client->bind)
221         free(client->bind);
222     if(client->pass)
223         free(client->pass);
224     free(client);
225     return 1;
226 }
227
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) {
231         #ifdef WIN32
232         send(client->sock, msg, len, 0);
233         #else
234         write(client->sock, msg, len);
235         #endif
236     }
237     client->traffic_out += len;
238     return 1;
239 }
240
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);
245     else
246         return write_socket_force(client, msg, len);
247 }
248
249 void socket_loop(int timeout_seconds) {
250     if(sockets == NULL) return;
251     fd_set fds;
252     struct timeval timeout;
253     struct ClientSocket *sock;
254     int ret = 0, bytes, i;
255     
256     FD_ZERO(&fds);
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);
260         if(sock->sock > ret)
261             ret = sock->sock;
262     }
263     timeout.tv_sec = timeout_seconds;
264     timeout.tv_usec = 0;
265     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
266     if(ret == 0) return;
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) {
271                     #ifdef WIN32
272                     bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
273                     #else
274                     bytes = read(sock->sock, buffer, sizeof(buffer));
275                     #endif
276                 }
277                 if(bytes > 0) {
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];
281                     }
282                     sock->bufferpos += i;
283                 }
284             } else {
285                 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
286                     #ifdef WIN32
287                     bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
288                     #else
289                     bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
290                     #endif
291                 }
292                 if(bytes > 0)
293                     sock->bufferpos = bytes;
294             }
295             if(bytes <= 0) {
296                 //error
297                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
298                 bot_disconnect(sock);
299                 if(sock->queue)
300                     queue_destroy(sock);
301                 close(sock->sock);
302                 if(sock->flags & SOCKET_FLAG_SSL)
303                     ssl_disconnect(sock);
304             } else {
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
309                     sock->bufferpos = 0;
310                 } else {
311                     for(i = 0; i < sock->bufferpos - used; i++) {
312                         sock->buffer[i] = sock->buffer[i+used];
313                     }
314                     sock->bufferpos -= used;
315                 }
316             }
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);
320             }
321         }
322     }
323 }
324
325 void
326 putsock(struct ClientSocket *client, const char *text, ...)
327 {
328     va_list arg_list;
329     char sendBuf[MAXLEN];
330     int pos;
331     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
332     sendBuf[0] = '\0';
333     va_start(arg_list, text);
334     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
335     va_end(arg_list);
336     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
337     sendBuf[pos] = '\n';
338     sendBuf[pos+1] = '\0';
339     write_socket(client, sendBuf, pos+1);
340 }
341
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)
347             return sock;
348     }
349     return NULL;
350 }
351
352 void free_sockets() {
353     if(!sockets) return;
354     struct ClientSocket *client, *next;
355     for (client = sockets->data; client; client = next) {
356         next = client->next;
357         if((client->flags & SOCKET_FLAG_CONNECTED))
358             close(client->sock);
359         if(client->flags & SOCKET_FLAG_SSL)
360             ssl_disconnect(client);
361         if(client->queue)
362             queue_destroy(client);
363         free(client->host);
364         if(client->bind)
365             free(client->bind);
366         if(client->pass)
367             free(client->pass);
368         free(client);
369     }
370     free(sockets);
371     sockets = NULL;
372 }