tcp/quic lab finished
This commit is contained in:
118
network/tcpquiclab/quic_client.c
Normal file
118
network/tcpquiclab/quic_client.c
Normal file
@ -0,0 +1,118 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <quiche.h>
|
||||
|
||||
#define MAX_DATAGRAM_SIZE 1350
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
quiche_config *config = quiche_config_new(QUICHE_PROTOCOL_VERSION);
|
||||
if (config == NULL) return -1;
|
||||
|
||||
quiche_config_verify_peer(config, false);
|
||||
quiche_config_set_application_protos(config, (uint8_t *) "\x0ahq-interop\x05hq-29\x05hq-28\x05hq-27\x08http/0.9", 38);
|
||||
quiche_config_set_max_idle_timeout(config, 5000);
|
||||
quiche_config_set_max_recv_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
||||
quiche_config_set_max_send_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
||||
quiche_config_set_initial_max_data(config, 10000000);
|
||||
quiche_config_set_initial_max_stream_data_bidi_local(config, 1000000);
|
||||
quiche_config_set_initial_max_streams_bidi(config, 100);
|
||||
|
||||
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0) return -1;
|
||||
|
||||
struct sockaddr_in peer_addr;
|
||||
memset(&peer_addr, 0, sizeof(peer_addr));
|
||||
peer_addr.sin_family = AF_INET;
|
||||
peer_addr.sin_port = htons(8888);
|
||||
inet_pton(AF_INET, "127.0.0.1", &peer_addr.sin_addr);
|
||||
|
||||
if (connect(sock, (struct sockaddr *)&peer_addr, sizeof(peer_addr)) < 0) {
|
||||
perror("connect");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int flags = fcntl(sock, F_GETFL, 0);
|
||||
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
|
||||
int rng = open("/dev/urandom", O_RDONLY);
|
||||
if (rng >= 0) {
|
||||
read(rng, scid, sizeof(scid));
|
||||
close(rng);
|
||||
}
|
||||
|
||||
quiche_conn *conn = quiche_connect("127.0.0.1", (const uint8_t *)scid, sizeof(scid), NULL, 0, (struct sockaddr *)&peer_addr, sizeof(peer_addr), config);
|
||||
if (conn == NULL) {
|
||||
fprintf(stderr, "quiche_connect failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Connecting to QUIC server...\n");
|
||||
|
||||
uint8_t buf[65535];
|
||||
uint8_t out[MAX_DATAGRAM_SIZE];
|
||||
bool req_sent = false;
|
||||
|
||||
while (1) {
|
||||
ssize_t read_len = recv(sock, buf, sizeof(buf), 0);
|
||||
if (read_len > 0) {
|
||||
quiche_conn_recv(conn, buf, read_len, &(quiche_recv_info){
|
||||
.to = NULL,
|
||||
.to_len = 0,
|
||||
.from = (struct sockaddr *)&peer_addr,
|
||||
.from_len = sizeof(peer_addr),
|
||||
});
|
||||
}
|
||||
|
||||
if (quiche_conn_is_closed(conn)) {
|
||||
printf("Connection closed.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (quiche_conn_is_established(conn)) {
|
||||
if (!req_sent) {
|
||||
const char *msg = "Hello from QUIC Client!";
|
||||
uint64_t err_code = 0;
|
||||
quiche_conn_stream_send(conn, 4, (uint8_t*)msg, strlen(msg), true, &err_code);
|
||||
printf("Sent: %s\n", msg);
|
||||
req_sent = true;
|
||||
}
|
||||
|
||||
uint64_t s = 0;
|
||||
quiche_stream_iter *readable = quiche_conn_readable(conn);
|
||||
while (quiche_stream_iter_next(readable, &s)) {
|
||||
uint8_t recv_buf[1024];
|
||||
bool fin = false;
|
||||
uint64_t err_code = 0;
|
||||
ssize_t len = quiche_conn_stream_recv(conn, s, recv_buf, sizeof(recv_buf), &fin, &err_code);
|
||||
if (len > 0) {
|
||||
printf("Received: %.*s\n", (int)len, recv_buf);
|
||||
quiche_conn_close(conn, true, 0, (const uint8_t *)"Done", 4);
|
||||
}
|
||||
}
|
||||
quiche_stream_iter_free(readable);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
quiche_send_info send_info;
|
||||
ssize_t written = quiche_conn_send(conn, out, sizeof(out), &send_info);
|
||||
if (written == QUICHE_ERR_DONE) break;
|
||||
if (written < 0) break;
|
||||
send(sock, out, written, 0);
|
||||
}
|
||||
|
||||
quiche_conn_on_timeout(conn);
|
||||
usleep(1000);
|
||||
}
|
||||
|
||||
quiche_conn_free(conn);
|
||||
quiche_config_free(config);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user