mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-07 21:53:41 +08:00
Use select in TLCP client
Example of `tlcp_client` ``` $ gmssl tlcp_client -host ebssec.boc.cn Connection established! GET / HTTP/1.1[enter] Host: localhost[enter] [enter] [enter] ```
This commit is contained in:
@@ -595,7 +595,7 @@ int tlcp_do_connect(TLS_CONNECT *conn)
|
|||||||
tls_send_alert(conn, TLS_alert_decrypt_error);
|
tls_send_alert(conn, TLS_alert_decrypt_error);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
tls_trace("Connection established!\n");
|
printf("Connection established!\n");
|
||||||
|
|
||||||
|
|
||||||
conn->protocol = TLS_protocol_tlcp;
|
conn->protocol = TLS_protocol_tlcp;
|
||||||
|
|||||||
@@ -1721,8 +1721,6 @@ int tls_send(TLS_CONNECT *conn, const uint8_t *in, size_t inlen, size_t *sentlen
|
|||||||
|
|
||||||
tls_trace("send ApplicationData\n");
|
tls_trace("send ApplicationData\n");
|
||||||
|
|
||||||
format_bytes(stderr, 0, 0, "tls_send: payload", in, inlen);
|
|
||||||
|
|
||||||
if (tls_record_set_type(record, TLS_record_application_data) != 1
|
if (tls_record_set_type(record, TLS_record_application_data) != 1
|
||||||
|| tls_record_set_protocol(record, conn->protocol) != 1
|
|| tls_record_set_protocol(record, conn->protocol) != 1
|
||||||
|| tls_record_set_length(record, inlen) != 1) {
|
|| tls_record_set_length(record, inlen) != 1) {
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ int tlcp_client_main(int argc, char *argv[])
|
|||||||
int sock;
|
int sock;
|
||||||
TLS_CTX ctx;
|
TLS_CTX ctx;
|
||||||
TLS_CONNECT conn;
|
TLS_CONNECT conn;
|
||||||
char buf[100] = {0};
|
char buf[1000] = {0};
|
||||||
size_t len = sizeof(buf);
|
size_t len = sizeof(buf);
|
||||||
char send_buf[1024] = {0};
|
char send_buf[1024] = {0};
|
||||||
size_t send_len;
|
size_t send_len;
|
||||||
@@ -180,10 +180,37 @@ bad:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fd_set fds;
|
||||||
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
size_t sentlen;
|
size_t sentlen;
|
||||||
|
|
||||||
|
|
||||||
|
FD_ZERO(&fds);
|
||||||
|
FD_SET(conn.sock, &fds);
|
||||||
|
FD_SET(0, &fds);
|
||||||
|
|
||||||
|
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
|
||||||
|
fprintf(stderr, "%s: select failed\n", prog);
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (FD_ISSET(conn.sock, &fds)) {
|
||||||
|
memset(buf, 0, sizeof(buf));
|
||||||
|
len = sizeof(buf);
|
||||||
|
|
||||||
|
if (tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len) != 1) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf[len] = 0;
|
||||||
|
printf("%s\n", buf);
|
||||||
|
|
||||||
|
}
|
||||||
|
if (FD_ISSET(0, &fds)) {
|
||||||
memset(send_buf, 0, sizeof(send_buf));
|
memset(send_buf, 0, sizeof(send_buf));
|
||||||
|
|
||||||
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
||||||
if (feof(stdin)) {
|
if (feof(stdin)) {
|
||||||
tls_shutdown(&conn);
|
tls_shutdown(&conn);
|
||||||
@@ -196,15 +223,6 @@ bad:
|
|||||||
fprintf(stderr, "%s: send error\n", prog);
|
fprintf(stderr, "%s: send error\n", prog);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
memset(buf, 0, sizeof(buf));
|
|
||||||
len = sizeof(buf);
|
|
||||||
if (tls_recv(&conn, (uint8_t *)buf, sizeof(len), &len) != 1) {
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
buf[len] = 0;
|
|
||||||
printf("%s\n", buf);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user