BIO_S_FD(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… BIO_S_FD β€” the OpenSSL manual, bestie edition πŸŽ€

okay so this is the BIO_S_FD(3ossl) page from the OpenSSL manual β€” yes, that OpenSSL β€” and we're about to make file descriptor BIOs make sense. grab a strawberry drink πŸ“, we're going in.


✨ NAME ✨

BIO_s_fd, BIO_set_fd, BIO_get_fd, BIO_new_fd β€” the file descriptor BIO, aka the little wrapper that lets a BIO talk to a raw fd. iconic behavior honestly.


🌸 SYNOPSIS 🌸

#include <openssl/bio.h>

const BIO_METHOD *BIO_s_fd(void);

int BIO_set_fd(BIO *b, int fd, int c);
int BIO_get_fd(BIO *b, int *c);

BIO *BIO_new_fd(int fd, int close_flag);

like, that's the whole vibe check right there. four functions, zero drama.


πŸ’– DESCRIPTION πŸ’–

BIO_s_fd() returns the file descriptor BIO method. it's literally a wrapper round the platform's file descriptor routines β€” think read() and write() but wearing a cute little BIO outfit. πŸŽ€

BIO_read_ex() and BIO_write_ex() read or write the underlying descriptor. BIO_puts() is supported but BIO_gets() is not β€” so don't come for her with BIO_gets(), she simply doesn't do that. πŸ™ˆ

if the close flag is set, then close() is called on the underlying file descriptor when the BIO is freed. so like, she cleans up after herself. slay.

BIO_reset() attempts to change the file pointer to the start of file β€” such as by using lseek(fd, 0, 0).

BIO_seek() sets the file pointer to position ofs from start of file β€” such as by using lseek(fd, ofs, 0).

BIO_tell() returns the current file position β€” such as by calling lseek(fd, 0, 1).

BIO_set_fd() sets the file descriptor of BIO b to fd and the close flag to c.

BIO_get_fd() places the file descriptor of BIO b in c if it is not NULL. it also returns the file descriptor. two for one, we love a multitasker. πŸ’…

BIO_new_fd() returns a file descriptor BIO using fd and close_flag.


πŸ¦‹ NOTES πŸ¦‹

the behaviour of BIO_read_ex() and BIO_write_ex() depends on the behavior of the platform's read() and write() calls on the descriptor. so if the underlying file descriptor is in a non blocking mode, then the BIO will behave in the manner described in the BIO_read_ex(3) and BIO_should_retry(3) manual pages. iykyk. ✨

🚨 real talk: file descriptor BIOs should not be used for socket I/O. use socket BIOs instead. no cap, that's the rule.

also, BIO_set_fd() and BIO_get_fd() are implemented as macros. surprise! they're macros. not me finding that out the hard way 😭


🌈 RETURN VALUES 🌈

  • BIO_s_fd() returns the file descriptor BIO method.
  • BIO_set_fd() returns 1 on success or <=0 for failure.
  • BIO_get_fd() returns the file descriptor, or -1 if the BIO has not been initialized. it also returns zero and negative values if other error occurs.
  • BIO_new_fd() returns the newly allocated BIO, or NULL is an error occurred.

so like, check your return values bestie. -1 is not a vibe. πŸ’€


⭐ EXAMPLES ⭐

this is a file descriptor BIO version of "Hello World" β€” and honestly? she ate.

BIO *out;

out = BIO_new_fd(fileno(stdout), BIO_NOCLOSE);
BIO_printf(out, "Hello World\n");
BIO_free(out);

four lines. hello world. main character energy. πŸ‘‘


🧚 SEE ALSO 🧚

BIO_seek(3), BIO_tell(3), BIO_reset(3), BIO_read_ex(3), BIO_write_ex(3), BIO_puts(3), BIO_gets(3), BIO_printf(3), BIO_set_close(3), BIO_get_close(3)

(the full friend group. go read them next, fr fr. 🫢)


Copyright 2000-2021 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 page!! πŸ’– this was OpenSSL 3.5.0, dated 2026-09-25, and it's the BIO_S_FD(3ossl) manual page β€” go forth and wrap those file descriptors, bestie. mwah. πŸŽ€βœ¨

πŸ“¦ where this page comes from