Fix indent and add Modelines info for new common ui source file(s)

svn path=/trunk/; revision=45762
This commit is contained in:
Alexis La Goutte 2012-10-24 15:51:59 +00:00
parent 936082acb5
commit ee2a04416d
5 changed files with 534 additions and 470 deletions

View File

@ -50,143 +50,156 @@
gboolean
eo_save_entry(const gchar *save_as_filename, export_object_entry_t *entry, gboolean show_err)
{
int to_fd;
gint64 bytes_left;
int bytes_to_write;
ssize_t bytes_written;
guint8 *ptr;
int err;
int to_fd;
gint64 bytes_left;
int bytes_to_write;
ssize_t bytes_written;
guint8 *ptr;
int err;
to_fd = ws_open(save_as_filename, O_WRONLY | O_CREAT | O_EXCL |
O_BINARY, 0644);
if(to_fd == -1) { /* An error occurred */
if (show_err)
open_failure_alert_box(save_as_filename, errno, TRUE);
return FALSE;
}
to_fd = ws_open(save_as_filename, O_WRONLY | O_CREAT | O_EXCL |
O_BINARY, 0644);
if(to_fd == -1) { /* An error occurred */
if (show_err)
open_failure_alert_box(save_as_filename, errno, TRUE);
return FALSE;
}
/*
* The third argument to _write() on Windows is an unsigned int,
* so, on Windows, that's the size of the third argument to
* ws_write().
*
* The third argument to write() on UN*X is a size_t, although
* the return value is an ssize_t, so one probably shouldn't
* write more than the max value of an ssize_t.
*
* In either case, there's no guarantee that a gint64 such as
* payload_len can be passed to ws_write(), so we write in
* chunks of, at most 2^31 bytes.
*/
ptr = entry->payload_data;
bytes_left = entry->payload_len;
while (bytes_left != 0) {
if (bytes_left > 0x40000000)
bytes_to_write = 0x40000000;
else
bytes_to_write = (int)bytes_left;
bytes_written = ws_write(to_fd, ptr, bytes_to_write);
if(bytes_written <= 0) {
if (bytes_written < 0)
err = errno;
else
err = WTAP_ERR_SHORT_WRITE;
if (show_err)
write_failure_alert_box(save_as_filename, err);
ws_close(to_fd);
return FALSE;
}
bytes_left -= bytes_written;
ptr += bytes_written;
}
if (ws_close(to_fd) < 0) {
if (show_err)
write_failure_alert_box(save_as_filename, errno);
return FALSE;
}
/*
* The third argument to _write() on Windows is an unsigned int,
* so, on Windows, that's the size of the third argument to
* ws_write().
*
* The third argument to write() on UN*X is a size_t, although
* the return value is an ssize_t, so one probably shouldn't
* write more than the max value of an ssize_t.
*
* In either case, there's no guarantee that a gint64 such as
* payload_len can be passed to ws_write(), so we write in
* chunks of, at most 2^31 bytes.
*/
ptr = entry->payload_data;
bytes_left = entry->payload_len;
while (bytes_left != 0) {
if (bytes_left > 0x40000000)
bytes_to_write = 0x40000000;
else
bytes_to_write = (int)bytes_left;
bytes_written = ws_write(to_fd, ptr, bytes_to_write);
if(bytes_written <= 0) {
if (bytes_written < 0)
err = errno;
else
err = WTAP_ERR_SHORT_WRITE;
if (show_err)
write_failure_alert_box(save_as_filename, err);
ws_close(to_fd);
return FALSE;
}
bytes_left -= bytes_written;
ptr += bytes_written;
}
if (ws_close(to_fd) < 0) {
if (show_err)
write_failure_alert_box(save_as_filename, errno);
return FALSE;
}
return TRUE;
return TRUE;
}
#define HINIBBLE(x) (((x) >> 4) & 0xf)
#define LONIBBLE(x) ((x) & 0xf)
#define HEXTOASCII(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a'))
#define MAXFILELEN 255
#define HINIBBLE(x) (((x) >> 4) & 0xf)
#define LONIBBLE(x) ((x) & 0xf)
#define HEXTOASCII(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a'))
#define MAXFILELEN 255
static GString *eo_rename(GString *gstr, int dup)
{
GString *gstr_tmp;
gchar *tmp_ptr;
GString *ext_str;
GString *gstr_tmp;
gchar *tmp_ptr;
GString *ext_str;
gstr_tmp = g_string_new("(");
g_string_append_printf (gstr_tmp, "%d)", dup);
if ( (tmp_ptr = strrchr(gstr->str, '.')) != NULL ) {
/* Retain the extension */
ext_str = g_string_new(tmp_ptr);
gstr = g_string_truncate(gstr, gstr->len - ext_str->len);
if ( gstr->len >= (MAXFILELEN - (strlen(gstr_tmp->str) + ext_str->len)) )
gstr = g_string_truncate(gstr, MAXFILELEN - (strlen(gstr_tmp->str) + ext_str->len));
gstr = g_string_append(gstr, gstr_tmp->str);
gstr = g_string_append(gstr, ext_str->str);
g_string_free(ext_str, TRUE);
}
else {
if ( gstr->len >= (MAXFILELEN - strlen(gstr_tmp->str)) )
gstr = g_string_truncate(gstr, MAXFILELEN - strlen(gstr_tmp->str));
gstr = g_string_append(gstr, gstr_tmp->str);
}
g_string_free(gstr_tmp, TRUE);
return gstr;
gstr_tmp = g_string_new("(");
g_string_append_printf (gstr_tmp, "%d)", dup);
if ( (tmp_ptr = strrchr(gstr->str, '.')) != NULL ) {
/* Retain the extension */
ext_str = g_string_new(tmp_ptr);
gstr = g_string_truncate(gstr, gstr->len - ext_str->len);
if ( gstr->len >= (MAXFILELEN - (strlen(gstr_tmp->str) + ext_str->len)) )
gstr = g_string_truncate(gstr, MAXFILELEN - (strlen(gstr_tmp->str) + ext_str->len));
gstr = g_string_append(gstr, gstr_tmp->str);
gstr = g_string_append(gstr, ext_str->str);
g_string_free(ext_str, TRUE);
}
else {
if ( gstr->len >= (MAXFILELEN - strlen(gstr_tmp->str)) )
gstr = g_string_truncate(gstr, MAXFILELEN - strlen(gstr_tmp->str));
gstr = g_string_append(gstr, gstr_tmp->str);
}
g_string_free(gstr_tmp, TRUE);
return gstr;
}
GString *
eo_massage_str(const gchar *in_str, gsize maxlen, int dup)
{
gchar *tmp_ptr;
/* The characters in "reject" come from:
* http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx.
* Add to the list as necessary for other OS's.
*/
const gchar *reject = "<>:\"/\\|?*"
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
"\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
"\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
GString *out_str;
GString *ext_str;
gchar *tmp_ptr;
/* The characters in "reject" come from:
* http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx.
* Add to the list as necessary for other OS's.
*/
const gchar *reject = "<>:\"/\\|?*"
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
"\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
"\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
GString *out_str;
GString *ext_str;
out_str = g_string_new("");
out_str = g_string_new("");
/* Find all disallowed characters/bytes and replace them with %xx */
while ( (tmp_ptr = strpbrk(in_str, reject)) != NULL ) {
out_str = g_string_append_len(out_str, in_str, tmp_ptr - in_str);
out_str = g_string_append_c(out_str, '%');
out_str = g_string_append_c(out_str, HEXTOASCII(HINIBBLE(*tmp_ptr)));
out_str = g_string_append_c(out_str, HEXTOASCII(LONIBBLE(*tmp_ptr)));
in_str = tmp_ptr + 1;
}
out_str = g_string_append(out_str, in_str);
if ( out_str->len > maxlen ) {
if ( (tmp_ptr = strrchr(out_str->str, '.')) != NULL ) {
/* Retain the extension */
ext_str = g_string_new(tmp_ptr);
out_str = g_string_truncate(out_str, maxlen - ext_str->len);
out_str = g_string_append(out_str, ext_str->str);
g_string_free(ext_str, TRUE);
}
else
out_str = g_string_truncate(out_str, maxlen);
}
if ( dup != 0 )
out_str = eo_rename(out_str, dup);
return out_str;
/* Find all disallowed characters/bytes and replace them with %xx */
while ( (tmp_ptr = strpbrk(in_str, reject)) != NULL ) {
out_str = g_string_append_len(out_str, in_str, tmp_ptr - in_str);
out_str = g_string_append_c(out_str, '%');
out_str = g_string_append_c(out_str, HEXTOASCII(HINIBBLE(*tmp_ptr)));
out_str = g_string_append_c(out_str, HEXTOASCII(LONIBBLE(*tmp_ptr)));
in_str = tmp_ptr + 1;
}
out_str = g_string_append(out_str, in_str);
if ( out_str->len > maxlen ) {
if ( (tmp_ptr = strrchr(out_str->str, '.')) != NULL ) {
/* Retain the extension */
ext_str = g_string_new(tmp_ptr);
out_str = g_string_truncate(out_str, maxlen - ext_str->len);
out_str = g_string_append(out_str, ext_str->str);
g_string_free(ext_str, TRUE);
}
else
out_str = g_string_truncate(out_str, maxlen);
}
if ( dup != 0 )
out_str = eo_rename(out_str, dup);
return out_str;
}
const char *
ct2ext(const char *content_type)
{
/* TODO: Map the content type string to an extension string. If no match,
* return NULL. */
return content_type;
/* TODO: Map the content type string to an extension string. If no match,
* return NULL. */
return content_type;
}
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/

