blob: b062533c53dee6c2f30077f4cf847051aabbfbe2 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
Harald Weltef6214982017-09-24 10:23:24 +08002 File autogenerated by gengetopt version 2.22.6
jjako52c24142002-12-16 13:33:51 +00003 generated with the following command:
jjako1a51df72004-07-20 08:30:21 +00004 gengetopt --conf-parser
jjako52c24142002-12-16 13:33:51 +00005
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
9*/
10
jjako52c24142002-12-16 13:33:51 +000011/* If we use autoconf. */
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
jjako52c24142002-12-16 13:33:51 +000015
Harald Welte1b3e5772010-05-04 11:13:56 +020016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
Harald Weltef6214982017-09-24 10:23:24 +080020#ifndef FIX_UNUSED
21#define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22#endif
23
24#include <getopt.h>
jjako52c24142002-12-16 13:33:51 +000025
26#include "cmdline.h"
27
Harald Welte1b3e5772010-05-04 11:13:56 +020028const char *gengetopt_args_info_purpose = "";
jjako52c24142002-12-16 13:33:51 +000029
Harald Welte7e1175f2017-09-24 10:53:53 +080030const char *gengetopt_args_info_usage = "Usage: sgsnemu [OPTIONS]...";
jjako52c24142002-12-16 13:33:51 +000031
Harald Weltef6214982017-09-24 10:23:24 +080032const char *gengetopt_args_info_versiontext = "";
33
34const char *gengetopt_args_info_description = "";
35
Harald Welte1b3e5772010-05-04 11:13:56 +020036const char *gengetopt_args_info_help[] = {
Harald Weltef6214982017-09-24 10:23:24 +080037 " -h, --help Print help and exit",
38 " -V, --version Print version and exit",
39 " -d, --debug Run in debug mode (default=off)",
40 " -c, --conf=STRING Read configuration file",
41 " --pidfile=STRING Filename of process id file\n (default=`./sgsnemu.pid')",
42 " --statedir=STRING Directory of nonvolatile data (default=`./')",
43 " --dns=STRING DNS Server to use",
Pau Espin Pedrol5fdda132018-02-12 18:03:09 +010044 " -l, --listen=STRING Local host",
Harald Weltef6214982017-09-24 10:23:24 +080045 " -r, --remote=STRING Remote host",
46 " --contexts=INT Number of contexts (default=`1')",
47 " --timelimit=INT Exit after timelimit seconds (default=`0')",
48 " --gtpversion=INT GTP version to use (default=`1')",
49 " -a, --apn=STRING Access point name (default=`internet')",
50 " --selmode=INT Selection mode (default=`0x01')",
51 " --rattype=1..5 Radio Access Technology Type (default=`1')",
52 " --userloc=type.MCC.MNC.LAC.CIorSACorRAC\n User Location Information\n (default=`02509946241207')",
53 " --rai=MCC.MNC.LAC.RAC Routing Area Information\n (default=`02509946241207')",
54 " --mstz=sign.NbQuartersOfAnHour.DSTAdjustment\n MS Time Zone (default=`0')",
55 " --imeisv=STRING IMEI(SV) International Mobile Equipment\n Identity (and Software Version)\n (default=`2143658709214365')",
56 " --norecovery Do not send recovery (default=off)",
57 " -i, --imsi=STRING IMSI (default=`240010123456789')",
58 " --nsapi=INT NSAPI (default=`0')",
59 " -m, --msisdn=STRING Mobile Station ISDN number\n (default=`46702123456')",
60 " -q, --qos=INT Requested quality of service\n (default=`0x000b921f')",
61 " --qose1=INT Requested quality of service Extension 1\n (default=`0x9396404074f9ffff')",
62 " --qose2=INT Requested quality of service Extension 2\n (default=`0x11')",
63 " --qose3=INT Requested quality of service Extension 3\n (default=`0x0101')",
64 " --qose4=INT Requested quality of service Extension 4\n (default=`0x4040')",
65 " --charging=INT Charging characteristics (default=`0x0800')",
66 " -u, --uid=STRING Login user ID (default=`mig')",
67 " -p, --pwd=STRING Login password (default=`hemmelig')",
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +020068 "\n Mode: createif\n any option of this mode is related to tun interface, all payload going in and\n out via tunN interface",
Harald Weltef6214982017-09-24 10:23:24 +080069 " --createif Create local network interface (default=off)",
70 " -n, --net=STRING Network address for local interface",
71 " --defaultroute Create default route (default=off)",
72 " --ipup=STRING Script to run after link-up",
73 " --ipdown=STRING Script to run after link-down",
Harald Welte73abc382017-10-10 08:50:11 +080074 " --tun-device=STRING Name of the local network interface",
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +020075 "\n Mode: pinghost\n generate ICMP payload inside G-PDU without setting up tun interface",
Harald Weltef6214982017-09-24 10:23:24 +080076 " --pinghost=STRING Ping remote host",
77 " --pingrate=INT Number of ping req per second (default=`1')",
78 " --pingsize=INT Number of ping data bytes (default=`56')",
79 " --pingcount=INT Number of ping req to send (default=`0')",
80 " --pingquiet Do not print ping packet info (default=off)",
Harald Weltefbb9c7f2017-09-24 11:50:20 +080081 " --no-tx-gpdu-seq Don't transmit G-PDU sequence nums\n (default=off)",
Harald Welte840a8e92017-09-24 18:12:40 +080082 " -t, --pdp-type=(v4|v6) PDP Type (default=`v4')",
Harald Weltebed35df2011-11-02 13:06:18 +010083 0
Harald Welte1b3e5772010-05-04 11:13:56 +020084};
85
Harald Weltef6214982017-09-24 10:23:24 +080086typedef enum { ARG_NO, ARG_FLAG, ARG_STRING, ARG_INT
87} cmdline_parser_arg_type;
88
Harald Welte1b3e5772010-05-04 11:13:56 +020089static
Harald Weltebed35df2011-11-02 13:06:18 +010090void clear_given(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020091static
Harald Weltebed35df2011-11-02 13:06:18 +010092void clear_args(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020093
94static int
Harald Weltef6214982017-09-24 10:23:24 +080095cmdline_parser_internal(int argc, char **argv,
96 struct gengetopt_args_info *args_info,
97 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +010098 const char *additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +020099
Harald Welte73abc382017-10-10 08:50:11 +0800100static int
101cmdline_parser_required2(struct gengetopt_args_info *args_info,
102 const char *prog_name, const char *additional_error);
Harald Weltebed35df2011-11-02 13:06:18 +0100103struct line_list {
104 char *string_arg;
105 struct line_list *next;
Harald Welte1b3e5772010-05-04 11:13:56 +0200106};
107
108static struct line_list *cmd_line_list = 0;
109static struct line_list *cmd_line_list_tmp = 0;
110
Harald Weltebed35df2011-11-02 13:06:18 +0100111static void free_cmd_list(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200112{
Harald Weltebed35df2011-11-02 13:06:18 +0100113 /* free the list of a previous call */
114 if (cmd_line_list) {
115 while (cmd_line_list) {
116 cmd_line_list_tmp = cmd_line_list;
117 cmd_line_list = cmd_line_list->next;
118 free(cmd_line_list_tmp->string_arg);
119 free(cmd_line_list_tmp);
120 }
121 }
jjako52c24142002-12-16 13:33:51 +0000122}
123
Harald Weltebed35df2011-11-02 13:06:18 +0100124static char *gengetopt_strdup(const char *s);
jjako52c24142002-12-16 13:33:51 +0000125
Harald Welte1b3e5772010-05-04 11:13:56 +0200126static
Harald Weltebed35df2011-11-02 13:06:18 +0100127void clear_given(struct gengetopt_args_info *args_info)
jjako52c24142002-12-16 13:33:51 +0000128{
Harald Weltebed35df2011-11-02 13:06:18 +0100129 args_info->help_given = 0;
130 args_info->version_given = 0;
131 args_info->debug_given = 0;
132 args_info->conf_given = 0;
133 args_info->pidfile_given = 0;
134 args_info->statedir_given = 0;
135 args_info->dns_given = 0;
136 args_info->listen_given = 0;
137 args_info->remote_given = 0;
138 args_info->contexts_given = 0;
139 args_info->timelimit_given = 0;
140 args_info->gtpversion_given = 0;
141 args_info->apn_given = 0;
142 args_info->selmode_given = 0;
143 args_info->rattype_given = 0;
144 args_info->userloc_given = 0;
145 args_info->rai_given = 0;
146 args_info->mstz_given = 0;
147 args_info->imeisv_given = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800148 args_info->norecovery_given = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100149 args_info->imsi_given = 0;
150 args_info->nsapi_given = 0;
151 args_info->msisdn_given = 0;
152 args_info->qos_given = 0;
153 args_info->qose1_given = 0;
154 args_info->qose2_given = 0;
155 args_info->qose3_given = 0;
156 args_info->qose4_given = 0;
157 args_info->charging_given = 0;
158 args_info->uid_given = 0;
159 args_info->pwd_given = 0;
160 args_info->createif_given = 0;
161 args_info->net_given = 0;
162 args_info->defaultroute_given = 0;
163 args_info->ipup_given = 0;
164 args_info->ipdown_given = 0;
Harald Welte73abc382017-10-10 08:50:11 +0800165 args_info->tun_device_given = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100166 args_info->pinghost_given = 0;
167 args_info->pingrate_given = 0;
168 args_info->pingsize_given = 0;
169 args_info->pingcount_given = 0;
170 args_info->pingquiet_given = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800171 args_info->no_tx_gpdu_seq_given = 0;
Harald Welte840a8e92017-09-24 18:12:40 +0800172 args_info->pdp_type_given = 0;
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +0200173 args_info->createif_mode_counter = 0;
174 args_info->pinghost_mode_counter = 0;
jjako52c24142002-12-16 13:33:51 +0000175}
176
Harald Welte1b3e5772010-05-04 11:13:56 +0200177static
Harald Weltebed35df2011-11-02 13:06:18 +0100178void clear_args(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200179{
Harald Weltef6214982017-09-24 10:23:24 +0800180 FIX_UNUSED(args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100181 args_info->debug_flag = 0;
182 args_info->conf_arg = NULL;
183 args_info->conf_orig = NULL;
184 args_info->pidfile_arg = gengetopt_strdup("./sgsnemu.pid");
185 args_info->pidfile_orig = NULL;
186 args_info->statedir_arg = gengetopt_strdup("./");
187 args_info->statedir_orig = NULL;
188 args_info->dns_arg = NULL;
189 args_info->dns_orig = NULL;
190 args_info->listen_arg = NULL;
191 args_info->listen_orig = NULL;
192 args_info->remote_arg = NULL;
193 args_info->remote_orig = NULL;
194 args_info->contexts_arg = 1;
195 args_info->contexts_orig = NULL;
196 args_info->timelimit_arg = 0;
197 args_info->timelimit_orig = NULL;
198 args_info->gtpversion_arg = 1;
199 args_info->gtpversion_orig = NULL;
200 args_info->apn_arg = gengetopt_strdup("internet");
201 args_info->apn_orig = NULL;
202 args_info->selmode_arg = 0x01;
203 args_info->selmode_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800204 args_info->rattype_arg = 1;
Harald Weltebed35df2011-11-02 13:06:18 +0100205 args_info->rattype_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800206 args_info->userloc_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100207 args_info->userloc_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800208 args_info->rai_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100209 args_info->rai_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800210 args_info->mstz_arg = gengetopt_strdup("0");
Harald Weltebed35df2011-11-02 13:06:18 +0100211 args_info->mstz_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800212 args_info->imeisv_arg = gengetopt_strdup("2143658709214365");
Harald Weltebed35df2011-11-02 13:06:18 +0100213 args_info->imeisv_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800214 args_info->norecovery_flag = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100215 args_info->imsi_arg = gengetopt_strdup("240010123456789");
216 args_info->imsi_orig = NULL;
217 args_info->nsapi_arg = 0;
218 args_info->nsapi_orig = NULL;
219 args_info->msisdn_arg = gengetopt_strdup("46702123456");
220 args_info->msisdn_orig = NULL;
221 args_info->qos_arg = 0x000b921f;
222 args_info->qos_orig = NULL;
223 args_info->qose1_arg = 0x9396404074f9ffff;
224 args_info->qose1_orig = NULL;
225 args_info->qose2_arg = 0x11;
226 args_info->qose2_orig = NULL;
227 args_info->qose3_arg = 0x0101;
228 args_info->qose3_orig = NULL;
229 args_info->qose4_arg = 0x4040;
230 args_info->qose4_orig = NULL;
231 args_info->charging_arg = 0x0800;
232 args_info->charging_orig = NULL;
233 args_info->uid_arg = gengetopt_strdup("mig");
234 args_info->uid_orig = NULL;
235 args_info->pwd_arg = gengetopt_strdup("hemmelig");
236 args_info->pwd_orig = NULL;
237 args_info->createif_flag = 0;
238 args_info->net_arg = NULL;
239 args_info->net_orig = NULL;
240 args_info->defaultroute_flag = 0;
241 args_info->ipup_arg = NULL;
242 args_info->ipup_orig = NULL;
243 args_info->ipdown_arg = NULL;
244 args_info->ipdown_orig = NULL;
Harald Welte73abc382017-10-10 08:50:11 +0800245 args_info->tun_device_arg = NULL;
246 args_info->tun_device_orig = NULL;
Harald Weltebed35df2011-11-02 13:06:18 +0100247 args_info->pinghost_arg = NULL;
248 args_info->pinghost_orig = NULL;
249 args_info->pingrate_arg = 1;
250 args_info->pingrate_orig = NULL;
251 args_info->pingsize_arg = 56;
252 args_info->pingsize_orig = NULL;
253 args_info->pingcount_arg = 0;
254 args_info->pingcount_orig = NULL;
255 args_info->pingquiet_flag = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800256 args_info->no_tx_gpdu_seq_flag = 0;
Harald Welte840a8e92017-09-24 18:12:40 +0800257 args_info->pdp_type_arg = gengetopt_strdup("v4");
258 args_info->pdp_type_orig = NULL;
Harald Weltebed35df2011-11-02 13:06:18 +0100259
Harald Welte1b3e5772010-05-04 11:13:56 +0200260}
261
262static
263void init_args_info(struct gengetopt_args_info *args_info)
264{
Harald Weltef6214982017-09-24 10:23:24 +0800265
Harald Weltebed35df2011-11-02 13:06:18 +0100266 args_info->help_help = gengetopt_args_info_help[0];
267 args_info->version_help = gengetopt_args_info_help[1];
268 args_info->debug_help = gengetopt_args_info_help[2];
269 args_info->conf_help = gengetopt_args_info_help[3];
270 args_info->pidfile_help = gengetopt_args_info_help[4];
271 args_info->statedir_help = gengetopt_args_info_help[5];
272 args_info->dns_help = gengetopt_args_info_help[6];
273 args_info->listen_help = gengetopt_args_info_help[7];
274 args_info->remote_help = gengetopt_args_info_help[8];
275 args_info->contexts_help = gengetopt_args_info_help[9];
276 args_info->timelimit_help = gengetopt_args_info_help[10];
277 args_info->gtpversion_help = gengetopt_args_info_help[11];
278 args_info->apn_help = gengetopt_args_info_help[12];
279 args_info->selmode_help = gengetopt_args_info_help[13];
Harald Weltef6214982017-09-24 10:23:24 +0800280 args_info->rattype_help = gengetopt_args_info_help[14];
281 args_info->userloc_help = gengetopt_args_info_help[15];
282 args_info->rai_help = gengetopt_args_info_help[16];
283 args_info->mstz_help = gengetopt_args_info_help[17];
284 args_info->imeisv_help = gengetopt_args_info_help[18];
285 args_info->norecovery_help = gengetopt_args_info_help[19];
286 args_info->imsi_help = gengetopt_args_info_help[20];
287 args_info->nsapi_help = gengetopt_args_info_help[21];
288 args_info->msisdn_help = gengetopt_args_info_help[22];
289 args_info->qos_help = gengetopt_args_info_help[23];
290 args_info->qose1_help = gengetopt_args_info_help[24];
291 args_info->qose2_help = gengetopt_args_info_help[25];
292 args_info->qose3_help = gengetopt_args_info_help[26];
293 args_info->qose4_help = gengetopt_args_info_help[27];
294 args_info->charging_help = gengetopt_args_info_help[28];
295 args_info->uid_help = gengetopt_args_info_help[29];
296 args_info->pwd_help = gengetopt_args_info_help[30];
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +0200297 args_info->createif_help = gengetopt_args_info_help[32];
298 args_info->net_help = gengetopt_args_info_help[33];
299 args_info->defaultroute_help = gengetopt_args_info_help[34];
300 args_info->ipup_help = gengetopt_args_info_help[35];
301 args_info->ipdown_help = gengetopt_args_info_help[36];
302 args_info->tun_device_help = gengetopt_args_info_help[37];
303 args_info->pinghost_help = gengetopt_args_info_help[39];
304 args_info->pingrate_help = gengetopt_args_info_help[40];
305 args_info->pingsize_help = gengetopt_args_info_help[41];
306 args_info->pingcount_help = gengetopt_args_info_help[42];
307 args_info->pingquiet_help = gengetopt_args_info_help[43];
308 args_info->no_tx_gpdu_seq_help = gengetopt_args_info_help[44];
309 args_info->pdp_type_help = gengetopt_args_info_help[45];
Harald Weltebed35df2011-11-02 13:06:18 +0100310
Harald Welte1b3e5772010-05-04 11:13:56 +0200311}
312
Harald Weltebed35df2011-11-02 13:06:18 +0100313void cmdline_parser_print_version(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200314{
Harald Weltef6214982017-09-24 10:23:24 +0800315 printf("%s %s\n",
316 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ?
317 CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
318 CMDLINE_PARSER_VERSION);
319
320 if (strlen(gengetopt_args_info_versiontext) > 0)
321 printf("\n%s\n", gengetopt_args_info_versiontext);
Harald Welte1b3e5772010-05-04 11:13:56 +0200322}
323
Harald Weltef6214982017-09-24 10:23:24 +0800324static void print_help_common(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200325{
Harald Weltebed35df2011-11-02 13:06:18 +0100326 cmdline_parser_print_version();
Harald Welte1b3e5772010-05-04 11:13:56 +0200327
Harald Weltebed35df2011-11-02 13:06:18 +0100328 if (strlen(gengetopt_args_info_purpose) > 0)
329 printf("\n%s\n", gengetopt_args_info_purpose);
Harald Welte1b3e5772010-05-04 11:13:56 +0200330
Harald Weltef6214982017-09-24 10:23:24 +0800331 if (strlen(gengetopt_args_info_usage) > 0)
332 printf("\n%s\n", gengetopt_args_info_usage);
333
334 printf("\n");
335
336 if (strlen(gengetopt_args_info_description) > 0)
337 printf("%s\n\n", gengetopt_args_info_description);
338}
339
340void cmdline_parser_print_help(void)
341{
342 int i = 0;
343 print_help_common();
Harald Weltebed35df2011-11-02 13:06:18 +0100344 while (gengetopt_args_info_help[i])
345 printf("%s\n", gengetopt_args_info_help[i++]);
Harald Welte1b3e5772010-05-04 11:13:56 +0200346}
347
Harald Weltebed35df2011-11-02 13:06:18 +0100348void cmdline_parser_init(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200349{
Harald Weltebed35df2011-11-02 13:06:18 +0100350 clear_given(args_info);
351 clear_args(args_info);
352 init_args_info(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200353}
354
Harald Weltef6214982017-09-24 10:23:24 +0800355void cmdline_parser_params_init(struct cmdline_parser_params *params)
356{
357 if (params) {
358 params->override = 0;
359 params->initialize = 1;
360 params->check_required = 1;
361 params->check_ambiguity = 0;
362 params->print_errors = 1;
363 }
364}
365
366struct cmdline_parser_params *cmdline_parser_params_create(void)
367{
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +0200368 struct cmdline_parser_params *params = (struct cmdline_parser_params *)
Harald Weltef6214982017-09-24 10:23:24 +0800369 malloc(sizeof(struct cmdline_parser_params));
370 cmdline_parser_params_init(params);
371 return params;
372}
373
374static void free_string_field(char **s)
375{
376 if (*s) {
377 free(*s);
378 *s = 0;
379 }
380}
381
Harald Weltebed35df2011-11-02 13:06:18 +0100382static void cmdline_parser_release(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200383{
Harald Weltebed35df2011-11-02 13:06:18 +0100384
Harald Weltef6214982017-09-24 10:23:24 +0800385 free_string_field(&(args_info->conf_arg));
386 free_string_field(&(args_info->conf_orig));
387 free_string_field(&(args_info->pidfile_arg));
388 free_string_field(&(args_info->pidfile_orig));
389 free_string_field(&(args_info->statedir_arg));
390 free_string_field(&(args_info->statedir_orig));
391 free_string_field(&(args_info->dns_arg));
392 free_string_field(&(args_info->dns_orig));
393 free_string_field(&(args_info->listen_arg));
394 free_string_field(&(args_info->listen_orig));
395 free_string_field(&(args_info->remote_arg));
396 free_string_field(&(args_info->remote_orig));
397 free_string_field(&(args_info->contexts_orig));
398 free_string_field(&(args_info->timelimit_orig));
399 free_string_field(&(args_info->gtpversion_orig));
400 free_string_field(&(args_info->apn_arg));
401 free_string_field(&(args_info->apn_orig));
402 free_string_field(&(args_info->selmode_orig));
403 free_string_field(&(args_info->rattype_orig));
404 free_string_field(&(args_info->userloc_arg));
405 free_string_field(&(args_info->userloc_orig));
406 free_string_field(&(args_info->rai_arg));
407 free_string_field(&(args_info->rai_orig));
408 free_string_field(&(args_info->mstz_arg));
409 free_string_field(&(args_info->mstz_orig));
410 free_string_field(&(args_info->imeisv_arg));
411 free_string_field(&(args_info->imeisv_orig));
412 free_string_field(&(args_info->imsi_arg));
413 free_string_field(&(args_info->imsi_orig));
414 free_string_field(&(args_info->nsapi_orig));
415 free_string_field(&(args_info->msisdn_arg));
416 free_string_field(&(args_info->msisdn_orig));
417 free_string_field(&(args_info->qos_orig));
418 free_string_field(&(args_info->qose1_orig));
419 free_string_field(&(args_info->qose2_orig));
420 free_string_field(&(args_info->qose3_orig));
421 free_string_field(&(args_info->qose4_orig));
422 free_string_field(&(args_info->charging_orig));
423 free_string_field(&(args_info->uid_arg));
424 free_string_field(&(args_info->uid_orig));
425 free_string_field(&(args_info->pwd_arg));
426 free_string_field(&(args_info->pwd_orig));
427 free_string_field(&(args_info->net_arg));
428 free_string_field(&(args_info->net_orig));
429 free_string_field(&(args_info->ipup_arg));
430 free_string_field(&(args_info->ipup_orig));
431 free_string_field(&(args_info->ipdown_arg));
432 free_string_field(&(args_info->ipdown_orig));
Harald Welte73abc382017-10-10 08:50:11 +0800433 free_string_field(&(args_info->tun_device_arg));
434 free_string_field(&(args_info->tun_device_orig));
Harald Weltef6214982017-09-24 10:23:24 +0800435 free_string_field(&(args_info->pinghost_arg));
436 free_string_field(&(args_info->pinghost_orig));
437 free_string_field(&(args_info->pingrate_orig));
438 free_string_field(&(args_info->pingsize_orig));
439 free_string_field(&(args_info->pingcount_orig));
Harald Welte840a8e92017-09-24 18:12:40 +0800440 free_string_field(&(args_info->pdp_type_arg));
441 free_string_field(&(args_info->pdp_type_orig));
Harald Weltebed35df2011-11-02 13:06:18 +0100442
443 clear_given(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200444}
445
Harald Weltef6214982017-09-24 10:23:24 +0800446static void
447write_into_file(FILE * outfile, const char *opt, const char *arg,
448 const char *values[])
449{
450 FIX_UNUSED(values);
451 if (arg) {
452 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
453 } else {
454 fprintf(outfile, "%s\n", opt);
455 }
456}
457
458int cmdline_parser_dump(FILE * outfile, struct gengetopt_args_info *args_info)
459{
460 int i = 0;
461
462 if (!outfile) {
463 fprintf(stderr, "%s: cannot dump options to stream\n",
464 CMDLINE_PARSER_PACKAGE);
465 return EXIT_FAILURE;
466 }
467
468 if (args_info->help_given)
469 write_into_file(outfile, "help", 0, 0);
470 if (args_info->version_given)
471 write_into_file(outfile, "version", 0, 0);
472 if (args_info->debug_given)
473 write_into_file(outfile, "debug", 0, 0);
474 if (args_info->conf_given)
475 write_into_file(outfile, "conf", args_info->conf_orig, 0);
476 if (args_info->pidfile_given)
477 write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0);
478 if (args_info->statedir_given)
479 write_into_file(outfile, "statedir", args_info->statedir_orig,
480 0);
481 if (args_info->dns_given)
482 write_into_file(outfile, "dns", args_info->dns_orig, 0);
483 if (args_info->listen_given)
484 write_into_file(outfile, "listen", args_info->listen_orig, 0);
485 if (args_info->remote_given)
486 write_into_file(outfile, "remote", args_info->remote_orig, 0);
487 if (args_info->contexts_given)
488 write_into_file(outfile, "contexts", args_info->contexts_orig,
489 0);
490 if (args_info->timelimit_given)
491 write_into_file(outfile, "timelimit", args_info->timelimit_orig,
492 0);
493 if (args_info->gtpversion_given)
494 write_into_file(outfile, "gtpversion",
495 args_info->gtpversion_orig, 0);
496 if (args_info->apn_given)
497 write_into_file(outfile, "apn", args_info->apn_orig, 0);
498 if (args_info->selmode_given)
499 write_into_file(outfile, "selmode", args_info->selmode_orig, 0);
500 if (args_info->rattype_given)
501 write_into_file(outfile, "rattype", args_info->rattype_orig, 0);
502 if (args_info->userloc_given)
503 write_into_file(outfile, "userloc", args_info->userloc_orig, 0);
504 if (args_info->rai_given)
505 write_into_file(outfile, "rai", args_info->rai_orig, 0);
506 if (args_info->mstz_given)
507 write_into_file(outfile, "mstz", args_info->mstz_orig, 0);
508 if (args_info->imeisv_given)
509 write_into_file(outfile, "imeisv", args_info->imeisv_orig, 0);
510 if (args_info->norecovery_given)
511 write_into_file(outfile, "norecovery", 0, 0);
512 if (args_info->imsi_given)
513 write_into_file(outfile, "imsi", args_info->imsi_orig, 0);
514 if (args_info->nsapi_given)
515 write_into_file(outfile, "nsapi", args_info->nsapi_orig, 0);
516 if (args_info->msisdn_given)
517 write_into_file(outfile, "msisdn", args_info->msisdn_orig, 0);
518 if (args_info->qos_given)
519 write_into_file(outfile, "qos", args_info->qos_orig, 0);
520 if (args_info->qose1_given)
521 write_into_file(outfile, "qose1", args_info->qose1_orig, 0);
522 if (args_info->qose2_given)
523 write_into_file(outfile, "qose2", args_info->qose2_orig, 0);
524 if (args_info->qose3_given)
525 write_into_file(outfile, "qose3", args_info->qose3_orig, 0);
526 if (args_info->qose4_given)
527 write_into_file(outfile, "qose4", args_info->qose4_orig, 0);
528 if (args_info->charging_given)
529 write_into_file(outfile, "charging", args_info->charging_orig,
530 0);
531 if (args_info->uid_given)
532 write_into_file(outfile, "uid", args_info->uid_orig, 0);
533 if (args_info->pwd_given)
534 write_into_file(outfile, "pwd", args_info->pwd_orig, 0);
535 if (args_info->createif_given)
536 write_into_file(outfile, "createif", 0, 0);
537 if (args_info->net_given)
538 write_into_file(outfile, "net", args_info->net_orig, 0);
539 if (args_info->defaultroute_given)
540 write_into_file(outfile, "defaultroute", 0, 0);
541 if (args_info->ipup_given)
542 write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
543 if (args_info->ipdown_given)
544 write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0);
Harald Welte73abc382017-10-10 08:50:11 +0800545 if (args_info->tun_device_given)
546 write_into_file(outfile, "tun-device",
547 args_info->tun_device_orig, 0);
Harald Weltef6214982017-09-24 10:23:24 +0800548 if (args_info->pinghost_given)
549 write_into_file(outfile, "pinghost", args_info->pinghost_orig,
550 0);
551 if (args_info->pingrate_given)
552 write_into_file(outfile, "pingrate", args_info->pingrate_orig,
553 0);
554 if (args_info->pingsize_given)
555 write_into_file(outfile, "pingsize", args_info->pingsize_orig,
556 0);
557 if (args_info->pingcount_given)
558 write_into_file(outfile, "pingcount", args_info->pingcount_orig,
559 0);
560 if (args_info->pingquiet_given)
561 write_into_file(outfile, "pingquiet", 0, 0);
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800562 if (args_info->no_tx_gpdu_seq_given)
563 write_into_file(outfile, "no-tx-gpdu-seq", 0, 0);
Harald Welte840a8e92017-09-24 18:12:40 +0800564 if (args_info->pdp_type_given)
565 write_into_file(outfile, "pdp-type", args_info->pdp_type_orig,
566 0);
Harald Weltef6214982017-09-24 10:23:24 +0800567
568 i = EXIT_SUCCESS;
569 return i;
570}
571
Harald Welte1b3e5772010-05-04 11:13:56 +0200572int
Harald Weltebed35df2011-11-02 13:06:18 +0100573cmdline_parser_file_save(const char *filename,
574 struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200575{
Harald Weltebed35df2011-11-02 13:06:18 +0100576 FILE *outfile;
577 int i = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200578
Harald Weltebed35df2011-11-02 13:06:18 +0100579 outfile = fopen(filename, "w");
Harald Welte1b3e5772010-05-04 11:13:56 +0200580
Harald Weltebed35df2011-11-02 13:06:18 +0100581 if (!outfile) {
582 fprintf(stderr, "%s: cannot open file for writing: %s\n",
583 CMDLINE_PARSER_PACKAGE, filename);
584 return EXIT_FAILURE;
585 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200586
Harald Weltef6214982017-09-24 10:23:24 +0800587 i = cmdline_parser_dump(outfile, args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100588 fclose(outfile);
589
Harald Weltebed35df2011-11-02 13:06:18 +0100590 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200591}
592
Harald Weltebed35df2011-11-02 13:06:18 +0100593void cmdline_parser_free(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200594{
Harald Weltebed35df2011-11-02 13:06:18 +0100595 cmdline_parser_release(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200596}
597
Harald Weltef6214982017-09-24 10:23:24 +0800598/** @brief replacement of strdup, which is not standard */
Harald Weltebed35df2011-11-02 13:06:18 +0100599char *gengetopt_strdup(const char *s)
Harald Welte1b3e5772010-05-04 11:13:56 +0200600{
Harald Weltef6214982017-09-24 10:23:24 +0800601 char *result = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100602 if (!s)
603 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200604
Harald Weltebed35df2011-11-02 13:06:18 +0100605 result = (char *)malloc(strlen(s) + 1);
606 if (result == (char *)0)
607 return (char *)0;
608 strcpy(result, s);
609 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200610}
611
Harald Weltef6214982017-09-24 10:23:24 +0800612int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200613{
Harald Weltebed35df2011-11-02 13:06:18 +0100614 return cmdline_parser2(argc, argv, args_info, 0, 1, 1);
Harald Welte1b3e5772010-05-04 11:13:56 +0200615}
616
617int
Harald Weltef6214982017-09-24 10:23:24 +0800618cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info,
619 struct cmdline_parser_params *params)
Harald Welte1b3e5772010-05-04 11:13:56 +0200620{
Harald Weltebed35df2011-11-02 13:06:18 +0100621 int result;
Harald Weltef6214982017-09-24 10:23:24 +0800622 result = cmdline_parser_internal(argc, argv, args_info, params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200623
Harald Weltef6214982017-09-24 10:23:24 +0800624 if (result == EXIT_FAILURE) {
625 cmdline_parser_free(args_info);
626 exit(EXIT_FAILURE);
627 }
628
629 return result;
630}
631
632int
633cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info,
634 int override, int initialize, int check_required)
635{
636 int result;
637 struct cmdline_parser_params params;
638
639 params.override = override;
640 params.initialize = initialize;
641 params.check_required = check_required;
642 params.check_ambiguity = 0;
643 params.print_errors = 1;
644
645 result = cmdline_parser_internal(argc, argv, args_info, &params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200646
Harald Weltebed35df2011-11-02 13:06:18 +0100647 if (result == EXIT_FAILURE) {
648 cmdline_parser_free(args_info);
649 exit(EXIT_FAILURE);
650 }
651
652 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200653}
654
655int
Harald Weltebed35df2011-11-02 13:06:18 +0100656cmdline_parser_required(struct gengetopt_args_info *args_info,
657 const char *prog_name)
Harald Welte1b3e5772010-05-04 11:13:56 +0200658{
Harald Welte73abc382017-10-10 08:50:11 +0800659 int result = EXIT_SUCCESS;
660
661 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
662 result = EXIT_FAILURE;
663
664 if (result == EXIT_FAILURE) {
665 cmdline_parser_free(args_info);
666 exit(EXIT_FAILURE);
667 }
668
669 return result;
670}
671
672int
673cmdline_parser_required2(struct gengetopt_args_info *args_info,
674 const char *prog_name, const char *additional_error)
675{
676 int error_occurred = 0;
677 FIX_UNUSED(additional_error);
678
679 /* checks for required options */
680
681 /* checks for dependences among options */
Harald Welteea0c26a2017-10-10 08:54:04 +0800682 if (args_info->net_given && !args_info->createif_given) {
683 fprintf(stderr,
684 "%s: '--net' ('-n') option depends on option 'createif'%s\n",
685 prog_name, (additional_error ? additional_error : ""));
686 error_occurred = 1;
687 }
688 if (args_info->defaultroute_given && !args_info->createif_given) {
689 fprintf(stderr,
690 "%s: '--defaultroute' option depends on option 'createif'%s\n",
691 prog_name, (additional_error ? additional_error : ""));
692 error_occurred = 1;
693 }
694 if (args_info->ipup_given && !args_info->createif_given) {
695 fprintf(stderr,
696 "%s: '--ipup' option depends on option 'createif'%s\n",
697 prog_name, (additional_error ? additional_error : ""));
698 error_occurred = 1;
699 }
700 if (args_info->ipdown_given && !args_info->createif_given) {
701 fprintf(stderr,
702 "%s: '--ipdown' option depends on option 'createif'%s\n",
703 prog_name, (additional_error ? additional_error : ""));
704 error_occurred = 1;
705 }
Harald Welte73abc382017-10-10 08:50:11 +0800706 if (args_info->tun_device_given && !args_info->createif_given) {
707 fprintf(stderr,
708 "%s: '--tun-device' option depends on option 'createif'%s\n",
709 prog_name, (additional_error ? additional_error : ""));
710 error_occurred = 1;
711 }
Harald Welteea0c26a2017-10-10 08:54:04 +0800712 if (args_info->pingrate_given && !args_info->pinghost_given) {
713 fprintf(stderr,
714 "%s: '--pingrate' option depends on option 'pinghost'%s\n",
715 prog_name, (additional_error ? additional_error : ""));
716 error_occurred = 1;
717 }
718 if (args_info->pingsize_given && !args_info->pinghost_given) {
719 fprintf(stderr,
720 "%s: '--pingsize' option depends on option 'pinghost'%s\n",
721 prog_name, (additional_error ? additional_error : ""));
722 error_occurred = 1;
723 }
724 if (args_info->pingcount_given && !args_info->pinghost_given) {
725 fprintf(stderr,
726 "%s: '--pingcount' option depends on option 'pinghost'%s\n",
727 prog_name, (additional_error ? additional_error : ""));
728 error_occurred = 1;
729 }
730 if (args_info->pingquiet_given && !args_info->pinghost_given) {
731 fprintf(stderr,
732 "%s: '--pingquiet' option depends on option 'pinghost'%s\n",
733 prog_name, (additional_error ? additional_error : ""));
734 error_occurred = 1;
735 }
Harald Welte73abc382017-10-10 08:50:11 +0800736
737 return error_occurred;
Harald Welte1b3e5772010-05-04 11:13:56 +0200738}
739
Harald Weltef6214982017-09-24 10:23:24 +0800740static char *package_name = 0;
741
742/**
743 * @brief updates an option
744 * @param field the generic pointer to the field to update
745 * @param orig_field the pointer to the orig field
746 * @param field_given the pointer to the number of occurrence of this option
747 * @param prev_given the pointer to the number of occurrence already seen
748 * @param value the argument for this option (if null no arg was specified)
749 * @param possible_values the possible values for this option (if specified)
750 * @param default_value the default value (in case the option only accepts fixed values)
751 * @param arg_type the type of this option
752 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
753 * @param override @see cmdline_parser_params.override
754 * @param no_free whether to free a possible previous value
755 * @param multiple_option whether this is a multiple option
756 * @param long_opt the corresponding long option
757 * @param short_opt the corresponding short option (or '-' if none)
758 * @param additional_error possible further error specification
759 */
760static
761int update_arg(void *field, char **orig_field,
762 unsigned int *field_given, unsigned int *prev_given,
763 char *value, const char *possible_values[],
764 const char *default_value,
765 cmdline_parser_arg_type arg_type,
766 int check_ambiguity, int override,
767 int no_free, int multiple_option,
768 const char *long_opt, char short_opt,
769 const char *additional_error)
770{
771 char *stop_char = 0;
772 const char *val = value;
773 int found;
774 char **string_field;
775 FIX_UNUSED(field);
776
777 stop_char = 0;
778 found = 0;
779
780 if (!multiple_option && prev_given
781 && (*prev_given || (check_ambiguity && *field_given))) {
782 if (short_opt != '-')
783 fprintf(stderr,
784 "%s: `--%s' (`-%c') option given more than once%s\n",
785 package_name, long_opt, short_opt,
786 (additional_error ? additional_error : ""));
787 else
788 fprintf(stderr,
789 "%s: `--%s' option given more than once%s\n",
790 package_name, long_opt,
791 (additional_error ? additional_error : ""));
792 return 1; /* failure */
793 }
794
795 FIX_UNUSED(default_value);
796
797 if (field_given && *field_given && !override)
798 return 0;
799 if (prev_given)
800 (*prev_given)++;
801 if (field_given)
802 (*field_given)++;
803 if (possible_values)
804 val = possible_values[found];
805
806 switch (arg_type) {
807 case ARG_FLAG:
808 *((int *)field) = !*((int *)field);
809 break;
810 case ARG_INT:
811 if (val)
812 *((int *)field) = strtol(val, &stop_char, 0);
813 break;
814 case ARG_STRING:
815 if (val) {
816 string_field = (char **)field;
817 if (!no_free && *string_field)
818 free(*string_field); /* free previous string */
819 *string_field = gengetopt_strdup(val);
820 }
821 break;
822 default:
823 break;
824 };
825
826 /* check numeric conversion */
827 switch (arg_type) {
828 case ARG_INT:
829 if (val && !(stop_char && *stop_char == '\0')) {
830 fprintf(stderr, "%s: invalid numeric value: %s\n",
831 package_name, val);
832 return 1; /* failure */
833 }
834 break;
835 default:
836 ;
837 };
838
839 /* store the original value */
840 switch (arg_type) {
841 case ARG_NO:
842 case ARG_FLAG:
843 break;
844 default:
845 if (value && orig_field) {
846 if (no_free) {
847 *orig_field = value;
848 } else {
849 if (*orig_field)
850 free(*orig_field); /* free previous string */
851 *orig_field = gengetopt_strdup(value);
852 }
853 }
854 };
855
856 return 0; /* OK */
857}
858
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +0200859static int check_modes(int given1[], const char *options1[],
860 int given2[], const char *options2[])
861{
862 int i = 0, j = 0, errors = 0;
863
864 while (given1[i] >= 0) {
865 if (given1[i]) {
866 while (given2[j] >= 0) {
867 if (given2[j]) {
868 ++errors;
869 fprintf(stderr,
870 "%s: option %s conflicts with option %s\n",
871 package_name, options1[i],
872 options2[j]);
873 }
874 ++j;
875 }
876 }
877 ++i;
878 }
879
880 return errors;
881}
882
Harald Welte1b3e5772010-05-04 11:13:56 +0200883int
Harald Weltef6214982017-09-24 10:23:24 +0800884cmdline_parser_internal(int argc, char **argv,
885 struct gengetopt_args_info *args_info,
886 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +0100887 const char *additional_error)
Harald Welte1b3e5772010-05-04 11:13:56 +0200888{
Harald Weltebed35df2011-11-02 13:06:18 +0100889 int c; /* Character of the parsed option. */
Harald Welte1b3e5772010-05-04 11:13:56 +0200890
Harald Weltef6214982017-09-24 10:23:24 +0800891 int error_occurred = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100892 struct gengetopt_args_info local_args_info;
Harald Welte1b3e5772010-05-04 11:13:56 +0200893
Harald Weltef6214982017-09-24 10:23:24 +0800894 int override;
895 int initialize;
896 int check_required;
897 int check_ambiguity;
898
899 package_name = argv[0];
900
901 override = params->override;
902 initialize = params->initialize;
903 check_required = params->check_required;
904 check_ambiguity = params->check_ambiguity;
905
Harald Weltebed35df2011-11-02 13:06:18 +0100906 if (initialize)
907 cmdline_parser_init(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200908
Harald Weltebed35df2011-11-02 13:06:18 +0100909 cmdline_parser_init(&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000910
Harald Weltebed35df2011-11-02 13:06:18 +0100911 optarg = 0;
912 optind = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800913 opterr = params->print_errors;
Harald Weltebed35df2011-11-02 13:06:18 +0100914 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000915
Harald Weltebed35df2011-11-02 13:06:18 +0100916 while (1) {
917 int option_index = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200918
Harald Weltebed35df2011-11-02 13:06:18 +0100919 static struct option long_options[] = {
920 {"help", 0, NULL, 'h'},
921 {"version", 0, NULL, 'V'},
922 {"debug", 0, NULL, 'd'},
923 {"conf", 1, NULL, 'c'},
924 {"pidfile", 1, NULL, 0},
925 {"statedir", 1, NULL, 0},
926 {"dns", 1, NULL, 0},
927 {"listen", 1, NULL, 'l'},
928 {"remote", 1, NULL, 'r'},
929 {"contexts", 1, NULL, 0},
930 {"timelimit", 1, NULL, 0},
931 {"gtpversion", 1, NULL, 0},
932 {"apn", 1, NULL, 'a'},
933 {"selmode", 1, NULL, 0},
934 {"rattype", 1, NULL, 0},
935 {"userloc", 1, NULL, 0},
936 {"rai", 1, NULL, 0},
937 {"mstz", 1, NULL, 0},
938 {"imeisv", 1, NULL, 0},
Harald Weltef6214982017-09-24 10:23:24 +0800939 {"norecovery", 0, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100940 {"imsi", 1, NULL, 'i'},
941 {"nsapi", 1, NULL, 0},
942 {"msisdn", 1, NULL, 'm'},
943 {"qos", 1, NULL, 'q'},
944 {"qose1", 1, NULL, 0},
945 {"qose2", 1, NULL, 0},
946 {"qose3", 1, NULL, 0},
947 {"qose4", 1, NULL, 0},
948 {"charging", 1, NULL, 0},
949 {"uid", 1, NULL, 'u'},
950 {"pwd", 1, NULL, 'p'},
951 {"createif", 0, NULL, 0},
952 {"net", 1, NULL, 'n'},
953 {"defaultroute", 0, NULL, 0},
954 {"ipup", 1, NULL, 0},
955 {"ipdown", 1, NULL, 0},
Harald Welte73abc382017-10-10 08:50:11 +0800956 {"tun-device", 1, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100957 {"pinghost", 1, NULL, 0},
958 {"pingrate", 1, NULL, 0},
959 {"pingsize", 1, NULL, 0},
960 {"pingcount", 1, NULL, 0},
961 {"pingquiet", 0, NULL, 0},
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800962 {"no-tx-gpdu-seq", 0, NULL, 0},
Harald Welte840a8e92017-09-24 18:12:40 +0800963 {"pdp-type", 1, NULL, 't'},
Harald Weltef6214982017-09-24 10:23:24 +0800964 {0, 0, 0, 0}
Harald Weltebed35df2011-11-02 13:06:18 +0100965 };
jjako52c24142002-12-16 13:33:51 +0000966
Harald Welte840a8e92017-09-24 18:12:40 +0800967 c = getopt_long(argc, argv, "hVdc:l:r:a:i:m:q:u:p:n:t:",
Harald Weltebed35df2011-11-02 13:06:18 +0100968 long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000969
Harald Weltebed35df2011-11-02 13:06:18 +0100970 if (c == -1)
971 break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000972
Harald Weltebed35df2011-11-02 13:06:18 +0100973 switch (c) {
974 case 'h': /* Print help and exit. */
975 cmdline_parser_print_help();
976 cmdline_parser_free(&local_args_info);
977 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000978
Harald Weltebed35df2011-11-02 13:06:18 +0100979 case 'V': /* Print version and exit. */
980 cmdline_parser_print_version();
981 cmdline_parser_free(&local_args_info);
982 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000983
Harald Weltebed35df2011-11-02 13:06:18 +0100984 case 'd': /* Run in debug mode. */
jjako52c24142002-12-16 13:33:51 +0000985
Harald Weltef6214982017-09-24 10:23:24 +0800986 if (update_arg
987 ((void *)&(args_info->debug_flag), 0,
988 &(args_info->debug_given),
989 &(local_args_info.debug_given), optarg, 0, 0,
990 ARG_FLAG, check_ambiguity, override, 1, 0, "debug",
991 'd', additional_error))
992 goto failure;
993
994 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100995 case 'c': /* Read configuration file. */
jjako52c24142002-12-16 13:33:51 +0000996
Harald Weltef6214982017-09-24 10:23:24 +0800997 if (update_arg((void *)&(args_info->conf_arg),
998 &(args_info->conf_orig),
999 &(args_info->conf_given),
1000 &(local_args_info.conf_given), optarg, 0,
1001 0, ARG_STRING, check_ambiguity, override,
1002 0, 0, "conf", 'c', additional_error))
1003 goto failure;
1004
1005 break;
Pau Espin Pedrol5fdda132018-02-12 18:03:09 +01001006 case 'l': /* Local host. */
jjako52c24142002-12-16 13:33:51 +00001007
Harald Weltef6214982017-09-24 10:23:24 +08001008 if (update_arg((void *)&(args_info->listen_arg),
1009 &(args_info->listen_orig),
1010 &(args_info->listen_given),
1011 &(local_args_info.listen_given), optarg,
1012 0, 0, ARG_STRING, check_ambiguity,
1013 override, 0, 0, "listen", 'l',
1014 additional_error))
1015 goto failure;
1016
1017 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001018 case 'r': /* Remote host. */
jjako52c24142002-12-16 13:33:51 +00001019
Harald Weltef6214982017-09-24 10:23:24 +08001020 if (update_arg((void *)&(args_info->remote_arg),
1021 &(args_info->remote_orig),
1022 &(args_info->remote_given),
1023 &(local_args_info.remote_given), optarg,
1024 0, 0, ARG_STRING, check_ambiguity,
1025 override, 0, 0, "remote", 'r',
1026 additional_error))
1027 goto failure;
1028
1029 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001030 case 'a': /* Access point name. */
jjako52c24142002-12-16 13:33:51 +00001031
Harald Weltef6214982017-09-24 10:23:24 +08001032 if (update_arg((void *)&(args_info->apn_arg),
1033 &(args_info->apn_orig),
1034 &(args_info->apn_given),
1035 &(local_args_info.apn_given), optarg, 0,
1036 "internet", ARG_STRING, check_ambiguity,
1037 override, 0, 0, "apn", 'a',
1038 additional_error))
1039 goto failure;
1040
1041 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001042 case 'i': /* IMSI. */
jjako52c24142002-12-16 13:33:51 +00001043
Harald Weltef6214982017-09-24 10:23:24 +08001044 if (update_arg((void *)&(args_info->imsi_arg),
1045 &(args_info->imsi_orig),
1046 &(args_info->imsi_given),
1047 &(local_args_info.imsi_given), optarg, 0,
1048 "240010123456789", ARG_STRING,
1049 check_ambiguity, override, 0, 0, "imsi",
1050 'i', additional_error))
1051 goto failure;
1052
1053 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001054 case 'm': /* Mobile Station ISDN number. */
jjako52c24142002-12-16 13:33:51 +00001055
Harald Weltef6214982017-09-24 10:23:24 +08001056 if (update_arg((void *)&(args_info->msisdn_arg),
1057 &(args_info->msisdn_orig),
1058 &(args_info->msisdn_given),
1059 &(local_args_info.msisdn_given), optarg,
1060 0, "46702123456", ARG_STRING,
1061 check_ambiguity, override, 0, 0,
1062 "msisdn", 'm', additional_error))
1063 goto failure;
1064
1065 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001066 case 'q': /* Requested quality of service. */
jjako52c24142002-12-16 13:33:51 +00001067
Harald Weltef6214982017-09-24 10:23:24 +08001068 if (update_arg((void *)&(args_info->qos_arg),
1069 &(args_info->qos_orig),
1070 &(args_info->qos_given),
1071 &(local_args_info.qos_given), optarg, 0,
1072 "0x000b921f", ARG_INT, check_ambiguity,
1073 override, 0, 0, "qos", 'q',
1074 additional_error))
1075 goto failure;
1076
1077 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001078 case 'u': /* Login user ID. */
jjako52c24142002-12-16 13:33:51 +00001079
Harald Weltef6214982017-09-24 10:23:24 +08001080 if (update_arg((void *)&(args_info->uid_arg),
1081 &(args_info->uid_orig),
1082 &(args_info->uid_given),
1083 &(local_args_info.uid_given), optarg, 0,
1084 "mig", ARG_STRING, check_ambiguity,
1085 override, 0, 0, "uid", 'u',
1086 additional_error))
1087 goto failure;
1088
1089 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001090 case 'p': /* Login password. */
jjako52c24142002-12-16 13:33:51 +00001091
Harald Weltef6214982017-09-24 10:23:24 +08001092 if (update_arg((void *)&(args_info->pwd_arg),
1093 &(args_info->pwd_orig),
1094 &(args_info->pwd_given),
1095 &(local_args_info.pwd_given), optarg, 0,
1096 "hemmelig", ARG_STRING, check_ambiguity,
1097 override, 0, 0, "pwd", 'p',
1098 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001099 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001100
1101 break;
1102 case 'n': /* Network address for local interface. */
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001103 args_info->createif_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001104
1105 if (update_arg((void *)&(args_info->net_arg),
1106 &(args_info->net_orig),
1107 &(args_info->net_given),
1108 &(local_args_info.net_given), optarg, 0,
1109 0, ARG_STRING, check_ambiguity, override,
1110 0, 0, "net", 'n', additional_error))
1111 goto failure;
1112
Harald Weltebed35df2011-11-02 13:06:18 +01001113 break;
Harald Welte840a8e92017-09-24 18:12:40 +08001114 case 't': /* PDP Type. */
1115
1116 if (update_arg((void *)&(args_info->pdp_type_arg),
1117 &(args_info->pdp_type_orig),
1118 &(args_info->pdp_type_given),
1119 &(local_args_info.pdp_type_given),
1120 optarg, 0, "v4", ARG_STRING,
1121 check_ambiguity, override, 0, 0,
1122 "pdp-type", 't', additional_error))
1123 goto failure;
1124
1125 break;
jjako193e8b12003-11-10 12:31:41 +00001126
Harald Weltebed35df2011-11-02 13:06:18 +01001127 case 0: /* Long option with no short option */
1128 /* Filename of process id file. */
1129 if (strcmp(long_options[option_index].name, "pidfile")
1130 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001131
1132 if (update_arg
1133 ((void *)&(args_info->pidfile_arg),
1134 &(args_info->pidfile_orig),
1135 &(args_info->pidfile_given),
1136 &(local_args_info.pidfile_given), optarg,
1137 0, "./sgsnemu.pid", ARG_STRING,
1138 check_ambiguity, override, 0, 0, "pidfile",
1139 '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001140 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001141
Harald Weltebed35df2011-11-02 13:06:18 +01001142 }
1143 /* Directory of nonvolatile data. */
1144 else if (strcmp
1145 (long_options[option_index].name,
1146 "statedir") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001147
1148 if (update_arg
1149 ((void *)&(args_info->statedir_arg),
1150 &(args_info->statedir_orig),
1151 &(args_info->statedir_given),
1152 &(local_args_info.statedir_given), optarg,
1153 0, "./", ARG_STRING, check_ambiguity,
1154 override, 0, 0, "statedir", '-',
1155 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001156 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001157
Harald Weltebed35df2011-11-02 13:06:18 +01001158 }
1159 /* DNS Server to use. */
1160 else if (strcmp(long_options[option_index].name, "dns")
1161 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001162
1163 if (update_arg((void *)&(args_info->dns_arg),
1164 &(args_info->dns_orig),
1165 &(args_info->dns_given),
1166 &(local_args_info.dns_given),
1167 optarg, 0, 0, ARG_STRING,
1168 check_ambiguity, override, 0, 0,
1169 "dns", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001170 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001171
Harald Weltebed35df2011-11-02 13:06:18 +01001172 }
1173 /* Number of contexts. */
1174 else if (strcmp
1175 (long_options[option_index].name,
1176 "contexts") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001177
1178 if (update_arg
1179 ((void *)&(args_info->contexts_arg),
1180 &(args_info->contexts_orig),
1181 &(args_info->contexts_given),
1182 &(local_args_info.contexts_given), optarg,
1183 0, "1", ARG_INT, check_ambiguity, override,
1184 0, 0, "contexts", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001185 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001186
Harald Weltebed35df2011-11-02 13:06:18 +01001187 }
1188 /* Exit after timelimit seconds. */
1189 else if (strcmp
1190 (long_options[option_index].name,
1191 "timelimit") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001192
1193 if (update_arg
1194 ((void *)&(args_info->timelimit_arg),
1195 &(args_info->timelimit_orig),
1196 &(args_info->timelimit_given),
1197 &(local_args_info.timelimit_given), optarg,
1198 0, "0", ARG_INT, check_ambiguity, override,
1199 0, 0, "timelimit", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001200 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001201
Harald Weltebed35df2011-11-02 13:06:18 +01001202 }
1203 /* GTP version to use. */
1204 else if (strcmp
1205 (long_options[option_index].name,
1206 "gtpversion") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001207
1208 if (update_arg
1209 ((void *)&(args_info->gtpversion_arg),
1210 &(args_info->gtpversion_orig),
1211 &(args_info->gtpversion_given),
1212 &(local_args_info.gtpversion_given),
1213 optarg, 0, "1", ARG_INT, check_ambiguity,
1214 override, 0, 0, "gtpversion", '-',
1215 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001216 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001217
Harald Weltebed35df2011-11-02 13:06:18 +01001218 }
1219 /* Selection mode. */
1220 else if (strcmp
1221 (long_options[option_index].name,
1222 "selmode") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001223
1224 if (update_arg
1225 ((void *)&(args_info->selmode_arg),
1226 &(args_info->selmode_orig),
1227 &(args_info->selmode_given),
1228 &(local_args_info.selmode_given), optarg,
1229 0, "0x01", ARG_INT, check_ambiguity,
1230 override, 0, 0, "selmode", '-',
1231 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001232 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001233
Harald Weltebed35df2011-11-02 13:06:18 +01001234 }
1235 /* Radio Access Technology Type. */
1236 else if (strcmp
1237 (long_options[option_index].name,
1238 "rattype") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001239
1240 if (update_arg
1241 ((void *)&(args_info->rattype_arg),
1242 &(args_info->rattype_orig),
1243 &(args_info->rattype_given),
1244 &(local_args_info.rattype_given), optarg,
1245 0, "1", ARG_INT, check_ambiguity, override,
1246 0, 0, "rattype", '-', additional_error))
1247 goto failure;
1248
Harald Weltebed35df2011-11-02 13:06:18 +01001249 }
1250 /* User Location Information. */
1251 else if (strcmp
1252 (long_options[option_index].name,
1253 "userloc") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001254
1255 if (update_arg
1256 ((void *)&(args_info->userloc_arg),
1257 &(args_info->userloc_orig),
1258 &(args_info->userloc_given),
1259 &(local_args_info.userloc_given), optarg,
1260 0, "02509946241207", ARG_STRING,
1261 check_ambiguity, override, 0, 0, "userloc",
1262 '-', additional_error))
1263 goto failure;
1264
Harald Weltebed35df2011-11-02 13:06:18 +01001265 }
1266 /* Routing Area Information. */
1267 else if (strcmp(long_options[option_index].name, "rai")
1268 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001269
1270 if (update_arg((void *)&(args_info->rai_arg),
1271 &(args_info->rai_orig),
1272 &(args_info->rai_given),
1273 &(local_args_info.rai_given),
1274 optarg, 0, "02509946241207",
1275 ARG_STRING, check_ambiguity,
1276 override, 0, 0, "rai", '-',
1277 additional_error))
1278 goto failure;
1279
Harald Weltebed35df2011-11-02 13:06:18 +01001280 }
Harald Weltef6214982017-09-24 10:23:24 +08001281 /* MS Time Zone. */
Harald Weltebed35df2011-11-02 13:06:18 +01001282 else if (strcmp(long_options[option_index].name, "mstz")
1283 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001284
1285 if (update_arg((void *)&(args_info->mstz_arg),
1286 &(args_info->mstz_orig),
1287 &(args_info->mstz_given),
1288 &(local_args_info.mstz_given),
1289 optarg, 0, "0", ARG_STRING,
1290 check_ambiguity, override, 0, 0,
1291 "mstz", '-', additional_error))
1292 goto failure;
1293
Harald Weltebed35df2011-11-02 13:06:18 +01001294 }
Harald Weltef6214982017-09-24 10:23:24 +08001295 /* IMEI(SV) International Mobile Equipment Identity (and Software Version). */
Harald Weltebed35df2011-11-02 13:06:18 +01001296 else if (strcmp
1297 (long_options[option_index].name,
1298 "imeisv") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001299
1300 if (update_arg((void *)&(args_info->imeisv_arg),
1301 &(args_info->imeisv_orig),
1302 &(args_info->imeisv_given),
1303 &(local_args_info.imeisv_given),
1304 optarg, 0, "2143658709214365",
1305 ARG_STRING, check_ambiguity,
1306 override, 0, 0, "imeisv", '-',
1307 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001308 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001309
Harald Weltebed35df2011-11-02 13:06:18 +01001310 }
1311 /* Do not send recovery. */
1312 else if (strcmp
1313 (long_options[option_index].name,
1314 "norecovery") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001315
1316 if (update_arg
1317 ((void *)&(args_info->norecovery_flag), 0,
1318 &(args_info->norecovery_given),
1319 &(local_args_info.norecovery_given),
1320 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1321 override, 1, 0, "norecovery", '-',
1322 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001323 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001324
1325 }
1326 /* NSAPI. */
1327 else if (strcmp
1328 (long_options[option_index].name,
1329 "nsapi") == 0) {
1330
1331 if (update_arg((void *)&(args_info->nsapi_arg),
1332 &(args_info->nsapi_orig),
1333 &(args_info->nsapi_given),
1334 &(local_args_info.nsapi_given),
1335 optarg, 0, "0", ARG_INT,
1336 check_ambiguity, override, 0, 0,
1337 "nsapi", '-', additional_error))
1338 goto failure;
1339
1340 }
1341 /* Requested quality of service Extension 1. */
1342 else if (strcmp
1343 (long_options[option_index].name,
1344 "qose1") == 0) {
1345
1346 if (update_arg((void *)&(args_info->qose1_arg),
1347 &(args_info->qose1_orig),
1348 &(args_info->qose1_given),
1349 &(local_args_info.qose1_given),
1350 optarg, 0, "0x9396404074f9ffff",
1351 ARG_INT, check_ambiguity,
1352 override, 0, 0, "qose1", '-',
1353 additional_error))
1354 goto failure;
1355
1356 }
1357 /* Requested quality of service Extension 2. */
1358 else if (strcmp
1359 (long_options[option_index].name,
1360 "qose2") == 0) {
1361
1362 if (update_arg((void *)&(args_info->qose2_arg),
1363 &(args_info->qose2_orig),
1364 &(args_info->qose2_given),
1365 &(local_args_info.qose2_given),
1366 optarg, 0, "0x11", ARG_INT,
1367 check_ambiguity, override, 0, 0,
1368 "qose2", '-', additional_error))
1369 goto failure;
1370
1371 }
1372 /* Requested quality of service Extension 3. */
1373 else if (strcmp
1374 (long_options[option_index].name,
1375 "qose3") == 0) {
1376
1377 if (update_arg((void *)&(args_info->qose3_arg),
1378 &(args_info->qose3_orig),
1379 &(args_info->qose3_given),
1380 &(local_args_info.qose3_given),
1381 optarg, 0, "0x0101", ARG_INT,
1382 check_ambiguity, override, 0, 0,
1383 "qose3", '-', additional_error))
1384 goto failure;
1385
1386 }
1387 /* Requested quality of service Extension 4. */
1388 else if (strcmp
1389 (long_options[option_index].name,
1390 "qose4") == 0) {
1391
1392 if (update_arg((void *)&(args_info->qose4_arg),
1393 &(args_info->qose4_orig),
1394 &(args_info->qose4_given),
1395 &(local_args_info.qose4_given),
1396 optarg, 0, "0x4040", ARG_INT,
1397 check_ambiguity, override, 0, 0,
1398 "qose4", '-', additional_error))
1399 goto failure;
1400
1401 }
1402 /* Charging characteristics. */
1403 else if (strcmp
1404 (long_options[option_index].name,
1405 "charging") == 0) {
1406
1407 if (update_arg
1408 ((void *)&(args_info->charging_arg),
1409 &(args_info->charging_orig),
1410 &(args_info->charging_given),
1411 &(local_args_info.charging_given), optarg,
1412 0, "0x0800", ARG_INT, check_ambiguity,
1413 override, 0, 0, "charging", '-',
1414 additional_error))
1415 goto failure;
1416
1417 }
1418 /* Create local network interface. */
1419 else if (strcmp
1420 (long_options[option_index].name,
1421 "createif") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001422 args_info->createif_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001423
1424 if (update_arg
1425 ((void *)&(args_info->createif_flag), 0,
1426 &(args_info->createif_given),
1427 &(local_args_info.createif_given), optarg,
1428 0, 0, ARG_FLAG, check_ambiguity, override,
1429 1, 0, "createif", '-', additional_error))
1430 goto failure;
1431
1432 }
1433 /* Create default route. */
1434 else if (strcmp
1435 (long_options[option_index].name,
1436 "defaultroute") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001437 args_info->createif_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001438
1439 if (update_arg
1440 ((void *)&(args_info->defaultroute_flag), 0,
1441 &(args_info->defaultroute_given),
1442 &(local_args_info.defaultroute_given),
1443 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1444 override, 1, 0, "defaultroute", '-',
1445 additional_error))
1446 goto failure;
1447
1448 }
1449 /* Script to run after link-up. */
1450 else if (strcmp(long_options[option_index].name, "ipup")
1451 == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001452 args_info->createif_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001453
1454 if (update_arg((void *)&(args_info->ipup_arg),
1455 &(args_info->ipup_orig),
1456 &(args_info->ipup_given),
1457 &(local_args_info.ipup_given),
1458 optarg, 0, 0, ARG_STRING,
1459 check_ambiguity, override, 0, 0,
1460 "ipup", '-', additional_error))
1461 goto failure;
1462
1463 }
1464 /* Script to run after link-down. */
1465 else if (strcmp
1466 (long_options[option_index].name,
1467 "ipdown") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001468 args_info->createif_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001469
1470 if (update_arg((void *)&(args_info->ipdown_arg),
1471 &(args_info->ipdown_orig),
1472 &(args_info->ipdown_given),
1473 &(local_args_info.ipdown_given),
1474 optarg, 0, 0, ARG_STRING,
1475 check_ambiguity, override, 0, 0,
1476 "ipdown", '-', additional_error))
1477 goto failure;
1478
1479 }
Harald Welte73abc382017-10-10 08:50:11 +08001480 /* Name of the local network interface. */
1481 else if (strcmp
1482 (long_options[option_index].name,
1483 "tun-device") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001484 args_info->createif_mode_counter += 1;
Harald Welte73abc382017-10-10 08:50:11 +08001485
1486 if (update_arg
1487 ((void *)&(args_info->tun_device_arg),
1488 &(args_info->tun_device_orig),
1489 &(args_info->tun_device_given),
1490 &(local_args_info.tun_device_given),
1491 optarg, 0, 0, ARG_STRING, check_ambiguity,
1492 override, 0, 0, "tun-device", '-',
1493 additional_error))
1494 goto failure;
1495
1496 }
Harald Weltef6214982017-09-24 10:23:24 +08001497 /* Ping remote host. */
1498 else if (strcmp
1499 (long_options[option_index].name,
1500 "pinghost") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001501 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001502
1503 if (update_arg
1504 ((void *)&(args_info->pinghost_arg),
1505 &(args_info->pinghost_orig),
1506 &(args_info->pinghost_given),
1507 &(local_args_info.pinghost_given), optarg,
1508 0, 0, ARG_STRING, check_ambiguity,
1509 override, 0, 0, "pinghost", '-',
1510 additional_error))
1511 goto failure;
1512
1513 }
1514 /* Number of ping req per second. */
1515 else if (strcmp
1516 (long_options[option_index].name,
1517 "pingrate") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001518 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001519
1520 if (update_arg
1521 ((void *)&(args_info->pingrate_arg),
1522 &(args_info->pingrate_orig),
1523 &(args_info->pingrate_given),
1524 &(local_args_info.pingrate_given), optarg,
1525 0, "1", ARG_INT, check_ambiguity, override,
1526 0, 0, "pingrate", '-', additional_error))
1527 goto failure;
1528
1529 }
1530 /* Number of ping data bytes. */
1531 else if (strcmp
1532 (long_options[option_index].name,
1533 "pingsize") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001534 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001535
1536 if (update_arg
1537 ((void *)&(args_info->pingsize_arg),
1538 &(args_info->pingsize_orig),
1539 &(args_info->pingsize_given),
1540 &(local_args_info.pingsize_given), optarg,
1541 0, "56", ARG_INT, check_ambiguity,
1542 override, 0, 0, "pingsize", '-',
1543 additional_error))
1544 goto failure;
1545
1546 }
1547 /* Number of ping req to send. */
1548 else if (strcmp
1549 (long_options[option_index].name,
1550 "pingcount") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001551 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001552
1553 if (update_arg
1554 ((void *)&(args_info->pingcount_arg),
1555 &(args_info->pingcount_orig),
1556 &(args_info->pingcount_given),
1557 &(local_args_info.pingcount_given), optarg,
1558 0, "0", ARG_INT, check_ambiguity, override,
1559 0, 0, "pingcount", '-', additional_error))
1560 goto failure;
1561
1562 }
1563 /* Do not print ping packet info. */
1564 else if (strcmp
1565 (long_options[option_index].name,
1566 "pingquiet") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001567 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001568
1569 if (update_arg
1570 ((void *)&(args_info->pingquiet_flag), 0,
1571 &(args_info->pingquiet_given),
1572 &(local_args_info.pingquiet_given), optarg,
1573 0, 0, ARG_FLAG, check_ambiguity, override,
1574 1, 0, "pingquiet", '-', additional_error))
1575 goto failure;
1576
Harald Weltebed35df2011-11-02 13:06:18 +01001577 }
Harald Weltefbb9c7f2017-09-24 11:50:20 +08001578 /* Don't transmit G-PDU sequence nums. */
1579 else if (strcmp
1580 (long_options[option_index].name,
1581 "no-tx-gpdu-seq") == 0) {
1582
1583 if (update_arg
1584 ((void *)&(args_info->no_tx_gpdu_seq_flag),
1585 0, &(args_info->no_tx_gpdu_seq_given),
1586 &(local_args_info.no_tx_gpdu_seq_given),
1587 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1588 override, 1, 0, "no-tx-gpdu-seq", '-',
1589 additional_error))
1590 goto failure;
1591
1592 }
jjako52c24142002-12-16 13:33:51 +00001593
Harald Weltebed35df2011-11-02 13:06:18 +01001594 break;
1595 case '?': /* Invalid option. */
1596 /* `getopt_long' already printed an error message. */
1597 goto failure;
jjako52c24142002-12-16 13:33:51 +00001598
Harald Weltebed35df2011-11-02 13:06:18 +01001599 default: /* bug: option not considered. */
1600 fprintf(stderr, "%s: option unknown: %c%s\n",
1601 CMDLINE_PARSER_PACKAGE, c,
1602 (additional_error ? additional_error : ""));
1603 abort();
1604 } /* switch */
1605 } /* while */
jjako52c24142002-12-16 13:33:51 +00001606
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001607 if (args_info->createif_mode_counter
1608 && args_info->pinghost_mode_counter) {
1609 int createif_given[] =
1610 { args_info->createif_given, args_info->net_given,
1611 args_info->defaultroute_given, args_info->ipup_given,
1612 args_info->ipdown_given, args_info->tun_device_given, -1
1613 };
1614 const char *createif_desc[] =
1615 { "--createif", "--net", "--defaultroute", "--ipup",
1616 "--ipdown", "--tun-device", 0
1617 };
1618 int pinghost_given[] =
1619 { args_info->pinghost_given, args_info->pingrate_given,
1620 args_info->pingsize_given, args_info->pingcount_given,
1621 args_info->pingquiet_given, -1
1622 };
1623 const char *pinghost_desc[] =
1624 { "--pinghost", "--pingrate", "--pingsize", "--pingcount",
1625 "--pingquiet", 0
1626 };
1627 error_occurred +=
1628 check_modes(createif_given, createif_desc, pinghost_given,
1629 pinghost_desc);
1630 }
1631
Harald Welte73abc382017-10-10 08:50:11 +08001632 if (check_required) {
1633 error_occurred +=
1634 cmdline_parser_required2(args_info, argv[0],
1635 additional_error);
1636 }
1637
Harald Weltebed35df2011-11-02 13:06:18 +01001638 cmdline_parser_release(&local_args_info);
jjako52c24142002-12-16 13:33:51 +00001639
Harald Weltef6214982017-09-24 10:23:24 +08001640 if (error_occurred)
Harald Weltebed35df2011-11-02 13:06:18 +01001641 return (EXIT_FAILURE);
Harald Welte1b3e5772010-05-04 11:13:56 +02001642
Harald Weltebed35df2011-11-02 13:06:18 +01001643 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001644
1645failure:
Harald Weltebed35df2011-11-02 13:06:18 +01001646
1647 cmdline_parser_release(&local_args_info);
1648 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +00001649}
1650
Harald Welte1b3e5772010-05-04 11:13:56 +02001651#ifndef CONFIG_FILE_LINE_SIZE
1652#define CONFIG_FILE_LINE_SIZE 2048
1653#endif
1654#define ADDITIONAL_ERROR " in configuration file "
1655
1656#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1657/* 3 is for "--" and "=" */
1658
Harald Weltef6214982017-09-24 10:23:24 +08001659static int _cmdline_parser_configfile(const char *filename, int *my_argc)
jjako52c24142002-12-16 13:33:51 +00001660{
Harald Weltebed35df2011-11-02 13:06:18 +01001661 FILE *file;
Harald Weltef6214982017-09-24 10:23:24 +08001662 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE + 1];
Harald Weltebed35df2011-11-02 13:06:18 +01001663 char linebuf[CONFIG_FILE_LINE_SIZE];
1664 int line_num = 0;
Harald Weltef6214982017-09-24 10:23:24 +08001665 int result = 0, equal;
Harald Weltebed35df2011-11-02 13:06:18 +01001666 char *fopt, *farg;
1667 char *str_index;
1668 size_t len, next_token;
1669 char delimiter;
Harald Welte1b3e5772010-05-04 11:13:56 +02001670
Harald Weltef6214982017-09-24 10:23:24 +08001671 if ((file = fopen(filename, "r")) == 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001672 fprintf(stderr, "%s: Error opening configuration file '%s'\n",
1673 CMDLINE_PARSER_PACKAGE, filename);
Harald Weltef6214982017-09-24 10:23:24 +08001674 return EXIT_FAILURE;
Harald Weltebed35df2011-11-02 13:06:18 +01001675 }
jjako52c24142002-12-16 13:33:51 +00001676
Harald Weltef6214982017-09-24 10:23:24 +08001677 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001678 ++line_num;
1679 my_argv[0] = '\0';
1680 len = strlen(linebuf);
1681 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE - 1)) {
1682 fprintf(stderr,
1683 "%s:%s:%d: Line too long in configuration file\n",
1684 CMDLINE_PARSER_PACKAGE, filename, line_num);
1685 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001686 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001687 }
jjako52c24142002-12-16 13:33:51 +00001688
Harald Weltebed35df2011-11-02 13:06:18 +01001689 /* find first non-whitespace character in the line */
1690 next_token = strspn(linebuf, " \t\r\n");
1691 str_index = linebuf + next_token;
jjako52c24142002-12-16 13:33:51 +00001692
Harald Weltebed35df2011-11-02 13:06:18 +01001693 if (str_index[0] == '\0' || str_index[0] == '#')
1694 continue; /* empty line or comment line is skipped */
Harald Welte1b3e5772010-05-04 11:13:56 +02001695
Harald Weltebed35df2011-11-02 13:06:18 +01001696 fopt = str_index;
Harald Welte1b3e5772010-05-04 11:13:56 +02001697
Harald Weltebed35df2011-11-02 13:06:18 +01001698 /* truncate fopt at the end of the first non-valid character */
1699 next_token = strcspn(fopt, " \t\r\n=");
Harald Welte1b3e5772010-05-04 11:13:56 +02001700
Harald Weltebed35df2011-11-02 13:06:18 +01001701 if (fopt[next_token] == '\0') { /* the line is over */
Harald Weltef6214982017-09-24 10:23:24 +08001702 farg = 0;
Harald Weltebed35df2011-11-02 13:06:18 +01001703 equal = 0;
1704 goto noarg;
1705 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001706
Harald Weltebed35df2011-11-02 13:06:18 +01001707 /* remember if equal sign is present */
1708 equal = (fopt[next_token] == '=');
1709 fopt[next_token++] = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001710
Harald Weltebed35df2011-11-02 13:06:18 +01001711 /* advance pointers to the next token after the end of fopt */
1712 next_token += strspn(fopt + next_token, " \t\r\n");
Harald Weltef6214982017-09-24 10:23:24 +08001713
Harald Weltebed35df2011-11-02 13:06:18 +01001714 /* check for the presence of equal sign, and if so, skip it */
1715 if (!equal)
1716 if ((equal = (fopt[next_token] == '='))) {
1717 next_token++;
1718 next_token +=
1719 strspn(fopt + next_token, " \t\r\n");
1720 }
1721 str_index += next_token;
Harald Welte1b3e5772010-05-04 11:13:56 +02001722
Harald Weltebed35df2011-11-02 13:06:18 +01001723 /* find argument */
1724 farg = str_index;
1725 if (farg[0] == '\"' || farg[0] == '\'') { /* quoted argument */
1726 str_index = strchr(++farg, str_index[0]); /* skip opening quote */
1727 if (!str_index) {
1728 fprintf
1729 (stderr,
1730 "%s:%s:%d: unterminated string in configuration file\n",
1731 CMDLINE_PARSER_PACKAGE, filename,
1732 line_num);
1733 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001734 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001735 }
1736 } else { /* read up the remaining part up to a delimiter */
1737 next_token = strcspn(farg, " \t\r\n#\'\"");
1738 str_index += next_token;
1739 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001740
Harald Weltebed35df2011-11-02 13:06:18 +01001741 /* truncate farg at the delimiter and store it for further check */
1742 delimiter = *str_index, *str_index++ = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001743
Harald Weltebed35df2011-11-02 13:06:18 +01001744 /* everything but comment is illegal at the end of line */
1745 if (delimiter != '\0' && delimiter != '#') {
1746 str_index += strspn(str_index, " \t\r\n");
1747 if (*str_index != '\0' && *str_index != '#') {
1748 fprintf
1749 (stderr,
1750 "%s:%s:%d: malformed string in configuration file\n",
1751 CMDLINE_PARSER_PACKAGE, filename,
1752 line_num);
1753 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001754 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001755 }
1756 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001757
Harald Weltebed35df2011-11-02 13:06:18 +01001758noarg:
Harald Weltef6214982017-09-24 10:23:24 +08001759 if (!strcmp(fopt, "include")) {
1760 if (farg && *farg) {
1761 result =
1762 _cmdline_parser_configfile(farg, my_argc);
1763 } else {
1764 fprintf(stderr,
1765 "%s:%s:%d: include requires a filename argument.\n",
1766 CMDLINE_PARSER_PACKAGE, filename,
1767 line_num);
1768 }
1769 continue;
1770 }
Harald Weltebed35df2011-11-02 13:06:18 +01001771 len = strlen(fopt);
Harald Weltebed35df2011-11-02 13:06:18 +01001772 strcat(my_argv, len > 1 ? "--" : "-");
1773 strcat(my_argv, fopt);
1774 if (len > 1 && ((farg && *farg) || equal))
1775 strcat(my_argv, "=");
1776 if (farg && *farg)
1777 strcat(my_argv, farg);
Harald Weltef6214982017-09-24 10:23:24 +08001778 ++(*my_argc);
Harald Welte1b3e5772010-05-04 11:13:56 +02001779
Harald Weltebed35df2011-11-02 13:06:18 +01001780 cmd_line_list_tmp =
1781 (struct line_list *)malloc(sizeof(struct line_list));
1782 cmd_line_list_tmp->next = cmd_line_list;
1783 cmd_line_list = cmd_line_list_tmp;
1784 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1785 } /* while */
jjako52c24142002-12-16 13:33:51 +00001786
Harald Weltebed35df2011-11-02 13:06:18 +01001787 if (file)
1788 fclose(file);
Harald Weltef6214982017-09-24 10:23:24 +08001789 return result;
1790}
1791
1792int
1793cmdline_parser_configfile(const char *filename,
1794 struct gengetopt_args_info *args_info,
1795 int override, int initialize, int check_required)
1796{
1797 struct cmdline_parser_params params;
1798
1799 params.override = override;
1800 params.initialize = initialize;
1801 params.check_required = check_required;
1802 params.check_ambiguity = 0;
1803 params.print_errors = 1;
1804
1805 return cmdline_parser_config_file(filename, args_info, &params);
1806}
1807
1808int
1809cmdline_parser_config_file(const char *filename,
1810 struct gengetopt_args_info *args_info,
1811 struct cmdline_parser_params *params)
1812{
1813 int i, result;
1814 int my_argc = 1;
1815 char **my_argv_arg;
1816 char *additional_error;
1817
1818 /* store the program name */
1819 cmd_line_list_tmp =
1820 (struct line_list *)malloc(sizeof(struct line_list));
1821 cmd_line_list_tmp->next = cmd_line_list;
1822 cmd_line_list = cmd_line_list_tmp;
1823 cmd_line_list->string_arg = gengetopt_strdup(CMDLINE_PARSER_PACKAGE);
1824
1825 result = _cmdline_parser_configfile(filename, &my_argc);
1826
1827 if (result != EXIT_FAILURE) {
1828 my_argv_arg = (char **)malloc((my_argc + 1) * sizeof(char *));
1829 cmd_line_list_tmp = cmd_line_list;
1830
1831 for (i = my_argc - 1; i >= 0; --i) {
1832 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1833 cmd_line_list_tmp = cmd_line_list_tmp->next;
1834 }
1835
1836 my_argv_arg[my_argc] = 0;
1837
1838 additional_error =
1839 (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) +
1840 1);
1841 strcpy(additional_error, ADDITIONAL_ERROR);
1842 strcat(additional_error, filename);
1843 result =
1844 cmdline_parser_internal(my_argc, my_argv_arg, args_info,
1845 params, additional_error);
1846
1847 free(additional_error);
1848 free(my_argv_arg);
1849 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001850
Harald Weltebed35df2011-11-02 13:06:18 +01001851 free_cmd_list();
1852 if (result == EXIT_FAILURE) {
1853 cmdline_parser_free(args_info);
1854 exit(EXIT_FAILURE);
1855 }
1856
1857 return result;
jjako52c24142002-12-16 13:33:51 +00001858}