1 /* ClientSocket.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "ClientSocket.h"
19 #include "IRCParser.h"
22 #include "WHOHandler.h"
23 #include "HandleInfoHandler.h"
24 #include "ConfigParser.h"
26 #include "IOHandler.h"
27 #include "IRCEvents.h"
31 struct ClientSocket *data;
36 static pthread_mutex_t synchronized;
37 static pthread_mutex_t synchronized_recv;
41 struct ParseOrder *next;
43 struct ParseOrder *parse_order = NULL;
47 static struct socket_list *sockets = NULL;
49 static IOHANDLER_CALLBACK(socket_callback);
52 THREAD_MUTEX_INIT(synchronized);
53 THREAD_MUTEX_INIT(synchronized_recv);
55 sockets = malloc(sizeof(*sockets));
58 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
65 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
68 struct ClientSocket *client = malloc(sizeof(*client));
73 client->host = strdup(host);
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);
81 client->network_name = NULL;
83 client->traffic_in = 0;
84 client->traffic_out = 0;
85 client->connection_time = 0;
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);
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;
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);
111 bot_disconnect(client);
113 iohandler_close(client->iofd);
116 client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_CONNECTED);
118 queue_destroy(client);
119 if(client->whoqueue_first)
120 clear_whoqueue(client);
121 if(client->handleinfo_first)
122 clear_handleinfoqueue(client);
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);
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) {
140 last_sock->next = sock->next;
142 sockets->data = sock->next;
148 event_freeclient(client);
154 if(client->network_name)
155 free(client->network_name);
156 if(client->iofd) //reconnect timer?
157 iohandler_close(client->iofd);
159 DESYNCHRONIZE(synchronized);
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);
167 printf_log("main", LOG_IRCRAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
169 printf_log("main", LOG_IRCRAW, "[send %d] %s", len, msg);
171 iohandler_send(client->iofd, msg, len);
172 client->traffic_out += len;
173 DESYNCHRONIZE(synchronized);
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);
182 return write_socket_force(client, msg, len);
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)
193 entry = malloc(sizeof(*entry));
200 DESYNCHRONIZE(whohandler_sync);
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) {
209 last->next = entry->next;
211 parse_order = entry->next;
217 DESYNCHRONIZE(whohandler_sync);
220 int clientsocket_parseorder_top(unsigned int tid) {
221 if(parse_order && parse_order->tid == tid)
228 static IOHANDLER_CALLBACK(socket_callback) {
229 struct ClientSocket *client = event->iofd->data;
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);
243 case IOEVENT_NOTCONNECTED:
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;
254 case IOEVENT_TIMEOUT: //reconnect timer
255 connect_socket(client);
259 tid = (unsigned int) pthread_self_tid();
260 clientsocket_start_of_recv(tid);
262 client->traffic_in += strlen(event->data.recv_str);
263 parse_line(client, event->data.recv_str);
265 clientsocket_end_of_recv(tid);
273 void putsock(struct ClientSocket *client, const char *text, ...) {
275 char sendBuf[MAXLEN];
277 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
279 va_start(arg_list, text);
280 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
282 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
284 sendBuf[pos+1] = '\0';
285 write_socket(client, sendBuf, pos+1);
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)
298 void free_sockets(int close_only) {
300 struct ClientSocket *client, *next;
301 for (client = sockets->data; client; client = next) {
304 if((client->flags & SOCKET_FLAG_CONNECTED))
305 iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] shutdown requested.\n", NEONSERV_VERSION, patchlevel);
307 destroy_socket(client);