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