- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在获得随机字节来领导每个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;
}
最佳答案
这些“随机字节”是HTTP chunked encoding使用的十六进制内容长度指示符。
关于c - 不属于的随机字节,SSL_read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22107264/
我正在寻找通过 HTTPS 从服务器下载或读取文件,并且我正在使用 openSSL。 我看到连接成功,但是 SSL_read()返回 0。SSL_get_error()返回 6 表示 SSL_ERRO
我正在尝试通过 SSL 连接到安全服务,但是在下面的代码中,SSL_read 永远不会返回,如果服务器不返回任何消息,这当然是正常行为,但是我尝试连接的服务器应该返回某种消息。以下内容是否有不允许阅读
考虑以下代码,它显示了尝试从 ssl 连接读取 buff_size 字节的尝试。 int bytes = SSL_read(m_ssl, buf, buff_size); int err = SSL_
我正在尝试使用 SSL_read 从 Openssl 链接套接字读取数据。我在客户端模式下执行 Openssl 操作,发送命令并从真实世界的服务器接收数据。我使用了两个线程,其中一个线程处理所有 Op
我有一个使用 ssl 的 openssl 客户端。客户端之前正在与 openssl 服务器通信,一切都很好。我们现在正在用 Java 重写服务器,我们看到发生了以下情况。 1) 连接到服务器 WORK
我正在获得随机字节来领导每个SSL_read缓冲区中的内容 char *getJSON(char *url) { int sock, length, bytes, https = 0;
当前的 OpenSSL 版本是 1.1.1d。 我正在使用 SSL 到 TCP 套接字。 以下是相关来源。 ... ... struct timeval tv; fd_set fdIn;
我正在读一本 openssl 编程 tutorial . 我不能让服务器应用程序在单个连接的任何操作上被阻止, 因此我将使用非 block 套接字。 看来,ssl 握手发生在 ssl_accept 和
我正在尝试将 OpenSSL 添加到 epoll 中,并且套接字处于非阻塞模式。令我困惑的一个问题是:由于 SSL 重新协商,SSL_read 可能会产生 SSL_ERROR_WANT_WRITE ,
我正在尝试编写一个连接到带有 OpenSSL 的服务器的 C++ 应用程序。我可以发送数据,它完好无损地到达服务器,但读取操作只读取 1 个字节。 代码: char* dest_u
我制作了一个服务器和一个客户端,它们在安全的 TLSv1.3、OpenSSL 3.0.0 channel 中进行通信。他们必须能够发送和接收,但在这个测试中,服务器只接收,客户端只定期发送应用程序数据
我是套接字编程的新手。 下面的代码片段用于从服务器获取大约 100 万行的响应。我不知道响应的确切长度,因此检查响应串是否包含结束词,如 OK、NO、SUCCESS、FAILURE 等。 size =
我有一个用C来处理ssl的程序,步骤如下: retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); if (retval == -1) {
这个问题的答案可能是“取决于”,那么我的问题就变成了“它取决于什么?”以及“应该怎么做才能按顺序收到它们?”。 最佳答案 是的。 TLS 在保证流的字节顺序的 TCP 上运行,并且正如@dave_th
我正在编写一个使用 OpenSSL 进行连接的程序。除了一个选项外,使用阻塞套接字和线程一切正常: 我的线程 1 调用 SSL_read() 并等待它接收任何东西 - 但如果连接被合作伙伴关闭,SSL
我在 LogCat 中收到大量(一次数百条)消息: 07-15 21:24:48.096: D/NativeCrypto(1606): Doing SSL_Read() 07-15 21:24:48.
我正在编写一个 https 服务器。我已经创建了 csr 并使用我的测试域的根证书对其进行了签名。当客户端连接时,SSL_accept() 成功完成。我正在使用非阻塞 IO。因此,我将首先在 char
我有一个需要升级到 SSL 通信的客户端/服务器通信。目前我有一个发送和接收 tcp 数据的网络套接字。 客户端向服务器发出一个 tcp connect()。 服务器已经实现了接受部分,接受连接后服务
我正在尝试使用 openssl 用 c 编写 Apple 推送通知服务器提供程序(APNS 提供程序)。 到目前为止,我已经能够通过 ssl_write 成功发送通知,但有时可能会发生我发送的消息被拒
我正在尝试使用 OpenSSL 创建一个非阻塞套接字,这样我就可以遍历 SSL_read() 直到没有更多数据剩余,然后中断循环。经过大量工作,我终于使连接正常工作,但现在对于前一千多次迭代,SSL_
我是一名优秀的程序员,十分优秀!