DHCP IP discovery, offer, request, ack - request

Why does my DHCP server use my old ip adress as destination?
0.0.0.0 - 255.255.255.255 -> discovery
192.168.0.1 - 192.168.0.100 -> offer
0.0.0.0 - 255.255.255.255 -> request
192.168.0.1 - 192.168.0.100 -> ACK
Normally it should use 255.255.255.255.
Can someone help?

The machine requesting an IP address is not filtering the received packets based on destination address and so it doesn't matter what address is placed as the destination.
The server machine has an ARP entry that connects an IP address to a given Ethernet MAC address (which is how you really contact the client) and so DHCP server process can use that address to create a directed reply rather than broadcasting to the entire subnet. To create a directed reply without some specific IP address would require the DHCP server to create its own UDP packets rather than just letting the system do it.

Related

How to get original ip instead of proxy's IP in TCP?

There are three host, clients A, Proxy B and Server C:
A->B->C.
In this example, how can I get A's IP address on server C?
This is TCP protocol.

Assign address address in UDP connection between two directly connected computers

I want to exchange messages between client and server which are on two different machines. The two machines are directly connected by an Ethernet cable. I can successfully send and receive messages between client and server if they are both on the same machine. What should be the server address in order to have communication between the two machines?
serveraddr.sin_addr.s_addr = htonl( ???? );
I've tried running ifconfig on the server machine but just found the MAC address and no IP to assign in the code.
Both machines are running DHCP.
Have a look at the UDP wiki, as you can see, it requires an network layer. This is typically IP. You can set a static IP address to both devices and use these static IP address or you can set up a DHCP server on one of these machines to assign IP addresses automatically.
Directly connecting the machines with a cable creates a unique independent network (with only the two nodes).
Note that the cable will have to be a crossover cable.
DHCP probably won't help if neither node is a DHCP server.
As indicated in the answer by Marrten Arias, assign both the client, and the server a static ip address; perhaps something like this:
Server IP: 192.168.01.01
Client IP: 192.168.01.02
Mask (for both): 255.255.255.0

Get IP address of the client in C sockets

How can I get the ip address of the client of which my server is connected to?
Here is how I accept incoming clients:
newsockfd = accept(sockfd, (struct sockaddr*)&clt_addr, &addrlen);
I tried the following:
printf("ip is: %s\n", inet_ntoa(clt_addr.sin_addr));
But I am getting 127.0.0.1 which is not my ip address. Could it have something to do with me using localhost to test my client and server?
Could it have something to do with me using localhost to test my client and server?
Yes. If you connect to a server on localhost the client will also be localhost. This connection is not happening over the Internet, there is no network hardware involved, the client packets will come from localhost. This is known as loopback and occurs entirely in software.
If the client were to connect to the server using the server's external IP address it will have to do a connection via external routing and thus require a routable IP address. For example, I can connect to a server on localhost, but I cannot using my routable IP address because my router blocks incoming connections.

Connecing to server by MAC winsock2

i am working on a remote control program so i need to create connecting to the server(the remote pc) by MAC address (i cant go by ip because he is behind NAT)
It is not possible to connect to a TCP/UDP server by its MAC address, you must use its IP address. Every TCP/UDP server has an IP address, even behind a NAT. TCP/UDP are designed around IP and are transport-agnostic so they can run on networks that are not based on Ethernet/MAC to begin with.
Since your server is behind a NAT, you must connect to the NAT's IP address, and the NAT must be configured to forward that connection to the server's IP address. You cannot avoid that, that is simply how NATs work. If the NAT supports uPNP, the server can programmably configure a port forwarding rule on the NAT. Otherwise the NAT admin must configure it manually.

I am getting error in IPv6 socket connection

My server is creating a IPV6 socket.
On client I am creating a IPV4 socket.
Now, In the connect API on client side I am passing the socket descriptor of IPV4 and IPV6 address structure.
INET_connect( sock,(SocketAddress *)in6_addr,(int)sizeof( sockaddr_in6 ) ) == 0 )
where
struct sockaddr_in6 in6_addr;
I am getting error in connection. Is it due to this or some other reason ?
Please note that my server is IPV6(having an IPV6 address)
You must use an IPV6 socket to connect to an IPV6 address, or use a tunneling service which will translate traffic from IPV4 addresses to and from IPV6 addresses.
On client I am creating a IPV4 socket
Why? Don't do that. If your client program creates an AF_INET socket (or is running on ip4-only OS) then there is nothing you can do on the client side to make such connection happen.
If you just have to do so - then it is the server that should be adapted to handle IPv4 clients.
Server might be able to accept a connection request from ip4 client - but it can only happen if server side disables IPV6_V6ONLY socket option and, obviously, if server's OS allows that. In this case ip6 server will see the ip4 client via ipv4-mapped ipv6 address.

Resources