Skip to content

Commit

Permalink
Add BIO_get_new_index()
Browse files Browse the repository at this point in the history
Reviewed-by: Dr. Stephen Henson <steve@openssl.org>
  • Loading branch information
Rich Salz committed Aug 20, 2016
1 parent 9e31356 commit 8b8d963
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 30 deletions.
4 changes: 3 additions & 1 deletion crypto/bio/b_addr.c
Expand Up @@ -19,6 +19,7 @@
#include <ctype.h>

CRYPTO_RWLOCK *bio_lookup_lock;
extern CRYPTO_RWLOCK *bio_type_lock;
static CRYPTO_ONCE bio_lookup_init = CRYPTO_ONCE_STATIC_INIT;

/*
Expand Down Expand Up @@ -605,7 +606,8 @@ static int addrinfo_wrap(int family, int socktype,
DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init)
{
bio_lookup_lock = CRYPTO_THREAD_lock_new();
return (bio_lookup_lock != NULL);
bio_type_lock = CRYPTO_THREAD_lock_new();
return bio_lookup_lock != NULL && bio_type_lock != NULL;
}

/*-
Expand Down
1 change: 1 addition & 0 deletions crypto/bio/bio_lcl.h
Expand Up @@ -137,6 +137,7 @@ typedef unsigned int socklen_t;
# endif

extern CRYPTO_RWLOCK *bio_lookup_lock;
extern CRYPTO_RWLOCK *bio_type_lock;

int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa);
const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap);
Expand Down
2 changes: 2 additions & 0 deletions crypto/bio/bio_lib.c
Expand Up @@ -594,5 +594,7 @@ void bio_cleanup(void)
bio_sock_cleanup_int();
CRYPTO_THREAD_lock_free(bio_lookup_lock);
bio_lookup_lock = NULL;
CRYPTO_THREAD_lock_free(bio_type_lock);
bio_type_lock = NULL;
#endif
}
12 changes: 12 additions & 0 deletions crypto/bio/bio_meth.c
Expand Up @@ -9,6 +9,18 @@

#include "bio_lcl.h"

CRYPTO_RWLOCK *bio_type_lock;
static int bio_count = BIO_TYPE_START;

int BIO_get_new_index()
{
int newval;

if (!CRYPTO_atomic_add(&bio_count, 1, &newval, bio_type_lock))
return -1;
return newval;
}

BIO_METHOD *BIO_meth_new(int type, const char *name)
{
BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
Expand Down
7 changes: 6 additions & 1 deletion doc/crypto/BIO_meth_new.pod
Expand Up @@ -2,6 +2,7 @@

=head1 NAME

BIO_get_new_index,
BIO_meth_new, BIO_meth_free, BIO_meth_get_write, BIO_meth_set_write,
BIO_meth_get_read, BIO_meth_set_read, BIO_meth_get_puts, BIO_meth_set_puts,
BIO_meth_get_gets, BIO_meth_set_gets, BIO_meth_get_ctrl, BIO_meth_set_ctrl,
Expand All @@ -13,6 +14,7 @@ BIO_meth_set_callback_ctrl - Routines to build up BIO methods

#include <openssl/bio.h>

int BIO_get_new_index(void);
BIO_METHOD *BIO_meth_new(int type, const char *name);
void BIO_meth_free(BIO_METHOD *biom);
int (*BIO_meth_get_write(BIO_METHOD *biom)) (BIO *, const char *, int);
Expand Down Expand Up @@ -47,7 +49,10 @@ types. It provides a set of of functions used by OpenSSL for the implementation
of the various BIO capabilities. See the L<bio> page for more information.

BIO_meth_new() creates a new B<BIO_METHOD> structure. It should be given a
unique integer B<type> and a string that represents its B<name>. The set of
unique integer B<type> and a string that represents its B<name>.
Use BIO_get_new_index() to get the value for B<type>.

The set of
standard OpenSSL provided BIO types is provided in B<bio.h>. Some examples
include B<BIO_TYPE_BUFFER> and B<BIO_TYPE_CIPHER>. Filter BIOs should have a
type which have the "filter" bit set (B<BIO_TYPE_FILTER>). Source/sink BIOs
Expand Down
58 changes: 30 additions & 28 deletions include/openssl/bio.h
Expand Up @@ -31,38 +31,39 @@
extern "C" {
#endif

/* There are the classes of BIOs */
# define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */
# define BIO_TYPE_FILTER 0x0200
# define BIO_TYPE_SOURCE_SINK 0x0400

/* These are the 'types' of BIOs */
# define BIO_TYPE_NONE 0
# define BIO_TYPE_MEM (1|0x0400)
# define BIO_TYPE_FILE (2|0x0400)

