From de04372cb0a8df0b9733a2923da8ac9c9267a911 Mon Sep 17 00:00:00 2001 From: Seth Hamilton Date: Mon, 15 Sep 2025 13:21:34 -0500 Subject: [PATCH] some work on getting an http response from google.com --- .gitignore | 3 +++ activate.bat | 15 +------------- inc/tcp_client.h | 53 +++++++++++++++++++++++++++++++++++++++++------- src/main.cpp | 14 +++++++++++++ 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 54b8d5a..c5b0db3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ compile_commands.json bin/** obj/** .cache/** +*.rad +*.idb +*.pdb diff --git a/activate.bat b/activate.bat index 535e029..7a6b935 100644 --- a/activate.bat +++ b/activate.bat @@ -1,14 +1 @@ -SETLOCAL ENABLEDELAYEDEXPANSION - -FOR /r "C:\Program Files\Microsoft Visual Studio" %%G IN (*vcvars64.bat) DO ( - SET "_file=%%G" - SET "_comp=!_file:\VC\=!" - IF NOT "!_comp!" == "!_file!" "!_file!" -) - -FOR /r "C:\Program Files (x86)\Microsoft Visual Studio" %%G IN (*vcvars64.bat) DO ( - SET "_file=%%G" - SET "_comp=!_file:\VC\=!" - IF NOT "!_comp!" == "!_file!" "!_file!" -) - +"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" diff --git a/inc/tcp_client.h b/inc/tcp_client.h index 7b05d31..5f5cdf4 100644 --- a/inc/tcp_client.h +++ b/inc/tcp_client.h @@ -6,19 +6,36 @@ #include #endif -#ifdef _WIN32 -typedef SOCKET Socket; -#endif +#define DEBUG typedef unsigned char byte; +#ifdef _WIN32 +typedef SOCKET Socket; +bool init_socket_lib() { + // TODO: Should do some sort of smart version upgrade via data + // See https://learn.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-wsadata + WSADATA data; + if (WSAStartup((2 << 8) | 2, &data) != 0) { + printf("init_socket_lib failed: %d\n", WSAGetLastError()); + WSACleanup(); + return false; + } + return true; +} +#endif + +extern bool socket_lib_initialized = init_socket_lib(); + namespace tcp { Socket make_socket(); -bool connect(Socket& s, const char* hostname, int port); +bool connect(Socket &s, const char* hostname, int port); +bool send_transmit_stop(Socket &s); +bool send_read_stop(Socket &s); bool disconnect(Socket &s); -bool send_data(Socket& s, byte* buf, int len); -bool get_data(Socket& s, byte* out_buf, int buf_size, int& out_num_written); +bool send_data(Socket &s, byte* buf, int len); +bool get_data(Socket &s, byte* out_buf, int buf_size, int& out_num_written); #ifdef _WIN32 @@ -56,6 +73,8 @@ bool connect(Socket& s, const char *hostname, int port) { if (code == SOCKET_ERROR) { closesocket(s); s = INVALID_SOCKET; + } else { + break; } ptr = ptr->ai_next; } while (ptr); @@ -65,12 +84,32 @@ bool connect(Socket& s, const char *hostname, int port) { if (s == INVALID_SOCKET) { printf("Unable to connect to %s:%d\n", hostname, port); WSACleanup(); - return 1; + return false; } return true; } +bool send_transmit_stop(Socket &s) { + int code = shutdown(s, SD_SEND); + if (code == SOCKET_ERROR) { + printf("send_transmit_stop failed: %d\n", WSAGetLastError()); + closesocket(s); + return false; + } + return true; +} + +bool send_read_stop(Socket &s) { + int code = shutdown(s, SD_RECEIVE); + if (code == SOCKET_ERROR) { + printf("send_read_stop failed: %d\n", WSAGetLastError()); + closesocket(s); + return false; + } + return true; +} + bool disconnect(Socket &s) { int code = shutdown(s, SD_BOTH); if (code == SOCKET_ERROR) { diff --git a/src/main.cpp b/src/main.cpp index 7ab1dae..4e0da62 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,20 @@ using namespace tcp; +const int buf_size = 1024*1024; +static byte buf[buf_size]; + +const char *basic_get_request = "GET / HTTP/1.1\nHOST: google.com\nUser-Agent:\ncoupon scraper\nAccept: */*"; + + int main() { + if (!socket_lib_initialized) return -1; Socket s = make_socket(); + if (!connect(s, "google.com", 80)) return -1; + if (!send_data(s, (byte*)basic_get_request, strlen(basic_get_request))) return -1; + int n_read; + if (!send_transmit_stop(s)) return -1; + if (!get_data(s, buf, buf_size, n_read)) return -1; + buf[n_read] = '\0'; + printf("%s", (char*)buf); }