View File

@ -37,14 +37,14 @@ struct _export_object_list_t;
typedef struct _export_object_list_t export_object_list_t;
typedef struct _export_object_entry_t {
guint32 pkt_num;
gchar *hostname;
gchar *content_type;
gchar *filename;
/* We need to store a 64 bit integer to hold a file length
(was guint payload_len;) */
gint64 payload_len;
guint8 *payload_data;
guint32 pkt_num;
gchar *hostname;
gchar *content_type;
gchar *filename;
/* We need to store a 64 bit integer to hold a file length
(was guint payload_len;) */
gint64 payload_len;
guint8 *payload_data;
} export_object_entry_t;
void object_list_add_entry(export_object_list_t *object_list, export_object_entry_t *entry);
@ -57,11 +57,11 @@ const char *ct2ext(const char *content_type);
/* Protocol specific */
gboolean eo_dicom_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_,
const void *data);
const void *data);
gboolean eo_http_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_,
const void *data);
const void *data);
gboolean eo_smb_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_,
const void *data);
const void *data);
void eo_smb_cleanup(void);
@ -71,3 +71,16 @@ void eo_smb_cleanup(void);
#endif /* __cplusplus */
#endif /* __EXPORT_OBJECT_H__ */
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/

View File

@ -39,31 +39,43 @@
gboolean
eo_dicom_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_,
const void *data)
const void *data)
{
export_object_list_t *object_list = tapdata;
const dicom_eo_t *eo_info = data;
export_object_entry_t *entry;
export_object_list_t *object_list = tapdata;
const dicom_eo_t *eo_info = data;
export_object_entry_t *entry;
if (eo_info) { /* We have data waiting for us */
/*
Don't copy any data. dcm_export_create_object() is already g_malloc() the items
Still, the values will be freed when the export Object window is closed.
Therefore, strings and buffers must be copied
*/
entry = g_malloc(sizeof(export_object_entry_t));
if (eo_info) { /* We have data waiting for us */
/*
Don't copy any data. dcm_export_create_object() is already g_malloc() the items
Still, the values will be freed when the export Object window is closed.
Therefore, strings and buffers must be copied
*/
entry = g_malloc(sizeof(export_object_entry_t));
entry->pkt_num = pinfo->fd->num;
entry->hostname = eo_info->hostname;
entry->content_type = eo_info->content_type;
entry->filename = g_strdup(g_path_get_basename(eo_info->filename));
entry->payload_len = eo_info->payload_len;
entry->payload_data = eo_info->payload_data;
entry->pkt_num = pinfo->fd->num;
entry->hostname = eo_info->hostname;
entry->content_type = eo_info->content_type;
entry->filename = g_strdup(g_path_get_basename(eo_info->filename));
entry->payload_len = eo_info->payload_len;
entry->payload_data = eo_info->payload_data;
object_list_add_entry(object_list, entry);
object_list_add_entry(object_list, entry);
return TRUE; /* State changed - window should be redrawn */
} else {
return FALSE; /* State unchanged - no window updates needed */
}
return TRUE; /* State changed - window should be redrawn */
} else {
return FALSE; /* State unchanged - no window updates needed */
}
}
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/

