OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / native / jni / native-lib / cpnet.h
1 /* 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/socket.h>
46 #include <netinet/in.h>
47 #include <netinet/ip.h>
48
49 typedef struct {
50   jint len;
51   char data[1];
52 } cpnet_address;
53
54 #define CPNET_SHUTDOWN_READ 1
55 #define CPNET_SHUTDOWN_WRITE 2
56
57 JNIEXPORT jint cpnet_openSocketStream(JNIEnv *env, jint *fd, jint family);
58 JNIEXPORT jint cpnet_openSocketDatagram(JNIEnv *env, jint *fd, jint family);
59 JNIEXPORT jint cpnet_shutdown (JNIEnv *env, jint fd, jbyte flag);
60 JNIEXPORT jint cpnet_close(JNIEnv *env, jint fd);
61 JNIEXPORT jint cpnet_listen(JNIEnv *env, jint fd, jint queuelen);
62 JNIEXPORT jint cpnet_accept(JNIEnv *env, jint fd, jint *newfd);
63 JNIEXPORT jint cpnet_bind(JNIEnv *env, jint fd, cpnet_address *addr);
64 JNIEXPORT jint cpnet_connect(JNIEnv *env, jint fd, cpnet_address *addr);
65 JNIEXPORT jint cpnet_getLocalAddr(JNIEnv *env, jint fd, cpnet_address **addr);
66 JNIEXPORT jint cpnet_getRemoteAddr(JNIEnv *env, jint fd, cpnet_address **addr);
67 JNIEXPORT jint cpnet_setBroadcast(JNIEnv *env, jint fd, jint flag);
68 JNIEXPORT jint cpnet_send (JNIEnv *env, jint fd, jbyte *data, jint len, jint *bytes_sent);
69 JNIEXPORT jint cpnet_sendTo (JNIEnv *env, jint fd, jbyte *data, jint len, cpnet_address *addr, jint *bytes_sent);
70 JNIEXPORT jint cpnet_recv (JNIEnv *env, jint fd, jbyte *data, jint len, jint *bytes_recv);
71 JNIEXPORT jint cpnet_recvFrom (JNIEnv *env, jint fd, jbyte *data, jint len, cpnet_address **addr, jint *bytes_recv);
72 JNIEXPORT jint cpnet_setSocketTCPNoDelay (JNIEnv *env, jint fd, jint nodelay);
73 JNIEXPORT jint cpnet_getSocketTCPNoDelay (JNIEnv *env, jint fd, jint *nodelay);
74 JNIEXPORT jint cpnet_setLinger (JNIEnv *env, jint fd, jint flag, jint value);
75 JNIEXPORT jint cpnet_getLinger (JNIEnv *env, jint fd, jint *flag, jint *value);
76 JNIEXPORT jint cpnet_setSocketTimeout (JNIEnv *env, jint fd, jint value);
77 JNIEXPORT jint cpnet_getSocketTimeout (JNIEnv *env, jint fd, jint *value);
78 JNIEXPORT jint cpnet_setSendBuf (JNIEnv *env, jint fd, jint value);
79 JNIEXPORT jint cpnet_getSendBuf (JNIEnv *env, jint fd, jint *value);
80 JNIEXPORT jint cpnet_setRecvBuf (JNIEnv *env, jint fd, jint value);
81 JNIEXPORT jint cpnet_getRecvBuf (JNIEnv *env, jint fd, jint *value);
82 JNIEXPORT jint cpnet_setTTL (JNIEnv *env, jint fd, jint value);
83 JNIEXPORT jint cpnet_getTTL (JNIEnv *env, jint fd, jint *value);
84 JNIEXPORT jint cpnet_setMulticastIF (JNIEnv *env, jint fd, cpnet_address *addr);
85 JNIEXPORT jint cpnet_getMulticastIF (JNIEnv *env, jint fd, cpnet_address **addr);
86 JNIEXPORT jint cpnet_setReuseAddress (JNIEnv *env, jint fd, jint reuse);
87 JNIEXPORT jint cpnet_getReuseAddress (JNIEnv *env, jint fd, jint *reuse);
88 JNIEXPORT jint cpnet_setKeepAlive (JNIEnv *env, jint fd, jint keep);
89 JNIEXPORT jint cpnet_getKeepAlive (JNIEnv *env, jint fd, jint *keep);
90 JNIEXPORT jint cpnet_getBindAddress (JNIEnv *env, jint fd, cpnet_address **addr);
91 JNIEXPORT jint cpnet_addMembership (JNIEnv *env, jint fd, cpnet_address *addr);
92 JNIEXPORT jint cpnet_dropMembership (JNIEnv *env, jint fd, cpnet_address *addr);
93 JNIEXPORT jint cpnet_getAvailableBytes (JNIEnv *env, jint fd, jint *availableBytes);
94 JNIEXPORT jint cpnet_getHostname (JNIEnv *env, char *hostname, jint hostname_len);
95 JNIEXPORT jint cpnet_getHostByName (JNIEnv *env, const char *hostname, cpnet_address ***adresses, jint *addresses_count);
96 JNIEXPORT jint cpnet_getHostByAddr (JNIEnv *env, cpnet_address *addr, char *hostname, jint hostname_len);
97 JNIEXPORT jint cpnet_aton (JNIEnv *env, const char *hostname, cpnet_address **addr);
98 JNIEXPORT void cpnet_freeAddresses(JNIEnv * env, cpnet_address **addr, jint addresses_count);
99
100 static inline cpnet_address *cpnet_newIPV4Address(JNIEnv * env)
101 {
102   cpnet_address *addr = (cpnet_address *)JCL_malloc(env, sizeof(cpnet_address) + sizeof(struct sockaddr_in));
103   struct sockaddr_in *netaddr = (struct sockaddr_in *)&(addr->data[0]);
104
105   addr->len = sizeof(struct sockaddr_in);
106   memset(netaddr, 0, addr->len);
107   netaddr->sin_family = AF_INET;
108   return addr;
109 }
110
111 static inline void cpnet_setIPV4Any(cpnet_address *addr)
112 {
113   struct sockaddr_in *netaddr = (struct sockaddr_in *)&(addr->data[0]);
114   
115   netaddr->sin_addr.s_addr = INADDR_ANY;
116 }
117
118 static inline cpnet_address *cpnet_newIPV6Address(JNIEnv * env)
119 {
120   cpnet_address * addr = (cpnet_address *)JCL_malloc(env, sizeof(cpnet_address) + sizeof(struct sockaddr_in6));
121   struct sockaddr_in6 *netaddr = (struct sockaddr_in6 *)&(addr->data[0]);
122
123   addr->len = sizeof(struct sockaddr_in6);
124   memset(netaddr, 0, addr->len);
125   netaddr->sin6_family = AF_INET6;
126
127   return addr;
128 }
129
130 static inline void cpnet_freeAddress(JNIEnv * env, cpnet_address *addr)
131 {
132   JCL_free(env, addr);
133 }
134
135 static inline void cpnet_addressSetPort(cpnet_address *addr, jint port)
136 {
137   struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
138
139   ipaddr->sin_port = htons(port);
140 }
141
142 static inline jint cpnet_addressGetPort(cpnet_address *addr)
143 {
144   struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
145
146   return ntohs(ipaddr->sin_port);
147 }
148
149 static inline jboolean cpnet_isAddressEqual(cpnet_address *addr1, cpnet_address *addr2)
150 {
151   if (addr1->len != addr2->len)
152     return JNI_FALSE;
153
154   return memcmp(addr1->data, addr2->data, addr1->len) == 0;
155 }
156
157 static inline jboolean cpnet_isIPV6Address(cpnet_address *addr)
158 {
159   struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
160
161   return ipaddr->sin_family == AF_INET6;
162 }
163
164 static inline jboolean cpnet_isIPV4Address(cpnet_address *addr)
165 {
166   struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
167
168   return ipaddr->sin_family == AF_INET;
169 }
170
171 static inline void cpnet_IPV4AddressToBytes(cpnet_address *netaddr, jbyte *octets)
172 {
173   struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(netaddr->data[0]);
174   unsigned long sysaddr = ntohl(ipaddr->sin_addr.s_addr);
175
176   octets[0] = ((sysaddr >> 24) & 0xff);
177   octets[1] = ((sysaddr >> 16) & 0xff);
178   octets[2] = ((sysaddr >> 8) & 0xff);
179   octets[3] = (sysaddr & 0xff);
180 }
181
182 static inline void cpnet_bytesToIPV4Address(cpnet_address *netaddr, jbyte *octets)
183 {
184   jint sysaddr;
185   struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(netaddr->data[0]);
186
187   sysaddr = ((jint)(unsigned char)octets[0]) << 24;
188   sysaddr |= ((jint)(unsigned char)octets[1]) << 16;
189   sysaddr |= ((jint)(unsigned char)octets[2]) << 8;
190   sysaddr |= ((jint)(unsigned char)octets[3]);
191
192   ipaddr->sin_addr.s_addr = htonl(sysaddr);
193 }
194
195 static inline void cpnet_IPV6AddressToBytes(cpnet_address *netaddr, jbyte *octets)
196 {
197   struct sockaddr_in6 *ipaddr = (struct sockaddr_in6 *)&(netaddr->data[0]);
198
199   memcpy(octets, &ipaddr->sin6_addr, 16);
200 }
201
202 static inline void cpnet_bytesToIPV6Address(cpnet_address *netaddr, jbyte *octets)
203 {
204   struct sockaddr_in6 *ipaddr = (struct sockaddr_in6 *)&(netaddr->data[0]);
205
206   memcpy(&ipaddr->sin6_addr, octets, 16);
207 }
208
209 #endif