套接字编程:使用openssl库发送请求时获得相同的响应

时间:2013-06-25 11:26:14

标签: c sockets https openssl

我将两个不同的https标头发送到同一主机上的不同地址。我从test()和test1()函数得到相同的响应......似乎只能使用一次......提前感谢:)

#include <iostream>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define FAIL    -1
using namespace std;
SSL_CTX* InitCTX(void);
void test(SSL_CTX* ctx, int server);
void test1(SSL_CTX* ctx, int server);
int OpenConnection(char *hostname, int port);
int main()
{
    SSL_CTX *ctx;
    char *host = "academics.vit.ac.in";
    char *port = "443";
    SSL_library_init();
    ctx = InitCTX();
    int server = OpenConnection(host, atoi(port));
    test(ctx, server); 
    test1(ctx, server);// Both functions prints exactly same data
    return 0;
}

void test(SSL_CTX* ctx, int server)
{
    char buf[9999];
    int bytes;
    SSL *ssl;
    ssl = SSL_new(ctx);
    SSL_set_fd(ssl, server);
    if(SSL_connect(ssl) == FAIL)
    {
        ERR_print_errors_fp(stderr);
    }
    else
    {

        printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
        char*msg = "GET /student/captcha.asp HTTP/1.1\nHost: academics.vit.ac.in\nCookie: ASPSESSIONIDCUTRADAT=OMDEAOKCPOLOKCLJMPAALAJN\n\n";
        SSL_write(ssl, msg, strlen(msg));
        bytes = SSL_read(ssl, buf, sizeof(buf));
        buf[bytes] = 0;
        printf("%s",buf);
        SSL_free(ssl);
    }
}
void test1(SSL_CTX* ctx, int server)
{
    char buf[9999];
    int bytes;
    SSL *ssl;
    ssl = SSL_new(ctx);
    SSL_set_fd(ssl, server);
    if(SSL_connect(ssl) == FAIL)
    {
        ERR_print_errors_fp(stderr);
    }
    else
    {

        printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
        char*msg = "GET / HTTP/1.1\nHost: academics.vit.ac.in\nCookie: ASPSESSIONIDCUTRADAT=OMDEAOKCPOLOKCLJMPAALAJN\n\n";
        SSL_write(ssl, msg, strlen(msg));
        bytes = SSL_read(ssl, buf, sizeof(buf));
        buf[bytes] = 0;
        printf("%s",buf);
        SSL_free(ssl);
    }
}
SSL_CTX* InitCTX(void)
{  
    const SSL_METHOD *method;
    SSL_CTX *ctx;

    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();
    method = SSLv3_client_method();
    ctx = SSL_CTX_new(method);   
    if ( ctx == NULL )
    {
        ERR_print_errors_fp(stderr);
        abort();
    }
    return ctx;
}
int OpenConnection(char *hostname, int port)
{   
    int sd;
    struct hostent *host;
    struct sockaddr_in addr;

    if ( (host = gethostbyname(hostname)) == NULL )
    {
        perror(hostname);
        abort();
    }
    sd = socket(PF_INET, SOCK_STREAM, 0);
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = *(long*)(host->h_addr);
    if ( connect(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
    {
        close(sd);
        perror(hostname);
        abort();
    }
    return sd;
}

我得到的回应是

Connected with RC4-SHA encryption
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: image/bmp
Expires: Tue, 25 Jun 2013 08:27:30 GMT
Server: Microsoft-IIS/7.0
Content-Disposition: inline; filename=captcha.bmp
Set-Cookie: ASPSESSIONIDCUSTBBBT=BHEANFEAHBBCBIJDIBNOPDAI; secure; path=/
X-Powered-By: ASP.NET
Date: Tue, 25 Jun 2013 08:28:30 GMT
Connection: close

Connected with (NONE) encryption
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: image/bmp
Expires: Tue, 25 Jun 2013 08:27:30 GMT
Server: Microsoft-IIS/7.0
Content-Disposition: inline; filename=captcha.bmp
Set-Cookie: ASPSESSIONIDCUSTBBBT=BHEANFEAHBBCBIJDIBNOPDAI; secure; path=/
X-Powered-By: ASP.NET
Date: Tue, 25 Jun 2013 08:28:30 GMT
Connection: close

0 个答案:

没有答案