Skip to content

Commit

Permalink
Hash transport instance pointer when using it in event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero committed Sep 9, 2020
1 parent 53d8696 commit a8e0470
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion events.c
Expand Up @@ -235,7 +235,10 @@ void janus_events_notify_handlers(int type, int subtype, guint64 session_id, ...
char *instance = va_arg(args, void *);
char id[32];
memset(id, 0, sizeof(id));
g_snprintf(id, sizeof(id), "%p", instance);
/* To avoid sending a stringified version of the transport pointer
* around, we convert it to a number and hash it instead */
uint64_t p = janus_uint64_hash(GPOINTER_TO_UINT(instance));
g_snprintf(id, sizeof(id), "%"SCNu64, p);
json_object_set_new(body, "id", json_string(id));
json_t *data = va_arg(args, json_t *);
json_object_set_new(body, "data", data);
Expand Down
5 changes: 4 additions & 1 deletion janus.c
Expand Up @@ -1051,7 +1051,10 @@ int janus_process_incoming_request(janus_request *request) {
json_object_set_new(transport, "transport", json_string(session->source->transport->get_package()));
char id[32];
memset(id, 0, sizeof(id));
g_snprintf(id, sizeof(id), "%p", session->source->instance);
/* To avoid sending a stringified version of the transport pointer
* around, we convert it to a number and hash it instead */
uint64_t p = janus_uint64_hash(GPOINTER_TO_UINT(session->source->instance));
g_snprintf(id, sizeof(id), "%"SCNu64, p);
json_object_set_new(transport, "id", json_string(id));
janus_events_notify_handlers(JANUS_EVENT_TYPE_SESSION, JANUS_EVENT_SUBTYPE_NONE,
session_id, "created", transport);
Expand Down
7 changes: 7 additions & 0 deletions utils.c
Expand Up @@ -135,6 +135,13 @@ guint64 *janus_uint64_dup(guint64 num) {
return numdup;
}

guint64 janus_uint64_hash(guint64 num) {
num = (num ^ (num >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
num = (num ^ (num >> 27)) * UINT64_C(0x94d049bb133111eb);
num = num ^ (num >> 31);
return num;
}

int janus_string_to_uint8(const char *str, uint8_t *num) {
if(str == NULL || num == NULL)
return -EINVAL;
Expand Down
6 changes: 6 additions & 0 deletions utils.h
Expand Up @@ -82,6 +82,12 @@ char *janus_random_uuid(void);
* @returns A pointer to a guint64 number, if successful, NULL otherwise */
guint64 *janus_uint64_dup(guint64 num);

/*! \brief Helper to hash a guint64 number to another guint64 number
* @note We currently only use for event handlers
* @param
* @returns The hashed number */
guint64 janus_uint64_hash(guint64 num);

/*! \brief Helper method to convert a string to a uint8_t
* @note The value of \c num should be ignored, if the method returned an error
* @param[in] str The string to convert
Expand Down

0 comments on commit a8e0470

Please sign in to comment.