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;
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);
121 void socket_loop(int timeout_seconds) {
122 if(sockets == NULL) return;
124 struct timeval timeout;
125 struct ClientSocket *sock;
126 int ret = 0, bytes, i;
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);
135 timeout.tv_sec = timeout_seconds;
137 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
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));
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];
148 sock->bufferpos += i;
151 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
153 sock->bufferpos = bytes;
157 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
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
164 for(i = 0; i < sock->bufferpos - used; i++) {
165 sock->buffer[i] = sock->buffer[i+used];
167 sock->bufferpos -= used;