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;
144 if(client->network_name)
145 free(client->network_name);
146 if(client->iofd) //reconnect timer?
147 iohandler_close(client->iofd);
149 DESYNCHRONIZE(synchronized);
153 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
154 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
155 SYNCHRONIZE(synchronized);
157 putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
159 putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg);
161 iohandler_send(client->iofd, msg, len);
162 client->traffic_out += len;
163 DESYNCHRONIZE(synchronized);
167 int write_socket(struct ClientSocket *client, char* msg, int len) {
168 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
169 if(client->flags & SOCKET_FLAG_USE_QUEUE)
170 return queue_add(client, msg, len);
172 return write_socket_force(client, msg, len);
176 static void clientsocket_start_of_recv(unsigned int tid) {
177 SYNCHRONIZE(whohandler_sync);
178 struct ParseOrder *entry, *last;
179 for(last = parse_order; last; last = last->next) {
180 if(last->next == NULL)
183 entry = malloc(sizeof(*entry));
190 DESYNCHRONIZE(whohandler_sync);
193 static void clientsocket_end_of_recv(unsigned int tid) {
194 SYNCHRONIZE(whohandler_sync);
195 struct ParseOrder *entry, *last = NULL;
196 for(entry = parse_order; entry; entry = entry->next) {
197 if(entry->tid == tid) {
199 last->next = entry->next;
201 parse_order = entry->next;
207 DESYNCHRONIZE(whohandler_sync);
210 int clientsocket_parseorder_top(unsigned int tid) {
211 if(parse_order && parse_order->tid == tid)
218 static IOHANDLER_CALLBACK(socket_callback) {
219 struct ClientSocket *client = event->iofd->data;
223 switch(event->type) {
224 case IOEVENT_CONNECTED:
225 client->flags |= SOCKET_FLAG_CONNECTED;
226 if(client->pass && strcmp(client->pass, ""))
227 iohandler_printf(event->iofd, "PASS :%s", client->pass);
228 iohandler_printf(event->iofd, "USER %s 0 0 :%s", client->ident, client->realname);
229 iohandler_printf(event->iofd, "NICK %s", client->nick);
231 case IOEVENT_NOTCONNECTED:
233 close_socket(client);
234 if(client->flags & SOCKET_FLAG_RECONNECT) {
235 struct timeval timeout;
236 gettimeofday(&timeout, NULL);
237 timeout.tv_sec += SOCKET_RECONNECT_TIME;
238 client->iofd = iohandler_timer(timeout, socket_callback);
239 client->iofd->data = client;
242 case IOEVENT_TIMEOUT: //reconnect timer
243 connect_socket(client);
247 tid = (unsigned int) pthread_self_tid();
248 clientsocket_start_of_recv(tid);
250 client->traffic_in += strlen(event->data.recv_str);
251 parse_line(client, event->data.recv_str);
253 clientsocket_end_of_recv(tid);
261 void putsock(struct ClientSocket *client, const char *text, ...) {
263 char sendBuf[MAXLEN];
265 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
267 va_start(arg_list, text);
268 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
270 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
272 sendBuf[pos+1] = '\0';
273 write_socket(client, sendBuf, pos+1);
276 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
277 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
278 if(sock == NULL) return NULL;
279 for (; sock; sock = sock->next) {
280 if(!flags || (sock->flags & flags) == flags)
286 void free_sockets() {
288 struct ClientSocket *client, *next;
289 for (client = sockets->data; client; client = next) {
291 destroy_socket(client);