Finished project on Windows

This commit is contained in:
Gregory 2023-10-04 01:10:24 -05:00
parent 15ed6e5c7c
commit 0ad810a19f
6 changed files with 69 additions and 17 deletions

1
.gitignore vendored
View File

@ -35,3 +35,4 @@
# Extras
build
test/web_server
.vscode

View File

@ -8,3 +8,7 @@ add_executable(web_server
./network.cpp
)
if(WIN32)
target_link_libraries(proxy wsock32 ws2_32)
target_link_libraries(web_server wsock32 ws2_32)
endif()

View File

@ -1,5 +1,7 @@
#include "network.hpp"
#ifdef UNIX
#include <arpa/inet.h>
#endif
#include <fcntl.h>
#include <stdlib.h> // Needed for exit()
#include <cstring>
@ -35,8 +37,14 @@ void PipeSockets(int sender_s, int receiver_s) {
send(sender_s, in_buf, strlen(in_buf), 0);
strcpy(in_buf, MESS_403);
send(sender_s, in_buf, strlen(in_buf), 0);
#ifdef WIN
closesocket(sender_s);
closesocket(receiver_s);
#endif
#ifdef UNIX
close(sender_s);
close(receiver_s);
#endif
return;
}
@ -72,8 +80,14 @@ void ProxySockets(int sender_s, int receiver_s) {
send(receiver_s, out_buf, strlen(out_buf), 0);
strcpy(out_buf, MESS_403);
send(receiver_s, out_buf, strlen(out_buf), 0);
#ifdef WIN
closesocket(sender_s);
closesocket(receiver_s);
#endif
#ifdef UNIX
close(sender_s);
close(receiver_s);
#endif
return;
}
@ -83,8 +97,14 @@ void ProxySockets(int sender_s, int receiver_s) {
std::cout << "LOG (info) - proxy packet send size: " << buf_len << '\n';
} while ((buf_len == BUF_SIZE) || (buf_len == 40));
std::cout << "Sent to browser" << std::endl;
#ifdef WIN
closesocket(sender_s);
closesocket(receiver_s);
#endif
#ifdef UNIX
close(sender_s);
close(receiver_s);
#endif
}
Client::Client(void) {
@ -116,9 +136,13 @@ bool Client::ConnectTo(int portNumber) {
Server::Server(int portNumber) {
std::cout << "Opening the server" << std::endl;
#ifdef WIN
socketFD = socket(AF_INET, SOCK_STREAM, 0);
if (socketFD == -1)
{
#endif
#ifdef UNIX
socketFD = socket(AF_INET, SOCK_STREAM, 0);
#endif
if (socketFD == -1) {
std::cerr << "ERROR - Unable to create socket on server" << std::endl;
exit(1);
}
@ -126,8 +150,7 @@ Server::Server(int portNumber) {
address.sin_port = htons(portNumber);
address.sin_addr.s_addr = htonl(INADDR_ANY);
// Create a socket, fill-in address information, and then bind it
if (bind(socketFD, (struct sockaddr *)&address, sizeof(address)) == -1)
{
if (bind(socketFD, (struct sockaddr *)&address, sizeof(address)) == -1) {
std::cerr << "ERROR - Unable to bind socket" << std::endl;
exit(1);
}
@ -138,5 +161,11 @@ Server::Server(int portNumber) {
void Server::Close(void) {
std::cout << "Closing the server" << std::endl;
#ifdef WIN
closesocket(socketFD);
WSACleanup();
#endif
#ifdef UNIX
close(socketFD);
#endif
}

View File

@ -1,7 +1,15 @@
#ifndef NETWORK_HPP
#define NETWORK_HPP
#define WIN // WIN for Windows environment, UNIX for BSD or LINUX env.
#ifdef UNIX
#include <netinet/in.h>
#endif
#ifdef WIN
#include <winsock.h>
#include <ws2tcpip.h>
#endif
#include <unistd.h>
#define BUF_SIZE 4096 // Buffer size (big enough for a GET)

View File

@ -8,6 +8,18 @@
int main(void)
{
/* FOR WIN ------------------------------------------------------------- */
#ifdef WIN
WORD wVersionRequested = MAKEWORD(1, 1); // Stuff for WSA functions
WSADATA wsaData;
int wsResult; // Stuff for WSA functions
wsResult = WSAStartup(wVersionRequested, &wsaData);
if (wsResult != 0) {
printf("WSAStartup failed: %d\n", wsResult);
return 1;
}
#endif
/* --------------------------------------------------------------------- */
strcpy(hazardous_contents_CS_01, "password.txt");
strcpy(hazardous_contents_CS_02, "admin.config");
std::vector<std::future<void>> pending_futures;

View File

@ -28,7 +28,6 @@
//= History: KJC (12/29/00) - Genesis (from server.c) =
//= HF (01/25/01) - Ported to multi-platform environment =
//=============================================================================
#define UNIX // WIN for Windows environment, UNIX for BSD or LINUX env.
//----- Include files ---------------------------------------------------------
#include <string.h> // Needed for strcpy() and strlen()
@ -51,13 +50,7 @@
/* FOR WIN ---------------------------------------------------------------- */
#ifdef WIN
#include <stddef.h> // Needed for _threadid
#include <process.h> // Needed for _beginthread() and _endthread()
#include <io.h> // Needed for open(), close(), and eof()
#include <winsock2.h>
#include <windows.h> // Needed for all Winsock stuff
// Lazy struct fixes
typedef int32_t socklen_t;
#endif
/* ------------------------------------------------------------------------ */
@ -82,18 +75,13 @@ int main(void)
#ifdef WIN
WORD wVersionRequested = MAKEWORD(1, 1); // Stuff for WSA functions
WSADATA wsaData; // Stuff for WSA functions
WSAStartup(wVersionRequested, &wsaData);
#endif
/* --------------------------------------------------------------------- */
std::vector<std::future<void>> pending_futures;
Server webserver(kWebserverPort);
Client proxy;
/* FOR WIN ------------------------------------------------------------- */
#ifdef WIN
// Initialize winsock
WSAStartup(wVersionRequested, &wsaData);
#endif
/* --------------------------------------------------------------------- */
// Main loop to listen, accept, and then spin-off a thread to handle the GET
while (1)
@ -168,9 +156,19 @@ void ClientRequest(int client_s) {
std::cout << "LOG (info) - webserver send size: " << buf_len << '\n';
} while (buf_len == BUF_SIZE);
std::cout << std::endl;
#ifdef WIN
closesocket(fh);
#endif
#ifdef UNIX
close(fh);
#endif
}
// Close the client socket and end the thread
#ifdef WIN
closesocket(client_s);
#endif
#ifdef UNIX
close(client_s);
#endif
}