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, struct UserNode *user) {
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));
59 client->bufferpos = 0;
60 client->traffic_in = 0;
61 client->traffic_out = 0;
62 client->connection_time = 0;
66 client->whoqueue_first = NULL;
67 client->whoqueue_last = NULL;
68 client->handleinfo_first = NULL;
69 client->handleinfo_last = NULL;
70 client->next = sockets->data;
71 sockets->data = client;
77 int connect_socket(struct ClientSocket *client) {
78 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
80 struct sockaddr_in addr;
82 addr.sin_addr.s_addr = inet_addr(client->host);
83 if (addr.sin_addr.s_addr == INADDR_NONE) {
84 host = gethostbyname(client->host);
88 memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
90 sock = socket(PF_INET, SOCK_STREAM, 0);
93 perror("socket() failed");
97 addr.sin_port = htons(client->port);
98 addr.sin_family = AF_INET;
100 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
102 perror("connect() failed");
107 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
108 client->connection_time = time(0);
110 //send the IRC Headers
114 if(client->pass && strcmp(client->pass, "")) {
115 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
116 write_socket(client, sendBuf, len);
118 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
119 write_socket(client, sendBuf, len);
120 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
121 write_socket(client, sendBuf, len);
128 int connect_socket(struct ClientSocket *client) {
129 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
130 struct hostent *host;
131 struct sockaddr_in addr;
133 if (!inet_aton(client->host, &addr.sin_addr))
135 host = gethostbyname(client->host);
138 perror("gethostbyname() failed");
141 addr.sin_addr = *(struct in_addr*)host->h_addr;
143 sock = socket(PF_INET, SOCK_STREAM, 0);
146 perror("socket() failed");
150 addr.sin_port = htons(client->port);
151 addr.sin_family = AF_INET;
153 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
155 perror("connect() failed");
160 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
161 client->connection_time = time(0);
163 //send the IRC Headers
167 if(client->pass && strcmp(client->pass, "")) {
168 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
169 write_socket(client, sendBuf, len);
171 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
172 write_socket(client, sendBuf, len);
173 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
174 write_socket(client, sendBuf, len);
181 int close_socket(struct ClientSocket *client) {
182 if(client == NULL) return 0;
183 if((client->flags & SOCKET_FLAG_CONNECTED))
185 struct ClientSocket *sock, *last_sock = NULL;
186 for (sock = sockets->data; sock; sock = sock->next) {
189 last_sock->next = sock->next;
191 sockets->data = sock->next;
197 queue_destroy(client);
198 if(client->whoqueue_first)
199 clear_whoqueue(client);
200 if(client->handleinfo_first)
201 clear_handleinfoqueue(client);
208 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
209 printf("[send %d] %s", len, msg);
211 send(client->sock, msg, len, 0);
213 write(client->sock, msg, len);
215 client->traffic_out += len;
219 int write_socket(struct ClientSocket *client, char* msg, int len) {
220 if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
221 if(client->flags & SOCKET_FLAG_USE_QUEUE)
222 return queue_add(client, msg, len);
224 return write_socket_force(client, msg, len);
227 void socket_loop(int timeout_seconds) {
228 if(sockets == NULL) return;
230 struct timeval timeout;
231 struct ClientSocket *sock;
232 int ret = 0, bytes, i;
235 for (sock = sockets->data; sock; sock = sock->next) {
236 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
237 FD_SET(sock->sock, &fds);
241 timeout.tv_sec = timeout_seconds;
243 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
245 for (sock = sockets->data; sock; sock = sock->next) {
246 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
247 if(sock->bufferpos != 0) {
249 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
251 bytes = read(sock->sock, buffer, sizeof(buffer));
254 for(i = 0; i < bytes; i++) {
255 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
256 sock->buffer[sock->bufferpos + i] = buffer[i];
258 sock->bufferpos += i;
262 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
264 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
267 sock->bufferpos = bytes;
271 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
272 bot_disconnect(sock);
276 sock->traffic_in += bytes;
277 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
278 if(used == sock->bufferpos + 1) {
279 //used all bytes so just reset the bufferpos
282 for(i = 0; i < sock->bufferpos - used; i++) {
283 sock->buffer[i] = sock->buffer[i+used];
285 sock->bufferpos -= used;
288 } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
289 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
290 connect_socket(sock);
297 putsock(struct ClientSocket *client, const char *text, ...)
300 char sendBuf[MAXLEN];
302 if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
304 va_start(arg_list, text);
305 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
307 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
309 sendBuf[pos+1] = '\0';
310 write_socket(client, sendBuf, pos+1);
313 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
314 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
315 if(sock == NULL) return NULL;
316 for (; sock; sock = sock->next) {
317 if(!flags || (sock->flags & flags) == flags)
323 void free_sockets() {
325 struct ClientSocket *client, *next;
326 for (client = sockets->data; client; client = next) {
328 if((client->flags & SOCKET_FLAG_CONNECTED))
331 queue_destroy(client);