Syntax Fixes, program crashes with segmentation fault

This commit is contained in:
2024-05-09 16:07:04 -04:00
parent 7f3d251d35
commit 46d0315e0a
8 changed files with 99 additions and 145 deletions

View File

@@ -1,60 +0,0 @@
#pragma once
#include <iostream>
#include <string>
#include <netdb.h>
#include <memory>
namespace Socket {
class SocketException : public std::exception {
protected:
char * message;
public:
SocketException(std::string msg) : message(msg.c_str()) {}
SocketException(char *msg) : message(msg) {}
char * what() {
return message;
}
};
class SocketBindingException : public SocketException {
public:
SocketBindingException(char *msg) : SocketException{msg} {}
};
class SocketConnectException : public SocketException {
public:
SocketConnectException(char *msg) : SocketException{msg} {}
};
class SocketCreationException : public SocketException {
public:
SocketCreationException(char * msg) : SocketException{msg} {}
};
class SocketListenException : public SocketException {
public:
SocketListenException(char * msg) : SocketException{msg} {}
};
class SocketAcceptException : public SocketException {
public:
SocketAcceptException(char * msg) : SocketException{msg} {}
};
class SocketSendException : public SocketException {
public:
SocketSendException(char * msg) : SocketException{msg} {}
};
class SocketReceiveException : public SocketException {
public:
SocketReceiveException(char * msg) : SocketException{msg} {}
};
class SocketCloseException : public SocketException {
public:
SocketCloseException(char * msg) : SocketException{msg} {}
};
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include <Sockets/Sockets.hpp>
#include <cstring>
#include <array>
#include <string>
@@ -17,8 +18,8 @@ namespace Socket
typedef struct sockaddr sockaddr_t;
struct IPAddress
{
std::array<uint8_t, 4> octets{};
uint16_t port{};
std::array<u8, 4> octets{};
u16 port{};
public:
static IPAddress Any(uint16_t portno) { return IPAddress{INADDR_ANY, portno};}
@@ -26,14 +27,14 @@ namespace Socket
static IPAddress Broadcast(uint16_t portno) { return IPAddress{INADDR_BROADCAST, portno};}
// Tries to resolve an IP address from a URL
static IPAddress Resolve(std::string const& uri, uint16_t port);
static IPAddress Resolve(std::string const& uri, u16 port);
IPAddress() {}
IPAddress(const std::string& ipaddr, uint16_t port);
IPAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t portno);
IPAddress(const std::string& ipaddr, u16 port);
IPAddress(u8 a, u8 b, u8 c, u8 d, u16 port);
IPAddress(const sockaddr_in_t& addr_in);
operator sockaddr_in_t() const;
const uint8_t& operator[](size_t octet) const;
explicit operator sockaddr_in_t() const;
const u8& operator[](size_t octet) const;
bool operator == (const IPAddress& other) const;
bool operator != (const IPAddress& other) const;
[[nodiscard]] std::string addr_string() const;

View File

@@ -9,6 +9,7 @@
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <Sockets/Sockets.hpp>
#include <Sockets/IPAddress.hpp>
@@ -36,10 +37,10 @@ namespace Socket {
void Connect(std::string address, uint16_t port);
void Listen(int maxQueue);
std::shared_ptr<TcpSocket> Accept();
void Send(const char *data, unsigned int length, int flags);
void Send(const u8 *data, unsigned int length, int flags);
// Receive data (blocking)
// @return true if socket is still open, false otherwise
bool Receive(char* msg, int len, int flags);
bool Receive(u8* msg, int len, int flags);
void Close();
private:
void setInfo(int port);

View File

@@ -1,38 +1,33 @@
#pragma once
#include <Sockets/IPAddress.hpp>
#ifndef INPORT_ANY
#define INPORT_ANY 0
#endif
#include <string>
// Game-specific Network code written against GameNetworkingSocket API
// GameNetworkingServer
// GameNetworkingClient
// GameNetworkingSocket
// UdpReliability
// TcpServer
// TcpClient
// UdpServer
// UdpClient
// UdpSocket
// TcpSocket
// Berkeley Sockets API
#ifndef INPORT_ANY
#define INPORT_ANY 0
#endif
namespace Socket
{
// TODO: Implement Blocking and Nonblocking UDP
using u8 = uint8_t;
typedef struct sockaddr_in sockaddr_in_t;
typedef struct sockaddr sockaddr_t;
typedef std::vector<uint8_t> msg_t;
@@ -56,39 +51,31 @@ namespace Socket
void Bind(uint16_t portno);
void BindAny();
void BindAny(uint16_t& portno);
bool Connected() const;
/// In UDP, the client does not form a connection with the server like in TCP, and instead, it just sends a datagram.
/// Similarly, the server need not accept a connection and just waits for datagrams to arrive. There is no 3 way handshake.
/// It just checks for any immediate errors and stores the peer's IP address and port number.
void Connect(const std::string& hostname, u16 port);
void Connect(const IPAddress& ipaddr);
void Connect(uint16_t portno);
void Connect(u16 port);
[[nodiscard]] IPAddress GetSelfIP() const {return self_addr;}
[[nodiscard]] IPAddress GetPeerIP() const {return peer_addr;}
public:
/// TODO: Consider ditching templates here...
/// Sends a message to the bound peer only.
template <typename T>
int Send(const T& message) const;
int Send(const u8 *data, unsigned int length, int flags = 0);
int Send(const std::vector<u8>& payload);
/// Sends a message to a specified IPAddress.
template <typename T>
int SendTo(const T& message, const IPAddress& ipaddr) const
{
sockaddr_in_t addr_in = ipaddr;
socklen_t addr_in_len = sizeof(addr_in);
int ret = ::sendto(sock, (const char*)message.data(), message.size(), 0, (struct sockaddr*)&addr_in, addr_in_len);
int SendTo(const IPAddress& ipaddr, const u8 *data, unsigned int length, int flags);
int SendTo(const IPAddress& ipaddr, const std::vector<u8> data);
if (ret < 0)
{
throw SocketException(strerror(errno));
}
return ret;
}
/// Sends a message to anyone who might listen.
template <typename T>
int SendToAll(const T& message);
int Receive(IPAddress& ipaddr, const u8 *message) const;
int ReceiveFromAny(const u8 *message);
template <typename T>
int Receive(T& message, IPAddress& ipaddr) const;
template <typename T>
int ReceiveFromAny(T& message);
int Broadcast(int opt) const;
void Interrupt() const;
private:
@@ -97,5 +84,7 @@ namespace Socket
socklen_t self_addr_len = sizeof(self_addr);
sockaddr_in_t peer_addr{};
socklen_t peer_addr_len = sizeof(peer_addr);
};
};
}

