Merge branch 'development'
[NeonServV5.git] / src / ClientSocket.c
1 /* ClientSocket.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  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 #include "main.h"
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 "ConfigParser.h"
25 #include "version.h"
26 #include "IOHandler.h"
27 #include "IRCEvents.h"
28 #include "log.h"
29
30 struct socket_list {
31     struct ClientSocket *data;
32     unsigned count;
33 };
34
35 #ifdef HAVE_THREADS
36 static pthread_mutex_t synchronized;
37 static pthread_mutex_t synchronized_recv;
38
39 struct ParseOrder {
40     unsigned int tid;
41     struct ParseOrder *next;
42 };
43 struct ParseOrder *parse_order = NULL;
44 #endif
45
46 //the magic list :P
47 static struct socket_list *sockets = NULL;
48
49 static IOHANDLER_CALLBACK(socket_callback);
50
51 void init_sockets() {
52     THREAD_MUTEX_INIT(synchronized);
53     THREAD_MUTEX_INIT(synchronized_recv);
54     
55     sockets = malloc(sizeof(*sockets));
56     if (!sockets)
57     {
58         printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
59         return;
60     }
61     sockets->data = NULL;
62     sockets->count = 0;
63 }
64
65 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
66     if(!sockets)
67         init_sockets();
68     struct ClientSocket *client = malloc(sizeof(*client));
69     if (!client) {
70         return NULL;
71     }
72     client->iofd = NULL;
73     client->host = strdup(host);
74     client->port = port;
75     client->bind = (bindto ? strdup(bindto) : NULL);
76     client->pass = (pass == NULL ? NULL : strdup(pass));
77     client->nick = strdup(nick);
78     client->ident = strdup(ident);
79     client->realname = strdup(realname);
80     client->user = NULL;
81     client->network_name = NULL;
82     client->flags = 0;
83     client->traffic_in = 0;
84     client->traffic_out = 0;
85     client->connection_time = 0;
86         client->botid = 0;
87     client->clientid = 0;
88     client->queue = NULL;
89     client->whoqueue_first = NULL;
90     client->whoqueue_last = NULL;
91     client->handleinfo_first = NULL;
92     client->handleinfo_last = NULL;
93     SYNCHRONIZE(synchronized);
94     client->next = sockets->data;
95     sockets->data = client;
96     DESYNCHRONIZE(synchronized);
97     return client;
98 }
99
100 void connect_socket(struct ClientSocket *client) {
101     client->iofd = iohandler_connect(client->host, client->port, ((client->flags & SOCKET_FLAG_SSL) ? 1 : 0), client->bind, socket_callback);
102     client->iofd->data = client;
103     client->flags |= SOCKET_FLAG_RECONNECT;
104 }
105
106 static int _close_socket(struct ClientSocket *client) {
107     if(client == NULL) return 0;
108     if((client->flags & SOCKET_FLAG_CONNECTED)) {
109         iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
110         
111         bot_disconnect(client);
112         
113         iohandler_close(client->iofd);
114         client->iofd = NULL;
115     }
116     client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_CONNECTED);
117     if(client->queue)
118         queue_destroy(client);
119     if(client->whoqueue_first)
120         clear_whoqueue(client);
121     if(client->handleinfo_first)
122         clear_handleinfoqueue(client);
123     return 1;
124 }
125
126 int close_socket(struct ClientSocket *client) { //external call (don't reconnect)
127     if(client == NULL) return 0;
128     client->flags &= ~SOCKET_FLAG_RECONNECT;
129     return _close_socket(client);
130 }
131
132 int destroy_socket(struct ClientSocket *client) {
133     if(client == NULL) return 0;
134     close_socket(client);
135     SYNCHRONIZE(synchronized);
136     struct ClientSocket *sock, *last_sock = NULL;
137     for (sock = sockets->data; sock; sock = sock->next) {
138         if(sock == client) {
139             if(last_sock)
140                 last_sock->next = sock->next;
141             else
142                 sockets->data = sock->next;
143             sockets->count--;
144             break;
145         } else
146             last_sock = sock;
147     }
148     event_freeclient(client);
149     free(client->host);
150     if(client->bind)
151         free(client->bind);
152     if(client->pass)
153         free(client->pass);
154     if(client->network_name)
155         free(client->network_name);
156     if(client->iofd) //reconnect timer?
157         iohandler_close(client->iofd);
158     free(client);
159     DESYNCHRONIZE(synchronized);
160     return 1;
161 }
162
163 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
164     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
165     SYNCHRONIZE(synchronized);
166     #ifdef HAVE_THREADS
167     printf_log("main", LOG_IRCRAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
168     #else
169     printf_log("main", LOG_IRCRAW, "[send %d] %s", len, msg);
170     #endif
171         iohandler_send(client->iofd, msg, len);
172     client->traffic_out += len;
173     DESYNCHRONIZE(synchronized);
174     return 1;
175 }
176
177 int write_socket(struct ClientSocket *client, char* msg, int len) {
178     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
179     if(client->flags & SOCKET_FLAG_USE_QUEUE)
180         return queue_add(client, msg, len);
181     else
182         return write_socket_force(client, msg, len);
183 }
184
185 #if HAVE_THREADS
186 static void clientsocket_start_of_recv(unsigned int tid) {
187     SYNCHRONIZE(whohandler_sync);
188     struct ParseOrder *entry, *last;
189     for(last = parse_order; last; last = last->next) {
190         if(last->next == NULL)
191             break;
192     }
193     entry = malloc(sizeof(*entry));
194     entry->tid = tid;
195     entry->next = NULL;
196     if(last)
197         last->next = entry;
198     else
199         parse_order = entry;
200     DESYNCHRONIZE(whohandler_sync);
201 }
202
203 static void clientsocket_end_of_recv(unsigned int tid) {
204     SYNCHRONIZE(whohandler_sync);
205     struct ParseOrder *entry, *last = NULL;
206     for(entry = parse_order; entry; entry = entry->next) {
207         if(entry->tid == tid) {
208             if(last)
209                 last->next = entry->next;
210             else
211                 parse_order = entry->next;
212             free(entry);
213             break;
214         } else
215             last = entry;
216     }
217     DESYNCHRONIZE(whohandler_sync);
218 }
219
220 int clientsocket_parseorder_top(unsigned int tid) {
221     if(parse_order && parse_order->tid == tid)
222         return 1;
223     else
224         return 0;
225 }
226 #endif
227
228 static IOHANDLER_CALLBACK(socket_callback) {
229     struct ClientSocket *client = event->iofd->data;
230     #ifdef HAVE_THREADS
231     unsigned int tid;
232     #endif
233     if(process_state.running == 0)
234         return; //just ignore the event (shutdown sequence)
235     switch(event->type) {
236     case IOEVENT_CONNECTED:
237         client->flags |= SOCKET_FLAG_CONNECTED;
238         if(client->pass && strcmp(client->pass, ""))
239             putsock(client, "PASS :%s", client->pass);
240         putsock(client, "USER %s 0 0 :%s", client->ident, client->realname);
241         putsock(client, "NICK %s", client->nick);
242         break;
243     case IOEVENT_NOTCONNECTED:
244     case IOEVENT_CLOSED:
245         _close_socket(client);
246         if(client->flags & SOCKET_FLAG_RECONNECT) {
247             struct timeval timeout;
248             gettimeofday(&timeout, NULL);
249             timeout.tv_sec += SOCKET_RECONNECT_TIME;
250             client->iofd = iohandler_timer(timeout, socket_callback);
251             client->iofd->data = client;
252         }
253         break;
254     case IOEVENT_TIMEOUT: //reconnect timer
255         connect_socket(client);
256         break;
257     case IOEVENT_RECV:
258         #ifdef HAVE_THREADS
259         tid = (unsigned int) pthread_self_tid();
260         clientsocket_start_of_recv(tid);
261         #endif
262         client->traffic_in += strlen(event->data.recv_str);
263         parse_line(client, event->data.recv_str);
264         #ifdef HAVE_THREADS
265         clientsocket_end_of_recv(tid);
266         #endif
267         break;
268     default:
269         break;
270     }
271 }
272
273 void putsock(struct ClientSocket *client, const char *text, ...) {
274     va_list arg_list;
275     char sendBuf[MAXLEN];
276     int pos;
277     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
278     sendBuf[0] = '\0';
279     va_start(arg_list, text);
280     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
281     va_end(arg_list);
282     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
283     sendBuf[pos] = '\n';
284     sendBuf[pos+1] = '\0';
285     write_socket(client, sendBuf, pos+1);
286 }
287
288 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
289     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
290     if(sock == NULL) return NULL;
291     for (; sock; sock = sock->next) {
292         if(!flags || (sock->flags & flags) == flags)
293             return sock;
294     }
295     return NULL;
296 }
297
298 void free_sockets(int close_only) {
299     if(!sockets) return;
300     struct ClientSocket *client, *next;
301     for (client = sockets->data; client; client = next) {
302         next = client->next;
303         if(close_only) {
304             if((client->flags & SOCKET_FLAG_CONNECTED))
305                 iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] shutdown requested.\n", NEONSERV_VERSION, patchlevel);
306         } else
307             destroy_socket(client);
308     }
309     if(!close_only) {
310         free(sockets);
311         sockets = NULL;
312     }
313 }