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.
 
 
 

74 lines
1.8 KiB

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