summaryrefslogtreecommitdiff
path: root/examples/java/ZeroTierHelloWorld/src/zerotier/ZeroTier.java
blob: b312f251deee6e1bad632c0805d9365e45573084 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2015  ZeroTier, Inc.
 *
 * 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/
 */

package zerotier;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.zip.ZipError;

public class ZeroTier {

	public static String Version()
	{
		return "1.2.2";
	}
	
    // Socket families
    public static int AF_UNIX = 1;
    public static int AF_INET = 2;

    // Socket types
    public static int SOCK_STREAM = 1;
    public static int SOCK_DGRAM = 2;

    // fcntl flags
    public static int O_APPEND = 1024;
    public static int O_NONBLOCK = 2048;
    public static int O_ASYNC = 8192;
    public static int O_DIRECT = 65536;
    public static int O_NOATIME = 262144;

    // fcntl cmds
    public static int F_GETFL = 3;
    public static int F_SETFL = 4;

    // Loads JNI code
    static { System.loadLibrary("zt"); }

    // ZeroTier service controls
    public native void ztjni_start(String homeDir);
    public void start(String homeDir) {
    	ztjni_start(homeDir);
    }

    public native void ztjni_join(String nwid);
    public void join(String nwid) {
    	ztjni_join(nwid);
    }

    public native void ztjni_leave(String nwid);
    public void leave(String nwid) {
    	ztjni_leave(nwid);
    }

    public native ArrayList<String> ztjni_get_addresses(String nwid);
    public ArrayList<String> get_addresses(String nwid) {
        int err = -1;
        ArrayList<String> addresses;
        while (err < 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            addresses = ztjni_get_addresses(nwid);
            if (addresses.size() > 0) {
                return addresses;
            }
        }
        return null;
    }
    
    public native boolean ztjni_running();
    public boolean running() {
        return ztjni_running();
    }

    public native int ztjni_socket(int family, int type, int protocol);
    public int socket(int family, int type, int protocol) {
        return ztjni_socket(family, type, protocol);
    }

    public native int ztjni_connect(int fd, String addr, int port);

    public int connect(int sock, Address zaddr, String nwid) {
        return connect(sock, zaddr.Address(), zaddr.Port(), nwid);
    }

    public int connect(int sock, String addr, int port, String nwid)
    {
        int err = -1;
        ArrayList<String> addresses;
        while (err < 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            addresses = ztjni_get_addresses(nwid);
            if (addresses.size() > 0) {
                if(!addresses.get(0).startsWith("-1.-1.-1.-1/-1")) {
                    err = ztjni_connect(sock, addr, port);
                }
            }
        }
        return err;
    }

    public native int ztjni_bind(int fd, String addr, int port);

    public int bind(int sock, Address zaddr, String nwid) {
        return bind(sock, zaddr.Address(), zaddr.Port(), nwid);
    }
    public int bind(int sock, String addr, int port, String nwid) {
        int err = -1;
        ArrayList<String> addresses;
        while (err < 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            addresses = ztjni_get_addresses(nwid);
            if (addresses.size() > 0) {
                if(!addresses.get(0).startsWith("-1.-1.-1.-1/-1")) {
                    err = ztjni_bind(sock, addr, port);
                }
            }
        }
        return err;
    }

    public native int ztjni_accept4(int fd, String addr, int port);
    public int accept4(int fd, String addr, int port) {
        return ztjni_accept4(fd,addr,port);
    }

    public native int ztjni_accept(int fd, zerotier.Address addr);
    public int accept(int fd, zerotier.Address addr) {
        return ztjni_accept(fd, addr);
    }

    public native int ztjni_listen(int fd, int backlog);
    public int listen(int fd, int backlog) {
        return ztjni_listen(fd,backlog);
    }

    public native int ztjni_close(int fd);
    public int close(int fd) {
        return ztjni_close(fd);
    }

    public native int ztjni_read(int fd, byte[] buf, int len);
    public int read(int fd, byte[] buf, int len) {
        return ztjni_read(fd, buf, len);
    }

    public native int ztjni_write(int fd, byte[] buf, int len);
    public int write(int fd, byte[] buf, int len) {
        return ztjni_write(fd, buf, len);
    }

    public native int ztjni_sendto(int fd, byte[] buf, int len, int flags, zerotier.Address addr);
    public int sendto(int fd, byte[] buf, int len, int flags, zerotier.Address addr){
        return ztjni_sendto(fd,buf,len,flags,addr);
    }

    public native int ztjni_send(int fd, byte[] buf, int len, int flags);
    public int send(int fd, byte[] buf, int len, int flags) {
        return ztjni_send(fd, buf, len, flags);
    }

    public native int ztjni_recvfrom(int fd, byte[] buf, int len, int flags, zerotier.Address addr);
    public int recvfrom(int fd, byte[] buf, int len, int flags, zerotier.Address addr){
        return ztjni_recvfrom(fd,buf,len,flags,addr);
    }

    public native int ztjni_fcntl(int sock, int cmd, int flag);
    public int fcntl(int sock, int cmd, int flag) {
        return  ztjni_fcntl(sock, F_SETFL, O_NONBLOCK);
    }
}