[2488852] | 1 | #include <cstdlib>
|
---|
[371ce29] | 2 | #include <cstdio>
|
---|
[e3535b3] | 3 | #include <unistd.h>
|
---|
[2488852] | 4 | #include <string>
|
---|
[e3535b3] | 5 | #include <iostream>
|
---|
[3b1efcc] | 6 | #include <sstream>
|
---|
[2488852] | 7 | #include <vector>
|
---|
| 8 | #include <algorithm>
|
---|
[e3535b3] | 9 |
|
---|
[371ce29] | 10 | #include <fcntl.h>
|
---|
| 11 | #include <assert.h>
|
---|
| 12 |
|
---|
[73f75c1] | 13 | #include <sys/socket.h>
|
---|
[371ce29] | 14 | #include <netdb.h>
|
---|
[73f75c1] | 15 | #include <netinet/in.h>
|
---|
| 16 | #include <arpa/inet.h>
|
---|
| 17 |
|
---|
[e3535b3] | 18 | #include <openssl/bio.h>
|
---|
| 19 | #include <openssl/ssl.h>
|
---|
| 20 | #include <openssl/err.h>
|
---|
| 21 |
|
---|
[b53c6b3] | 22 | #include "../common/Compiler.h"
|
---|
| 23 | #include "../common/Message.h"
|
---|
[3b1efcc] | 24 | #include "../common/Common.h"
|
---|
[b53c6b3] | 25 |
|
---|
[8e540f4] | 26 | #include "Player.h"
|
---|
[36082e8] | 27 | #include "DataAccess.h"
|
---|
[d2b411a] | 28 |
|
---|
[e3535b3] | 29 | using namespace std;
|
---|
| 30 |
|
---|
[3b1efcc] | 31 | bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg);
|
---|
[8e540f4] | 32 |
|
---|
[73f75c1] | 33 | // this should probably go somewhere in the common folder
|
---|
[e3535b3] | 34 | void error(const char *msg)
|
---|
| 35 | {
|
---|
| 36 | perror(msg);
|
---|
| 37 | exit(0);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[8e540f4] | 40 | Player *findPlayerByName(vector<Player> &vec, string name)
|
---|
[2488852] | 41 | {
|
---|
[8e540f4] | 42 | vector<Player>::iterator it;
|
---|
[2488852] | 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 |
|
---|
[8e540f4] | 53 | Player *findPlayerByAddr(vector<Player> &vec, const sockaddr_in &addr)
|
---|
[73f75c1] | 54 | {
|
---|
[8e540f4] | 55 | vector<Player>::iterator it;
|
---|
[73f75c1] | 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 |
|
---|
[e3535b3] | 67 | int main(int argc, char *argv[])
|
---|
| 68 | {
|
---|
| 69 | int sock, length, n;
|
---|
| 70 | struct sockaddr_in server;
|
---|
[3b1efcc] | 71 | struct sockaddr_in from; // info of client sending the message
|
---|
[e084950] | 72 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[8e540f4] | 73 | vector<Player> vctPlayers;
|
---|
[e084950] | 74 |
|
---|
[e3535b3] | 75 | SSL_load_error_strings();
|
---|
| 76 | ERR_load_BIO_strings();
|
---|
| 77 | OpenSSL_add_all_algorithms();
|
---|
| 78 |
|
---|
| 79 | if (argc < 2) {
|
---|
[73f75c1] | 80 | cerr << "ERROR, no port provided" << endl;
|
---|
| 81 | exit(1);
|
---|
[e3535b3] | 82 | }
|
---|
[41ad8ed] | 83 |
|
---|
[371ce29] | 84 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
[e3535b3] | 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]));
|
---|
[2488852] | 90 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 91 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 92 | error("binding");
|
---|
[73f75c1] | 93 |
|
---|
[371ce29] | 94 | set_nonblock(sock);
|
---|
| 95 |
|
---|
[da692b9] | 96 | bool broadcastResponse;
|
---|
[cb1f288] | 97 | while (true) {
|
---|
[371ce29] | 98 |
|
---|
| 99 | usleep(5000);
|
---|
| 100 |
|
---|
[e084950] | 101 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
[8e540f4] | 102 |
|
---|
[371ce29] | 103 | if (n >= 0) {
|
---|
| 104 | cout << "Got a message" << endl;
|
---|
[8e540f4] | 105 |
|
---|
[da692b9] | 106 | broadcastResponse = processMessage(clientMsg, from, vctPlayers, serverMsg);
|
---|
[371ce29] | 107 |
|
---|
| 108 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
| 109 |
|
---|
[da692b9] | 110 | if (broadcastResponse)
|
---|
[3b1efcc] | 111 | {
|
---|
[da692b9] | 112 | cout << "Should be broadcasting the message" << endl;
|
---|
| 113 |
|
---|
[3b1efcc] | 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 | {
|
---|
[da692b9] | 124 | cout << "Should be sending back the message" << endl;
|
---|
| 125 |
|
---|
[3b1efcc] | 126 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
| 127 | error("sendMessage");
|
---|
| 128 | }
|
---|
[371ce29] | 129 | }
|
---|
[8e540f4] | 130 |
|
---|
| 131 | }
|
---|
[371ce29] | 132 |
|
---|
[8e540f4] | 133 | return 0;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[3b1efcc] | 136 | bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)
|
---|
[8e540f4] | 137 | {
|
---|
[41ad8ed] | 138 | DataAccess da;
|
---|
| 139 |
|
---|
[8e540f4] | 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 |
|
---|
[da692b9] | 145 | // maybe we should make a message class and have this be a member
|
---|
[3b1efcc] | 146 | bool broadcastResponse = false;
|
---|
| 147 |
|
---|
[8e540f4] | 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:
|
---|
[d2b411a] | 153 | {
|
---|
[8e540f4] | 154 | string username(clientMsg.buffer);
|
---|
| 155 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[d2b411a] | 156 |
|
---|
[8e540f4] | 157 | cout << "username: " << username << endl;
|
---|
| 158 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 159 |
|
---|
[3b1efcc] | 160 | int error = da.insertPlayer(username, password);
|
---|
[41ad8ed] | 161 |
|
---|
[3b1efcc] | 162 | if (!error)
|
---|
| 163 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 164 | else
|
---|
| 165 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
[8e540f4] | 166 |
|
---|
| 167 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[d2b411a] | 168 |
|
---|
[8e540f4] | 169 | break;
|
---|
| 170 | }
|
---|
| 171 | case MSG_TYPE_LOGIN:
|
---|
| 172 | {
|
---|
| 173 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 174 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 175 | cout << "Player logging in: " << username << endl;
|
---|
| 176 |
|
---|
[41ad8ed] | 177 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 178 |
|
---|
[41ad8ed] | 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
|
---|
[8e540f4] | 188 | {
|
---|
[59061f6] | 189 | Player newP(username, "");
|
---|
| 190 | newP.setAddr(from);
|
---|
| 191 |
|
---|
| 192 | vctPlayers.push_back(newP);
|
---|
[da692b9] | 193 | strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players.");
|
---|
[07028b9] | 194 | }
|
---|
| 195 |
|
---|
[8e540f4] | 196 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[41ad8ed] | 197 |
|
---|
| 198 | delete(p);
|
---|
[07028b9] | 199 |
|
---|
[8e540f4] | 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);
|
---|
[633f42a] | 208 |
|
---|
[8e540f4] | 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.");
|
---|
[07028b9] | 212 | }
|
---|
[8e540f4] | 213 | else if( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 214 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 215 | {
|
---|
[8e540f4] | 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.");
|
---|
[2488852] | 217 | }
|
---|
[8e540f4] | 218 | else
|
---|
[2488852] | 219 | {
|
---|
[8e540f4] | 220 | vctPlayers.erase((vector<Player>::iterator)p);
|
---|
[41ad8ed] | 221 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
[8e540f4] | 222 | }
|
---|
[07028b9] | 223 |
|
---|
[8e540f4] | 224 | break;
|
---|
| 225 | }
|
---|
| 226 | case MSG_TYPE_CHAT:
|
---|
| 227 | {
|
---|
[da692b9] | 228 | cout << "Got a chat message" << endl;
|
---|
| 229 |
|
---|
[8e540f4] | 230 | Player *p = findPlayerByAddr(vctPlayers, from);
|
---|
[07028b9] | 231 |
|
---|
[8e540f4] | 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.");
|
---|
[2488852] | 235 | }
|
---|
[8e540f4] | 236 | else
|
---|
| 237 | {
|
---|
[3b1efcc] | 238 | broadcastResponse = true;
|
---|
| 239 |
|
---|
| 240 | stringstream ss;
|
---|
[da692b9] | 241 | ss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 242 |
|
---|
| 243 | strcpy(serverMsg.buffer, ss.str().c_str());
|
---|
[8e540f4] | 244 | }
|
---|
| 245 |
|
---|
| 246 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 247 |
|
---|
| 248 | break;
|
---|
[e084950] | 249 | }
|
---|
[8e540f4] | 250 | default:
|
---|
| 251 | {
|
---|
| 252 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 253 |
|
---|
[8e540f4] | 254 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[e084950] | 255 |
|
---|
[8e540f4] | 256 | break;
|
---|
| 257 | }
|
---|
[e3535b3] | 258 | }
|
---|
[da692b9] | 259 |
|
---|
| 260 | return broadcastResponse;
|
---|
[e3535b3] | 261 | }
|
---|
[da692b9] | 262 |
|
---|