-
Notifications
You must be signed in to change notification settings - Fork 2
/
params.c
215 lines (208 loc) · 5.32 KB
/
params.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <assert.h>
#include <signal.h>
#include "signals.h"
#include "params.h"
#include "help.h"
#include "collision.h"
char** p_targetv = NULL;
int p_targetc = 0;
int p_server = 0;
int p_once = 0;
int p_connqueue = 128;
const char* p_cmd = "/bin/sh";
char* p_incmd = NULL;
enum iomode p_inmode = IOMODE_NONE;
char* p_outcmd = NULL;
enum iomode p_outmode = IOMODE_NONE;
char* p_iocmd = NULL;
enum iomode p_iomode = IOMODE_NONE;
int p_chldterm = SIGTERM;
struct timeval p_wait = { 0 };
int p_sync = 0;
int p_recvbuff = 4096;
int p_sendbuff = 4096;
const static char options[] = "hvVl1q:c:i:o:w:s:b:";
const static struct option long_options[] = {
{ "io", 1, 0, 0 },
{ "rb", 1, 0, 0 },
{ "wb", 1, 0, 0 },
{ "sync", 0, 0, 0 },
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'v' },
{ "listen", 0, 0, 'l' },
{ "once", 0, 0, '1' },
{ "connqueue", 1, 0, 'q' },
{ "cmd", 1, 0, 'c' },
{ "in", 1, 0, 'i' },
{ "out", 1, 0, 'o' },
{ "wait", 1, 0, 'w' },
{ "signal", 1, 0, 's' },
{ 0, 0, 0, 0 }
};
char* getcmd(char* str, enum iomode* mode)
{
if ( !memcmp( str, "null", 4 ) )
{
*mode = IOMODE_NULL;
return NULL;
}
else if ( !memcmp( str, "block", 5) )
{
*mode = IOMODE_BLOCK;
return NULL;
}
else if ( !memcmp( str, "every:", 6 ) )
{
*mode = IOMODE_EVERY;
return str + 6;
}
else if ( !memcmp( str, "once:", 5 ) )
{
*mode = IOMODE_ONCE;
return str + 5;
}
return NULL;
}
void params(int argc, char* argv[])
{
int option;
int option_index;
while( 1 )
{
option = getopt_long(argc, argv, options, long_options, &option_index);
if ( option == -1 )
break;
switch( option )
{
case 0:
{
if ( option_index == 0 )
{
p_iocmd = strdup( getcmd( optarg, &p_iomode ) );
}
else if ( option_index == 1 )
{
char* pend;
p_recvbuff = strtol( optarg, &pend, 10 );
if ( *pend == 'k' || *pend == 'K' )
p_recvbuff *= 1024;
else if ( *pend == 'm' || *pend == 'M' )
p_recvbuff *= 1024 * 1024;
}
else if ( option_index == 2 )
{
char* pend;
p_sendbuff = strtol( optarg, &pend, 10 );
if ( *pend == 'k' || *pend == 'K' )
p_sendbuff *= 1024;
else if ( *pend == 'm' || *pend == 'M' )
p_sendbuff *= 1024 * 1024;
}
else if ( option_index == 3 )
{
p_sync = 1;
}
}
break;
case 'h':
{
help();
exit( EXIT_SUCCESS );
}
break;
case 'v':
{
build_str( stdout );
exit( EXIT_SUCCESS );
}
break;
case 'V':
{
build_str( stderr );
}
break;
case 'l':
{
p_server = 1;
}
break;
case '1':
{
p_once = 1;
}
break;
case 'q':
{
p_connqueue = atoi( optarg );
}
break;
case 'c':
{
p_cmd = strdup( optarg );
}
break;
case 'i':
{
p_incmd = strdup( getcmd( optarg, &p_inmode ) );
}
break;
case 'o':
{
p_outcmd = strdup( getcmd( optarg, &p_outmode ) );
}
break;
case 'w':
{
char* pend;
long msec = strtol( optarg, &pend, 10 );
switch ( *pend )
{
case 's':
msec *= 1000;
break;
case 'm':
msec *= 1000 * 60;
break;
case 'h':
msec *= 1000 * 60 * 60;
break;
}
p_wait.tv_sec = msec / 1000;
p_wait.tv_usec = msec % 1000;
}
break;
case 's':
{
p_chldterm = signal_str( optarg );
}
break;
default:
{
help();
exit( EXIT_FAILURE );
}
break;
}
}
if ( optind < argc )
{
p_targetc = argc - optind;
p_targetv = (char**)malloc( sizeof(char*) * p_targetc );
int ind;
int argind;
for (ind = 0, argind = optind; argind < argc; ++ind, ++argind)
p_targetv[ind] = strdup( argv[argind] );
}
else
{
fprintf( stderr, "<target> not specified!\n" );
help();
exit( EXIT_FAILURE );
}
/* collision check */
collision_check();
}