Debian Codegraph @

src/main/java/jnr/netdb/NativeProtocolsDB.java

raw
1 /*
2 * Copyright (C) 2010 Wayne Meissner
3 *
4 * This file is part of jnr.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package jnr.netdb;
20
21 import jnr.ffi.*;
22 import jnr.ffi.Runtime;
23 import jnr.ffi.annotations.Direct;
24
25 import java.util.ArrayList;
26
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
35 import static jnr.ffi.Platform.OS.*;
36
37 /**
38 *
39 */
40 abstract class NativeProtocolsDB implements ProtocolsDB {
41
42 public static final NativeProtocolsDB getInstance() {
43 return SingletonHolder.INSTANCE;
44 }
45
46 private static final class SingletonHolder {
47 public static final NativeProtocolsDB INSTANCE = load();
48 }
49
50 private static final NativeProtocolsDB load() {
51 try {
52 Platform.OS os = Platform.getNativePlatform().getOS();
53
54 // The protoent struct is only known to match on Windows, MacOSX, Linux, Solaris.
55 // We assume FreeBSD and NetBSD also match.
56 if (!(os.equals(DARWIN) || (os.equals(WINDOWS) && Platform.getNativePlatform().getCPU() == Platform.CPU.I386)
57 || os.equals(LINUX) || os.equals(SOLARIS)
58 || os.equals(FREEBSD) || os.equals(NETBSD))) {
59 return null;
60 }
61
62 LibProto lib;
63 if (os.equals(WINDOWS)) {
64 Map<LibraryOption, Object> options = new HashMap<LibraryOption, Object>();
65 options.put(LibraryOption.CallingConvention, CallingConvention.STDCALL);
66 lib = Library.loadLibrary(LibProto.class, options, "Ws2_32");
67 } else {
68 String[] libnames = os.equals(SOLARIS)
69 ? new String[]{"socket", "nsl", "c"}
70 : new String[]{"c"};
71 lib = os.equals(LINUX)
72 ? Library.loadLibrary(LinuxLibProto.class, libnames)
73 : Library.loadLibrary(LibProto.class, libnames);
74 }
75
76 NativeProtocolsDB protocolsDB = os.equals(LINUX)
77 ? new LinuxNativeProtocolsDB((LinuxLibProto) lib)
78 : new DefaultNativeProtocolsDB(lib);
79 // Try to lookup a protocol to make sure the library loaded and found the functions
80 protocolsDB.getProtocolByName("ip");
81 protocolsDB.getProtocolByNumber(0);
82
83 return protocolsDB;
84 } catch (Throwable t) {
85 Logger.getLogger(NativeProtocolsDB.class.getName()).log(Level.WARNING, "Failed to load native protocols db", t);
86 return null;
87 }
88 }
89
90 public static class UnixProtoent extends jnr.ffi.Struct {
91 public final String name = new UTF8StringRef();
92 public final Pointer aliases = new Pointer();
93 public final Signed32 proto = new Signed32();
94
95 public UnixProtoent(jnr.ffi.Runtime runtime) {
96 super(runtime);
97 }
98 }
99
100 public static interface LibProto {
101 UnixProtoent getprotobyname(String name);
102 UnixProtoent getprotobynumber(int proto);
103 UnixProtoent getprotoent();
104 void setprotoent(int stayopen);
105 void endprotoent();
106 }
107
108 public static interface LinuxLibProto extends LibProto{
109 int getprotobyname_r(String proto, @Direct UnixProtoent protoent, Pointer buf, NativeLong buflen, Pointer result);
110 int getprotobynumber_r(int proto, @Direct UnixProtoent protoent, Pointer buf, NativeLong buflen, Pointer result);
111 int getprotoent_r(@Direct UnixProtoent protoent, Pointer buf, NativeLong buflen, Pointer result);
112 }
113
114 static Protocol protocolFromNative(UnixProtoent p) {
115 if (p == null) {
116 return null;
117 }
118
119 List<String> emptyAliases = Collections.emptyList();
120
121 Pointer ptr;
122 final Collection<String> aliases = ((ptr = p.aliases.get()) != null)
123 ? StringUtil.getNullTerminatedStringArray(ptr) : emptyAliases;
124
125 return new Protocol(p.name.get(), (short) p.proto.get(), aliases);
126 }
127
128 static final class DefaultNativeProtocolsDB extends NativeProtocolsDB {
129 private final LibProto lib;
130
131 DefaultNativeProtocolsDB(LibProto lib) {
132 this.lib = lib;
133 }
134
135 public synchronized Protocol getProtocolByName(String name) {
136 return protocolFromNative(lib.getprotobyname(name));
137 }
138
139 public synchronized Protocol getProtocolByNumber(Integer proto) {
140 return protocolFromNative(lib.getprotobynumber(proto));
141 }
142
143 public synchronized Collection<Protocol> getAllProtocols() {
144 UnixProtoent p;
145 List<Protocol> allProtocols = new ArrayList<Protocol>();
146
147 lib.setprotoent(0);
148 try {
149 while ((p = lib.getprotoent()) != null) {
150 allProtocols.add(protocolFromNative(p));
151 }
152 } finally {
153 lib.endprotoent();
154 }
155
156 return allProtocols;
157 }
158 }
159
160 static final class LinuxNativeProtocolsDB extends NativeProtocolsDB {
161 private static final int BUFLEN = 4096;
162 private final Runtime runtime;
163 private final Pointer buf;
164 private final LinuxLibProto lib;
165
166
167 LinuxNativeProtocolsDB(LinuxLibProto lib) {
168 this.lib = lib;
169 this.runtime = Library.getRuntime(lib);
170 this.buf = Memory.allocateDirect(runtime, BUFLEN);
171 }
172
173 public synchronized Protocol getProtocolByName(String name) {
174 UnixProtoent protoent = new UnixProtoent(runtime);
175 Pointer result = Memory.allocateDirect(runtime, runtime.addressSize());
176 if (lib.getprotobyname_r(name, protoent, buf, new NativeLong(BUFLEN), result) == 0) {
177 return result.getPointer(0) != null ? protocolFromNative(protoent) : null;
178 }
179
180 throw new RuntimeException("getprotobyname_r failed");
181 }
182
183 public synchronized Protocol getProtocolByNumber(Integer number) {
184 UnixProtoent protoent = new UnixProtoent(runtime);
185 Pointer result = Memory.allocateDirect(runtime, runtime.addressSize());
186 if (lib.getprotobynumber_r(number, protoent, buf, new NativeLong(BUFLEN), result) == 0) {
187 return result.getPointer(0) != null ? protocolFromNative(protoent) : null;
188 }
189
190 throw new RuntimeException("getprotobynumber_r failed");
191 }
192
193 public synchronized Collection<Protocol> getAllProtocols() {
194 UnixProtoent p = new UnixProtoent(runtime);
195 List<Protocol> allProtocols = new ArrayList<Protocol>();
196 Pointer result = Memory.allocateDirect(runtime, runtime.addressSize());
197 NativeLong buflen = new NativeLong(BUFLEN);
198
199 lib.setprotoent(0);
200 try {
201 while (lib.getprotoent_r(p, buf, buflen, result) == 0 && result.getPointer(0) != null) {
202 allProtocols.add(protocolFromNative(p));
203 }
204 } finally {
205 lib.endprotoent();
206 }
207
208 return allProtocols;
209 }
210 }
211 }