SSL_SESSION_GET0_HOSTNAME(3SSL)

πŸ’– SSL_SESSION_GET0_HOSTNAME πŸ’–

okay bestie, so this is a page from the OpenSSL manual, section 3SSL β€” it's literally all about grabbing and setting the SNI and ALPN data that's chillin' inside a session. yep. two whole vibes, four whole functions, one iconic little header. let's get into it ✨


✨ NAME ✨

SSL_SESSION_get0_hostname, SSL_SESSION_set1_hostname, SSL_SESSION_get0_alpn_selected, SSL_SESSION_set1_alpn_selected β€” get and set SNI and ALPN data associated with a session πŸŽ€

like. it does exactly what it says on the tin, i fear.


🌸 SYNOPSIS 🌸

#include <openssl/ssl.h>

const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s);
int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname);

void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
                                    const unsigned char **alpn,
                                    size_t *len);
int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
                                   size_t len);

look at that include. #include <openssl/ssl.h> bestie. don't forget it or nothing works and that's not a vibe πŸ™ˆ


πŸ¦‹ DESCRIPTION πŸ¦‹

getting the hostname (SNI) 🫢

SSL_SESSION_get0_hostname() goes and fetches the Server Name Indication (SNI) value that the client sent way back when the session was created β€” but only if the server acknowledged the client's SNI extension by including an empty SNI extension in response. if it didn't? you get NULL. that's it. that's the whole deal πŸ’€

the value it hands back is a pointer to memory that lives inside s β€” so do not free it. not yours. hands off. it's giving "borrowed, not gifted" πŸŽ€

setting the hostname πŸ”₯

SSL_SESSION_set1_hostname() sets the SNI value for the hostname to a copy of the string you pass in as hostname. it copies. you keep your own string. very demure, very mindful.

getting the ALPN πŸ¦‹

SSL_SESSION_get0_alpn_selected() retrieves the selected ALPN protocol for this session, plus its associated length in bytes. the returned value of *alpn is β€” you guessed it β€” a pointer to memory maintained within s, and it should not be free'd. same rules, bestie. same rules.

setting the ALPN πŸ’…

SSL_SESSION_set1_alpn_selected() sets the ALPN protocol for this session to whatever's in alpn, which should be len bytes long. a copy of the input value is made, and you β€” yes, you β€” retain ownership of the memory that alpn points to. so it's yours. keep it. water it. whatever 🌱


⭐ RETURN VALUES ⭐

  • SSL_SESSION_get0_hostname() β€” returns the SNI string if it's available, or NULL if it's not. simple as that πŸ’–
  • SSL_SESSION_set1_hostname() β€” returns 1 on success or 0 on error.
  • SSL_SESSION_set1_alpn_selected() β€” returns 1 on success or 0 on error.
no cap: those setters only ever give you 1 or 0. check it. always check it πŸ‘‘

🌈 SEE ALSO 🌈

ssl(7), d2i_SSL_SESSION(3), SSL_SESSION_get_time(3), SSL_SESSION_free(3)

go read those too, they're the friend group of this page 🫢


πŸŽ€ HISTORY πŸŽ€

SSL_SESSION_set1_hostname(), SSL_SESSION_get0_alpn_selected() and SSL_SESSION_set1_alpn_selected() were all added in OpenSSL 1.1.1. so they're not ancient. they're not brand new either. they're just... there. thriving. iconic.


Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


and that's the whole page, bestie πŸ§šπŸ’œ this one's from OpenSSL version 4.0.3, dated 2026-09-29 β€” so if you're citing it, that's the receipt. now go forth and set your SNI, periodt πŸ’…βœ¨

πŸ“¦ where this page comes from