]> sjero.net Git - ltp2tcp/blob - ltp.c
Updated commandline args: Added -V for version, -h for help, changed -d to -v for...
[ltp2tcp] / ltp.c
1 /******************************************************************************
2 Utility to convert a LTP flow to a TCP flow for LTP analysis via tcptrace.
3 LTP Header manipulation functions
4
5 Copyright (C) 2013  Samuel Jero <sj323707@ohio.edu>
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 3 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, see <http://www.gnu.org/licenses/>.
19
20 Author: Samuel Jero <sj323707@ohio.edu>
21 Date: 06/2013
22
23 Notes:
24         1)Only handles one LTP "connection". There isn't a good way to separate
25                 different LTP "connections" from new sessions of the same "connection".
26                 Use Tcpdump filters to separate connections. Libpcap filtering could also
27                 be added in ltp2tcp.
28         2)Uses some special types from Linux (u_char, u_int32_t)
29 ******************************************************************************/
30 #include "ltp.h"
31
32
33 /*
34  * SDNV has a zero in high-order bit position of last byte. The high-order
35  * bit of all preceding bytes is set to one. This returns the numeric value
36  * in an integer and sets the value of the second argument to the number of
37  * bytes used to code the SDNV. A -1 is returned if the evaluation fails
38  * (value exceeds maximum for signed integer). 0 is an acceptable value.
39  */
40
41 #define SDNV_MASK       0x7f
42
43 /*2rd arg is number of bytes in field (returned)*/
44 int evaluate_sdnv(const u_char* loc, int *bytecount)
45 {
46     int value = 0;
47     u_char curbyte;
48
49     *bytecount = 0;
50
51     /*
52      * Get 1st byte and continue to get them while high-order bit is 1
53      */
54
55     while((curbyte = *(loc++)) & ~SDNV_MASK) {
56         if(*bytecount >= (int) sizeof(int)) {
57             *bytecount = 0;
58             return -1;
59         }
60         value = value << 7;
61         value |= (curbyte & SDNV_MASK);
62         ++*bytecount;
63     }
64
65     /*
66      * Add in the byte whose high-order bit is 0 (last one)
67      */
68
69     value = value << 7;
70     value |= (curbyte & SDNV_MASK);
71     ++*bytecount;
72     return value;
73 }
74
75 /* Special Function to evaluate 64 bit SDNVs */
76 long long evaluate_sdnv_64(const u_char* loc, int *bytecount)
77 {
78     long long value = 0;
79     u_char curbyte;
80
81     *bytecount = 0;
82
83     /*
84      * Get 1st byte and continue to get them while high-order bit is 1
85      */
86
87     while((curbyte = *(loc++)) & ~SDNV_MASK) {
88         if(*bytecount >= (int) sizeof(long long)) {
89             *bytecount = 0;
90             return -1;
91         }
92         value = value<< 7;
93         value |= (curbyte & SDNV_MASK);
94         ++*bytecount;
95     }
96
97     /*
98      * Add in the byte whose high-order bit is 0 (last one)
99      */
100
101     value = value << 7;
102     value |= (curbyte & SDNV_MASK);
103     ++*bytecount;
104     return value;
105 }