This repository has been archived on 2024-01-08. You can view files and clone it, but cannot push or open issues or pull requests.
WorldsChatRevived/main.cpp

145 lines
6.4 KiB
C++

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <iomanip>
int main ( int argument, char const *argv[] ) {
int obj_server, sock, reader;
struct sockaddr_in address;
int opted = 1;
int address_length = sizeof(address);
char buffer[128] = {0};
if ((obj_server = socket ( AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Opening of Socket Failed!");
exit(EXIT_FAILURE);
}
if (setsockopt(obj_server, SOL_SOCKET, SO_REUSEADDR, &opted, sizeof (opted))) {
perror("Can't set the socket");
exit(EXIT_FAILURE );
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(5150);
if (bind(obj_server, (struct sockaddr*)&address, sizeof(address))<0) {
perror("Binding of socket failed!");
exit(EXIT_FAILURE);
}
if (listen (obj_server, 3) < 0) {
perror("Can't listen from the server!");
exit(EXIT_FAILURE);
}
if ((sock = accept(obj_server, (struct sockaddr *)&address, (socklen_t*)&address_length)) < 0) {
perror("Accept");
exit(EXIT_FAILURE);
}
char* username;
while ((reader = read(sock, buffer, sizeof(buffer)-1)) > 0) {
int blength = buffer[0];
if (blength <= 3) continue;
int objid = buffer[1];
int mestype = buffer[2];
if (mestype == 1) { // MOVE
int locX = (buffer[3] << 8) + (buffer[4]);
int locY = (buffer[5] << 8) + (buffer[6]);
int locZ = (buffer[7] << 8) + (buffer[8]);
int rot = (buffer[9] << 8) + (buffer[10]);
//printf("[LOCATION (%i)] x: %d, y: %d, z: %d, r: %d\n", mestype, locX, locY, locZ, rot);
} else if (mestype == 3) { // PROP
if (buffer[3] == 5) { // AVATAR
int avalen = buffer[4];
char avatar[] = {0};
memcpy(avatar, &buffer[5], avalen-1);
avatar[avalen]='\0';
printf("AVATAR: %s\n", avatar);
}
} else if (mestype == 5) { // CHANGE ROOM
int rid = (buffer[3] << 8) + (buffer[4]);
int locX = (buffer[5] << 8) + (buffer[6]);
int locY = (buffer[7] << 8) + (buffer[8]);
int locZ = (buffer[9] << 8) + (buffer[10]);
int rot = (buffer[11] << 8) + (buffer[12]);
printf("[ROOMCHNG (%i)] room: %d, x: %d, y: %d, z: %d, r: %d\n", mestype, rid, locX, locY, locZ, rot);
} else if (mestype == 6) { // SESS INIT
char password[128] = {0};
char logonoff[128] = {0};
char protocol[128] = {0};
char clientvr[128] = {0};
int l {3};
while (l < blength){
int type = buffer[l++];
int len = buffer[l++];
if (type == 2) {
username = new char[len+1];
memcpy(username, &buffer[l], len);
username[len]='\0';
} else if (type == 6) {
memcpy(password, &buffer[l], len);
password[len]='\0';
} else if (type == 12) {
memcpy(logonoff, &buffer[l], len);
logonoff[len]='\0';
} else if (type == 3) {
memcpy(protocol, &buffer[l], len);
protocol[len]='\0';
} else if (type == 9) {
memcpy(clientvr, &buffer[l], len);
clientvr[len]='\0';
}
l+=len;
}
printf("[SESSINIT (%i)] USERNAME: %s, PASSWORD: %s, LOGONOFF: %s, PROTOCOL: %s, CLIENT: %s\n", mestype, username, password, logonoff, protocol, clientvr);
// Sending accepted packet.
unsigned char neit[] = {0x1b, 0x01, 0x06, 0x04, 0x01, 0x30, 0x01, 0x0c, 0x57, 0x4f, 0x52, 0x4c, 0x44, 0x53, 0x4d, 0x41, 0x53, 0x54, 0x45, 0x52, 0x03, 0x02, 0x32, 0x34, 0x0f, 0x01, 0x31};
send(sock, neit, sizeof(neit)/sizeof(*neit), 0);
} else if (mestype == 7) { // SESS EXIT
printf("Session Exit\n");
} else if (mestype == 14) { // TEXT
int namelen = buffer[3];
char name[namelen+1];
strcpy(name, &buffer[4]);
int msglen = buffer[5+buffer[3]];
char msg[msglen+1];
strcpy(msg, &buffer[buffer[3]+6]);
char output[namelen+msglen+5] = {0};
output[1] = 0x01; output[2] = 0x0e;
output[4] = strlen(name);
strcpy(&output[5], name);
output[5+buffer[3]] = strlen(msg);
strcpy(&output[4+(buffer[3]+1)+1], msg);
output[0] = strlen(output);
send(sock, output, output[0], 0);
printf("[TEXT (%i)] sender: %s, message: %s\n", mestype, name, msg);
printf("SEND[TEXT (%i)] %s\n", mestype, output);
} else if (mestype == 18) { // TELEPORT
int rid = (buffer[3] << 8) + (buffer[4]);
int wid = (buffer[5] << 8) + (buffer[6]);
int locX = (buffer[7] << 8) + (buffer[8]);
int locY = (buffer[9] << 8) + (buffer[10]);
int locZ = (buffer[11] << 8) + (buffer[12]);
int rot = (buffer[13] << 8) + (buffer[14]);
printf("[TELEPORT (%i)] world: %d, room: %d, x: %d, y: %d, z: %d, r: %d\n", mestype, wid, rid, locX, locY, locZ, rot);
if (rid == 3 && wid == 1) {
unsigned char zato[] = {0x0b, 0xff, 0x0d, 0x06, 0x64, 0x6f, 0x73, 0x66, 0x6f, 0x78, 0x02};
send(sock, zato, sizeof(zato)/sizeof(*zato), 0);
unsigned char coot[] = {0x0c, 0xfe, 0x0c, 0x02, 0x04, 0x1a, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x59};
send(sock, coot, sizeof(coot)/sizeof(*coot), 0);
} else if (rid == 1) {
unsigned char muen[] = {0x0f, 0x01, 0x12, 0x00, 0x01, 0x00, 0x01, 0x04, 0x1a, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x59};
send(sock, muen, sizeof(muen)/sizeof(*muen), 0);
}
} else {
printf("[UNKNOWN (%i)] obj: %i, len: %i\n", mestype, objid, blength);
/*for (int x = 0; x < blength; x++) {
std::cout << " " << std::setfill('0') << std::setw(2) << std::hex << (0xff & (unsigned int)buffer[x]);
}
std::cout << std::endl;*/
}
}
return 0;
}