# define BIO_TYPE_FD (4|0x0400|0x0100)
# define BIO_TYPE_SOCKET (5|0x0400|0x0100)
# define BIO_TYPE_NULL (6|0x0400)
# define BIO_TYPE_SSL (7|0x0200)
# define BIO_TYPE_MD (8|0x0200)/* passive filter */
# define BIO_TYPE_BUFFER (9|0x0200)/* filter */
# define BIO_TYPE_CIPHER (10|0x0200)/* filter */
# define BIO_TYPE_BASE64 (11|0x0200)/* filter */
# define BIO_TYPE_CONNECT (12|0x0400|0x0100)/* socket - connect */
# define BIO_TYPE_ACCEPT (13|0x0400|0x0100)/* socket for accept */
/* # define BIO_TYPE_PROXY_CLIENT (14|0x0200)*/ /* client proxy BIO */
/* # define BIO_TYPE_PROXY_SERVER (15|0x0200)*/ /* server proxy BIO */
# define BIO_TYPE_NBIO_TEST (16|0x0200)/* server proxy BIO */
# define BIO_TYPE_NULL_FILTER (17|0x0200)
# define BIO_TYPE_BER (18|0x0200)/* BER -> bin filter */
# define BIO_TYPE_BIO (19|0x0400)/* (half a) BIO pair */
# define BIO_TYPE_LINEBUFFER (20|0x0200)/* filter */
# define BIO_TYPE_DGRAM (21|0x0400|0x0100)
# define BIO_TYPE_NONE 0
# define BIO_TYPE_MEM ( 1|BIO_TYPE_SOURCE_SINK)
# define BIO_TYPE_FILE ( 2|BIO_TYPE_SOURCE_SINK)

# define BIO_TYPE_FD ( 4|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR)
# define BIO_TYPE_SOCKET ( 5|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR)
# define BIO_TYPE_NULL ( 6|BIO_TYPE_SOURCE_SINK)
# define BIO_TYPE_SSL ( 7|BIO_TYPE_FILTER)
# define BIO_TYPE_MD ( 8|BIO_TYPE_FILTER)
# define BIO_TYPE_BUFFER ( 9|BIO_TYPE_FILTER)
# define BIO_TYPE_CIPHER (10|BIO_TYPE_FILTER)
# define BIO_TYPE_BASE64 (11|BIO_TYPE_FILTER)
# define BIO_TYPE_CONNECT (12|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR)
# define BIO_TYPE_ACCEPT (13|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR)

# define BIO_TYPE_NBIO_TEST (16|BIO_TYPE_FILTER)/* server proxy BIO */
# define BIO_TYPE_NULL_FILTER (17|BIO_TYPE_FILTER)
# define BIO_TYPE_BIO (19|BIO_TYPE_SOURCE_SINK)/* half a BIO pair */
# define BIO_TYPE_LINEBUFFER (20|BIO_TYPE_FILTER)
# define BIO_TYPE_DGRAM (21|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR)
# define BIO_TYPE_ASN1 (22|BIO_TYPE_FILTER)
# define BIO_TYPE_COMP (23|BIO_TYPE_FILTER)
# ifndef OPENSSL_NO_SCTP
# define BIO_TYPE_DGRAM_SCTP (24|0x0400|0x0100)
# define BIO_TYPE_DGRAM_SCTP (24|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR)
# endif
# define BIO_TYPE_ASN1 (22|0x0200)/* filter */
# define BIO_TYPE_COMP (23|0x0200)/* filter */

# define BIO_TYPE_DESCRIPTOR 0x0100/* socket, fd, connect or accept */
# define BIO_TYPE_FILTER 0x0200
# define BIO_TYPE_SOURCE_SINK 0x0400
#define BIO_TYPE_START 128

/*
* BIO_FILENAME_READ|BIO_CLOSE to open or close on free.
Expand Down Expand Up @@ -177,6 +178,7 @@ extern "C" {
typedef union bio_addr_st BIO_ADDR;
typedef struct bio_addrinfo_st BIO_ADDRINFO;

int BIO_get_new_index(void);
void BIO_set_flags(BIO *b, int flags);
int BIO_test_flags(const BIO *b, int flags);
void BIO_clear_flags(BIO *b, int flags);
Expand Down
1 change: 1 addition & 0 deletions util/libcrypto.num
Expand Up @@ -4198,3 +4198,4 @@ X509_CRL_get0_lastUpdate 4144 1_1_0 EXIST::FUNCTION:
X509_get0_notBefore 4145 1_1_0 EXIST::FUNCTION:
X509_get0_notAfter 4146 1_1_0 EXIST::FUNCTION:
X509_CRL_get0_nextUpdate 4147 1_1_0 EXIST::FUNCTION:
BIO_get_new_index 4148 1_1_0 EXIST::FUNCTION:

0 comments on commit 8b8d963

Please sign in to comment.