1 /* ClientSocket.c - NeonServ v5.0
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"
23 struct ClientSocket *data;
28 static struct socket_list *sockets = NULL;
29 static char buffer[BUF_SIZ];
31 static void init_sockets() {
32 sockets = malloc(sizeof(*sockets));
35 perror("malloc() failed");
42 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
43 if(sockets == NULL) init_sockets();
44 struct ClientSocket *client = malloc(sizeof(*client));
47 perror("malloc() failed");
50 client->host = strdup(host);
52 printf("Connect: %s:%d\n", client->host, client->port);
53 client->pass = (pass == NULL ? NULL : strdup(pass));
56 client->bufferpos = 0;
57 client->traffic_in = 0;
58 client->traffic_out = 0;
59 client->connection_time = 0;
62 client->next = sockets->data;
63 sockets->data = client;
69 int connect_socket(struct ClientSocket *client) {
70 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
72 struct sockaddr_in addr;
74 addr.sin_addr.s_addr = inet_addr(client->host);
75 if (addr.sin_addr.s_addr == INADDR_NONE) {
76 host = gethostbyname(client->host);
80 memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
82 sock = socket(PF_INET, SOCK_STREAM, 0);
85 perror("socket() failed");
89 addr.sin_port = htons(client->port);
90 addr.sin_family = AF_INET;
92 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
94 perror("connect() failed");
99 client->flags |= SOCKET_FLAG_CONNECTED;
100 client->connection_time = time(0);
102 //send the IRC Headers
106 if(client->pass && strcmp(client->pass, "")) {
107 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
108 write_socket(client, sendBuf, len);
110 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
111 write_socket(client, sendBuf, len);
112 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
113 write_socket(client, sendBuf, len);
120 int connect_socket(struct ClientSocket *client) {
121 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
122 struct hostent *host;
123 struct sockaddr_in addr;
125 if (!inet_aton(client->host, &addr.sin_addr))
127 host = gethostbyname(client->host);
130 perror("gethostbyname() failed");
133 addr.sin_addr = *(struct in_addr*)host->h_addr;
135 sock = socket(PF_INET, SOCK_STREAM, 0);
138 perror("socket() failed");
142 addr.sin_port = htons(client->port);
143 addr.sin_family = AF_INET;
145 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
147 perror("connect() failed");
152 client->flags |= SOCKET_FLAG_CONNECTED;
153 client->connection_time = time(0);
155 //send the IRC Headers
160 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
161 write_socket(client, sendBuf, len);
163 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
164 write_socket(client, sendBuf, len);
165 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
166 write_socket(client, sendBuf, len);
173 int close_socket(struct ClientSocket *client) {
174 if(client == NULL) return 0;
175 if((client->flags & SOCKET_FLAG_CONNECTED))
177 struct ClientSocket *sock, *last_sock = NULL;
178 for (sock = sockets->data; sock; sock = sock->next) {
181 last_sock->next = sock->next;
183 sockets->data = sock->next;
194 int write_socket(struct ClientSocket *client, char* msg, int len) {
195 if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
196 printf("[send %d] %s", len, msg);
198 send(client->sock, msg, len, 0);
200 write(client->sock, msg, len);
202 client->traffic_out += len;
206 void socket_loop(int timeout_seconds) {
207 if(sockets == NULL) return;
209 struct timeval timeout;
210 struct ClientSocket *sock;
211 int ret = 0, bytes, i;
214 for (sock = sockets->data; sock; sock = sock->next) {
215 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
216 FD_SET(sock->sock, &fds);
220 timeout.tv_sec = timeout_seconds;
222 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
224 for (sock = sockets->data; sock; sock = sock->next) {
225 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
226 if(sock->bufferpos != 0) {
228 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
230 bytes = read(sock->sock, buffer, sizeof(buffer));
233 for(i = 0; i < bytes; i++) {
234 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
235 sock->buffer[sock->bufferpos + i] = buffer[i];
237 sock->bufferpos += i;
241 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
243 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
246 sock->bufferpos = bytes;
250 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
251 bot_disconnect(sock);
253 sock->traffic_in += bytes;
254 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
255 if(used == sock->bufferpos + 1) {
256 //used all bytes so just reset the bufferpos
259 for(i = 0; i < sock->bufferpos - used; i++) {
260 sock->buffer[i] = sock->buffer[i+used];
262 sock->bufferpos -= used;
270 putsock(struct ClientSocket *client, const char *text, ...)
273 char sendBuf[MAXLEN];
275 if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
277 va_start(arg_list, text);
278 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
280 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
282 sendBuf[pos+1] = '\0';
283 write_socket(client, sendBuf, pos+1);
286 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
287 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
288 if(sock == NULL) return NULL;
289 for (; sock; sock = sock->next) {
290 if(!flags || (sock->flags & flags) == flags)
296 void free_sockets() {
298 struct ClientSocket *client, *next;
299 for (client = sockets->data; client; client = next) {
301 if((client->flags & SOCKET_FLAG_CONNECTED))