some modifications for WIN32 support :)
[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", 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) {
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     write(client->sock, msg, len);
182     client->traffic_out += len;
183     return 1;
184 }
185
186 void socket_loop(int timeout_seconds) {
187     if(sockets == NULL) return;
188     fd_set fds;
189     struct timeval timeout;
190     struct ClientSocket *sock;
191     int ret = 0, bytes, i;
192     
193     FD_ZERO(&fds);
194     for (sock = sockets->data; sock; sock = sock->next) {
195         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
196         FD_SET(sock->sock, &fds);
197         if(sock->sock > ret)
198             ret = sock->sock;
199     }
200     timeout.tv_sec = timeout_seconds;
201     timeout.tv_usec = 0;
202     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
203     if(ret == 0) return;
204     for (sock = sockets->data; sock; sock = sock->next) {
205         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
206             if(sock->bufferpos != 0) {
207                 bytes = read(sock->sock, buffer, sizeof(buffer));
208                 if(bytes > 0) {
209                     for(i = 0; i < bytes; i++) {
210                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
211                         sock->buffer[sock->bufferpos + i] = buffer[i];
212                     }
213                     sock->bufferpos += i;
214                 }
215             } else {
216                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
217                 if(bytes > 0)
218                     sock->bufferpos = bytes;
219             }
220             if(bytes <= 0) {
221                 //error
222                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
223                 bot_disconnect(sock);
224             } else {
225                 sock->traffic_in += bytes;
226                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
227                 if(used == sock->bufferpos + 1) {
228                     //used all bytes so just reset the bufferpos
229                     sock->bufferpos = 0;
230                 } else {
231                     for(i = 0; i < sock->bufferpos - used; i++) {
232                         sock->buffer[i] = sock->buffer[i+used];
233                     }
234                     sock->bufferpos -= used;
235                 }
236             }
237         }
238     }
239 }
240
241 void
242 putsock(struct ClientSocket *client, const char *text, ...)
243 {
244     va_list arg_list;
245     char sendBuf[MAXLEN];
246     int pos;
247     if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
248     sendBuf[0] = '\0';
249     va_start(arg_list, text);
250     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
251     va_end(arg_list);
252     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
253     sendBuf[pos] = '\n';
254     sendBuf[pos+1] = '\0';
255     write_socket(client, sendBuf, pos+1);
256 }
257
258 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
259     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
260     if(sock == NULL) return NULL;
261     for (; sock; sock = sock->next) {
262         if(!flags || (sock->flags & flags) == flags)
263             return sock;
264     }
265     return NULL;
266 }
267
268 void free_sockets() {
269     if(!sockets) return;
270     struct ClientSocket *client, *next;
271     for (client = sockets->data; client; client = next) {
272         next = client->next;
273         if((client->flags & SOCKET_FLAG_CONNECTED))
274             close(client->sock);
275         free(client->host);
276         free(client->pass);
277         free(client);
278     }
279     free(sockets);
280     sockets = NULL;
281 }