30c2efb4ec7e2d9c7cf69280fc47719720a36884
[NeonServV5.git] / src / ClientSocket.c
1
2 #include "ClientSocket.h"
3 #include "IRCParser.h"
4 #include "UserNode.h"
5
6 struct socket_list {
7     struct ClientSocket *data;
8     unsigned count;
9 };
10
11 //the magic list :P
12 static struct socket_list *sockets = NULL;
13 static char buffer[BUF_SIZ];
14
15 static void init_sockets() {
16     sockets = malloc(sizeof(*sockets));
17     if (!sockets)
18     {
19         perror("malloc() failed");
20         return;
21     }
22     sockets->data = NULL;
23     sockets->count = 0;
24 }
25
26 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
27     if(sockets == NULL) init_sockets();
28     struct ClientSocket *client = malloc(sizeof(*client));
29     if (!client)
30     {
31         perror("malloc() failed");
32         return NULL;
33     }
34     client->host = strdup(host);
35     client->port = port;
36     printf("Connect: %s:%d\n", client->host, client->port);
37     client->pass = (pass == NULL ? NULL : strdup(pass));
38     client->user = user;
39     client->flags = 0;
40     client->bufferpos = 0;
41     client->traffic_in = 0;
42     client->traffic_out = 0;
43     client->connection_time = 0;
44         client->botid = 0;
45     client->clientid = 0;
46     client->next = sockets->data;
47     sockets->data = client;
48     return client;
49 }
50
51 #ifdef WIN32
52
53 int connect_socket(struct ClientSocket *client) {
54     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
55     struct hostent *host;
56     struct sockaddr_in addr;
57     int sock;
58     addr.sin_addr.s_addr = inet_addr(client->host);
59     if (addr.sin_addr.s_addr == INADDR_NONE) {
60         host = gethostbyname(client->host);
61         if(!host) {
62             return SOCKET_ERROR;
63         }
64         memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
65     }
66     sock = socket(PF_INET, SOCK_STREAM, 0);
67     if (sock == -1)
68     {
69         perror("socket() failed");
70         return 0;
71     }
72
73     addr.sin_port = htons(client->port);
74     addr.sin_family = AF_INET;
75
76     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
77     {
78         perror("connect() failed");
79         return 0;
80     }
81
82     client->sock = sock;
83     client->flags |= SOCKET_FLAG_CONNECTED;
84     client->connection_time = time(0);
85
86     //send the IRC Headers
87     char sendBuf[512];
88     int len;
89
90     if(client->pass && strcmp(client->pass, "")) {
91         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
92         write_socket(client, sendBuf, len);
93     }
94     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
95     write_socket(client, sendBuf, len);
96     len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
97     write_socket(client, sendBuf, len);
98
99     return 1;
100 }
101
102 #else
103
104 int connect_socket(struct ClientSocket *client) {
105     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
106     struct hostent *host;
107     struct sockaddr_in addr;
108     int sock;
109     if (!inet_aton(client->host, &addr.sin_addr))
110     {
111         host = gethostbyname(client->host);
112         if (!host)
113         {
114             perror("gethostbyname() failed");
115             return 0;
116         }
117         addr.sin_addr = *(struct in_addr*)host->h_addr;
118     }
119     sock = socket(PF_INET, SOCK_STREAM, 0);
120     if (sock == -1)
121     {
122         perror("socket() failed");
123         return 0;
124     }
125
126     addr.sin_port = htons(client->port);
127     addr.sin_family = AF_INET;
128
129     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
130     {
131         perror("connect() failed");
132         return 0;
133     }
134
135     client->sock = sock;
136     client->flags |= SOCKET_FLAG_CONNECTED;
137     client->connection_time = time(0);
138
139     //send the IRC Headers
140     char sendBuf[512];
141     int len;
142
143     if(client->pass) {
144         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
145         write_socket(client, sendBuf, len);
146     }
147     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
148     write_socket(client, sendBuf, len);
149     len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
150     write_socket(client, sendBuf, len);
151
152     return 1;
153 }
154
155 #endif
156
157 int close_socket(struct ClientSocket *client) {
158     if(client == NULL) return 0;
159     if((client->flags & SOCKET_FLAG_CONNECTED))
160         close(client->sock);
161     struct ClientSocket *sock, *last_sock = NULL;
162     for (sock = sockets->data; sock; sock = sock->next) {
163         if(sock == client) {
164             if(last_sock)
165                 last_sock->next = sock->next;
166             else
167                 sockets->data = sock->next;
168             sockets->count--;
169         } else
170             last_sock = sock;
171     }
172     free(client->host);
173     free(client->pass);
174     free(client);
175     return 1;
176 }
177
178 int write_socket(struct ClientSocket *client, char* msg, int len) {
179     if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
180     printf("[send %d] %s", len, msg);
181     #ifdef WIN32
182     send(client->sock, msg, len, 0);
183     #else
184     write(client->sock, msg, len);
185     #endif
186     client->traffic_out += len;
187     return 1;
188 }
189
190 void socket_loop(int timeout_seconds) {
191     if(sockets == NULL) return;
192     fd_set fds;
193     struct timeval timeout;
194     struct ClientSocket *sock;
195     int ret = 0, bytes, i;
196     
197     FD_ZERO(&fds);
198     for (sock = sockets->data; sock; sock = sock->next) {
199         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
200         FD_SET(sock->sock, &fds);
201         if(sock->sock > ret)
202             ret = sock->sock;
203     }
204     timeout.tv_sec = timeout_seconds;
205     timeout.tv_usec = 0;
206     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
207     if(ret == 0) return;
208     for (sock = sockets->data; sock; sock = sock->next) {
209         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
210             if(sock->bufferpos != 0) {
211                 #ifdef WIN32
212                 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
213                 #else
214                 bytes = read(sock->sock, buffer, sizeof(buffer));
215                 #endif
216                 if(bytes > 0) {
217                     for(i = 0; i < bytes; i++) {
218                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
219                         sock->buffer[sock->bufferpos + i] = buffer[i];
220                     }
221                     sock->bufferpos += i;
222                 }
223             } else {
224                 #ifdef WIN32
225                 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
226                 #else
227                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
228                 #endif
229                 if(bytes > 0)
230                     sock->bufferpos = bytes;
231             }
232             if(bytes <= 0) {
233                 //error
234                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
235                 bot_disconnect(sock);
236             } else {
237                 sock->traffic_in += bytes;
238                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
239                 if(used == sock->bufferpos + 1) {
240                     //used all bytes so just reset the bufferpos
241                     sock->bufferpos = 0;
242                 } else {
243                     for(i = 0; i < sock->bufferpos - used; i++) {
244                         sock->buffer[i] = sock->buffer[i+used];
245                     }
246                     sock->bufferpos -= used;
247                 }
248             }
249         }
250     }
251 }
252
253 void
254 putsock(struct ClientSocket *client, const char *text, ...)
255 {
256     va_list arg_list;
257     char sendBuf[MAXLEN];
258     int pos;
259     if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
260     sendBuf[0] = '\0';
261     va_start(arg_list, text);
262     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
263     va_end(arg_list);
264     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
265     sendBuf[pos] = '\n';
266     sendBuf[pos+1] = '\0';
267     write_socket(client, sendBuf, pos+1);
268 }
269
270 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
271     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
272     if(sock == NULL) return NULL;
273     for (; sock; sock = sock->next) {
274         if(!flags || (sock->flags & flags) == flags)
275             return sock;
276     }
277     return NULL;
278 }
279
280 void free_sockets() {
281     if(!sockets) return;
282     struct ClientSocket *client, *next;
283     for (client = sockets->data; client; client = next) {
284         next = client->next;
285         if((client->flags & SOCKET_FLAG_CONNECTED))
286             close(client->sock);
287         free(client->host);
288         free(client->pass);
289         free(client);
290     }
291     free(sockets);
292     sockets = NULL;
293 }