AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Coding MM:S Plugins & SM Extensions (https://forums.alliedmods.net/forumdisplay.php?f=75)
-   -   SourceMod extension: how to get server's IP? (https://forums.alliedmods.net/showthread.php?t=331043)

root88 03-03-2021 11:43

SourceMod extension: how to get server's IP?
 
Hi.
How can I get server IP in sourcemod extension?

DarkDeviL 03-03-2021 12:05

Re: SourceMod extension: how to get server's IP?
 
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.

root88 03-04-2021 08:57

Re: SourceMod extension: how to get server's IP?
 
That's one way of doing it for the plugin, I need to do it in extension :)

DarkDeviL 03-05-2021 00:12

Re: SourceMod extension: how to get server's IP?
 
Quote:

Originally Posted by root88 (Post 2739235)
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.

root88 03-05-2021 09:25

Re: SourceMod extension: how to get server's IP?
 
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); 


DarkDeviL 03-05-2021 14:42

Re: SourceMod extension: how to get server's IP?
 
Quote:

Originally Posted by root88 (Post 2739365)
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).

root88 03-07-2021 04:39

Re: SourceMod extension: how to get server's IP?
 
That's true, however it should work for most dedicated servers (if started correctly).


All times are GMT -4. The time now is 02:32.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.