aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-01-03 23:50:12 -0800
committerGarrett D'Amore <garrett@damore.org>2017-01-03 23:50:12 -0800
commit856c5c8e2aa4e07b2b628dd194a63ae13dae7ae3 (patch)
tree881df2438f89137f014d1706124b58d40115a47d /src/platform/posix
parent7ba8f81d551af515864c2c4ca47edf540a5edd32 (diff)
downloadnng-856c5c8e2aa4e07b2b628dd194a63ae13dae7ae3.tar.gz
nng-856c5c8e2aa4e07b2b628dd194a63ae13dae7ae3.tar.bz2
nng-856c5c8e2aa4e07b2b628dd194a63ae13dae7ae3.zip
Working towards TCP support.
Diffstat (limited to 'src/platform/posix')
-rw-r--r--src/platform/posix/posix_debug.c79
-rw-r--r--src/platform/posix/posix_impl.h29
2 files changed, 93 insertions, 15 deletions
diff --git a/src/platform/posix/posix_debug.c b/src/platform/posix/posix_debug.c
index 56b443e9..f1c6f9e7 100644
--- a/src/platform/posix/posix_debug.c
+++ b/src/platform/posix/posix_debug.c
@@ -11,8 +11,10 @@
#ifdef PLATFORM_POSIX_DEBUG
+#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
void
nni_plat_abort(void)
@@ -28,4 +30,81 @@ nni_plat_println(const char *message)
}
+const char *
+nni_plat_strerror(int errnum)
+{
+ if (errnum > NNG_ESYSERR) {
+ errnum -= NNG_ESYSERR;
+ }
+ return (strerror(errnum));
+}
+
+
+#define NNI_ERR(x, y) { x, y },
+
+// There are of course other errors than these, but these are the ones
+// that we might reasonably expect and want to handle "cleanly". Most of
+// the others should be handled by the system error code. Note that EFAULT
+// is very special, because if the error code is *that*, then we should panic
+// because an invalid system call has been made. (That would be a sign
+// of a serious software bug, in other words.) POSIX says that all these
+// error codes should exist, and be distinct positive numbers. (EWOULDBLOCK
+// and EAGAIN are permitted to have the same value.)
+static struct {
+ int posix_err;
+ int nng_err;
+}
+nni_plat_errnos[] = {
+ NNI_ERR(EINTR, NNG_EINTR)
+ NNI_ERR(EINVAL, NNG_EINVAL)
+ NNI_ERR(ENOMEM, NNG_ENOMEM)
+ NNI_ERR(EACCES, NNG_EPERM)
+ NNI_ERR(EADDRINUSE, NNG_EADDRINUSE)
+ NNI_ERR(EADDRNOTAVAIL, NNG_EADDRINVAL)
+ NNI_ERR(EAFNOSUPPORT, NNG_ENOTSUP)
+ NNI_ERR(EAGAIN, NNG_EAGAIN)
+ NNI_ERR(EBADF, NNG_ECLOSED)
+ NNI_ERR(EBUSY, NNG_EBUSY)
+ NNI_ERR(ECONNABORTED, NNG_ECLOSED)
+ NNI_ERR(ECONNREFUSED, NNG_ECONNREFUSED)
+ NNI_ERR(ECONNRESET, NNG_ECLOSED)
+ NNI_ERR(EHOSTUNREACH, NNG_EUNREACHABLE)
+ NNI_ERR(ENETUNREACH, NNG_EUNREACHABLE)
+ NNI_ERR(ENAMETOOLONG, NNG_EINVAL)
+ NNI_ERR(ENOENT, NNG_ENOENT)
+ NNI_ERR(ENOBUFS, NNG_ENOMEM)
+ NNI_ERR(ENOPROTOOPT, NNG_ENOTSUP)
+ NNI_ERR(ENOSYS, NNG_ENOTSUP)
+ NNI_ERR(ENOTSUP, NNG_ENOTSUP)
+ NNI_ERR(EPERM, NNG_EPERM)
+ NNI_ERR(EPIPE, NNG_ECLOSED)
+ NNI_ERR(EPROTO, NNG_EPROTO)
+ NNI_ERR(EPROTONOSUPPORT, NNG_ENOTSUP)
+ NNI_ERR(ETIME, NNG_ETIMEDOUT)
+ NNI_ERR(ETIMEDOUT, NNG_ETIMEDOUT)
+ NNI_ERR(EWOULDBLOCK, NNG_EAGAIN)
+ NNI_ERR(0, 0) // must be last
+};
+
+int
+nni_plat_errno(int errnum)
+{
+ int i;
+
+ if (errnum == 0) {
+ return (0);
+ }
+ if (errnum == EFAULT) {
+ nni_panic("System EFAULT encountered!");
+ }
+ for (i = 0; nni_plat_errnos[i].nng_err != 0; i++) {
+ if (errnum == nni_plat_errnos[i].posix_err) {
+ return (nni_plat_errnos[i].nng_err);
+ }
+ }
+ // Other system errno.
+ return (NNG_ESYSERR + errnum);
+}
+
+
#endif // PLATFORM_POSIX_DEBUG
diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h
index 5c5c3798..38fe27c9 100644
--- a/src/platform/posix/posix_impl.h
+++ b/src/platform/posix/posix_impl.h
@@ -12,35 +12,34 @@
// Some dependency notes:
//
-// PLATFORM_POSIX_THREAD and PLATFORM_POSIX_SYNCH depend on each other,
-// and they both depend on PLATFORM_POSIX_CLOCK. Furthermore, note that
+// PLATFORM_POSIX_THREAD depends on PLATFORM_POSIX_CLOCK. Furthermore,
// when using PLATFORM_POSIX_CLOCK, your condition variable timeouts need
-// to use the same base clock values. Normally all three should be used
-// together.
+// to use the same base clock values. Normally these should be used
+// together. Almost everything depends on PLATFORM_POSIX_DEBUG.
#ifdef PLATFORM_POSIX
#define PLATFORM_POSIX_ALLOC
#define PLATFORM_POSIX_DEBUG
#define PLATFORM_POSIX_CLOCK
+#define PLATFORM_POSIX_NET
#define PLATFORM_POSIX_RANDOM
-#define PLATFORM_POSIX_SYNCH
#define PLATFORM_POSIX_THREAD
#include "platform/posix/posix_config.h"
#endif
-// Define types that this platform uses.
-#ifdef PLATFORM_POSIX_SYNCH
+#ifdef PLATFORM_POSIX_DEBUG
+extern int nni_plat_errno(int);
-#include <pthread.h>
+#endif
-struct nni_mutex {
- pthread_mutex_t mx;
-};
+#ifdef PLATFORM_POSIX_NET
+typedef int nni_plat_tcpsock;
+#endif
-struct nni_cond {
- pthread_cond_t cv;
- pthread_mutex_t * mx;
-};
+// Define types that this platform uses.
+#ifdef PLATFORM_POSIX_THREAD
+
+#include <pthread.h>
// These types are provided for here, to permit them to be directly inlined
// elsewhere.