summaryrefslogtreecommitdiff
path: root/node/RPC.cpp
blob: e6591d7d41f4b02f39b5864fe58d26f74129cda8 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * ZeroTier One - Global Peer to Peer Ethernet
 * Copyright (C) 2012-2013  ZeroTier Networks LLC
 *
 * 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/>.
 *
 * --
 *
 * ZeroTier may be used and distributed under the terms of the GPLv3, which
 * are available at: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * If you would like to embed ZeroTier into a commercial application or
 * redistribute it in a modified binary form, please contact ZeroTier Networks
 * LLC. Start here: http://www.zerotier.com/
 */

#ifndef __WINDOWS__
#include <dlfcn.h>
#endif

#include "Utils.hpp"
#include "RuntimeEnvironment.hpp"
#include "RPC.hpp"
#include "Switch.hpp"
#include "Topology.hpp"

namespace ZeroTier {

RPC::LocalService::LocalService(const char *dllPath)
	throw(std::invalid_argument) :
	_handle((void *)0),
	_init((void *)0),
	_do((void *)0),
	_free((void *)0),
	_destroy((void *)0)
{
	_handle = dlopen(dllPath,RTLD_LAZY|RTLD_LOCAL);
	if (!_handle)
		throw std::invalid_argument("Unable to load DLL: dlopen() failed");

	_init = dlsym(_handle,"ZeroTierPluginInit");
	if (!_init) {
		dlclose(_handle);
		throw std::invalid_argument("Unable to resolve symbol ZeroTierPluginInit in DLL");
	}
	_do = dlsym(_handle,"ZeroTierPluginDo");
	if (!_do) {
		dlclose(_handle);
		throw std::invalid_argument("Unable to resolve symbol ZeroTierPluginDo in DLL");
	}
	_free = dlsym(_handle,"ZeroTierPluginFree");
	if (!_free) {
		dlclose(_handle);
		throw std::invalid_argument("Unable to resolve symbol ZeroTierPluginFree in DLL");
	}
	_destroy = dlsym(_handle,"ZeroTierPluginDestroy");
	if (!_destroy) {
		dlclose(_handle);
		throw std::invalid_argument("Unable to resolve symbol ZeroTierPluginDestroy in DLL");
	}

	if (((int (*)())_init)() < 0) {
		dlclose(_handle);
		throw std::invalid_argument("ZeroTierPluginInit() returned error");
	}
}

RPC::LocalService::~LocalService()
{
	if (_handle) {
		if (_destroy)
			((void (*)())_destroy)();
		dlclose(_handle);
	}
}

std::pair< int,std::vector<std::string> > RPC::LocalService::operator()(const std::vector<std::string> &args)
{
	unsigned int alengths[4096];
	const void *argptrs[4096];
	const unsigned int *rlengths = (const unsigned int *)0;
	const void **resultptrs = (const void **)0;
	std::vector<std::string> results;

	if (args.size() > 4096)
		throw std::runtime_error("args[] too long");

	for(unsigned int i=0;i<args.size();++i) {
		alengths[i] = args[i].length();
		argptrs[i] = (const void *)args[i].data();
	}

	int rcount = ((int (*)(unsigned int,const unsigned int *,const void **,const unsigned int **,const void ***))_do)((unsigned int)args.size(),alengths,argptrs,&rlengths,&resultptrs);

	for(int i=0;i<rcount;++i)
		results.push_back(std::string((const char *)resultptrs[i],rlengths[i]));

	((void (*)(int,const unsigned int *,const void **))_free)(rcount,rlengths,resultptrs);

	return std::pair< int,std::vector<std::string> >(rcount,results);
}

RPC::RPC(const RuntimeEnvironment *renv) :
	_r(renv)
{
}

RPC::~RPC()
{
	for(std::map<uint64_t,RemoteCallOutstanding>::iterator co(_remoteCallsOutstanding.begin());co!=_remoteCallsOutstanding.end();++co) {
		if (co->second.handler)
			co->second.handler(co->second.arg,co->first,co->second.peer,ZT_RPC_ERROR_CANCELLED,std::vector<std::string>());
	}

	for(std::map<std::string,LocalService *>::iterator s(_rpcServices.begin());s!=_rpcServices.end();++s)
		delete s->second;
}

std::pair< int,std::vector<std::string> > RPC::callLocal(const std::string &name,const std::vector<std::string> &args)
{
	Mutex::Lock _l(_rpcServices_m);
	std::map<std::string,LocalService *>::iterator s(_rpcServices.find(name));
	if (s == _rpcServices.end())
		return std::pair< int,std::vector<std::string> >(ZT_RPC_ERROR_NOT_FOUND,std::vector<std::string>());
	return ((*(s->second))(args));
}

uint64_t RPC::callRemote(
	const Address &peer,
	const std::string &name,
	const std::vector<std::string> &args,
	void (*handler)(void *,uint64_t,const Address &,int,const std::vector<std::string> &),
	void *arg)
	throw(std::invalid_argument,std::out_of_range)
{
	Packet outp(peer,_r->identity.address(),Packet::VERB_RPC);

	if (name.length() > 0xffff)
		throw std::invalid_argument("function name too long");
	outp.append((uint16_t)name.length());
	outp.append(name);
	for(std::vector<std::string>::const_iterator a(args.begin());a!=args.end();++a) {
		if (a->length() > 0xffff)
			throw std::invalid_argument("argument too long");
		outp.append((uint16_t)a->length());
		outp.append(*a);
	}
	outp.compress();

	uint64_t id = outp.packetId();

	{
		Mutex::Lock _l(_remoteCallsOutstanding_m);
		RemoteCallOutstanding &rc = _remoteCallsOutstanding[id];
		rc.callTime = Utils::now();
		rc.peer = peer;
		rc.handler = handler;
		rc.arg = arg;
	}

	_r->sw->send(outp,true);

	return id;
}

void RPC::clean()
{
	Mutex::Lock _l(_remoteCallsOutstanding_m);
	uint64_t now = Utils::now();
	for(std::map<uint64_t,RemoteCallOutstanding>::iterator co(_remoteCallsOutstanding.begin());co!=_remoteCallsOutstanding.end();) {
		if ((now - co->second.callTime) >= ZT_RPC_TIMEOUT) {
			if (co->second.handler)
				co->second.handler(co->second.arg,co->first,co->second.peer,ZT_RPC_ERROR_EXPIRED_NO_RESPONSE,std::vector<std::string>());
			_remoteCallsOutstanding.erase(co++);
		} else ++co;
	}
}

} // namespace ZeroTier