我得到随机字节,引导每个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;
}
感兴趣区域问题的样本加粗:
&#34; UNO / POT&#34; :{ &#34; last_trade&#34;:&#34; 1000.00000000&#34;, &#34; last_sell&#34;:&#34; 1000.00000000&#34;, &#34; last_buy&#34;:&#34; 1000.00000000&#34;, &#34; current_ask&#34;:&#34; 4000.00000000&#34 ;, &#34; current_ask_volume&#34;:&#34; 1.00000000&#34 ;, &#34; CURRENT_BID&#34;:&#34; 291.00000000&#34 ;, &#34; current_bid_volume&#34;:&#34; 2.00000000&#34 ;, &#34; highest_24h&#34;:&#34;&#34;, &#34;最低_24h&#34;:&#34;&#34;, &#34; volume_base_24h&#34;:&#34; 0&#34; }, &#34; USDE / BTC&#34; :{ &#34; last_trade&#34;:&#34; 0.00000040&#34;, &#34; last_sell&#34;:&#34; 0.00000040&#34;, &#34; last_buy&#34;:&#34; 0.00000040&#34;, &#34; current_ask&#34;:&#34; 0.00000041&#34 ;, &#34; current_ask_volume&#34;:&#34; 100.00000000&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00000039&#34 ;, &#34; current_bid_volume&#34;:&#34; 350000.00000000&#34 ;, &#34; highest_24h&#34;:&#34; 0.00000058&#34;, &#34;最低_24h&#34;:&#34; 0.00000036&#34;, &#34; volume_base_24h&#34;:&#34; 0.63630518&#34; }, &#34; USDE / DOGE&#34; :{ &#34; last_trade&#34;:&#34; 0.30000000&#34;, &#34; last_sell&#34;:&#34; 0.30000000&#34;, &#34; last_buy&#34;:&#34; 0.00000000&#34;, &#34; current_ask&#34;:&#34; 0.40000000&#34 ;, &#34; current_ask_volume&#34;:&#34; 1000.00000000&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.20000000&#34 ;, &#34; current_bid_volume&#34;:&#34; 600.00000000&#34 ;, &#34; highest_24h&#34;:&#34; 0.30000000&#34;, &#34;最低_24h&#34;:&#34; 0.00000001&#34;, &#34; volume_base_24h&#34;:&#34; 150.00004453&#34; }, &#34; USDE / LTC&#34; :{ &#34; last_trade&#34;:&#34; 0.00001400&#34;, &#34; last_sell&#34;:&#34; 0.00001400&#34;, &#34; last_buy&#34;:&#34; 0.00001500&#34;, &#34; current_ask&#34;:&#34; 0.00006699&#34 ;, &#34; current_ask_volume&#34;:&#34; 999.02392380&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00001400&#34 ;, &#34; current_bid_volume&#34;:&#34; 6545.30401900&#34 ;, &#34; highest_24h&#34;:&#34; 0.00002000&#34;, &#34;最低_24h&#34;:&#34; 0.00001400&#34;, &#34; volume_base_24h&#34;:&#34; 0.13267596&#34; }, 的&#34;û A3B TC / BTC&#34; :{ &#34; last_trade&#34;:&#34; 0.00017500&#34;, &#34; last_sell&#34;:&#34; 0.00017500&#34;, &#34; last_buy&#34;:&#34; 0.00020001&#34;, &#34; current_ask&#34;:&#34; 0.00020001&#34 ;, &#34; current_ask_volume&#34;:&#34; 159.31100000&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00018000&#34 ;, &#34; current_bid_volume&#34;:&#34; 404.44538889&#34 ;, &#34; highest_24h&#34;:&#34; 0.00025794&#34;, &#34;最低_24h&#34;:&#34; 0.00015200&#34;, &#34; volume_base_24h&#34;:&#34; 2.03433487&#34; }, &#34; UTC / DOGE&#34; :{ &#34; last_trade&#34;:&#34; 0.00017500&#34;, &#34; last_sell&#34;:&#34; 0.00000000&#34;, &#34; last_buy&#34;:&#34; 0.00000000&#34;, &#34; current_ask&#34;:&#34; 100000.00000000&#34 ;, &#34; current_ask_volume&#34;:&#34; 7.57259300&#34 ;, &#34; CURRENT_BID&#34;:&#34; 1.00000011&#34 ;, &#34; current_bid_volume&#34;:&#34; 50.00000000&#34 ;, &#34; highest_24h&#34;:&#34;&#34;, &#34;最低_24h&#34;:&#34;&#34;, &#34; volume_base_24h&#34;:&#34; 0&#34; }, &#34; UTC / LTC&#34; :{ &#34; last_trade&#34;:&#34; 0.01600000&#34;, &#34; last_sell&#34;:&#34; 0.01580000&#34;, &#34; last_buy&#34;:&#34; 0.01600000&#34;, &#34; current_ask&#34;:&#34; 0.01490000&#34 ;, &#34; current_ask_volume&#34;:&#34; 50.11762200&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00001101&#34 ;, &#34; current_bid_volume&#34;:&#34; 1283.55681200&#34 ;, &#34; highest_24h&#34;:&#34; 0.01600000&#34;, &#34;最低_24h&#34;:&#34; 0.01600000&#34;, &#34; volume_base_24h&#34;:&#34; 0.04516711&#34; }, &#34; VDC / BTC&#34; :{ &#34; last_trade&#34;:&#34; 0.00000010&#34;, &#34; last_sell&#34;:&#34; 0.00000012&#34;, &#34; last_buy&#34;:&#34; 0.00000010&#34;, &#34; current_ask&#34;:&#34; 0.00000026&#34 ;, &#34; current_ask_volume&#34;:&#34; 26918.98003600&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00000005&#34 ;, &#34; current_bid_volume&#34;:&#34; 100000.00000000&#34 ;, &#34; highest_24h&#34;:&#34; 0.00000028&#34;, &#34;最低_24h&#34;:&#34; 0.00000010&#34;, &#34; volume_base_24h&#34;:&#34; 0.02909542&#34; }, &#34; VDC / LTC&#34; :{ &#34; last_trade&#34;:&#34; 0.00000100&#34;, &#34; last_sell&#34;:&#34; 0.00000100&#34;, &#34; last_buy&#34;:&#34; 0.00000300&#34;, &#34; current_ask&#34;:&#34; 0.00000600&#34 ;, &#34; current_ask_volume&#34;:&#34; 1249.60000000&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00000100&#34 ;, &#34; current_bid_volume&#34;:&#34; 2181786.00000000&#34 ;, &#34; highest_24h&#34;:&#34;&#34;, &#34;最低_24h&#34;:&#34;&#34;, &#34; volume_base_24h&#34;:&#34; 0&#34; }, &#34; VMP / BTC&#34; :{ &#34; last_trade&#34;:&#34; 0.00002610&#34;, &#34; last_sell&#34;:&#34; 0.00002610&#34;, &#34; last_buy&#34;:&#34; 0.00003200&#34;, &#34; current_ask&#34;:&#34; 0.00005000&#34 ;, &#34; current_ask_volume&#34;:&#34; 66.00000000&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00002610&#34 ;, &#34; current_bid_volume&#34;:&#34; 26.00464672&#34 ;, &#34; highest_24h&#34;:&#34; 0.00005000&#34;, &#34;最低_24h&#34;:&#34; 0.00002610&#34;, &#34; volume_base_24h&#34;:&#34; 0.04879532&#34; }, &#34; VMP / DOGE&#34; :{ &#34; last_trade&#34;:&#34; 33.00000000&#34;, &#34; last_sell&#34;:&#34; 10.00000000&#34;, &#34; last_buy&#34;:&#34; 33.00000000&#34;, &#34; current_ask&#34;:&#34; 33.00000000&#34 ;, &#34; current_ask_volume&#34;:&#34; 300.00000000&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00000006&#34 ;, &#34; current_bid_volume&#34;:&#34; 20000000.00000000&#34 ;, &#34; highest_24h&#34;:&#34;&#34;, &#34;最低_24h&#34;:&#34;&#34;, &#34; volume_base_24h&#34;:&#34; 0&#34; }, &#34; VMP / POT&#34; :{ &#34; last_trade&#34;:&#34; 0.00548000&#34;, &#34; last_sell&#34;:&#34; 0.00548000&#34;, &#34; last_buy&#34;:&#34; 0.00000000&#34;, &#34; current_ask&#34;:&#34; 33.00000000&#34 ;, &#34; current_ask_volume&#34;:&#34; 300.00000000&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00549000&#34 ;, &#34; current_bid_volume&#34;:&#34; 100.00000000&#34 ;, &#34; highest_24h&#34;:&#34;&#34;, &#34;最低_24h&#34;:&#34;&#34;, &#34; volume_base_24h&#34;:&#34; 0&#34; }, 的&#34; VO 5A8 LT / BTC&#34; :{ &#34; last_trade&#34;:&#34; 0.00000002&#34;, &#34; last_sell&#34;:&#34; 0.00000002&#34;, &#34; last_buy&#34;:&#34; 0.00000003&#34;, &#34; current_ask&#34;:&#34; 0.00000003&#34 ;, &#34; current_ask_volume&#34;:&#34; 115696031.91739591&#34 ;, &#34; CURRENT_BID&#34;:&#34; 0.00000002&#34 ;, &#34; current_bid_volume&#34;:&#34; 200707569.17978617&#34 ;, &#34; highest_24h&#34;:&#34; 0.00000003&#34;, &#34;最低_24h&#34;:&#34; 0.00000002&#34;, &#34; volume_base_24h&#34;:&#34; 0.97345629&#34; },