*push*
[NeonServV5.git] / ClientSocket.c
1
2 #include "ClientSocket.h"
3 #include "IRCParser.h"
4
5 struct socket_list {
6     struct ClientSocket *data;
7     unsigned count;
8 };
9
10 //the magic list :P
11 static struct socket_list *sockets = NULL;
12 static char buffer[BUF_SIZ];
13
14 static void init() {
15     sockets = malloc(sizeof(*sockets));
16     if (!sockets)
17     {
18         perror("malloc() failed");
19         return;
20     }
21     sockets->data = NULL;
22     sockets->count = 0;
23 }
24
25 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
26     if(sockets == NULL) init();
27     struct ClientSocket *client = malloc(sizeof(*client));
28     if (!client)
29     {
30         perror("malloc() failed");
31         return NULL;
32     }
33     client->host = strdup(host);
34     client->port = port;
35     client->pass = (pass == NULL ? NULL : strdup(pass));
36     client->user = user;
37     client->flags = 0;
38     client->bufferpos = 0;
39     client->next = sockets->data;
40     sockets->data = client;
41     return client;
42 }
43
44 int connect_socket(struct ClientSocket *client) {
45     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
46     struct hostent *host;
47     struct sockaddr_in addr;
48     int sock;
49     if (!inet_aton(client->host, &addr.sin_addr))
50     {
51         host = gethostbyname(client->host);
52         if (!host)
53         {
54             herror("gethostbyname() failed");
55             return 0;
56         }
57         addr.sin_addr = *(struct in_addr*)host->h_addr;
58     }
59     sock = socket(PF_INET, SOCK_STREAM, 0);
60     if (sock == -1)
61     {
62         perror("socket() failed");
63         return 0;
64     }
65
66     addr.sin_port = htons(client->port);
67     addr.sin_family = AF_INET;
68
69     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
70     {
71         perror("connect() failed");
72         return 0;
73     }
74
75     //send the IRC Headers
76     char sendBuf[512];
77     int len;
78
79     if(client->pass) {
80         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
81         write_socket(client, sendBuf, len);
82     }
83     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
84     write_socket(client, sendBuf, len);
85     len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
86     write_socket(client, sendBuf, len);
87
88     client->sock = sock;
89     client->flags |= SOCKET_FLAG_CONNECTED;
90     return 1;
91 }
92
93 int close_socket(struct ClientSocket *client) {
94     if(client == NULL) return 0;
95     if((client->flags & SOCKET_FLAG_CONNECTED))
96         close(client->sock);
97     struct ClientSocket *sock, *last_sock = NULL;
98     for (sock = sockets->data; sock; sock = sock->next) {
99         if(sock == client) {
100             if(last_sock)
101                 last_sock->next = sock->next;
102             else
103                 sockets->data = sock->next;
104             sockets->count--;
105         } else
106             last_sock = sock;
107     }
108     free(client->host);
109     free(client->pass);
110     free(client);
111 }
112
113 int write_socket(struct ClientSocket *client, char* msg, int len) {
114     if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
115     printf("[send %d] %s", len, msg);
116     write(client->sock, msg, len);
117     return 1;
118 }
119
120 void socket_loop(int timeout_seconds) {
121     if(sockets == NULL) return;
122     fd_set fds;
123     struct timeval timeout;
124     struct ClientSocket *sock;
125     int ret = 0, bytes, i;
126     
127     FD_ZERO(&fds);
128     for (sock = sockets->data; sock; sock = sock->next) {
129         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
130         FD_SET(sock->sock, &fds);
131         if(sock->sock > ret)
132             ret = sock->sock;
133     }
134     timeout.tv_sec = timeout_seconds;
135     timeout.tv_usec = 0;
136     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
137     if(ret == 0) return;
138     for (sock = sockets->data; sock; sock = sock->next) {
139         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
140             if(sock->bufferpos != 0) {
141                 bytes = read(sock->sock, buffer, sizeof(buffer));
142                 if(bytes > 0) {
143                     for(i = 0; i < bytes; i++) {
144                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
145                         sock->buffer[sock->bufferpos + i] = buffer[i];
146                     }
147                     sock->bufferpos += i;
148                 }
149             } else {
150                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
151                 if(bytes > 0)
152                     sock->bufferpos = bytes;
153             }
154             if(bytes >= 0) {
155                 //error
156                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
157             } else {
158                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
159                 if(used == sock->bufferpos + 1) {
160                     //used all bytes so just reset the bufferpos
161                     sock->bufferpos = 0;
162                 } else {
163                     for(i = 0; i < sock->bufferpos - used; i++) {
164                         sock->buffer[i] = sock->buffer[i+used];
165                     }
166                     sock->bufferpos -= used;
167                 }
168             }
169         }
170     }
171 }
172