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