1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdint.h>
19#include <string.h>
20#include <errno.h>
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <arpa/inet.h>
24#include <netinet/in.h>
25
26#define LOG_TAG "RtpStream"
27#include <utils/Log.h>
28
29#include "jni.h"
30#include "JNIHelp.h"
31
32extern int parse(JNIEnv *env, jstring jAddress, int port, sockaddr_storage *ss);
33
34namespace {
35
36jfieldID gSocket;
37
38jint create(JNIEnv *env, jobject thiz, jstring jAddress)
39{
40    env->SetIntField(thiz, gSocket, -1);
41
42    sockaddr_storage ss;
43    if (parse(env, jAddress, 0, &ss) < 0) {
44        // Exception already thrown.
45        return -1;
46    }
47
48    int socket = ::socket(ss.ss_family, SOCK_DGRAM, 0);
49    socklen_t len = sizeof(ss);
50    if (socket == -1 || bind(socket, (sockaddr *)&ss, sizeof(ss)) != 0 ||
51        getsockname(socket, (sockaddr *)&ss, &len) != 0) {
52        jniThrowException(env, "java/net/SocketException", strerror(errno));
53        ::close(socket);
54        return -1;
55    }
56
57    uint16_t *p = (ss.ss_family == AF_INET) ?
58        &((sockaddr_in *)&ss)->sin_port : &((sockaddr_in6 *)&ss)->sin6_port;
59    uint16_t port = ntohs(*p);
60    if ((port & 1) == 0) {
61        env->SetIntField(thiz, gSocket, socket);
62        return port;
63    }
64    ::close(socket);
65
66    socket = ::socket(ss.ss_family, SOCK_DGRAM, 0);
67    if (socket != -1) {
68        uint16_t delta = port << 1;
69        ++port;
70
71        for (int i = 0; i < 1000; ++i) {
72            do {
73                port += delta;
74            } while (port < 1024);
75            *p = htons(port);
76
77            if (bind(socket, (sockaddr *)&ss, sizeof(ss)) == 0) {
78                env->SetIntField(thiz, gSocket, socket);
79                return port;
80            }
81        }
82    }
83
84    jniThrowException(env, "java/net/SocketException", strerror(errno));
85    ::close(socket);
86    return -1;
87}
88
89void close(JNIEnv *env, jobject thiz)
90{
91    int socket = env->GetIntField(thiz, gSocket);
92    ::close(socket);
93    env->SetIntField(thiz, gSocket, -1);
94}
95
96JNINativeMethod gMethods[] = {
97    {"create", "(Ljava/lang/String;)I", (void *)create},
98    {"close", "()V", (void *)close},
99};
100
101} // namespace
102
103int registerRtpStream(JNIEnv *env)
104{
105    jclass clazz;
106    if ((clazz = env->FindClass("android/net/rtp/RtpStream")) == NULL ||
107        (gSocket = env->GetFieldID(clazz, "mSocket", "I")) == NULL ||
108        env->RegisterNatives(clazz, gMethods, NELEM(gMethods)) < 0) {
109        ALOGE("JNI registration failed");
110        return -1;
111    }
112    return 0;
113}
114