I am currently trying to send a POST request to my database which add the operating system version.
If i do it without variable and manualy craft the request it works fine.
I am now trying to find the operating system using a function (which works) and copy the output using strcpy into the post request parameter and send it to the database.
At the momment my terminal just hangs when trying to send the request.
Any help would be great
Thank you
My C program....
#include <stdio.h>
#include <winsock2.h>
#include <string.h>
#pragma comment (lib"ws32.lib")
int main(void){
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char *message, server_reply[2000];
int recv_size;
char version[50];
printf("starting winsock...\n\n");
if(WSAStartup(MAKEWORD(2,2),&wsa) !=0 ){
printf("winsock not started");
}
printf("winsock all good...\n\n");
//create socket
if((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET){
printf("bad socket");
}
printf("good socket\n\n");
//make a connection
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(80);
if(connect(s, (struct sockaddr *)&server, sizeof(server)) <0 ){
puts("bad connection");
return 1;
}
printf("good conecttion\n\n");
void connect_server(SOCKET s, char* version)
{
char* str2;
char* str3;
char* str5;
char* str20;
str2 = "POST /test/connect.php HTTP/1.0\r\n";
str3 = "Content-Type: application/x-www-form-urlencoded\r\n";
str20= "Content-Length: 9\r\n\r\n";
str5 = "version=";
char * message2 = (char *) malloc(1 + strlen(str2)+ strlen(str3)+ strlen(str5)+ strlen(version) + strlen(str20));
strcpy(message2,str2);
strcat(message2,str3);
strcat(message2,str20);
strcat(message2,str5);
strcat(message2,version);
// send http requests
send(s,message2,strlen(message2),0);
}
//recieve
if((recv_size = recv(s, server_reply, 2000, 0)) == SOCKET_ERROR){
puts("bad reply");
return 1;
}
server_reply[recv_size] = '\0';
puts(server_reply);
void os_version(char* version){
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
// os version
GetVersionEx(&osvi);
//printf("Windows version: %u.%u\n", info.dwMajorVersion, info.dwMinorVersion);
if(osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1)
{
strcpy(version, "Windows7");
}
else if(osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0)
{
strcpy(version, "WindowsVista");
}
else if(osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 2)
{
strcpy(version, "Windows8");
}
else if(osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 3)
{
strcpy(version, "Windows8.1");
}
else if(osvi.dwMajorVersion == 10 && osvi.dwMinorVersion == 0)
{
strcpy(version, "Windows10");
}
else
{
strcpy(version, "Unknown");
}
return 0;
}
}