summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/include/smlib/server.inc
blob: ae54cc4f8df3fad83ac30feb401e69be28593687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#if defined _smlib_server_included
	#endinput
#endif
#define _smlib_server_included

#include <sourcemod>
#include <smlib/general>

/*
 * Gets the server's public/external (default) or
 * private/local (usually server's behind a NAT) IP.
 * If your server is behind a NAT Router, you need the SteamTools
 * extension available at http://forums.alliedmods.net/showthread.php?t=129763
 * to get the public IP. <steamtools> has to be included BEFORE <smlib>.
 * If the server is not behind NAT, the public IP is the same as the private IP.
 *
 * @param public		Set to true to retrieve the server's public/external IP, false otherwise.
 * @return				Long IP or 0 if the IP couldn't be retrieved.
 */
stock int Server_GetIP(bool public_=true)
{
	int ip = 0;

	static ConVar cvHostip = null;

	if (cvHostip == INVALID_HANDLE) {
		cvHostip = FindConVar("hostip");
		MarkNativeAsOptional("Steam_GetPublicIP");
	}

	if (cvHostip != INVALID_HANDLE) {
		ip = cvHostip.IntValue;
	}

	if (ip != 0 && IsIPLocal(ip) == public_) {
		ip = 0;
	}

#if defined _steamtools_included
	if (ip == 0) {
		if (CanTestFeatures() && GetFeatureStatus(FeatureType_Native, "Steam_GetPublicIP") == FeatureStatus_Available) {
			int octets[4];
			Steam_GetPublicIP(octets);

			ip =
				octets[0] << 24	|
				octets[1] << 16	|
				octets[2] << 8	|
				octets[3];

			if (IsIPLocal(ip) == public_) {
				ip = 0;
			}
		}
	}
#endif

	return ip;
}

/*
 * Gets the server's public/external (default) or
 * private/local (usually server's behind a NAT) as IP String in dotted format.
 * If your server is behind a NAT Router, you need the SteamTools
 * extension available at http://forums.alliedmods.net/showthread.php?t=129763
 * to get the public IP. <steamtools> has to be included BEFORE <smlib>.
 * If the public IP couldn't be found, an empty String is returned.
 * If the server is not behind NAT, the public IP is the same as the private IP.
 *
 * @param buffer		String buffer (size=16)
 * @param size			String buffer size.
 * @param public		Set to true to retrieve the server's public/external IP, false otherwise.
 * @return				True on success, false otherwise.
 */
stock bool Server_GetIPString(char[] buffer, int size, bool public_=true)
{
	int ip;

	if ((ip = Server_GetIP(public_)) == 0) {
		buffer[0] = '\0';
		return false;
	}

	LongToIP(ip, buffer, size);

	return true;
}

/*
 * Gets the server's local port.
 *
 * @noparam
 * @return			The server's port, 0 if there is no port.
 */
stock int Server_GetPort()
{
	static ConVar cvHostport = null;

	if (cvHostport == INVALID_HANDLE) {
		cvHostport = FindConVar("hostport");
	}

	if (cvHostport == INVALID_HANDLE) {
		return 0;
	}

	int port = cvHostport.IntValue;

	return port;
}

/*
 * Gets the server's hostname
 *
 * @param hostname		String buffer
 * @param size			String buffer size
 * @return				True on success, false otherwise.
 */
stock bool Server_GetHostName(char[] buffer, int size)
{
	static ConVar cvHostname = null;

	if (cvHostname == INVALID_HANDLE) {
		cvHostname = FindConVar("hostname");
	}

	if (cvHostname == INVALID_HANDLE) {
		buffer[0] = '\0';
		return false;
	}

	cvHostname.GetString(buffer, size);

	return true;
}