-3

I'm trying to port some Windows code that uses RtlIpv4StringToAddressExA/RtlIpv6StringToAddressExA and RtlIpv6AddressToStringExA/RtlIpv4AddressToStringExA to Linux and can't seem to find quite equivalent APIs. There seem to be plenty of ways to convert just the IP address to string (and back), but I can't find something that supports the port as well.

For instance, to convert [2001:4898:d8:34:b912:426d:1c88:5859]:443 to an IP address from a string (and back).

To reiterate, I explicitly need an API that takes port into consideration.

Nick Banks
  • 4,298
  • 5
  • 39
  • 65
  • 1
    You have to split off the port number yourself. – S.S. Anne Oct 18 '19 at 17:08
  • I'm explicitly looking for an API that does that already. Windows has one already. I find it hard to believe no one else has this problem on other platforms. – Nick Banks Oct 18 '19 at 17:09
  • There is none. See also https://stackoverflow.com/a/14525435/10795151 – S.S. Anne Oct 18 '19 at 17:09
  • If it is truly the case that no Linux APIs support the port syntax that's going to be a pain to port existing Windows code that already uses this functionality. – Nick Banks Oct 18 '19 at 17:13
  • There is a windows api that does that? – KamilCuk Oct 18 '19 at 17:13
  • Did you read the question? I explicitly listed them already. – Nick Banks Oct 18 '19 at 17:14
  • 1
    It would probably be trivial to just write it yourself. – S.S. Anne Oct 18 '19 at 17:15
  • I have access the Windows source for those functions, and they are not trivial. They are hundreds of lines of code. – Nick Banks Oct 18 '19 at 17:25
  • Every time I've needed such a thing I've written my own code to split up ip address and port so they can be passed to `getaddrinfo()` like normal. It's only a few extra lines. – Shawn Oct 18 '19 at 17:39
  • Converting the address and port to string is simple enough to write manually. Taking a generic string that may be v4 or v6 and may or may not have a port and converting it to a struct is definitely more complicated. But it seems like I may have no choice. – Nick Banks Oct 18 '19 at 17:43
  • Converting a generic ip address that might be v4 or v6 to the appropriate `struct sockaddr_XXX` is also very simple. `getaddrinfo()` is your friend. – Shawn Oct 18 '19 at 18:04
  • [inet_pton](http://man7.org/linux/man-pages/man3/inet_pton.3.html) I think first extract port yourself, then `inet_pton(AP_INET6` – KamilCuk Oct 18 '19 at 18:54
  • Well, if someone wants to add an answer saying there is no solution for Linux, I'll accept it. – Nick Banks Oct 18 '19 at 22:26

1 Answers1

1

There is no API to do this. You have to split off the port number yourself.

Also see this answer, which mentions that most utilities use a -p port argument instead of :port to specify the port.

If you really want something like this, it would probably be trivial to just write it yourself.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76