Same host UDP packet correlation in Go - go

In Go one can send UDP packets using net.Addr interface to specify the target endpoint. Some special addresses, e.g. :8080 and 0.0.0.0, sends packets using local loopback interface. When received, still on the same host, the message's net.Addr shows [::1]:8080 as source. What is the the easiest way to determine that the packet was sent and received by the same host?
Here's an example in the Go Playground. It shows 0.0.0.0:8080 (ipv4) instead of [::1]:8080.

I ended up using net.Dial("udp", addr) and eminently closing the connection. Dial also resolves hostnames, which I also needed.
Would be nice to avoid creating a socket, but Dial works for now.

:8080 is not an address, it is a port number, typically used when testing your own http websites on Windows because on Windows you cannot easily use port 80, the actual http port.
This will only use the loopback interface if you use localhost as the IP address.
The localhost address for IPv4 is 127.0.0.1 and for IPv6 it is ::1. The address 0.0.0.0 is usually used as a placeholder address to indicate, for example, listening on all IP addresses, see this question.
As mentioned in the comment, you can use net.IP.Equal to check if your peer is localhost. Just compare your address in question to 127.0.0.1 or to ::1, the net.IP.Equal function considers them equal.

Related

what does tcp:*:port mean in zeromq?

I see this sort of address used in a bunch of examples. What does it mean exactly? Does it mean it will connect to any/all machines on the subnet that have something listening to that port? Or something else entirely? I see such usage in the docs and in books without explanation. Sort of annoying.
It is explained in the manual.
ZeroMQ supports multiple transports. tcp means you are using the TCP transport.
The address (or endpoint) for the TCP transport has the following format:
tcp://interface:port
When you bind to a local address, interface is either the IP address of a specific interface (network) or *, which means to listen on all interfaces (networks). port is the TCP port or * for a random port.
When you connect to a remote endpoint, interface is the hostname or IP address of the remote machine. port is the TCP port of the remote endpoint.
To add to rveerd's answer, what's often missed is that you can multiply bind a socket. So, tcp://*:5555 specifies port 5555 on any interface and you can bind the socket accordindly. But by calling zmq_bind() again you can bind the same socket to, say, ipc:///tmp/feeds/0, which means it will also accept connections on the /tmp/feeds/0 IPC pipe.
This is a pretty spectacularly useful feature in my view, because you can trvially have other actors local and remote to the machine though a single zmq socket.

Force gateway for IP connection

I have two NICs in my Windows PC, one for Internet and the other for outbound UDP streams. Both NICs have gateways and I tweak the metrics so that Internet bound traffic goes to the first. I would rather disable the gateway on the second NIC and specify the gateway when I create the UDP socket. Is this possible? Can I force the destination MAC address on a socket?
You have to bind() the socket to the local IP address of the NIC you want to use. If you don't know the IP, use GetAdaptersInfo() or GetAdaptersAddresses() to enumerate the NICs until you find the one you want, and then you will know its current IP to bind to.

What happens when an ipv6 client connects to an ipv4 host

This is a noob question, but networking isn't my forte. For example if I have an ipv4 server and an ipv6 client connects, what would their ip show as? Also if I wanted to setup a socket connection for example, does my server have to be ipv6 too, or does the code just need to be able to handle it.
I have researched how code handling works, but nothing says if the server has to be ipv6, itself.
IPv4 and IPv6 are not directly compatible. In most installations, a client will have both an IPv4 and an IPv6 address, and will use whichever one is appropriate to connect to a server. That is, they will use their IPv4 address to connect to an IPv4 server, IPv6 to connect to an IPv6 server, and will preferentially choose one of the two — usually IPv6 — if a server supports both.
IPv4-only clients cannot connect to IPv6 servers. Unless you intend to provide a service to IPv6 users only, you will need to provide your service on IPv4, or on both protocols, to support IPv4 clients.
IPv4 and IPv6 are separate incompatible protocols. An IPv6 client cannot connect directly to a server running only IPv4.
A connection is possible if an intervening router or switch maps from the IPv6 protocol to IPv4, or if the server runs both protocols. However, the IPv6 client is still maintaining an IPv6 connection and is unaware of any IPv4 connection.

how can I connect to an ipv4 host with ipv6 request?

Although most of the hosts have ipv6 address now, there are still some hosts that only have ipv4. In my LAN, connections using ipv4 will cost money, while connections using ipv6 is free. I want to implement a proxy to convert ipv4 and ipv6 request, so that I can connect to ipv4 host free.
Is it possible to implement that? And is there any available software?
This largely depends on the devices, services/protocols and the direction you want to connect in.
NAT64/DNS64
With NAT64/DNS64 you can let IPv6-only clients connect to IPv4-only servers. The system looks up the name of the server it wants to connect to using the DNS64 server. If the DNS64 server sees that only an IPv4 address is available it will replace the IPv4 address of the server with a special IPv6 address in which it has encoded the original IPv4 address. When the IPv6-only system connects to that IPv6 address the NAT64 router knows that the intention is to connect to the IPv4 address encoded in the IPv6 address and it will set up a NAT session to that IPv4 address. The NAT64 box needs to have both an IPv4 and IPv6 address to be able to do this.
HTTP Proxy
If you only want to support HTTP and similar protocols then you might be able to use an HTTP proxy server. It will need to have both an IPv4 and IPv6 address, and your applications/devices need to support using a proxy server. It will work both for IPv4-only clients and IPv6-only servers and vice-versa.
SOCKS5
A SOCKS5 proxy server can also be used in the same way that an HTTP proxy server can be used, but with a wider variety of protocols. Your clients need to support it though.
Other
There are other more application-specific ways to proxy between IPv4 and IPv6. The few mentioned above are just to give you an idea of common ones.

tcp server ip address

When starting H2 tcp server and the host pc has multiple IP address's how can I define the IP that the server is going to bind to listen for connections ?
We can define the tcp port but there does not seems to be a way to define the ip address.
Thank you, Oscar
http://www.h2database.com/html/advanced.html#server_bind_address
Usually server sockets accept connections on any/all local addresses.
This may be a problem on multi-homed hosts. To bind only to one
address, use the system property h2.bindAddress. This setting is used
for both regular server sockets and for SSL server sockets. IPv4 and
IPv6 address formats are supported.

Resources