繁体   English   中英

不属于的随机字节,SSL_read

[英]Random bytes that dont belong, SSL_read

我正在获得随机字节来领导每个SSL_read的缓冲区中的内容

char *getJSON(char *url) {
int sock, length, bytes, https = 0;                         
struct sockaddr_in server;
char *json = NULL, buffer[4096], command[200], *hostname = malloc(100);
size_t size;

char *q = url, *r = malloc(100), *s = hostname;
if(q[4] == 's')
    https = 1;
int k = 0, j = 0, i = https ? 8 : 7;
for(; q[i] != '/'; i++, j++)
    s[j] = q[i];
for(; q[i] != '\0'; i++, k++)   
    r[k] = q[i];
r[k] = '\0';
s[j] = '\0';

struct hostent *host;
if((host = gethostbyname(hostname)) == NULL) {
    printf("gethostbyname() failed\n");
    return NULL;
}

memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr = *((struct in_addr *)host->h_addr);
server.sin_port = htons(https ? 443 : 80);
bzero(&(server.sin_zero), 8);

sprintf(command, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", r, hostname);

if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket: ");
    return NULL;
}

struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));

if(connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("connect: ");
    free(hostname);
    free(r);
    return NULL;
}
length = strlen(command);
if(!https) {
    if(send(sock, command, length, 0) != length) {
        printf("send fail\n");
        close(sock);
        free(hostname);
        free(r);
        return NULL;
    }

    size = 0;
    while((bytes = recv(sock, buffer, sizeof(buffer), 0)) > 0) {
        size += bytes;
        json = realloc(json, size + 1);
        memcpy(json + size - bytes, buffer, bytes);
    }
    if(json != NULL)
        json[size] = '\0';

    close(sock);
    free(hostname);
    free(r);
}
else {
    int status;
    SSL_CTX *ctx = NULL;
    SSL *session = NULL;
    SSL_METHOD *method;

    SSL_library_init();
    SSL_load_error_strings();
    OpenSSL_add_all_algorithms();
    method = SSLv3_client_method();
    ctx = SSL_CTX_new(method);
  if(ctx == NULL) {
        printf("ctx: %s\n", ERR_error_string(ERR_get_error(), NULL));
        close(sock);
        free(hostname);
        free(r);
        return NULL;
    }
  session = SSL_new(ctx);
  if(session == NULL) {
        printf("session: %s\n", ERR_error_string(ERR_get_error(), NULL));
        SSL_CTX_free(ctx);
        close(sock);
        free(hostname);
        free(r);
        return NULL;
    }
  if(!SSL_set_fd(session, sock)) {
        printf("set_fd: %s\n", ERR_error_string(ERR_get_error(), NULL));
        SSL_free(session);
        SSL_CTX_free(ctx);
        close(sock);
        free(hostname);
        free(r);
        return NULL;
    }
  if(SSL_connect(session) != 1) {
        printf("ssl_connect: %s\n", ERR_error_string(ERR_get_error(), NULL));
        SSL_free(session);
        SSL_CTX_free(ctx);
        close(sock);
        free(hostname);
        free(r);
        return NULL;
    }

    SSL_write(session, command, length);
    sleep(1);

    size = 0;
    while((bytes = SSL_read(session, buffer, sizeof(buffer))) > 0) {
        json = realloc(json, size + bytes + 1);
        memcpy(json + size, buffer, bytes);
        size += bytes;
    }
    if(json != NULL)
        json[size] = '\0';

    SSL_shutdown(session);
    close(sock);
    free(hostname);
    free(r);

    SSL_free(session);
    SSL_CTX_free(ctx);
}

i = 0;
while(json[i] != '{' && i < size)
    i++;
if(i == size -1)
    json = NULL;
else {
    char *temp = malloc(size - i + 2);
    strncpy(temp, json + i, size - i + 1);
    free(json);
    json = temp;
}

return json;
}

感兴趣的区域以粗体显示的问题样本:

“ UNO / POT”:{“ last_trade”:“ 1000.00000000”,“ last_sell”:“ 1000.00000000”,“ last_buy”:“ 1000.00000000”,“ current_ask”:“ 4000.00000000”,“ current_ask_volume”:“ 1.00000000”,“ current_bid” :“ 291.00000000”,“ current_bid_volume”:“ 2.00000000”,“ highest_24h”:“”,“ lowest_24h”:“”,“ volume_base_24h”:“ 0”},“ USDE / BTC”:{“ last_trade”:“ 0.00000040” ,“ last_sell”:“ 0.00000040”,“ last_buy”:“ 0.00000040”,“ current_ask”:“ 0.00000041”,“ current_ask_volume”:“ 100.00000000”,“ current_bid”:“ 0.00000039”,“ current_bid_volume”:“ 350000.00000000”,“ maximum_24h“:” 0.00000058“,” lowest_24h“:” 0.00000036“,” volume_base_24h“:” 0.63630518“},” USDE / DOGE“:{” last_trade“:” 0.30000000“,” last_sell“:” 0.30000000“,” last_buy“ :“ 0.00000000”,“ current_ask”:“ 0.40000000”,“ current_ask_volume”:“ 1000.00000000”,“ current_bid”:“ 0.20000000”,“ current_bid_volume”:“ 600.00000000”,“ highest_24h”:“ 0.30000000”,“ lowest_24h”:“ 0.00000001”,“ volume_base_24h”:“ 150.00004453”},“ USDE / LTC”:{“ last_trade”:“ 0.00001400”,“ last_sell”:“ 0。 00001400”,“ last_buy”:“ 0.00001500”,“ current_ask”:“ 0.00006699”,“ current_ask_volume”:“ 999.02392380”,“ current_bid”:“ 0.00001400”,“ current_bid_volume”:“ 6545.30401900”,“ highest_24h”:“ 0.00002000” ,“ lowest_24h”:“ 0.00001400”,“ volume_base_24h”:“ 0.13267596”}, “ U a3b TC / BTC” :{“ last_trade”:“ 0.00017500”,“ last_sell”:“ 0.00017500”,“ last_buy”:“ 0.00020001” ,“ current_ask”:“ 0.00020001”,“ current_ask_volume”:“ 159.31100000”,“ current_bid”:“ 0.00018000”,“ current_bid_volume”:“ 404.44538889”,“ highest_24h”:“ 0.00025794”,“ lowest_24h”:“ 0.00015200”,“ volume_base_24h“:” 2.03433487“},” UTC / DOGE“:{” last_trade“:” 0.00017500“,” last_sell“:” 0.00000000“,” last_buy“:” 0.00000000“,” current_ask“:” 100000.00000000“,” current_ask_volume“ :“ 7.57259300”,“ current_bid”:“ 1.00000011”,“ current_bid_volume”:“ 50.00000000”,“ highest_24h”:“”,“ lowest_24h”:“”,“ volume_base_24h”:“ 0”},“ UTC / LTC”: {“ last_trade”:“ 0.01600000”,“ last_sell”:“ 0.01580000”,“ last_buy”:“ 0.01600000”,“ current_ask”:“ 0.01490000”,“ cur rent_ask_volume“:” 50.11762200“,” current_bid“:” 0.00001101“,” current_bid_volume“:” 1283.55681200“,” highest_24h“:” 0.01600000“,” lowest_24h“:” 0.01600000“,” volume_base_24h“:” 0.04516711“},” VDC / BTC“:{” last_trade“:” 0.00000010“,” last_sell“:” 0.00000012“,” last_buy“:” 0.00000010“,” current_ask“:” 0.00000026“,” current_ask_volume“:” 26918.98003600“,” current_bid“:” 0.00000005“,” current_bid_volume“:” 100000.00000000“,” highest_24h“:” 0.00000028“,” lowest_24h“:” 0.00000010“,” volume_base_24h“:” 0.02909542“},” VDC / LTC“:{” last_trade“:” 0.00000100“ ,“ last_sell”:“ 0.00000100”,“ last_buy”:“ 0.00000300”,“ current_ask”:“ 0.00000600”,“ current_ask_volume”:“ 1249.60000000”,“ current_bid”:“ 0.00000100”,“ current_bid_volume”:“ 2181786.00000000”,“ maximum_24h“:”“,” lowest_24h“:”“,” volume_base_24h“:” 0“},” VMP / BTC“:{” last_trade“:” 0.00002610“,” last_sell“:” 0.00002610“,” last_buy“:” 0.00003200”,“ current_ask”:“ 0.00005000”,“ current_ask_volume”:“ 66.00000000”,“ current_bid”:“ 0.00002610”,“ current _bid_volume“:” 26.00464672“,” highest_24h“:” 0.00005000“,” lowest_24h“:” 0.00002610“,” volume_base_24h“:” 0.04879532“},” VMP / DOGE“:{” last_trade“:” 33.00000000“,” last_sell“ :“ 10.00000000”,“ last_buy”:“ 33.00000000”,“ current_ask”:“ 33.00000000”,“ current_ask_volume”:“ 300.00000000”,“ current_bid”:“ 0.00000006”,“ current_bid_volume”:“ 20000000.00000000”,“ highest_24h”:“ “,” lowest_24h“:”“,” volume_base_24h“:” 0“},” VMP / POT“:{” last_trade“:” 0.00548000“,” last_sell“:” 0.00548000“,” last_buy“:” 0.00000000“,” current_ask“:” 33.00000000“,” current_ask_volume“:” 300.00000000“,” current_bid“:” 0.00549000“,” current_bid_volume“:” 100.00000000“,” highest_24h“:”“,” lowest_24h“:”“,” volume_base_24h“:” 0“}, ” VO 5a8 LT / BTC“ :{” last_trade“:” 0.00000002“,” last_sell“:” 0.00000002“,” last_buy“:” 0.00000003“,” current_ask“:” 0.00000003“,” current_ask_volume“:” 115696031.91739591“,” current_bid“:” 0.00000002“,” current_bid_volume“:” 200707569.17978617“,” highest_24h“:” 0.00000003“,” lowest_24h“:” 0.0000 0002“,” volume_base_24h“:” 0.97345629“},

那些“随机字节”是HTTP分块编码使用的十六进制内容长度指示符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM