blob: 21a278e9beb7a586a337601e361865354e15643c (
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
|
/*
* Copyright (c)2013-2021 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2026-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
package com.zerotier.sockets;
import com.zerotier.sockets.ZeroTierNative;
import java.net.InetAddress;
/**
* Convenience class for holding address information. Used internally by JNI layer.
* You (as a consumer of this library) should probably not use this as it is non-standard
* and very likely to be removed at some point.
*/
class ZeroTierSocketAddress {
private byte[] _ip6 = new byte[16];
private byte[] _ip4 = new byte[4];
private int _family;
private int _port; // Also reused for netmask or prefix
public ZeroTierSocketAddress()
{
}
/**
* Constructor
*/
public ZeroTierSocketAddress(String ipStr, int port)
{
if (ipStr.contains(":")) {
_family = ZeroTierNative.ZTS_AF_INET6;
try {
InetAddress ip = InetAddress.getByName(ipStr);
_ip6 = ip.getAddress();
}
catch (Exception e) {
}
}
else if (ipStr.contains(".")) {
_family = ZeroTierNative.ZTS_AF_INET;
try {
InetAddress ip = InetAddress.getByName(ipStr);
_ip4 = ip.getAddress();
}
catch (Exception e) {
}
}
_port = port;
}
/**
* Convert to string (ip portion only)
*/
public String ipString()
{
if (_family == ZeroTierNative.ZTS_AF_INET) {
try {
InetAddress inet = InetAddress.getByAddress(_ip4);
return "" + inet.getHostAddress();
}
catch (Exception e) {
System.out.println(e);
}
}
if (_family == ZeroTierNative.ZTS_AF_INET6) {
try {
InetAddress inet = InetAddress.getByAddress(_ip6);
return "" + inet.getHostAddress();
}
catch (Exception e) {
System.out.println(e);
}
}
return "";
}
/**
* Convert to string (ip and port)
*/
public String toString()
{
return ipString() + ":" + _port;
}
/**
* Convert to string (ip+netmask and port)
*/
public String toCIDR()
{
return ipString() + "/" + _port;
}
/**
* Get port
*/
public int getPort()
{
return _port;
}
/**
* Get netmask
*/
public int getNetmask()
{
return _port;
}
/**
* Get prefix (stored in port)
*/
public int getPrefix()
{
return _port;
}
}
|