| 1 | #include <cstdlib>
|
|---|
| 2 | #include <cstdio>
|
|---|
| 3 | #include <unistd.h>
|
|---|
| 4 | #include <string>
|
|---|
| 5 | #include <iostream>
|
|---|
| 6 | #include <sstream>
|
|---|
| 7 | #include <vector>
|
|---|
| 8 | #include <algorithm>
|
|---|
| 9 |
|
|---|
| 10 | #include <fcntl.h>
|
|---|
| 11 | #include <assert.h>
|
|---|
| 12 |
|
|---|
| 13 | #include <sys/socket.h>
|
|---|
| 14 | #include <netdb.h>
|
|---|
| 15 | #include <netinet/in.h>
|
|---|
| 16 | #include <arpa/inet.h>
|
|---|
| 17 |
|
|---|
| 18 | #include <openssl/bio.h>
|
|---|
| 19 | #include <openssl/ssl.h>
|
|---|
| 20 | #include <openssl/err.h>
|
|---|
| 21 |
|
|---|
| 22 | #include "../common/Compiler.h"
|
|---|
| 23 | #include "../common/Message.h"
|
|---|
| 24 | #include "../common/Common.h"
|
|---|
| 25 |
|
|---|
| 26 | #include "Player.h"
|
|---|
| 27 | #include "DataAccess.h"
|
|---|
| 28 |
|
|---|
| 29 | using namespace std;
|
|---|
| 30 |
|
|---|
| 31 | bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg);
|
|---|
| 32 |
|
|---|
| 33 | // this should probably go somewhere in the common folder
|
|---|
| 34 | void error(const char *msg)
|
|---|
| 35 | {
|
|---|
| 36 | perror(msg);
|
|---|
| 37 | exit(0);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | Player *findPlayerByName(vector<Player> &vec, string name)
|
|---|
| 41 | {
|
|---|
| 42 | vector<Player>::iterator it;
|
|---|
| 43 |
|
|---|
| 44 | for (it = vec.begin(); it != vec.end(); it++)
|
|---|
| 45 | {
|
|---|
| 46 | if ( it->name.compare(name) == 0 )
|
|---|
| 47 | return &(*it);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | return NULL;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | Player *findPlayerByAddr(vector<Player> &vec, const sockaddr_in &addr)
|
|---|
| 54 | {
|
|---|
| 55 | vector<Player>::iterator it;
|
|---|
| 56 |
|
|---|
| 57 | for (it = vec.begin(); it != vec.end(); it++)
|
|---|
| 58 | {
|
|---|
| 59 | if ( it->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
|---|
| 60 | it->addr.sin_port == addr.sin_port )
|
|---|
| 61 | return &(*it);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | return NULL;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | int main(int argc, char *argv[])
|
|---|
| 68 | {
|
|---|
| 69 | int sock, length, n;
|
|---|
| 70 | struct sockaddr_in server;
|
|---|
| 71 | struct sockaddr_in from; // info of client sending the message
|
|---|
| 72 | NETWORK_MSG clientMsg, serverMsg;
|
|---|
| 73 | vector<Player> vctPlayers;
|
|---|
| 74 |
|
|---|
| 75 | SSL_load_error_strings();
|
|---|
| 76 | ERR_load_BIO_strings();
|
|---|
| 77 | OpenSSL_add_all_algorithms();
|
|---|
| 78 |
|
|---|
| 79 | if (argc < 2) {
|
|---|
| 80 | cerr << "ERROR, no port provided" << endl;
|
|---|
| 81 | exit(1);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|---|
| 85 | if (sock < 0) error("Opening socket");
|
|---|
| 86 | length = sizeof(server);
|
|---|
| 87 | bzero(&server,length);
|
|---|
| 88 | server.sin_family=AF_INET;
|
|---|
| 89 | server.sin_port=htons(atoi(argv[1]));
|
|---|
| 90 | server.sin_addr.s_addr=INADDR_ANY;
|
|---|
| 91 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
|---|
| 92 | error("binding");
|
|---|
| 93 |
|
|---|
| 94 | set_nonblock(sock);
|
|---|
| 95 |
|
|---|
| 96 | bool broadcastResponse;
|
|---|
| 97 | while (true) {
|
|---|
| 98 |
|
|---|
| 99 | usleep(5000);
|
|---|
| 100 |
|
|---|
| 101 | n = receiveMessage(&clientMsg, sock, &from);
|
|---|
| 102 |
|
|---|
| 103 | if (n >= 0) {
|
|---|
| 104 | cout << "Got a message" << endl;
|
|---|
| 105 |
|
|---|
| 106 | broadcastResponse = processMessage(clientMsg, from, vctPlayers, serverMsg);
|
|---|
| 107 |
|
|---|
| 108 | cout << "msg: " << serverMsg.buffer << endl;
|
|---|
| 109 |
|
|---|
| 110 | if (broadcastResponse)
|
|---|
| 111 | {
|
|---|
| 112 | cout << "Should be broadcasting the message" << endl;
|
|---|
| 113 |
|
|---|
| 114 | vector<Player>::iterator it;
|
|---|
| 115 |
|
|---|
| 116 | for (it = vctPlayers.begin(); it != vctPlayers.end(); it++)
|
|---|
| 117 | {
|
|---|
| 118 | if ( sendMessage(&serverMsg, sock, &(it->addr)) < 0 )
|
|---|
| 119 | error("sendMessage");
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | else
|
|---|
| 123 | {
|
|---|
| 124 | cout << "Should be sending back the message" << endl;
|
|---|
| 125 |
|
|---|
| 126 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
|---|
| 127 | error("sendMessage");
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | return 0;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)
|
|---|
| 137 | {
|
|---|
| 138 | DataAccess da;
|
|---|
| 139 |
|
|---|
| 140 | cout << "ip address: " << inet_ntoa(from.sin_addr) << endl;
|
|---|
| 141 | cout << "port: " << from.sin_port << endl;
|
|---|
| 142 | cout << "MSG: type: " << clientMsg.type << endl;
|
|---|
| 143 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
|---|
| 144 |
|
|---|
| 145 | // maybe we should make a message class and have this be a member
|
|---|
| 146 | bool broadcastResponse = false;
|
|---|
| 147 |
|
|---|
| 148 | // Check that if an invalid message is sent, the client will correctly
|
|---|
| 149 | // receive and display the response. Maybe make a special error msg type
|
|---|
| 150 | switch(clientMsg.type)
|
|---|
| 151 | {
|
|---|
| 152 | case MSG_TYPE_REGISTER:
|
|---|
| 153 | {
|
|---|
| 154 | string username(clientMsg.buffer);
|
|---|
| 155 | string password(strchr(clientMsg.buffer, '\0')+1);
|
|---|
| 156 |
|
|---|
| 157 | cout << "username: " << username << endl;
|
|---|
| 158 | cout << "password: " << password << endl;
|
|---|
| 159 |
|
|---|
| 160 | int error = da.insertPlayer(username, password);
|
|---|
| 161 |
|
|---|
| 162 | if (!error)
|
|---|
| 163 | strcpy(serverMsg.buffer, "Registration successful.");
|
|---|
| 164 | else
|
|---|
| 165 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
|---|
| 166 |
|
|---|
| 167 | serverMsg.type = MSG_TYPE_REGISTER;
|
|---|
| 168 |
|
|---|
| 169 | break;
|
|---|
| 170 | }
|
|---|
| 171 | case MSG_TYPE_LOGIN:
|
|---|
| 172 | {
|
|---|
| 173 | string username(clientMsg.buffer);
|
|---|
| 174 | string password(strchr(clientMsg.buffer, '\0')+1);
|
|---|
| 175 | cout << "Player logging in: " << username << endl;
|
|---|
| 176 |
|
|---|
| 177 | Player* p = da.getPlayer(username);
|
|---|
| 178 |
|
|---|
| 179 | if (p == NULL || p->password != password)
|
|---|
| 180 | {
|
|---|
| 181 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
|---|
| 182 | }
|
|---|
| 183 | else if(findPlayerByName(vctPlayers, username) != NULL)
|
|---|
| 184 | {
|
|---|
| 185 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
|---|
| 186 | }
|
|---|
| 187 | else
|
|---|
| 188 | {
|
|---|
| 189 | Player newP(username, "");
|
|---|
| 190 | newP.setAddr(from);
|
|---|
| 191 |
|
|---|
| 192 | vctPlayers.push_back(newP);
|
|---|
| 193 | strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players.");
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | serverMsg.type = MSG_TYPE_LOGIN;
|
|---|
| 197 |
|
|---|
| 198 | delete(p);
|
|---|
| 199 |
|
|---|
| 200 | break;
|
|---|
| 201 | }
|
|---|
| 202 | case MSG_TYPE_LOGOUT:
|
|---|
| 203 | {
|
|---|
| 204 | string name(clientMsg.buffer);
|
|---|
| 205 | cout << "Player logging out: " << name << endl;
|
|---|
| 206 |
|
|---|
| 207 | Player *p = findPlayerByName(vctPlayers, name);
|
|---|
| 208 |
|
|---|
| 209 | if (p == NULL)
|
|---|
| 210 | {
|
|---|
| 211 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
|---|
| 212 | }
|
|---|
| 213 | else if( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
|---|
| 214 | p->addr.sin_port != from.sin_port )
|
|---|
| 215 | {
|
|---|
| 216 | strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
|
|---|
| 217 | }
|
|---|
| 218 | else
|
|---|
| 219 | {
|
|---|
| 220 | vctPlayers.erase((vector<Player>::iterator)p);
|
|---|
| 221 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | break;
|
|---|
| 225 | }
|
|---|
| 226 | case MSG_TYPE_CHAT:
|
|---|
| 227 | {
|
|---|
| 228 | cout << "Got a chat message" << endl;
|
|---|
| 229 |
|
|---|
| 230 | Player *p = findPlayerByAddr(vctPlayers, from);
|
|---|
| 231 |
|
|---|
| 232 | if (p == NULL)
|
|---|
| 233 | {
|
|---|
| 234 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
|---|
| 235 | }
|
|---|
| 236 | else
|
|---|
| 237 | {
|
|---|
| 238 | broadcastResponse = true;
|
|---|
| 239 |
|
|---|
| 240 | stringstream ss;
|
|---|
| 241 | ss << p->name << ": " << clientMsg.buffer;
|
|---|
| 242 |
|
|---|
| 243 | strcpy(serverMsg.buffer, ss.str().c_str());
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | serverMsg.type = MSG_TYPE_CHAT;
|
|---|
| 247 |
|
|---|
| 248 | break;
|
|---|
| 249 | }
|
|---|
| 250 | default:
|
|---|
| 251 | {
|
|---|
| 252 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
|---|
| 253 |
|
|---|
| 254 | serverMsg.type = MSG_TYPE_CHAT;
|
|---|
| 255 |
|
|---|
| 256 | break;
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | return broadcastResponse;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|