gnutls_alpn_get_selected_protocol - Linux


Overview

The gnutls_alpn_get_selected_protocol function returns the selected application protocol as a null-terminated string if an ALPN extension was negotiated, or NULL otherwise.

Syntax

char * gnutls_alpn_get_selected_protocol(gnutls_session_t session);

Options/Flags

None.

Examples

The following example demonstrates how to use gnutls_alpn_get_selected_protocol to get the selected ALPN protocol:

#include <gnutls/gnutls.h>

int main() {
    gnutls_session_t session;
    gnutls_certificate_credentials_t x509_cred;

    // Initialize the TLS session
    gnutls_init(&session, GNUTLS_CLIENT);

    // Set the ALPN protocols
    gnutls_alpn_set_protocols(session, "http/1.1", "h2");

    // Load TLS credentials
    gnutls_certificate_allocate_credentials(&x509_cred);

    // Connect to the server
    gnutls_connect(session, "example.com", 443);

    // Check if an ALPN protocol was negotiated
    if (gnutls_alpn_get_selected_protocol(session)) {
        printf("The selected ALPN protocol is: %s\n", gnutls_alpn_get_selected_protocol(session));
    } else {
        printf("No ALPN protocol was negotiated.\n");
    }

    // Cleanup
    gnutls_certificate_free_credentials(x509_cred);
    gnutls_deinit(session);

    return 0;
}

Common Issues

If no ALPN protocol was negotiated, gnutls_alpn_get_selected_protocol will return NULL.

Integration

gnutls_alpn_get_selected_protocol can be combined with other GnuTLS functions to handle TLS/SSL connections and negotiate ALPN protocols.

Related Commands

  • gnutls_alpn_add_protocol
  • gnutls_alpn_set_protocols