1

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;
}


}
  • Should you be adding a `\r\n` character after the version to post? – cleblanc May 31 '18 at 20:27
  • You never check the return value from `send()`. How do you know you sent the entire message? – Andrew Henle May 31 '18 at 20:33
  • 1
    Your use of `strcat` is a textbook example of shlemiel the painter. https://www.joelonsoftware.com/2001/12/11/back-to-basics/ – Christian Gibbons May 31 '18 at 20:47
  • You are still just fiddling around instead of learning HTTP. You are not even able to learn from last mistakes you've made and which I've explained to you. In [this question](https://stackoverflow.com/questions/50449009) I've explained that the `Content-length` must match the actual body content. You have a fixed `Content-length: 9` even though your body is something like `version=Windows7` which is definitely not 9 bytes long. Again: *""... anybody coding HTTP directly should study and understand the standard. Looking at a few requests as example is not sufficient..."*. -1 for ignorance. – Steffen Ullrich May 31 '18 at 20:50
  • Hi Steffen, I have tried sending the request with the correct length and still does not work. It works only if if i do it without the variable. I know it is possible to send the request without specifying the length but im not 100% sure how. – Jack Wilson May 31 '18 at 20:54
  • @JackWilson: no its not possible to send a request body without specifying the length. Even with chunked encoding the length will be specified, only at a different place. Read the standard if you don't believe me. Also, check where your code actually hangs. Apart from that there is no Host header which most servers need. And the code is incomplete, i.e. `connect_server` is not called at all. – Steffen Ullrich May 31 '18 at 20:55
  • I changed content length to 9 and version=1 and it posts fine. So the post is working – Jack Wilson May 31 '18 at 21:08
  • Instead of all those `strcpy` and `strcat` calls, use `sprintf(message2, "%s%s%s%s%s", str2, str3, str20, str5, version);` – Barmar May 31 '18 at 21:53
  • Thank you Barmar that was some very useful information. – Jack Wilson May 31 '18 at 22:10

0 Answers0