summaryrefslogtreecommitdiff
path: root/src/platform/posix/posix_tcp.c
blob: 9caa157fb77a5d5abef6931a849265bc66bc4f58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "core/nng_impl.h"

#ifdef NNG_PLATFORM_POSIX
#include "platform/posix/posix_aio.h"

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

int
nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const nni_sockaddr *lsa,
    const nni_sockaddr *rsa, int mode)
{
	nni_posix_epdesc *      ed;
	int                     rv;
	struct sockaddr_storage ss;
	int                     len;

	NNI_ARG_UNUSED(mode);

	if ((rv = nni_posix_epdesc_init(&ed)) != 0) {
		return (rv);
	}

	if ((rsa != NULL) && (rsa->s_family != NNG_AF_UNSPEC)) {
		len = nni_posix_nn2sockaddr((void *) &ss, rsa);
		nni_posix_epdesc_set_remote(ed, &ss, len);
	}
	if ((lsa != NULL) && (lsa->s_family != NNG_AF_UNSPEC)) {
		len = nni_posix_nn2sockaddr((void *) &ss, lsa);
		nni_posix_epdesc_set_local(ed, &ss, len);
	}

	*epp = (void *) ed;
	return (0);
}

void
nni_plat_tcp_ep_fini(nni_plat_tcp_ep *ep)
{
	nni_posix_epdesc_fini((void *) ep);
}

void
nni_plat_tcp_ep_close(nni_plat_tcp_ep *ep)
{
	nni_posix_epdesc_close((void *) ep);
}

int
nni_plat_tcp_ep_listen(nni_plat_tcp_ep *ep, nng_sockaddr *bsa)
{
	int rv;
	rv = nni_posix_epdesc_listen((void *) ep);
	if ((rv == 0) && (bsa != NULL)) {
		rv = nni_posix_epdesc_sockname((void *) ep, bsa);
	}
	return (rv);
}

void
nni_plat_tcp_ep_connect(nni_plat_tcp_ep *ep, nni_aio *aio)
{
	return (nni_posix_epdesc_connect((void *) ep, aio));
}

void
nni_plat_tcp_ep_accept(nni_plat_tcp_ep *ep, nni_aio *aio)
{
	return (nni_posix_epdesc_accept((void *) ep, aio));
}

void
nni_plat_tcp_pipe_fini(nni_plat_tcp_pipe *p)
{
	nni_posix_pipedesc_fini((void *) p);
}

void
nni_plat_tcp_pipe_close(nni_plat_tcp_pipe *p)
{
	nni_posix_pipedesc_close((void *) p);
}

void
nni_plat_tcp_pipe_send(nni_plat_tcp_pipe *p, nni_aio *aio)
{
	nni_posix_pipedesc_send((void *) p, aio);
}

void
nni_plat_tcp_pipe_recv(nni_plat_tcp_pipe *p, nni_aio *aio)
{
	nni_posix_pipedesc_recv((void *) p, aio);
}

int
nni_plat_tcp_pipe_peername(nni_plat_tcp_pipe *p, nni_sockaddr *sa)
{
	return (nni_posix_pipedesc_peername((void *) p, sa));
}

int
nni_plat_tcp_pipe_sockname(nni_plat_tcp_pipe *p, nni_sockaddr *sa)
{
	return (nni_posix_pipedesc_sockname((void *) p, sa));
}

int
nni_plat_tcp_ntop(const nni_sockaddr *sa, char *ipstr, char *portstr)
{
	const void *ap;
	uint16_t    port;
	int         af;
	switch (sa->s_family) {
	case NNG_AF_INET:
		ap   = &sa->s_in.sa_addr;
		port = sa->s_in.sa_port;
		af   = AF_INET;
		break;
	case NNG_AF_INET6:
		ap   = &sa->s_in6.sa_addr;
		port = sa->s_in6.sa_port;
		af   = AF_INET6;
		break;
	default:
		return (NNG_EINVAL);
	}
	if (ipstr != NULL) {
		if (af == AF_INET6) {
			size_t l;
			ipstr[0] = '[';
			inet_ntop(af, ap, ipstr + 1, INET6_ADDRSTRLEN);
			l          = strlen(ipstr);
			ipstr[l++] = ']';
			ipstr[l++] = '\0';
		} else {
			inet_ntop(af, ap, ipstr, INET6_ADDRSTRLEN);
		}
	}
	if (portstr != NULL) {
#ifdef NNG_LITTLE_ENDIAN
		port = ((port >> 8) & 0xff) | ((port & 0xff) << 8);
#endif
		snprintf(portstr, 6, "%u", port);
	}
	return (0);
}

#endif // NNG_PLATFORM_POSIX