Skip to content

Commit 976a2fa

Browse files
committed
v0.9.1
-make ls_epan_field_value_get_bytes() more robust
1 parent d961926 commit 976a2fa

File tree

8 files changed

+118
-91
lines changed

8 files changed

+118
-91
lines changed

build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
lemonshark_major_version: int = 0
3838
lemonshark_minor_version: int = 9
39-
lemonshark_patch_version: int = 0
39+
lemonshark_patch_version: int = 1
4040

4141
wireshark_git_url: str = "https://gitlab.com/wireshark/wireshark.git"
4242
wireshark_major_version: int = 4

dotnet/LemonShark/EpanField.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,11 @@ public byte[] BytesValue
504504

505505
byte[] result = new byte[length];
506506

507-
_ = ls_epan_field_value_get_bytes(EpanFieldReference, result, length);
507+
int actualLength = ls_epan_field_value_get_bytes(EpanFieldReference, result, length);
508+
if (actualLength < 0)
509+
{
510+
return null;
511+
}
508512

509513
return result;
510514
}

dotnet/LemonShark/LemonShark.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
<Authors>DevAM</Authors>
1313
<Company>DevAM</Company>
1414
<Product>LemonShark</Product>
15-
<Version>0.9.0</Version>
16-
<AssemblyVersion>0.9.0</AssemblyVersion>
17-
<FileVersion>0.9.0</FileVersion>
15+
<Version>0.9.1</Version>
16+
<AssemblyVersion>0.9.1</AssemblyVersion>
17+
<FileVersion>0.9.1</FileVersion>
1818
<Description>Wireshark bindings for C# and .NET.</Description>
1919
<Copyright>Copyright (c) 2024 DevAM. All Rights Reserved.</Copyright>
2020
<PackageIcon>icon.png</PackageIcon>

