You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

59 lines
1.5 KiB

  1. #include <arpa/inet.h>
  2. #include <netdb.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. // TODO: fix over buf overflow
  10. void fillHttpReq(char *buf, char *host, size_t size) {
  11. char *req = "GET / HTTP/1.1\r\nHost: \0";
  12. memcpy(buf, req, strlen(req));
  13. memcpy(buf + strlen(buf), host, strlen(host));
  14. memcpy(buf + strlen(buf), "\r\n\r\n", strlen("\r\n\r\n"));
  15. }
  16. int main(int argc, char **argv) {
  17. int status;
  18. struct addrinfo hints;
  19. struct addrinfo *servinfo;
  20. if (argc != 2) {
  21. fprintf(stderr, "Usage: %s <hostname>", argv[0]);
  22. exit(1);
  23. }
  24. memset(&hints, 0, sizeof hints);
  25. hints.ai_family = AF_UNSPEC;
  26. hints.ai_socktype = SOCK_STREAM;
  27. if ((status = getaddrinfo(argv[1], "80", &hints, &servinfo)) != 0) {
  28. fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
  29. exit(1);
  30. }
  31. int s = socket(servinfo->ai_family, servinfo->ai_socktype,
  32. servinfo->ai_protocol);
  33. printf("%d\n", s);
  34. int connection;
  35. if ((connection = connect(s, servinfo->ai_addr, servinfo->ai_addrlen)) ==
  36. -1) {
  37. fprintf(stderr, "connection error: %d\n", connection);
  38. exit(1);
  39. }
  40. char buf[10000];
  41. fillHttpReq(buf, argv[1], 10000);
  42. printf("%s\n", buf);
  43. // TODO: send throw a loop
  44. send(s, buf, 10000, 0);
  45. memset(buf, 0, 10000);
  46. recv(s, buf, 10000, 0);
  47. printf("%s\n", buf);
  48. close(s);
  49. freeaddrinfo(servinfo);
  50. }