a46a4bf82d1bbc2e4d1b4ab8c64436c99e0a0f52
[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
25 struct socket_list {
26     struct ClientSocket *data;
27     unsigned count;
28 };
29
30 //the magic list :P
31 static struct socket_list *sockets = NULL;
32 static char buffer[BUF_SIZ];
33
34 static void init_sockets() {
35     sockets = malloc(sizeof(*sockets));
36     if (!sockets)
37     {
38         perror("malloc() failed");
39         return;
40     }
41     sockets->data = NULL;
42     sockets->count = 0;
43 }
44
45 struct ClientSocket* create_socket(char *host, int port, char *pass, char *nick, char *ident, char *realname) {
46     if(sockets == NULL) init_sockets();
47     struct ClientSocket *client = malloc(sizeof(*client));
48     if (!client)
49     {
50         perror("malloc() failed");
51         return NULL;
52     }
53     client->host = strdup(host);
54     client->port = port;
55     printf("Connect: %s:%d\n", client->host, client->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 #ifdef WIN32
79
80 int connect_socket(struct ClientSocket *client) {
81     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
82     struct hostent *host;
83     struct sockaddr_in addr;
84     int sock;
85     addr.sin_addr.s_addr = inet_addr(client->host);
86     if (addr.sin_addr.s_addr == INADDR_NONE) {
87         host = gethostbyname(client->host);
88         if(!host) {
89             return SOCKET_ERROR;
90         }
91         memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
92     }
93     sock = socket(PF_INET, SOCK_STREAM, 0);
94     if (sock == -1)
95     {
96         perror("socket() failed");
97         return 0;
98     }
99
100     addr.sin_port = htons(client->port);
101     addr.sin_family = AF_INET;
102
103     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
104     {
105         perror("connect() failed");
106         return 0;
107     }
108
109     client->sock = sock;
110     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
111     client->connection_time = time(0);
112
113     //send the IRC Headers
114     char sendBuf[512];
115     int len;
116
117     if(client->pass && strcmp(client->pass, "")) {
118         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
119         write_socket(client, sendBuf, len);
120     }
121     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
122     write_socket(client, sendBuf, len);
123     len = sprintf(sendBuf, "NICK %s\n", client->nick);
124     write_socket(client, sendBuf, len);
125
126     return 1;
127 }
128
129 #else
130
131 int connect_socket(struct ClientSocket *client) {
132     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
133     struct hostent *host;
134     struct sockaddr_in addr;
135     int sock;
136     if (!inet_aton(client->host, &addr.sin_addr))
137     {
138         host = gethostbyname(client->host);
139         if (!host)
140         {
141             perror("gethostbyname() failed");
142             return 0;
143         }
144         addr.sin_addr = *(struct in_addr*)host->h_addr;
145     }
146     sock = socket(PF_INET, SOCK_STREAM, 0);
147     if (sock == -1)
148     {
149         perror("socket() failed");
150         return 0;
151     }
152
153     addr.sin_port = htons(client->port);
154     addr.sin_family = AF_INET;
155
156     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
157     {
158         perror("connect() failed");
159         return 0;
160     }
161
162     client->sock = sock;
163     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
164     client->connection_time = time(0);
165
166     //send the IRC Headers
167     char sendBuf[512];
168     int len;
169
170     if(client->pass && strcmp(client->pass, "")) {
171         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
172         write_socket(client, sendBuf, len);
173     }
174     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
175     write_socket(client, sendBuf, len);
176     len = sprintf(sendBuf, "NICK %s\n", client->nick);
177     write_socket(client, sendBuf, len);
178     
179     return 1;
180 }
181
182 #endif
183
184 int close_socket(struct ClientSocket *client) {
185     if(client == NULL) return 0;
186     if((client->flags & SOCKET_FLAG_CONNECTED))
187         close(client->sock);
188     struct ClientSocket *sock, *last_sock = NULL;
189     for (sock = sockets->data; sock; sock = sock->next) {
190         if(sock == client) {
191             if(last_sock)
192                 last_sock->next = sock->next;
193             else
194                 sockets->data = sock->next;
195             sockets->count--;
196         } else
197             last_sock = sock;
198     }
199     if(client->queue)
200         queue_destroy(client);
201     if(client->whoqueue_first)
202         clear_whoqueue(client);
203     if(client->handleinfo_first)
204         clear_handleinfoqueue(client);
205     free(client->host);
206     free(client->pass);
207     free(client);
208     return 1;
209 }
210
211 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
212     printf("[send %d] %s", len, msg);
213     #ifdef WIN32
214     send(client->sock, msg, len, 0);
215     #else
216     write(client->sock, msg, len);
217     #endif
218     client->traffic_out += len;
219     return 1;
220 }
221
222 int write_socket(struct ClientSocket *client, char* msg, int len) {
223     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
224     if(client->flags & SOCKET_FLAG_USE_QUEUE)
225         return queue_add(client, msg, len);
226     else
227         return write_socket_force(client, msg, len);
228 }
229
230 void socket_loop(int timeout_seconds) {
231     if(sockets == NULL) return;
232     fd_set fds;
233     struct timeval timeout;
234     struct ClientSocket *sock;
235     int ret = 0, bytes, i;
236     
237     FD_ZERO(&fds);
238     for (sock = sockets->data; sock; sock = sock->next) {
239         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
240         FD_SET(sock->sock, &fds);
241         if(sock->sock > ret)
242             ret = sock->sock;
243     }
244     timeout.tv_sec = timeout_seconds;
245     timeout.tv_usec = 0;
246     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
247     if(ret == 0) return;
248     for (sock = sockets->data; sock; sock = sock->next) {
249         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
250             if(sock->bufferpos != 0) {
251                 #ifdef WIN32
252                 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
253                 #else
254                 bytes = read(sock->sock, buffer, sizeof(buffer));
255                 #endif
256                 if(bytes > 0) {
257                     for(i = 0; i < bytes; i++) {
258                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
259                         sock->buffer[sock->bufferpos + i] = buffer[i];
260                     }
261                     sock->bufferpos += i;
262                 }
263             } else {
264                 #ifdef WIN32
265                 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
266                 #else
267                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
268                 #endif
269                 if(bytes > 0)
270                     sock->bufferpos = bytes;
271             }
272             if(bytes <= 0) {
273                 //error
274                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
275                 bot_disconnect(sock);
276                 if(sock->queue)
277                     queue_destroy(sock);
278             } else {
279                 sock->traffic_in += bytes;
280                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
281                 if(used == sock->bufferpos + 1) {
282                     //used all bytes so just reset the bufferpos
283                     sock->bufferpos = 0;
284                 } else {
285                     for(i = 0; i < sock->bufferpos - used; i++) {
286                         sock->buffer[i] = sock->buffer[i+used];
287                     }
288                     sock->bufferpos -= used;
289                 }
290             }
291         } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
292             if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
293                 connect_socket(sock);
294             }
295         }
296     }
297 }
298
299 void
300 putsock(struct ClientSocket *client, const char *text, ...)
301 {
302     va_list arg_list;
303     char sendBuf[MAXLEN];
304     int pos;
305     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
306     sendBuf[0] = '\0';
307     va_start(arg_list, text);
308     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
309     va_end(arg_list);
310     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
311     sendBuf[pos] = '\n';
312     sendBuf[pos+1] = '\0';
313     write_socket(client, sendBuf, pos+1);
314 }
315
316 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
317     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
318     if(sock == NULL) return NULL;
319     for (; sock; sock = sock->next) {
320         if(!flags || (sock->flags & flags) == flags)
321             return sock;
322     }
323     return NULL;
324 }
325
326 void free_sockets() {
327     if(!sockets) return;
328     struct ClientSocket *client, *next;
329     for (client = sockets->data; client; client = next) {
330         next = client->next;
331         if((client->flags & SOCKET_FLAG_CONNECTED))
332             close(client->sock);
333         if(client->queue)
334             queue_destroy(client);
335         free(client->host);
336         free(client->pass);
337         free(client);
338     }
339     free(sockets);
340     sockets = NULL;
341 }