| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # Copyright (C) 2008 Adriano Monteiro Marques |
|---|
| 4 | # |
|---|
| 5 | # Author: Francesco Piccinno <stack.box@gmail.com> |
|---|
| 6 | # |
|---|
| 7 | # This program is free software; you can redistribute it and/or modify |
|---|
| 8 | # it under the terms of the GNU General Public License as published by |
|---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | # (at your option) any later version. |
|---|
| 11 | # |
|---|
| 12 | # This program is distributed in the hope that it will be useful, |
|---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | # GNU General Public License for more details. |
|---|
| 16 | # |
|---|
| 17 | # You should have received a copy of the GNU General Public License |
|---|
| 18 | # along with this program; if not, write to the Free Software |
|---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | """ |
|---|
| 22 | A perspective to show and edit packets |
|---|
| 23 | """ |
|---|
| 24 | |
|---|
| 25 | import gtk |
|---|
| 26 | |
|---|
| 27 | from PM import Backend |
|---|
| 28 | from PM.Core.I18N import _ |
|---|
| 29 | from PM.Manager.PreferenceManager import Prefs |
|---|
| 30 | |
|---|
| 31 | from PM.Gui.Core.App import PMApp |
|---|
| 32 | from PM.Gui.Core.Icons import get_pixbuf |
|---|
| 33 | from PM.Gui.Widgets.HexView import HexView |
|---|
| 34 | from PM.Gui.Widgets.Plotter import Plotter |
|---|
| 35 | from PM.Gui.Tabs.OperationsTab import SendOperation, SendReceiveOperation |
|---|
| 36 | |
|---|
| 37 | class ProtocolHierarchy(gtk.ScrolledWindow): |
|---|
| 38 | def __init__(self, parent): |
|---|
| 39 | gtk.ScrolledWindow.__init__(self) |
|---|
| 40 | |
|---|
| 41 | self.__create_widgets() |
|---|
| 42 | self.__pack_widgets() |
|---|
| 43 | self.__connect_signals() |
|---|
| 44 | |
|---|
| 45 | self.session = parent |
|---|
| 46 | self.proto_icon = get_pixbuf('protocol_small') |
|---|
| 47 | |
|---|
| 48 | self.reload() |
|---|
| 49 | |
|---|
| 50 | def reload(self): |
|---|
| 51 | self.store.clear() |
|---|
| 52 | |
|---|
| 53 | if not self.session.packet: |
|---|
| 54 | return |
|---|
| 55 | |
|---|
| 56 | root = None |
|---|
| 57 | |
|---|
| 58 | # We pray to be ordered :( |
|---|
| 59 | for proto in Backend.get_packet_protos(self.session.packet): |
|---|
| 60 | root = self.store.append(root, [self.proto_icon, Backend.get_proto_name(proto), proto]) |
|---|
| 61 | |
|---|
| 62 | self.tree.expand_all() |
|---|
| 63 | self.tree.get_selection().select_path((0, )) |
|---|
| 64 | |
|---|
| 65 | def __create_widgets(self): |
|---|
| 66 | # Icon / string (like TCP packet with some info?) / hidden |
|---|
| 67 | self.store = gtk.TreeStore(gtk.gdk.Pixbuf, str, object) |
|---|
| 68 | self.tree = gtk.TreeView(self.store) |
|---|
| 69 | |
|---|
| 70 | pix = gtk.CellRendererPixbuf() |
|---|
| 71 | txt = gtk.CellRendererText() |
|---|
| 72 | |
|---|
| 73 | col = gtk.TreeViewColumn(_('Protocol')) |
|---|
| 74 | |
|---|
| 75 | col.pack_start(pix, False) |
|---|
| 76 | col.pack_start(txt, True) |
|---|
| 77 | |
|---|
| 78 | col.set_attributes(pix, pixbuf=0) |
|---|
| 79 | col.set_attributes(txt, text=1) |
|---|
| 80 | |
|---|
| 81 | self.tree.append_column(col) |
|---|
| 82 | |
|---|
| 83 | self.tree.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_BOTH) |
|---|
| 84 | self.tree.set_rules_hint(True) |
|---|
| 85 | |
|---|
| 86 | def __pack_widgets(self): |
|---|
| 87 | self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) |
|---|
| 88 | self.set_shadow_type(gtk.SHADOW_ETCHED_IN) |
|---|
| 89 | |
|---|
| 90 | self.add(self.tree) |
|---|
| 91 | |
|---|
| 92 | def __connect_signals(self): |
|---|
| 93 | self.tree.enable_model_drag_dest([('text/plain', 0, 0)], gtk.gdk.ACTION_COPY) |
|---|
| 94 | self.tree.connect('drag-data-received', self.__on_drag_data) |
|---|
| 95 | |
|---|
| 96 | def __on_drag_data(self, widget, ctx, x, y, data, info, time): |
|---|
| 97 | if not self.session.packet: |
|---|
| 98 | ctx.finish(False, False, time) |
|---|
| 99 | |
|---|
| 100 | if data and data.format == 8: |
|---|
| 101 | ret = self.tree.get_dest_row_at_pos(x, y) |
|---|
| 102 | |
|---|
| 103 | try: |
|---|
| 104 | # Try to construct an empty packet |
|---|
| 105 | packet = Backend.get_proto(data.data)() |
|---|
| 106 | packet = Backend.MetaPacket(packet) |
|---|
| 107 | except Exception: |
|---|
| 108 | ctx.finish(False, False, time) |
|---|
| 109 | return |
|---|
| 110 | |
|---|
| 111 | # We append as default |
|---|
| 112 | where = -1 |
|---|
| 113 | |
|---|
| 114 | if ret: |
|---|
| 115 | path, pos = ret |
|---|
| 116 | where = len(path) # because it's a treeview with only one child for row |
|---|
| 117 | |
|---|
| 118 | if pos == gtk.TREE_VIEW_DROP_BEFORE or \ |
|---|
| 119 | pos == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE: |
|---|
| 120 | where -= 1 |
|---|
| 121 | |
|---|
| 122 | # Now try to insert this stuff into the packet |
|---|
| 123 | |
|---|
| 124 | if self.session.packet.insert(packet, where): |
|---|
| 125 | ctx.finish(True, False, time) |
|---|
| 126 | |
|---|
| 127 | self.session.reload_container(self.session.packet) |
|---|
| 128 | self.session.reload_editor() |
|---|
| 129 | |
|---|
| 130 | else: |
|---|
| 131 | ctx.finish(False, False, time) |
|---|
| 132 | |
|---|
| 133 | else: |
|---|
| 134 | ctx.finish(False, False, time) |
|---|
| 135 | |
|---|
| 136 | def get_active_protocol(self): |
|---|
| 137 | """ |
|---|
| 138 | Return the selected protocol or the most |
|---|
| 139 | important protocol if no selection. |
|---|
| 140 | |
|---|
| 141 | @return a tuple Packet, Protocol or None, None |
|---|
| 142 | """ |
|---|
| 143 | |
|---|
| 144 | model, iter = self.tree.get_selection().get_selected() |
|---|
| 145 | |
|---|
| 146 | if not iter: |
|---|
| 147 | return None, None |
|---|
| 148 | |
|---|
| 149 | obj = model.get_value(iter, 2) |
|---|
| 150 | |
|---|
| 151 | assert (Backend.is_proto(obj), "Should be a Protocol instance.") |
|---|
| 152 | |
|---|
| 153 | return self.session.packet, obj |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | class PacketPage(gtk.HBox): |
|---|
| 157 | def __init__(self, parent): |
|---|
| 158 | super(PacketPage, self).__init__(False, 4) |
|---|
| 159 | |
|---|
| 160 | self.session = parent |
|---|
| 161 | |
|---|
| 162 | # Create the toolbar for sending selected packet |
|---|
| 163 | self.toolbar = gtk.Toolbar() |
|---|
| 164 | self.toolbar.set_style(gtk.TOOLBAR_ICONS) |
|---|
| 165 | self.toolbar.set_orientation(gtk.ORIENTATION_VERTICAL) |
|---|
| 166 | |
|---|
| 167 | stocks = ( |
|---|
| 168 | gtk.STOCK_EDIT, |
|---|
| 169 | gtk.STOCK_DELETE, |
|---|
| 170 | gtk.STOCK_CLEAR, |
|---|
| 171 | gtk.STOCK_SELECT_COLOR |
|---|
| 172 | ) |
|---|
| 173 | |
|---|
| 174 | tooltips = ( |
|---|
| 175 | _('Complete layers'), |
|---|
| 176 | _('Remove selected layer'), |
|---|
| 177 | _('Reset layer to default'), |
|---|
| 178 | _('Graph packet') |
|---|
| 179 | ) |
|---|
| 180 | |
|---|
| 181 | callbacks = ( |
|---|
| 182 | self.__on_complete, |
|---|
| 183 | self.__on_remove, |
|---|
| 184 | self.__on_reset, |
|---|
| 185 | self.__on_graph |
|---|
| 186 | ) |
|---|
| 187 | |
|---|
| 188 | for tooltip, stock, callback in zip(tooltips, stocks, callbacks): |
|---|
| 189 | action = gtk.Action(None, None, tooltip, stock) |
|---|
| 190 | action.connect('activate', callback) |
|---|
| 191 | self.toolbar.insert(action.create_tool_item(), -1) |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | self.proto_hierarchy = ProtocolHierarchy(self.session) |
|---|
| 195 | self.hexview = HexView() |
|---|
| 196 | |
|---|
| 197 | self.pack_start(self.proto_hierarchy) |
|---|
| 198 | self.pack_start(self.toolbar, False, False) |
|---|
| 199 | self.pack_start(self.hexview, False, False) |
|---|
| 200 | |
|---|
| 201 | Prefs()['gui.maintab.hexview.font'].connect(self.hexview.modify_font) |
|---|
| 202 | Prefs()['gui.maintab.hexview.bpl'].connect(self.hexview.set_bpl) |
|---|
| 203 | |
|---|
| 204 | def redraw_hexview(self): |
|---|
| 205 | """ |
|---|
| 206 | Redraws the hexview |
|---|
| 207 | """ |
|---|
| 208 | if self.session.packet: |
|---|
| 209 | self.hexview.payload = Backend.get_packet_raw(self.session.packet) |
|---|
| 210 | else: |
|---|
| 211 | self.hexview.payload = "" |
|---|
| 212 | |
|---|
| 213 | def reload(self): |
|---|
| 214 | # Hide the toolbar while merging fields |
|---|
| 215 | |
|---|
| 216 | # FIXME: cyclic |
|---|
| 217 | #if isinstance(self.session, SequenceSession) and \ |
|---|
| 218 | if getattr(self.session, 'sequence_page', None) and \ |
|---|
| 219 | self.session.sequence_page.merging: |
|---|
| 220 | self.toolbar.hide() |
|---|
| 221 | self.proto_hierarchy.hide() |
|---|
| 222 | else: |
|---|
| 223 | self.toolbar.show() |
|---|
| 224 | self.proto_hierarchy.show() |
|---|
| 225 | |
|---|
| 226 | self.redraw_hexview() |
|---|
| 227 | self.proto_hierarchy.reload() |
|---|
| 228 | |
|---|
| 229 | def __on_remove(self, action): |
|---|
| 230 | packet, protocol = self.proto_hierarchy.get_active_protocol() |
|---|
| 231 | |
|---|
| 232 | if not packet: |
|---|
| 233 | return |
|---|
| 234 | |
|---|
| 235 | if packet.remove(protocol): |
|---|
| 236 | self.session.reload_container(packet) |
|---|
| 237 | self.reload() |
|---|
| 238 | |
|---|
| 239 | def __on_reset(self, action): |
|---|
| 240 | packet, protocol = self.proto_hierarchy.get_active_protocol() |
|---|
| 241 | |
|---|
| 242 | if not packet: |
|---|
| 243 | return |
|---|
| 244 | |
|---|
| 245 | if packet.reset(protocol): |
|---|
| 246 | self.session.reload_container(packet) |
|---|
| 247 | self.reload() |
|---|
| 248 | |
|---|
| 249 | def __on_complete(self, action): |
|---|
| 250 | packet, protocol = self.proto_hierarchy.get_active_protocol() |
|---|
| 251 | |
|---|
| 252 | if not packet: |
|---|
| 253 | return |
|---|
| 254 | |
|---|
| 255 | if packet.complete(): |
|---|
| 256 | self.session.reload_container(packet) |
|---|
| 257 | self.reload() |
|---|
| 258 | |
|---|
| 259 | def __on_graph(self, action): |
|---|
| 260 | if not self.session.packet: |
|---|
| 261 | return |
|---|
| 262 | |
|---|
| 263 | dialog = gtk.Dialog( |
|---|
| 264 | _('Graph for %s') % self.session.packet.get_protocol_str(), |
|---|
| 265 | self.get_toplevel(), 0, (gtk.STOCK_CLOSE, gtk.RESPONSE_REJECT, |
|---|
| 266 | gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT)) |
|---|
| 267 | |
|---|
| 268 | dialog.plotter = Plotter(self.session.packet) |
|---|
| 269 | dialog.vbox.pack_start(dialog.plotter) |
|---|
| 270 | dialog.show_all() |
|---|
| 271 | |
|---|
| 272 | dialog.connect('response', self.__on_graph_response) |
|---|
| 273 | |
|---|
| 274 | def __on_graph_response(self, dialog, id): |
|---|
| 275 | if id == gtk.RESPONSE_REJECT: |
|---|
| 276 | dialog.hide() |
|---|
| 277 | dialog.destroy() |
|---|
| 278 | elif id == gtk.RESPONSE_ACCEPT: |
|---|
| 279 | chooser = gtk.FileChooserDialog(_('Save graph to'), dialog, |
|---|
| 280 | gtk.FILE_CHOOSER_ACTION_SAVE, |
|---|
| 281 | (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, |
|---|
| 282 | gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT)) |
|---|
| 283 | |
|---|
| 284 | if chooser.run() == gtk.RESPONSE_ACCEPT: |
|---|
| 285 | fname = chooser.get_filename() |
|---|
| 286 | dialog.plotter.export_to(fname) |
|---|
| 287 | |
|---|
| 288 | chooser.hide() |
|---|
| 289 | chooser.destroy() |
|---|