Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ CMUTIL_LogDefine("cmutils.http")
// absurd or hostile length headers.
#define CMUTIL_HTTP_MAX_BODY (1024L*1024L*1024L)

// How wide a host name this client stores, and how wide the "host:port"
// key derived from it can get - a truncated key would let two different
// hosts share one connection pool.
#define CMUTIL_HTTP_HOST_MAX 256
#define CMUTIL_HTTP_POOLKEY_MAX (CMUTIL_HTTP_HOST_MAX + 16)

struct CMUTIL_HttpContext {
CMUTIL_Map *socket_pools;
CMUTIL_Mutex *socket_pools_mutex;
Expand All @@ -48,7 +54,7 @@ struct CMUTIL_HttpContext {
typedef struct CMUTIL_SocketPoolElem {
time_t last_used;
CMUTIL_Mem *memst;
char host[256];
char host[CMUTIL_HTTP_HOST_MAX];
int port;
CMUTIL_Socket *sock;
} CMUTIL_SocketPoolElem;
Expand Down Expand Up @@ -120,7 +126,7 @@ CMUTIL_STATIC CMUTIL_Socket *CMUTIL_HttpContextGetSocket(
{
CMUTIL_Pool *pool = NULL;
CMUTIL_SocketPoolElem *elem = NULL;
char buf[256];
char buf[CMUTIL_HTTP_POOLKEY_MAX];
snprintf(buf, sizeof(buf), "%s:%d", host, port);
CMCall(g_httpctx.socket_pools_mutex, Lock);
pool = CMCall(g_httpctx.socket_pools, Get, buf);
Expand Down Expand Up @@ -164,7 +170,7 @@ CMUTIL_STATIC CMBool CMUTIL_HttpContextPutSocket(
const char *host, int port, CMUTIL_Socket *sock)
{
CMUTIL_Pool *pool = NULL;
char buf[256];
char buf[CMUTIL_HTTP_POOLKEY_MAX];
CMBool res = CMFalse;
snprintf(buf, sizeof(buf), "%s:%d", host, port);
CMCall(g_httpctx.socket_pools_mutex, Lock);
Expand Down Expand Up @@ -192,7 +198,7 @@ CMUTIL_STATIC CMBool CMUTIL_HttpContextPutSocket(
typedef struct CMUTIL_HttpClient_Internal {
CMUTIL_HttpClient base;
CMUTIL_Mem *memst;
char host[256];
char host[CMUTIL_HTTP_HOST_MAX];
int port;
CMBool ishttps;
CMBool verifypeer;
Expand Down Expand Up @@ -329,7 +335,7 @@ CMUTIL_STATIC CMSocketResult CMUTIL_HttpClientWriteLine(
{
CMSocketResult sr = CMSocketOk;
if (line && *line)
sr = CMCall(sock, Write, line, strlen(line), timeout);
sr = CMCall(sock, Write, line, (uint32_t)strlen(line), timeout);
if (sr == CMSocketOk)
sr = CMCall(sock, Write, "\r\n", 2, timeout);
CMLogTrace("Write -> %s", line);
Expand Down Expand Up @@ -438,7 +444,7 @@ CMUTIL_STATIC CMUTIL_ByteBuffer *CMUTIL_HttpClientRequest(
// Content-Length already delimits the body. A CRLF after it is
// extra bytes the peer reads as the start of the next request,
// which desynchronizes a kept-alive connection.
sr = CMCall(sock, Write, data, size, timeout);
sr = CMCall(sock, Write, data, (uint32_t)size, timeout);
if (sr != CMSocketOk) {
CMLogError("failed to write request body");
goto FAILED;
Expand Down
16 changes: 9 additions & 7 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,12 @@ CMSocketResult CMUTIL_SocketCheckBase(
tv.tv_sec = timeout / 1000;
tv.tv_usec = (int)((timeout % 1000) * 1000);

// nfds is an int; a SOCKET is a 64 bit handle on Windows, where the
// argument is ignored outright.
if (isread) {
rc = select(sock+1, &fdset, NULL, NULL, &tv);
rc = select((int)(sock+1), &fdset, NULL, NULL, &tv);
} else {
rc = select(sock+1, NULL, &fdset, NULL, &tv);
rc = select((int)(sock+1), NULL, &fdset, NULL, &tv);
}
if (rc < 0) {
if (!silent)
Expand Down Expand Up @@ -837,7 +839,7 @@ CMUTIL_STATIC CMBool CMUTIL_SocketConnectByIP(
return CMFalse;
}

rc = connect(s, (struct sockaddr*)addr, addrlen);
rc = connect(s, (struct sockaddr*)addr, (socklen_t)addrlen);
is->sock = s;
if (rc < 0) {

Expand Down Expand Up @@ -1128,9 +1130,9 @@ CMUTIL_STATIC CMBool CMUTIL_SocketConnectIPCInternal(
{
struct sockaddr_storage ss;
#if defined(MSWIN)
struct sockaddr_in *sin = (struct sockaddr_in*)&ss;
long port = strtol(ipc_path, NULL, 10);
CMUTIL_SocketAddrSet(sin, "127.0.0.1", port);
// IPC is a loopback connection here, with the path naming the port.
const long port = strtol(ipc_path, NULL, 10);
CMUTIL_SocketAddrSet(&ss, "127.0.0.1", (int)port);
#else
struct sockaddr_un *sun = (struct sockaddr_un*)&ss;
if (strlen(ipc_path) >= sizeof(sun->sun_path) - 1) {
Expand Down Expand Up @@ -1394,7 +1396,7 @@ CMUTIL_STATIC CMBool CMUTIL_ServerSocketCreateBase(
goto FAILED;
}

rc = bind(sock, (struct sockaddr *) &addr, addrlen);
rc = bind(sock, (struct sockaddr *) &addr, (socklen_t)addrlen);
if (rc == SOCKET_ERROR) {
if (res->silent)
CMLogTrace("bind failed.(%d:%s)", errno, strerror(errno));
Expand Down
5 changes: 2 additions & 3 deletions test/crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ int main() {
OpenSSL_add_all_algorithms();
CMUTIL_Init(CMUTIL_MEM_TYPE);

EVP_CIPHER *cipher;

CMUTIL_BlockCrypto *block = NULL;
CMUTIL_Map *env = NULL;
CMUTIL_String *pathstr = NULL;
Expand Down Expand Up @@ -198,7 +196,8 @@ int main() {
"6wIDAQAB\n"
"-----END PUBLIC KEY-----";

priv = CMUTIL_PrivateKeyCreateFromPEM(priv_pem, "");
/* the passphrase is bytes, not text */
priv = CMUTIL_PrivateKeyCreateFromPEM(priv_pem, (const uint8_t*)"");
ASSERT(priv != NULL, "CMUTIL_PrivateKeyCreateFromPEM");

pub = CMUTIL_PublicKeyCreateFromPEM(pub_pem);
Expand Down
6 changes: 3 additions & 3 deletions test/dgram_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int main() {
CMLogInfo("datagram socket connected to %s:%d", rhost, rport);

const char *send_data = "this is sample message";
CMCall(buffer, AddBytes, (const uint8_t*)send_data, strlen(send_data));
CMCall(buffer, AddBytes, (const uint8_t*)send_data, (uint32_t)strlen(send_data));
sr = CMCall(client, Send, buffer, 1000);
ASSERT(sr == CMSocketOk, "Send");

Expand All @@ -108,7 +108,7 @@ int main() {

client = CMUTIL_DGramSocketCreateBind(NULL);
send_data = "second data";
CMCall(buffer, AddBytes, (const uint8_t*)send_data, strlen(send_data));
CMCall(buffer, AddBytes, (const uint8_t*)send_data, (uint32_t)strlen(send_data));
sr = CMCall(client, SendTo, buffer, &localaddr, 1000);
ASSERT(sr == CMSocketOk, "SendTo");

Expand All @@ -127,7 +127,7 @@ int main() {

CMCall(buffer, Clear);
send_data = "!@#$$#@!";
CMCall(buffer, AddBytes, (const uint8_t*)send_data, strlen(send_data));
CMCall(buffer, AddBytes, (const uint8_t*)send_data, (uint32_t)strlen(send_data));
sr = CMCall(client, SendTo, buffer, &localaddr, 1000);
if (sr != CMSocketOk) {
CMLogError("datagram socket send failed - failed");
Expand Down
6 changes: 3 additions & 3 deletions test/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void client_handler(void* udata) {
CMCall(str, InsertPrint, 0, "%04d", len);
p = (uint8_t*)CMCall(str, GetCString);
blen = CMCall(str, GetSize);
sr = CMCall(csock, Write, p, blen, 1000);
sr = CMCall(csock, Write, p, (uint32_t)blen, 1000);
if (sr != CMSocketOk) break;
CMLogInfo("sent to client: %s", CMCall(str, GetCString));
CMCall(buf, Clear);
Expand Down Expand Up @@ -93,11 +93,11 @@ void client_proc(void* udata) {
CMLogInfo("connected to server: %s:%d", host, port);
for (int i=0; i<10; i++) {
CMCall(sbuf, AddPrint, "%s: %d", tname, i);
int len = CMCall(sbuf, GetSize);
int len = (int)CMCall(sbuf, GetSize);
CMCall(sbuf, InsertPrint, 0, "%04d", len);
uint8_t *p = (uint8_t*)CMCall(sbuf, GetCString);
size_t blen = CMCall(sbuf, GetSize);
sr = CMCall(csock, Write, p, blen, 1000);
sr = CMCall(csock, Write, p, (uint32_t)blen, 1000);
if (sr != CMSocketOk) break;
CMLogInfo("sent to server: %s", CMCall(sbuf, GetCString));
CMCall(sbuf, Clear);
Expand Down
Loading