View File

@ -37,29 +37,42 @@
gboolean
eo_http_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_,
const void *data)
const void *data)
{
export_object_list_t *object_list = tapdata;
const http_eo_t *eo_info = data;
export_object_entry_t *entry;
export_object_list_t *object_list = tapdata;
const http_eo_t *eo_info = data;
export_object_entry_t *entry;
if(eo_info) { /* We have data waiting for us */
/* These values will be freed when the Export Object window
* is closed. */
entry = g_malloc(sizeof(export_object_entry_t));
if(eo_info) { /* We have data waiting for us */
/* These values will be freed when the Export Object window
* is closed. */
entry = g_malloc(sizeof(export_object_entry_t));
entry->pkt_num = pinfo->fd->num;
entry->hostname = g_strdup(eo_info->hostname);
entry->content_type = g_strdup(eo_info->content_type);
entry->filename = g_strdup(g_path_get_basename(eo_info->filename));
entry->payload_len = eo_info->payload_len;
entry->payload_data = g_memdup(eo_info->payload_data,
eo_info->payload_len);
entry->pkt_num = pinfo->fd->num;
entry->hostname = g_strdup(eo_info->hostname);
entry->content_type = g_strdup(eo_info->content_type);
entry->filename = g_strdup(g_path_get_basename(eo_info->filename));
entry->payload_len = eo_info->payload_len;
entry->payload_data = g_memdup(eo_info->payload_data,
eo_info->payload_len);
object_list_add_entry(object_list, entry);
object_list_add_entry(object_list, entry);
return TRUE; /* State changed - window should be redrawn */
} else {
return FALSE; /* State unchanged - no window updates needed */
}
return TRUE; /* State changed - window should be redrawn */
} else {
return FALSE; /* State unchanged - no window updates needed */
}
}
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/

View File

