*first commit :D*
[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;
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 }
42
43 int connect_socket(struct ClientSocket *socket) {
44     if((socket->flsgs & SOCKET_FLAG_CONNECTED)) return 1;
45     struct hostent *host;
46     struct sockaddr_in addr;
47     int sock;
48     if (!inet_aton(socket->host, &addr.sin_addr))
49     {
50         host = gethostbyname(socket->host);
51         if (!host)
52         {
53             herror("gethostbyname() failed");
54             return 0;
55         }
56         addr.sin_addr = *(struct in_addr*)host->h_addr;
57     }
58     sock = socket(PF_INET, SOCK_STREAM, 0);
59     if (sock == -1)
60     {
61         perror("socket() failed");
62         return 0;
63     }
64
65     addr.sin_port = htons(socket->port);
66     addr.sin_family = AF_INET;
67
68     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
69     {
70         perror("connect() failed");
71         return 0;
72     }
73
74     //send the IRC Headers
75     char sendBuf[512];
76     int len;
77
78     if(socket->pass) {
79         len = sprintf(sendBuf, "PASS :%s\n", socket->pass);
80         write_socket(sock, sendBuf, len);
81     }
82     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", socket->user->ident, socket->user->realname);
83     write_socket(sock, sendBuf, len);
84     len = sprintf(sendBuf, "NICK %s\n", socket->user->nick);
85     write_socket(sock, sendBuf, len);
86
87     socket->socket = sock;
88     socket->flags |= SOCKET_FLAG_CONNECTED;
89     return 1;
90 }
91
92 int close_socket(struct ClientSocket *socket) {
93     if(socket == NULL) return 0;
94     if((socket->flags & SOCKET_FLAG_CONNECTED))
95         close(socket->sock);
96     struct ClientSocket *sock, *last_sock = NULL;
97     for (sock = sockets->data; sock; sock = sock->next) {
98         if(sock == socket) {
99             if(last_sock)
100                 last_sock->next = sock->next;
101             else
102                 sockets->data = sock->next;
103             sockets->count--;
104         } else
105             last_sock = sock;
106     }
107     free(socket->host);
108     free(socket->pass);
109     free(socket);
110 }
111
112 int write_socket(struct ClientSocket *socket, char* msg, int len) {
113     if(!(socket->flags & SOCKET_FLAG_CONNECTED)) return 0;
114     printf("[send %d] %s", len, msg);
115     write(socket->sock, msg, len);
116     return 1;
117 }
118
119 void socket_loop(int timeout) {
120     if(sockets == NULL) return;
121     fd_set fds;
122     struct timeval timeout;
123     struct ClientSocket *sock;
124     int ret = 0, bytes, i;
125     
126     FD_ZERO(&fds);
127     for (sock = sockets->data; sock; sock = sock->next) {
128         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
129         FD_SET(sock->sock, fds);
130         if(sock->sock > ret)
131             ret = sock->sock;
132     }
133     timeout.tv_sec = timeout;
134     timeout.tv_usec = 0;
135     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
136     if(ret == 0) return;
137     for (sock = sockets->data; sock; sock = sock->next) {
138         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
139             if(sock->bufferpos != 0) {
140                 bytes = read(sock->sock, buffer, sizeof(buffer));
141                 if(bytes > 0) {
142                     for(i = 0; i < bytes; i++) {
143                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
144                         sock->buffer[sock->bufferpos + i] = buffer[i];
145                     }
146                     sock->bufferpos += i;
147                 }
148             } else {
149                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
150                 if(bytes > 0)
151                     sock->bufferpos = bytes;
152             }
153             if(bytes >= 0) {
154                 //error
155                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
156             } else {
157                 int used = parse_lines(sock->buffer, sock->bufferpos);
158                 if(used == sock->bufferpos + 1) {
159                     //used all bytes so just reset the bufferpos
160                     sock->bufferpos = 0;
161                 } else {
162                     for(i = 0; i < sock->bufferpos - used; i++) {
163                         sock->buffer[i] = sock->buffer[i+used];
164                     }
165                     sock->bufferpos -= used;
166                 }
167             }
168         }
169     }
170 }
171