Skip to content

Commit bb37a17

Browse files
authored
Merge pull request #588 from h2o/kazuho/typesafe-getsni
getsni object for logging
2 parents b84869f + a22cafd commit bb37a17

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

include/picotls.h

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,21 +1405,39 @@ uint64_t ptls_decode_quicint(const uint8_t **src, const uint8_t *end);
14051405
ptls_decode_assert_block_close((src), end); \
14061406
} while (0)
14071407

1408+
typedef struct st_ptls_log_getsni_t {
1409+
const char *(*cb)(void *arg);
1410+
void *arg;
1411+
} ptls_log_getsni_t;
1412+
1413+
/**
1414+
* Creates a lazy callback object for obtaining SNI. The object is used to delay materialization of SNI to only when it is needed.
1415+
*/
1416+
#define PTLS_LOG_DEFINE_GETSNI(suffix, type, body) \
1417+
static inline const char *ptls_log_getsni_cb_##suffix(void *_arg) \
1418+
{ \
1419+
type arg = (type)_arg; \
1420+
body \
1421+
} \
1422+
static inline ptls_log_getsni_t ptls_log_getsni_##suffix(type arg) \
1423+
{ \
1424+
return (ptls_log_getsni_t){ptls_log_getsni_cb_##suffix, arg}; \
1425+
}
1426+
14081427
#if PTLS_HAVE_LOG
1409-
#define PTLS_LOG__DO_LOG(module, name, conn_state, get_sni, get_sni_arg, add_time, block) \
1428+
#define PTLS_LOG__DO_LOG(module, name, conn_state, get_sni, add_time, block) \
14101429
do { \
14111430
int ptlslog_include_appdata = 0; \
14121431
do { \
14131432
ptls_log__do_write_start(&logpoint, (add_time)); \
14141433
do { \
14151434
block \
14161435
} while (0); \
1417-
ptlslog_include_appdata = \
1418-
ptls_log__do_write_end(&logpoint, (conn_state), (get_sni), (get_sni_arg), ptlslog_include_appdata); \
1436+
ptlslog_include_appdata = ptls_log__do_write_end(&logpoint, (conn_state), (get_sni), ptlslog_include_appdata); \
14191437
} while (PTLS_UNLIKELY(ptlslog_include_appdata)); \
14201438
} while (0)
14211439
#else
1422-
#define PTLS_LOG__DO_LOG(module, name, conn_state, get_sni, get_sni_arg, add_time, block) /* don't generate code */
1440+
#define PTLS_LOG__DO_LOG(module, name, conn_state, get_sni, add_time, block) /* don't generate code */
14231441
#endif
14241442

14251443
#define PTLS_LOG_DEFINE_POINT(_module, _name, _var) \
@@ -1430,7 +1448,7 @@ uint64_t ptls_decode_quicint(const uint8_t **src, const uint8_t *end);
14301448
PTLS_LOG_DEFINE_POINT(module, name, logpoint); \
14311449
if (PTLS_LIKELY(ptls_log_point_maybe_active(&logpoint) == 0)) \
14321450
break; \
1433-
PTLS_LOG__DO_LOG(module, name, NULL, NULL, NULL, 1, {block}); \
1451+
PTLS_LOG__DO_LOG(module, name, NULL, (ptls_log_getsni_t){NULL}, 1, {block}); \
14341452
} while (0)
14351453

