0
# Socket Capabilities
1
2
Runtime capability detection and platform support validation for socket features and protocol availability across different operating systems and environments.
3
4
## Core Imports
5
6
```java
7
import org.newsclub.net.unix.AFSocketCapability;
8
import org.newsclub.net.unix.AFUNIXSocketCapability;
9
import org.newsclub.net.unix.AFSocketCapabilityRequirement;
10
```
11
12
## Capabilities
13
14
### AFSocketCapability
15
16
Base enumeration for testing socket type and protocol support on the current platform.
17
18
```java { .api }
19
/**
20
* Enumeration of socket capabilities that can be tested
21
*/
22
public enum AFSocketCapability {
23
/** Peer credential support */
24
CAPABILITY_PEER_CREDENTIALS,
25
26
/** Ancillary message support */
27
CAPABILITY_ANCILLARY_MESSAGES,
28
29
/** File descriptor passing support */
30
CAPABILITY_FILE_DESCRIPTORS,
31
32
/** Linux abstract namespace support */
33
CAPABILITY_ABSTRACT_NAMESPACE,
34
35
/** Unix Domain Socket support */
36
CAPABILITY_UNIX_DOMAIN,
37
38
/** Unix datagram socket support */
39
CAPABILITY_UNIX_DATAGRAMS,
40
41
/** Native socket pair support */
42
CAPABILITY_NATIVE_SOCKETPAIR,
43
44
/** TIPC socket support (Linux) */
45
CAPABILITY_TIPC,
46
47
/** VSOCK socket support (VM communication) */
48
CAPABILITY_VSOCK,
49
50
/** AF_SYSTEM socket support (macOS) */
51
CAPABILITY_SYSTEM;
52
53
/**
54
* Tests if this capability is supported on the current platform
55
* @return true if capability is supported
56
*/
57
public boolean isSupported();
58
59
/**
60
* Gets a human-readable description of this capability
61
* @return capability description
62
*/
63
public String getDescription();
64
}
65
```
66
67
**Usage Examples:**
68
69
```java
70
import org.newsclub.net.unix.*;
71
72
// Test basic socket support
73
public class CapabilityTester {
74
public static void main(String[] args) {
75
System.out.println("Socket Capability Report:");
76
77
for (AFSocketCapability capability : AFSocketCapability.values()) {
78
boolean supported = capability.isSupported();
79
System.out.printf(" %-20s: %s - %s%n",
80
capability.name(),
81
supported ? "✓ SUPPORTED" : "✗ NOT SUPPORTED",
82
capability.getDescription());
83
}
84
85
// Make decisions based on capabilities
86
if (AFSocketCapability.CAPABILITY_UNIX_DOMAIN.isSupported()) {
87
System.out.println("\nUnix Domain Sockets are available - proceeding with UDS implementation");
88
initializeUnixSocketService();
89
} else {
90
System.out.println("\nUnix Domain Sockets not available - falling back to TCP");
91
initializeTcpService();
92
}
93
94
// Test advanced features
95
if (AFSocketCapability.CAPABILITY_PEER_CREDENTIALS.isSupported()) {
96
System.out.println("Peer credentials available - enabling authentication");
97
}
98
99
if (AFSocketCapability.CAPABILITY_FILE_DESCRIPTORS.isSupported()) {
100
System.out.println("File descriptor passing available - enabling advanced IPC");
101
}
102
103
if (AFSocketCapability.CAPABILITY_ABSTRACT_NAMESPACE.isSupported()) {
104
System.out.println("Abstract namespace available - Linux features enabled");
105
}
106
107
if (AFSocketCapability.CAPABILITY_TIPC.isSupported()) {
108
System.out.println("TIPC support available for cluster communication");
109
}
110
111
if (AFSocketCapability.CAPABILITY_VSOCK.isSupported()) {
112
System.out.println("VSOCK support available for VM communication");
113
}
114
}
115
116
private static void initializeUnixSocketService() {
117
// Unix socket service initialization
118
}
119
120
private static void initializeTcpService() {
121
// TCP fallback service initialization
122
}
123
}
124
```
125
126
### AFUNIXSocketCapability
127
128
Unix Domain Socket-specific capabilities for platform-specific features and advanced functionality.
129
130
```java { .api }
131
/**
132
* Unix Domain Socket specific capabilities
133
*/
134
public enum AFUNIXSocketCapability {
135
/** Peer credentials support (PID, UID, GID) */
136
CAPABILITY_PEER_CREDENTIALS,
137
138
/** File descriptor passing support */
139
CAPABILITY_FILE_DESCRIPTORS,
140
141
/** Abstract namespace support (Linux) */
142
CAPABILITY_ABSTRACT_NAMESPACE,
143
144
/** Ancillary message support */
145
CAPABILITY_ANCILLARY_MESSAGES,
146
147
/** Socket pair creation support */
148
CAPABILITY_SOCKET_PAIRS,
149
150
/** Large buffer support for high-throughput operations */
151
CAPABILITY_LARGE_BUFFERS,
152
153
/** Non-blocking I/O support */
154
CAPABILITY_NON_BLOCKING_IO,
155
156
/** Address reuse support */
157
CAPABILITY_ADDRESS_REUSE;
158
159
/**
160
* Tests if this Unix socket capability is supported
161
* @return true if capability is supported
162
*/
163
public boolean isSupported();
164
165
/**
166
* Gets the minimum version requirement for this capability
167
* @return version string or null if no specific requirement
168
*/
169
public String getMinimumVersion();
170
171
/**
172
* Gets platform-specific notes for this capability
173
* @return platform notes or empty string
174
*/
175
public String getPlatformNotes();
176
}
177
```
178
179
**Usage Examples:**
180
181
```java
182
// Platform-specific feature detection
183
public class UnixSocketFeatures {
184
185
public static void demonstrateCapabilities() {
186
System.out.println("Unix Domain Socket Feature Report:");
187
188
// Check peer credentials support
189
if (AFUNIXSocketCapability.CAPABILITY_PEER_CREDENTIALS.isSupported()) {
190
System.out.println("✓ Peer credentials available - can authenticate connections");
191
demonstratePeerCredentials();
192
} else {
193
System.out.println("✗ Peer credentials not available - using alternative auth");
194
}
195
196
// Check abstract namespace (Linux-specific)
197
if (AFUNIXSocketCapability.CAPABILITY_ABSTRACT_NAMESPACE.isSupported()) {
198
System.out.println("✓ Abstract namespace available - using abstract sockets");
199
demonstrateAbstractSockets();
200
} else {
201
System.out.println("✗ Abstract namespace not available - using filesystem sockets");
202
}
203
204
// Check file descriptor passing
205
if (AFUNIXSocketCapability.CAPABILITY_FILE_DESCRIPTORS.isSupported()) {
206
System.out.println("✓ File descriptor passing available");
207
demonstrateFileDescriptorPassing();
208
}
209
210
// Check large buffer support for high-performance scenarios
211
if (AFUNIXSocketCapability.CAPABILITY_LARGE_BUFFERS.isSupported()) {
212
System.out.println("✓ Large buffer support - optimizing for high throughput");
213
configureHighThroughput();
214
}
215
}
216
217
private static void demonstratePeerCredentials() {
218
try {
219
// Implementation would use peer credentials
220
System.out.println(" Peer credential authentication enabled");
221
} catch (Exception e) {
222
System.err.println(" Failed to configure peer credentials: " + e.getMessage());
223
}
224
}
225
226
private static void demonstrateAbstractSockets() {
227
try {
228
AFUNIXSocketAddress abstractAddr = AFUNIXSocketAddress.inAbstractNamespace("test-service");
229
System.out.println(" Using abstract socket: " + abstractAddr.getPathname());
230
} catch (Exception e) {
231
System.err.println(" Failed to create abstract socket: " + e.getMessage());
232
}
233
}
234
235
private static void demonstrateFileDescriptorPassing() {
236
System.out.println(" File descriptor passing configured");
237
// Implementation would set up FD passing
238
}
239
240
private static void configureHighThroughput() {
241
System.out.println(" Configuring large buffers for high throughput");
242
// Implementation would configure buffer sizes
243
}
244
}
245
```
246
247
### AFSocketCapabilityRequirement
248
249
Capability requirement specification for validating platform support before socket operations.
250
251
```java { .api }
252
/**
253
* Defines capability requirements for socket operations
254
*/
255
public class AFSocketCapabilityRequirement {
256
257
/**
258
* Creates a requirement for a single capability
259
* @param capability The required capability
260
* @return AFSocketCapabilityRequirement instance
261
*/
262
public static AFSocketCapabilityRequirement require(AFSocketCapability capability);
263
264
/**
265
* Creates a requirement for a Unix-specific capability
266
* @param capability The required Unix capability
267
* @return AFSocketCapabilityRequirement instance
268
*/
269
public static AFSocketCapabilityRequirement require(AFUNIXSocketCapability capability);
270
271
/**
272
* Creates a requirement for multiple capabilities (all must be supported)
273
* @param capabilities Required capabilities
274
* @return AFSocketCapabilityRequirement instance
275
*/
276
public static AFSocketCapabilityRequirement requireAll(AFSocketCapability... capabilities);
277
278
/**
279
* Creates a requirement where at least one capability must be supported
280
* @param capabilities Alternative capabilities
281
* @return AFSocketCapabilityRequirement instance
282
*/
283
public static AFSocketCapabilityRequirement requireAny(AFSocketCapability... capabilities);
284
285
/**
286
* Tests if the requirements are satisfied on current platform
287
* @return true if all requirements are met
288
*/
289
public boolean isSatisfied();
290
291
/**
292
* Gets detailed information about unsatisfied requirements
293
* @return list of unsatisfied requirements
294
*/
295
public List<String> getUnsatisfiedRequirements();
296
297
/**
298
* Throws exception if requirements are not satisfied
299
* @throws OperationNotSupportedSocketException if requirements not met
300
*/
301
public void validateOrThrow() throws OperationNotSupportedSocketException;
302
}
303
```
304
305
**Usage Examples:**
306
307
```java
308
// Requirement validation for robust applications
309
public class RequirementBasedSocketService {
310
311
public void initializeService() throws OperationNotSupportedSocketException {
312
// Define service requirements
313
AFSocketCapabilityRequirement requirements = AFSocketCapabilityRequirement.requireAll(
314
AFSocketCapability.CAPABILITY_UNIX_DOMAIN,
315
AFSocketCapability.CAPABILITY_SERVER_SOCKETS,
316
AFSocketCapability.CAPABILITY_NIO_CHANNELS
317
);
318
319
// Validate requirements before proceeding
320
if (!requirements.isSatisfied()) {
321
List<String> unsatisfied = requirements.getUnsatisfiedRequirements();
322
System.err.println("Service requirements not met:");
323
for (String requirement : unsatisfied) {
324
System.err.println(" - " + requirement);
325
}
326
throw new OperationNotSupportedSocketException("Platform does not meet service requirements");
327
}
328
329
System.out.println("All requirements satisfied - initializing service");
330
startSocketService();
331
}
332
333
public void initializeAuthenticationService() {
334
// Authentication service with peer credentials or alternative
335
AFSocketCapabilityRequirement authRequirement = AFSocketCapabilityRequirement.requireAny(
336
AFUNIXSocketCapability.CAPABILITY_PEER_CREDENTIALS,
337
AFUNIXSocketCapability.CAPABILITY_FILE_DESCRIPTORS
338
);
339
340
if (authRequirement.isSatisfied()) {
341
if (AFUNIXSocketCapability.CAPABILITY_PEER_CREDENTIALS.isSupported()) {
342
initializePeerCredentialAuth();
343
} else {
344
initializeFileDescriptorAuth();
345
}
346
} else {
347
initializeTokenBasedAuth();
348
}
349
}
350
351
public void validateCapabilitiesWithFeedback() {
352
// Comprehensive capability validation with user feedback
353
System.out.println("Validating platform capabilities...");
354
355
try {
356
AFSocketCapabilityRequirement.require(AFSocketCapability.CAPABILITY_UNIX_DOMAIN)
357
.validateOrThrow();
358
System.out.println("✓ Unix Domain Sockets supported");
359
} catch (OperationNotSupportedSocketException e) {
360
System.err.println("✗ Unix Domain Sockets not supported: " + e.getMessage());
361
return;
362
}
363
364
// Check optional features
365
AFSocketCapabilityRequirement optionalFeatures = AFSocketCapabilityRequirement.requireAny(
366
AFUNIXSocketCapability.CAPABILITY_PEER_CREDENTIALS,
367
AFUNIXSocketCapability.CAPABILITY_ABSTRACT_NAMESPACE,
368
AFUNIXSocketCapability.CAPABILITY_FILE_DESCRIPTORS
369
);
370
371
if (optionalFeatures.isSatisfied()) {
372
System.out.println("✓ Advanced Unix socket features available");
373
} else {
374
System.out.println("! No advanced features available - using basic functionality");
375
}
376
}
377
378
private void startSocketService() {
379
System.out.println("Socket service started");
380
}
381
382
private void initializePeerCredentialAuth() {
383
System.out.println("Initializing peer credential authentication");
384
}
385
386
private void initializeFileDescriptorAuth() {
387
System.out.println("Initializing file descriptor authentication");
388
}
389
390
private void initializeTokenBasedAuth() {
391
System.out.println("Initializing token-based authentication");
392
}
393
}
394
```
395
396
## Capability Detection Patterns
397
398
### Runtime Platform Detection
399
400
```java
401
// Comprehensive platform capability detection
402
public class PlatformCapabilities {
403
404
public static class CapabilityReport {
405
private final Map<AFSocketCapability, Boolean> socketCapabilities;
406
private final Map<AFUNIXSocketCapability, Boolean> unixCapabilities;
407
private final String platformInfo;
408
409
public CapabilityReport() {
410
this.socketCapabilities = detectSocketCapabilities();
411
this.unixCapabilities = detectUnixCapabilities();
412
this.platformInfo = detectPlatformInfo();
413
}
414
415
public boolean hasCapability(AFSocketCapability capability) {
416
return socketCapabilities.getOrDefault(capability, false);
417
}
418
419
public boolean hasCapability(AFUNIXSocketCapability capability) {
420
return unixCapabilities.getOrDefault(capability, false);
421
}
422
423
public String getPlatformInfo() {
424
return platformInfo;
425
}
426
427
public void printReport() {
428
System.out.println("Platform Capability Report");
429
System.out.println("==========================");
430
System.out.println("Platform: " + platformInfo);
431
System.out.println();
432
433
System.out.println("Socket Capabilities:");
434
for (Map.Entry<AFSocketCapability, Boolean> entry : socketCapabilities.entrySet()) {
435
System.out.printf(" %-25s: %s%n",
436
entry.getKey().name(),
437
entry.getValue() ? "✓" : "✗");
438
}
439
440
System.out.println();
441
System.out.println("Unix Socket Capabilities:");
442
for (Map.Entry<AFUNIXSocketCapability, Boolean> entry : unixCapabilities.entrySet()) {
443
System.out.printf(" %-30s: %s%n",
444
entry.getKey().name(),
445
entry.getValue() ? "✓" : "✗");
446
447
if (entry.getValue()) {
448
String notes = entry.getKey().getPlatformNotes();
449
if (!notes.isEmpty()) {
450
System.out.println(" " + notes);
451
}
452
}
453
}
454
}
455
456
private Map<AFSocketCapability, Boolean> detectSocketCapabilities() {
457
Map<AFSocketCapability, Boolean> capabilities = new HashMap<>();
458
for (AFSocketCapability capability : AFSocketCapability.values()) {
459
capabilities.put(capability, capability.isSupported());
460
}
461
return capabilities;
462
}
463
464
private Map<AFUNIXSocketCapability, Boolean> detectUnixCapabilities() {
465
Map<AFUNIXSocketCapability, Boolean> capabilities = new HashMap<>();
466
for (AFUNIXSocketCapability capability : AFUNIXSocketCapability.values()) {
467
capabilities.put(capability, capability.isSupported());
468
}
469
return capabilities;
470
}
471
472
private String detectPlatformInfo() {
473
return System.getProperty("os.name") + " " +
474
System.getProperty("os.version") + " " +
475
System.getProperty("os.arch");
476
}
477
}
478
479
public static void main(String[] args) {
480
CapabilityReport report = new CapabilityReport();
481
report.printReport();
482
483
// Example usage based on capabilities
484
if (report.hasCapability(AFSocketCapability.CAPABILITY_UNIX_DOMAIN)) {
485
System.out.println("\nUnix Domain Sockets available - configuring UDS service");
486
487
if (report.hasCapability(AFUNIXSocketCapability.CAPABILITY_PEER_CREDENTIALS)) {
488
System.out.println("Enabling peer credential authentication");
489
}
490
491
if (report.hasCapability(AFUNIXSocketCapability.CAPABILITY_ABSTRACT_NAMESPACE)) {
492
System.out.println("Using abstract namespace for temporary sockets");
493
}
494
}
495
}
496
}
497
```
498
499
### Application Configuration Based on Capabilities
500
501
```java
502
// Application configuration that adapts to platform capabilities
503
public class AdaptiveSocketConfiguration {
504
private final CapabilityReport capabilities;
505
506
public AdaptiveSocketConfiguration() {
507
this.capabilities = new PlatformCapabilities.CapabilityReport();
508
}
509
510
public SocketConfiguration getOptimalConfiguration() {
511
SocketConfiguration config = new SocketConfiguration();
512
513
// Base configuration
514
if (capabilities.hasCapability(AFSocketCapability.CAPABILITY_UNIX_DOMAIN)) {
515
config.setSocketType(SocketType.UNIX_DOMAIN);
516
} else {
517
config.setSocketType(SocketType.TCP_FALLBACK);
518
}
519
520
// Authentication configuration
521
if (capabilities.hasCapability(AFUNIXSocketCapability.CAPABILITY_PEER_CREDENTIALS)) {
522
config.setAuthenticationMethod(AuthMethod.PEER_CREDENTIALS);
523
} else if (capabilities.hasCapability(AFUNIXSocketCapability.CAPABILITY_FILE_DESCRIPTORS)) {
524
config.setAuthenticationMethod(AuthMethod.FILE_DESCRIPTOR);
525
} else {
526
config.setAuthenticationMethod(AuthMethod.TOKEN_BASED);
527
}
528
529
// Address configuration
530
if (capabilities.hasCapability(AFUNIXSocketCapability.CAPABILITY_ABSTRACT_NAMESPACE)) {
531
config.setAddressType(AddressType.ABSTRACT);
532
} else {
533
config.setAddressType(AddressType.FILESYSTEM);
534
}
535
536
// Performance configuration
537
if (capabilities.hasCapability(AFUNIXSocketCapability.CAPABILITY_LARGE_BUFFERS)) {
538
config.setBufferSize(65536); // 64KB buffers
539
} else {
540
config.setBufferSize(8192); // 8KB buffers
541
}
542
543
return config;
544
}
545
546
public static class SocketConfiguration {
547
private SocketType socketType;
548
private AuthMethod authenticationMethod;
549
private AddressType addressType;
550
private int bufferSize;
551
552
// Getters and setters
553
public SocketType getSocketType() { return socketType; }
554
public void setSocketType(SocketType socketType) { this.socketType = socketType; }
555
556
public AuthMethod getAuthenticationMethod() { return authenticationMethod; }
557
public void setAuthenticationMethod(AuthMethod authenticationMethod) { this.authenticationMethod = authenticationMethod; }
558
559
public AddressType getAddressType() { return addressType; }
560
public void setAddressType(AddressType addressType) { this.addressType = addressType; }
561
562
public int getBufferSize() { return bufferSize; }
563
public void setBufferSize(int bufferSize) { this.bufferSize = bufferSize; }
564
565
@Override
566
public String toString() {
567
return String.format("SocketConfiguration{socketType=%s, auth=%s, address=%s, buffer=%d}",
568
socketType, authenticationMethod, addressType, bufferSize);
569
}
570
}
571
572
public enum SocketType { UNIX_DOMAIN, TCP_FALLBACK }
573
public enum AuthMethod { PEER_CREDENTIALS, FILE_DESCRIPTOR, TOKEN_BASED }
574
public enum AddressType { ABSTRACT, FILESYSTEM }
575
}
576
```