d8faf26d4a0ec7c56ae3475f083b300300517df3
[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 }
104
105 int close_socket(struct ClientSocket *client) {
106     if(client == NULL) return 0;
107     if((client->flags & SOCKET_FLAG_CONNECTED)) {
108         iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
109         
110         bot_disconnect(client);
111         
112         iohandler_close(client->iofd);
113         client->iofd = NULL;
114     }
115     client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_CONNECTED);
116     if(client->queue)
117         queue_destroy(client);
118     if(client->whoqueue_first)
119         clear_whoqueue(client);
120     if(client->handleinfo_first)
121         clear_handleinfoqueue(client);
122     return 1;
123 }
124
125 int destroy_socket(struct ClientSocket *client) {
126     if(client == NULL) return 0;
127     close_socket(client);
128     SYNCHRONIZE(synchronized);
129     struct ClientSocket *sock, *last_sock = NULL;
130     for (sock = sockets->data; sock; sock = sock->next) {
131         if(sock == client) {
132             if(last_sock)
133                 last_sock->next = sock->next;
134             else
135                 sockets->data = sock->next;
136             sockets->count--;
137             break;
138         } else
139             last_sock = sock;
140     }
141     event_freeclient(client);
142     free(client->host);
143     if(client->bind)
144         free(client->bind);
145     if(client->pass)
146         free(client->pass);
147     if(client->network_name)
148         free(client->network_name);
149     if(client->iofd) //reconnect timer?
150         iohandler_close(client->iofd);
151     free(client);
152     DESYNCHRONIZE(synchronized);
153     return 1;
154 }
155
156 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
157     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
158     SYNCHRONIZE(synchronized);
159     #ifdef HAVE_THREADS
160     printf_log("main", LOG_IRCRAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
161     #else
162     printf_log("main", LOG_IRCRAW, "[send %d] %s", len, msg);
163     #endif
164         iohandler_send(client->iofd, msg, len);
165     client->traffic_out += len;
166     DESYNCHRONIZE(synchronized);
167     return 1;
168 }
169
170 int write_socket(struct ClientSocket *client, char* msg, int len) {
171     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
172     if(client->flags & SOCKET_FLAG_USE_QUEUE)
173         return queue_add(client, msg, len);
174     else
175         return write_socket_force(client, msg, len);
176 }
177
178 #if HAVE_THREADS
179 static void clientsocket_start_of_recv(unsigned int tid) {
180     SYNCHRONIZE(whohandler_sync);
181     struct ParseOrder *entry, *last;
182     for(last = parse_order; last; last = last->next) {
183         if(last->next == NULL)
184             break;
185     }
186     entry = malloc(sizeof(*entry));
187     entry->tid = tid;
188     entry->next = NULL;
189     if(last)
190         last->next = entry;
191     else
192         parse_order = entry;
193     DESYNCHRONIZE(whohandler_sync);
194 }
195
196 static void clientsocket_end_of_recv(unsigned int tid) {
197     SYNCHRONIZE(whohandler_sync);
198     struct ParseOrder *entry, *last = NULL;
199     for(entry = parse_order; entry; entry = entry->next) {
200         if(entry->tid == tid) {
201             if(last)
202                 last->next = entry->next;
203             else
204                 parse_order = entry->next;
205             free(entry);
206             break;
207         } else
208             last = entry;
209     }
210     DESYNCHRONIZE(whohandler_sync);
211 }
212
213 int clientsocket_parseorder_top(unsigned int tid) {
214     if(parse_order && parse_order->tid == tid)
215         return 1;
216     else
217         return 0;
218 }
219 #endif
220
221 static IOHANDLER_CALLBACK(socket_callback) {
222     struct ClientSocket *client = event->iofd->data;
223     #ifdef HAVE_THREADS
224     unsigned int tid;
225     #endif
226     if(process_state.running == 0)
227         return; //just ignore the event (shutdown sequence)
228     switch(event->type) {
229     case IOEVENT_CONNECTED:
230         client->flags |= SOCKET_FLAG_CONNECTED;
231         if(client->pass && strcmp(client->pass, ""))
232             putsock(client, "PASS :%s", client->pass);
233         putsock(client, "USER %s 0 0 :%s", client->ident, client->realname);
234         putsock(client, "NICK %s", client->nick);
235         break;
236     case IOEVENT_NOTCONNECTED:
237     case IOEVENT_CLOSED:
238         close_socket(client);
239         if(client->flags & SOCKET_FLAG_RECONNECT) {
240             struct timeval timeout;
241             gettimeofday(&timeout, NULL);
242             timeout.tv_sec += SOCKET_RECONNECT_TIME;
243             client->iofd = iohandler_timer(timeout, socket_callback);
244             client->iofd->data = client;
245         }
246         break;
247     case IOEVENT_TIMEOUT: //reconnect timer
248         connect_socket(client);
249         break;
250     case IOEVENT_RECV:
251         #ifdef HAVE_THREADS
252         tid = (unsigned int) pthread_self_tid();
253         clientsocket_start_of_recv(tid);
254         #endif
255         client->traffic_in += strlen(event->data.recv_str);
256         parse_line(client, event->data.recv_str);
257         #ifdef HAVE_THREADS
258         clientsocket_end_of_recv(tid);
259         #endif
260         break;
261     default:
262         break;
263     }
264 }
265
266 void putsock(struct ClientSocket *client, const char *text, ...) {
267     va_list arg_list;
268     char sendBuf[MAXLEN];
269     int pos;
270     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
271     sendBuf[0] = '\0';
272     va_start(arg_list, text);
273     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
274     va_end(arg_list);
275     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
276     sendBuf[pos] = '\n';
277     sendBuf[pos+1] = '\0';
278     write_socket(client, sendBuf, pos+1);
279 }
280
281 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
282     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
283     if(sock == NULL) return NULL;
284     for (; sock; sock = sock->next) {
285         if(!flags || (sock->flags & flags) == flags)
286             return sock;
287     }
288     return NULL;
289 }
290
291 void free_sockets(int close_only) {
292     if(!sockets) return;
293     struct ClientSocket *client, *next;
294     for (client = sockets->data; client; client = next) {
295         next = client->next;
296         if(close_only) {
297             if((client->flags & SOCKET_FLAG_CONNECTED))
298                 iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] shutdown requested.\n", NEONSERV_VERSION, patchlevel);
299         } else
300             destroy_socket(client);
301     }
302     if(!close_only) {
303         free(sockets);
304         sockets = NULL;
305     }
306 }