2 #include "ClientSocket.h"
6 struct ClientSocket *data;
11 static struct socket_list *sockets = NULL;
12 static char buffer[BUF_SIZ];
15 sockets = malloc(sizeof(*sockets));
18 perror("malloc() failed");
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));
30 perror("malloc() failed");
33 client->host = strdup(host);
35 client->pass = (pass == NULL ? NULL : strdup(pass));
38 client->bufferpos = 0;
39 client->next = sockets->data;
40 sockets->data = client;
44 int connect_socket(struct ClientSocket *client) {
45 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
47 struct sockaddr_in addr;
49 if (!inet_aton(client->host, &addr.sin_addr))
51 host = gethostbyname(client->host);
54 herror("gethostbyname() failed");
57 addr.sin_addr = *(struct in_addr*)host->h_addr;
59 sock = socket(PF_INET, SOCK_STREAM, 0);
62 perror("socket() failed");
66 addr.sin_port = htons(client->port);
67 addr.sin_family = AF_INET;
69 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
71 perror("connect() failed");
75 //send the IRC Headers
80 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
81 write_socket(client, sendBuf, len);
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);
89 client->flags |= SOCKET_FLAG_CONNECTED;
93 int close_socket(struct ClientSocket *client) {
94 if(client == NULL) return 0;
95 if((client->flags & SOCKET_FLAG_CONNECTED))
97 struct ClientSocket *sock, *last_sock = NULL;
98 for (sock = sockets->data; sock; sock = sock->next) {
101 last_sock->next = sock->next;
103 sockets->data = sock->next;
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);
120 void socket_loop(int timeout_seconds) {
121 if(sockets == NULL) return;
123 struct timeval timeout;
124 struct ClientSocket *sock;
125 int ret = 0, bytes, i;
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);
134 timeout.tv_sec = timeout_seconds;
136 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
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));
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];
147 sock->bufferpos += i;
150 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
152 sock->bufferpos = bytes;
156 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
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
163 for(i = 0; i < sock->bufferpos - used; i++) {
164 sock->buffer[i] = sock->buffer[i+used];
166 sock->bufferpos -= used;