From 3b9b668c2b9d0f9803d34520a347d5dc9275db04 Mon Sep 17 00:00:00 2001 From: Wirlaburla Date: Sun, 24 Sep 2023 01:41:00 -0500 Subject: [PATCH] shits dynamic and cool now. --- src/client.h | 23 +++--- src/main.cpp | 185 +++++++++++++++++++++++++----------------------- src/var_error.h | 2 +- 3 files changed, 109 insertions(+), 101 deletions(-) diff --git a/src/client.h b/src/client.h index 928f4a3..0b70f55 100644 --- a/src/client.h +++ b/src/client.h @@ -18,14 +18,15 @@ int roomsock; std::thread aRecv_t; std::thread rRecv_t; -std::thread rKA_t; +std::thread rKeepAlive_t; -char* worldsserver = "209.240.84.122"; +uint8_t autoserver[4] = { 209, 240, 84, 122 }; uint16_t autoport = 6650; +uint8_t roomserver[4] = { 209, 240, 84, 122 }; uint16_t roomport = 5672; int protocol = 24; -char* version = "0000000000"; +char* version = "1000000000"; char* avatar = "avatar:pengo.mov"; int avatars = 253; char* room = "GroundZero#Reception"; @@ -42,28 +43,28 @@ uint16_t rot = 0; std::map properties; std::map objects; -std::map rooms; std::vector friends; std::vector mutes; +int deinit(int response); static char* trim(char *str); char* zero(int size); unsigned char* uzero(int size); void autoInit(); void roomInit(); void roomKeepAlive(); -void reciever(int sock_fd, bool autoserver); -void sessInit(int sock_fd, char* username, char* password); -void sessExit(int sock_fd); +void reciever(int *sock, uint16_t port); +void sessInit(int *sock, char* username, char* password); +void sessExit(int *sock); void readPropertyList(unsigned char* in); std::map readOldPropertyList(unsigned char* in); -void setAvatar(int sock_fd, char* avatar); -void roomIDReq(int sock_fd, char* room); -void teleport(int sock_fd, int x, int y, int z, int rot); +void setAvatar(int *sock, char* avatar); +void roomIDReq(int *sock, char* room); +void teleport(int *sock, int x, int y, int z, int rot); char* dimAdd(char* room); void userEnter(char id); void userExit(char id); -int wsend(int sock_fd, unsigned char str[], int flags); +int wsend(int *sock, unsigned char str[], int flags); #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 116e4e8..57584d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,19 +30,10 @@ static char* trim(char *str) { *(end+1) = '\0'; return str; } -struct sockaddr_in auto_addr; -struct sockaddr_in room_addr; + int main(int argc, char const* argv[]) { unsigned char bufout[BUFFERSIZE] = {0}; - auto_addr.sin_family = AF_INET; - inet_pton(AF_INET, worldsserver, &auto_addr.sin_addr); - auto_addr.sin_port = htons(autoport); - - room_addr.sin_family = AF_INET; - inet_pton(AF_INET, worldsserver, &room_addr.sin_addr); - room_addr.sin_port = htons(roomport); - initscr(); cbreak(); noecho(); @@ -127,7 +118,7 @@ int main(int argc, char const* argv[]) { autoInit(); } else if (strcmp(cmd, "disconnect") == 0) { if (aRecv_t.joinable()) aRecv_t.detach(); - wsend(autosock, new unsigned char[] {0x03, 0x01, CMD_SESSEXIT}, 0); + wsend(&autosock, new unsigned char[] {0x03, 0x01, CMD_SESSEXIT}, 0); } else if (strcmp(cmd, "login") == 0) { char username[32] = {0}; j = 0; while (msgoff < msglen && (s = inmsg[msgoff++]) != 0x20) { @@ -150,7 +141,7 @@ int main(int argc, char const* argv[]) { login_password[p] = password[p]; login_password[p]; wprintw(wlog, "\n*Logging in as %s.", username); - sessInit(autosock, username, password); + sessInit(&autosock, username, password); } else if ((strcmp(cmd, "w") == 0 || strcmp(cmd, "whisper") == 0) && srcLine < 0) { int k = 1; bufout[k++] = 0; @@ -178,7 +169,7 @@ int main(int argc, char const* argv[]) { bufout[k] = 0; bufout[0] = k; wprintw(wlog, "\n <- [%s] %s", username, message); - wsend(roomsock, bufout, 0); + wsend(&roomsock, bufout, 0); } else if (strcmp(cmd, "f") == 0 || strcmp(cmd, "friends") == 0) { char subcmd[4] = {0}; j = 0; @@ -213,7 +204,7 @@ int main(int argc, char const* argv[]) { bufout[k++] = 0x01; bufout[k] = 0; wprintw(wlog, "\n*%s added to friendslist.", username); - wsend(roomsock, bufout, 0); + wsend(&roomsock, bufout, 0); } else { wprintw(wlog, "\n*%s is already your friend!", username); } @@ -243,14 +234,14 @@ int main(int argc, char const* argv[]) { bufout[k++] = 0x00; bufout[k] = 0; wprintw(wlog, "\n*%s removed from friendslist.", username); - wsend(roomsock, bufout, 0); + wsend(&roomsock, bufout, 0); } else { wprintw(wlog, "\n*%s isn't on your friendslist.", username); } } else if (strcmp(subcmd, "list") == 0) { wprintw(wlog, "\n*Friendslist:"); for (std::vector::iterator v = friends.begin(); v != friends.end(); ++v) - wprintw(wlog, "%s %s", v==friends.begin()?",":"", *v); + wprintw(wlog, "%s %s", v!=friends.begin()?",":"", *v); } else { wprintw(wlog, "\n.friend [user]"); } @@ -304,7 +295,7 @@ int main(int argc, char const* argv[]) { } else if (strcmp(subcmd, "list") == 0) { wprintw(wlog, "\n*Mutelist:"); for (std::vector::iterator v = mutes.begin(); v != mutes.end(); ++v) - wprintw(wlog, "%s %s", v==mutes.begin()?",":"", *v); + wprintw(wlog, "%s %s", v!=mutes.begin()?",":"", *v); } else { wprintw(wlog, "\n.mute [user]"); } @@ -316,7 +307,7 @@ int main(int argc, char const* argv[]) { avatar[j] = 0; if (strlen(avatar) == 0) { wprintw(wlog, "\n*.avatar "); continue; } wprintw(wlog, "\n*Avatar set to: %s", avatar); - setAvatar(roomsock, avatar); + setAvatar(&roomsock, avatar); } else if (strcmp(cmd, "teleport") == 0) { char _x[6] = {0}; int j = 0; while (msgoff < msglen && (s = inmsg[msgoff++]) != 0x20) { @@ -353,12 +344,12 @@ int main(int argc, char const* argv[]) { } _room[j] = 0; if (strlen(_room) == 0) { - teleport(roomsock, xPos, yPos, zPos, rot); + teleport(&roomsock, xPos, yPos, zPos, rot); wprintw(wlog, "\n*Teleported to [%u,%u,%u,%u]", xPos, yPos, zPos, rot); } else { room = _room; wprintw(wlog, "\n*Teleporting to %s [%u,%u,%u,%u]", room, xPos, yPos, zPos, rot); - roomIDReq(roomsock, room); + roomIDReq(&roomsock, room); } } else if (strcmp(cmd, "dimension") == 0) { char dim[32] = {0}; int j = 0; @@ -369,7 +360,7 @@ int main(int argc, char const* argv[]) { if (strlen(dim) == 0) { wprintw(wlog, "\n*.dimension "); continue; } wprintw(wlog, "\n*Changing dimension: %s", dim); dimension = dim; - roomIDReq(roomsock, room); + roomIDReq(&roomsock, room); } else if (strcmp(cmd, "clear") == 0) { wclear(wlog); wmove(wlog, LINES-2, 0); @@ -381,10 +372,9 @@ int main(int argc, char const* argv[]) { _f=true; } } else if (strcmp(cmd, "logout") == 0) { - wsend(autosock, new unsigned char[] {0x03, 0x01, CMD_SESSEXIT}, 0); - wsend(roomsock, new unsigned char[] {0x03, 0x01, CMD_SESSEXIT}, 0); + sessExit(&roomsock); sessExit(&autosock); } else if (strcmp(cmd, "q") == 0) { - goto quit; + return deinit(0); } else { wprintw(wlog, "\n*Unknown command: \"%s\"", cmd); } @@ -408,7 +398,7 @@ int main(int argc, char const* argv[]) { bufout[k++] = inmsg[l]; bufout[k] = 0; bufout[0] = k; - wsend(roomsock, bufout, 0); + wsend(&roomsock, bufout, 0); } bufout[0] = 0; } @@ -432,16 +422,38 @@ int main(int argc, char const* argv[]) { wrefresh(wlog); refresh(); } - quit: unpost_form(form); free_form(form); free_field(entries[0]); endwin(); close(roomsock); - return 0; + return deinit(0); +} + +int deinit(int response) { + if (netstatus) { + sessExit(&roomsock); + sessExit(&autosock); + } + if (aRecv_t.joinable()) aRecv_t.detach(); + if (rRecv_t.joinable()) rRecv_t.detach(); + if (rKeepAlive_t.joinable()) rKeepAlive_t.detach(); + unpost_form(form); + free_form(form); + free_field(entries[0]); + endwin(); + close(autosock); + close(roomsock); + return response; } void autoInit() { + if (aRecv_t.joinable()) aRecv_t.detach(); + sockaddr_in auto_addr; + auto_addr.sin_family = AF_INET; + memcpy((void*)&auto_addr.sin_addr.s_addr, (void*)&autoserver, sizeof(roomserver)); + auto_addr.sin_port = htons(autoport); + if ((autosock = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror("AutoSock creation error"); @@ -449,39 +461,46 @@ void autoInit() { if (connect(autosock, (struct sockaddr*)&auto_addr, sizeof(auto_addr)) < 0) perror("AutoServ connection failed"); - wprintw(wlog, "\n*Connected to AutoServer %s:%i", worldsserver, autoport); - // Initialize connection. - aRecv_t = std::thread(reciever, autosock, true); - wsend(autosock, new unsigned char[] {0x03, 0xff, CMD_PROPREQ}, 0); + wprintw(wlog, "\n*Connected to AutoServer: %i.%i.%i.%i:%i", autoserver[1], autoserver[1], autoserver[2], autoserver[3], autoport); + aRecv_t = std::thread(reciever, &autosock, autoport); + wsend(&autosock, new unsigned char[] {0x03, 0xff, CMD_PROPREQ}, 0); wrefresh(wlog); netstatus = true; } void roomInit() { + if (rRecv_t.joinable()) rRecv_t.detach(); + if (rKeepAlive_t.joinable()) rKeepAlive_t.detach(); + sockaddr_in room_addr; + room_addr.sin_family = AF_INET; + memcpy((void*)&room_addr.sin_addr.s_addr, (void*)&roomserver, sizeof(roomserver)); + room_addr.sin_port = htons(roomport); + if ((roomsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror("AutoSock creation error"); if ((connect(roomsock, (struct sockaddr*)&room_addr, sizeof(room_addr))) < 0) perror("RoomServ connection failed"); - wprintw(wlog, "\n*Connected to RoomServer %s:%i", worldsserver, roomport); - rRecv_t = std::thread(reciever, roomsock, false); - rKA_t = std::thread(roomKeepAlive); - sessInit(roomsock, login_username, login_password); - login_password[0] = 0; + + wsend(&autosock, new unsigned char[] {0x03, 0xff, CMD_PROPREQ}, 0); + wprintw(wlog, "\n*Connected to RoomServer: %i.%i.%i.%i:%i", roomserver[1], roomserver[1], roomserver[2], roomserver[3], roomport); + rRecv_t = std::thread(reciever, &roomsock, roomport); + rKeepAlive_t = std::thread(roomKeepAlive); + sessInit(&roomsock, login_username, login_password); wrefresh(wlog); } void roomKeepAlive() { while (netstatus) { - teleport(roomsock, xPos, yPos, zPos, rot); + teleport(&roomsock, xPos, yPos, zPos, rot); sleep(5); } } -void reciever(int sock_fd, bool autoserver = false) { +void reciever(int *sock, uint16_t port) { unsigned char bufin[BUFFERSIZE] = {}; unsigned char bufout[BUFFERSIZE] = {}; - while (read(sock_fd, bufin, sizeof(bufin)) > 0) { + while (read(*sock, bufin, sizeof(bufin)) > 0) { int p = 0; more_in_buffer: int buflen = bufin[p]; @@ -550,22 +569,21 @@ void reciever(int sock_fd, bool autoserver = false) { objects[shortID] = newDrone; } } else if (bufin[p+2] == CMD_SESSEXIT && bufin[p+1] == 0x01) { - close(sock_fd); - if (autoserver) { + close(*sock); + if (port == autoport) { wprintw(wlog, "\n*Logged out of %s.", login_username); - memset(&(login_password[0]), 0, sizeof(login_password)); // Clear properties.erase(properties.begin(), properties.end()); objects.erase(objects.begin(), objects.end()); - rooms.erase(rooms.begin(), rooms.end()); friends.clear(); mutes.clear(); + autosock = -1; aRecv_t.detach(); netstatus = false; - wprintw(wlog, "\n*Disconnected!", sock_fd); - } else { + wprintw(wlog, "\n*Disconnected!"); + } else if (port == roomport) { wprintw(wlog, "\n*Leaving room %s<%s>", room, dimension); roomID = 1; room = "GroundZero#Reception"; @@ -574,9 +592,6 @@ void reciever(int sock_fd, bool autoserver = false) { yPos = 0; zPos = 0; rot = 0; - - rRecv_t.detach(); - rKA_t.detach(); } } else if (bufin[p+2] == CMD_SESSINIT && bufin[p+1] == 0x01) { std::map props = readOldPropertyList(&bufin[p]); @@ -602,19 +617,16 @@ void reciever(int sock_fd, bool autoserver = false) { } wprintw(wlog, "\n*Error %i: %s", err, errMsg); - if (autoserver) { - aRecv_t.detach(); + if (port == autoport) { netstatus = false; } else { - rRecv_t.detach(); - rKA_t.detach(); + } } else { - if (!autoserver) { - memset(&(login_password[0]), 0, sizeof(login_password)); - setAvatar(sock_fd, avatar); + if (port != autoport) { + setAvatar(sock, avatar); } else { - roomIDReq(sock_fd, room); + roomIDReq(sock, room); } } } else if ((bufin[p+2] == CMD_ROOMID || bufin[p+2] == 0x1A) && bufin[p+1] == 0x01) { @@ -626,14 +638,14 @@ void reciever(int sock_fd, bool autoserver = false) { longID[long_len+1] = 0; offs+=long_len; shortID = ((uint16_t)bufin[offs++] << 8) | bufin[offs++]; - rooms[longID] = shortID; roomID = shortID; - if (autoserver) roomInit(); + memcpy(&roomserver, new uint8_t[4]{ bufin[offs++], bufin[offs++], bufin[offs++], bufin[offs++] }, 4); + roomport = (bufin[offs++] << 8) | bufin[offs++]; + if (port != autoport) sessExit(sock); wprintw(wlog, "\n*Joining room %s", longID); - for (const auto& [key, value] : objects) { - objects.erase((char)key); - } - teleport(sock_fd, xPos, yPos, zPos, rot); + objects.erase(objects.begin(), objects.end()); + roomInit(); + teleport(sock, xPos, yPos, zPos, rot); } else if (bufin[p+2] == CMD_BUDDYNTF && bufin[p+1] == 0x01) { char* username = new char[32]; int offs = p+3; @@ -675,7 +687,7 @@ void reciever(int sock_fd, bool autoserver = false) { wrefresh(wlog); } -void sessInit(int sock_fd, char* name, char* pass) { +void sessInit(int *sock, char* name, char* pass) { unsigned char bufout[BUFFERSIZE] = {0}; bufout[1] = 0x01; bufout[2] = CMD_SESSINIT; @@ -722,15 +734,15 @@ void sessInit(int sock_fd, char* name, char* pass) { bufout[0] = l; bufout[l+1] = 0; - wsend(sock_fd, bufout, 0); + wsend(sock, bufout, 0); } -void sessExit(int sock_fd) { +void sessExit(int *sock) { unsigned char bufout[BUFFERSIZE] = {0}; bufout[0] = 0x03; bufout[1] = 0x01; bufout[2] = CMD_SESSEXIT; - wsend(sock_fd, bufout, 0); + wsend(sock, bufout, 0); } void constructPropertyList(int type, std::map props, unsigned char* snd) { @@ -792,7 +804,7 @@ void readPropertyList(unsigned char* in) { } */ -void setAvatar(int sock_fd, char* avatar) { +void setAvatar(int *sock, char* avatar) { unsigned char bufav[BUFFERSIZE] = {0}; int l = 1; bufav[l++] = 0x01; @@ -808,29 +820,24 @@ void setAvatar(int sock_fd, char* avatar) { bufav[0] = l; bufav[l+1] = 0; - wsend(sock_fd, bufav, 0); + wsend(sock, bufav, 0); } -void roomIDReq(int sock_fd, char* room) { +void roomIDReq(int *sock, char* room) { char* fullRoom = dimAdd(room); - // Don't do this, rooms with new IDs won't update. - //if (rooms.find(fullRoom) != rooms.end()) { - // roomID = rooms[fullRoom]; - //} else { - unsigned char bufrm[BUFFERSIZE] = {0}; - bufrm[1] = 1; - bufrm[2] = CMD_ROOMIDREQ; - bufrm[3] = strlen(fullRoom); - int x = 4; - for (int z = 0; z < strlen(fullRoom); z++) - bufrm[x++] = fullRoom[z]; - bufrm[x++] = 0; - bufrm[0] = x; - wsend(sock_fd, bufrm, 0); - //} + unsigned char bufrm[BUFFERSIZE] = {0}; + bufrm[1] = 1; + bufrm[2] = CMD_ROOMIDREQ; + bufrm[3] = strlen(fullRoom); + int x = 4; + for (int z = 0; z < strlen(fullRoom); z++) + bufrm[x++] = fullRoom[z]; + bufrm[x++] = 0; + bufrm[0] = x; + wsend(sock, bufrm, 0); } -void teleport(int sock_fd, int x, int y, int z, int rot) { +void teleport(int *sock, int x, int y, int z, int rot) { unsigned char buftp[16] = {0}; uint8_t _roomID[2]; uint8_t _x[2]; @@ -853,7 +860,7 @@ void teleport(int sock_fd, int x, int y, int z, int rot) { buftp[12] = _z[0]; buftp[11] = _z[1]; buftp[14] = _rot[0]; buftp[13] = _rot[1]; - wsend(sock_fd, buftp, 0); + wsend(sock, buftp, 0); } char* dimAdd(char* room) { @@ -883,6 +890,6 @@ void userExit(char id) { } } -int wsend(int sock_fd, unsigned char str[], int flags) { - return send(sock_fd, str, str[0], flags); -} \ No newline at end of file +int wsend(int *sock, unsigned char str[], int flags) { + return send(*sock, str, str[0], flags); +} diff --git a/src/var_error.h b/src/var_error.h index b05144b..88dc925 100644 --- a/src/var_error.h +++ b/src/var_error.h @@ -16,7 +16,7 @@ #define VAR_BAD_ACCOUNT 14 #define VAR_NOT_LOGGEDON 15 #define VAR_BAD_IPADDRESS 16 -#define VAR_LOGGEDON 16 +#define VAR_LOGGEDON 17 #define VAR_ROOM_FULL 21 #define VAR_UNEXPECTED 100 | 101 | 102 | 103 #define VAR_UNREACHABLE 104 | 105 | 106 | 107