View File

@@ -24,12 +24,14 @@ void TCP_Roundtrip()
std::cout << e.what() << std::endl;
return;
}
// Welcoming the new user
client->Send("Welcome !\n\f", 15, 0);
// Welcoming the new users
std::string payload = "Welcome!\n\f";
client->Send(reinterpret_cast<const u8 *>(payload.data()), payload.size(), 0);
// Closing the listening socket, we want nobody else.
sock->Close();
char data[512];
u8 data[512];
memset(&data, 0, 512);
while (client->Receive(data, sizeof data, 0))
{
@@ -61,7 +63,8 @@ void UDP_Roundtrip()
try {
client->Connect(42575);
client->Send(std::string("Bruh"));
std::string payload = "bruh";
client->Send(reinterpret_cast<const u8 *>(payload.data()), payload.size());
//client->BindAny();
//client->Open();
//client->Connect(sock->GetSelfIP());

View File

@@ -35,7 +35,7 @@ namespace Socket
}
}
IPAddress::IPAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t portno)
IPAddress::IPAddress(u8 a, u8 b, u8 c, u8 d, u16 portno)
{
octets[0] = a;
octets[1] = b;

View File

@@ -4,7 +4,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
#include <cerrno>
#include <unistd.h>
#include <cstring>
#include <memory>
@@ -147,9 +147,9 @@ namespace Socket {
void TcpSocket::Send(const char *data, unsigned int length, int flags)
void TcpSocket::Send(const u8 *data, unsigned int length, int flags)
{
const char * buff = data;
const u8 * buff = data;
int status = 0;
int total_sent = 0;
unsigned int left_to_send = length;
@@ -165,7 +165,7 @@ namespace Socket {
}
}
bool TcpSocket::Receive(char* msg, int len, int flags)
bool TcpSocket::Receive(u8* msg, int len, int flags)
{
int status = 0;
if ((status = ::recv(mSock, msg, len, flags)) == -1)

View File

@@ -1,14 +1,10 @@
#include "Sockets/UdpSocket.hpp"
#include "Sockets/Sockets.hpp"
namespace Socket
{
// TODO: Move error-code string-literals to static const std::strings here
namespace ErrorCodeDescriptions
{
}
UdpSocket::UdpSocket(int family, int flags)
{
this->Open();
@@ -19,13 +15,7 @@ namespace Socket
this->Close();
sock = (int)::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0)
{
auto errcode = std::string(strerror(errno));
std::cout << "[Socket::UdpSocket::ERROR] " << errcode << std::endl;
}
CheckErrors_socket(errno);
if (this->IsClosed()) {
//throw error();
}
@@ -33,8 +23,6 @@ namespace Socket
int UdpSocket::Close()
{
if (!this->IsClosed()){
#ifdef _WIN32
int ret = ::shutdown(sock, SD_BOTH);
@@ -63,7 +51,7 @@ namespace Socket
if (IsClosed())
throw SocketBindingException("Socket has not been Open()'ed!");
self_addr = ipAddress;
self_addr = ipAddress.operator sockaddr_in_t();
self_addr_len = sizeof(self_addr);
int opt = 1;
int ret = ::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
@@ -93,28 +81,41 @@ namespace Socket
//portno = IPAddress{self_addr}.port;
}
void UdpSocket::Connect(const std::string& hostname, u16 port)
{
auto ipaddr = IPAddress::Resolve(hostname, port);
this->Connect(ipaddr, port);
}
void UdpSocket::Connect(const IPAddress& ipaddr)
{
peer_addr = ipaddr;
peer_addr = ipaddr.operator sockaddr_in_t();
peer_addr_len = sizeof(peer_addr);
int ret = ::connect(sock, (sockaddr_t*)&peer_addr, peer_addr_len);
if (ret < 0)
CheckErrors_connect(errno);
}
void UdpSocket::Connect(uint16_t portno)
void UdpSocket::Connect(u16 port)
{
auto ipaddr = IPAddress::Loopback(portno);
auto ipaddr = IPAddress::Loopback(port);
this->Connect(ipaddr);
}
template <typename T>
int UdpSocket::Receive(T& message, IPAddress& ipaddr) const
struct ReceivedPacket
{
IPAddress Sender;
int Length;
std::vector<u8> Payload;
};
int UdpSocket::Receive(IPAddress& ipaddr, const u8 *message) const
{
sockaddr_in_t addr_in;
socklen_t addr_in_len = sizeof(addr_in);
typename T::value_type buffer[10 * 1024];
u8 buffer[10 * 1024];
int ret = ::recvfrom(sock, (char*)buffer, sizeof(buffer), 0, (sockaddr_t*)&addr_in, &addr_in_len);
if (ret < 0) {
@@ -122,8 +123,8 @@ namespace Socket
CheckErrors_recvfrom(errno);
}
ipaddr = addr_in;
message = { std::begin(buffer), std::begin(buffer)+ret};
ipaddr = IPAddress(addr_in);
message = buffer;
return ret;
}
int UdpSocket::Broadcast(int opt) const
@@ -137,18 +138,37 @@ namespace Socket
void UdpSocket::Interrupt() const
{
uint16_t portno = IPAddress{self_addr}.port;
auto ipaddr = IPAddress::Loopback(portno);
this->SendTo(msg_t{}, ipaddr);
// What is this supposed to accomplish?
//uint16_t portno = IPAddress{self_addr}.port;
//auto ipaddr = IPAddress::Loopback(portno);
//this->SendTo(ipaddr, msg_t{}, );
}
template<typename T>
int UdpSocket::Send(const T &message) const {
int ret = ::send(sock, (const char*)message.data(), message.size(), 0);
int UdpSocket::Send(const u8 *data, unsigned int length, int flags)
{
int ret = ::send(sock, data, length, flags);
if (ret < 0)
CheckErrors_send(errno);
return ret;
}
int UdpSocket::Send(const std::vector<u8>& payload)
{
this->Send(payload.data(), payload.size());
}
int UdpSocket::SendTo(const IPAddress &ipaddr, const u8 *data, unsigned int length, int flags) {
sockaddr_in_t addr_in = ipaddr.operator sockaddr_in_t();
socklen_t addr_in_len = sizeof(addr_in);
int ret = ::sendto(sock, (const char*)data, length, 0, (struct sockaddr*)&addr_in, addr_in_len);
if (ret < 0)
CheckErrors_sendto(errno);
return ret;
}
}