summaryrefslogtreecommitdiff
path: root/tests/tests/net/jni/NativeMultinetworkJni.c
blob: c2dff8dbefa8aa3d1d6509eeb7efaf4873e403f4 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#define LOG_TAG "MultinetworkApiTest"
#include <utils/Log.h>

#include <arpa/inet.h>
#include <errno.h>
#include <inttypes.h>
#include <jni.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <android/multinetwork.h>

#define UNUSED(X) ((void) (X))

static const char kHostname[] = "connectivitycheck.android.com";


JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runGetaddrinfoCheck(
        JNIEnv* env, jclass class, jlong nethandle) {
    UNUSED(env);
    UNUSED(class);
    net_handle_t handle = (net_handle_t) nethandle;
    struct addrinfo *res = NULL;

    errno = 0;
    int rval = android_getaddrinfofornetwork(handle, kHostname, NULL, NULL, &res);
    const int saved_errno = errno;
    freeaddrinfo(res);

    ALOGD("android_getaddrinfofornetwork(%llu, %s) returned rval=%d errno=%d",
          handle, kHostname, rval, saved_errno);
    return rval == 0 ? 0 : -saved_errno;
}

JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runSetprocnetwork(
        JNIEnv* env, jclass class, jlong nethandle) {
    UNUSED(env);
    UNUSED(class);
    net_handle_t handle = (net_handle_t) nethandle;

    errno = 0;
    int rval = android_setprocnetwork(handle);
    const int saved_errno = errno;
    ALOGD("android_setprocnetwork(%llu) returned rval=%d errno=%d",
          handle, rval, saved_errno);
    return rval == 0 ? 0 : -saved_errno;
}

JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runSetsocknetwork(
        JNIEnv* env, jclass class, jlong nethandle) {
    UNUSED(env);
    UNUSED(class);
    net_handle_t handle = (net_handle_t) nethandle;

    errno = 0;
    int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    if (fd < 0) {
        ALOGD("socket() failed, errno=%d", errno);
        return -errno;
    }

    errno = 0;
    int rval = android_setsocknetwork(handle, fd);
    const int saved_errno = errno;
    ALOGD("android_setprocnetwork(%llu, %d) returned rval=%d errno=%d",
          handle, fd, rval, saved_errno);
    close(fd);
    return rval == 0 ? 0 : -saved_errno;
}

// Use sizeof("x") - 1 because we need a compile-time constant, and strlen("x")
// isn't guaranteed to fold to a constant.
static const int kSockaddrStrLen = INET6_ADDRSTRLEN + sizeof("[]:65535") - 1;

void sockaddr_ntop(const struct sockaddr *sa, socklen_t salen, char *dst, const size_t size) {
    char addrstr[INET6_ADDRSTRLEN];
    char portstr[sizeof("65535")];
    char buf[kSockaddrStrLen+1];

    int ret = getnameinfo(sa, salen,
                          addrstr, sizeof(addrstr),
                          portstr, sizeof(portstr),
                          NI_NUMERICHOST | NI_NUMERICSERV);
    if (ret == 0) {
        snprintf(buf, sizeof(buf),
                 (sa->sa_family == AF_INET6) ? "[%s]:%s" : "%s:%s",
                 addrstr, portstr);
    } else {
        sprintf(buf, "???");
    }

    strlcpy(dst, buf, size);
}

JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runDatagramCheck(
        JNIEnv* env, jclass class, jlong nethandle) {
    UNUSED(env);
    UNUSED(class);
    const struct addrinfo kHints = {
        .ai_flags = AI_ADDRCONFIG,
        .ai_family = AF_UNSPEC,
        .ai_socktype = SOCK_DGRAM,
        .ai_protocol = IPPROTO_UDP,
    };
    struct addrinfo *res = NULL;
    net_handle_t handle = (net_handle_t) nethandle;

    static const char kPort[] = "443";
    int rval = android_getaddrinfofornetwork(handle, kHostname, kPort, &kHints, &res);
    if (rval != 0) {
        ALOGD("android_getaddrinfofornetwork(%llu, %s) returned rval=%d errno=%d",
              handle, kHostname, rval, errno);
        freeaddrinfo(res);
        return -errno;
    }

    // Rely upon getaddrinfo sorting the best destination to the front.
    int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (fd < 0) {
        ALOGD("socket(%d, %d, %d) failed, errno=%d",
              res->ai_family, res->ai_socktype, res->ai_protocol, errno);
        freeaddrinfo(res);
        return -errno;
    }

    rval = android_setsocknetwork(handle, fd);
    ALOGD("android_setprocnetwork(%llu, %d) returned rval=%d errno=%d",
          handle, fd, rval, errno);
    if (rval != 0) {
        close(fd);
        freeaddrinfo(res);
        return -errno;
    }

    char addrstr[kSockaddrStrLen+1];
    sockaddr_ntop(res->ai_addr, res->ai_addrlen, addrstr, sizeof(addrstr));
    ALOGD("Attempting connect() to %s ...", addrstr);

    rval = connect(fd, res->ai_addr, res->ai_addrlen);
    if (rval != 0) {
        close(fd);
        freeaddrinfo(res);
        return -errno;
    }
    freeaddrinfo(res);

    struct sockaddr_storage src_addr;
    socklen_t src_addrlen = sizeof(src_addr);
    if (getsockname(fd, (struct sockaddr *)&src_addr, &src_addrlen) != 0) {
        close(fd);
        return -errno;
    }
    sockaddr_ntop((const struct sockaddr *)&src_addr, sizeof(src_addr), addrstr, sizeof(addrstr));
    ALOGD("... from %s", addrstr);

    // Don't let reads or writes block indefinitely.
    const struct timeval timeo = { 2, 0 };  // 2 seconds
    setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));

    // For reference see:
    //     https://tools.ietf.org/html/draft-tsvwg-quic-protocol#section-6.1
    uint8_t quic_packet[1200] = {
        0x0d,                    // public flags:
                                 //   - version present (0x01),
                                 //   - 64bit connection ID (0x0c),
                                 //   - 1 byte packet number (0x00)
        0, 0, 0, 0, 0, 0, 0, 0,  // 64bit connection ID
        0xaa, 0xda, 0xca, 0xaa,  // reserved-space version number
        1,                       // 1 byte packet number
        0x00,                    // private flags
        0x07,                    // PING frame (cuz why not)
    };

    arc4random_buf(quic_packet + 1, 8);  // random connection ID

    uint8_t response[1500];
    ssize_t sent, rcvd;
    static const int MAX_RETRIES = 5;
    int i, errnum = 0;

    for (i = 0; i < MAX_RETRIES; i++) {
        sent = send(fd, quic_packet, sizeof(quic_packet), 0);
        if (sent < (ssize_t)sizeof(quic_packet)) {
            errnum = errno;
            ALOGD("send(QUIC packet) returned sent=%zd, errno=%d", sent, errnum);
            close(fd);
            return -errnum;
        }

        rcvd = recv(fd, response, sizeof(response), 0);
        if (rcvd > 0) {
            break;
        } else {
            errnum = errno;
            ALOGD("[%d/%d] recv(QUIC response) returned rcvd=%zd, errno=%d",
                  i + 1, MAX_RETRIES, rcvd, errnum);
        }
    }
    if (rcvd < 9) {
        ALOGD("QUIC UDP %s: sent=%zd but rcvd=%zd, errno=%d", kPort, sent, rcvd, errnum);
        if (rcvd <= 0) {
            ALOGD("Does this network block UDP port %s?", kPort);
        }
        close(fd);
        return -EPROTO;
    }

    int conn_id_cmp = memcmp(quic_packet + 1, response + 1, 8);
    if (conn_id_cmp != 0) {
        ALOGD("sent and received connection IDs do not match");
        close(fd);
        return -EPROTO;
    }

    // TODO: Replace this quick 'n' dirty test with proper QUIC-capable code.

    close(fd);
    return 0;
}