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;
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);
110 bot_disconnect(client);
112 iohandler_close(client->iofd);
115 client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_CONNECTED);
117 queue_destroy(client);
118 if(client->whoqueue_first)
119 clear_whoqueue(client);
120 if(client->handleinfo_first)
121 clear_handleinfoqueue(client);
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) {
133 last_sock->next = sock->next;
135 sockets->data = sock->next;
141 event_freeclient(client);
147 if(client->network_name)
148 free(client->network_name);
149 if(client->iofd) //reconnect timer?
150 iohandler_close(client->iofd);
152 DESYNCHRONIZE(synchronized);
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);
160 printf_log("main", LOG_IRCRAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
162 printf_log("main", LOG_IRCRAW, "[send %d] %s", len, msg);
164 iohandler_send(client->iofd, msg, len);
165 client->traffic_out += len;
166 DESYNCHRONIZE(synchronized);
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);
175 return write_socket_force(client, msg, len);
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)
186 entry = malloc(sizeof(*entry));
193 DESYNCHRONIZE(whohandler_sync);
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) {
202 last->next = entry->next;
204 parse_order = entry->next;
210 DESYNCHRONIZE(whohandler_sync);
213 int clientsocket_parseorder_top(unsigned int tid) {
214 if(parse_order && parse_order->tid == tid)
221 static IOHANDLER_CALLBACK(socket_callback) {
222 struct ClientSocket *client = event->iofd->data;
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);
236 case IOEVENT_NOTCONNECTED:
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;
247 case IOEVENT_TIMEOUT: //reconnect timer
248 connect_socket(client);
252 tid = (unsigned int) pthread_self_tid();
253 clientsocket_start_of_recv(tid);
255 client->traffic_in += strlen(event->data.recv_str);
256 parse_line(client, event->data.recv_str);
258 clientsocket_end_of_recv(tid);
266 void putsock(struct ClientSocket *client, const char *text, ...) {
268 char sendBuf[MAXLEN];
270 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
272 va_start(arg_list, text);
273 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
275 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
277 sendBuf[pos+1] = '\0';
278 write_socket(client, sendBuf, pos+1);
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)
291 void free_sockets(int close_only) {
293 struct ClientSocket *client, *next;
294 for (client = sockets->data; client; client = next) {
297 if((client->flags & SOCKET_FLAG_CONNECTED))
298 iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] shutdown requested.\n", NEONSERV_VERSION, patchlevel);
300 destroy_socket(client);