modified code to use IOHandler functions instead of own ones
[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 static char buffer[BUF_SIZ];
47
48 static IOHANDLER_CALLBACK(socket_callback);
49
50 void init_sockets() {
51     THREAD_MUTEX_INIT(synchronized);
52     THREAD_MUTEX_INIT(synchronized_recv);
53     
54     sockets = malloc(sizeof(*sockets));
55     if (!sockets)
56     {
57         perror("malloc() failed");
58         return;
59     }
60     sockets->data = NULL;
61     sockets->count = 0;
62 }
63
64 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
65     struct ClientSocket *client = malloc(sizeof(*client));
66     if (!client) {
67         return NULL;
68     }
69     client->iofd = NULL;
70     client->host = strdup(host);
71     client->port = port;
72     client->bind = (bindto ? strdup(bindto) : NULL);
73     client->pass = (pass == NULL ? NULL : strdup(pass));
74     client->nick = strdup(nick);
75     client->ident = strdup(ident);
76     client->realname = strdup(realname);
77     client->user = NULL;
78     client->network_name = NULL;
79     client->flags = 0;
80     client->bufferpos = 0;
81     client->traffic_in = 0;
82     client->traffic_out = 0;
83     client->connection_time = 0;
84         client->botid = 0;
85     client->clientid = 0;
86     client->queue = NULL;
87     client->whoqueue_first = NULL;
88     client->whoqueue_last = NULL;
89     client->handleinfo_first = NULL;
90     client->handleinfo_last = NULL;
91     SYNCHRONIZE(synchronized);
92     client->next = sockets->data;
93     sockets->data = client;
94     DESYNCHRONIZE(synchronized);
95     return client;
96 }
97
98 void connect_socket(struct ClientSocket *client) {
99     client->iofd = iohandler_connect(client->host, client->port, ((client->flags & SOCKET_FLAG_SSL) ? 1 : 0), client->bind, socket_callback);
100     client->iofd->data = client;
101 }
102
103 int close_socket(struct ClientSocket *client) {
104     if(client == NULL) return 0;
105     if((client->flags & SOCKET_FLAG_CONNECTED)) {
106         char quitbuf[MAXLEN];
107         int quitlen = sprintf(quitbuf, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
108         iohandler_send(client, quitbuf, quitlen);
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     free(client->host);
142     if(client->bind)
143         free(client->bind);
144     if(client->pass)
145         free(client->pass);
146     if(client->network_name)
147         free(client->network_name);
148     if(client->iofd) //reconnect timer?
149         iohandler_close(client->iofd);
150     free(client);
151     DESYNCHRONIZE(synchronized);
152     return 1;
153 }
154
155 static int write_socket_force(struct ClientSocket *client, char* msg, int len) {
156     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
157     SYNCHRONIZE(synchronized);
158     #ifdef HAVE_THREADS
159     putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
160     #else
161     putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg);
162     #endif
163         iohandler_send(client->iofd, msg, len)
164     client->traffic_out += len;
165     DESYNCHRONIZE(synchronized);
166     return ret;
167 }
168
169 int write_socket(struct ClientSocket *client, char* msg, int len) {
170     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
171     if(client->flags & SOCKET_FLAG_USE_QUEUE)
172         return queue_add(client, msg, len);
173     else
174         return write_socket_force(client, msg, len);
175 }
176
177 #if HAVE_THREADS
178 static void clientsocket_start_of_recv(unsigned int tid) {
179     SYNCHRONIZE(whohandler_sync);
180     struct ParseOrder *entry, *last;
181     for(last = parse_order; last; last = last->next) {
182         if(last->next == NULL)
183             break;
184     }
185     entry = malloc(sizeof(*entry));
186     entry->tid = tid;
187     entry->next = NULL;
188     if(last)
189         last->next = entry;
190     else
191         parse_order = entry;
192     DESYNCHRONIZE(whohandler_sync);
193 }
194
195 static void clientsocket_end_of_recv(unsigned int tid) {
196     SYNCHRONIZE(whohandler_sync);
197     struct ParseOrder *entry, *last = NULL;
198     for(entry = parse_order; entry; entry = entry->next) {
199         if(entry->tid == tid) {
200             if(last)
201                 last->next = entry->next;
202             else
203                 parse_order = entry->next;
204             free(entry);
205             break;
206         } else
207             last = entry;
208     }
209     DESYNCHRONIZE(whohandler_sync);
210 }
211
212 int clientsocket_parseorder_top(unsigned int tid) {
213     if(parse_order && parse_order->tid == tid)
214         return 1;
215     else
216         return 0;
217 }
218 #endif
219
220 static IOHANDLER_CALLBACK(socket_callback) {
221     struct ClientSocket *client = event->iofd->data;
222     switch(event->type) {
223     case IOEVENT_CONNECTED:
224         client->flags |= SOCKET_FLAG_CONNECTED;
225         if(client->pass && strcmp(client->pass, ""))
226             iohandler_printf(event->iofd, "PASS :%s", client->pass);
227         iohandler_printf(event->iofd, "USER %s 0 0 :%s", client->ident, client->realname);
228         iohandler_printf(event->iofd, "NICK %s", client->nick);
229         break;
230     case IOEVENT_NOTCONNECTED:
231     case IOEVENT_CLOSED:
232         close_socket(client);
233         if(client->flags & SOCKET_FLAG_RECONNECT) {
234             struct timeval timeout;
235             gettimeofday(&timeout, NULL);
236             timeout.tv_sec += SOCKET_RECONNECT_TIME;
237             client->iofd = iohandler_timer(timeout, socket_callback);
238             client->iofd->data = client;
239         }
240         break;
241     case IOEVENT_TIMEOUT: //reconnect timer
242         connect_socket(client);
243         break;
244     case IOEVENT_RECV:
245         #ifdef HAVE_THREADS
246         clientsocket_start_of_recv(tid);
247         #endif
248         parse_line(client, event->data.recv_str);
249         #ifdef HAVE_THREADS
250         clientsocket_end_of_recv(tid);
251         #endif
252         break;
253     default:
254         break;
255     }
256 }
257
258 void putsock(struct ClientSocket *client, const char *text, ...) {
259     va_list arg_list;
260     char sendBuf[MAXLEN];
261     int pos;
262     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
263     sendBuf[0] = '\0';
264     va_start(arg_list, text);
265     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
266     va_end(arg_list);
267     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
268     sendBuf[pos] = '\n';
269     sendBuf[pos+1] = '\0';
270     write_socket(client, sendBuf, pos+1);
271 }
272
273 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
274     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
275     if(sock == NULL) return NULL;
276     for (; sock; sock = sock->next) {
277         if(!flags || (sock->flags & flags) == flags)
278             return sock;
279     }
280     return NULL;
281 }
282
283 void free_sockets() {
284     if(!sockets) return;
285     struct ClientSocket *client, *next;
286     for (client = sockets->data; client; client = next) {
287         next = client->next;
288         destroy_socket(client);
289     }
290     free(sockets);
291     sockets = NULL;
292 }