Commit 758461ab authored by Robert Scott James's avatar Robert Scott James

Merge branch 'master' into 'master'

Added byte, int and string read functions, the string one is slightly broken. Di…

…d more work on parsing messages, bot can not understand the Died, dropflag, scoreflag, initclient, timeupm, mapchance and welcome messages, though they produced some garbled string data. Client also now auto spectates.

See merge request !1
parents f5ab5edb fa8a6d71
......@@ -6,7 +6,7 @@ INCLUDES=-I/usr/local/include -I.
BinaryName=sauerbot.app
OBJS=main.o
OBJS=main.o handledata.o create.o
all: $(OBJS) $(BinaryName)
......@@ -17,4 +17,4 @@ $(BinaryName):
$(CC) -o $(BinaryName) $(CFLAGS) $(LDFLAGS) $(INCLUDES) $(OBJS)
clean:
rm -rf *.o $(BinaryName)
rm -rf *.o $(BinaryName)
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <vector>
#include <inttypes.h>
#include <signal.h>
#include <enet/enet.h>
#include <cstdarg>
#include "handler.h"
#include "sauer.h"
#include "create.h"
std::string putint(int n){
if(n < 128 && n > -127){
std::string output(1, n);
return(output);
} else if (n < 0x8000 && n >= -0x8000) {
char *t = "..."; //This is ugly as fuck, check it rob
t[0] = 0x80;
t[1] = n & 0xFF;
t[2] = n >> 8;
std::string output(t,3);
return(output);
} else {
char *t = ".....";
t[0] = 0x80;
t[1] = n & 0xFF;
t[2] = (n >> 8) & 0xFF;
t[3] = (n >> 16) & 0xFF;
t[4] = n >> 24;
std::string output(t,5);
return(output);
}
}
void createpacket(ENetPeer *connpeer, const char* fmt...){
std::vector<char> argstring;
va_list args;
va_start(args, fmt);
while (*fmt != '\0') {
if (*fmt == 'd') {
int i = va_arg(args, int);
std::string weirdstring = putint(i);
for(unsigned int i = 0; i < weirdstring.size(); i++){
argstring.push_back(weirdstring[i]);
}
} else if (*fmt == 's') {
char *string_temp = va_arg(args, char *);
for(unsigned int i = 0; i < std::strlen(string_temp); i++){
argstring.push_back(string_temp[i]);
}
argstring.push_back('\0');
}
++fmt;
}
std::string packetstring;
std::cout << std::hex;
for(unsigned int i = 0; i < argstring.size(); i++){
packetstring.push_back(argstring.at(i));
std::cout << (int)packetstring[i];
}
std::cout << std::endl << std::dec;
std::cerr << "Just Sent Packet of Length: " << packetstring.size() << std::endl;
ENetPacket *packet = enet_packet_create(packetstring.c_str(),argstring.size(), ENET_PACKET_FLAG_RELIABLE);
va_end(args);
sendpacket(connpeer,packet);
}
void sendpacket(ENetPeer *connpeer,ENetPacket *packet){
enet_peer_send (connpeer, 1, packet);
}
\ No newline at end of file
#ifndef CREATE_H
#define CREATE_H
void createpacket(ENetPeer *connpeer, const char* fmt...);
void sendpacket(ENetPeer *connpeer,ENetPacket *packet);
#endif
\ No newline at end of file
char *N_REVERSE_DEBUG[] = {
"N_CONNECT", "N_SERVINFO", "N_WELCOME", "N_INITCLIENT", "N_POS", "N_TEXT", "N_SOUND", "N_CDIS",
"N_SHOOT", "N_EXPLODE", "N_SUICIDE",
"N_DIED", "N_DAMAGE", "N_HITPUSH", "N_SHOTFX", "N_EXPLODEFX",
"N_TRYSPAWN", "N_SPAWNSTATE", "N_SPAWN", "N_FORCEDEATH",
"N_GUNSELECT", "N_TAUNT",
"N_MAPCHANGE", "N_MAPVOTE", "N_TEAMINFO", "N_ITEMSPAWN", "N_ITEMPICKUP", "N_ITEMACC", "N_TELEPORT", "N_JUMPPAD",
"N_PING", "N_PONG", "N_CLIENTPING",
"N_TIMEUP", "N_FORCEINTERMISSION",
"N_SERVMSG", "N_ITEMLIST", "N_RESUME",
"N_EDITMODE", "N_EDITENT", "N_EDITF", "N_EDITT", "N_EDITM", "N_FLIP", "N_COPY", "N_PASTE", "N_ROTATE", "N_REPLACE", "N_DELCUBE", "N_REMIP", "N_NEWMAP", "N_GETMAP", "N_SENDMAP", "N_CLIPBOARD", "N_EDITVAR",
"N_MASTERMODE", "N_KICK", "N_CLEARBANS", "N_CURRENTMASTER", "N_SPECTATOR", "N_SETMASTER", "N_SETTEAM",
"N_BASES", "N_BASEINFO", "N_BASESCORE", "N_REPAMMO", "N_BASEREGEN", "N_ANNOUNCE",
"N_LISTDEMOS", "N_SENDDEMOLIST", "N_GETDEMO", "N_SENDDEMO",
"N_DEMOPLAYBACK", "N_RECORDDEMO", "N_STOPDEMO", "N_CLEARDEMOS",
"N_TAKEFLAG", "N_RETURNFLAG", "N_RESETFLAG", "N_INVISFLAG", "N_TRYDROPFLAG", "N_DROPFLAG", "N_SCOREFLAG", "N_INITFLAGS",
"N_SAYTEAM",
"N_CLIENT",
"N_AUTHTRY", "N_AUTHKICK", "N_AUTHCHAL", "N_AUTHANS", "N_REQAUTH",
"N_PAUSEGAME", "N_GAMESPEED",
"N_ADDBOT", "N_DELBOT", "N_INITAI", "N_FROMAI", "N_BOTLIMIT", "N_BOTBALANCE",
"N_MAPCRC", "N_CHECKMAPS",
"N_SWITCHNAME", "N_SWITCHMODEL", "N_SWITCHTEAM",
"N_INITTOKENS", "N_TAKETOKEN", "N_EXPIRETOKENS", "N_DROPTOKENS", "N_DEPOSITTOKENS", "N_STEALTOKENS",
"N_SERVCMD",
"N_DEMOPACKET"
};
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <string>
#include <cstring>
#include <inttypes.h>
#include <signal.h>
#include <enet/enet.h>
#include "handler.h"
#include "create.h"
#include "sauer.h"
#include "debug.h"
void datahandler(char *data, long datalength, ENetPeer *connpeer){
int off = 0;
int messagecode = getint(data,off);
bool welcomeMessage = false;
switch(messagecode){
//Server and Map related messages
case N_SERVINFO:{
std::cout<< "Server Info" << std::endl;
int mycn = getint(data,off);
int prot = getint(data,off);
int sessId = getint(data,off);
bool passworded = getint(data,off)>0;
char* info = getstring(data,off);
char* auth = getstring(data,off);
std::cout << "Connected" << std::endl << " clientNumber:" << mycn << " protocol:" << prot << std::endl;
std::cout << " sessionId:" << sessId << " passworded " << passworded << std::endl;
std::cout << " " << info << ". AuthInfo:" << auth << std::endl;
//Reply with an N_CONNECT to the SERVER, and also make ourselves a spectator
createpacket(connpeer, "dsdsss",N_CONNECT,"li'Botler",1,"","","");
createpacket(connpeer, "ddd",N_SPECTATOR,mycn,1);
break;
}
case N_WELCOME:{
//Has no payload of it's own but contains several other packets as it's payload
//They seem to be (in order): MAPCHANGE, TIMEUP, ITEMLIST, SPAWNSTATE,
//INITCLIENT (for each client). 0xFF seems to be used to end lists for spawnstate and itemlist...
welcomeMessage = true;
std::cout << "== WELCOME DUMP ==" << std::endl;
std::cout << " == RAW DATA ==" << std::endl;
for(int i = 0; i < datalength; i++){
std::cout << (int)(unsigned char)data[i] << " ";
}
std::cout << std::endl << " == END RAW ==" << std::endl;
}
case N_MAPCHANGE:{
char* newmap = getstring(data,off);
int mode = getint(data,off), u2 = getint(data,off);
std::cout << "Map changed to " << newmap << " on mode " << mode << std::endl;
if (!welcomeMessage || getint(data,off) != N_TIMEUP) break;
}
case N_TIMEUP:{
int timeLeft = getint(data,off);
std::cout << timeLeft << "Seconds left on current map" << std::endl;
if (!welcomeMessage || getint(data,off) != N_ITEMLIST) break;
}
case N_ITEMLIST:{
if (!welcomeMessage) break;
while (getint(data,off) != -1){}// Cheat here till I get this one implemented
if (getint(data,off) != N_TEAMINFO) break;
}
case N_TEAMINFO:{
if (!welcomeMessage) break;
while (getint(data,off) != -1){}// Cheat here till I get this one implemented
if (getint(data,off) != N_SPAWNSTATE) break;
}
case N_SPAWNSTATE:{
if (!welcomeMessage) break;
while (getint(data,off) != -1 && off < datalength){}// Cheat here till I get this one implemented
if (getint(data,off) != N_INITCLIENT) break;
}
case N_INITCLIENT:{ //Also thrown for user changing name
int cn;
while ((cn = getint(data,off)) != -1 && off < datalength){
getstring(data,off);
getstring(data,off);
getint(data,off);
char* name = getstring(data,off);
char* team = getstring(data,off);
int model = getint(data,off);
std::cout << name << " joined the game with client number " << cn << " on team " << team << " with model " << model << std::endl;
if (!welcomeMessage) break;
}
if (welcomeMessage){
std::cout << "=== END DUMP ===" << std::endl;
}
break;
}
//General Player Interation mEssages
case N_DIED:{
int vcn = getint(data,off), acn = getint(data,off), frags = getint(data,off), tfrags = getint(data,off);
std::cout << (int)(unsigned char)acn << " Killed " << (int)(unsigned char)vcn << ". Frags, " << frags << " tfrags? " << tfrags << std::endl;
break;
}
//CTF Messages
case N_DROPFLAG:{
int ocn = getint(data,off), flag = getint(data,off), version = getint(data,off);
int loc[3] = {getint(data,off),getint(data,off),getint(data,off)};
std::cout << (int)(unsigned char)ocn << " Droped flag " << flag << "." << version
<< " at " << loc[0] << "," << loc[1] << "," << loc[2] << std::endl;
break;
}
case N_SCOREFLAG:{
int ocn = (int)(unsigned char)getint(data,off),
relayflag = getint(data,off), relayversion = getint(data,off),
goalflag = getint(data,off), goalversion = getint(data,off), goalspawn = getint(data,off),
team = getint(data,off), score = getint(data,off), oflags = getint(data,off);
std::cout << ocn << " Captured flag " << relayflag << "." << relayversion
<< " Bringing it to " << goalflag << "." << goalversion << " at " << goalspawn
<< " for team " << team << " Scoring " << score << ". oflags? " << oflags << std::endl;
}
//Ignore form loggin
case N_CLIENT:
case N_SHOTFX: //Sound effect from gun firing, mabe can be sued to work out where and in what directions guns are fired
case N_HITPUSH:
{break;}
default:{
std::cout << " Unhandled Packet:" << N_REVERSE_DEBUG[data[0]] << " len:" << std::dec << datalength << std::endl;
/*for(int i = 0; i < datalength; i++){
std::cout << (int)(unsigned char)data[i] << " ";
}
std::cout << std::endl;*/
break;
}
break;
}
}
char getbyte(char *data,int& off){
return data[off++];
}
int getint(char *data,int& off){
int c = getbyte(data,off);
if(c==-128) {
int n = getbyte(data,off);
n |= getbyte(data,off)<<8;
return n;
} else if(c==-127) {
int n = getbyte(data,off);
n |= getbyte(data,off)<<8;
n |= getbyte(data,off)<<16;
return n|(getbyte(data,off))<<24;
} else {
return c;
}
}
char* getstring(char *data,int& off){
//int len = strlen(data+off);
int i;
char c;
char out[256];
for(i=0;(c = *(data+off+i)) != '\x00';i++){
out[i] = c;
}
out[++i] = '\x00';
off+=i;
return out;
}
\ No newline at end of file
#ifndef HANDLER_H
#define HANDLER_H
void datahandler(char *,long, ENetPeer *);
int getint(char *,int&);
char getbyte(char *,int&);
char* getstring(char *,int&);
#endif
\ No newline at end of file
connected to server
Server Info
Connected
clientNumber:1 protocol:259
sessionId:-22 passworded 0
. AuthInfo:
06c6927426f746c657201000
3b11
garden joined the game with client number 22 on team gardenV` with model 4
Unhandled Packet:N_SERVMSG len:41
35 87 101 108 99 111 109 101 32 116 111 32 116 104 101 32 83 85 67 83 32 83 97 117 101 114 98 114 97 116 101 110 32 83 101 114 118 101 114 33 0
Unhandled Packet:N_SPECTATOR len:3
59 1 1
......@@ -4,9 +4,12 @@
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <inttypes.h>
#include <signal.h>
#include <enet/enet.h>
#include "handler.h"
void deinitialise(int s){
int signal = s;
......@@ -14,7 +17,7 @@ void deinitialise(int s){
exit(1);
}
int main() {
int main(int argc, char *argv[]) {
//Enet Initilize
if (enet_initialize () != 0) {
fprintf (stderr, "An error occurred while initializing ENet.\n");
......@@ -32,9 +35,7 @@ int main() {
sigaction(SIGINT, &sigIntHandler, NULL);
//Connection to client, start with the client's host, (how cube does it)
std::cerr << "Connecting" << std::endl;
ENetHost *clienthost = NULL;
ENetPeer *connpeer = NULL;
ENetAddress address;
......@@ -66,20 +67,19 @@ int main() {
case ENET_EVENT_TYPE_RECEIVE:
if (event.channelID == 1){
//std::cout << event.packet->data << std::endl;
unsigned char *datarecv;
char *datarecv;
size_t packetlength = event.packet->dataLength;
datarecv = (unsigned char *)malloc(packetlength);
datarecv = (char *)malloc(packetlength);
std::memcpy(datarecv, event.packet->data,packetlength);
std::cout << "Packet Length: " << packetlength << std::endl;
for(unsigned int i = 0; i < packetlength; i++){
std::cout << datarecv[i];
}
std::cout << std::endl;
enet_packet_destroy (event.packet);
//Handler Function
datahandler(datarecv,packetlength,connpeer);
enet_host_flush(clienthost);
free(datarecv);
enet_packet_destroy (event.packet);
} else {
std::cerr << "Data over another channel: " << event.channelID << std::endl;
//std::cerr << "Data over another channel: " << event.channelID << std::endl;
}
break;
......@@ -99,4 +99,4 @@ int main() {
enet_host_destroy(clienthost);
std::cout << "Shutting down\n" << std::endl;
}
}
\ No newline at end of file
#ifndef SAUER_H
#define SAUER_H
enum {
N_CONNECT = 0, N_SERVINFO, N_WELCOME, N_INITCLIENT, N_POS, N_TEXT, N_SOUND, N_CDIS,
N_SHOOT, N_EXPLODE, N_SUICIDE,
N_DIED, N_DAMAGE, N_HITPUSH, N_SHOTFX, N_EXPLODEFX,
N_TRYSPAWN, N_SPAWNSTATE, N_SPAWN, N_FORCEDEATH,
N_GUNSELECT, N_TAUNT,
N_MAPCHANGE, N_MAPVOTE, N_TEAMINFO, N_ITEMSPAWN, N_ITEMPICKUP, N_ITEMACC, N_TELEPORT, N_JUMPPAD,
N_PING, N_PONG, N_CLIENTPING,
N_TIMEUP, N_FORCEINTERMISSION,
N_SERVMSG, N_ITEMLIST, N_RESUME,
N_EDITMODE, N_EDITENT, N_EDITF, N_EDITT, N_EDITM, N_FLIP, N_COPY, N_PASTE, N_ROTATE, N_REPLACE, N_DELCUBE, N_REMIP, N_NEWMAP, N_GETMAP, N_SENDMAP, N_CLIPBOARD, N_EDITVAR,
N_MASTERMODE, N_KICK, N_CLEARBANS, N_CURRENTMASTER, N_SPECTATOR, N_SETMASTER, N_SETTEAM,
N_BASES, N_BASEINFO, N_BASESCORE, N_REPAMMO, N_BASEREGEN, N_ANNOUNCE,
N_LISTDEMOS, N_SENDDEMOLIST, N_GETDEMO, N_SENDDEMO,
N_DEMOPLAYBACK, N_RECORDDEMO, N_STOPDEMO, N_CLEARDEMOS,
N_TAKEFLAG, N_RETURNFLAG, N_RESETFLAG, N_INVISFLAG, N_TRYDROPFLAG, N_DROPFLAG, N_SCOREFLAG, N_INITFLAGS,
N_SAYTEAM,
N_CLIENT,
N_AUTHTRY, N_AUTHKICK, N_AUTHCHAL, N_AUTHANS, N_REQAUTH,
N_PAUSEGAME, N_GAMESPEED,
N_ADDBOT, N_DELBOT, N_INITAI, N_FROMAI, N_BOTLIMIT, N_BOTBALANCE,
N_MAPCRC, N_CHECKMAPS,
N_SWITCHNAME, N_SWITCHMODEL, N_SWITCHTEAM,
N_INITTOKENS, N_TAKETOKEN, N_EXPIRETOKENS, N_DROPTOKENS, N_DEPOSITTOKENS, N_STEALTOKENS,
N_SERVCMD,
N_DEMOPACKET,
NUMMSG
};
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment