or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration-management.mddata-structure-utilities.mdframework-integration.mdindex.mdio-file-operations.mdnetwork-operations.mdpath-template-matching.mdsecurity-cryptography.mdstring-processing.mdtime-date-operations.md

network-operations.mddocs/

0

# Network Operations

1

2

Network utilities for IP address handling, port management, and network validation. These utilities provide comprehensive networking support for microservices, including local address detection, hostname resolution, and URL formatting for both IPv4 and IPv6 environments.

3

4

## Capabilities

5

6

### Network Utilities

7

8

Comprehensive network operations for IP address handling, validation, and network discovery.

9

10

```java { .api }

11

/**

12

* Network utilities for IP address handling and validation

13

*/

14

public class NetUtils {

15

public static final String LOCALHOST = "127.0.0.1";

16

public static final String ANYHOST = "0.0.0.0";

17

18

// Host validation

19

public static boolean isInvalidLocalHost(String host);

20

public static boolean isValidLocalHost(String host);

21

public static boolean isValidAddress(String address);

22

public static boolean isValidAddress(InetAddress address);

23

24

// Local address discovery

25

public static InetAddress getLocalAddress();

26

public static InetAddress getLocalAddress(Map<String, Integer> destHostPorts);

27

public static String getLocalAddressByDatagram();

28

29

// Network operations

30

public static String getHostName(SocketAddress socketAddress);

31

public static int getAvailablePort();

32

33

// URL formatting

34

public static String hostAndPortToUrlString(String host, int port);

35

public static String ipAddressAndPortToUrlString(InetAddress address, int port);

36

public static String ipAddressToUrlString(InetAddress address);

37

38

// DNS resolution

39

public static String resolveHost2Address(String fqdn);

40

public static URI resolveUriHost2Address(URI uri);

41

}

42

```

43

44

**Usage Examples:**

45

46

```java

47

import com.networknt.utility.NetUtils;

48

import java.net.InetAddress;

49

import java.net.URI;

50

51

// Local address discovery

52

InetAddress localAddr = NetUtils.getLocalAddress();

53

String localIP = NetUtils.getLocalAddressByDatagram();

54

55

// Host validation

56

boolean isValid = NetUtils.isValidLocalHost("192.168.1.100"); // true

57

boolean isInvalid = NetUtils.isInvalidLocalHost("0.0.0.0"); // true

58

59

// Port management

60

int availablePort = NetUtils.getAvailablePort(); // e.g., 8080

61

62

// URL formatting

63

String url = NetUtils.hostAndPortToUrlString("localhost", 8080); // "localhost:8080"

64

String ipUrl = NetUtils.ipAddressToUrlString(InetAddress.getByName("192.168.1.1")); // "192.168.1.1"

65

66

// IPv6 handling

67

InetAddress ipv6 = InetAddress.getByName("2001:db8::1");

68

String ipv6Url = NetUtils.ipAddressToUrlString(ipv6); // "[2001:db8::1]"

69

70

// DNS resolution

71

String resolved = NetUtils.resolveHost2Address("example.com"); // "93.184.216.34"

72

URI resolvedUri = NetUtils.resolveUriHost2Address(URI.create("http://example.com/path"));

73

74

// Advanced local address detection with destination mapping

75

Map<String, Integer> destinations = Map.of(

76

"api.service.com", 443,

77

"db.service.com", 5432

78

);

79

InetAddress bestLocal = NetUtils.getLocalAddress(destinations);

80

```

81

82

### Use Cases

83

84

**Microservice Discovery:**

85

```java

86

// Find best local interface for service registration

87

InetAddress serviceAddr = NetUtils.getLocalAddress();

88

int servicePort = NetUtils.getAvailablePort();

89

String serviceUrl = NetUtils.ipAddressAndPortToUrlString(serviceAddr, servicePort);

90

```

91

92

**Load Balancer Health Checks:**

93

```java

94

// Validate health check source addresses

95

boolean isValidSource = NetUtils.isValidAddress("10.0.1.50");

96

String sourceName = NetUtils.getHostName(socketAddress);

97

```

98

99

**Service Configuration:**

100

```java

101

// Resolve configuration hostnames to IP addresses

102

String dbHost = "database.internal.com";

103

String dbIp = NetUtils.resolveHost2Address(dbHost);

104

String connectionUrl = "jdbc:postgresql://" + dbIp + ":5432/mydb";

105

```

106

107

**IPv6 Support:**

108

```java

109

// Handle both IPv4 and IPv6 addresses in URL construction

110

InetAddress address = InetAddress.getByName(hostString);

111

String urlSafeAddress = NetUtils.ipAddressToUrlString(address);

112

// IPv4: "192.168.1.1" -> "192.168.1.1"

113

// IPv6: "2001:db8::1" -> "[2001:db8::1]"

114

```