[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 |
|
---|
[3b1efcc] | 96 | bool broadcastMessage;
|
---|
[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 |
|
---|
[3b1efcc] | 106 | broadcastMessage = processMessage(clientMsg, from, vctPlayers, serverMsg);
|
---|
[371ce29] | 107 |
|
---|
| 108 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
| 109 |
|
---|
[3b1efcc] | 110 | if (broadcastMessage)
|
---|
| 111 | {
|
---|
| 112 | vector<Player>::iterator it;
|
---|
| 113 |
|
---|
| 114 | for (it = vctPlayers.begin(); it != vctPlayers.end(); it++)
|
---|
| 115 | {
|
---|
| 116 | if ( sendMessage(&serverMsg, sock, &(it->addr)) < 0 )
|
---|
| 117 | error("sendMessage");
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | else
|
---|
| 121 | {
|
---|
| 122 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
| 123 | error("sendMessage");
|
---|
| 124 | }
|
---|
[371ce29] | 125 | }
|
---|
[8e540f4] | 126 |
|
---|
| 127 | }
|
---|
[371ce29] | 128 |
|
---|
[8e540f4] | 129 | return 0;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[3b1efcc] | 132 | bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)
|
---|
[8e540f4] | 133 | {
|
---|
[41ad8ed] | 134 | DataAccess da;
|
---|
| 135 |
|
---|
[8e540f4] | 136 | cout << "ip address: " << inet_ntoa(from.sin_addr) << endl;
|
---|
| 137 | cout << "port: " << from.sin_port << endl;
|
---|
| 138 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
| 139 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
| 140 |
|
---|
[3b1efcc] | 141 | bool broadcastResponse = false;
|
---|
| 142 |
|
---|
[8e540f4] | 143 | // Check that if an invalid message is sent, the client will correctly
|
---|
| 144 | // receive and display the response. Maybe make a special error msg type
|
---|
| 145 | switch(clientMsg.type)
|
---|
| 146 | {
|
---|
| 147 | case MSG_TYPE_REGISTER:
|
---|
[d2b411a] | 148 | {
|
---|
[8e540f4] | 149 | string username(clientMsg.buffer);
|
---|
| 150 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[d2b411a] | 151 |
|
---|
[8e540f4] | 152 | cout << "username: " << username << endl;
|
---|
| 153 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 154 |
|
---|
[3b1efcc] | 155 | int error = da.insertPlayer(username, password);
|
---|
[41ad8ed] | 156 |
|
---|
[3b1efcc] | 157 | if (!error)
|
---|
| 158 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 159 | else
|
---|
| 160 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
[8e540f4] | 161 |
|
---|
| 162 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[d2b411a] | 163 |
|
---|
[8e540f4] | 164 | break;
|
---|
| 165 | }
|
---|
| 166 | case MSG_TYPE_LOGIN:
|
---|
| 167 | {
|
---|
| 168 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 169 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 170 | cout << "Player logging in: " << username << endl;
|
---|
| 171 |
|
---|
[41ad8ed] | 172 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 173 |
|
---|
[41ad8ed] | 174 | if (p == NULL || p->password != password)
|
---|
| 175 | {
|
---|
| 176 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
| 177 | }
|
---|
| 178 | else if(findPlayerByName(vctPlayers, username) != NULL)
|
---|
| 179 | {
|
---|
| 180 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 181 | }
|
---|
| 182 | else
|
---|
[8e540f4] | 183 | {
|
---|
[59061f6] | 184 | Player newP(username, "");
|
---|
| 185 | newP.setAddr(from);
|
---|
| 186 |
|
---|
| 187 | vctPlayers.push_back(newP);
|
---|
[3b1efcc] | 188 | strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with outher players.");
|
---|
[07028b9] | 189 | }
|
---|
| 190 |
|
---|
[8e540f4] | 191 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[41ad8ed] | 192 |
|
---|
| 193 | delete(p);
|
---|
[07028b9] | 194 |
|
---|
[8e540f4] | 195 | break;
|
---|
| 196 | }
|
---|
| 197 | case MSG_TYPE_LOGOUT:
|
---|
| 198 | {
|
---|
| 199 | string name(clientMsg.buffer);
|
---|
| 200 | cout << "Player logging out: " << name << endl;
|
---|
| 201 |
|
---|
| 202 | Player *p = findPlayerByName(vctPlayers, name);
|
---|
[633f42a] | 203 |
|
---|
[8e540f4] | 204 | if (p == NULL)
|
---|
| 205 | {
|
---|
| 206 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
[07028b9] | 207 | }
|
---|
[8e540f4] | 208 | else if( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 209 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 210 | {
|
---|
[8e540f4] | 211 | 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] | 212 | }
|
---|
[8e540f4] | 213 | else
|
---|
[2488852] | 214 | {
|
---|
[8e540f4] | 215 | vctPlayers.erase((vector<Player>::iterator)p);
|
---|
[41ad8ed] | 216 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
[8e540f4] | 217 | }
|
---|
[07028b9] | 218 |
|
---|
[8e540f4] | 219 | break;
|
---|
| 220 | }
|
---|
| 221 | case MSG_TYPE_CHAT:
|
---|
| 222 | {
|
---|
| 223 | Player *p = findPlayerByAddr(vctPlayers, from);
|
---|
[07028b9] | 224 |
|
---|
[8e540f4] | 225 | if (p == NULL)
|
---|
| 226 | {
|
---|
| 227 | 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] | 228 | }
|
---|
[8e540f4] | 229 | else
|
---|
| 230 | {
|
---|
[3b1efcc] | 231 | broadcastResponse = true;
|
---|
| 232 |
|
---|
| 233 | stringstream ss;
|
---|
| 234 | ss << p->name << ": " << clientMsg.buffer << endl;
|
---|
| 235 |
|
---|
| 236 | strcpy(serverMsg.buffer, ss.str().c_str());
|
---|
[8e540f4] | 237 | }
|
---|
| 238 |
|
---|
| 239 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 240 |
|
---|
| 241 | break;
|
---|
[e084950] | 242 | }
|
---|
[8e540f4] | 243 | default:
|
---|
| 244 | {
|
---|
| 245 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 246 |
|
---|
[8e540f4] | 247 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[e084950] | 248 |
|
---|
[8e540f4] | 249 | break;
|
---|
| 250 | }
|
---|
[3b1efcc] | 251 |
|
---|
| 252 | return broadcastResponse;
|
---|
[e3535b3] | 253 | }
|
---|
| 254 | }
|
---|