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