diff --git a/include/net.cpp b/include/net.cpp index e69de29..baa9a10 100644 --- a/include/net.cpp +++ b/include/net.cpp @@ -0,0 +1,55 @@ +#include "net.hpp" +#include + +Socket::Socket() { + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) { + throw runtime_error("Failed to create socket"); + } + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; +} + +Socket::~Socket() { + close(sock); +} + +void Socket::connect(const char *ip, uint16_t port) { + server_addr.sin_port = htons(port); + if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) { + throw runtime_error("Invalid IP address"); + } +} + +void Socket::send() { + ssize_t sent_bytes = sendto(sock, &packet_data, sizeof(packet_data), 0, + (struct sockaddr *)&server_addr, sizeof(server_addr)); + if (sent_bytes < 0) { + throw runtime_error("Failed to send data"); + } +} + +void Socket::send(const api_packet &data) { + ssize_t sent_bytes = sendto(sock, &data, sizeof(data), 0, + (struct sockaddr *)&server_addr, sizeof(server_addr)); + if (sent_bytes < 0) { + throw runtime_error("Failed to send data"); + } +} + +void Socket::set_packet(const api_packet &data) { + packet_data = data; +} + +api_packet Socket::create_packet(uint8_t tracker_id) { + api_packet pkt; + memset(&pkt, 0, sizeof(pkt)); + + pkt.message_type = 65; // Handshake message type + pkt.tracker_id = tracker_id; + return pkt; +} + +api_packet Socket::get_packet() { + return packet_data; +} diff --git a/include/net.hpp b/include/net.hpp index abe5691..baf7410 100644 --- a/include/net.hpp +++ b/include/net.hpp @@ -1,11 +1,11 @@ #ifndef NET_HPP #define NET_HPP +#include +#include #include #include -#include #include -#include #include #include "server_structs.h" @@ -13,21 +13,24 @@ using namespace std; class Socket { - private: - // Socket file descriptor - int sock; - struct sockaddr_in server_addr; - // Socket Data - api_packet packet_data; + private: + // Socket file descriptor + int sock; + struct sockaddr_in server_addr; + // Socket Data + api_packet packet_data; - public: - Socket(); - ~Socket(); + public: + Socket(); + ~Socket(); - void connect(const char *ip, uint16_t port); - void send(); - void send(const api_packet &data); + void connect(const char *ip, uint16_t port); + void send(); + void send(const api_packet &data); + void set_packet(const api_packet &data); + api_packet create_packet(uint8_t tracker_id); + api_packet get_packet(); }; #endif // NET_HPP diff --git a/source/main.cpp b/source/main.cpp index a4523ff..8d85960 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,12 +7,25 @@ #include "file.hpp" // for parce_args #include "net.hpp" // for socket operations +#include "server_structs.h" using namespace std; int main(int argc, char *argv[]) { app_info app = parce_args(argc, argv); + api_packet packet; + try { + Socket sock; + sock.connect(app.app_server_out_ip, app.app_port_out); + + packet = sock.create_packet((uint8_t)app.app_id); + sock.set_packet(packet); + sock.send(); + } catch (const runtime_error &e) { + cerr << "Error: " << e.what() << endl; + return 1; + } return 0; }