summaryrefslogtreecommitdiff
path: root/src/bindings/java/com/zerotier/sockets/ZeroTierInputStream.java
blob: e5d2d4f380b525f9937bcfb96ad44e1388e660b6 (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
206
207
208
209
210
/*
 * 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.io.*;
import java.util.Objects;

/**
 * Extends InputStream using ZeroTier as a transport
 */
public class ZeroTierInputStream extends InputStream {
    /**
     * File descriptor used by lower native layer
     */
    public int zfd = -1;

    /**
     * Close the ZeroTierInputStream
     * @exception IOException when an I/O error occurs
     */
    public void close() throws IOException
    {
        /* Note: this operation currently only stops RX on a socket that is shared
        between both I/OStreams. This means that closing this stream will only shutdown
        that aspect of the socket but not actually close it and free resources. Resources
        will be properly freed when the socket implementation's close() is called or if
        both I/OStreams are closed separately */
        ZeroTierNative.zts_bsd_shutdown(zfd, ZeroTierNative.ZTS_SHUT_RD);
        zfd = -1;
    }

    /**
     * Transfer bytes from this stream to another
     * @param destStream Unused
     * @return Number of bytes transferred
     * @exception IOException when an I/O error occurs
     */
    public long transferTo(OutputStream destStream) throws IOException
    {
        Objects.requireNonNull(destStream, "destStream must not be null");
        int bytesTransferred = 0, bytesRead;
        byte[] buf = new byte[8192];
        while (((bytesRead = ZeroTierNative.zts_bsd_read(zfd, buf)) >= 0)) {
            destStream.write(buf, 0, bytesRead);
            bytesTransferred += bytesRead;
        }
        return bytesTransferred;
    }

    /**
     * Read a single byte from the stream
     * @return Single byte read
     * @exception IOException when an I/O error occurs
     */
    public int read() throws IOException
    {
        byte[] buf = new byte[1];
        // Unlike a native read(), if nothing is read we should return -1
        int retval = ZeroTierNative.zts_bsd_read(zfd, buf);
        if ((retval == 0) | (retval == -104) /* EINTR, from SO_RCVTIMEO */) {
            return -1;
        }
        if (retval < 0) {
            throw new IOException("read(), errno=" + retval);
        }
        return buf[0];
    }

    /**
     * Read from stream into buffer
     * @param destBuffer Destination buffer
     * @return Number of bytes read
     * @exception IOException when an I/O error occurs
     */
    public int read(byte[] destBuffer) throws IOException
    {
        Objects.requireNonNull(destBuffer, "input byte array must not be null");
        // Unlike a native read(), if nothing is read we should return -1
        int retval = ZeroTierNative.zts_bsd_read(zfd, destBuffer);
        if ((retval == 0) | (retval == -104) /* EINTR, from SO_RCVTIMEO */) {
            return -1;
        }
        if (retval < 0) {
            throw new IOException("read(destBuffer), errno=" + retval);
        }
        return retval;
    }

    /**
     * Read from stream into buffer at offset
     * @param destBuffer Destination buffer
     * @param offset Where in the destination buffer bytes should be written
     * @param numBytes Number of bytes to read
     * @return Number of bytes read.
     * @exception IOException when an I/O error occurs
     */
    public int read(byte[] destBuffer, int offset, int numBytes) throws IOException
    {
        Objects.requireNonNull(destBuffer, "input byte array must not be null");
        if (offset < 0) {
            throw new IndexOutOfBoundsException("offset < 0");
        }
        if (numBytes < 0) {
            throw new IndexOutOfBoundsException("numBytes < 0");
        }
        if (numBytes > (destBuffer.length - offset)) {
            throw new IndexOutOfBoundsException("numBytes > (destBuffer.length - offset");
        }
        if (numBytes == 0) {
            return 0;
        }
        // Unlike a native read(), if nothing is read we should return -1
        int retval = ZeroTierNative.zts_bsd_read_offset(zfd, destBuffer, offset, numBytes);
        if ((retval == 0) | (retval == -104) /* EINTR, from SO_RCVTIMEO */) {
            return -1;
        }
        if (retval < 0) {
            throw new IOException("read(destBuffer, offset, numBytes), errno=" + retval);
        }
        return retval;
    }

    /**
     * Read all available data from stream
     * @return Array of bytes
     * @exception IOException when an I/O error occurs
     */
    public byte[] readAllBytes() throws IOException
    {
        int pendingDataSize = ZeroTierNative.zts_get_pending_data_size(zfd);
        byte[] buf = new byte[pendingDataSize];
        int retval = ZeroTierNative.zts_bsd_read(zfd, buf);
        if ((retval == 0) | (retval == -104) /* EINTR, from SO_RCVTIMEO */) {
            // No action needed
        }
        if (retval < 0) {
            throw new IOException("readAllBytes(), errno=" + retval);
        }
        return buf;
    }

    /**
     * Read a given number of bytes from the stream into a buffer
     * @param destBuffer Destination buffer
     * @param offset Where in the destination buffer bytes should be written
     * @param numBytes Number of bytes to read
     * @return Nothing.
     * @exception IOException when an I/O error occurs
     */
    public int readNBytes(byte[] destBuffer, int offset, int numBytes) throws IOException
    {
        Objects.requireNonNull(destBuffer, "input byte array must not be null");
        if (offset < 0) {
            throw new IndexOutOfBoundsException("offset < 0");
        }
        if (numBytes < 0) {
            throw new IndexOutOfBoundsException("numBytes < 0");
        }
        if (numBytes > (destBuffer.length - offset)) {
            throw new IndexOutOfBoundsException("numBytes > destBuffer.length - offset");
        }
        if (numBytes == 0) {
            return 0;
        }
        int retval = ZeroTierNative.zts_bsd_read_offset(zfd, destBuffer, offset, numBytes);
        if ((retval == 0) | (retval == -104) /* EINTR, from SO_RCVTIMEO */) {
            // No action needed
        }
        if (retval < 0) {
            throw new IOException("readNBytes(destBuffer, offset, numBytes), errno=" + retval);
        }
        return retval;
    }

    /**
     * Skip a certain number of bytes
     * @param numBytes Unused
     * @return Nothing.
     * @exception IOException when an I/O error occurs
     */
    public long skip(long numBytes) throws IOException
    {
        if (numBytes <= 0) {
            return 0;
        }
        long bytesRemaining = numBytes, bytesRead;
        int bufSize = (int)Math.min(2048, bytesRemaining);
        byte[] buf = new byte[bufSize];
        while (bytesRemaining > 0) {
            if ((bytesRead = ZeroTierNative.zts_bsd_read_length(zfd, buf, (int)Math.min(bufSize, bytesRemaining)))
                < 0) {
                break;
            }
            bytesRemaining -= bytesRead;
        }
        return numBytes - bytesRemaining;
    }
}