| 1 | /* |
| 2 | * libnet |
| 3 | * libnet_build_sebek.c - sebek packet assembler |
| 4 | * |
| 5 | * Copyright (c) 2004 Frederic Raynal <pappy@security-labs.org> |
| 6 | * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com> |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #if (HAVE_CONFIG_H) |
| 33 | #include "../include/config.h" |
| 34 | #endif |
| 35 | #if (!(_WIN32) || (__CYGWIN__)) |
| 36 | #include "../include/libnet.h" |
| 37 | #else |
| 38 | #include "../include/win32/libnet.h" |
| 39 | #endif |
| 40 | |
| 41 | libnet_ptag_t |
| 42 | libnet_build_sebek(u_int32_t magic, u_int16_t version, u_int16_t type, |
| 43 | u_int32_t counter, u_int32_t time_sec, u_int32_t time_usec, u_int32_t pid, |
| 44 | u_int32_t uid, u_int32_t fd, u_int8_t cmd[SEBEK_CMD_LENGTH], u_int32_t length, |
| 45 | u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag) |
| 46 | { |
| 47 | u_int32_t n; |
| 48 | libnet_pblock_t *p; |
| 49 | struct libnet_sebek_hdr sebek_hdr; |
| 50 | |
| 51 | if (l == NULL) |
| 52 | { |
| 53 | return (-1); |
| 54 | } |
| 55 | |
| 56 | n = LIBNET_SEBEK_H + payload_s; /* size of memory block */ |
| 57 | |
| 58 | /* |
| 59 | * Find the existing protocol block if a ptag is specified, or create |
| 60 | * a new one. |
| 61 | */ |
| 62 | p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_SEBEK_H); |
| 63 | if (p == NULL) |
| 64 | { |
| 65 | return (-1); |
| 66 | } |
| 67 | |
| 68 | memset(&sebek_hdr, 0, sizeof(sebek_hdr)); |
| 69 | sebek_hdr.magic = htonl(magic); |
| 70 | sebek_hdr.version = htons(version); |
| 71 | sebek_hdr.type = htons(type); |
| 72 | sebek_hdr.counter = htonl(counter); |
| 73 | sebek_hdr.time_sec = htonl(time_sec); |
| 74 | sebek_hdr.time_usec = htonl(time_usec); |
| 75 | sebek_hdr.pid = htonl(pid); |
| 76 | sebek_hdr.uid = htonl(uid); |
| 77 | sebek_hdr.fd = htonl(fd); |
| 78 | memcpy(sebek_hdr.cmd, cmd, SEBEK_CMD_LENGTH*sizeof(u_int8_t)); |
| 79 | sebek_hdr.length = htonl(length); |
| 80 | |
| 81 | n = libnet_pblock_append(l, p, (u_int8_t *)&sebek_hdr, LIBNET_SEBEK_H); |
| 82 | if (n == -1) |
| 83 | { |
| 84 | goto bad; |
| 85 | } |
| 86 | |
| 87 | if ((payload && !payload_s) || (!payload && payload_s)) |
| 88 | { |
| 89 | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| 90 | "%s(): payload inconsistency\n", __func__); |
| 91 | goto bad; |
| 92 | } |
| 93 | |
| 94 | if (payload && payload_s) |
| 95 | { |
| 96 | n = libnet_pblock_append(l, p, payload, payload_s); |
| 97 | if (n == -1) |
| 98 | { |
| 99 | goto bad; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return (ptag ? ptag : libnet_pblock_update(l, p, 0, LIBNET_PBLOCK_SEBEK_H)); |
| 104 | bad: |
| 105 | libnet_pblock_delete(l, p); |
| 106 | return (-1); |
| 107 | } |
| 108 | /* EOF */ |