-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtcp-scan.h
230 lines (203 loc) · 6.55 KB
/
tcp-scan.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* The TCP Scanner (tcp-scan) is Copyright (C) 2003-2008 Roy Hills,
* NTA Monitor Ltd.
*
* This file is part of tcp-scan.
*
* tcp-scan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* tcp-scan 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tcp-scan. If not, see <http://www.gnu.org/licenses/>.
*
* tcp-scan.h -- Header file for TCP protocol specific scanner
*
* Author: Roy Hills
* Date: 16 September 2003
*
* This header file contains definitions required by only the protocol-
* specific code.
*/
/* Includes */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* C89 standard headers */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h> /* FreeBSD needs explicit include for sys/types.h */
#ifdef __CYGWIN__
#include <windows.h> /* Include windows.h if compiling under Cygwin */
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
/* Include getopt.h for the sake of getopt_long.
We don't need the declaration of getopt, and it could conflict
with something from a system header file, so effectively nullify that. */
#define getopt getopt_loser
#include "getopt.h"
#undef getopt
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> /* For struct sockaddr */
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_REGEX_H
#include <regex.h> /* Posix regular expression support */
#endif
#ifdef HAVE_PCAP_H
#include <pcap.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
#include "ip.h"
#include "tcp.h"
/* Defines */
#define MAXLINE 255 /* Max line length for input files */
#define MAXIP 65515 /* Max IP data size = 64k - 20 */
#define REALLOC_COUNT 1000 /* Entries to realloc at once */
#define DEFAULT_BANDWIDTH 56000 /* Default bandwidth in bits/sec */
#define MINIMUM_FRAME_SIZE 46 /* Minimum data size for layer 2 */
#define PACKET_OVERHEAD 18 /* Size of Ethernet header */
/* IP protocol 6 = TCP */
#define IP_PROTOCOL 6 /* Default IP Protocol */
#define DEFAULT_BACKOFF_FACTOR 1.5 /* Default timeout backoff factor */
#define DEFAULT_RETRY 3 /* Default number of retries */
#define DEFAULT_TIMEOUT 2000 /* Default per-host timeout in ms */
#define SNAPLEN 94 /* 14 (ether) + 20 (IP) + 60 (TCP) */
#define PROMISC 0 /* Enable promiscuous mode */
#define TO_MS 0 /* Timeout for pcap_open_live() */
#define OPTIMISE 1 /* Optimise pcap filter */
#define DEFAULT_WINDOW 5840 /* TCP Window */
#define DEFAULT_MSS 1460 /* TCP MSS */
#define DEFAULT_TTL 64 /* IP TTL */
#define DEFAULT_DF 1 /* IP DF Flag */
#define DEFAULT_TOS 0 /* IP TOS Field */
#define SERVICE_FILE "tcp-scan-services"
/* Structures */
typedef union {
struct in_addr v4;
struct in6_addr v6;
} ip_address;
typedef struct {
unsigned n; /* Ordinal number for this entry */
unsigned timeout; /* Timeout for this host in us */
ip_address addr; /* Host IP address */
struct timeval last_send_time; /* Time when last packet sent to this addr */
unsigned short num_sent; /* Number of packets sent */
unsigned short num_recv; /* Number of packets received */
uint16_t dport; /* Destination port */
unsigned char live; /* Set when awaiting response */
} host_entry;
typedef struct {
int cwr;
int ecn;
int urg;
int ack;
int psh;
int rst;
int syn;
int fin;
} tcp_flags_struct;
/* TCP Pseudo Header for checksum calculation */
typedef struct {
uint32_t s_addr;
uint32_t d_addr;
uint8_t mbz;
uint8_t proto;
uint16_t len;
} pseudo_hdr;
/* Functions */
#ifndef HAVE_STRLCAT
size_t strlcat(char *dst, const char *src, size_t siz);
#endif
#ifndef HAVE_STRLCPY
size_t strlcpy(char *dst, const char *src, size_t siz);
#endif
void err_sys(const char *, ...);
void warn_sys(const char *, ...);
void err_msg(const char *, ...);
void warn_msg(const char *, ...);
void err_print(int, const char *, va_list);
void usage(int, int);
void add_host(const char *, unsigned);
int send_packet(int, host_entry *, int, struct timeval *);
void recvfrom_wto(int, int);
void remove_host(host_entry **);
void timeval_diff(const struct timeval *, const struct timeval *,
struct timeval *);
host_entry *find_host(host_entry **, const struct in_addr *,
const unsigned char *, unsigned);
void display_packet(unsigned, const unsigned char *, const host_entry *,
const struct in_addr *);
void advance_cursor(void);
void dump_list(void);
void print_times(void);
void initialise(void);
void clean_up(void);
void tcp_scan_version(void);
char *make_message(const char *, ...);
char *printable(const unsigned char*, size_t);
void callback(u_char *, const struct pcap_pkthdr *, const u_char *);
void process_options(int, char *[]);
ip_address *get_host_address(const char *, int, ip_address *, char **);
const char *my_ntoa(ip_address, int);
/* Wrappers */
int Gettimeofday(struct timeval *);
void *Malloc(size_t);
void *Realloc(void *, size_t);
unsigned long int Strtoul(const char *, int);
long int Strtol(const char *, int);
char *my_lookupdev(char *);
unsigned int hstr_i(const char *);
uint16_t in_cksum(const uint16_t *, int);
uint32_t get_source_ip(const char *);
void add_host_port(const char *, unsigned, unsigned);
void create_port_list(const char *);
void process_tcp_flags(const char *);
unsigned str_to_bandwidth(const char *);
unsigned str_to_interval(const char *);
char *dupstr(const char *);
/* MT19937 prototypes */
void init_genrand(unsigned long);
void init_by_array(unsigned long[], int);
unsigned long genrand_int32(void);
long genrand_int31(void);
double genrand_real1(void);
double genrand_real2(void);
double genrand_real3(void);
double genrand_res53(void);