ANTICHAT.XYZ    VIDEO.ANTICHAT.XYZ    НОВЫЕ СООБЩЕНИЯ    ФОРУМ  
Баннер 1   Баннер 2
Antichat снова доступен.
Форум Antichat (Античат) возвращается и снова открыт для пользователей. Здесь обсуждаются безопасность, программирование, технологии и многое другое. Сообщество снова собирается вместе.
Новый адрес: forum.antichat.xyz
Вернуться   Форум АНТИЧАТ > Программирование > С/С++, C#, Delphi, .NET, Asm
   
 
 
Опции темы Поиск в этой теме Опции просмотра

  #14  
Старый 05.08.2009, 19:25
Аватар для __mad
__mad
Участник форума
Регистрация: 04.11.2007
Сообщений: 103
Провел на форуме:
548128

Репутация: 104
По умолчанию

Цитата:
Сообщение от ErrorNeo  
а нет ни у кого, случайно, POST и GET на вин-соке через SSL ?;P

Код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <openssl/crypto.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

#if defined _WIN32
#include <winsock.h>
#define sleep(x) Sleep(x)
#define close(x) closesocket(x)
#pragma comment(lib, "ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#endif

static char *fetch_ssl_url(const char *, const char *, int *);

int main(int argc, char *argv[])
{
  char *body;

  if (argc < 1 && argc > 4) {
    printf("%s <ssl url> <path>\n", argv[0]);
    return 1;
  }

  body = fetch_ssl_url(argv[1], argv[2], NULL);

  printf("%s\n", body);

  return 0;
}

int hard_fetch_ip = 0;

#define MAX_LEN 1024

static char *fetch_ssl_url(const char *host, const char *url, int *res_len)
{
  struct hostent *phe;
  struct sockaddr_in sin;
  int s, len, len_respose;
  char *body_send;
  char *body_recv;
  char *body_response;

  SSL_METHOD *method;
  SSL_CTX *ctx;
  SSL *ssl;

  SSL_library_init();
  OpenSSL_add_all_algorithms();
  SSL_load_error_strings();
  method = SSLv2_client_method();
  ctx = SSL_CTX_new(method);
  ssl = SSL_new(ctx);

#if defined _WIN32
  WSADATA wsaData;
  WSAStartup(0x101, &wsaData);
#endif

  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s == -1) {
    perror("socket");
    return NULL;
  }

  if (!hard_fetch_ip) {
    phe = gethostbyname(host);
    if (phe == NULL) {
      perror("gethostbyname");
      return NULL;
    }
    hard_fetch_ip = ((struct in_addr*)phe->h_addr_list[0])->s_addr;
  }

  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = hard_fetch_ip;
  sin.sin_port = htons(443);

  if (connect(s, (struct sockaddr*)&sin, sizeof(struct sockaddr_in))) {
    perror("connect");
    return NULL;
  }

  SSL_set_fd(ssl, s);
  SSL_connect(ssl);

  body_send = (char *)malloc(MAX_LEN);
  body_recv = (char *)malloc(MAX_LEN);
  body_response = (char *)malloc(MAX_LEN);

  memset(body_response, 0, MAX_LEN);
  len_respose = 0;

  sprintf(body_send,
	  "GET /%s HTTP/1.1\r\n"
	  "Host: %s\r\n"
	  "Connection: close\r\n"
	  "Accept: */*\r\n\r\n", url, host);

  SSL_write(ssl, body_send, strlen(body_send));

  int new_len = MAX_LEN;
  char *tmp;

  do {
    len = SSL_read(ssl, body_recv, MAX_LEN);
    len_respose += len;
    if (len_respose > new_len) {
      new_len = new_len * 2;
      body_response = (char *) realloc(body_response, new_len );
    }
    memcpy(body_response + (len_respose - len), body_recv, len);
  } while (len);

  // remove headers
  tmp = strstr(body_response, "\r\n\r\n");
  len_respose -= (tmp - body_response + 4);
  memcpy(body_response, tmp + 4, len_respose);

  free(body_recv);
  free(body_send);
  close(s);
  SSL_free(ssl);

  if (res_len) {
    *res_len = len_respose;
  }
  return body_response;
}

$ gcc http_ssl.c -Wall -lssl -o http_ssl

Windows

$ i586-mingw32msvc-gcc http_ssl.c -Wall -lssl -o http_ssl

2 bad hash

Запускать, например, так:

$./http_ssl gmail.com /


PS:

Переделать для POST запросов, думаю не составит труда.
 
Ответить с цитированием
 



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Посимвольный перебор в базах данных на примере MySQL LoFFi Чужие Статьи 5 30.04.2007 05:05
Post запрос... RedBull@12 С/С++, C#, Delphi, .NET, Asm 4 06.12.2006 17:26
Коды состояния HTTP - Коды ошибок http сервера D=P=CH= MOD= *nix 6 15.10.2006 20:47
Post запрос vvs777 PHP, PERL, MySQL, JavaScript 5 10.11.2005 20:41
Внедрение Sql кода с завязанными глазами k00p3r Чужие Статьи 0 12.06.2005 20:48



Здесь присутствуют: 1 (пользователей: 0 , гостей: 1)
 


Быстрый переход




ANTICHAT.XYZ