Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | /* |
2 | * | |
3 | * libnet | |
4 | * libnet_build_rpc.c - RPC packet assembler | |
5 | * | |
6 | * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com> | |
7 | * Jason Damron <jdamron@stackheap.org> | |
8 | * All rights reserved. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | * | |
31 | */ | |
32 | ||
33 | #if (HAVE_CONFIG_H) | |
34 | #include "../include/config.h" | |
35 | #endif | |
36 | #if (!(_WIN32) || (__CYGWIN__)) | |
37 | #include "../include/libnet.h" | |
38 | #else | |
39 | #include "../include/win32/libnet.h" | |
40 | #endif | |
41 | ||
42 | libnet_ptag_t | |
43 | libnet_build_rpc_call(u_int32_t rm, u_int32_t xid, u_int32_t prog_num, | |
44 | u_int32_t prog_vers, u_int32_t procedure, u_int32_t cflavor, u_int32_t clength, | |
45 | u_int8_t *cdata, u_int32_t vflavor, u_int32_t vlength, u_int8_t *vdata, | |
46 | u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag) | |
47 | { | |
48 | u_int32_t n, h; | |
49 | libnet_pblock_t *p; | |
50 | struct libnet_rpc_call_tcp_hdr rpc_hdr; | |
51 | ||
52 | if (l == NULL) | |
53 | { | |
54 | return (-1); | |
55 | } | |
56 | ||
57 | /* Credential and Verifier buffers not yet implemented. | |
58 | * n = LIBNET_RPC_CALL_H + clength + vlength + payload_s; | |
59 | */ | |
60 | ||
61 | if (rm) | |
62 | { | |
63 | n = LIBNET_RPC_CALL_TCP_H + payload_s; | |
64 | } | |
65 | else | |
66 | { | |
67 | n = LIBNET_RPC_CALL_H + payload_s; | |
68 | } | |
69 | ||
70 | h = 0; | |
71 | ||
72 | /* | |
73 | * Find the existing protocol block if a ptag is specified, or create | |
74 | * a new one. | |
75 | */ | |
76 | p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_RPC_CALL_H); | |
77 | if (p == NULL) | |
78 | { | |
79 | return (-1); | |
80 | } | |
81 | ||
82 | memset(&rpc_hdr, 0, sizeof(rpc_hdr)); | |
83 | if (rm) | |
84 | { | |
85 | rpc_hdr.rpc_record_marking = htonl(rm + payload_s); | |
86 | } | |
87 | rpc_hdr.rpc_common.rpc_xid = htonl(xid); | |
88 | rpc_hdr.rpc_common.rpc_type = LIBNET_RPC_CALL; | |
89 | rpc_hdr.rpc_common.rpc_call.rpc_rpcvers = htonl(LIBNET_RPC_VERS); | |
90 | rpc_hdr.rpc_common.rpc_call.rpc_prognum = htonl(prog_num); | |
91 | rpc_hdr.rpc_common.rpc_call.rpc_vers = htonl(prog_vers); | |
92 | rpc_hdr.rpc_common.rpc_call.rpc_procedure = htonl(procedure); | |
93 | /* XXX Eventually should allow for opaque auth data. */ | |
94 | rpc_hdr.rpc_common.rpc_call.rpc_credentials.rpc_auth_flavor= htonl(cflavor); | |
95 | rpc_hdr.rpc_common.rpc_call.rpc_credentials.rpc_auth_length= htonl(clength); | |
96 | rpc_hdr.rpc_common.rpc_call.rpc_verifier.rpc_auth_flavor = htonl(vflavor); | |
97 | rpc_hdr.rpc_common.rpc_call.rpc_verifier.rpc_auth_length = htonl(vlength); | |
98 | ||
99 | if (rm) | |
100 | { | |
101 | n = libnet_pblock_append(l, p, (u_int8_t *)&rpc_hdr, | |
102 | LIBNET_RPC_CALL_TCP_H); | |
103 | } | |
104 | else | |
105 | { | |
106 | n = libnet_pblock_append(l, p, (u_int8_t *)&rpc_hdr.rpc_common, | |
107 | LIBNET_RPC_CALL_H); | |
108 | } | |
109 | ||
110 | if (n == -1) | |
111 | { | |
112 | goto bad; | |
113 | } | |
114 | ||
115 | if ((payload && !payload_s) || (!payload && payload_s)) | |
116 | { | |
117 | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, | |
118 | "%s(): payload inconsistency\n", __func__); | |
119 | goto bad; | |
120 | } | |
121 | ||
122 | if (payload && payload_s) | |
123 | { | |
124 | n = libnet_pblock_append(l, p, payload, payload_s); | |
125 | if (n == -1) | |
126 | { | |
127 | goto bad; | |
128 | } | |
129 | } | |
130 | ||
131 | return (ptag ? ptag : libnet_pblock_update(l, p, h, | |
132 | LIBNET_PBLOCK_RPC_CALL_H)); | |
133 | bad: | |
134 | libnet_pblock_delete(l, p); | |
135 | return (-1); | |
136 | } | |
137 | ||
138 | /* EOF */ |