1 /* ClientSocket.c - NeonServ v5.5
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) {
64 struct ClientSocket *client = malloc(sizeof(*client));
69 client->host = strdup(host);
71 client->bind = (bindto ? strdup(bindto) : NULL);
72 client->pass = (pass == NULL ? NULL : strdup(pass));
73 client->nick = strdup(nick);
74 client->ident = strdup(ident);
75 client->realname = strdup(realname);
77 client->network_name = NULL;
79 client->traffic_in = 0;
80 client->traffic_out = 0;
81 client->connection_time = 0;
85 client->whoqueue_first = NULL;
86 client->whoqueue_last = NULL;
87 client->handleinfo_first = NULL;
88 client->handleinfo_last = NULL;
89 SYNCHRONIZE(synchronized);
90 client->next = sockets->data;
91 sockets->data = client;
92 DESYNCHRONIZE(synchronized);
96 void connect_socket(struct ClientSocket *client) {
97 client->iofd = iohandler_connect(client->host, client->port, ((client->flags & SOCKET_FLAG_SSL) ? 1 : 0), client->bind, socket_callback);
98 client->iofd->data = client;
101 int close_socket(struct ClientSocket *client) {
102 if(client == NULL) return 0;
103 if((client->flags & SOCKET_FLAG_CONNECTED)) {
104 iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
106 bot_disconnect(client);
108 iohandler_close(client->iofd);
111 client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_CONNECTED);
113 queue_destroy(client);
114 if(client->whoqueue_first)
115 clear_whoqueue(client);
116 if(client->handleinfo_first)
117 clear_handleinfoqueue(client);
121 int destroy_socket(struct ClientSocket *client) {
122 if(client == NULL) return 0;
123 close_socket(client);
124 SYNCHRONIZE(synchronized);
125 struct ClientSocket *sock, *last_sock = NULL;
126 for (sock = sockets->data; sock; sock = sock->next) {
129 last_sock->next = sock->next;
131 sockets->data = sock->next;
142 if(client->network_name)
143 free(client->network_name);
144 if(client->iofd) //reconnect timer?
145 iohandler_close(client->iofd);
147 DESYNCHRONIZE(synchronized);
151 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
152 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
153 SYNCHRONIZE(synchronized);
155 putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
157 putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg);
159 iohandler_send(client->iofd, msg, len);
160 client->traffic_out += len;
161 DESYNCHRONIZE(synchronized);
165 int write_socket(struct ClientSocket *client, char* msg, int len) {
166 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
167 if(client->flags & SOCKET_FLAG_USE_QUEUE)
168 return queue_add(client, msg, len);
170 return write_socket_force(client, msg, len);
174 static void clientsocket_start_of_recv(unsigned int tid) {
175 SYNCHRONIZE(whohandler_sync);
176 struct ParseOrder *entry, *last;
177 for(last = parse_order; last; last = last->next) {
178 if(last->next == NULL)
181 entry = malloc(sizeof(*entry));
188 DESYNCHRONIZE(whohandler_sync);
191 static void clientsocket_end_of_recv(unsigned int tid) {
192 SYNCHRONIZE(whohandler_sync);
193 struct ParseOrder *entry, *last = NULL;
194 for(entry = parse_order; entry; entry = entry->next) {
195 if(entry->tid == tid) {
197 last->next = entry->next;
199 parse_order = entry->next;
205 DESYNCHRONIZE(whohandler_sync);
208 int clientsocket_parseorder_top(unsigned int tid) {
209 if(parse_order && parse_order->tid == tid)
216 static IOHANDLER_CALLBACK(socket_callback) {
217 struct ClientSocket *client = event->iofd->data;
219 switch(event->type) {
220 case IOEVENT_CONNECTED:
221 client->flags |= SOCKET_FLAG_CONNECTED;
222 if(client->pass && strcmp(client->pass, ""))
223 iohandler_printf(event->iofd, "PASS :%s", client->pass);
224 iohandler_printf(event->iofd, "USER %s 0 0 :%s", client->ident, client->realname);
225 iohandler_printf(event->iofd, "NICK %s", client->nick);
227 case IOEVENT_NOTCONNECTED:
229 close_socket(client);
230 if(client->flags & SOCKET_FLAG_RECONNECT) {
231 struct timeval timeout;
232 gettimeofday(&timeout, NULL);
233 timeout.tv_sec += SOCKET_RECONNECT_TIME;
234 client->iofd = iohandler_timer(timeout, socket_callback);
235 client->iofd->data = client;
238 case IOEVENT_TIMEOUT: //reconnect timer
239 connect_socket(client);
243 tid = (unsigned int) pthread_self_tid();
244 clientsocket_start_of_recv(tid);
246 parse_line(client, event->data.recv_str);
248 clientsocket_end_of_recv(tid);
256 void putsock(struct ClientSocket *client, const char *text, ...) {
258 char sendBuf[MAXLEN];
260 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
262 va_start(arg_list, text);
263 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
265 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
267 sendBuf[pos+1] = '\0';
268 write_socket(client, sendBuf, pos+1);
271 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
272 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
273 if(sock == NULL) return NULL;
274 for (; sock; sock = sock->next) {
275 if(!flags || (sock->flags & flags) == flags)
281 void free_sockets() {
283 struct ClientSocket *client, *next;
284 for (client = sockets->data; client; client = next) {
286 destroy_socket(client);