OLD | NEW |
| (Empty) |
1 /* Licensed to the Apache Software Foundation (ASF) under one or more | |
2 * contributor license agreements. See the NOTICE file distributed with | |
3 * this work for additional information regarding copyright ownership. | |
4 * The ASF licenses this file to You under the Apache License, Version 2.0 | |
5 * (the "License"); you may not use this file except in compliance with | |
6 * the License. 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 /* | |
18 * | |
19 * @author Mladen Turk | |
20 * @version $Id: address.c 1442587 2013-02-05 13:49:48Z rjung $ | |
21 */ | |
22 | |
23 #include "tcn.h" | |
24 | |
25 TCN_IMPLEMENT_CALL(jlong, Address, info)(TCN_STDARGS, | |
26 jstring hostname, | |
27 jint family, jint port, | |
28 jint flags, jlong pool) | |
29 { | |
30 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
31 TCN_ALLOC_CSTRING(hostname); | |
32 char *sp = NULL; | |
33 int scope_id = 0; | |
34 apr_sockaddr_t *sa = NULL; | |
35 apr_sockaddr_t *sl = NULL; | |
36 apr_int32_t f; | |
37 | |
38 | |
39 UNREFERENCED(o); | |
40 GET_S_FAMILY(f, family); | |
41 #if APR_HAVE_IPV6 | |
42 if (hostname) { | |
43 /* XXX: This only works for real scope_id's | |
44 */ | |
45 if ((sp = strchr(J2S(hostname), '%'))) { | |
46 *sp++ = '\0'; | |
47 scope_id = atoi(sp); | |
48 } | |
49 } | |
50 #endif | |
51 TCN_THROW_IF_ERR(apr_sockaddr_info_get(&sa, | |
52 J2S(hostname), f, (apr_port_t)port, | |
53 (apr_int32_t)flags, p), sa); | |
54 sl = sa; | |
55 /* | |
56 * apr_sockaddr_info_get may return several address so this is not | |
57 * go to work in some cases (but as least it works for Linux) | |
58 * XXX: with AP_ENABLE_V4_MAPPED it is going to work otherwise it won't. | |
59 */ | |
60 #if APR_HAVE_IPV6 | |
61 if (hostname == NULL) { | |
62 /* Try all address using IPV6 one */ | |
63 while (sl) { | |
64 if (sl->family == APR_INET6) | |
65 break; /* Done */ | |
66 sl = sl->next; | |
67 } | |
68 /* If we don't find an IPv6 address, use the original one */ | |
69 if (sl == NULL) { | |
70 sl = sa; | |
71 } | |
72 } | |
73 if (sp) { | |
74 /* Set the provided scope id | |
75 * APR lack the api for setting this directly so lets presume | |
76 * the sin6_scope_id is present everywhere | |
77 */ | |
78 sl->sa.sin6.sin6_scope_id = scope_id; | |
79 } | |
80 #endif | |
81 | |
82 cleanup: | |
83 TCN_FREE_CSTRING(hostname); | |
84 return P2J(sl); | |
85 } | |
86 | |
87 TCN_IMPLEMENT_CALL(jstring, Address, getnameinfo)(TCN_STDARGS, | |
88 jlong sa, jint flags) | |
89 { | |
90 apr_sockaddr_t *s = J2P(sa, apr_sockaddr_t *); | |
91 char *hostname; | |
92 | |
93 UNREFERENCED(o); | |
94 if (apr_getnameinfo(&hostname, s, (apr_int32_t)flags) == APR_SUCCESS) | |
95 return AJP_TO_JSTRING(hostname); | |
96 else | |
97 return NULL; | |
98 } | |
99 | |
100 TCN_IMPLEMENT_CALL(jstring, Address, getip)(TCN_STDARGS, jlong sa) | |
101 { | |
102 apr_sockaddr_t *s = J2P(sa, apr_sockaddr_t *); | |
103 char *ipaddr; | |
104 | |
105 UNREFERENCED(o); | |
106 if (apr_sockaddr_ip_get(&ipaddr, s) == APR_SUCCESS) | |
107 return AJP_TO_JSTRING(ipaddr); | |
108 else | |
109 return NULL; | |
110 } | |
111 | |
112 TCN_IMPLEMENT_CALL(jlong, Address, get)(TCN_STDARGS, jint which, | |
113 jlong sock) | |
114 { | |
115 tcn_socket_t *s = J2P(sock, tcn_socket_t *); | |
116 apr_sockaddr_t *sa = NULL; | |
117 | |
118 UNREFERENCED(o); | |
119 TCN_THROW_IF_ERR(apr_socket_addr_get(&sa, | |
120 (apr_interface_e)which, s->sock), sa); | |
121 cleanup: | |
122 return P2J(sa); | |
123 } | |
124 | |
125 TCN_IMPLEMENT_CALL(jboolean, Address, equal)(TCN_STDARGS, | |
126 jlong a, jlong b) | |
127 { | |
128 apr_sockaddr_t *sa = J2P(a, apr_sockaddr_t *); | |
129 apr_sockaddr_t *sb = J2P(b, apr_sockaddr_t *); | |
130 | |
131 UNREFERENCED_STDARGS; | |
132 return apr_sockaddr_equal(sa, sb) ? JNI_TRUE : JNI_FALSE; | |
133 } | |
134 | |
135 TCN_IMPLEMENT_CALL(jint, Address, getservbyname)(TCN_STDARGS, | |
136 jlong sa, jstring servname) | |
137 { | |
138 apr_sockaddr_t *s = J2P(sa, apr_sockaddr_t *); | |
139 TCN_ALLOC_CSTRING(servname); | |
140 apr_status_t rv; | |
141 | |
142 UNREFERENCED(o); | |
143 rv = apr_getservbyname(s, J2S(servname)); | |
144 TCN_FREE_CSTRING(servname); | |
145 return (jint)rv; | |
146 } | |
OLD | NEW |