1 /* ClientSocket.c - NeonServ v5.2
2 * Copyright (C) 2011 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"
26 struct ClientSocket *data;
31 static struct socket_list *sockets = NULL;
32 static char buffer[BUF_SIZ];
34 static void init_sockets() {
35 sockets = malloc(sizeof(*sockets));
38 perror("malloc() failed");
45 struct ClientSocket* create_socket(char *host, int port, char *pass, char *nick, char *ident, char *realname) {
46 if(sockets == NULL) init_sockets();
47 struct ClientSocket *client = malloc(sizeof(*client));
50 perror("malloc() failed");
53 client->host = strdup(host);
55 printf("Connect: %s:%d\n", client->host, client->port);
56 client->pass = (pass == NULL ? NULL : strdup(pass));
57 client->nick = strdup(nick);
58 client->ident = strdup(ident);
59 client->realname = strdup(realname);
62 client->bufferpos = 0;
63 client->traffic_in = 0;
64 client->traffic_out = 0;
65 client->connection_time = 0;
69 client->whoqueue_first = NULL;
70 client->whoqueue_last = NULL;
71 client->handleinfo_first = NULL;
72 client->handleinfo_last = NULL;
73 client->next = sockets->data;
74 sockets->data = client;
80 int connect_socket(struct ClientSocket *client) {
81 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
83 struct sockaddr_in addr;
85 addr.sin_addr.s_addr = inet_addr(client->host);
86 if (addr.sin_addr.s_addr == INADDR_NONE) {
87 host = gethostbyname(client->host);
91 memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
93 sock = socket(PF_INET, SOCK_STREAM, 0);
96 perror("socket() failed");
100 addr.sin_port = htons(client->port);
101 addr.sin_family = AF_INET;
103 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
105 perror("connect() failed");
110 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
111 client->connection_time = time(0);
113 //send the IRC Headers
117 if(client->pass && strcmp(client->pass, "")) {
118 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
119 write_socket(client, sendBuf, len);
121 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
122 write_socket(client, sendBuf, len);
123 len = sprintf(sendBuf, "NICK %s\n", client->nick);
124 write_socket(client, sendBuf, len);
131 int connect_socket(struct ClientSocket *client) {
132 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
133 struct hostent *host;
134 struct sockaddr_in addr;
136 if (!inet_aton(client->host, &addr.sin_addr))
138 host = gethostbyname(client->host);
141 perror("gethostbyname() failed");
144 addr.sin_addr = *(struct in_addr*)host->h_addr;
146 sock = socket(PF_INET, SOCK_STREAM, 0);
149 perror("socket() failed");
153 addr.sin_port = htons(client->port);
154 addr.sin_family = AF_INET;
156 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
158 perror("connect() failed");
163 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
164 client->connection_time = time(0);
166 //send the IRC Headers
170 if(client->pass && strcmp(client->pass, "")) {
171 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
172 write_socket(client, sendBuf, len);
174 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
175 write_socket(client, sendBuf, len);
176 len = sprintf(sendBuf, "NICK %s\n", client->nick);
177 write_socket(client, sendBuf, len);
184 int close_socket(struct ClientSocket *client) {
185 if(client == NULL) return 0;
186 if((client->flags & SOCKET_FLAG_CONNECTED))
188 struct ClientSocket *sock, *last_sock = NULL;
189 for (sock = sockets->data; sock; sock = sock->next) {
192 last_sock->next = sock->next;
194 sockets->data = sock->next;
200 queue_destroy(client);
201 if(client->whoqueue_first)
202 clear_whoqueue(client);
203 if(client->handleinfo_first)
204 clear_handleinfoqueue(client);
211 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
212 printf("[send %d] %s", len, msg);
214 send(client->sock, msg, len, 0);
216 write(client->sock, msg, len);
218 client->traffic_out += len;
222 int write_socket(struct ClientSocket *client, char* msg, int len) {
223 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
224 if(client->flags & SOCKET_FLAG_USE_QUEUE)
225 return queue_add(client, msg, len);
227 return write_socket_force(client, msg, len);
230 void socket_loop(int timeout_seconds) {
231 if(sockets == NULL) return;
233 struct timeval timeout;
234 struct ClientSocket *sock;
235 int ret = 0, bytes, i;
238 for (sock = sockets->data; sock; sock = sock->next) {
239 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
240 FD_SET(sock->sock, &fds);
244 timeout.tv_sec = timeout_seconds;
246 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
248 for (sock = sockets->data; sock; sock = sock->next) {
249 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
250 if(sock->bufferpos != 0) {
252 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
254 bytes = read(sock->sock, buffer, sizeof(buffer));
257 for(i = 0; i < bytes; i++) {
258 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
259 sock->buffer[sock->bufferpos + i] = buffer[i];
261 sock->bufferpos += i;
265 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
267 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
270 sock->bufferpos = bytes;
274 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
275 bot_disconnect(sock);
279 sock->traffic_in += bytes;
280 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
281 if(used == sock->bufferpos + 1) {
282 //used all bytes so just reset the bufferpos
285 for(i = 0; i < sock->bufferpos - used; i++) {
286 sock->buffer[i] = sock->buffer[i+used];
288 sock->bufferpos -= used;
291 } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
292 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
293 connect_socket(sock);
300 putsock(struct ClientSocket *client, const char *text, ...)
303 char sendBuf[MAXLEN];
305 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
307 va_start(arg_list, text);
308 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
310 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
312 sendBuf[pos+1] = '\0';
313 write_socket(client, sendBuf, pos+1);
316 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
317 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
318 if(sock == NULL) return NULL;
319 for (; sock; sock = sock->next) {
320 if(!flags || (sock->flags & flags) == flags)
326 void free_sockets() {
328 struct ClientSocket *client, *next;
329 for (client = sockets->data; client; client = next) {
331 if((client->flags & SOCKET_FLAG_CONNECTED))
334 queue_destroy(client);