1 /* ClientSocket.c - NeonServ v5.1
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"
24 struct ClientSocket *data;
29 static struct socket_list *sockets = NULL;
30 static char buffer[BUF_SIZ];
32 static void init_sockets() {
33 sockets = malloc(sizeof(*sockets));
36 perror("malloc() failed");
43 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
44 if(sockets == NULL) init_sockets();
45 struct ClientSocket *client = malloc(sizeof(*client));
48 perror("malloc() failed");
51 client->host = strdup(host);
53 printf("Connect: %s:%d\n", client->host, client->port);
54 client->pass = (pass == NULL ? NULL : strdup(pass));
57 client->bufferpos = 0;
58 client->traffic_in = 0;
59 client->traffic_out = 0;
60 client->connection_time = 0;
64 client->next = sockets->data;
65 sockets->data = client;
71 int connect_socket(struct ClientSocket *client) {
72 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
74 struct sockaddr_in addr;
76 addr.sin_addr.s_addr = inet_addr(client->host);
77 if (addr.sin_addr.s_addr == INADDR_NONE) {
78 host = gethostbyname(client->host);
82 memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
84 sock = socket(PF_INET, SOCK_STREAM, 0);
87 perror("socket() failed");
91 addr.sin_port = htons(client->port);
92 addr.sin_family = AF_INET;
94 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
96 perror("connect() failed");
101 client->flags |= SOCKET_FLAG_CONNECTED;
102 client->connection_time = time(0);
104 //send the IRC Headers
108 if(client->pass && strcmp(client->pass, "")) {
109 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
110 write_socket(client, sendBuf, len);
112 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
113 write_socket(client, sendBuf, len);
114 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
115 write_socket(client, sendBuf, len);
122 int connect_socket(struct ClientSocket *client) {
123 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
124 struct hostent *host;
125 struct sockaddr_in addr;
127 if (!inet_aton(client->host, &addr.sin_addr))
129 host = gethostbyname(client->host);
132 perror("gethostbyname() failed");
135 addr.sin_addr = *(struct in_addr*)host->h_addr;
137 sock = socket(PF_INET, SOCK_STREAM, 0);
140 perror("socket() failed");
144 addr.sin_port = htons(client->port);
145 addr.sin_family = AF_INET;
147 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
149 perror("connect() failed");
154 client->flags |= SOCKET_FLAG_CONNECTED;
155 client->connection_time = time(0);
157 //send the IRC Headers
162 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
163 write_socket(client, sendBuf, len);
165 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
166 write_socket(client, sendBuf, len);
167 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
168 write_socket(client, sendBuf, len);
175 int close_socket(struct ClientSocket *client) {
176 if(client == NULL) return 0;
177 if((client->flags & SOCKET_FLAG_CONNECTED))
179 struct ClientSocket *sock, *last_sock = NULL;
180 for (sock = sockets->data; sock; sock = sock->next) {
183 last_sock->next = sock->next;
185 sockets->data = sock->next;
191 queue_destroy(client);
198 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
199 printf("[send %d] %s", len, msg);
201 send(client->sock, msg, len, 0);
203 write(client->sock, msg, len);
205 client->traffic_out += len;
209 int write_socket(struct ClientSocket *client, char* msg, int len) {
210 if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
211 if(client->flags & SOCKET_FLAG_USE_QUEUE)
212 return queue_add(client, msg, len);
214 return write_socket_force(client, msg, len);
217 void socket_loop(int timeout_seconds) {
218 if(sockets == NULL) return;
220 struct timeval timeout;
221 struct ClientSocket *sock;
222 int ret = 0, bytes, i;
225 for (sock = sockets->data; sock; sock = sock->next) {
226 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
227 FD_SET(sock->sock, &fds);
231 timeout.tv_sec = timeout_seconds;
233 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
235 for (sock = sockets->data; sock; sock = sock->next) {
236 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
237 if(sock->bufferpos != 0) {
239 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
241 bytes = read(sock->sock, buffer, sizeof(buffer));
244 for(i = 0; i < bytes; i++) {
245 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
246 sock->buffer[sock->bufferpos + i] = buffer[i];
248 sock->bufferpos += i;
252 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
254 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
257 sock->bufferpos = bytes;
261 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
262 bot_disconnect(sock);
266 sock->traffic_in += bytes;
267 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
268 if(used == sock->bufferpos + 1) {
269 //used all bytes so just reset the bufferpos
272 for(i = 0; i < sock->bufferpos - used; i++) {
273 sock->buffer[i] = sock->buffer[i+used];
275 sock->bufferpos -= used;
283 putsock(struct ClientSocket *client, const char *text, ...)
286 char sendBuf[MAXLEN];
288 if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
290 va_start(arg_list, text);
291 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
293 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
295 sendBuf[pos+1] = '\0';
296 write_socket(client, sendBuf, pos+1);
299 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
300 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
301 if(sock == NULL) return NULL;
302 for (; sock; sock = sock->next) {
303 if(!flags || (sock->flags & flags) == flags)
309 void free_sockets() {
311 struct ClientSocket *client, *next;
312 for (client = sockets->data; client; client = next) {
314 if((client->flags & SOCKET_FLAG_CONNECTED))
317 queue_destroy(client);