liblemonshark/examples/epan_packet/epan_packet_demo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static void print_fields(epan_field_t *epan_field, void *parameter)
9999
{
100100
if (field_type != ls_field_type_protocol())
101101
{
102-
length = ls_epan_field_length_get(epan_field);
102+
length = ls_epan_field_value_bytes_length(epan_field);
103103
guint8 *value = g_malloc(length);
104104
ls_epan_field_value_get_bytes(epan_field, value, length);
105105

liblemonshark/src/epan_field.c

Lines changed: 96 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ SPDX-License-Identifier: GPL-2.0-only
88

99
// wireshark includes
1010
#include "epan/proto.h"
11+
#include "epan/exceptions.h"
1112

1213
// lemonshark includes
1314
#include "ls_common.h"
@@ -405,28 +406,35 @@ const char *ls_epan_field_value_get_string(epan_field_t *epan_field)
405406
const header_field_info *current_header_field_info = current_field_info->hfinfo;
406407

407408
const char *value = NULL;
408-
409-
switch (current_header_field_info->type)
409+
TRY
410410
{
411-
case FT_STRING:
412-
case FT_STRINGZ:
413-
case FT_STRINGZPAD:
414-
case FT_STRINGZTRUNC:
415-
case FT_UINT_STRING:
416-
case FT_AX25:
417-
{
418-
const wmem_strbuf_t *string_buffer = fvalue_get_strbuf(current_field_info->value);
419-
value = wmem_strbuf_get_str(string_buffer);
411+
switch (current_header_field_info->type)
412+
{
413+
case FT_STRING:
414+
case FT_STRINGZ:
415+
case FT_STRINGZPAD:
416+
case FT_STRINGZTRUNC:
417+
case FT_UINT_STRING:
418+
case FT_AX25:
419+
{
420+
const wmem_strbuf_t *string_buffer = fvalue_get_strbuf(current_field_info->value);
421+
value = wmem_strbuf_get_str(string_buffer);
422+
}
423+
break;
424+
case FT_NONE:
425+
{
426+
value = current_field_info->rep->representation;
427+
}
428+
break;
429+
default:
430+
break;
431+
}
420432
}
421-
break;
422-
case FT_NONE:
433+
CATCH_ALL
423434
{
424-
value = current_field_info->rep->representation;
425-
}
426-
break;
427-
default:
428-
break;
435+
value = NULL;
429436
}
437+
ENDTRY;
430438

431439
return value;
432440
}
@@ -449,82 +457,90 @@ gint32 ls_epan_field_value_get_bytes(epan_field_t *epan_field, guint8 *target, g
449457

450458
gint32 length = 0;
451459

452-
switch (current_header_field_info->type)
460+
TRY
453461
{
454-
case FT_ETHER:
455-
case FT_BYTES:
456-
case FT_UINT_BYTES:
457-
{
458-
length = (gint32)(fvalue_get_bytes_size(current_field_info->value) & 0x7FFFFFFF);
459-
const guint8 *value = (const guint8 *)fvalue_get_bytes_data(current_field_info->value);
460-
memcpy(target, value, length < max_length ? length : max_length);
461-
}
462-
break;
463-
464-
case FT_IPv6:
465-
{
466-
const ipv6_addr_and_prefix *current_ipv6_addr_and_prefix = fvalue_get_ipv6(current_field_info->value);
467-
length = 16;
468-
const guint8 *value = (const guint8 *)current_ipv6_addr_and_prefix->addr.bytes;
469-
memcpy(target, value, length < max_length ? length : max_length);
470-
}
471-
break;
472-
case FT_GUID:
473-
{
474-
const e_guid_t *guid = fvalue_get_guid(current_field_info->value);
475-
476-
length = 0;
477-
478-
if (max_length >= 4)
462+
switch (current_header_field_info->type)
479463
{
480-
((guint32 *)target)[0] = (guint32)guid->data1;
481-
length = 4;
482-
}
483-
if (max_length >= 6)
464+
case FT_ETHER:
465+
case FT_BYTES:
466+
case FT_UINT_BYTES:
484467
{
485-
((guint16 *)target)[2] = (guint16)guid->data2;
486-
length = 6;
468+
length = (gint32)(fvalue_get_bytes_size(current_field_info->value) & 0x7FFFFFFF);
469+
const guint8 *value = (const guint8 *)fvalue_get_bytes_data(current_field_info->value);
470+
memcpy(target, value, length < max_length ? length : max_length);
487471
}
488-
if (max_length >= 8)
472+
break;
473+
474+
case FT_IPv6:
489475
{
490-
((guint16 *)target)[3] = (guint16)guid->data3;
491-
length = 8;
476+
const ipv6_addr_and_prefix *current_ipv6_addr_and_prefix = fvalue_get_ipv6(current_field_info->value);
477+
length = 16;
478+
const guint8 *value = (const guint8 *)current_ipv6_addr_and_prefix->addr.bytes;
479+
memcpy(target, value, length < max_length ? length : max_length);
492480
}
493-
if (max_length >= 16)
481+
break;
482+
case FT_GUID:
494483
{
495-
((guint64 *)target)[1] = ((guint64 *)guid->data4)[0];
496-
length = 16;
484+
const e_guid_t *guid = fvalue_get_guid(current_field_info->value);
485+
486+
length = 0;
487+
488+
if (max_length >= 4)
489+
{
490+
((guint32 *)target)[0] = (guint32)guid->data1;
491+
length = 4;
492+
}
493+
if (max_length >= 6)
494+
{
495+
((guint16 *)target)[2] = (guint16)guid->data2;
496+
length = 6;
497+
}
498+
if (max_length >= 8)
499+
{
500+
((guint16 *)target)[3] = (guint16)guid->data3;
501+
length = 8;
502+
}
503+
if (max_length >= 16)
504+
{
505+
((guint64 *)target)[1] = ((guint64 *)guid->data4)[0];
506+
length = 16;
507+
}
497508
}
498-
}
499-
break;
500-
case FT_OID:
501-
case FT_VINES:
502-
case FT_REL_OID:
503-
case FT_SYSTEM_ID:
504-
case FT_FCWWN:
505-
{
506-
length = (gint32)(fvalue_get_bytes_size(current_field_info->value) & 0x7FFFFFFF);
507-
const guint8 *value = fvalue_get_bytes_data(current_field_info->value);
508-
memcpy(target, value, length < max_length ? length : max_length);
509-
}
510-
break;
511-
case FT_PROTOCOL:
512-
{
513-
length = ls_epan_field_length_get(epan_field);
514-
if (length > 0)
509+
break;
510+
case FT_OID:
511+
case FT_VINES:
512+
case FT_REL_OID:
513+
case FT_SYSTEM_ID:
514+
case FT_FCWWN:
515515
{
516-
tvbuff_t *buffer = fvalue_get_protocol(current_field_info->value);
517-
tvb_memcpy(buffer, target, 0, length < max_length ? length : max_length);
516+
length = (gint32)(fvalue_get_bytes_size(current_field_info->value) & 0x7FFFFFFF);
517+
const guint8 *value = fvalue_get_bytes_data(current_field_info->value);
518+
memcpy(target, value, length < max_length ? length : max_length);
518519
}
519-
else
520+
break;
521+
case FT_PROTOCOL:
520522
{
521-
length = 0;
523+
length = ls_epan_field_length_get(epan_field);
524+
if (length > 0)
525+
{
526+
tvbuff_t *buffer = fvalue_get_protocol(current_field_info->value);
527+
tvb_memcpy(buffer, target, 0, length < max_length ? length : max_length);
528+
}
529+
else
530+
{
531+
length = 0;
532+
}
522533
}
523-
}
524-
break;
525-
default:
526534
break;
535+
default:
536+
break;
537+
}
538+
}
539+
CATCH_ALL
540+
{
541+
length = -1;
527542
}
543+
ENDTRY;
528544

529545
return length;
530546
}

