mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-06 16:36:16 +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);
|
||||
goto end;
|
||||
}
|
||||
tls_trace("Connection established!\n");
|
||||
printf("Connection established!\n");
|
||||
|
||||
|
||||
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");
|
||||
|
||||
format_bytes(stderr, 0, 0, "tls_send: payload", in, inlen);
|
||||
|
||||
if (tls_record_set_type(record, TLS_record_application_data) != 1
|
||||
|| tls_record_set_protocol(record, conn->protocol) != 1
|
||||
|| tls_record_set_length(record, inlen) != 1) {
|
||||
|
||||
@@ -85,7 +85,7 @@ int tlcp_client_main(int argc, char *argv[])
|
||||
int sock;
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[100] = {0};
|
||||
char buf[1000] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
char send_buf[1024] = {0};
|
||||
size_t send_len;
|
||||
@@ -180,31 +180,49 @@ bad:
|
||||
goto end;
|
||||
}
|
||||
|
||||
fd_set fds;
|
||||
|
||||
|
||||
for (;;) {
|
||||
size_t sentlen;
|
||||
|
||||
memset(send_buf, 0, sizeof(send_buf));
|
||||
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
||||
if (feof(stdin)) {
|
||||
tls_shutdown(&conn);
|
||||
goto end;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (tls_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send error\n", prog);
|
||||
|
||||
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(len), &len) != 1) {
|
||||
|
||||
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));
|
||||
|
||||
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
||||
if (feof(stdin)) {
|
||||
tls_shutdown(&conn);
|
||||
goto end;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (tls_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user