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"
29 struct ClientSocket *data;
34 static pthread_mutex_t synchronized;
35 static pthread_mutex_t synchronized_recv;
39 struct ParseOrder *next;
41 struct ParseOrder *parse_order = NULL;
45 static struct socket_list *sockets = NULL;
47 static IOHANDLER_CALLBACK(socket_callback);
50 THREAD_MUTEX_INIT(synchronized);
51 THREAD_MUTEX_INIT(synchronized_recv);
53 sockets = malloc(sizeof(*sockets));
56 perror("malloc() failed");
63 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
66 struct ClientSocket *client = malloc(sizeof(*client));
71 client->host = strdup(host);
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);
79 client->network_name = NULL;
81 client->traffic_in = 0;
82 client->traffic_out = 0;
83 client->connection_time = 0;
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);
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;
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);
108 bot_disconnect(client);
110 iohandler_close(client->iofd);
113 client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_CONNECTED);
115 queue_destroy(client);
116 if(client->whoqueue_first)
117 clear_whoqueue(client);
118 if(client->handleinfo_first)
119 clear_handleinfoqueue(client);
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) {
131 last_sock->next = sock->next;
133 sockets->data = sock->next;
139 event_freeclient(client);
145 if(client->network_name)
146 free(client->network_name);
147 if(client->iofd) //reconnect timer?
148 iohandler_close(client->iofd);
150 DESYNCHRONIZE(synchronized);
154 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
155 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
156 SYNCHRONIZE(synchronized);
158 putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
160 putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg);
162 iohandler_send(client->iofd, msg, len);
163 client->traffic_out += len;
164 DESYNCHRONIZE(synchronized);
168 int write_socket(struct ClientSocket *client, char* msg, int len) {
169 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
170 if(client->flags & SOCKET_FLAG_USE_QUEUE)
171 return queue_add(client, msg, len);
173 return write_socket_force(client, msg, len);
177 static void clientsocket_start_of_recv(unsigned int tid) {
178 SYNCHRONIZE(whohandler_sync);
179 struct ParseOrder *entry, *last;
180 for(last = parse_order; last; last = last->next) {
181 if(last->next == NULL)
184 entry = malloc(sizeof(*entry));
191 DESYNCHRONIZE(whohandler_sync);
194 static void clientsocket_end_of_recv(unsigned int tid) {
195 SYNCHRONIZE(whohandler_sync);
196 struct ParseOrder *entry, *last = NULL;
197 for(entry = parse_order; entry; entry = entry->next) {
198 if(entry->tid == tid) {
200 last->next = entry->next;
202 parse_order = entry->next;
208 DESYNCHRONIZE(whohandler_sync);
211 int clientsocket_parseorder_top(unsigned int tid) {
212 if(parse_order && parse_order->tid == tid)
219 static IOHANDLER_CALLBACK(socket_callback) {
220 struct ClientSocket *client = event->iofd->data;
224 switch(event->type) {
225 case IOEVENT_CONNECTED:
226 client->flags |= SOCKET_FLAG_CONNECTED;
227 if(client->pass && strcmp(client->pass, ""))
228 iohandler_printf(event->iofd, "PASS :%s", client->pass);
229 iohandler_printf(event->iofd, "USER %s 0 0 :%s", client->ident, client->realname);
230 iohandler_printf(event->iofd, "NICK %s", client->nick);
232 case IOEVENT_NOTCONNECTED:
234 close_socket(client);
235 if(client->flags & SOCKET_FLAG_RECONNECT) {
236 struct timeval timeout;
237 gettimeofday(&timeout, NULL);
238 timeout.tv_sec += SOCKET_RECONNECT_TIME;
239 client->iofd = iohandler_timer(timeout, socket_callback);
240 client->iofd->data = client;
243 case IOEVENT_TIMEOUT: //reconnect timer
244 connect_socket(client);
248 tid = (unsigned int) pthread_self_tid();
249 clientsocket_start_of_recv(tid);
251 client->traffic_in += strlen(event->data.recv_str);
252 parse_line(client, event->data.recv_str);
254 clientsocket_end_of_recv(tid);
262 void putsock(struct ClientSocket *client, const char *text, ...) {
264 char sendBuf[MAXLEN];
266 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
268 va_start(arg_list, text);
269 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
271 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
273 sendBuf[pos+1] = '\0';
274 write_socket(client, sendBuf, pos+1);
277 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
278 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
279 if(sock == NULL) return NULL;
280 for (; sock; sock = sock->next) {
281 if(!flags || (sock->flags & flags) == flags)
287 void free_sockets() {
289 struct ClientSocket *client, *next;
290 for (client = sockets->data; client; client = next) {
292 destroy_socket(client);