forked from gdamore/libzt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libzt.h
622 lines (560 loc) · 19.1 KB
/
libzt.h
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
/**
* @file
*
* Application-facing, partially-POSIX-compliant socket API
*/
#ifndef LIBZT_H
#define LIBZT_H
#include "libztDebug.h"
#include "libztDefs.h"
#include <stdlib.h>
#include <stdint.h>
#if defined(__linux__) || defined(__APPLE__)
#include <sys/socket.h>
#include <unistd.h>
#endif
#if defined(_WIN32)
#include <WinSock2.h>
#include <stdint.h>
#include <WS2tcpip.h>
#endif
/****************************************************************************/
/* DLL export for Windows */
/****************************************************************************/
#ifdef _WIN32
#ifdef ADD_EXPORTS
#define ZT_SOCKET_API __declspec(dllexport)
#else
#define ZT_SOCKET_API __declspec(dllimport)
#endif
#define ZTCALL __cdecl
#else
#define ZT_SOCKET_API
#define ZTCALL
#endif
/****************************************************************************/
/* ZeroTier Service Controls */
/****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief (optional) Sets the port for the background libzt service. If this function is called
* with a port number between 1-65535 it will attempt to bind to that port. If it is called with
* a port number of 0 it will attempt to randomly search for an available port. If this function
* is never called, the service will try to bind on LIBZT_DEFAULT_PORT which is 9994.
*
* @usage Should be called at the beginning of your application before `zts_startjoin()`
* @param portno Port number
* @return 0 if successful; or -1 if failed
*/
ZT_SOCKET_API int ZTCALL zts_set_service_port(int portno);
/**
* @brief Starts libzt
*
* @usage Should be called at the beginning of your application. Will blocks until all of the following conditions are met:
* - ZeroTier core service has been initialized
* - Cryptographic identity has been generated or loaded from directory specified by `path`
* - Virtual network is successfully joined
* - IP address is assigned by network controller service
* @param path path directory where cryptographic identities and network configuration files are stored and retrieved
* (`identity.public`, `identity.secret`)
* @param nwid A 16-digit hexidecimal network identifier (e.g. Earth: `8056c2e21c000001`)
* @return 0 if successful; or 1 if failed
*/
ZT_SOCKET_API int ZTCALL zts_start(const char *path, int blocking);
/**
* @brief Starts libzt
*
* @usage Should be called at the beginning of your application. Will blocks until all of the following conditions are met:
* - ZeroTier core service has been initialized
* - Cryptographic identity has been generated or loaded from directory specified by `path`
* - Virtual network is successfully joined
* - IP address is assigned by network controller service
* @param path path directory where cryptographic identities and network configuration files are stored and retrieved
* (`identity.public`, `identity.secret`)
* @param nwid A 16-digit hexidecimal network identifier (e.g. Earth: `8056c2e21c000001`)
* @return 0 if successful; or 1 if failed
*/
ZT_SOCKET_API int ZTCALL zts_startjoin(const char *path, const uint64_t nwid);
/**
* @brief Stops ZeroTier core services, stack drivers, stack threads, etc
*
* @usage This should be called at the end of your program or when you do not anticipate communicating over ZeroTier
* @return Returns 0 on success, -1 on failure
*/
ZT_SOCKET_API void ZTCALL zts_stop();
/**
* @brief Return whether ZeroTier core service is currently running
*
* @usage Call this after zts_start()
* @return 1 if running, 0 if not running
*/
ZT_SOCKET_API int ZTCALL zts_core_running();
/**
* @brief Return whether the userspace network stack is currently running
*
* @usage Call this after zts_start()
* @return 1 if running, 0 if not running
*/
ZT_SOCKET_API int ZTCALL zts_stack_running();
/**
* @brief Return whether libzt is ready to handle socket API calls. Alternatively you could
* have just called zts_startjoin(path, nwid)
*
* @usage Call this after zts_start()
* @return 1 if running, 0 if not running
*/
ZT_SOCKET_API int ZTCALL zts_ready();
/**
* @brief Join a network
*
* @usage Call this from application thread. Only after zts_start() has succeeded
* @param nwid A 16-digit hexidecimal virtual network ID
* @return 0 if successful, -1 for any failure
*/
ZT_SOCKET_API int ZTCALL zts_join(const uint64_t nwid);
/**
* @brief Leave a network
*
* @usage Call this from application thread. Only after zts_start() has succeeded
* @param nwid A 16-digit hexidecimal virtual network ID
* @return 0 if successful, -1 for any failure
*/
ZT_SOCKET_API int ZTCALL zts_leave(const uint64_t nwid);
/**
* @brief Copies the configuration path used by ZeroTier into the provided buffer
*
* @usage
* @param homePath Path to ZeroTier configuration files
* @param len Length of destination buffer
* @return
*/
ZT_SOCKET_API void ZTCALL zts_get_path(char *homePath, const size_t len);
/**
* @brief Returns the node ID of this instance
*
* @usage Call this after zts_start() and/or when zts_running() returns true
* @return
*/
ZT_SOCKET_API uint64_t ZTCALL zts_get_node_id();
/**
* @brief Returns the node ID of this instance (as read from a file)
*
* @usage Call with or without starting the service with zts_start()
* @param filepath Path to ZeroTier configuration files
* @return
*/
ZT_SOCKET_API uint64_t ZTCALL zts_get_node_id_from_file(const char *filepath);
/**
* @brief Returns whether any address has been assigned to the SockTap for this network
*
* @usage This is used as an indicator of readiness for service for the ZeroTier core and stack
* @param nwid Network ID
* @return
*/
ZT_SOCKET_API int ZTCALL zts_has_address(const uint64_t nwid);
/**
* @brief Returns the number of addresses assigned to this node for the given nwid
*
* @param nwid Network ID
* @return The number of addresses assigned
*/
ZT_SOCKET_API int ZTCALL zts_get_num_assigned_addresses(const uint64_t nwid);
/**
* @brief Returns the assigned address located at the given index
*
* @usage The indices of each assigned address are not guaranteed and should only
* be used for iterative purposes.
* @param nwid Network ID
* @param index location of assigned address
* @return The number of addresses assigned
*/
ZT_SOCKET_API int ZTCALL zts_get_address_at_index(
const uint64_t nwid, const int index, struct sockaddr *addr, socklen_t *addrlen);
/**
* @brief Get IP address for this device on a given network
*
* @usage FIXME: Only returns first address found, good enough for most cases
* @param nwid Network ID
* @param addr Destination structure for address
* @param addrlen size of destination address buffer, will be changed to size of returned address
* @return 0 if an address was successfully found, -1 if failure
*/
ZT_SOCKET_API int ZTCALL zts_get_address(
const uint64_t nwid, struct sockaddr_storage *addr, const int address_family);
/**
* @brief Computes a 6PLANE IPv6 address for the given Network ID and Node ID
*
* @usage Can call any time
* @param addr Destination structure for address
* @param nwid Network ID
* @param nodeId Node ID
* @return
*/
ZT_SOCKET_API void ZTCALL zts_get_6plane_addr(
struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
/**
* @brief Computes a RFC4193 IPv6 address for the given Network ID and Node ID
*
* @usage Can call any time
* @param addr Destination structure for address
* @param nwid Network ID
* @param nodeId Node ID
* @return
*/
ZT_SOCKET_API void ZTCALL zts_get_rfc4193_addr(
struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
/**
* @brief Return the number of peers
*
* @usage Call this after zts_start() has succeeded
* @return
*/
ZT_SOCKET_API unsigned long zts_get_peer_count();
/****************************************************************************/
/* Socket-like API */
/****************************************************************************/
/**
* @brief Create a socket
*
* This function will return an integer which can be used in much the same way as a
* typical file descriptor, however it is only valid for use with libzt library calls
* as this is merely a facade which is associated with the internal socket representation
* of both the network stacks and drivers.
*
* @usage Call this after zts_start() has succeeded
* @param socket_family Address family (AF_INET, AF_INET6)
* @param socket_type Type of socket (SOCK_STREAM, SOCK_DGRAM, SOCK_RAW)
* @param protocol Protocols supported on this socket
* @return
*/
ZT_SOCKET_API int ZTCALL zts_socket(int socket_family, int socket_type, int protocol);
/**
* @brief Connect a socket to a remote host
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param addr Remote host address to connect to
* @param addrlen Length of address
* @return
*/
ZT_SOCKET_API int ZTCALL zts_connect(int fd, const struct sockaddr *addr, socklen_t addrlen);
/**
* @brief Bind a socket to a virtual interface
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param addr Local interface address to bind to
* @param addrlen Length of address
* @return
*/
ZT_SOCKET_API int ZTCALL zts_bind(int fd, const struct sockaddr *addr, socklen_t addrlen);
/**
* @brief Listen for incoming connections
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param backlog Number of backlogged connection allowed
* @return
*/
ZT_SOCKET_API int ZTCALL zts_listen(int fd, int backlog);
/**
* @brief Accept an incoming connection
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param addr Address of remote host for accepted connection
* @param addrlen Length of address
* @return
*/
ZT_SOCKET_API int ZTCALL zts_accept(int fd, struct sockaddr *addr, socklen_t *addrlen);
/**
* @brief Accept an incoming connection
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param addr Address of remote host for accepted connection
* @param addrlen Length of address
* @param flags
* @return
*/
#if defined(__linux__)
int zts_accept4(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags);
#endif
/**
* @brief Set socket options
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param level Protocol level to which option name should apply
* @param optname Option name to set
* @param optval Source of option value to set
* @param optlen Length of option value
* @return
*/
ZT_SOCKET_API int ZTCALL zts_setsockopt(
int fd, int level, int optname, const void *optval, socklen_t optlen);
/**
* @brief Get socket options
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param level Protocol level to which option name should apply
* @param optname Option name to get
* @param optval Where option value will be stored
* @param optlen Length of value
* @return
*/
ZT_SOCKET_API int ZTCALL zts_getsockopt(
int fd, int level, int optname, void *optval, socklen_t *optlen);
/**
* @brief Get socket name
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param addr Name associated with this socket
* @param addrlen Length of name
* @return
*/
ZT_SOCKET_API int ZTCALL zts_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen);
/**
* @brief Get the peer name for the remote end of a connected socket
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param addr Name associated with remote end of this socket
* @param addrlen Length of name
* @return
*/
ZT_SOCKET_API int ZTCALL zts_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen);
/**
* @brief Gets current hostname
*
* @usage Call this after zts_start() has succeeded
* @param name
* @param len
* @return
*/
ZT_SOCKET_API int ZTCALL zts_gethostname(char *name, size_t len);
/**
* @brief Sets current hostname
*
* @usage Call this after zts_start() has succeeded
* @param name
* @param len
* @return
*/
ZT_SOCKET_API int ZTCALL zts_sethostname(const char *name, size_t len);
/**
* @brief Return a pointer to an object with the following structure describing an internet host referenced by name
*
* @usage Call this after zts_start() has succeeded
* @param name
* @return Returns pointer to hostent structure otherwise NULL if failure
*/
ZT_SOCKET_API struct hostent *zts_gethostbyname(const char *name);
/**
* @brief Close a socket
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @return
*/
ZT_SOCKET_API int ZTCALL zts_close(int fd);
/**
* @brief Waits for one of a set of file descriptors to become ready to perform I/O.
*
* @usage Call this after zts_start() has succeeded
* @param fds
* @param nfds
* @param timeout
* @return
*/
#if defined(__linux__)
/*
typedef unsigned int nfds_t;
int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout);
*/
#endif
/**
* @brief Monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready"
*
* @usage Call this after zts_start() has succeeded
* @param nfds
* @param readfds
* @param writefds
* @param exceptfds
* @param timeout
* @return
*/
ZT_SOCKET_API int ZTCALL zts_select(
int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
/**
* @brief Issue file control commands on a socket
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param cmd
* @param flags
* @return
*/
#if defined(_WIN32)
#define F_SETFL 0
#define O_NONBLOCK 0
#endif
ZT_SOCKET_API int ZTCALL zts_fcntl(int fd, int cmd, int flags);
/**
* @brief Control a device
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param request
* @param argp
* @return
*/
ZT_SOCKET_API int ZTCALL zts_ioctl(int fd, unsigned long request, void *argp);
/**
* @brief Send data to remote host
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param buf Pointer to data buffer
* @param len Length of data to write
* @param flags
* @return
*/
ZT_SOCKET_API ssize_t ZTCALL zts_send(int fd, const void *buf, size_t len, int flags);
/**
* @brief Send data to remote host
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param buf Pointer to data buffer
* @param len Length of data to write
* @param flags
* @param addr Destination address
* @param addrlen Length of destination address
* @return
*/
ZT_SOCKET_API ssize_t ZTCALL zts_sendto(
int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen);
/**
* @brief Send message to remote host
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param msg
* @param flags
* @return
*/
ZT_SOCKET_API ssize_t ZTCALL zts_sendmsg(int fd, const struct msghdr *msg, int flags);
/**
* @brief Receive data from remote host
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param buf Pointer to data buffer
* @param len Length of data buffer
* @param flags
* @return
*/
ZT_SOCKET_API ssize_t ZTCALL zts_recv(int fd, void *buf, size_t len, int flags);
/**
* @brief Receive data from remote host
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param buf Pointer to data buffer
* @param len Length of data buffer
* @param flags
* @param addr
* @param addrlen
* @return
*/
ZT_SOCKET_API ssize_t ZTCALL zts_recvfrom(
int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen);
/**
* @brief Receive a message from remote host
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param msg
* @param flags
* @return
*/
ZT_SOCKET_API ssize_t ZTCALL zts_recvmsg(int fd, struct msghdr *msg,int flags);
/**
* @brief Read bytes from socket onto buffer
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param buf Pointer to data buffer
* @param len Length of data buffer to receive data
* @return
*/
ZT_SOCKET_API int ZTCALL zts_read(int fd, void *buf, size_t len);
/**
* @brief Write bytes from buffer to socket
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param buf Pointer to data buffer
* @param len Length of buffer to write
* @return
*/
ZT_SOCKET_API int ZTCALL zts_write(int fd, const void *buf, size_t len);
/**
* @brief Shut down some aspect of a socket (read, write, or both)
*
* @usage Call this after zts_start() has succeeded
* @param fd File descriptor (only valid for use with libzt calls)
* @param how Which aspects of the socket should be shut down
* @return
*/
ZT_SOCKET_API int ZTCALL zts_shutdown(int fd, int how);
/**
* @brief Adds a DNS nameserver for the network stack to use
*
* @usage Call this after zts_start() has succeeded
* @param addr Address for DNS nameserver
* @return
*/
ZT_SOCKET_API int ZTCALL zts_add_dns_nameserver(struct sockaddr *addr);
/**
* @brief Removes a DNS nameserver
*
* @usage Call this after zts_start() has succeeded
* @param addr Address for DNS nameserver
* @return
*/
ZT_SOCKET_API int ZTCALL zts_del_dns_nameserver(struct sockaddr *addr);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // _H