*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     return 1;
112 }
113
114 int write_socket(struct ClientSocket *client, char* msg, int len) {
115     if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
116     printf("[send %d] %s", len, msg);
117     write(client->sock, msg, len);
118     return 1;
119 }
120
121 void socket_loop(int timeout_seconds) {
122     if(sockets == NULL) return;
123     fd_set fds;
124     struct timeval timeout;
125     struct ClientSocket *sock;
126     int ret = 0, bytes, i;
127     
128     FD_ZERO(&fds);
129     for (sock = sockets->data; sock; sock = sock->next) {
130         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
131         FD_SET(sock->sock, &fds);
132         if(sock->sock > ret)
133             ret = sock->sock;
134     }
135     timeout.tv_sec = timeout_seconds;
136     timeout.tv_usec = 0;
137     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
138     if(ret == 0) return;
139     for (sock = sockets->data; sock; sock = sock->next) {
140         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
141             if(sock->bufferpos != 0) {
142                 bytes = read(sock->sock, buffer, sizeof(buffer));
143                 if(bytes > 0) {
144                     for(i = 0; i < bytes; i++) {
145                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
146                         sock->buffer[sock->bufferpos + i] = buffer[i];
147                     }
148                     sock->bufferpos += i;
149                 }
150             } else {
151                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
152                 if(bytes > 0)
153                     sock->bufferpos = bytes;
154             }
155             if(bytes >= 0) {
156                 //error
157                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
158             } else {
159                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
160                 if(used == sock->bufferpos + 1) {
161                     //used all bytes so just reset the bufferpos
162                     sock->bufferpos = 0;
163                 } else {
164                     for(i = 0; i < sock->bufferpos - used; i++) {
165                         sock->buffer[i] = sock->buffer[i+used];
166                     }
167                     sock->bufferpos -= used;
168                 }
169             }
170         }
171     }
172 }
173