aboutsummaryrefslogtreecommitdiffstats
path: root/ui/export_object_tftp.c
blob: 11547aca38c95b9828fa066817c1cd6e2f4c711e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* export_object_tftp.c
 * Routines for aving objects (files) found in TFTP sessions
 * See also: export_object.c / export_object.h for common code
 * Initial file, prototypes and general structure initially copied
 * from export_object_smb.c
 *
 * Martin Mathieson
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include "config.h"


#include <epan/packet.h>
#include <epan/dissectors/packet-tftp.h>
#include <epan/tap.h>

#include "export_object.h"

/* A list of block list entries to delete from cleanup callback when window is closed. */
typedef struct eo_info_dynamic_t {
    gchar  *filename;
    GSList *block_list;
} eo_info_dynamic_t;
static GSList *s_dynamic_info_list = NULL;

/* Tap function */
gboolean
eo_tftp_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_,
           const void *data)
{
    export_object_list_t *object_list = (export_object_list_t *)tapdata;
    const tftp_eo_t *eo_info = (const tftp_eo_t *)data;
    export_object_entry_t *entry;

    GSList *block_iterator;
    guint  payload_data_offset = 0;
    eo_info_dynamic_t *dynamic_info;

    /* These values will be freed when the Export Object window is closed. */
    entry = (export_object_entry_t*)g_malloc(sizeof(export_object_entry_t));

    /* Remember which frame had the last block of the file */
    entry->pkt_num = pinfo->num;

    /* Copy filename */
    entry->filename = g_strdup(g_path_get_basename(eo_info->filename));

    /* Iterate over list of blocks and concatenate into contiguous memory */
    entry->payload_len = eo_info->payload_len;
    entry->payload_data = (guint8 *)g_try_malloc((gsize)entry->payload_len);
    for (block_iterator = eo_info->block_list; block_iterator; block_iterator = block_iterator->next) {
        file_block_t *block = (file_block_t*)block_iterator->data;
        memcpy(entry->payload_data + payload_data_offset,
               block->data,
               block->length);
        payload_data_offset += block->length;
    }

    /* These 2 fields not used */
    entry->hostname = NULL;
    entry->content_type = NULL;

    /* Add to list of entries to be cleaned up.  eo_info is only packet scope, so
       need to make list only of block list now */
    dynamic_info = (eo_info_dynamic_t*)g_malloc(sizeof(eo_info_dynamic_t));
    dynamic_info->filename = eo_info->filename;
    dynamic_info->block_list = eo_info->block_list;
    s_dynamic_info_list = g_slist_append(s_dynamic_info_list, (eo_info_dynamic_t*)dynamic_info);

    /* Pass out entry to the GUI */
    object_list_add_entry(object_list, entry);

    return TRUE; /* State changed - window should be redrawn */
}

/* Clean up the stored parts of a single tapped entry */
static void cleanup_tftp_eo(eo_info_dynamic_t *dynamic_info)
{
    GSList *block_iterator;
    /* Free the filename */
    g_free(dynamic_info->filename);

    /* Walk list of block items */
    for (block_iterator = dynamic_info->block_list; block_iterator; block_iterator = block_iterator->next) {
        file_block_t *block = (file_block_t*)(block_iterator->data);
        /* Free block data */
        wmem_free(NULL, block->data);

        /* Free block itself */
        g_free(block);
    }
}

/* Callback for freeing up data supplied with taps.  The taps themselves only have
   packet scope, so only store/free dynamic memory pointers */
void eo_tftp_cleanup(void)
{
    /* Cleanup each entry in the global list */
    GSList *dynamic_iterator;
    for (dynamic_iterator = s_dynamic_info_list; dynamic_iterator; dynamic_iterator = dynamic_iterator->next) {
        eo_info_dynamic_t *dynamic_info = (eo_info_dynamic_t*)dynamic_iterator->data;
        cleanup_tftp_eo(dynamic_info);
    }
    /* List is empty again */
    s_dynamic_info_list = 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:
 */