1 |
774 |
jeremybenn |
/* cpnet.h -
|
2 |
|
|
Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
#ifndef _CLASSPATH_NET_H_INCLUDED
|
39 |
|
|
#define _CLASSPATH_NET_H_INCLUDED
|
40 |
|
|
|
41 |
|
|
#include <jni.h>
|
42 |
|
|
#include <jcl.h>
|
43 |
|
|
#include <string.h>
|
44 |
|
|
|
45 |
|
|
#include <sys/types.h>
|
46 |
|
|
#include <sys/socket.h>
|
47 |
|
|
#ifdef HAVE_NETINET_IN_SYSTM_H
|
48 |
|
|
#include <netinet/in_systm.h>
|
49 |
|
|
#endif /* HAVE_NETINET_IN_SYSTM_H */
|
50 |
|
|
#include <netinet/in.h>
|
51 |
|
|
#ifdef HAVE_NETINET_IP_H
|
52 |
|
|
#include <netinet/ip.h>
|
53 |
|
|
#endif /* HAVE_NETINET_IP_H */
|
54 |
|
|
|
55 |
|
|
typedef struct {
|
56 |
|
|
jint len;
|
57 |
|
|
char data[1];
|
58 |
|
|
} cpnet_address;
|
59 |
|
|
|
60 |
|
|
#define CPNET_SHUTDOWN_READ 1
|
61 |
|
|
#define CPNET_SHUTDOWN_WRITE 2
|
62 |
|
|
|
63 |
|
|
JNIEXPORT jint cpnet_openSocketStream(JNIEnv *env, jint *fd, jint family);
|
64 |
|
|
JNIEXPORT jint cpnet_openSocketDatagram(JNIEnv *env, jint *fd, jint family);
|
65 |
|
|
JNIEXPORT jint cpnet_shutdown (JNIEnv *env, jint fd, jbyte flag);
|
66 |
|
|
JNIEXPORT jint cpnet_close(JNIEnv *env, jint fd);
|
67 |
|
|
JNIEXPORT jint cpnet_listen(JNIEnv *env, jint fd, jint queuelen);
|
68 |
|
|
JNIEXPORT jint cpnet_accept(JNIEnv *env, jint fd, jint *newfd);
|
69 |
|
|
JNIEXPORT jint cpnet_bind(JNIEnv *env, jint fd, cpnet_address *addr);
|
70 |
|
|
JNIEXPORT jint cpnet_connect(JNIEnv *env, jint fd, cpnet_address *addr);
|
71 |
|
|
JNIEXPORT jint cpnet_getLocalAddr(JNIEnv *env, jint fd, cpnet_address **addr);
|
72 |
|
|
JNIEXPORT jint cpnet_getRemoteAddr(JNIEnv *env, jint fd, cpnet_address **addr);
|
73 |
|
|
JNIEXPORT jint cpnet_setBroadcast(JNIEnv *env, jint fd, jint flag);
|
74 |
|
|
JNIEXPORT jint cpnet_send (JNIEnv *env, jint fd, jbyte *data, jint len, jint *bytes_sent);
|
75 |
|
|
JNIEXPORT jint cpnet_sendTo (JNIEnv *env, jint fd, jbyte *data, jint len, cpnet_address *addr, jint *bytes_sent);
|
76 |
|
|
JNIEXPORT jint cpnet_recv (JNIEnv *env, jint fd, jbyte *data, jint len, jint *bytes_recv);
|
77 |
|
|
JNIEXPORT jint cpnet_recvFrom (JNIEnv *env, jint fd, jbyte *data, jint len, cpnet_address **addr, jint *bytes_recv);
|
78 |
|
|
JNIEXPORT jint cpnet_setSocketTCPNoDelay (JNIEnv *env, jint fd, jint nodelay);
|
79 |
|
|
JNIEXPORT jint cpnet_getSocketTCPNoDelay (JNIEnv *env, jint fd, jint *nodelay);
|
80 |
|
|
JNIEXPORT jint cpnet_setLinger (JNIEnv *env, jint fd, jint flag, jint value);
|
81 |
|
|
JNIEXPORT jint cpnet_getLinger (JNIEnv *env, jint fd, jint *flag, jint *value);
|
82 |
|
|
JNIEXPORT jint cpnet_setSocketTimeout (JNIEnv *env, jint fd, jint value);
|
83 |
|
|
JNIEXPORT jint cpnet_getSocketTimeout (JNIEnv *env, jint fd, jint *value);
|
84 |
|
|
JNIEXPORT jint cpnet_setSendBuf (JNIEnv *env, jint fd, jint value);
|
85 |
|
|
JNIEXPORT jint cpnet_getSendBuf (JNIEnv *env, jint fd, jint *value);
|
86 |
|
|
JNIEXPORT jint cpnet_setRecvBuf (JNIEnv *env, jint fd, jint value);
|
87 |
|
|
JNIEXPORT jint cpnet_getRecvBuf (JNIEnv *env, jint fd, jint *value);
|
88 |
|
|
JNIEXPORT jint cpnet_setTTL (JNIEnv *env, jint fd, jint value);
|
89 |
|
|
JNIEXPORT jint cpnet_getTTL (JNIEnv *env, jint fd, jint *value);
|
90 |
|
|
JNIEXPORT jint cpnet_setMulticastIF (JNIEnv *env, jint fd, cpnet_address *addr);
|
91 |
|
|
JNIEXPORT jint cpnet_getMulticastIF (JNIEnv *env, jint fd, cpnet_address **addr);
|
92 |
|
|
JNIEXPORT jint cpnet_setReuseAddress (JNIEnv *env, jint fd, jint reuse);
|
93 |
|
|
JNIEXPORT jint cpnet_getReuseAddress (JNIEnv *env, jint fd, jint *reuse);
|
94 |
|
|
JNIEXPORT jint cpnet_setKeepAlive (JNIEnv *env, jint fd, jint keep);
|
95 |
|
|
JNIEXPORT jint cpnet_getKeepAlive (JNIEnv *env, jint fd, jint *keep);
|
96 |
|
|
JNIEXPORT jint cpnet_getBindAddress (JNIEnv *env, jint fd, cpnet_address **addr);
|
97 |
|
|
JNIEXPORT jint cpnet_addMembership (JNIEnv *env, jint fd, cpnet_address *addr);
|
98 |
|
|
JNIEXPORT jint cpnet_dropMembership (JNIEnv *env, jint fd, cpnet_address *addr);
|
99 |
|
|
JNIEXPORT jint cpnet_getAvailableBytes (JNIEnv *env, jint fd, jint *availableBytes);
|
100 |
|
|
JNIEXPORT jint cpnet_getHostname (JNIEnv *env, char *hostname, jint hostname_len);
|
101 |
|
|
JNIEXPORT jint cpnet_getHostByName (JNIEnv *env, const char *hostname, cpnet_address ***adresses, jint *addresses_count);
|
102 |
|
|
JNIEXPORT jint cpnet_getHostByAddr (JNIEnv *env, cpnet_address *addr, char *hostname, jint hostname_len);
|
103 |
|
|
JNIEXPORT jint cpnet_aton (JNIEnv *env, const char *hostname, cpnet_address **addr);
|
104 |
|
|
JNIEXPORT void cpnet_freeAddresses(JNIEnv * env, cpnet_address **addr, jint addresses_count);
|
105 |
|
|
|
106 |
|
|
static inline cpnet_address *cpnet_newIPV4Address(JNIEnv * env)
|
107 |
|
|
{
|
108 |
|
|
cpnet_address *addr = (cpnet_address *)JCL_malloc(env, sizeof(cpnet_address) + sizeof(struct sockaddr_in));
|
109 |
|
|
struct sockaddr_in *netaddr = (struct sockaddr_in *)&(addr->data[0]);
|
110 |
|
|
|
111 |
|
|
addr->len = sizeof(struct sockaddr_in);
|
112 |
|
|
memset(netaddr, 0, addr->len);
|
113 |
|
|
netaddr->sin_family = AF_INET;
|
114 |
|
|
return addr;
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
static inline void cpnet_setIPV4Any(cpnet_address *addr)
|
118 |
|
|
{
|
119 |
|
|
struct sockaddr_in *netaddr = (struct sockaddr_in *)&(addr->data[0]);
|
120 |
|
|
|
121 |
|
|
netaddr->sin_addr.s_addr = INADDR_ANY;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
#ifdef HAVE_INET6
|
125 |
|
|
static inline cpnet_address *cpnet_newIPV6Address(JNIEnv * env)
|
126 |
|
|
{
|
127 |
|
|
cpnet_address * addr = (cpnet_address *)JCL_malloc(env, sizeof(cpnet_address) + sizeof(struct sockaddr_in6));
|
128 |
|
|
struct sockaddr_in6 *netaddr = (struct sockaddr_in6 *)&(addr->data[0]);
|
129 |
|
|
|
130 |
|
|
addr->len = sizeof(struct sockaddr_in6);
|
131 |
|
|
memset(netaddr, 0, addr->len);
|
132 |
|
|
netaddr->sin6_family = AF_INET6;
|
133 |
|
|
|
134 |
|
|
return addr;
|
135 |
|
|
}
|
136 |
|
|
#endif
|
137 |
|
|
|
138 |
|
|
static inline void cpnet_freeAddress(JNIEnv * env, cpnet_address *addr)
|
139 |
|
|
{
|
140 |
|
|
JCL_free(env, addr);
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
static inline void cpnet_addressSetPort(cpnet_address *addr, jint port)
|
144 |
|
|
{
|
145 |
|
|
struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
|
146 |
|
|
|
147 |
|
|
ipaddr->sin_port = htons(port);
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
static inline jint cpnet_addressGetPort(cpnet_address *addr)
|
151 |
|
|
{
|
152 |
|
|
struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
|
153 |
|
|
|
154 |
|
|
return ntohs(ipaddr->sin_port);
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
static inline jboolean cpnet_isAddressEqual(cpnet_address *addr1, cpnet_address *addr2)
|
158 |
|
|
{
|
159 |
|
|
if (addr1->len != addr2->len)
|
160 |
|
|
return JNI_FALSE;
|
161 |
|
|
|
162 |
|
|
return memcmp(addr1->data, addr2->data, addr1->len) == 0;
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
#ifdef HAVE_INET6
|
166 |
|
|
static inline jboolean cpnet_isIPV6Address(cpnet_address *addr)
|
167 |
|
|
{
|
168 |
|
|
struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
|
169 |
|
|
|
170 |
|
|
return ipaddr->sin_family == AF_INET6;
|
171 |
|
|
}
|
172 |
|
|
#endif
|
173 |
|
|
|
174 |
|
|
static inline jboolean cpnet_isIPV4Address(cpnet_address *addr)
|
175 |
|
|
{
|
176 |
|
|
struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
|
177 |
|
|
|
178 |
|
|
return ipaddr->sin_family == AF_INET;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
static inline void cpnet_IPV4AddressToBytes(cpnet_address *netaddr, jbyte *octets)
|
182 |
|
|
{
|
183 |
|
|
struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(netaddr->data[0]);
|
184 |
|
|
unsigned long sysaddr = ntohl(ipaddr->sin_addr.s_addr);
|
185 |
|
|
|
186 |
|
|
octets[0] = ((sysaddr >> 24) & 0xff);
|
187 |
|
|
octets[1] = ((sysaddr >> 16) & 0xff);
|
188 |
|
|
octets[2] = ((sysaddr >> 8) & 0xff);
|
189 |
|
|
octets[3] = (sysaddr & 0xff);
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
static inline void cpnet_bytesToIPV4Address(cpnet_address *netaddr, jbyte *octets)
|
193 |
|
|
{
|
194 |
|
|
jint sysaddr;
|
195 |
|
|
struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(netaddr->data[0]);
|
196 |
|
|
|
197 |
|
|
sysaddr = ((jint)(unsigned char)octets[0]) << 24;
|
198 |
|
|
sysaddr |= ((jint)(unsigned char)octets[1]) << 16;
|
199 |
|
|
sysaddr |= ((jint)(unsigned char)octets[2]) << 8;
|
200 |
|
|
sysaddr |= ((jint)(unsigned char)octets[3]);
|
201 |
|
|
|
202 |
|
|
ipaddr->sin_addr.s_addr = htonl(sysaddr);
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
#ifdef HAVE_INET6
|
206 |
|
|
static inline void cpnet_IPV6AddressToBytes(cpnet_address *netaddr, jbyte *octets)
|
207 |
|
|
{
|
208 |
|
|
struct sockaddr_in6 *ipaddr = (struct sockaddr_in6 *)&(netaddr->data[0]);
|
209 |
|
|
|
210 |
|
|
memcpy(octets, &ipaddr->sin6_addr, 16);
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
static inline void cpnet_bytesToIPV6Address(cpnet_address *netaddr, jbyte *octets)
|
214 |
|
|
{
|
215 |
|
|
struct sockaddr_in6 *ipaddr = (struct sockaddr_in6 *)&(netaddr->data[0]);
|
216 |
|
|
|
217 |
|
|
memcpy(&ipaddr->sin6_addr, octets, 16);
|
218 |
|
|
}
|
219 |
|
|
#endif
|
220 |
|
|
|
221 |
|
|
#endif
|