Example #1
0
int __ircsock_connect(IRCSOCK * sock) {
    struct addrinfo hints;
    struct addrinfo *result, *res_ptr;
    int ai_res;
    char sport[6];
    IRC * irc;

    irc = sock->__irc;
    memset(&hints, 0, sizeof (struct addrinfo));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;

    snprintf(sport, 6, "%d", sock->port);

    switch ((ai_res = getaddrinfo(sock->host, sport, &hints, &result))) {
        case 0: break;
        default:
            if (irc) irc->log(irc, IRC_LOG_ERR, "getaddrinfo failed: %s\n", gai_strerror(ai_res));
            return -1;
            break;
    }

    res_ptr = result;
    do {
        if ((sock->__sockfd = socket(res_ptr->ai_family, res_ptr->ai_socktype, res_ptr->ai_protocol)) != -1) {
            if (connect(sock->__sockfd, res_ptr->ai_addr, res_ptr->ai_addrlen) == 0) break;
            close(sock->__sockfd);
            sock->__sockfd = -1;
        }

    } while ((res_ptr = res_ptr->ai_next) != NULL);

    freeaddrinfo(result);
    if (res_ptr == NULL) {
        if (irc) irc->log(irc, IRC_LOG_ERR, "Connect failed\n", irc->id);
        return -1;
    }
    return 0;
}
Example #2
0
int __ircsock_read(IRCSOCK * sock, __irc_line * line) {
    int count;
    char c, * buffer;
    IRC * irc;

    irc = sock->__irc;

    buffer = line->data;

    c = EOF;
    count = 0;
    while ((c = sock->__getc(sock)) != EOF && c != '\n') buffer[count++] = c;

    if (errno) return errno;

    buffer[count] = '\0';
    if (count > 0 && buffer[count - 1] == '\r') buffer[count - 1] = '\0';
    if (c == EOF && strlen(buffer) == 0) return EOF;

    if (strncasecmp(buffer, "PING", 4) != 0)
        irc->log(irc, IRC_LOG_RAW, "%s\n", buffer);

    return 0;
}