Retrieving the IP address gmod is using from a C++ module

Hello I am working on a C++ module and I’m in the process of linking it to a web API I made. The problem is when the servers post to the API it sends it using my dedi’s IP instead of the individual servers IP. So I am looking to retrieve the IP the server is running on from the c++ module and send it with the API requests.

I’m not looking for someone to spoon feed me the code, I just need someone to point me in the right direction.

Any help is much appreciated, Thanks.

What does this code return, the individual server’s IP, or the Dedicated Server / Provider’s shared box IP?
[lua]
local url = “https://api.ipify.org/?format=text”;

local function onSuccess( body )

local ip = tostring( body );

Msg( string.format( “ip is %s”, ip ) );

end

local function onFailure( body )

Msg( string.format( “onFailure %s”, tostring( body ) ) );

end

http.Fetch( url, onSuccess, onFailure );
[/lua]

If you go the C++ route, try INetChannel:GetRemoteAddress, the returned struct netadr_t contains a method ToString that returns ip:port .

That Lua script will always return the machine’s main IP address, not the IP address the SRCDS instance is running on.

And it was also not give a port. Plus the C++ function linked won’t work. Net channels are between server and client and will only return the server’s IP address from the client.

Do some bitwise magic on the ‘hostip’ cvar. The ‘hostport’ cvar also exists if you want the port.

[editline]21st January 2016[/editline]

From SourceBans module, code by raBBish

[lua]
–[[ Automatic IP Locator ]]–
local serverport = GetConVarNumber(“hostport”);
local serverip;
do – Thanks raBBish! http://www.facepunch.com/showpost.php?p=23402305&postcount=1382
local function band( x, y )
local z, i, j = 0, 1
for j = 0,31 do
if ( x%2 == 1 and y%2 == 1 ) then
z = z + i
end
x = math.floor( x/2 )
y = math.floor( y/2 )
i = i * 2
end
return z
end
local hostip = tonumber(string.format("%u", GetConVarString(“hostip”)))
local parts = {
band( hostip / 2^24, 0xFF );
band( hostip / 2^16, 0xFF );
band( hostip / 2^8, 0xFF );
band( hostip, 0xFF );
}

serverip = string.format( "%u.%u.%u.%u", unpack( parts ) )

end
–]]
[/lua]

Unfortunately, lot’s of server providers don’t set hostip or hostport, but if you’re in control of the server you can set them.

That is set automatically…

It’s set via command-line or server.cfg and some hosts do not set it.

Not sure which host allows no access to your own config files nor command-line, but if they didn’t then the IP:port would be the default IP and port on your machine. Source will use the default settings unless specified.

It’s more of an issue for an API you’ll be handing out to lots of clients (ScriptEnforcer DRM is a good example), and Phoenixf129 mentioned NFO not setting it by default, which means people using your code will need to set hostip, hostport up on the command-line themselves.

Without the hostip, hostport cvar’s set I’m pretty sure there is no other way to get that information via Lua.

I wrote it in Lua because everyone else was really. The question is for C++, but the exact same bitwise math and CVar fetching can be done in C++ (it’s also a lot easier, using inet_ntoa).

He mentioned he’s linking to a web interface, which means he can simply do the request from the a default IP if hostip is not set and perform the request using a specific IP if it is set (along with passing along a specific port if required). Simply put, if hostip is not set, there’s nothing he needs to know.

[editline]21st January 2016[/editline]

*Also just a side note:

do not simply have your module send what IP it’s using, send it FROM that IP so the server has some way of authenticating that the sender does actually own the IP. *

The -ip and -port flags set when launching a server directly affect hostip and hostport

Yea I was planning on verifying the boxes IP, and using the IP just as a method for identifying the server in the DB. I decided getting the IP wasn’t worth the trouble and instead just generated an api key for each server which when the module starts it will use that api key to fetch a session api key that is used for generating checksums for the data of each request, along with an IP whitelist, and another key in the header of the requests. Might be a little overkill but fuck’em.

Thanks to willox as of the next update we can simply use game.GetIPAddress().

Isn’t this kinda necroposting? ._.