|
|
@ -55,7 +55,7 @@ struct client_instance { |
|
|
|
/* Which serverurl is this instance connected to */ |
|
|
|
/* Which serverurl is this instance connected to */ |
|
|
|
int server; |
|
|
|
int server; |
|
|
|
|
|
|
|
|
|
|
|
char buf[PAGESIZE]; |
|
|
|
char *buf; |
|
|
|
unsigned long bufofs; |
|
|
|
unsigned long bufofs; |
|
|
|
|
|
|
|
|
|
|
|
/* Are we currently sending a blocked message from this client */ |
|
|
|
/* Are we currently sending a blocked message from this client */ |
|
|
@ -205,6 +205,10 @@ static client_instance_t *recruit_client(cdata_t *cdata) |
|
|
|
client = ckzalloc(sizeof(client_instance_t)); |
|
|
|
client = ckzalloc(sizeof(client_instance_t)); |
|
|
|
} else |
|
|
|
} else |
|
|
|
LOGDEBUG("Connector recycled client instance"); |
|
|
|
LOGDEBUG("Connector recycled client instance"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client->buf = realloc(client->buf, PAGESIZE); |
|
|
|
|
|
|
|
client->buf[0] = '\0'; |
|
|
|
|
|
|
|
|
|
|
|
return client; |
|
|
|
return client; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -457,42 +461,46 @@ static void parse_redirector_share(client_instance_t *client, const json_t *val) |
|
|
|
static void parse_client_msg(cdata_t *cdata, client_instance_t *client) |
|
|
|
static void parse_client_msg(cdata_t *cdata, client_instance_t *client) |
|
|
|
{ |
|
|
|
{ |
|
|
|
ckpool_t *ckp = cdata->ckp; |
|
|
|
ckpool_t *ckp = cdata->ckp; |
|
|
|
char msg[PAGESIZE], *eol; |
|
|
|
int buflen, ret, ofs; |
|
|
|
int buflen, ret; |
|
|
|
char *msg, *eol; |
|
|
|
json_t *val; |
|
|
|
json_t *val; |
|
|
|
|
|
|
|
|
|
|
|
retry: |
|
|
|
retry: |
|
|
|
if (unlikely(client->bufofs > MAX_MSGSIZE)) { |
|
|
|
ofs = client->bufofs; |
|
|
|
|
|
|
|
if (unlikely(ofs > MAX_MSGSIZE)) { |
|
|
|
|
|
|
|
if (!client->remote) { |
|
|
|
LOGNOTICE("Client id %"PRId64" fd %d overloaded buffer without EOL, disconnecting", |
|
|
|
LOGNOTICE("Client id %"PRId64" fd %d overloaded buffer without EOL, disconnecting", |
|
|
|
client->id, client->fd); |
|
|
|
client->id, client->fd); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
buflen = PAGESIZE - client->bufofs; |
|
|
|
client->buf = realloc(client->buf, round_up_page(ofs + MAX_MSGSIZE + 1)); |
|
|
|
|
|
|
|
} |
|
|
|
/* This read call is non-blocking since the socket is set to O_NOBLOCK */ |
|
|
|
/* This read call is non-blocking since the socket is set to O_NOBLOCK */ |
|
|
|
ret = read(client->fd, client->buf + client->bufofs, buflen); |
|
|
|
ret = read(client->fd, client->buf + ofs, MAX_MSGSIZE); |
|
|
|
if (ret < 1) { |
|
|
|
if (ret < 1) { |
|
|
|
if (likely(errno == EAGAIN || errno == EWOULDBLOCK || !ret)) |
|
|
|
if (likely(errno == EAGAIN || errno == EWOULDBLOCK || !ret)) |
|
|
|
return; |
|
|
|
return; |
|
|
|
LOGINFO("Client id %"PRId64" fd %d disconnected - recv fail with bufofs %lu ret %d errno %d %s", |
|
|
|
LOGINFO("Client id %"PRId64" fd %d disconnected - recv fail with bufofs %d ret %d errno %d %s", |
|
|
|
client->id, client->fd, client->bufofs, ret, errno, ret && errno ? strerror(errno) : ""); |
|
|
|
client->id, client->fd, ofs, ret, errno, ret && errno ? strerror(errno) : ""); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
client->bufofs += ret; |
|
|
|
client->bufofs += ret; |
|
|
|
reparse: |
|
|
|
reparse: |
|
|
|
eol = memchr(client->buf, '\n', client->bufofs); |
|
|
|
eol = memchr(client->buf + ofs, '\n', client->bufofs); |
|
|
|
if (!eol) |
|
|
|
if (!eol) |
|
|
|
goto retry; |
|
|
|
goto retry; |
|
|
|
|
|
|
|
|
|
|
|
/* Do something useful with this message now */ |
|
|
|
/* Do something useful with this message now */ |
|
|
|
buflen = eol - client->buf + 1; |
|
|
|
buflen = eol - client->buf + 1; |
|
|
|
if (unlikely(buflen > MAX_MSGSIZE)) { |
|
|
|
if (unlikely(buflen > MAX_MSGSIZE && !client->remote)) { |
|
|
|
LOGNOTICE("Client id %"PRId64" fd %d message oversize, disconnecting", client->id, client->fd); |
|
|
|
LOGNOTICE("Client id %"PRId64" fd %d message oversize, disconnecting", client->id, client->fd); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
invalidate_client(ckp, cdata, client); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg = alloca(round_up_page(buflen + 1)); |
|
|
|
memcpy(msg, client->buf, buflen); |
|
|
|
memcpy(msg, client->buf, buflen); |
|
|
|
msg[buflen] = '\0'; |
|
|
|
msg[buflen] = '\0'; |
|
|
|
client->bufofs -= buflen; |
|
|
|
client->bufofs -= buflen; |
|
|
|