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
libnet_build_sebek(uint32_t magic,uint16_t version,uint16_t type,uint32_t counter,uint32_t time_sec,uint32_t time_usec,uint32_t pid,uint32_t uid,uint32_t fd,uint8_t cmd[SEBEK_CMD_LENGTH],uint32_t length,const uint8_t * payload,uint32_t payload_s,libnet_t * l,libnet_ptag_t ptag)42 libnet_build_sebek(uint32_t magic, uint16_t version, uint16_t type,
43 uint32_t counter, uint32_t time_sec, uint32_t time_usec, uint32_t pid,
44 uint32_t uid, uint32_t fd, uint8_t cmd[SEBEK_CMD_LENGTH], uint32_t length,
45 const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
46 {
47     uint32_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(uint8_t));
79     sebek_hdr.length = htonl(length);
80 
81     n = libnet_pblock_append(l, p, (uint8_t *)&sebek_hdr, LIBNET_SEBEK_H);
82     if (n == -1)
83     {
84         goto bad;
85     }
86 
87     /* boilerplate payload sanity check / append macro */
88     LIBNET_DO_PAYLOAD(l, p);
89 
90     return (ptag ? ptag : libnet_pblock_update(l, p, 0, LIBNET_PBLOCK_SEBEK_H));
91 bad:
92     libnet_pblock_delete(l, p);
93     return (-1);
94 }
95 /* EOF */
96