-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (146 loc) · 3.74 KB
/
main.cpp
File metadata and controls
159 lines (146 loc) · 3.74 KB
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
#include <stdio.h>
#include <termios.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define VERSION "0.0.5"
#include "disp_basic.h"
#include "tcp.h"
#include "sersniff.h"
char * speed_str[] = { "300", "1200", "2400", "4800", "9600", "19200", "38400",
"57600", "115200", "230400", NULL };
speed_t speed_num[] = { B300, B1200, B2400, B4800, B9600, B19200, B38400,
B57600, B115200, B230400, B0 };
void usage()
{
fprintf(stderr,"sersniff v%s\n"
"Usage:\n"
"sersniff [-h] [-i DEV | -l PORT] [-o DEV | -c HOST:PORT] [-b BAUD] [-w USEC]\n"
"-h This help\n"
"-x Show hex characters instead of alpha\n"
"-f PRINTF_OPTS printf style options for printing hex characters\n"
" when '-x' switch is given (default \"<%%02hX>\")\n"
"-i DEVICE Port 1 device (defaults to /dev/ttyS0). Use host:port for\n"
" TCP.\n"
"-o DEVICE Port 2 device (defaults to /dev/ttyS1). Use :port for TCP.\n"
"-b BAUD Baud rate (Defaults to 19200)\n"
"-a Log the incoming traffic to console\n"
"-n No port configuration (do not set BAUD or change settings)\n"
"-w USECS How many microsecs to wait before reporting a delay\n"
" (default is %d)\n"
"-s Silent - don't pass data from port1 <=> port2,\n"
" just display what we see from them.\n"
"-z Don't quit when an EOF character is received.\n"
,VERSION,USEC);
::exit(1);
}
int main(int argc, char *argv[])
{
int optret, speed;
int port1, port2;
char *dev1=NULL;
char *dev2=NULL;
int listenport=0;
int connectport;
char *connecthost=NULL, *tmpchr=NULL;
int show_alpha=1;
speed_t baud=B0;
int silent=0;
long usec_threshold=USEC;
int setup_port=1;
int quit_on_eof=1;
char *format=NULL;
bool logToConsole = false;
while ((optret=getopt(argc,argv,"ahxsnzi:l:o:c:b:w:1:2:f:"))!=EOF) {
switch (optret) {
case '?': case 'h': case ':':
usage();
case 'a':
logToConsole = true;
break;
case 's':
silent=1;
break;
case 'w':
usec_threshold=atoi(optarg);
break;
case 'i':
if ((tmpchr=strchr(optarg, ':'))!=NULL &&
(strchr(optarg, '/')==NULL)) {
*tmpchr='\0';
listenport=atoi(++tmpchr);
} else {
dev1=strdup(optarg);
}
break;
case 'o':
if ((tmpchr=strchr(optarg, ':'))!=NULL &&
(strchr(optarg, '/')==NULL)) {
*tmpchr='\0';
connectport=atoi(++tmpchr);
connecthost=strdup(optarg);
} else {
dev2=strdup(optarg);
}
break;
case 'x':
show_alpha=0;
break;
case 'b':
for (speed=0;
speed_str[speed];
speed++) {
if (strstr(optarg,speed_str[speed])==optarg) {
baud=speed_num[speed];
break;
}
}
if (baud==B0) {
fprintf(stderr,"Unsupported Baud: '%s'\n",
optarg);
::exit(1);
}
break;
case 'n':
setup_port=0;
break;
case 'f':
format=strdup(optarg);
break;
case 'z':
quit_on_eof=0;
break;
}
}
/* Default settings */
//if (!dev1 && !listenport) dev1=strdup("/dev/ttyS0");
//if (!name1 && !listenport) name1=strdup("Port1");
//if (!dev2 && !connecthost) dev2=strdup("/dev/ttyS1");
//if (!name2 && !connecthost) name2=strdup("Port2");
if (baud==B0) baud=B19200;
if (!format) format=strdup("0x%02hX");
disp_init();
if (dev1) {
port1=openport(dev1, baud, setup_port);
} else {
disp_outputstatus("Waiting for connection to TCP port.");
port1=listensock(listenport);
}
if (dev2) {
port2=openport(dev2, baud, setup_port);
} else if (connecthost) {
disp_outputstatus("Connecting to TCP port.");
port2=opensock(connecthost, connectport);
}
if (port1 < 0 || port2 < 0) {
fprintf(stderr,"Argh. An open failed!\n");
::exit(1);
}
mainloop(port1, port2, silent, show_alpha, quit_on_eof, usec_threshold, format, logToConsole);
/* Clean up */
if (dev1) free(dev1);
if (dev2) free(dev2);
if (connecthost) free(connecthost);
disp_close();
return 0;
}