aboutsummaryrefslogtreecommitdiff
path: root/src/platform
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-02-05 10:23:35 -0800
committerGarrett D'Amore <garrett@damore.org>2018-02-05 10:23:35 -0800
commit844ce972fed056e1c4e0517e43b814c62d68edce (patch)
treeadfa91e3f7188d268f3d081e80c14f7e8a609a87 /src/platform
parentb893f8ff1f96dde567fa6a75f4b15bf69e53d6f5 (diff)
downloadnng-844ce972fed056e1c4e0517e43b814c62d68edce.tar.gz
nng-844ce972fed056e1c4e0517e43b814c62d68edce.tar.bz2
nng-844ce972fed056e1c4e0517e43b814c62d68edce.zip
fixes #228 aio iov should have larger limits (dynamically allocated)
Diffstat (limited to 'src/platform')
-rw-r--r--src/platform/posix/posix_pipedesc.c58
-rw-r--r--src/platform/posix/posix_udp.c25
-rw-r--r--src/platform/windows/win_ipc.c4
-rw-r--r--src/platform/windows/win_tcp.c8
-rw-r--r--src/platform/windows/win_udp.c25
5 files changed, 93 insertions, 27 deletions
diff --git a/src/platform/posix/posix_pipedesc.c b/src/platform/posix/posix_pipedesc.c
index 23d69e51..f387c60c 100644
--- a/src/platform/posix/posix_pipedesc.c
+++ b/src/platform/posix/posix_pipedesc.c
@@ -69,14 +69,32 @@ nni_posix_pipedesc_dowrite(nni_posix_pipedesc *pd)
nni_aio *aio;
while ((aio = nni_list_first(&pd->writeq)) != NULL) {
- int i;
- int n;
- struct iovec iovec[4];
- int niov;
- int naiov;
- nni_iov * aiov;
+ unsigned i;
+ int n;
+ int niov;
+ unsigned naiov;
+ nni_iov *aiov;
+#ifdef NNG_HAVE_ALLOCA
+ struct iovec *iovec;
+#else
+ struct iovec iovec[16];
+#endif
nni_aio_get_iov(aio, &naiov, &aiov);
+
+#ifdef NNG_HAVE_ALLOCA
+ if (naiov > 64) {
+ nni_posix_pipedesc_finish(aio, NNG_EINVAL);
+ continue;
+ }
+ iovec = alloca(naiov * sizeof(*iovec));
+#else
+ if (naiov > NNI_NUM_ELEMENTS(iovec)) {
+ nni_posix_pipedesc_finish(aio, NNG_EINVAL);
+ continue;
+ }
+#endif
+
for (niov = 0, i = 0; i < naiov; i++) {
if (aiov[i].iov_len > 0) {
iovec[niov].iov_len = aiov[i].iov_len;
@@ -112,14 +130,30 @@ nni_posix_pipedesc_doread(nni_posix_pipedesc *pd)
nni_aio *aio;
while ((aio = nni_list_first(&pd->readq)) != NULL) {
- int i;
- int n;
- struct iovec iovec[4];
- int niov;
- int naiov;
- nni_iov * aiov;
+ unsigned i;
+ int n;
+ int niov;
+ unsigned naiov;
+ nni_iov *aiov;
+#ifdef NNG_HAVE_ALLOCA
+ struct iovec *iovec;
+#else
+ struct iovec iovec[16];
+#endif
nni_aio_get_iov(aio, &naiov, &aiov);
+#ifdef NNG_HAVE_ALLOCA
+ if (naiov > 64) {
+ nni_posix_pipedesc_finish(aio, NNG_EINVAL);
+ continue;
+ }
+ iovec = alloca(naiov * sizeof(*iovec));
+#else
+ if (naiov > NNI_NUM_ELEMENTS(iovec)) {
+ nni_posix_pipedesc_finish(aio, NNG_EINVAL);
+ continue;
+ }
+#endif
for (niov = 0, i = 0; i < naiov; i++) {
if (aiov[i].iov_len != 0) {
iovec[niov].iov_len = aiov[i].iov_len;
diff --git a/src/platform/posix/posix_udp.c b/src/platform/posix/posix_udp.c
index e01f6883..cd7b0561 100644
--- a/src/platform/posix/posix_udp.c
+++ b/src/platform/posix/posix_udp.c
@@ -63,7 +63,7 @@ nni_posix_udp_dorecv(nni_plat_udp *udp)
// While we're able to recv, do so.
while ((aio = nni_list_first(q)) != NULL) {
struct iovec iov[4];
- int niov;
+ unsigned niov;
nni_iov * aiov;
struct sockaddr_storage ss;
nng_sockaddr * sa;
@@ -73,7 +73,7 @@ nni_posix_udp_dorecv(nni_plat_udp *udp)
nni_aio_get_iov(aio, &niov, &aiov);
- for (int i = 0; i < niov; i++) {
+ for (unsigned i = 0; i < niov; i++) {
iov[i].iov_base = aiov[i].iov_buf;
iov[i].iov_len = aiov[i].iov_len;
}
@@ -123,13 +123,28 @@ nni_posix_udp_dosend(nni_plat_udp *udp)
rv = NNG_EADDRINVAL;
} else {
struct msghdr hdr;
- struct iovec iov[4];
- int niov;
+ unsigned niov;
nni_iov * aiov;
+#ifdef NNG_HAVE_ALLOCA
+ struct iovec *iov;
+#else
+ struct iovec iov[16];
+#endif
nni_aio_get_iov(aio, &niov, &aiov);
+#ifdef NNG_HAVE_ALLOCA
+ if (niov > 64) {
+ rv = NNG_EINVAL;
+ } else {
+ iov = alloca(niov * sizeof(*iov));
+ }
+#else
+ if (niov > NNI_NUM_ELEMENTS(iov)) {
+ rv = NNG_EINVAL;
+ }
+#endif
- for (int i = 0; i < niov; i++) {
+ for (unsigned i = 0; i < niov; i++) {
iov[i].iov_base = aiov[i].iov_buf;
iov[i].iov_len = aiov[i].iov_len;
}
diff --git a/src/platform/windows/win_ipc.c b/src/platform/windows/win_ipc.c
index 022a18ea..76180d23 100644
--- a/src/platform/windows/win_ipc.c
+++ b/src/platform/windows/win_ipc.c
@@ -59,8 +59,8 @@ nni_win_ipc_pipe_start(nni_win_event *evt, nni_aio *aio)
BOOL ok;
int rv;
nni_plat_ipc_pipe *pipe = evt->ptr;
- int idx;
- int naiov;
+ unsigned idx;
+ unsigned naiov;
nni_iov * aiov;
NNI_ASSERT(aio != NULL);
diff --git a/src/platform/windows/win_tcp.c b/src/platform/windows/win_tcp.c
index c9935719..254cf40b 100644
--- a/src/platform/windows/win_tcp.c
+++ b/src/platform/windows/win_tcp.c
@@ -12,6 +12,7 @@
#ifdef NNG_PLATFORM_WINDOWS
+#include <malloc.h>
#include <stdio.h>
struct nni_plat_tcp_pipe {
@@ -101,15 +102,16 @@ nni_win_tcp_pipe_start(nni_win_event *evt, nni_aio *aio)
{
int rv;
SOCKET s;
- WSABUF iov[4]; // XXX: consider _alloca()
DWORD niov;
DWORD flags;
nni_plat_tcp_pipe *pipe = evt->ptr;
int i;
- int naiov;
+ unsigned naiov;
nni_iov * aiov;
+ WSABUF * iov;
nni_aio_get_iov(aio, &naiov, &aiov);
+ iov = _malloca(naiov * sizeof(*iov));
// Put the AIOs in Windows form.
for (niov = 0, i = 0; i < naiov; i++) {
@@ -121,6 +123,7 @@ nni_win_tcp_pipe_start(nni_win_event *evt, nni_aio *aio)
}
if ((s = pipe->s) == INVALID_SOCKET) {
+ _freea(iov);
evt->status = NNG_ECLOSED;
evt->count = 0;
return (1);
@@ -136,6 +139,7 @@ nni_win_tcp_pipe_start(nni_win_event *evt, nni_aio *aio)
} else {
rv = WSARecv(s, iov, niov, NULL, &flags, &evt->olpd, NULL);
}
+ _freea(iov);
if ((rv == SOCKET_ERROR) &&
((rv = GetLastError()) != ERROR_IO_PENDING)) {
diff --git a/src/platform/windows/win_udp.c b/src/platform/windows/win_udp.c
index ba947719..81aa2c06 100644
--- a/src/platform/windows/win_udp.c
+++ b/src/platform/windows/win_udp.c
@@ -15,6 +15,7 @@
#ifdef NNG_PLATFORM_WINDOWS
+#include <malloc.h>
#include <stdio.h>
struct nni_plat_udp {
@@ -134,11 +135,11 @@ nni_win_udp_start_rx(nni_win_event *evt, nni_aio *aio)
{
int rv;
SOCKET s;
- WSABUF iov[4]; // XXX: consider _alloca
DWORD flags;
nni_plat_udp *u = evt->ptr;
nni_iov * aiov;
- int naiov;
+ unsigned naiov;
+ WSABUF * iov;
if ((s = u->s) == INVALID_SOCKET) {
evt->status = NNG_ECLOSED;
@@ -149,8 +150,13 @@ nni_win_udp_start_rx(nni_win_event *evt, nni_aio *aio)
u->rxsalen = sizeof(SOCKADDR_STORAGE);
nni_aio_get_iov(aio, &naiov, &aiov);
+ // This is a stack allocation- it should always succeed - or
+ // throw an exception if there is not sufficient stack space.
+ // (Turns out it can allocate from the heap, but same semantics.)
+ iov = _malloca(sizeof(*iov) * naiov);
+
// Put the AIOs in Windows form.
- for (int i = 0; i < naiov; i++) {
+ for (unsigned i = 0; i < naiov; i++) {
iov[i].buf = aiov[i].iov_buf;
iov[i].len = (ULONG) aiov[i].iov_len;
}
@@ -164,6 +170,8 @@ nni_win_udp_start_rx(nni_win_event *evt, nni_aio *aio)
rv = WSARecvFrom(u->s, iov, (DWORD) naiov, NULL, &flags,
(struct sockaddr *) &u->rxsa, &u->rxsalen, &evt->olpd, NULL);
+ _freea(iov);
+
if ((rv == SOCKET_ERROR) &&
((rv = GetLastError()) != ERROR_IO_PENDING)) {
// Synchronous failure.
@@ -183,12 +191,12 @@ nni_win_udp_start_tx(nni_win_event *evt, nni_aio *aio)
{
int rv;
SOCKET s;
- WSABUF iov[4];
- int naiov;
+ unsigned naiov;
nni_iov * aiov;
nni_plat_udp *u = evt->ptr;
int salen;
nni_sockaddr *sa;
+ WSABUF * iov;
if ((s = u->s) == INVALID_SOCKET) {
evt->status = NNG_ECLOSED;
@@ -205,8 +213,11 @@ nni_win_udp_start_tx(nni_win_event *evt, nni_aio *aio)
}
nni_aio_get_iov(aio, &naiov, &aiov);
+
+ iov = _malloca(sizeof(*iov) * naiov);
+
// Put the AIOs in Windows form.
- for (int i = 0; i < naiov; i++) {
+ for (unsigned i = 0; i < naiov; i++) {
iov[i].buf = aiov[i].iov_buf;
iov[i].len = (ULONG) aiov[i].iov_len;
}
@@ -219,6 +230,8 @@ nni_win_udp_start_tx(nni_win_event *evt, nni_aio *aio)
rv = WSASendTo(u->s, iov, (DWORD) naiov, NULL, 0,
(struct sockaddr *) &u->txsa, salen, &evt->olpd, NULL);
+ _freea(iov);
+
if ((rv == SOCKET_ERROR) &&
((rv = GetLastError()) != ERROR_IO_PENDING)) {
// Synchronous failure.