tcp/quic lab bug fixed

This commit is contained in:
2025-12-25 15:14:14 +08:00
parent 200566e8fe
commit 1836a05f4c
9 changed files with 231 additions and 34 deletions

View File

@ -24,13 +24,25 @@ typedef struct {
int timer_started;
} Client;
// void debug_log(const char *line, void *argp) {
// fprintf(stderr, "%s\n", line);
// }
int main(int argc, char *argv[]) {
// 1. Configuration
// quiche_enable_debug_logging(debug_log, NULL);
quiche_config *config = quiche_config_new(QUICHE_PROTOCOL_VERSION);
if (config == NULL) return -1;
quiche_config_load_cert_chain_from_pem_file(config, "cert.crt");
quiche_config_load_priv_key_from_pem_file(config, "cert.key");
if (quiche_config_load_cert_chain_from_pem_file(config, "cert.crt") < 0) {
fprintf(stderr, "failed to load certificate chain\n");
return -1;
}
if (quiche_config_load_priv_key_from_pem_file(config, "cert.key") < 0) {
fprintf(stderr, "failed to load private key\n");
return -1;
}
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, 10000);
@ -85,16 +97,8 @@ int main(int argc, char *argv[]) {
if (!quiche_version_is_supported(version)) {
ssize_t written = quiche_negotiate_version(scid, scid_len, dcid, dcid_len, out, sizeof(out));
if (written > 0) sendto(sock, out, written, 0, (struct sockaddr *)&peer_addr, peer_addr_len);
} else if (token_len == 0) {
uint8_t new_scid[QUICHE_MAX_CONN_ID_LEN];
int rng = open("/dev/urandom", O_RDONLY);
if (rng >= 0) {
read(rng, new_scid, sizeof(new_scid));
close(rng);
}
ssize_t written = quiche_retry(scid, scid_len, dcid, dcid_len, new_scid, sizeof(new_scid), token, token_len, version, out, sizeof(out));
if (written > 0) sendto(sock, out, written, 0, (struct sockaddr *)&peer_addr, peer_addr_len);
} else {
// Skip retry and accept connection directly
client = malloc(sizeof(Client));
client->sock = sock;
client->peer_addr = peer_addr;
@ -133,7 +137,7 @@ int main(int argc, char *argv[]) {
quiche_conn_free(conn);
free(client);
client = NULL;
continue;
break; // Exit the main loop and terminate
}
if (quiche_conn_is_established(conn)) {
@ -184,4 +188,4 @@ int main(int argc, char *argv[]) {
quiche_config_free(config);
return 0;
}
}