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 switch(event->type) {
226 case IOEVENT_CONNECTED:
227 client->flags |= SOCKET_FLAG_CONNECTED;
228 if(client->pass && strcmp(client->pass, ""))
229 iohandler_printf(event->iofd, "PASS :%s", client->pass);
230 iohandler_printf(event->iofd, "USER %s 0 0 :%s", client->ident, client->realname);
231 iohandler_printf(event->iofd, "NICK %s", client->nick);
233 case IOEVENT_NOTCONNECTED:
235 close_socket(client);
236 if(client->flags & SOCKET_FLAG_RECONNECT) {
237 struct timeval timeout;
238 gettimeofday(&timeout, NULL);
239 timeout.tv_sec += SOCKET_RECONNECT_TIME;
240 client->iofd = iohandler_timer(timeout, socket_callback);
241 client->iofd->data = client;
244 case IOEVENT_TIMEOUT: //reconnect timer
245 connect_socket(client);
249 tid = (unsigned int) pthread_self_tid();
250 clientsocket_start_of_recv(tid);
252 client->traffic_in += strlen(event->data.recv_str);
253 parse_line(client, event->data.recv_str);
255 clientsocket_end_of_recv(tid);
263 void putsock(struct ClientSocket *client, const char *text, ...) {
265 char sendBuf[MAXLEN];
267 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
269 va_start(arg_list, text);
270 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
272 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
274 sendBuf[pos+1] = '\0';
275 write_socket(client, sendBuf, pos+1);
278 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
279 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
280 if(sock == NULL) return NULL;
281 for (; sock; sock = sock->next) {
282 if(!flags || (sock->flags & flags) == flags)
288 void free_sockets() {
290 struct ClientSocket *client, *next;
291 for (client = sockets->data; client; client = next) {
293 destroy_socket(client);