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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2018-2018. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*
* ----------------------------------------------------------------------
* Purpose : Utility "stuff" for socket and net.
* ----------------------------------------------------------------------
*
*/
#ifndef SOCKET_UTIL_H__
#define SOCKET_UTIL_H__
#include <erl_nif.h>
#include "socket_int.h"
#define VOIDP(P) ((void*)P)
#define CHARP(P) ((char*)P)
#define ESOCK_ABORT(E) esock_abort(E, __func__, __FILE__, __LINE__)
#define ESOCK_ASSERT(e) ((void) ((e) ? 1 : (ESOCK_ABORT(#e), 0)))
extern
char* esock_encode_msghdr(ErlNifEnv* env,
int read,
struct msghdr* msgHdrP,
ErlNifBinary* dataBufP,
ErlNifBinary* ctrlBufP,
ERL_NIF_TERM* eSockAddr);
extern
char* esock_encode_iov(ErlNifEnv* env,
int read,
struct iovec* iov,
size_t len,
ErlNifBinary* data,
ERL_NIF_TERM* eIOV);
extern
char* esock_encode_cmsghdrs(ErlNifEnv* env,
ErlNifBinary* cmsgBinP,
struct msghdr* msgHdrP,
ERL_NIF_TERM* eCMsgHdr);
extern
char* esock_encode_mshghdr_flags(ErlNifEnv* env,
int msgFlags,
ERL_NIF_TERM* flags);
extern
char* esock_decode_sockaddr(ErlNifEnv* env,
ERL_NIF_TERM eSockAddr,
SocketAddress* sockAddrP,
unsigned int* addrLen);
extern
char* esock_encode_sockaddr(ErlNifEnv* env,
SocketAddress* sockAddrP,
unsigned int addrLen,
ERL_NIF_TERM* eSockAddr);
extern
char* esock_decode_sockaddr_in4(ErlNifEnv* env,
ERL_NIF_TERM eSockAddr,
struct sockaddr_in* sockAddrP,
unsigned int* addrLen);
extern
char* esock_encode_sockaddr_in4(ErlNifEnv* env,
struct sockaddr_in* sockAddrP,
unsigned int addrLen,
ERL_NIF_TERM* eSockAddr);
#if defined(HAVE_IN6) && defined(AF_INET6)
extern
char* esock_decode_sockaddr_in6(ErlNifEnv* env,
ERL_NIF_TERM eSockAddr,
struct sockaddr_in6* sockAddrP,
unsigned int* addrLen);
extern
char* esock_encode_sockaddr_in6(ErlNifEnv* env,
struct sockaddr_in6* sockAddrP,
unsigned int addrLen,
ERL_NIF_TERM* eSockAddr);
#endif
#ifdef HAVE_SYS_UN_H
extern
char* esock_decode_sockaddr_un(ErlNifEnv* env,
ERL_NIF_TERM eSockAddr,
struct sockaddr_un* sockAddrP,
unsigned int* addrLen);
extern
char* esock_encode_sockaddr_un(ErlNifEnv* env,
struct sockaddr_un* sockAddrP,
unsigned int addrLen,
ERL_NIF_TERM* eSockAddr);
#endif
extern
char* esock_decode_ip4_address(ErlNifEnv* env,
ERL_NIF_TERM eAddr,
struct in_addr* inAddrP);
extern
char* esock_encode_ip4_address(ErlNifEnv* env,
struct in_addr* addrP,
ERL_NIF_TERM* eAddr);
#if defined(HAVE_IN6) && defined(AF_INET6)
extern
char* esock_decode_ip6_address(ErlNifEnv* env,
ERL_NIF_TERM eAddr,
struct in6_addr* inAddrP);
extern
char* esock_encode_ip6_address(ErlNifEnv* env,
struct in6_addr* addrP,
ERL_NIF_TERM* eAddr);
#endif
extern
char* esock_decode_domain(ErlNifEnv* env,
ERL_NIF_TERM eDomain,
int* domain);
extern
char* esock_encode_domain(ErlNifEnv* env,
int domain,
ERL_NIF_TERM* eDomain);
extern
char* esock_decode_type(ErlNifEnv* env,
ERL_NIF_TERM eType,
int* type);
extern
char* esock_encode_type(ErlNifEnv* env,
int type,
ERL_NIF_TERM* eType);
extern
char* esock_decode_protocol(ErlNifEnv* env,
ERL_NIF_TERM eProtocol,
int* protocol);
extern
char* esock_encode_protocol(ErlNifEnv* env,
int type,
ERL_NIF_TERM* eProtocol);
extern
BOOLEAN_T esock_decode_string(ErlNifEnv* env,
const ERL_NIF_TERM eString,
char** stringP);
extern
BOOLEAN_T esock_extract_bool_from_map(ErlNifEnv* env,
ERL_NIF_TERM map,
ERL_NIF_TERM key,
BOOLEAN_T def);
extern
BOOLEAN_T esock_decode_bool(ERL_NIF_TERM val);
extern
ERL_NIF_TERM esock_encode_bool(BOOLEAN_T val);
extern
size_t esock_strnlen(const char *s, size_t maxlen);
extern
void esock_abort(const char* expr,
const char* func,
const char* file,
int line);
extern
ERL_NIF_TERM esock_make_ok2(ErlNifEnv* env, ERL_NIF_TERM any);
extern
ERL_NIF_TERM esock_make_ok3(ErlNifEnv* env, ERL_NIF_TERM val1, ERL_NIF_TERM val2);
extern
ERL_NIF_TERM esock_make_error(ErlNifEnv* env, ERL_NIF_TERM reason);
extern
ERL_NIF_TERM esock_make_error_str(ErlNifEnv* env, char* reason);
extern
ERL_NIF_TERM esock_make_error_errno(ErlNifEnv* env, int err);
extern
void esock_warning_msg(const char* format, ... );
#endif // SOCKET_UTIL_H__
|