Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparison of IPv6 addresses in session_get_bydata() #1187

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions common/os_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -2822,6 +2822,22 @@ g_pos(const char *str, const char *to_find)
return (pp - str);
}

/*****************************************************************************/
int
g_last_ch_pos(const char *str, char ch_to_find, const int n)
{
int last_pos = -1;
int i;

for (i = 0; i < n && str[i] != 0; i++)
{
if (str[i] == ch_to_find)
last_pos = i;
}

return last_pos;
}

/*****************************************************************************/
int
g_mbstowcs(twchar *dest, const char *src, int n)
Expand Down
1 change: 1 addition & 0 deletions common/os_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ int g_htoi(char* str);
int g_bytes_to_hexstr(const void *bytes, int num_bytes, char *out_str,
int bytes_out_str);
int g_pos(const char* str, const char* to_find);
int g_last_ch_pos(const char *str, char ch_to_find, int n);
int g_mbstowcs(twchar* dest, const char* src, int n);
int g_wcstombs(char* dest, const twchar* src, int n);
int g_strtrim(char* str, int trim_flags);
Expand Down
21 changes: 20 additions & 1 deletion sesman/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ dumpItemsToString(struct list *self, char *outstr, int len)
return outstr ;
}

/* compare two strings containing ip-addresses and ports but ignore the latter */
static int
is_equal_ip_ignoring_port(const char *ip1, const char *ip2, const size_t n)
{
int last_colon_ip1 = g_last_ch_pos(ip1, ':', n);
int last_colon_ip2 = g_last_ch_pos(ip2, ':', n);

if (last_colon_ip1 != last_colon_ip2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please always use brackets.

{
return 0;
}

if (last_colon_ip1 == -1)
{
last_colon_ip1 = n;
}

return g_strncmp(ip1, ip2, last_colon_ip1) == 0;
}

/******************************************************************************/
struct session_item *
Expand Down Expand Up @@ -141,7 +160,7 @@ session_get_bydata(const char *name, int width, int height, int bpp, int type,
(!(policy & SESMAN_CFG_SESS_POLICY_D) ||
(tmp->item->width == width && tmp->item->height == height)) &&
(!(policy & SESMAN_CFG_SESS_POLICY_I) ||
(g_strncmp_d(client_ip, tmp->item->client_ip, ':', 255) == 0)) &&
is_equal_ip_ignoring_port(client_ip, tmp->item->client_ip, 255)) &&
(!(policy & SESMAN_CFG_SESS_POLICY_C) ||
(g_strncmp(client_ip, tmp->item->client_ip, 255) == 0)) &&
tmp->item->bpp == bpp &&
Expand Down