Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 368 Vote(s) - 3.65 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get IP address from socket descriptor?

#1
I've opened a TCP socket server (I've omitted a few stuff, it is taken from [here][1]

sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol))

Is it possible to get the IP address of the server from sockfd? If not where should I look?

EDIT: I want to know the address of the server (this is before any client connects).

[1]:

[To see links please register here]

Reply

#2
You can't use the socket to get the server's address before a client connects, because it isn't known.

In principle, a host may have multiple IPs. The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known.
Even if you have only one IP, connections may arrive from within the machine, in which case the address would be `127.0.0.1`.

So the listening socket has no information about the IP.
You'll need to find what interfaces the machine has, and what's their IPs.
Reply

#3
If you want to know who's at the other end of your socket you can use [getpeername][1] in Linux. [getsockname][2] will tell you who you are. You decide what address you want your server to sit on initially though, at [bind][3] time.

You may also find this SO question useful:

[To see links please register here]


And the book "Unix Network Programming, vol 1", by W. Richard Stevens.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#4
The address of the server is the one that was passed to the successfull call to `bind()` (as shown in the source you linked).
Reply

#5
The address of the server is up to you.

Depends on which parameters are passed to the `bind()` function.

You can specify a `single ip` or bind your socket to every address of your host.

Look at the [Bind man page][1]


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through