python/lemonshark/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"""
66
import os
77

8-
os.system(f"""pip install --upgrade setuptools twine wheel""")
8+
os.system(f"""pip install --upgrade setuptools twine wheel packaging""")
9+
910
os.system(f"""python setup.py build --build-base=../../build/python/temp egg_info --egg-base=../../build/python/temp bdist_wheel --dist-dir=../../build/python --bdist-dir=../../build/python/temp""")

python/lemonshark/lemonshark/epan_field.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,14 @@ def get_bytes_value(self) -> bytes:
324324
length: int = liblemonshark.ls_epan_field_value_bytes_length()
325325
if length <= 0:
326326
return None
327+
327328
buffer: bytes = bytes(length)
328329
c_buffer: c_char_p = c_char_p(buffer)
329-
liblemonshark.ls_epan_field_value_get_bytes(self.c_epan_field, c_buffer, length)
330+
331+
actual_length:int =liblemonshark.ls_epan_field_value_get_bytes(self.c_epan_field, c_buffer, length)
332+
if actual_length < 0:
333+
return None
334+
330335
return buffer
331336

332337
def get_value_representation(self) -> str:

python/lemonshark/setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
SPDX-License-Identifier: GPL-2.0-only
55
"""
66

7-
from setuptools import setup
7+
from setuptools import find_namespace_packages, setup
88

99
from os import path
1010
working_directory = path.abspath(path.dirname(__file__))
@@ -14,12 +14,13 @@
1414

1515
setup(
1616
name="lemonshark",
17-
version="0.9.0",
17+
version="0.9.1",
1818
description="lemonshark allows to use Wireshark as a library in an python application",
1919
long_description=long_description,
2020
long_description_content_type="text/markdown",
2121
license="GPL-2.0-only",
22-
packages=["lemonshark"],
22+
license_files=["LICENSE"],
23+
packages=find_namespace_packages(include=["lemonshark", "lemonshark.native.*"]),
2324
author="DevAM",
2425
author_email="[email protected]",
2526
keywords=["lemonshark", "shark", "Wireshark"],

0 commit comments

Comments
 (0)