Skip to content

Commit 78eb76c

Browse files
mattcaswellandrewkdinh
authored andcommitted
Add the ablity to set a local address for BIO_dgram_pair
BIOs created from a BIO_dgram_pair don't normally have a local BIO_ADDR associated with them. This allows us to set one. Fixes openssl/project#933
1 parent b7aeed3 commit 78eb76c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

crypto/bio/bss_dgram_pair.c

+20
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ struct bio_dgram_pair_st {
256256
size_t mtu;
257257
/* Capability flags. */
258258
uint32_t cap;
259+
/* The local address to use (if set) */
260+
BIO_ADDR *local_addr;
259261
/*
260262
* This lock protects updates to our rbuf. Since writes are directed to our
261263
* own rbuf, this means we use this lock for writes and our peer's lock for
@@ -405,6 +407,8 @@ static int dgram_pair_ctrl_destroy_bio_pair(BIO *bio1)
405407
ring_buf_destroy(&b1->rbuf);
406408
bio1->init = 0;
407409

410+
BIO_ADDR_free(b1->local_addr);
411+
408412
/* Early return if we don't have a peer. */
409413
if (b1->peer == NULL)
410414
return 1;
@@ -634,6 +638,16 @@ static int dgram_pair_ctrl_set_mtu(BIO *bio, size_t mtu)
634638
return 1;
635639
}
636640

641+
/* BIO_dgram_set0_local_addr (BIO_CTRL_DGRAM_SET0_LOCAL_ADDR) */
642+
static int dgram_pair_ctrl_set0_local_addr(BIO *bio, BIO_ADDR *addr)
643+
{
644+
struct bio_dgram_pair_st *b = bio->ptr;
645+
646+
BIO_ADDR_free(b->local_addr);
647+
b->local_addr = addr;
648+
return 1;
649+
}
650+
637651
/* Partially threadsafe (some commands) */
638652
static long dgram_mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
639653
{
@@ -731,6 +745,10 @@ static long dgram_mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
731745
ret = (long)dgram_pair_ctrl_set_mtu(bio, (uint32_t)num);
732746
break;
733747

748+
case BIO_CTRL_DGRAM_SET0_LOCAL_ADDR:
749+
ret = (long)dgram_pair_ctrl_set0_local_addr(bio, (BIO_ADDR *)ptr);
750+
break;
751+
734752
/*
735753
* BIO_eof: Returns whether this half of the BIO pair is empty of data to
736754
* read.
@@ -1230,6 +1248,8 @@ static ossl_ssize_t dgram_pair_write_actual(BIO *bio, const char *buf, size_t sz
12301248

12311249
hdr.len = sz;
12321250
hdr.dst_addr = (peer != NULL ? *peer : zero_addr);
1251+
if (local == NULL)
1252+
local = b->local_addr;
12331253
hdr.src_addr = (local != NULL ? *local : zero_addr);
12341254

12351255
saved_idx = b->rbuf.idx[0];

include/openssl/bio.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ extern "C" {
194194
# define BIO_CTRL_GET_RPOLL_DESCRIPTOR 91
195195
# define BIO_CTRL_GET_WPOLL_DESCRIPTOR 92
196196
# define BIO_CTRL_DGRAM_DETECT_PEER_ADDR 93
197+
# define BIO_CTRL_DGRAM_SET0_LOCAL_ADDR 94
197198

198199
# define BIO_DGRAM_CAP_NONE 0U
199200
# define BIO_DGRAM_CAP_HANDLES_SRC_ADDR (1U << 0)
@@ -670,6 +671,8 @@ int BIO_ctrl_reset_read_request(BIO *b);
670671
(unsigned int)BIO_ctrl((b), BIO_CTRL_DGRAM_GET_MTU, 0, NULL)
671672
# define BIO_dgram_set_mtu(b, mtu) \
672673
(int)BIO_ctrl((b), BIO_CTRL_DGRAM_SET_MTU, (mtu), NULL)
674+
# define BIO_dgram_set0_local_addr(b, addr) \
675+
(int)BIO_ctrl((b), BIO_CTRL_DGRAM_SET0_LOCAL_ADDR, 0, (addr))
673676

674677
/* ctrl macros for BIO_f_prefix */
675678
# define BIO_set_prefix(b,p) BIO_ctrl((b), BIO_CTRL_SET_PREFIX, 0, (void *)(p))

test/helpers/quictestlib.c

+14
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,27 @@ int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
193193
goto err;
194194
#endif
195195
} else {
196+
BIO_ADDR *localaddr = NULL;
197+
196198
if (!TEST_true(BIO_new_bio_dgram_pair(&cbio, 0, &sbio, 0)))
197199
goto err;
198200

199201
if (!TEST_true(BIO_dgram_set_caps(cbio, BIO_DGRAM_CAP_HANDLES_DST_ADDR))
200202
|| !TEST_true(BIO_dgram_set_caps(sbio, BIO_DGRAM_CAP_HANDLES_DST_ADDR)))
201203
goto err;
202204

205+
if (!TEST_ptr(localaddr = BIO_ADDR_new()))
206+
goto err;
207+
/* Dummy client local addresses */
208+
if (!TEST_true(BIO_ADDR_rawmake(localaddr, AF_INET, &ina, sizeof(ina),
209+
htons(0)))) {
210+
BIO_ADDR_free(localaddr);
211+
goto err;
212+
}
213+
if (!TEST_int_eq(BIO_dgram_set0_local_addr(cbio, localaddr), 1)) {
214+
BIO_ADDR_free(localaddr);
215+
goto err;
216+
}
203217
/* Dummy server address */
204218
if (!TEST_true(BIO_ADDR_rawmake(peeraddr, AF_INET, &ina, sizeof(ina),
205219
htons(0))))

0 commit comments

Comments
 (0)