/* Xen Store Daemon providing simple tree-like database. Copyright (C) 2005 Rusty Russell IBM Corporation This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef _XS_H #define _XS_H #include struct xs_handle; struct xs_transaction_handle; /* On failure, these routines set errno. */ /* Connect to the xs daemon. * Returns a handle or NULL. */ struct xs_handle *xs_daemon_open(void); struct xs_handle *xs_domain_open(void); /* Connect to the xs daemon (readonly for non-root clients). * Returns a handle or NULL. */ struct xs_handle *xs_daemon_open_readonly(void); /* Close the connection to the xs daemon. */ void xs_daemon_close(struct xs_handle *); /* Get contents of a directory. * Returns a malloced array: call free() on it after use. * Num indicates size. */ char **xs_directory(struct xs_handle *h, struct xs_transaction_handle *t, const char *path, unsigned int *num); /* Get the value of a single file, nul terminated. * Returns a malloced value: call free() on it after use. * len indicates length in bytes, not including terminator. */ void *xs_read(struct xs_handle *h, struct xs_transaction_handle *t, const char *path, unsigned int *len); /* Write the value of a single file. * Returns false on failure. */ bool xs_write(struct xs_handle *h, struct xs_transaction_handle *t, const char *path, const void *data, unsigned int len); /* Create a new directory. * Returns false on failure, or success if it already exists. */ bool xs_mkdir(struct xs_handle *h, struct xs_transaction_handle *t, const char *path); /* Destroy a file or directory (and children). * Returns false on failure, or success if it doesn't exist. */ bool xs_rm(struct xs_handle *h, struct xs_transaction_handle *t, const char *path); /* Get permissions of node (first element is owner, first perms is "other"). * Returns malloced array, or NULL: call free() after use. */ struct xs_permissions *xs_get_permissions(struct xs_handle *h, struct xs_transaction_handle *t, const char *path, unsigned int *num); /* Set permissions of node (must be owner). * Returns false on failure. */ bool xs_set_permissions(struct xs_handle *h, struct xs_transaction_handle *t, const char *path, struct xs_permissions *perms, unsigned int num_perms); /* Watch a node for changes (poll on fd to detect, or call read_watch()). * When the node (or any child) changes, fd will become readable. * Token is returned when watch is read, to allow matching. * Returns false on failure. */ bool xs_watch(struct xs_handle *h, const char *path, const char *token); /* Return the FD to poll on to see if a watch has fired. */ int xs_fileno(struct xs_handle *h); /* Find out what node change was on (will block if nothing pending). * Returns array containing the path and token. Use XS_WATCH_* to access these * elements. Call free() after use. */ char **xs_read_watch(struct xs_handle *h, unsigned int *num); /* Remove a watch on a node: implicitly acks any outstanding watch. * Returns false on failure (no watch on that node). */ bool xs_unwatch(struct xs_handle *h, const char *path, const char *token); /* Start a transaction: change
'''
tcp_message Inline Script Hook API Demonstration
------------------------------------------------

* modifies packets containing "foo" to "bar"
* prints various details for each packet.

example cmdline invocation:
mitmdump -T --host --tcp ".*" -q -s examples/tcp_message.py
'''
from netlib.utils import clean_bin

def tcp_message(ctx, tcp_msg):
    modified_msg = tcp_msg.message.replace("foo", "bar")

    is_modified = False if modified_msg == tcp_msg.message else True
    tcp_msg.message = modified_msg

    print("[tcp_message{}] from {} {} to {} {}:\r\n{}".format(
        " (modified)" if is_modified else "",
        "client" if tcp_msg.sender == tcp_msg.client_conn else "server",
        tcp_msg.sender.address,
        "server" if tcp_msg.receiver == tcp_msg.server_conn else "client",
        tcp_msg.receiver.address, clean_bin(tcp_msg.message)))