# HG changeset patch # User Matti Hamalainen # Date 1325111960 -7200 # Node ID 4523fc0941e8ed9277020586d57500d0df5edfbe # Parent bbd5edbf114314e5d2ca4840962969bb28475248 Cosmetics. diff -r bbd5edbf1143 -r 4523fc0941e8 libnnchat.c --- a/libnnchat.c Thu Dec 29 00:34:46 2011 +0200 +++ b/libnnchat.c Thu Dec 29 00:39:20 2011 +0200 @@ -22,7 +22,8 @@ const char *nn_get_socket_errstr(int err) { static char buf[64]; - switch (err) { + switch (err) + { case WSAEADDRINUSE: return "Address already in use"; case WSAECONNABORTED: return "Software caused connection abort"; case WSAECONNREFUSED: return "Connection refused"; @@ -53,7 +54,8 @@ void nn_conn_err(nn_conn_t *conn, const char *fmt, ...) { - if (conn->errfunc) { + if (conn->errfunc != NULL) + { va_list ap; va_start(ap, fmt); conn->errfunc(conn, fmt, ap); @@ -64,7 +66,8 @@ static void nn_conn_msg(nn_conn_t *conn, const char *fmt, ...) { - if (conn->msgfunc) { + if (conn->msgfunc != NULL) + { va_list ap; va_start(ap, fmt); conn->msgfunc(conn, fmt, ap); @@ -83,7 +86,8 @@ return res; } -static const char *nn_proxy_types[] = { +static const char *nn_proxy_types[] = +{ "none", "SOCKS 4", "SOCKS 4a", @@ -108,10 +112,13 @@ static BOOL nn_get_addr(struct in_addr *addr, struct hostent *hst) { - if (hst != NULL) { + if (hst != NULL) + { *addr = *(struct in_addr *) (hst->h_addr); return TRUE; - } else { + } + else + { addr->s_addr = 0; return FALSE; } @@ -126,7 +133,8 @@ conn->proxy.port = port; conn->proxy.host = th_strdup(host); - if (host != NULL) { + if (host != NULL) + { conn->proxy.hst = nn_resolve_host(conn, host); nn_get_addr(&(conn->proxy.addr), conn->proxy.hst); } else @@ -144,7 +152,8 @@ return -1; conn->port = port; - if (host != NULL) { + if (host != NULL) + { conn->host = th_strdup(host); conn->hst = nn_resolve_host(conn, host); } @@ -154,14 +163,17 @@ /* Prepare for connection */ dest.sin_family = AF_INET; - if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST) { + if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST) + { dest.sin_port = htons(conn->proxy.port); dest.sin_addr = conn->proxy.addr; nn_conn_msg(conn, "Connecting to %s proxy %s:%d ...\n", nn_proxy_types[conn->proxy.type], inet_ntoa(conn->proxy.addr), conn->proxy.port); - } else { + } + else + { dest.sin_port = htons(conn->port); dest.sin_addr = conn->addr; @@ -169,7 +181,8 @@ inet_ntoa(conn->addr), conn->port); } - if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) { + if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) + { conn->err = nn_get_socket_errno(); nn_conn_err(conn, "Could not open socket: %s\n", nn_get_socket_errstr(conn->err)); goto error; @@ -177,7 +190,8 @@ nn_conn_msg(conn, "Using socket %d.\n", conn->socket); - if (connect(conn->socket, (struct sockaddr *) &dest, sizeof(dest)) == -1) { + if (connect(conn->socket, (struct sockaddr *) &dest, sizeof(dest)) == -1) + { conn->err = nn_get_socket_errno(); nn_conn_err(conn, "Could not connect: %s\n", nn_get_socket_errstr(conn->err)); goto error; @@ -187,7 +201,8 @@ FD_SET(conn->socket, &(conn->sockfds)); /* Proxy-specific setup */ - if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A) { + if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A) + { struct nn_socks_t *socksh; size_t bufsiz = sizeof(struct nn_socks_t) + strlen(userid) + 1; char *ptr, *buf; @@ -197,7 +212,8 @@ bufsiz += strlen(conn->host) + 1; ptr = buf = th_malloc(bufsiz); - if (buf == NULL) { + if (buf == NULL) + { conn->err = -1; nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz); goto error; @@ -217,14 +233,16 @@ strcpy(ptr, userid); - if (conn->proxy.type == NN_PROXY_SOCKS4A) { + if (conn->proxy.type == NN_PROXY_SOCKS4A) + { ptr += strlen(userid) + 1; strcpy(ptr, conn->host); } /* Send request */ nn_conn_reset(conn); - if (!nn_conn_send_buf(conn, buf, bufsiz)) { + if (!nn_conn_send_buf(conn, buf, bufsiz)) + { th_free(buf); nn_conn_err(conn, "Error sending SOCKS proxy request.\n"); goto error; @@ -232,7 +250,8 @@ th_free(buf); /* Wait for SOCKS server to reply */ - for (status = tries = 1; tries <= 20 && status > 0; tries++) { + for (status = tries = 1; tries <= 20 && status > 0; tries++) + { #ifdef __WIN32 Sleep(50); #else @@ -243,15 +262,19 @@ } /* Check results */ - if (status == 0) { + if (status == 0) + { struct nn_socks_res_t *res = (struct nn_socks_res_t *) &(conn->buf); - if (res->nb != 0) { + if (res->nb != 0) + { nn_conn_err(conn, "Invalid SOCKS server reply, does not begin with NUL byte (%d).\n", res->nb); goto error; } - if (res->result != 0x5a) { + if (res->result != 0x5a) + { char *s = NULL; - switch (res->result) { + switch (res->result) + { case 0x5b: s = "Request rejected or failed"; break; case 0x5c: s = "Request failed because client is not running identd (or not reachable from the server)"; break; case 0x5d: s = "Request failed because client's identd could not confirm the user ID string in the request"; break; @@ -262,11 +285,13 @@ } nn_conn_msg(conn, "SOCKS connection established!\n"); } - else if (status < 0) { + else if (status < 0) + { nn_conn_err(conn, "Proxy negotiation failed at try %d with network error: %d\n", tries, status); goto error; } - else { + else + { nn_conn_err(conn, "Proxy negotiation timed out.\n"); goto error; } @@ -287,7 +312,8 @@ if (conn == NULL) return; - if (conn->socket >= 0) { + if (conn->socket >= 0) + { #ifdef __WIN32 closesocket(conn->socket); #else @@ -310,10 +336,12 @@ size_t bufLeft = len; const char *bufPtr = buf; - while (bufLeft > 0) { + while (bufLeft > 0) + { ssize_t bufSent; bufSent = send(conn->socket, bufPtr, bufLeft, 0); - if (bufSent < 0) { + if (bufSent < 0) + { conn->err = nn_get_socket_errno(); nn_conn_err(conn, "nn_conn_send_buf() failed: %s", nn_get_socket_errstr(conn->err)); return FALSE; @@ -327,7 +355,8 @@ void nn_conn_reset(nn_conn_t *conn) { - if (conn != NULL) { + if (conn != NULL) + { conn->ptr = conn->buf; conn->got = conn->total = 0; } @@ -347,25 +376,32 @@ socktv.tv_usec = NN_DELAY_USEC; tmpfds = conn->sockfds; - if ((result = select(conn->socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1) { + if ((result = select(conn->socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1) + { conn->err = nn_get_socket_errno(); - if (conn->err != EINTR) { + if (conn->err != EINTR) + { nn_conn_err(conn, "Error occured in select(%d, sockfds): %d, %s\n", socket, conn->err, nn_get_socket_errstr(conn->err)); return -1; } } else - if (FD_ISSET(conn->socket, &tmpfds)) { + if (FD_ISSET(conn->socket, &tmpfds)) + { conn->got = recv(conn->socket, conn->ptr, NN_CONNBUF_SIZE - conn->total, 0); - if (conn->got < 0) { + if (conn->got < 0) + { conn->err = nn_get_socket_errno(); nn_conn_err(conn, "Error in recv: %d, %s\n", conn->err, nn_get_socket_errstr(conn->err)); return -2; - } else if (conn->got == 0) { + } else if (conn->got == 0) + { nn_conn_err(conn, "Server closed connection.\n"); conn->status = NN_CONN_CLOSED; return -3; - } else { + } + else + { /* Handle protocol data */ conn->total += conn->got; conn->ptr += conn->got; @@ -392,7 +428,8 @@ /* Initialize WinSock, if needed */ WSADATA wsaData; int err = WSAStartup(0x0101, &wsaData); - if (err != 0) { + if (err != 0) + { THERR("Could not initialize WinSock library (err=%d).\n", err); return FALSE; } @@ -424,8 +461,10 @@ if ((result = th_malloc(resSize)) == NULL) return NULL; - while (*s) { - switch (*s) { + while (*s) + { + switch (*s) + { case 32: PUSHCHAR('+'); break; @@ -433,7 +472,8 @@ default: if (th_isalnum(*s)) PUSHCHAR(*s); - else { + else + { char tmpStr[4]; snprintf(tmpStr, sizeof(tmpStr), "%2X", (unsigned char) *s); PUSHCHAR('%'); @@ -479,8 +519,10 @@ if ((result = th_malloc(resSize)) == NULL) return NULL; - while (*s) { - switch (*s) { + while (*s) + { + switch (*s) + { case '+': PUSHCHAR(' '); s++; @@ -504,13 +546,18 @@ PUSHCHAR('%'); else if ((c = getHexDigit(*s, 4)) >= 0) { int i = getHexDigit(*(++s), 0); - if (i >= 0) { + if (i >= 0) + { PUSHCHAR(c | i); - } else { + } + else + { PUSHCHAR('§'); PUSHCHAR(*s); } - } else { + } + else + { PUSHCHAR('§'); PUSHCHAR(*s); } @@ -540,8 +587,10 @@ if ((result = th_malloc(resSize)) == NULL) return NULL; - while (*s) { - if (*s == '<') { + while (*s) + { + if (*s == '<') + { while (*s && *s != '>') s++; if (*s == '>') s++; } else @@ -553,13 +602,15 @@ } -typedef struct { +typedef struct +{ char c; char *ent; } html_entity_t; -static const html_entity_t HTMLEntities[] = { +static const html_entity_t HTMLEntities[] = +{ { '<', "<" }, { '>', ">" }, }; @@ -579,11 +630,13 @@ if ((result = th_malloc(resSize)) == NULL) return NULL; - while (*s) { + while (*s) + { int i; BOOL found = FALSE; for (i = 0; i < numHTMLEntities; i++) - if (HTMLEntities[i].c == *s) { + if (HTMLEntities[i].c == *s) + { PUSHSTR(HTMLEntities[i].ent); found = TRUE; break; @@ -610,14 +663,18 @@ if ((result = th_malloc(resSize)) == NULL) return NULL; - while (*s) { - if (*s == '&') { + while (*s) + { + if (*s == '&') + { int i; BOOL found = FALSE; - for (i = 0; i < numHTMLEntities; i++) { + for (i = 0; i < numHTMLEntities; i++) + { const html_entity_t *ent = &HTMLEntities[i]; int len = strlen(ent->ent); - if (!strncmp(s, ent->ent, len)) { + if (!strncmp(s, ent->ent, len)) + { PUSHCHAR(ent->c); s += len; found = TRUE; @@ -677,7 +734,8 @@ msg = th_strdup_printf("%s%s", user, tmp); th_free(tmp); - if (msg != NULL) { + if (msg != NULL) + { BOOL ret = nn_conn_send_buf(conn, msg, strlen(msg) + 1); th_free(msg); return ret; @@ -692,11 +750,11 @@ if (pos < 0) return -1; - else if (pos >= buf->len) { + else if (pos >= buf->len) buf->data[buf->len++] = ch; - } else { + else buf->data[pos] = ch; - } + return 0; } @@ -707,9 +765,12 @@ if (pos < 0) return -1; - else if (pos >= buf->len) { + else if (pos >= buf->len) + { buf->data[buf->len] = ch; - } else { + } + else + { memmove(&(buf->data[pos+1]), &(buf->data[pos]), buf->len - pos + 1); buf->data[pos] = ch; } @@ -722,7 +783,8 @@ { if (pos < 0) return -1; - else if (pos < buf->len) { + else if (pos < buf->len) + { memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos); buf->len--; return 0; @@ -751,7 +813,8 @@ void nn_editbuf_free(nn_editbuf_t *buf) { - if (buf != NULL) { + if (buf != NULL) + { th_free(buf->data); th_free(buf); } @@ -787,9 +850,12 @@ if (start < 0 || end > buf->len || start >= buf->len) return NULL; - if (end < 0) { + if (end < 0) + { siz = buf->len - start + 1; - } else if (start <= end) { + } + else if (start <= end) + { siz = end - start + 1; } else return NULL; @@ -824,7 +890,8 @@ const uint8_t *c = (uint8_t *)name; uint8_t hash = 0xff; - while (*c && n < 4) { + while (*c && n < 4) + { hash = (hash << 1) ^ tolower(*c); c++; n++; } @@ -849,9 +916,11 @@ if (list == NULL) return NULL; for (i = 0; i < NN_NUM_BUCKETS; i++) - if (list->buckets[i] != NULL) { + if (list->buckets[i] != NULL) + { nn_user_t *curr = list->buckets[i]; - while (curr != NULL) { + while (curr != NULL) + { if (func(curr) != 0) return curr; curr = curr->next; @@ -869,9 +938,11 @@ if (list == NULL) return NULL; hash = nn_hash_user(name); - if (list->buckets[hash] != NULL) { + if (list->buckets[hash] != NULL) + { nn_user_t *curr = list->buckets[hash]; - while (curr != NULL) { + while (curr != NULL) + { if (th_strcasecmp(curr->name, name) == 0) return curr; curr = curr->next; @@ -885,7 +956,9 @@ static nn_user_t *nn_user_match_do(nn_user_t *list, const char *pattern, size_t len) { nn_user_t *curr = list; - while (curr != NULL) { + + while (curr != NULL) + { if (len <= strlen(curr->name) && th_strncasecmp(curr->name, pattern, len) == 0) return curr; curr = curr->next; @@ -901,14 +974,18 @@ if (list == NULL || pattern == NULL) return NULL; hash = nn_hash_user(pattern); - if (list->buckets[hash] != NULL) { + if (list->buckets[hash] != NULL) + { nn_user_t *curr = list->buckets[hash]; size_t len = strlen(pattern); - if (current != NULL) { + if (current != NULL) + { nn_user_t *found = NULL; - while (curr != NULL) { - if (th_strcasecmp(curr->name, current) == 0) { + while (curr != NULL) + { + if (th_strcasecmp(curr->name, current) == 0) + { if (again) return curr; found = curr->next; @@ -973,12 +1050,15 @@ /* Check if username is already there */ hash = nn_hash_user(name); - if (list->buckets[hash] != NULL) { + if (list->buckets[hash] != NULL) + { nn_user_t *curr, *prev; curr = list->buckets[hash]; prev = NULL; - while (curr != NULL) { - if (th_strcasecmp(curr->name, name) == 0) { + while (curr != NULL) + { + if (th_strcasecmp(curr->name, name) == 0) + { if (prev) prev->next = curr->next; else @@ -987,7 +1067,9 @@ nn_user_free(curr); return 0; - } else { + } + else + { prev = curr; curr = curr->next; } @@ -1026,7 +1108,9 @@ void nn_user_free_list(nn_user_t *list) { nn_user_t *next, *curr = list; - while (curr != NULL) { + + while (curr != NULL) + { next = curr->next; nn_user_free(curr); curr = next; @@ -1039,7 +1123,8 @@ if (hash == NULL) return; - for (i = 0; i < NN_NUM_BUCKETS; i++) { + for (i = 0; i < NN_NUM_BUCKETS; i++) + { nn_user_free_list(hash->buckets[i]); hash->buckets[i] = NULL; } @@ -1075,7 +1160,8 @@ if (res == NULL) return NULL; res->data = th_ringbuf_new(NN_BACKBUF_LEN, th_free); - if (res->data == NULL) { + if (res->data == NULL) + { th_free(res); return NULL; } @@ -1088,7 +1174,8 @@ void nn_window_free(nn_window_t *win) { - if (win != NULL) { + if (win != NULL) + { th_ringbuf_free(win->data); th_free(win->id); th_free(win);