| 1 | package Games::NeverwinterNights::Query; | ||
| 2 | $VERSION = "1.0"; | ||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | use IO::Socket; | ||
| 6 | use IO::Handle; | ||
| 7 | |||
| 8 | # | ||
| 9 | # Constructor | ||
| 10 | # Takes a hash of parameters | ||
| 11 | # | ||
| 12 | |||
| 13 | sub new | ||
| 14 | { | ||
| 15 | my $class = shift; | ||
| 16 | my $self = {}; | ||
| 17 | bless ($self,$class); | ||
| 18 | return $self; | ||
| 19 | } | ||
| 20 | |||
| 21 | =item _timeout | ||
| 22 | |||
| 23 | Internal method to perform consistent timeout calls via aliarm (callback function) | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | sub _timeout | ||
| 28 | { | ||
| 29 | die "Timedout !\n"; | ||
| 30 | } | ||
| 31 | |||
| 32 | =item _getResponse | ||
| 33 | |||
| 34 | Internal method to read a packet form the UDP socket | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub _getResponse | ||
| 39 | { | ||
| 40 | my $socket = shift; | ||
| 41 | my $size = shift; | ||
| 42 | my $timeout = shift; | ||
| 43 | my $buffer; | ||
| 44 | my $recAddr; | ||
| 45 | $SIG{ALRM} = \&_timeout; | ||
| 46 | alarm $timeout; | ||
| 47 | eval | ||
| 48 | { | ||
| 49 | $recAddr = $socket->recv($buffer,$size); | ||
| 50 | alarm (0); | ||
| 51 | }; | ||
| 52 | return $buffer; | ||
| 53 | } | ||
| 54 | |||
| 55 | =item query | ||
| 56 | |||
| 57 | Query a server | ||
| 58 | Params: | ||
| 59 | host - the host to scan | ||
| 60 | port - the port to use | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub query | ||
| 65 | { | ||
| 66 | my $self = shift; | ||
| 67 | my ($host,$port); | ||
| 68 | my $nh = shift; | ||
| 69 | my $np = shift; | ||
| 70 | if($nh) | ||
| 71 | { | ||
| 72 | $host = $nh; | ||
| 73 | } | ||
| 74 | else | ||
| 75 | { | ||
| 76 | $host = 'localhost'; | ||
| 77 | } | ||
| 78 | if($np) | ||
| 79 | { | ||
| 80 | $port = $np; | ||
| 81 | } | ||
| 82 | else | ||
| 83 | { | ||
| 84 | $port = '5121'; | ||
| 85 | } | ||
| 86 | # Socket created with 10 second timeout value | ||
| 87 | my $socket = IO::Socket::INET->new( | ||
| 88 | PeerAddr => $host, | ||
| 89 | PeerPort => $port, | ||
| 90 | Proto => 'udp'); | ||
| 91 | # Send first request | ||
| 92 | my $queryAll = "\xFE\xFD\x00\xE0\xEB\x2D\x0d\x14\x01\x0B\x01\x05\x08\x0a\x33\x34\x35\x13\x03\x36\x37\x38\x39\x14\x3A\x3B\x3C\x3D\x00\x00"; | ||
| 93 | #fc #sn #pt #gn #md #cc #mc #ml #xl #pp #pw #pt #op #pu #vr #ds #gt #el #il #vl #ep | ||
| 94 | my $sent = $socket->send($queryAll); | ||
| 95 | my $response; | ||
| 96 | my $recAddr; | ||
| 97 | $response = _getResponse($socket,2048,10); | ||
| 98 | my %results; | ||
| 99 | if(!$response) | ||
| 100 | { | ||
| 101 | $socket->send("\\info\\"); | ||
| 102 | $response = _getResponse($socket,2048,10); | ||
| 103 | unless ($response) | ||
| 104 | { | ||
| 105 | # Not old style just has gamespy off | ||
| 106 | # so lets just ping it | ||
| 107 | $socket->send("BNES\x00\x14\x01"); | ||
| 108 | $response = _getResponse($socket,2048,10); | ||
| 109 | my @rt = split("\x0a",$response); | ||
| 110 | $response = "xx\x00" . | ||
| 111 | "UNKNOWN\x00" . | ||
| 112 | "UNKNOWN\x00" . | ||
| 113 | "UNKNOWN\x00" . | ||
| 114 | "0\x00" . | ||
| 115 | "0\x00" . | ||
| 116 | "0\x00" . | ||
| 117 | "0\x00" . | ||
| 118 | "0\x00" . | ||
| 119 | "UNKNOWN\x00" . | ||
| 120 | "0\x00" . | ||
| 121 | "$port\x00" . | ||
| 122 | "0\x00" . | ||
| 123 | "0\x00" . | ||
| 124 | "0\x00" . | ||
| 125 | "$rt[1]\x00" . | ||
| 126 | "0\x00" . | ||
| 127 | "0\x00" . | ||
| 128 | "0\x00" . | ||
| 129 | "0\x00" . | ||
| 130 | "0\x00"; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | my @resData; | ||
| 134 | if($response) | ||
| 135 | { | ||
| 136 | @resData = split("\x00",$response); | ||
| 137 | $results{Host} = $host; | ||
| 138 | $results{Port} = $resData[11]; | ||
| 139 | $results{GameType} = _getGameType($resData[16]); | ||
| 140 | $results{PlayType} = $resData[2]; | ||
| 141 | $results{GameName} = $resData[3]; | ||
| 142 | $results{Module} = $resData[4]; | ||
| 143 | $results{Version} = $resData[14]; | ||
| 144 | if($resData[20] == 1) | ||
| 145 | { | ||
| 146 | $results{Version} .= " XP-1"; | ||
| 147 | } | ||
| 148 | elsif($resData[20] == 2) | ||
| 149 | { | ||
| 150 | $results{Version} .= " XP-2"; | ||
| 151 | } | ||
| 152 | $results{MinLevel} = $resData[7]; | ||
| 153 | $results{MaxLevel} = $resData[8]; | ||
| 154 | $results{PvP} = $resData[9]; | ||
| 155 | $results{Vault} = ($resData[19] == 1 ? "Local" : "Server"); | ||
| 156 | $results{OneParty} = ($resData[12] == 1 ? "Enabled" : "Disabled"); | ||
| 157 | $results{PlayerPausable} = ($resData[13] == 1 ? "Enabled" : "Disabled"); | ||
| 158 | $results{ItemLevelRestriction} = ($resData[18] == 1 ? "Enabled" : "Disabled"); | ||
| 159 | $results{EnforceLegalCharacters} = ($resData[17] == 1 ? "Enabled" : "Disabled"); | ||
| 160 | $results{Password} = ($resData[10] == 0 ? " Not Required" : "Required"); | ||
| 161 | $results{MaxClients} = $resData[6]; | ||
| 162 | $results{CurrentClients} = $resData[5]; | ||
| 163 | $results{ServerDescription} = $resData[15]; | ||
| 164 | } | ||
| 165 | # try to get player data | ||
| 166 | #if($resData[5] > 0) | ||
| 167 | #{ | ||
| 168 | # CODE HERE TO SCAN FOR PLAYER DATA | ||
| 169 | # It seems we are boned to get this data | ||
| 170 | # END CODE | ||
| 171 | # Send login request | ||
| 172 | #$socket->send("\x42\x4e\x43\x53\x00\x14\x10\x8e\x1b\x00\x00\x01\x00\x00\xad\xc3\xf8\x02\x0a" . "player check". "\x08" . "BOGUSKEY"); | ||
| 173 | # Send Fake Keys | ||
| 174 | #$socket->send("\x42\x4e\x56\x53\x56\x02\x28" . | ||
| 175 | # "BOGUSKEY" . | ||
| 176 | # "\x64\x39\x32\x37\x65\x36\x34\x62\x63\x64\x38\x38\x34\x63\x34\x66\x63\x39\x66\x36\x65\x38\x65\x62\x66\x65\x31\x30\x30\x32\x38\x30\x28" . | ||
| 177 | # "BOGUSKEY" . | ||
| 178 | # "\x37\x38\x61\x35\x30\x63\x34\x62\x36\x35\x32\x37\x63\x37\x62\x34\x38\x35\x63\x37\x36\x62\x34\x38\x35\x32\x36\x66\x37\x62\x65\x63\x20\x61\x62\x32\x30\x61\x39\x61\x64\x39\x30\x38\x33\x35\x64\x32\x38\x30\x65\x36\x66\x39\x65\x35\x31\x37\x66\x31\x38\x36\x38\x61\x32"); | ||
| 179 | |||
| 180 | # Send fake response (SPOOFED ADDRESS) | ||
| 181 | #$socket->send("\x42\x4d\x50\x52\x0a\x00" . "player check" . "\x00\x00"); | ||
| 182 | #$socket->send("\x42\x4d\x41\x52\x02\x00\x08\x00" . | ||
| 183 | # "BOGUSKEY" . | ||
| 184 | # "\x00\x00\x00\x00\x08\x00" . | ||
| 185 | # "BOGUSKEY" . | ||
| 186 | # "\x00\x00\x01\x00"); | ||
| 187 | #$response = _getResponse($socket,2048,10); | ||
| 188 | #print "Login response $response\n"; | ||
| 189 | #$socket->send("\x4D\x5D\x69\x00\x02\xFF"); | ||
| 190 | #$response = _getResponse($socket,2048,20); | ||
| 191 | #print "Response $response\n"; | ||
| 192 | #$socket->flush(); | ||
| 193 | #} | ||
| 194 | $socket->close(); | ||
| 195 | return %results; | ||
| 196 | } | ||
| 197 | |||
| 198 | =item _getGameType | ||
| 199 | |||
| 200 | Internal method to parse the game type code | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub _getGameType | ||
| 205 | { | ||
| 206 | my $type = shift; | ||
| 207 | my $result = "Unknown"; | ||
| 208 | if ( $type == "274" ) | ||
| 209 | { | ||
| 210 | $result = "Action"; | ||
| 211 | } | ||
| 212 | elsif ( $type == "363" ) | ||
| 213 | { | ||
| 214 | $result = "Story"; | ||
| 215 | } | ||
| 216 | elsif ( $type == "364" ) | ||
| 217 | { | ||
| 218 | $result = "Story Lite"; | ||
| 219 | } | ||
| 220 | elsif ( $type == "275" ) | ||
| 221 | { | ||
| 222 | $result = "Role Play"; | ||
| 223 | } | ||
| 224 | elsif ( $type == "276" ) | ||
| 225 | { | ||
| 226 | $result = "Team"; | ||
| 227 | } | ||
| 228 | elsif ( $type == "365" ) | ||
| 229 | { | ||
| 230 | $result = "Melee"; | ||
| 231 | } | ||
| 232 | elsif ( $type == "366" ) | ||
| 233 | { | ||
| 234 | $result = "Arena"; | ||
| 235 | } | ||
| 236 | elsif ( $type == "277" ) | ||
| 237 | { | ||
| 238 | $result = "Social"; | ||
| 239 | } | ||
| 240 | elsif ( $type == "279" ) | ||
| 241 | { | ||
| 242 | $result = "Alternative"; | ||
| 243 | } | ||
| 244 | elsif ( $type == "278" ) | ||
| 245 | { | ||
| 246 | $result = "PW Action"; | ||
| 247 | } | ||
| 248 | elsif ( $type == "367" ) | ||
| 249 | { | ||
| 250 | $result = "PW Story"; | ||
| 251 | } | ||
| 252 | elsif ( $type == "368" ) | ||
| 253 | { | ||
| 254 | $result = "Solo"; | ||
| 255 | } | ||
| 256 | elsif ( $type == "370" ) | ||
| 257 | { | ||
| 258 | $result = "Tech Support"; | ||
| 259 | } | ||
| 260 | return $result; | ||
| 261 | } | ||
| 262 | 1; | ||
| 263 | |||
| 264 | __END__ |