summaryrefslogtreecommitdiff
path: root/src/Connection.hpp
blob: 681d919f80cce9b1268aa4970bc0e51944cd1fc7 (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
/*
 * ZeroTier SDK - Network Virtualization Everywhere
 * Copyright (C) 2011-2016  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/>.
 */

#ifndef ZT_CONNECTION_HPP
#define ZT_CONNECTION_HPP

#include <sys/socket.h>

// picoTCP
#include "pico_socket.h"

// ZT
#include "Phy.hpp"

// SDK
#include "ZeroTierSDK.h"
#include "SocketTap.hpp"
//#include "RingBuffer.hpp"

namespace ZeroTier {

	/*
	* Connection object
	*/
	struct Connection
	{
		//circular_buffer<char> crbuf = circular_buffer<char>(ZT_TCP_RX_BUF_SZ);
		//circular_buffer<char> ctbuf = circular_buffer<char>(ZT_TCP_TX_BUF_SZ);

		int pid;
		PhySocket *sock;				
		struct pico_socket *picosock;

		// TODO: For getsockname, etc
		struct sockaddr_storage *local_addr; // Address we've bound to locally
		struct sockaddr_storage *peer_addr; // Address of connection call to remote host

		// RX/TX buffers
		int txsz, rxsz;
		unsigned char txbuf[ZT_TCP_TX_BUF_SZ];
		unsigned char rxbuf[ZT_TCP_RX_BUF_SZ];

		int data_sock;
		int socket_family, socket_type;

		int app_fd; // provided to app for I/O
		int sdk_fd; // provided to SDK for I/O

		std::queue<Connection*> _AcceptedConnections;
		SocketTap *tap; // Reference to SocketTap  
		int state; // See ZeroTierSDK.h for (ZT_SOCK_STATE_*)

		Connection() {
			// DEBUG_INFO("Connection() this = %p", this);
			ZT_PHY_SOCKFD_TYPE fdpair[2];
			if(socketpair(PF_LOCAL, SOCK_STREAM, 0, fdpair) < 0) {
				if(errno < 0) {
					DEBUG_ERROR("unable to create socketpair");
					return;
				}
			}
			sdk_fd = fdpair[0];
			app_fd = fdpair[1];
			if(ZT_SOCK_BEHAVIOR_LINGER) {
				struct linger so_linger;
				so_linger.l_onoff = true;
				so_linger.l_linger = ZT_SOCK_BEHAVIOR_LINGER_TIME;
		        if(zts_setsockopt(app_fd, SOL_SOCKET, SO_LINGER, &so_linger, sizeof so_linger) < 0) {
    				DEBUG_ERROR("error setsockopt (%d)", errno);
		        }
		    }
		}
		~Connection()
		{
			// DEBUG_INFO("~Connection() this = %p", this);
		}
	};

	/*
	 * A helper object for passing SocketTaps and Connections through the stack
	 */
	struct ConnectionPair
	{
	  SocketTap *tap;
	  Connection *conn;
	  ConnectionPair(SocketTap *_tap, Connection *conn) : tap(_tap), conn(conn) {}
	};
}
#endif