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"
30 struct ClientSocket *data;
35 static pthread_mutex_t synchronized;
36 static pthread_mutex_t synchronized_recv;
40 struct ParseOrder *next;
42 struct ParseOrder *parse_order = NULL;
46 static struct socket_list *sockets = NULL;
48 static IOHANDLER_CALLBACK(socket_callback);
51 THREAD_MUTEX_INIT(synchronized);
52 THREAD_MUTEX_INIT(synchronized_recv);
54 sockets = malloc(sizeof(*sockets));
57 perror("malloc() failed");
64 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
67 struct ClientSocket *client = malloc(sizeof(*client));
72 client->host = strdup(host);
74 client->bind = (bindto ? strdup(bindto) : NULL);
75 client->pass = (pass == NULL ? NULL : strdup(pass));
76 client->nick = strdup(nick);
77 client->ident = strdup(ident);
78 client->realname = strdup(realname);
80 client->network_name = NULL;
82 client->traffic_in = 0;
83 client->traffic_out = 0;
84 client->connection_time = 0;
88 client->whoqueue_first = NULL;
89 client->whoqueue_last = NULL;
90 client->handleinfo_first = NULL;
91 client->handleinfo_last = NULL;
92 SYNCHRONIZE(synchronized);
93 client->next = sockets->data;
94 sockets->data = client;
95 DESYNCHRONIZE(synchronized);
99 void connect_socket(struct ClientSocket *client) {
100 client->iofd = iohandler_connect(client->host, client->port, ((client->flags & SOCKET_FLAG_SSL) ? 1 : 0), client->bind, socket_callback);
101 client->iofd->data = client;
104 int close_socket(struct ClientSocket *client) {
105 if(client == NULL) return 0;
106 if((client->flags & SOCKET_FLAG_CONNECTED)) {
107 iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
109 bot_disconnect(client);
111 iohandler_close(client->iofd);
114 client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_CONNECTED);
116 queue_destroy(client);
117 if(client->whoqueue_first)
118 clear_whoqueue(client);
119 if(client->handleinfo_first)
120 clear_handleinfoqueue(client);
124 int destroy_socket(struct ClientSocket *client) {
125 if(client == NULL) return 0;
126 close_socket(client);
127 SYNCHRONIZE(synchronized);
128 struct ClientSocket *sock, *last_sock = NULL;
129 for (sock = sockets->data; sock; sock = sock->next) {
132 last_sock->next = sock->next;
134 sockets->data = sock->next;
140 event_freeclient(client);
146 if(client->network_name)
147 free(client->network_name);
148 if(client->iofd) //reconnect timer?
149 iohandler_close(client->iofd);
151 DESYNCHRONIZE(synchronized);
155 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
156 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
157 SYNCHRONIZE(synchronized);
159 putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
161 putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg);
163 iohandler_send(client->iofd, msg, len);
164 client->traffic_out += len;
165 DESYNCHRONIZE(synchronized);
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);
174 return write_socket_force(client, msg, len);
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)
185 entry = malloc(sizeof(*entry));
192 DESYNCHRONIZE(whohandler_sync);
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) {
201 last->next = entry->next;
203 parse_order = entry->next;
209 DESYNCHRONIZE(whohandler_sync);
212 int clientsocket_parseorder_top(unsigned int tid) {
213 if(parse_order && parse_order->tid == tid)
220 static IOHANDLER_CALLBACK(socket_callback) {
221 struct ClientSocket *client = event->iofd->data;
225 if(process_state.running == 0)
226 return; //just ignore the event (shutdown sequence)
227 switch(event->type) {
228 case IOEVENT_CONNECTED:
229 client->flags |= SOCKET_FLAG_CONNECTED;
230 if(client->pass && strcmp(client->pass, ""))
231 putsock(client, "PASS :%s", client->pass);
232 putsock(client, "USER %s 0 0 :%s", client->ident, client->realname);
233 putsock(client, "NICK %s", client->nick);
235 case IOEVENT_NOTCONNECTED:
237 close_socket(client);
238 if(client->flags & SOCKET_FLAG_RECONNECT) {
239 struct timeval timeout;
240 gettimeofday(&timeout, NULL);
241 timeout.tv_sec += SOCKET_RECONNECT_TIME;
242 client->iofd = iohandler_timer(timeout, socket_callback);
243 client->iofd->data = client;
246 case IOEVENT_TIMEOUT: //reconnect timer
247 connect_socket(client);
251 tid = (unsigned int) pthread_self_tid();
252 clientsocket_start_of_recv(tid);
254 client->traffic_in += strlen(event->data.recv_str);
255 parse_line(client, event->data.recv_str);
257 clientsocket_end_of_recv(tid);
265 void putsock(struct ClientSocket *client, const char *text, ...) {
267 char sendBuf[MAXLEN];
269 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
271 va_start(arg_list, text);
272 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
274 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
276 sendBuf[pos+1] = '\0';
277 write_socket(client, sendBuf, pos+1);
280 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
281 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
282 if(sock == NULL) return NULL;
283 for (; sock; sock = sock->next) {
284 if(!flags || (sock->flags & flags) == flags)
290 void free_sockets(int close_only) {
292 struct ClientSocket *client, *next;
293 for (client = sockets->data; client; client = next) {
296 if((client->flags & SOCKET_FLAG_CONNECTED))
297 iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] shutdown requested.\n", NEONSERV_VERSION, patchlevel);
299 destroy_socket(client);