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"
25 struct ClientSocket *data;
30 static struct socket_list *sockets = NULL;
31 static char buffer[BUF_SIZ];
33 static void init_sockets() {
34 sockets = malloc(sizeof(*sockets));
37 perror("malloc() failed");
44 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
45 if(sockets == NULL) init_sockets();
46 struct ClientSocket *client = malloc(sizeof(*client));
49 perror("malloc() failed");
52 client->host = strdup(host);
54 printf("Connect: %s:%d\n", client->host, client->port);
55 client->pass = (pass == NULL ? NULL : strdup(pass));
58 client->bufferpos = 0;
59 client->traffic_in = 0;
60 client->traffic_out = 0;
61 client->connection_time = 0;
65 client->whoqueue_first = NULL;
66 client->whoqueue_last = NULL;
67 client->next = sockets->data;
68 sockets->data = client;
74 int connect_socket(struct ClientSocket *client) {
75 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
77 struct sockaddr_in addr;
79 addr.sin_addr.s_addr = inet_addr(client->host);
80 if (addr.sin_addr.s_addr == INADDR_NONE) {
81 host = gethostbyname(client->host);
85 memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
87 sock = socket(PF_INET, SOCK_STREAM, 0);
90 perror("socket() failed");
94 addr.sin_port = htons(client->port);
95 addr.sin_family = AF_INET;
97 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
99 perror("connect() failed");
104 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
105 client->connection_time = time(0);
107 //send the IRC Headers
111 if(client->pass && strcmp(client->pass, "")) {
112 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
113 write_socket(client, sendBuf, len);
115 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
116 write_socket(client, sendBuf, len);
117 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
118 write_socket(client, sendBuf, len);
125 int connect_socket(struct ClientSocket *client) {
126 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
127 struct hostent *host;
128 struct sockaddr_in addr;
130 if (!inet_aton(client->host, &addr.sin_addr))
132 host = gethostbyname(client->host);
135 perror("gethostbyname() failed");
138 addr.sin_addr = *(struct in_addr*)host->h_addr;
140 sock = socket(PF_INET, SOCK_STREAM, 0);
143 perror("socket() failed");
147 addr.sin_port = htons(client->port);
148 addr.sin_family = AF_INET;
150 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
152 perror("connect() failed");
157 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
158 client->connection_time = time(0);
160 //send the IRC Headers
164 if(client->pass && strcmp(client->pass, "")) {
165 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
166 write_socket(client, sendBuf, len);
168 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
169 write_socket(client, sendBuf, len);
170 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
171 write_socket(client, sendBuf, len);
178 int close_socket(struct ClientSocket *client) {
179 if(client == NULL) return 0;
180 if((client->flags & SOCKET_FLAG_CONNECTED))
182 struct ClientSocket *sock, *last_sock = NULL;
183 for (sock = sockets->data; sock; sock = sock->next) {
186 last_sock->next = sock->next;
188 sockets->data = sock->next;
194 queue_destroy(client);
195 if(client->whoqueue_first)
196 clear_whoqueue(client);
203 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
204 printf("[send %d] %s", len, msg);
206 send(client->sock, msg, len, 0);
208 write(client->sock, msg, len);
210 client->traffic_out += len;
214 int write_socket(struct ClientSocket *client, char* msg, int len) {
215 if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
216 if(client->flags & SOCKET_FLAG_USE_QUEUE)
217 return queue_add(client, msg, len);
219 return write_socket_force(client, msg, len);
222 void socket_loop(int timeout_seconds) {
223 if(sockets == NULL) return;
225 struct timeval timeout;
226 struct ClientSocket *sock;
227 int ret = 0, bytes, i;
230 for (sock = sockets->data; sock; sock = sock->next) {
231 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
232 FD_SET(sock->sock, &fds);
236 timeout.tv_sec = timeout_seconds;
238 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
240 for (sock = sockets->data; sock; sock = sock->next) {
241 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
242 if(sock->bufferpos != 0) {
244 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
246 bytes = read(sock->sock, buffer, sizeof(buffer));
249 for(i = 0; i < bytes; i++) {
250 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
251 sock->buffer[sock->bufferpos + i] = buffer[i];
253 sock->bufferpos += i;
257 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
259 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
262 sock->bufferpos = bytes;
266 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
267 bot_disconnect(sock);
271 sock->traffic_in += bytes;
272 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
273 if(used == sock->bufferpos + 1) {
274 //used all bytes so just reset the bufferpos
277 for(i = 0; i < sock->bufferpos - used; i++) {
278 sock->buffer[i] = sock->buffer[i+used];
280 sock->bufferpos -= used;
283 } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
284 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
285 connect_socket(sock);
292 putsock(struct ClientSocket *client, const char *text, ...)
295 char sendBuf[MAXLEN];
297 if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
299 va_start(arg_list, text);
300 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
302 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
304 sendBuf[pos+1] = '\0';
305 write_socket(client, sendBuf, pos+1);
308 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
309 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
310 if(sock == NULL) return NULL;
311 for (; sock; sock = sock->next) {
312 if(!flags || (sock->flags & flags) == flags)
318 void free_sockets() {
320 struct ClientSocket *client, *next;
321 for (client = sockets->data; client; client = next) {
323 if((client->flags & SOCKET_FLAG_CONNECTED))
326 queue_destroy(client);