return client;
}
+#ifdef WIN32
+
+int connect_socket(struct ClientSocket *client) {
+ if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
+ struct hostent *host;
+ struct sockaddr_in addr;
+ int sock;
+ addr.sin_addr.s_addr = inet_addr(client->host);
+ if (addr.sin_addr.s_addr == INADDR_NONE) {
+ host = gethostbyname(client->host);
+ if(!host) {
+ return SOCKET_ERROR;
+ }
+ memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
+ }
+ sock = socket(PF_INET, SOCK_STREAM, 0);
+ if (sock == -1)
+ {
+ perror("socket() failed");
+ return 0;
+ }
+
+ addr.sin_port = htons(client->port);
+ addr.sin_family = AF_INET;
+
+ if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
+ {
+ perror("connect() failed");
+ return 0;
+ }
+
+ client->sock = sock;
+ client->flags |= SOCKET_FLAG_CONNECTED;
+ client->connection_time = time(0);
+
+ //send the IRC Headers
+ char sendBuf[512];
+ int len;
+
+ if(client->pass) {
+ len = sprintf(sendBuf, "PASS :%s\n", client->pass);
+ write_socket(client, sendBuf, len);
+ }
+ len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
+ write_socket(client, sendBuf, len);
+ len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
+ write_socket(client, sendBuf, len);
+
+ return 1;
+}
+
+#else
+
int connect_socket(struct ClientSocket *client) {
if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
struct hostent *host;
host = gethostbyname(client->host);
if (!host)
{
- herror("gethostbyname() failed");
+ perror("gethostbyname() failed");
return 0;
}
addr.sin_addr = *(struct in_addr*)host->h_addr;
return 1;
}
+#endif
+
int close_socket(struct ClientSocket *client) {
if(client == NULL) return 0;
if((client->flags & SOCKET_FLAG_CONNECTED))
int write_socket(struct ClientSocket *client, char* msg, int len) {
if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
printf("[send %d] %s", len, msg);
+ #ifdef WIN32
+ send(client->sock, msg, len, 0);
+ #else
write(client->sock, msg, len);
+ #endif
client->traffic_out += len;
return 1;
}
for (sock = sockets->data; sock; sock = sock->next) {
if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
if(sock->bufferpos != 0) {
+ #ifdef WIN32
+ bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
+ #else
bytes = read(sock->sock, buffer, sizeof(buffer));
+ #endif
if(bytes > 0) {
for(i = 0; i < bytes; i++) {
if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
sock->bufferpos += i;
}
} else {
+ #ifdef WIN32
+ bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
+ #else
bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
+ #endif
if(bytes > 0)
sock->bufferpos = bytes;
}