Can I send and receive raw data with quic protocol using socket on linux?
Assistant
User
I need a stream socket associated with individual connection with quic, is it possible to get it with some library? How to close this socket, are there options like closing with fin or rst similar to how tcp works?
Assistant
Yes, you can send and receive raw data using the QUIC protocol on Linux, but it requires a bit more setup compared to traditional TCP or UDP socket programming. QUIC (Quick UDP Internet Connections) is a transport layer network protocol developed by Google, designed to improve upon TCP in terms of speed and efficiency. It operates over UDP, providing features like multiplexed connections, reduced latency, and improved congestion control.
To work with QUIC in a Linux environment, you typically need to use a library or a framework that implements the QUIC protocol, as the standard system sockets API does not natively support QUIC. Here's a general approach to using QUIC on Linux:
Install a QUIC Library: You need a library that implements the QUIC protocol. Popular libraries include:
Quiche (by Cloudflare): Written in Rust, can be integrated into applications written in various languages.
MsQuic (by Microsoft): A cross-platform, performant, and fully featured implementation of the QUIC protocol.
ngtcp2: A C library that has an accompanying simple QUIC client/server example.
Google's QUICHE: The QUIC implementation used in Chromium.
Set Up Your Environment: Depending on the library you choose, you'll need to install Rust, CMake, or other prerequisites. Make sure your development environment is ready to compile and link with these libraries.
Write Your Application: Use the chosen library's API to create a QUIC client or server:
Initialize the QUIC context and any necessary configurations.
Use UDP sockets to send and receive QUIC packets, as QUIC operates over UDP.
Implement handshake and session management using the library's facilities.
Handle data transfer, leveraging the multiplexing capabilities of QUIC streams.
Run and Test: After implementation, test your application to ensure that data is being sent and received correctly over QUIC. You'll also want to test things like connection establishment, error handling, and performance metrics.
Here is a very simple pseudo-outline of what a basic setup might look like using a QUIC library:
c
// Pseudo-code outline
#include <quic_library.h>
int main() {
// Initialize QUIC library context
quic_context *ctx = quic_init();
// Configure QUIC settings and create a socket
quic_configure(ctx, ...);
int udp_socket = create_udp_socket();
// Establish a QUIC connection
quic_connection *conn = quic_connect(ctx, udp_socket, ...);
// Sending data
quic_stream *stream = quic_create_stream(conn);
quic_send(stream, "Hello, QUIC!", strlen("Hello, QUIC!"));
// Receiving data
char buffer[256];
quic_receive(stream, buffer, sizeof(buffer));
printf("Received: %s\n", buffer);
// Clean up
quic_stream_close(stream);
quic_close_connection(conn);
quic_cleanup(ctx);
return 0;
}
Make sure to refer to the specific documentation of the library you choose, as each library will have its own API calls, setup procedures, and nuanced behavior.
In QUIC, the concept of "stream sockets" as in traditional TCP/IP socket programming doesn't apply directly, because QUIC operates over UDP and manages connections and streams at a higher abstraction level. However, QUIC does provide functionality for managing streams within a connection that offers similar, and in many cases, richer capabilities than standard TCP streams.
Implementing Streams in QUIC
Stream Management:
QUIC allows you to create multiple independent streams within a single connection. This is one of QUIC's significant advantages over TCP, as it allows for multiplexing data without head-of-line blocking.
Each stream is independently managed by the QUIC library, and you interact with these streams through the API provided by the library you use.
Closing Streams:
Graceful Closure (FIN equivalent): You can close a QUIC stream gracefully once you have finished sending data, similar to sending a FIN in TCP. This tells the receiver that no more data will be sent on that stream, but still allows for any remaining data in transit to be acknowledged.
Abortive Closure (RST equivalent): You can also abort a stream, effectively similar to an RST in TCP. This informs the peer that the stream has been abruptly terminated and any outstanding data should be discarded.
Using Libraries
Different libraries will handle these operations in slightly different ways, but the functionality is generally available. Let’s look at some general examples using popular QUIC libraries:
Example with MsQuic (Microsoft's QUIC library)
MsQuic provides a comprehensive API for managing connections and streams. Here’s a simplified example of how you might manage streams:
c
#include <msquic.h>
// This is a pseudo-code outline
void CreateStream(HQUIC Connection) {
HQUIC Stream = nullptr;
// Open a bidirectional stream
MsQuic->StreamOpen(
Connection,
QUIC_STREAM_OPEN_FLAG_NONE, // no special flags
StreamHandler, // Callback when stream events occur
nullptr, // Context
&Stream);
// Start the stream
MsQuic->StreamStart(
Stream,
QUIC_STREAM_START_FLAG_NONE);
// Send data on the stream
QUIC_BUFFER Buffer = { sizeof(Data), (uint8_t*)Data };
MsQuic->StreamSend(
Stream,
&Buffer,
1, // Buffer count
QUIC_SEND_FLAG_FIN, // Send with FIN flag, closing the stream
nullptr);
// Close the stream properly
MsQuic->StreamClose(Stream);
}
Example with ngtcp2
Using ngtcp2, you would create and close streams in a similar fashion, leveraging the library’s API that abstracts these details:
c
// Pseudo code for ngtcp2
#include <ngtcp2/ngtcp2.h>
void HandleStream(ngtcp2_conn *conn) {
// To send data on a stream
ngtcp2_conn_open_bidi_stream(conn, &stream_id, nullptr);
ngtcp2_conn_write_stream(
conn, nullptr, Buffer, BufferSize, stream_id, NGTCP2_WRITE_STREAM_FLAG_FIN);
// Close stream
// Closing in QUIC is hijacked by the application logic and the library
// Typically managed by the API by sending FIN or using specific flags
}
Closing Connections
QUIC itself abstracts connection teardown similar to TCP's RST via the connection close message. This can be initiated using library-specific calls to close the connection gracefully or abruptly, depending on your requirements.
Graceful Closure: This can be initiated by sending a CONNECTION_CLOSE frame, allowing any in-flight packets to be processed.
Abortive Closure: Occurs when a CONNECTION_CLOSE frame is sent with an error code, similar to how a TCP RST is used to terminate a connection immediately.
It’s crucial to consult the documentation for the specific library you’re using to understand the exact API calls and flags available for managing streams and closing connections. Each library provides comprehensive examples and API references to guide implementation.