Raised This Month: $67 Target: $400
 16% 

SourceMod extension: how to get server's IP?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
root88
Senior Member
Join Date: May 2016
Old 03-03-2021 , 11:43   SourceMod extension: how to get server's IP?
Reply With Quote #1

Hi.
How can I get server IP in sourcemod extension?
__________________
root88 is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 03-03-2021 , 12:05   Re: SourceMod extension: how to get server's IP?
Reply With Quote #2

PHP Code:
#include <SteamWorks>

// [... all your other code ...]
    
char sPublicIP[64];
    
int iPublicIP[4];
    if (
SteamWorks_GetPublicIP(iPublicIP)) {
        
Format(sPublicIPsizeof(sPublicIP), "%d.%d.%d.%d:%d"iPublicIP[0], iPublicIP[1], iPublicIP[2], iPublicIP[3], GetConVarInt(FindConVar("hostport")));
    } else {
        
LogError("Appears like we had an error on getting the Public IP address.");
    }
// [... all your other code ...] 
Now, the string variable "sPublicIP" holds the data "192.0.2.123:27015", assuming that the Steam network sees the public IP as "192.0.2.123", and the port is "27015".

PHP Code:
    Format(sPublicIPsizeof(sPublicIP), "%d.%d.%d.%d"iPublicIP[0], iPublicIP[1], iPublicIP[2], iPublicIP[3])); 
can eventually be used, if you don't like the port number to be a part of it.
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread].
DarkDeviL is offline
root88
Senior Member
Join Date: May 2016
Old 03-04-2021 , 08:57   Re: SourceMod extension: how to get server's IP?
Reply With Quote #3

That's one way of doing it for the plugin, I need to do it in extension
__________________
root88 is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 03-05-2021 , 00:12   Re: SourceMod extension: how to get server's IP?
Reply With Quote #4

Quote:
Originally Posted by root88 View Post
That's one way of doing it for the plugin, I need to do it in extension
Well, the SourceMod > Scripting section says "Scripting in SourcePawn" for it's description. As such, due to your thread in a wrong section, I was somehow under the impression that your "extension" mention would be incorrect, and that you actually meant "plugin".

Moved to SourceMod > Metamod: Source > Coding MM:S Plugins & SM Extensions.
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread].
DarkDeviL is offline
root88
Senior Member
Join Date: May 2016
Old 03-05-2021 , 09:25   Re: SourceMod extension: how to get server's IP?
Reply With Quote #5

Thank you, I hadn't noticed this category at first. I've figured out how to do it, in case someone need this:
PHP Code:
    char serverip[32];
    
int ip g_pCvar->FindVar("hostip")->GetInt();
    
int port g_pCvar->FindVar("hostport")->GetInt();
    
    
int iips[4];
    
iips[0] = (ip >> 24) & 0x000000FF;
    
iips[1] = (ip >> 16) & 0x000000FF;
    
iips[2] = (ip >> 8) & 0x000000FF;
    
iips[3] = ip 0x000000FF;

    
snprintf(serveripsizeof(serverip), "%d.%d.%d.%d:%d"iips[0], iips[1], iips[2], iips[3], port); 
__________________

Last edited by root88; 03-05-2021 at 09:26.
root88 is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 03-05-2021 , 14:42   Re: SourceMod extension: how to get server's IP?
Reply With Quote #6

Quote:
Originally Posted by root88 View Post
PHP Code:
    int ip g_pCvar->FindVar("hostip")->GetInt(); 
"hostip" may not always return the real (public) IP address, such as for example if your server runs behind a router/NAT. In these situations, you may actually see the RFC1918 IP address ranges, e.g. 10.xx.xx.xx, 172.16.xx.xx-172.31.xx.xx and/or 192.168.xx.xx (the one that your server actually listens to on the actual machine).
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread].
DarkDeviL is offline
root88
Senior Member
Join Date: May 2016
Old 03-07-2021 , 04:39   Re: SourceMod extension: how to get server's IP?
Reply With Quote #7

That's true, however it should work for most dedicated servers (if started correctly).
__________________
root88 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:06.


Powered by vBulletin®
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Theme made by Freecode