Skip to content

Commit 5722422

Browse files
authored
Add logic to derive either domain name or the IP address from a request URL. (#25)
1 parent ed5d509 commit 5722422

File tree

5 files changed

+131
-56
lines changed

5 files changed

+131
-56
lines changed

app/src/main/java/com/fivegmag/a5gmscommonlibrary/consumptionReporting/ConsumptionReport.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ data class ConsumptionReportingUnit(
3939
var mimeType: String? = null,
4040
@JsonIgnore
4141
var finished: Boolean = false
42-
) : Parcelable
43-
42+
) : Parcelable

app/src/main/java/com/fivegmag/a5gmscommonlibrary/helpers/Constants.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ object ContentTypes {
5757
object UserAgentTokens {
5858
const val FIVE_G_MS_REL_17_MEDIA_STREAM_HANDLER = "5GMSMediaStreamHandler/17"
5959
const val FIVE_G_MS_REL_17_MEDIA_SESSION_HANDLER = "5GMSMediaSessionHandler/17"
60+
}
61+
62+
object HostInfoTypes {
63+
const val IP_V4 = "ipv4"
64+
const val IP_V6 = "ipv6"
65+
const val DOMAIN_NAME = "domain_name"
6066
}

app/src/main/java/com/fivegmag/a5gmscommonlibrary/helpers/Utils.kt

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.fivegmag.a5gmscommonlibrary.helpers
22

33
import com.fivegmag.a5gmscommonlibrary.models.EndpointAddress
4+
import com.fivegmag.a5gmscommonlibrary.models.HostInfo
45
import java.text.SimpleDateFormat
56
import java.time.Duration
67
import java.util.Date
78
import java.util.Random
89
import java.util.TimeZone
910
import okhttp3.Headers
10-
import java.net.Inet6Address
11-
import java.net.InetAddress
1211
import java.net.NetworkInterface
13-
import java.net.URI
1412
import java.net.URL
1513
import java.time.Instant
1614
import java.util.Locale
@@ -127,13 +125,30 @@ class Utils {
127125
return null
128126
}
129127

130-
fun getDomainName(url: String): String? {
131-
return try {
132-
val uri = URI(url)
133-
val domain = uri.host
134-
domain?.removePrefix("www.")
128+
fun getHostInfo(urlString: String): HostInfo? {
129+
try {
130+
val url = URL(urlString)
131+
val host = url.host
132+
133+
// Check if it's a valid IPv4 address
134+
val ipv4Regex = """^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$""".toRegex()
135+
if (ipv4Regex.matches(host)) {
136+
return HostInfo(HostInfoTypes.IP_V4, host)
137+
}
138+
139+
// Check if it's a valid IPv6 address
140+
val ipv6Regex = """^\[([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\]$""".toRegex()
141+
if (ipv6Regex.matches(host)) {
142+
return HostInfo(HostInfoTypes.IP_V6, host)
143+
}
144+
145+
// Remove leading "www." from the domain
146+
val domain = if (host.startsWith("www.")) host.substring(4) else host
147+
148+
// Assume it's a domain name
149+
return HostInfo(HostInfoTypes.DOMAIN_NAME, domain)
135150
} catch (e: Exception) {
136-
null
151+
return null
137152
}
138153
}
139154

@@ -151,42 +166,28 @@ class Utils {
151166
}
152167
}
153168

154-
/**
155-
* Network operations are not allowed on the main thread.
156-
* Perform the IP lookup in a different thread and then call a callback function
157-
*
158-
* @param requestUrl
159-
* @param callback
160-
*/
161169
fun getEndpointAddressByRequestUrl(
162-
requestUrl: String,
163-
callback: (EndpointAddress?) -> Unit
164-
) {
165-
Thread {
166-
try {
167-
val domainName = getDomainName(requestUrl)
168-
val port = getPort(requestUrl)
169-
var ipv4Addr: String? = null
170-
var ipv6Addr: String? = null
171-
val inetAddresses = InetAddress.getAllByName(domainName)
172-
for (inetAddress in inetAddresses) {
173-
val ipAddress = inetAddress.hostAddress
174-
if (inetAddress is Inet6Address) {
175-
ipv6Addr = ipAddress
176-
} else {
177-
ipv4Addr = ipAddress
178-
}
179-
}
180-
if (port != null) {
181-
callback(EndpointAddress(domainName, ipv4Addr, ipv6Addr, port))
170+
requestUrl: String
171+
): EndpointAddress? {
172+
try {
173+
val hostInfo = getHostInfo(requestUrl)
174+
val port = getPort(requestUrl)
175+
176+
if (hostInfo != null && port != null) {
177+
return when (hostInfo.type) {
178+
HostInfoTypes.IP_V4 -> EndpointAddress(null, hostInfo.host, null, port)
179+
HostInfoTypes.IP_V6 -> EndpointAddress(null, null, hostInfo.host, port)
180+
HostInfoTypes.DOMAIN_NAME -> EndpointAddress(hostInfo.host, null, null, port)
181+
else -> null
182182
}
183+
}
183184

184-
callback(null)
185+
return null
186+
187+
} catch (e: Exception) {
188+
return null
189+
}
185190

186-
} catch (e: Exception) {
187-
callback(null)
188-
}
189-
}.start()
190191
}
191192

192193
}

app/src/main/java/com/fivegmag/a5gmscommonlibrary/models/SharedDataTypes.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ enum class CellIdentifierType : Parcelable {
3131
CGI,
3232
ECGI,
3333
NCGI
34-
}
34+
}
35+
data class HostInfo(
36+
val type: String,
37+
val host: String
38+
)

app/src/test/java/com/fivegmag/a5gmscommonlibrary/UtilsUnitTest.kt

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fivegmag.a5gmscommonlibrary
22

3+
import com.fivegmag.a5gmscommonlibrary.helpers.HostInfoTypes
34
import com.fivegmag.a5gmscommonlibrary.helpers.Utils
45
import com.fivegmag.a5gmscommonlibrary.models.EndpointAddress
56
import kotlinx.coroutines.CompletableDeferred
@@ -28,27 +29,91 @@ class UtilsUnitTest {
2829
}
2930

3031
@Test
31-
fun getDomainName() {
32+
fun getHostInfoIp4() {
33+
val url = "http://192.168.1.1/akamai/bbb_30fps/bbb_30fps.mpd"
34+
val utils = Utils()
35+
val hostInfo = utils.getHostInfo(url)
36+
37+
assertNotNull(hostInfo)
38+
39+
if (hostInfo != null) {
40+
assertEquals("192.168.1.1", hostInfo.host)
41+
assertEquals(HostInfoTypes.IP_V4, hostInfo.type)
42+
}
43+
}
44+
45+
@Test
46+
fun getHostInfoIp6() {
47+
val url = "http://[1080:0:0:0:8:800:200C:417A]/akamai/bbb_30fps/bbb_30fps.mpd"
48+
val utils = Utils()
49+
val hostInfo = utils.getHostInfo(url)
50+
51+
assertNotNull(hostInfo)
52+
53+
if (hostInfo != null) {
54+
assertEquals("[1080:0:0:0:8:800:200C:417A]", hostInfo.host)
55+
assertEquals(HostInfoTypes.IP_V6, hostInfo.type)
56+
}
57+
}
58+
59+
@Test
60+
fun getHostInfoDomain() {
3261
val url = "http://dash.akamaized.net/akamai/bbb_30fps/bbb_30fps.mpd"
3362
val utils = Utils()
34-
val domainName = utils.getDomainName(url)
63+
val hostInfo = utils.getHostInfo(url)
3564

36-
assertEquals("dash.akamaized.net", domainName)
65+
assertNotNull(hostInfo)
66+
67+
if (hostInfo != null) {
68+
assertEquals("dash.akamaized.net", hostInfo.host)
69+
assertEquals(HostInfoTypes.DOMAIN_NAME, hostInfo.type)
70+
}
3771
}
72+
@Test
73+
fun getEndpointAddressByRequestUrlDomain() {
74+
val url = "http://dash.akamaized.net:8888/akamai/bbb_30fps/bbb_30fps.mpd"
75+
val utils = Utils()
76+
val endpointAddress = utils.getEndpointAddressByRequestUrl(url)
3877

78+
assertNotNull(endpointAddress)
79+
80+
if (endpointAddress != null) {
81+
assertEquals("dash.akamaized.net", endpointAddress.domainName)
82+
assertEquals(8888, endpointAddress.portNumber)
83+
assertNull(endpointAddress.ipv4Addr)
84+
assertNull(endpointAddress.ipv6Addr)
85+
}
86+
}
3987
@Test
40-
fun getEndpointAddressByRequestUrl() = runBlocking {
41-
val url = "https://livesim.dashif.org/livesim2/testpic_2s/V300/852746919.m4s"
88+
fun getEndpointAddressByRequestUrlIp4() {
89+
val url = "http://192.168.1.1:8888/akamai/bbb_30fps/bbb_30fps.mpd"
4290
val utils = Utils()
43-
val deferredEndpointAddress = CompletableDeferred<EndpointAddress?>()
44-
utils.getEndpointAddressByRequestUrl(url) {endpointAddress ->
45-
deferredEndpointAddress.complete(endpointAddress)
91+
val endpointAddress = utils.getEndpointAddressByRequestUrl(url)
92+
93+
assertNotNull(endpointAddress)
94+
95+
if (endpointAddress != null) {
96+
assertEquals("192.168.1.1", endpointAddress.ipv4Addr)
97+
assertEquals(8888, endpointAddress.portNumber)
98+
assertNull(endpointAddress.domainName)
99+
assertNull(endpointAddress.ipv6Addr)
46100
}
47-
val endpointAddress = deferredEndpointAddress.await()
101+
}
102+
@Test
103+
fun getEndpointAddressByRequestUrlI6() {
104+
val url = "http://[1080:0:0:0:8:800:200C:417A]:8888/akamai/bbb_30fps/bbb_30fps.mpd"
105+
val utils = Utils()
106+
val endpointAddress = utils.getEndpointAddressByRequestUrl(url)
107+
108+
assertNotNull(endpointAddress)
109+
48110
if (endpointAddress != null) {
49-
assertEquals("livesim.dashif.org", endpointAddress.domainName)
50-
assertEquals(443, endpointAddress.portNumber)
51-
assertNotNull(endpointAddress.ipv4Addr)
111+
assertEquals("[1080:0:0:0:8:800:200C:417A]", endpointAddress.ipv6Addr)
112+
assertEquals(8888, endpointAddress.portNumber)
113+
assertNull(endpointAddress.domainName)
114+
assertNull(endpointAddress.ipv4Addr)
52115
}
53116
}
117+
118+
54119
}

0 commit comments

Comments
 (0)