14361454
#define PTLS_LOG_CONN(name, tls, block) \
@@ -1441,10 +1459,10 @@ uint64_t ptls_decode_quicint(const uint8_t **src, const uint8_t *end);
14411459
break; \
14421460
ptls_t *_tls = (tls); \
14431461
ptls_log_conn_state_t *conn_state = ptls_get_log_state(_tls); \
1444-
active &= ptls_log_conn_maybe_active(conn_state, (const char *(*)(void *))ptls_get_server_name, _tls); \
1462+
active &= ptls_log_conn_maybe_active(conn_state, ptls_log_getsni_ptls(_tls)); \
14451463
if (PTLS_LIKELY(active == 0)) \
14461464
break; \
1447-
PTLS_LOG__DO_LOG(picotls, name, conn_state, (const char *(*)(void *))ptls_get_server_name, _tls, 1, { \
1465+
PTLS_LOG__DO_LOG(picotls, name, conn_state, ptls_log_getsni_ptls(_tls), 1, { \
14481466
PTLS_LOG_ELEMENT_PTR(tls, _tls); \
14491467
do { \
14501468
block \
@@ -1571,7 +1589,7 @@ static uint32_t ptls_log_point_maybe_active(struct st_ptls_log_point_t *point);
15711589
/**
15721590
* returns a bitmap indicating the loggers active for given connection
15731591
*/
1574-
static uint32_t ptls_log_conn_maybe_active(ptls_log_conn_state_t *conn, const char *(*get_sni)(void *), void *get_sni_arg);
1592+
static uint32_t ptls_log_conn_maybe_active(ptls_log_conn_state_t *conn, ptls_log_getsni_t getsni);
15751593

15761594
/**
15771595
* Returns the number of log events that were unable to be emitted.
@@ -1588,8 +1606,7 @@ size_t ptls_log_num_lost(void);
15881606
int ptls_log_add_fd(int fd, float sample_ratio, const char *points, const char *snis, const char *addresses, int appdata);
15891607

15901608
void ptls_log__recalc_point(int caller_locked, struct st_ptls_log_point_t *point);
1591-
void ptls_log__recalc_conn(int caller_locked, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *),
1592-
void *get_sni_arg);
1609+
void ptls_log__recalc_conn(int caller_locked, struct st_ptls_log_conn_state_t *conn, ptls_log_getsni_t getsni);
15931610
void ptls_log__do_push_element_safestr(const char *prefix, size_t prefix_len, const char *s, size_t l);
15941611
void ptls_log__do_push_element_unsafestr(const char *prefix, size_t prefix_len, const char *s, size_t l);
15951612
void ptls_log__do_push_element_hexdump(const char *prefix, size_t prefix_len, const void *s, size_t l);
@@ -1603,8 +1620,8 @@ void ptls_log__do_push_appdata_element_unsafestr(int includes_appdata, const cha
16031620
void ptls_log__do_push_appdata_element_hexdump(int includes_appdata, const char *prefix, size_t prefix_len, const void *s,
16041621
size_t l);
16051622
void ptls_log__do_write_start(struct st_ptls_log_point_t *point, int add_time);
1606-
int ptls_log__do_write_end(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *),
1607-
void *get_sni_arg, int includes_appdata);
1623+
int ptls_log__do_write_end(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, ptls_log_getsni_t getsni,
1624+
int includes_appdata);
16081625

16091626
/**
16101627
* create a client object to handle new TLS connection
@@ -1963,6 +1980,8 @@ extern ptls_get_time_t ptls_get_time;
19631980
*/
19641981
static void ptls_hash_clone_memcpy(void *dst, const void *src, size_t size);
19651982

1983+
PTLS_LOG_DEFINE_GETSNI(ptls, ptls_t *, { return ptls_get_server_name(arg); })
1984+
19661985
/* inline functions */
19671986

19681987
inline uint32_t ptls_log_point_maybe_active(struct st_ptls_log_point_t *point)
@@ -1981,11 +2000,11 @@ inline void ptls_log_recalc_conn_state(ptls_log_conn_state_t *state)
19812000
state->state.generation = 0;
19822001
}
19832002

1984-
inline uint32_t ptls_log_conn_maybe_active(ptls_log_conn_state_t *conn, const char *(*get_sni)(void *), void *get_sni_arg)
2003+
inline uint32_t ptls_log_conn_maybe_active(ptls_log_conn_state_t *conn, ptls_log_getsni_t getsni)
19852004
{
19862005
#if PTLS_HAVE_LOG
19872006
if (PTLS_UNLIKELY(conn->state.generation != ptls_log._generation))
1988-
ptls_log__recalc_conn(0, conn, get_sni, get_sni_arg);
2007+
ptls_log__recalc_conn(0, conn, getsni);
19892008
return conn->state.active_conns;
19902009
#else
19912010
return 0;

lib/picotls.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6928,16 +6928,15 @@ void ptls_log__recalc_point(int caller_locked, struct st_ptls_log_point_t *point
69286928
pthread_mutex_unlock(&logctx.mutex);
69296929
}
69306930

6931-
void ptls_log__recalc_conn(int caller_locked, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *),
6932-
void *get_sni_arg)
6931+
void ptls_log__recalc_conn(int caller_locked, struct st_ptls_log_conn_state_t *conn, ptls_log_getsni_t getsni)
69336932
{
69346933
if (!caller_locked)
69356934
pthread_mutex_lock(&logctx.mutex);
69366935

69376936
if (conn->state.generation != ptls_log._generation) {
69386937
/* update active bitmap */
69396938
uint32_t new_active = 0;
6940-
const char *sni = get_sni != NULL ? get_sni(get_sni_arg) : NULL;
6939+
const char *sni = getsni.cb != NULL ? getsni.cb(getsni.arg) : NULL;
69416940
for (size_t slot = 0; slot < PTLS_ELEMENTSOF(logctx.conns); ++slot) {
69426941
if (logctx.conns[slot].points != NULL && conn->random_ < logctx.conns[slot].sample_ratio &&
69436942
is_in_stringlist(logctx.conns[slot].snis, sni) &&
@@ -7090,8 +7089,8 @@ void ptls_log__do_write_start(struct st_ptls_log_point_t *point, int add_time)
70907089
logbuf.buf.off = (size_t)written;
70917090
}
70927091

7093-
int ptls_log__do_write_end(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *),
7094-
void *get_sni_arg, int includes_appdata)
7092+
int ptls_log__do_write_end(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, ptls_log_getsni_t getsni,
7093+
int includes_appdata)
70957094
{
70967095
if (!expand_logbuf_or_invalidate("}\n", 2, 0))
70977096
return 0;
@@ -7105,7 +7104,7 @@ int ptls_log__do_write_end(struct st_ptls_log_point_t *point, struct st_ptls_log
71057104
ptls_log__recalc_point(1, point);
71067105
uint32_t active = point->state.active_conns;
71077106
if (conn != NULL && conn->state.generation != ptls_log._generation) {
7108-
ptls_log__recalc_conn(1, conn, get_sni, get_sni_arg);
7107+
ptls_log__recalc_conn(1, conn, getsni);
71097108
active &= conn->state.active_conns;
71107109
}
71117110

0 commit comments

Comments
 (0)