@ -41,19 +41,19 @@
/* These flags show what kind of data the object contains
(designed to be or'ed) */
#define SMB_EO_CONTAINS_NOTHING 0x00
#define SMB_EO_CONTAINS_READS 0x01
#define SMB_EO_CONTAINS_WRITES 0x02
#define SMB_EO_CONTAINS_READSANDWRITES 0x03
#define SMB_EO_CONTAINS_NOTHING 0x00
#define SMB_EO_CONTAINS_READS 0x01
#define SMB_EO_CONTAINS_WRITES 0x02
#define SMB_EO_CONTAINS_READSANDWRITES 0x03
#define LEGAL_FILENAME_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ1234567890_."
static const value_string smb_eo_contains_string[]={
{SMB_EO_CONTAINS_NOTHING, ""},
{SMB_EO_CONTAINS_READS, "R"},
{SMB_EO_CONTAINS_WRITES, "W"},
{SMB_EO_CONTAINS_READSANDWRITES, "R&W"},
{0, NULL}
};
{SMB_EO_CONTAINS_NOTHING, ""},
{SMB_EO_CONTAINS_READS, "R"},
{SMB_EO_CONTAINS_WRITES, "W"},
{SMB_EO_CONTAINS_READSANDWRITES, "R&W"},
{0, NULL}
};
/* This struct contains the relationship between
@ -61,27 +61,27 @@ static const value_string smb_eo_contains_string[]={
the row# in this GSList will match the row# in the entry list */
typedef struct _active_file {
guint16 tid,uid,fid;
guint64 file_length; /* The last free reported offset */
/* We treat it as the file length */
guint64 data_gathered; /* The actual total of data gathered */
guint8 flag_contains; /* What kind of data it contains */
GSList *free_chunk_list;
/* A list of virtual "holes" in the */
/* file stream stored in memory */
gboolean is_out_of_memory;
/* TRUE if we cannot allocate memory */
/* memory for this file */
} active_file ;
guint16 tid,uid,fid;
guint64 file_length; /* The last free reported offset */
/* We treat it as the file length */
guint64 data_gathered; /* The actual total of data gathered */
guint8 flag_contains; /* What kind of data it contains */
GSList *free_chunk_list;
/* A list of virtual "holes" in the */
/* file stream stored in memory */
gboolean is_out_of_memory;
/* TRUE if we cannot allocate memory */
/* memory for this file */
} active_file ;
/* This is the GSList that will contain all the files that we are tracking */
static GSList *GSL_active_files = NULL;
static GSList *GSL_active_files = NULL;
/* We define a free chunk in a file as an start offset and end offset
Consider a free chunk as a "hole" in a file that we are capturing */
typedef struct _free_chunk {
guint64 start_offset;
guint64 end_offset;
guint64 start_offset;
guint64 end_offset;
} free_chunk;
/* insert_chunk function will recalculate the free_chunk_list, the data_size,
@ -89,302 +89,302 @@ typedef struct _free_chunk {
It will also insert the data chunk that is coming in the right
place of the file in memory.
HINTS:
file->data_gathered contains the real data gathered independently
from the file length
file->file_length contains the length of the file in memory, i.e.,
the last offset captured. In most cases, the real
file length would be different.
file->data_gathered contains the real data gathered independently
from the file length
file->file_length contains the length of the file in memory, i.e.,
the last offset captured. In most cases, the real
file length would be different.
*/
static void
insert_chunk(active_file *file, export_object_entry_t *entry, const smb_eo_t *eo_info)
{
guint nfreechunks = g_slist_length(file->free_chunk_list);
guint i;
free_chunk *current_free_chunk;
free_chunk *new_free_chunk;
guint64 chunk_offset=eo_info->smb_file_offset;
guint64 chunk_length=eo_info->payload_len;
guint64 chunk_end_offset = chunk_offset+chunk_length-1;
/* Size of file in memory */
guint64 calculated_size = chunk_offset+chunk_length;
gpointer dest_memory_addr;
guint nfreechunks = g_slist_length(file->free_chunk_list);
guint i;
free_chunk *current_free_chunk;
free_chunk *new_free_chunk;
guint64 chunk_offset=eo_info->smb_file_offset;
guint64 chunk_length=eo_info->payload_len;
guint64 chunk_end_offset = chunk_offset+chunk_length-1;
/* Size of file in memory */
guint64 calculated_size = chunk_offset+chunk_length;
gpointer dest_memory_addr;
/* Let's recalculate the file length and data gathered */
if (file->data_gathered==0 && nfreechunks==0) {
/* If this is the first entry for this file, we first
create an initial free chunk */
new_free_chunk=g_malloc(sizeof(free_chunk));
new_free_chunk->start_offset=0;
new_free_chunk->end_offset=MAX(file->file_length,chunk_end_offset+1)-1;
file->free_chunk_list=NULL;
file->free_chunk_list=g_slist_append(file->free_chunk_list,new_free_chunk);
nfreechunks+=1;
} else {
if (chunk_end_offset > file->file_length-1) {
new_free_chunk=g_malloc(sizeof(free_chunk));
new_free_chunk->start_offset=file->file_length;
new_free_chunk->end_offset=chunk_end_offset;
file->free_chunk_list=g_slist_append(file->free_chunk_list,new_free_chunk);
nfreechunks+=1;
}
}
file->file_length = MAX(file->file_length,chunk_end_offset+1);
/* Let's recalculate the file length and data gathered */
if (file->data_gathered==0 && nfreechunks==0) {
/* If this is the first entry for this file, we first
create an initial free chunk */
new_free_chunk=g_malloc(sizeof(free_chunk));
new_free_chunk->start_offset=0;
new_free_chunk->end_offset=MAX(file->file_length,chunk_end_offset+1)-1;
file->free_chunk_list=NULL;
file->free_chunk_list=g_slist_append(file->free_chunk_list,new_free_chunk);
nfreechunks+=1;
} else {
if (chunk_end_offset > file->file_length-1) {
new_free_chunk=g_malloc(sizeof(free_chunk));
new_free_chunk->start_offset=file->file_length;
new_free_chunk->end_offset=chunk_end_offset;
file->free_chunk_list=g_slist_append(file->free_chunk_list,new_free_chunk);
nfreechunks+=1;
}
}
file->file_length = MAX(file->file_length,chunk_end_offset+1);
for (i=0;i<nfreechunks;i++) {
current_free_chunk = g_slist_nth_data(file->free_chunk_list,i);
if (chunk_offset <= current_free_chunk->start_offset ) {
if (chunk_end_offset >= current_free_chunk->start_offset) {
if (chunk_end_offset < current_free_chunk->end_offset) {
file->data_gathered+=
(chunk_end_offset-current_free_chunk->start_offset+1);
current_free_chunk->start_offset=chunk_end_offset+1;
} else {
file->data_gathered+=
(current_free_chunk->end_offset-current_free_chunk->start_offset+1);
file->free_chunk_list =
g_slist_remove(file->free_chunk_list,current_free_chunk);
nfreechunks-=1;
if (nfreechunks==0) { /* The free chunk list is empty */
g_slist_free(file->free_chunk_list);
file->free_chunk_list=NULL;
break;
}
}
} else {
break;
}
for (i=0;i<nfreechunks;i++) {
current_free_chunk = g_slist_nth_data(file->free_chunk_list,i);
if (chunk_offset <= current_free_chunk->start_offset ) {
if (chunk_end_offset >= current_free_chunk->start_offset) {
if (chunk_end_offset < current_free_chunk->end_offset) {
file->data_gathered+=
(chunk_end_offset-current_free_chunk->start_offset+1);
current_free_chunk->start_offset=chunk_end_offset+1;
} else {
if (chunk_offset <= current_free_chunk->end_offset) {
if (chunk_end_offset < current_free_chunk->end_offset) {
new_free_chunk=g_malloc(sizeof(free_chunk));
new_free_chunk->start_offset=chunk_end_offset+1;
new_free_chunk->end_offset=current_free_chunk->end_offset;
current_free_chunk->end_offset=chunk_offset-1;
file->free_chunk_list =
g_slist_insert(file->free_chunk_list,new_free_chunk,i+1);
file->data_gathered+=chunk_length;
} else {
file->data_gathered+=current_free_chunk->end_offset-chunk_offset+1;
current_free_chunk->end_offset=chunk_offset-1;
}
}
file->data_gathered+=
(current_free_chunk->end_offset-current_free_chunk->start_offset+1);
file->free_chunk_list =
g_slist_remove(file->free_chunk_list,current_free_chunk);
nfreechunks-=1;
if (nfreechunks==0) { /* The free chunk list is empty */
g_slist_free(file->free_chunk_list);
file->free_chunk_list=NULL;
break;
}
}
} else {
break;
}
} else {
if (chunk_offset <= current_free_chunk->end_offset) {
if (chunk_end_offset < current_free_chunk->end_offset) {
new_free_chunk=g_malloc(sizeof(free_chunk));
new_free_chunk->start_offset=chunk_end_offset+1;
new_free_chunk->end_offset=current_free_chunk->end_offset;
current_free_chunk->end_offset=chunk_offset-1;
file->free_chunk_list =
g_slist_insert(file->free_chunk_list,new_free_chunk,i+1);
file->data_gathered+=chunk_length;
} else {
file->data_gathered+=current_free_chunk->end_offset-chunk_offset+1;
current_free_chunk->end_offset=chunk_offset-1;
}
}
}
}
/* Now, let's insert the data chunk into memory
...first, we shall be able to allocate the memory */
if (!entry->payload_data) {
/* This is a New file */
if (calculated_size > G_MAXSIZE) {
/*
* The argument to g_try_malloc() is
* a gsize, the maximum value of which is
* G_MAXSIZE. If the calculated size is
* bigger than that, we just say the attempt
* to allocate memory failed.
*/
entry->payload_data=NULL;
} else {
entry->payload_data = g_try_malloc((gsize)calculated_size);
entry->payload_len=calculated_size;
}
if (!entry->payload_data) {
/* Memory error */
file->is_out_of_memory=TRUE;
}
} else {
/* This is an existing file in memory */
if (calculated_size > (guint64) entry->payload_len &&
!file->is_out_of_memory) {
/* We need more memory */
if (calculated_size > G_MAXSIZE) {
/*
* As for g_try_malloc(), so for
* g_try_realloc().
*/
dest_memory_addr=NULL;
} else {
dest_memory_addr=g_try_realloc(
entry->payload_data,
(gsize)calculated_size);
}
if(!dest_memory_addr) {
/* Memory error */
file->is_out_of_memory=TRUE;
/* We don't have memory for this file.
Free the current file content from memory */
g_free(entry->payload_data);
entry->payload_data=NULL;
entry->payload_len=0;
} else {
entry->payload_data=dest_memory_addr;
entry->payload_len=calculated_size;
}
}
}
/* ...then, put the chunk of the file in the right place */
if(!file->is_out_of_memory) {
dest_memory_addr=entry->payload_data+chunk_offset;
g_memmove(dest_memory_addr,eo_info->payload_data,eo_info->payload_len);
}
/* Now, let's insert the data chunk into memory
...first, we shall be able to allocate the memory */
if (!entry->payload_data) {
/* This is a New file */
if (calculated_size > G_MAXSIZE) {
/*
* The argument to g_try_malloc() is
* a gsize, the maximum value of which is
* G_MAXSIZE. If the calculated size is
* bigger than that, we just say the attempt
* to allocate memory failed.
*/
entry->payload_data=NULL;
} else {
entry->payload_data = g_try_malloc((gsize)calculated_size);
entry->payload_len=calculated_size;
}
if (!entry->payload_data) {
/* Memory error */
file->is_out_of_memory=TRUE;
}
} else {
/* This is an existing file in memory */
if (calculated_size > (guint64) entry->payload_len &&
!file->is_out_of_memory) {
/* We need more memory */
if (calculated_size > G_MAXSIZE) {
/*
* As for g_try_malloc(), so for
* g_try_realloc().
*/
dest_memory_addr=NULL;
} else {
dest_memory_addr=g_try_realloc(
entry->payload_data,
(gsize)calculated_size);
}
if(!dest_memory_addr) {
/* Memory error */
file->is_out_of_memory=TRUE;
/* We don't have memory for this file.
Free the current file content from memory */
g_free(entry->payload_data);
entry->payload_data=NULL;
entry->payload_len=0;
} else {
entry->payload_data=dest_memory_addr;
entry->payload_len=calculated_size;
}
}
}
/* ...then, put the chunk of the file in the right place */
if(!file->is_out_of_memory) {
dest_memory_addr=entry->payload_data+chunk_offset;
g_memmove(dest_memory_addr,eo_info->payload_data,eo_info->payload_len);
}
}
/* We use this function to obtain the index in the GSL of a given file */
static int
find_incoming_file(GSList *GSL_active_files,active_file *incoming_file)
{
int i,row,last;
active_file *in_list_file;
int i,row,last;
active_file *in_list_file;
row=-1;
last=g_slist_length(GSL_active_files)-1;
row=-1;
last=g_slist_length(GSL_active_files)-1;
/* We lookup in reverse order because it is more likely that the file
is one of the latest */
for (i=last;i>=0;i--) {
in_list_file=g_slist_nth_data(GSL_active_files, i);
/* The best-working criteria of two identical files is that the file
that is the same of the file that we are analyzing is the last one
in the list that has the same tid and the same fid */
/* note that we have excluded in_list_file->uid == incoming_file->uid
from the comparison, because a file can be opened by different
SMB users and it is still the same file */
if (in_list_file->tid == incoming_file->tid &&
in_list_file->fid == incoming_file->fid) {
row=i;
break;
}
}
/* We lookup in reverse order because it is more likely that the file
is one of the latest */
for (i=last;i>=0;i--) {
in_list_file=g_slist_nth_data(GSL_active_files, i);
/* The best-working criteria of two identical files is that the file
that is the same of the file that we are analyzing is the last one
in the list that has the same tid and the same fid */
/* note that we have excluded in_list_file->uid == incoming_file->uid
from the comparison, because a file can be opened by different
SMB users and it is still the same file */
if (in_list_file->tid == incoming_file->tid &&
in_list_file->fid == incoming_file->fid) {
row=i;
break;
}
}
return row;
return row;
}
/* This is the function answering to the registered tap listener call */
gboolean
eo_smb_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
{
export_object_list_t *object_list = tapdata;
const smb_eo_t *eo_info = data;
export_object_list_t *object_list = tapdata;
const smb_eo_t *eo_info = data;
export_object_entry_t *entry;
export_object_entry_t *current_entry;
active_file incoming_file;
gint active_row;
active_file *new_file;
active_file *current_file;
guint8 contains;
gboolean is_supported_filetype;
gfloat percent;
export_object_entry_t *entry;
export_object_entry_t *current_entry;
active_file incoming_file;
gint active_row;
active_file *new_file;
active_file *current_file;
guint8 contains;
gboolean is_supported_filetype;
gfloat percent;
gchar **aux_string_v;
gchar **aux_string_v;
/* Is this an eo_smb supported file_type? (right now we only support FILE */
is_supported_filetype = (eo_info->fid_type==SMB_FID_TYPE_FILE);
/* Is this an eo_smb supported file_type? (right now we only support FILE */
is_supported_filetype = (eo_info->fid_type==SMB_FID_TYPE_FILE);
/* What kind of data this packet contains? */
switch(eo_info->cmd) {
case SMB_COM_READ_ANDX:
contains=SMB_EO_CONTAINS_READS;
break;
case SMB_COM_WRITE_ANDX:
contains=SMB_EO_CONTAINS_WRITES;
break;
default:
contains=SMB_EO_CONTAINS_NOTHING;
break;
}
/* What kind of data this packet contains? */
switch(eo_info->cmd) {
case SMB_COM_READ_ANDX:
contains=SMB_EO_CONTAINS_READS;
break;
case SMB_COM_WRITE_ANDX:
contains=SMB_EO_CONTAINS_WRITES;
break;
default:
contains=SMB_EO_CONTAINS_NOTHING;
break;
}
/* Is this data from an already tracked file or not? */
incoming_file.tid=eo_info->tid;
incoming_file.uid=eo_info->uid;
incoming_file.fid=eo_info->fid;
active_row=find_incoming_file(GSL_active_files, &incoming_file);
/* Is this data from an already tracked file or not? */
incoming_file.tid=eo_info->tid;
incoming_file.uid=eo_info->uid;
incoming_file.fid=eo_info->fid;
active_row=find_incoming_file(GSL_active_files, &incoming_file);
if (active_row==-1) { /* This is a new-tracked file */
/* Construct the entry in the list of active files */
entry = g_malloc(sizeof(export_object_entry_t));
entry->payload_data=NULL;
entry->payload_len=0;
new_file = g_malloc(sizeof(active_file));
new_file->tid=incoming_file.tid;
new_file->uid=incoming_file.uid;
new_file->fid=incoming_file.fid;
new_file->file_length = eo_info->end_of_file;
new_file->flag_contains=contains;
new_file->free_chunk_list=NULL;
new_file->data_gathered=0;
new_file->is_out_of_memory=FALSE;
entry->pkt_num = pinfo->fd->num;
entry->hostname = g_strdup(eo_info->hostname);
if (g_str_has_prefix(eo_info->filename,"\\")) {
aux_string_v = g_strsplit(eo_info->filename, "\\", -1);
entry->filename = g_strdup(aux_string_v[g_strv_length(aux_string_v)-1]);
g_strfreev(aux_string_v);
} else {
entry->filename = g_strdup(eo_info->filename);
}
if (active_row==-1) { /* This is a new-tracked file */
/* Construct the entry in the list of active files */
entry = g_malloc(sizeof(export_object_entry_t));
entry->payload_data=NULL;
entry->payload_len=0;
new_file = g_malloc(sizeof(active_file));
new_file->tid=incoming_file.tid;
new_file->uid=incoming_file.uid;
new_file->fid=incoming_file.fid;
new_file->file_length = eo_info->end_of_file;
new_file->flag_contains=contains;
new_file->free_chunk_list=NULL;
new_file->data_gathered=0;
new_file->is_out_of_memory=FALSE;
entry->pkt_num = pinfo->fd->num;
entry->hostname = g_strdup(eo_info->hostname);
if (g_str_has_prefix(eo_info->filename,"\\")) {
aux_string_v = g_strsplit(eo_info->filename, "\\", -1);
entry->filename = g_strdup(aux_string_v[g_strv_length(aux_string_v)-1]);
g_strfreev(aux_string_v);
} else {
entry->filename = g_strdup(eo_info->filename);
}
/* Insert the first chunk in the chunk list of this file */
if (is_supported_filetype) {
insert_chunk(new_file, entry, eo_info);
}
/* Insert the first chunk in the chunk list of this file */
if (is_supported_filetype) {
insert_chunk(new_file, entry, eo_info);
}
if(new_file->is_out_of_memory) {
entry->content_type =
g_strdup_printf("%s (%"G_GUINT64_FORMAT"?/%"G_GUINT64_FORMAT") %s [mem!!]",
match_strval(eo_info->fid_type, smb_fid_types),
new_file->data_gathered,
new_file->file_length,
match_strval(contains, smb_eo_contains_string));
} else {
if (new_file->file_length > 0) {
percent=(gfloat) 100*new_file->data_gathered/new_file->file_length;
} else {
percent = 0.0;
}
if(new_file->is_out_of_memory) {
entry->content_type =
g_strdup_printf("%s (%"G_GUINT64_FORMAT"?/%"G_GUINT64_FORMAT") %s [mem!!]",
match_strval(eo_info->fid_type, smb_fid_types),
new_file->data_gathered,
new_file->file_length,
match_strval(contains, smb_eo_contains_string));
} else {
if (new_file->file_length > 0) {
percent=(gfloat) 100*new_file->data_gathered/new_file->file_length;
} else {
percent = 0.0;
}
entry->content_type =
g_strdup_printf("%s (%"G_GUINT64_FORMAT"/%"G_GUINT64_FORMAT") %s [%5.2f%%]",
match_strval(eo_info->fid_type, smb_fid_types),
new_file->data_gathered,
new_file->file_length,
match_strval(contains, smb_eo_contains_string),
percent);
}
entry->content_type =
g_strdup_printf("%s (%"G_GUINT64_FORMAT"/%"G_GUINT64_FORMAT") %s [%5.2f%%]",
match_strval(eo_info->fid_type, smb_fid_types),
new_file->data_gathered,
new_file->file_length,
match_strval(contains, smb_eo_contains_string),
percent);
}
object_list_add_entry(object_list, entry);
GSL_active_files =
g_slist_append(GSL_active_files, new_file);
}
else if (is_supported_filetype) {
current_file=g_slist_nth_data(GSL_active_files,active_row);
/* Recalculate the current file flags */
current_file->flag_contains=current_file->flag_contains|contains;
current_entry = object_list_get_entry(object_list, active_row);
object_list_add_entry(object_list, entry);
GSL_active_files =
g_slist_append(GSL_active_files, new_file);
}
else if (is_supported_filetype) {
current_file=g_slist_nth_data(GSL_active_files,active_row);
/* Recalculate the current file flags */
current_file->flag_contains=current_file->flag_contains|contains;
current_entry = object_list_get_entry(object_list, active_row);
insert_chunk(current_file, current_entry, eo_info);
insert_chunk(current_file, current_entry, eo_info);
/* Modify the current_entry object_type string */
if(current_file->is_out_of_memory) {
current_entry->content_type =
g_strdup_printf("%s (%"G_GUINT64_FORMAT"?/%"G_GUINT64_FORMAT") %s [mem!!]",
match_strval(eo_info->fid_type, smb_fid_types),
current_file->data_gathered,
current_file->file_length,
match_strval(current_file->flag_contains, smb_eo_contains_string));
} else {
percent=(gfloat) 100*current_file->data_gathered/current_file->file_length;
current_entry->content_type =
g_strdup_printf("%s (%"G_GUINT64_FORMAT"/%"G_GUINT64_FORMAT") %s [%5.2f%%]",
match_strval(eo_info->fid_type, smb_fid_types),
current_file->data_gathered,
current_file->file_length,
match_strval(current_file->flag_contains, smb_eo_contains_string),
percent);
}
}
/* Modify the current_entry object_type string */
if(current_file->is_out_of_memory) {
current_entry->content_type =
g_strdup_printf("%s (%"G_GUINT64_FORMAT"?/%"G_GUINT64_FORMAT") %s [mem!!]",
match_strval(eo_info->fid_type, smb_fid_types),
current_file->data_gathered,
current_file->file_length,
match_strval(current_file->flag_contains, smb_eo_contains_string));
} else {
percent=(gfloat) 100*current_file->data_gathered/current_file->file_length;
current_entry->content_type =
g_strdup_printf("%s (%"G_GUINT64_FORMAT"/%"G_GUINT64_FORMAT") %s [%5.2f%%]",
match_strval(eo_info->fid_type, smb_fid_types),
current_file->data_gathered,
current_file->file_length,
match_strval(current_file->flag_contains, smb_eo_contains_string),
percent);
}
}
return TRUE; /* State changed - window should be redrawn */
return TRUE; /* State changed - window should be redrawn */
}
/* This is the eo_protocoldata_reset function that is used in the export_object module
to cleanup any previous private data of the export object functionality before perform
@ -392,22 +392,35 @@ eo_smb_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const
void
eo_smb_cleanup(void)
{
int i,last;
active_file *in_list_file;
int i,last;
active_file *in_list_file;
/* Free any previous data structures used in previous invocation to the
export_object_smb function */
last=g_slist_length(GSL_active_files);
if (GSL_active_files) {
for (i=last-1;i>=0;i--) {
in_list_file=g_slist_nth_data(GSL_active_files, i);
if (in_list_file->free_chunk_list) {
g_slist_free(in_list_file->free_chunk_list);
in_list_file->free_chunk_list=NULL;
}
g_free(in_list_file);
}
g_slist_free(GSL_active_files);
GSL_active_files=NULL;
}
/* Free any previous data structures used in previous invocation to the
export_object_smb function */
last=g_slist_length(GSL_active_files);
if (GSL_active_files) {
for (i=last-1;i>=0;i--) {
in_list_file=g_slist_nth_data(GSL_active_files, i);
if (in_list_file->free_chunk_list) {
g_slist_free(in_list_file->free_chunk_list);
in_list_file->free_chunk_list=NULL;
}
g_free(in_list_file);
}
g_slist_free(GSL_active_files);
GSL_active_files=NULL;
}
}
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/