Does the client socket connect need a route table even if it bind a specific ip? - sockets

here explain why need client socket bind,
https://stackoverflow.com/questions/4118241/what-client-side-situations-need-bind
my question is:
if I bind a specific source ip before connect,
does this connection need an item in route table?
After tested , I found the connect will fail if I delete the related route item.
Why it need a routing table even if I choose to go out from the specific ip(via bind this ip)?
by the way ,the below question is not help
Does routing affect a socket with a bound source address?
import socket
s = socket.socket()
port = 12345
s.bind(("158.200.201.12",33123))
s.connect(("158.101.170.92", port)) #<--- this will fail if I delete the route table item for 158.*
This question has no relation with language, I just use python to explain

Related

How to get username of another server's admin, with given ServerResponse object?

I'm making a simple server search function where players enter their username and either host a server or join a specific server from a server list. I can successfully list all servers on a local area network by getting their IP addresses with ServerResponse object. What I want is to show host's username, instead of IP number (Or any other values owned by server client when needed, without actually joining the server). Anyone has any idea how can I achieve this? Thanks in advance.

Can I find what network interface/device is handling my socket?

Say I've got a file descriptor from socket(2) and I've done a connect(2) on it -- is there any way later to determine (from inside the running program) what network device might be in use for the underlying transport? A call to stat(2) on the fd gives a device number of 0; none of the ioctl(2) or getsockopt(2) options seem applicable.
There's no foolproof way to do so -- certainly not a posix-compliant way.
However, in practice, you can easily determine the interface 99% of the time. After you've done the connect, use getsockname to obtain the IP address, then look through the list of available interfaces on the box for one with a matching IP address.
From the accept call you should be able to get the remote client's ip address (seen here on Beej's). Assuming you do not have any asymmetric routes, you can look up the route to that address in your local routing table. The routing table should tell you what ethernet device is used to send packets to the remote client.
EDIT:
you could use the following command line tool to query your local routing table with the remote client's address:
ip route get <remote-client-ip-addr>

DNS TXT Record query using non-default DNS server

I have my own DNS server ex: IP 34.34.34.34.
This server knows about few domain names.
At times I need to send a TXT Record query to this server and process the result.
What is the best framework to use for this on iOS?
It should be fairly simple to do it in C and call it from ObjectiveC.
Here's an example I to get you started: https://gist.github.com/wil/6141275
Just make sure you link with -lresolv (i.e. libresolv.dylib)

Understanding the domain associations

http://publib.boulder.ibm.com/infocenter/aix/v7r1/index.jsp?topic=%2Fcom.ibm.aix.progcomm%2Fdoc%2Fprogcomc%2Fskt_bind.htm
Internet domain:
Produces an association composed of local and foreign addresses and local and foreign ports.
UNIX domain:
Produces an association composed of local and foreign path names.
NDD domain (Network Device Driver of the operating system):
Provides an association composed of local device name (operating system NDD
name) and foreign addresses, the form of which depends on the protocol
being used.
They talk about association composed of local and foreign addresses.
What exactly does that mean.
I can understand the local address, but what's with the foreigh addresses, how does it find them, and which are they, and how does it create their association?
The local and foreign socket addresses are simply the address of your program (the local one) and the address of the other program you are communicating with (the foreign one).
Different programs use different mechanisms to determine that foreign address. For example:
To view a Web page, you type in a desired URL or click on a link; the URL contains a name, and the address associated with the name is looked up and connected to in order to retrieve the resource.
To look up an address from a name, your computer will contact a name server at a different designated address. The address of the name server is itself often obtained from another protocol, DHCP.
To locate a DHCP server, your computer will send a broadcast message to all machines on the local network, then wait for a server to reply.
Associations can also vary depending upon whether a protocol is connection-based or connectionless.
In a connection-based protocol such as TCP, this association is called a connection; messages are exchanged between the network subsystems of the two machines to negotiate the set-up of the connection, and this connection persists until explicitly closed.
In a connectionless protocol such as UDP, a simple facility to send and receive individual messages to or from any addressing-compatible endpoint is provided; any associations are entirely at the convenience of the software using the protocol.

Question about getaddrinfo() function in socket programming

Below is a sample invoking of getaddrinfo()
status = getaddrinfo("www.example.net","1234", &hints, &server_info);
After that, the server_info will point to a linked list with all kinds of address information.
I have the following questions:
Since I have clearly specified the host name and port number, the only address infos I can think of are IPv4 and IPv6 addresses. So, the length of the linked list should be 2. Is there any other kind of address info besides them?
Thanks.
The name could resolve to more than one IPv4 or IPv6 address, there is nothing to say that only one IPv4 address will be returned, for example (try it with "www.google.com" for example, you'll likely get more than one IPv4 address).
But in any case, I think the basic premise of your question is wrong. Even if there was no possibility to return more than one IPv4 and one IPv6 address today, the function is documented to return an arbitrarily-long linked list of addrinfo objects. Therefore, even if your code worked today in every situation, there is no guarantee that it would continue to work tomorrow. If the function is documented to return an arbitrarily-long linked list, then you need to be able to handle that.
You want to disconnect the physical configuration of machines with names in your mind. DNS merely maps a name to a set of addresses. A lot of hosts will only have one interface. Many hosts will have multiple (called multi-homing). DNS doesn't care about what the configuration of the machine or machines that the addresses it maps a name to is. As simple examples often a server will have interfaces on multiple networks with different addresses that will all map to the same name. Sometimes when collapsing services from different machines onto one different names will map to the same address. So don't assume any sort of 1:1 mapping between names and machines much less interfaces.