blob: 13d02952e254b9bbd99d4b02f88be4c2980fd3af [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",
Andreas Schultzb6292402018-10-05 13:58:45 +010075 " --netns=STRING Network namespace to use",
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +020076 "\n Mode: pinghost\n generate ICMP payload inside G-PDU without setting up tun interface",
Harald Weltef6214982017-09-24 10:23:24 +080077 " --pinghost=STRING Ping remote host",
78 " --pingrate=INT Number of ping req per second (default=`1')",
79 " --pingsize=INT Number of ping data bytes (default=`56')",
80 " --pingcount=INT Number of ping req to send (default=`0')",
81 " --pingquiet Do not print ping packet info (default=off)",
Harald Weltefbb9c7f2017-09-24 11:50:20 +080082 " --no-tx-gpdu-seq Don't transmit G-PDU sequence nums\n (default=off)",
Harald Welte840a8e92017-09-24 18:12:40 +080083 " -t, --pdp-type=(v4|v6) PDP Type (default=`v4')",
Harald Weltebed35df2011-11-02 13:06:18 +010084 0
Harald Welte1b3e5772010-05-04 11:13:56 +020085};
86
Harald Weltef6214982017-09-24 10:23:24 +080087typedef enum { ARG_NO, ARG_FLAG, ARG_STRING, ARG_INT
88} cmdline_parser_arg_type;
89
Harald Welte1b3e5772010-05-04 11:13:56 +020090static
Harald Weltebed35df2011-11-02 13:06:18 +010091void clear_given(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020092static
Harald Weltebed35df2011-11-02 13:06:18 +010093void clear_args(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020094
95static int
Harald Weltef6214982017-09-24 10:23:24 +080096cmdline_parser_internal(int argc, char **argv,
97 struct gengetopt_args_info *args_info,
98 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +010099 const char *additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +0200100
Harald Welte73abc382017-10-10 08:50:11 +0800101static int
102cmdline_parser_required2(struct gengetopt_args_info *args_info,
103 const char *prog_name, const char *additional_error);
Harald Weltebed35df2011-11-02 13:06:18 +0100104struct line_list {
105 char *string_arg;
106 struct line_list *next;
Harald Welte1b3e5772010-05-04 11:13:56 +0200107};
108
109static struct line_list *cmd_line_list = 0;
110static struct line_list *cmd_line_list_tmp = 0;
111
Harald Weltebed35df2011-11-02 13:06:18 +0100112static void free_cmd_list(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200113{
Harald Weltebed35df2011-11-02 13:06:18 +0100114 /* free the list of a previous call */
115 if (cmd_line_list) {
116 while (cmd_line_list) {
117 cmd_line_list_tmp = cmd_line_list;
118 cmd_line_list = cmd_line_list->next;
119 free(cmd_line_list_tmp->string_arg);
120 free(cmd_line_list_tmp);
121 }
122 }
jjako52c24142002-12-16 13:33:51 +0000123}
124
Harald Weltebed35df2011-11-02 13:06:18 +0100125static char *gengetopt_strdup(const char *s);
jjako52c24142002-12-16 13:33:51 +0000126
Harald Welte1b3e5772010-05-04 11:13:56 +0200127static
Harald Weltebed35df2011-11-02 13:06:18 +0100128void clear_given(struct gengetopt_args_info *args_info)
jjako52c24142002-12-16 13:33:51 +0000129{
Harald Weltebed35df2011-11-02 13:06:18 +0100130 args_info->help_given = 0;
131 args_info->version_given = 0;
132 args_info->debug_given = 0;
133 args_info->conf_given = 0;
134 args_info->pidfile_given = 0;
135 args_info->statedir_given = 0;
136 args_info->dns_given = 0;
137 args_info->listen_given = 0;
138 args_info->remote_given = 0;
139 args_info->contexts_given = 0;
140 args_info->timelimit_given = 0;
141 args_info->gtpversion_given = 0;
142 args_info->apn_given = 0;
143 args_info->selmode_given = 0;
144 args_info->rattype_given = 0;
145 args_info->userloc_given = 0;
146 args_info->rai_given = 0;
147 args_info->mstz_given = 0;
148 args_info->imeisv_given = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800149 args_info->norecovery_given = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100150 args_info->imsi_given = 0;
151 args_info->nsapi_given = 0;
152 args_info->msisdn_given = 0;
153 args_info->qos_given = 0;
154 args_info->qose1_given = 0;
155 args_info->qose2_given = 0;
156 args_info->qose3_given = 0;
157 args_info->qose4_given = 0;
158 args_info->charging_given = 0;
159 args_info->uid_given = 0;
160 args_info->pwd_given = 0;
161 args_info->createif_given = 0;
162 args_info->net_given = 0;
163 args_info->defaultroute_given = 0;
164 args_info->ipup_given = 0;
165 args_info->ipdown_given = 0;
Harald Welte73abc382017-10-10 08:50:11 +0800166 args_info->tun_device_given = 0;
Andreas Schultzb6292402018-10-05 13:58:45 +0100167 args_info->netns_given = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100168 args_info->pinghost_given = 0;
169 args_info->pingrate_given = 0;
170 args_info->pingsize_given = 0;
171 args_info->pingcount_given = 0;
172 args_info->pingquiet_given = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800173 args_info->no_tx_gpdu_seq_given = 0;
Harald Welte840a8e92017-09-24 18:12:40 +0800174 args_info->pdp_type_given = 0;
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +0200175 args_info->createif_mode_counter = 0;
176 args_info->pinghost_mode_counter = 0;
jjako52c24142002-12-16 13:33:51 +0000177}
178
Harald Welte1b3e5772010-05-04 11:13:56 +0200179static
Harald Weltebed35df2011-11-02 13:06:18 +0100180void clear_args(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200181{
Harald Weltef6214982017-09-24 10:23:24 +0800182 FIX_UNUSED(args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100183 args_info->debug_flag = 0;
184 args_info->conf_arg = NULL;
185 args_info->conf_orig = NULL;
186 args_info->pidfile_arg = gengetopt_strdup("./sgsnemu.pid");
187 args_info->pidfile_orig = NULL;
188 args_info->statedir_arg = gengetopt_strdup("./");
189 args_info->statedir_orig = NULL;
190 args_info->dns_arg = NULL;
191 args_info->dns_orig = NULL;
192 args_info->listen_arg = NULL;
193 args_info->listen_orig = NULL;
194 args_info->remote_arg = NULL;
195 args_info->remote_orig = NULL;
196 args_info->contexts_arg = 1;
197 args_info->contexts_orig = NULL;
198 args_info->timelimit_arg = 0;
199 args_info->timelimit_orig = NULL;
200 args_info->gtpversion_arg = 1;
201 args_info->gtpversion_orig = NULL;
202 args_info->apn_arg = gengetopt_strdup("internet");
203 args_info->apn_orig = NULL;
204 args_info->selmode_arg = 0x01;
205 args_info->selmode_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800206 args_info->rattype_arg = 1;
Harald Weltebed35df2011-11-02 13:06:18 +0100207 args_info->rattype_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800208 args_info->userloc_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100209 args_info->userloc_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800210 args_info->rai_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100211 args_info->rai_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800212 args_info->mstz_arg = gengetopt_strdup("0");
Harald Weltebed35df2011-11-02 13:06:18 +0100213 args_info->mstz_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800214 args_info->imeisv_arg = gengetopt_strdup("2143658709214365");
Harald Weltebed35df2011-11-02 13:06:18 +0100215 args_info->imeisv_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800216 args_info->norecovery_flag = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100217 args_info->imsi_arg = gengetopt_strdup("240010123456789");
218 args_info->imsi_orig = NULL;
219 args_info->nsapi_arg = 0;
220 args_info->nsapi_orig = NULL;
221 args_info->msisdn_arg = gengetopt_strdup("46702123456");
222 args_info->msisdn_orig = NULL;
223 args_info->qos_arg = 0x000b921f;
224 args_info->qos_orig = NULL;
225 args_info->qose1_arg = 0x9396404074f9ffff;
226 args_info->qose1_orig = NULL;
227 args_info->qose2_arg = 0x11;
228 args_info->qose2_orig = NULL;
229 args_info->qose3_arg = 0x0101;
230 args_info->qose3_orig = NULL;
231 args_info->qose4_arg = 0x4040;
232 args_info->qose4_orig = NULL;
233 args_info->charging_arg = 0x0800;
234 args_info->charging_orig = NULL;
235 args_info->uid_arg = gengetopt_strdup("mig");
236 args_info->uid_orig = NULL;
237 args_info->pwd_arg = gengetopt_strdup("hemmelig");
238 args_info->pwd_orig = NULL;
239 args_info->createif_flag = 0;
240 args_info->net_arg = NULL;
241 args_info->net_orig = NULL;
242 args_info->defaultroute_flag = 0;
243 args_info->ipup_arg = NULL;
244 args_info->ipup_orig = NULL;
245 args_info->ipdown_arg = NULL;
246 args_info->ipdown_orig = NULL;
Harald Welte73abc382017-10-10 08:50:11 +0800247 args_info->tun_device_arg = NULL;
248 args_info->tun_device_orig = NULL;
Andreas Schultzb6292402018-10-05 13:58:45 +0100249 args_info->netns_arg = NULL;
250 args_info->netns_orig = NULL;
Harald Weltebed35df2011-11-02 13:06:18 +0100251 args_info->pinghost_arg = NULL;
252 args_info->pinghost_orig = NULL;
253 args_info->pingrate_arg = 1;
254 args_info->pingrate_orig = NULL;
255 args_info->pingsize_arg = 56;
256 args_info->pingsize_orig = NULL;
257 args_info->pingcount_arg = 0;
258 args_info->pingcount_orig = NULL;
259 args_info->pingquiet_flag = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800260 args_info->no_tx_gpdu_seq_flag = 0;
Harald Welte840a8e92017-09-24 18:12:40 +0800261 args_info->pdp_type_arg = gengetopt_strdup("v4");
262 args_info->pdp_type_orig = NULL;
Harald Weltebed35df2011-11-02 13:06:18 +0100263
Harald Welte1b3e5772010-05-04 11:13:56 +0200264}
265
266static
267void init_args_info(struct gengetopt_args_info *args_info)
268{
Harald Weltef6214982017-09-24 10:23:24 +0800269
Harald Weltebed35df2011-11-02 13:06:18 +0100270 args_info->help_help = gengetopt_args_info_help[0];
271 args_info->version_help = gengetopt_args_info_help[1];
272 args_info->debug_help = gengetopt_args_info_help[2];
273 args_info->conf_help = gengetopt_args_info_help[3];
274 args_info->pidfile_help = gengetopt_args_info_help[4];
275 args_info->statedir_help = gengetopt_args_info_help[5];
276 args_info->dns_help = gengetopt_args_info_help[6];
277 args_info->listen_help = gengetopt_args_info_help[7];
278 args_info->remote_help = gengetopt_args_info_help[8];
279 args_info->contexts_help = gengetopt_args_info_help[9];
280 args_info->timelimit_help = gengetopt_args_info_help[10];
281 args_info->gtpversion_help = gengetopt_args_info_help[11];
282 args_info->apn_help = gengetopt_args_info_help[12];
283 args_info->selmode_help = gengetopt_args_info_help[13];
Harald Weltef6214982017-09-24 10:23:24 +0800284 args_info->rattype_help = gengetopt_args_info_help[14];
285 args_info->userloc_help = gengetopt_args_info_help[15];
286 args_info->rai_help = gengetopt_args_info_help[16];
287 args_info->mstz_help = gengetopt_args_info_help[17];
288 args_info->imeisv_help = gengetopt_args_info_help[18];
289 args_info->norecovery_help = gengetopt_args_info_help[19];
290 args_info->imsi_help = gengetopt_args_info_help[20];
291 args_info->nsapi_help = gengetopt_args_info_help[21];
292 args_info->msisdn_help = gengetopt_args_info_help[22];
293 args_info->qos_help = gengetopt_args_info_help[23];
294 args_info->qose1_help = gengetopt_args_info_help[24];
295 args_info->qose2_help = gengetopt_args_info_help[25];
296 args_info->qose3_help = gengetopt_args_info_help[26];
297 args_info->qose4_help = gengetopt_args_info_help[27];
298 args_info->charging_help = gengetopt_args_info_help[28];
299 args_info->uid_help = gengetopt_args_info_help[29];
300 args_info->pwd_help = gengetopt_args_info_help[30];
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +0200301 args_info->createif_help = gengetopt_args_info_help[32];
302 args_info->net_help = gengetopt_args_info_help[33];
303 args_info->defaultroute_help = gengetopt_args_info_help[34];
304 args_info->ipup_help = gengetopt_args_info_help[35];
305 args_info->ipdown_help = gengetopt_args_info_help[36];
306 args_info->tun_device_help = gengetopt_args_info_help[37];
Andreas Schultzb6292402018-10-05 13:58:45 +0100307 args_info->netns_help = gengetopt_args_info_help[38];
308 args_info->pinghost_help = gengetopt_args_info_help[40];
309 args_info->pingrate_help = gengetopt_args_info_help[41];
310 args_info->pingsize_help = gengetopt_args_info_help[42];
311 args_info->pingcount_help = gengetopt_args_info_help[43];
312 args_info->pingquiet_help = gengetopt_args_info_help[44];
313 args_info->no_tx_gpdu_seq_help = gengetopt_args_info_help[45];
314 args_info->pdp_type_help = gengetopt_args_info_help[46];
Harald Weltebed35df2011-11-02 13:06:18 +0100315
Harald Welte1b3e5772010-05-04 11:13:56 +0200316}
317
Harald Weltebed35df2011-11-02 13:06:18 +0100318void cmdline_parser_print_version(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200319{
Harald Weltef6214982017-09-24 10:23:24 +0800320 printf("%s %s\n",
321 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ?
322 CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
323 CMDLINE_PARSER_VERSION);
324
325 if (strlen(gengetopt_args_info_versiontext) > 0)
326 printf("\n%s\n", gengetopt_args_info_versiontext);
Harald Welte1b3e5772010-05-04 11:13:56 +0200327}
328
Harald Weltef6214982017-09-24 10:23:24 +0800329static void print_help_common(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200330{
Harald Weltebed35df2011-11-02 13:06:18 +0100331 cmdline_parser_print_version();
Harald Welte1b3e5772010-05-04 11:13:56 +0200332
Harald Weltebed35df2011-11-02 13:06:18 +0100333 if (strlen(gengetopt_args_info_purpose) > 0)
334 printf("\n%s\n", gengetopt_args_info_purpose);
Harald Welte1b3e5772010-05-04 11:13:56 +0200335
Harald Weltef6214982017-09-24 10:23:24 +0800336 if (strlen(gengetopt_args_info_usage) > 0)
337 printf("\n%s\n", gengetopt_args_info_usage);
338
339 printf("\n");
340
341 if (strlen(gengetopt_args_info_description) > 0)
342 printf("%s\n\n", gengetopt_args_info_description);
343}
344
345void cmdline_parser_print_help(void)
346{
347 int i = 0;
348 print_help_common();
Harald Weltebed35df2011-11-02 13:06:18 +0100349 while (gengetopt_args_info_help[i])
350 printf("%s\n", gengetopt_args_info_help[i++]);
Harald Welte1b3e5772010-05-04 11:13:56 +0200351}
352
Harald Weltebed35df2011-11-02 13:06:18 +0100353void cmdline_parser_init(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200354{
Harald Weltebed35df2011-11-02 13:06:18 +0100355 clear_given(args_info);
356 clear_args(args_info);
357 init_args_info(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200358}
359
Harald Weltef6214982017-09-24 10:23:24 +0800360void cmdline_parser_params_init(struct cmdline_parser_params *params)
361{
362 if (params) {
363 params->override = 0;
364 params->initialize = 1;
365 params->check_required = 1;
366 params->check_ambiguity = 0;
367 params->print_errors = 1;
368 }
369}
370
371struct cmdline_parser_params *cmdline_parser_params_create(void)
372{
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +0200373 struct cmdline_parser_params *params = (struct cmdline_parser_params *)
Harald Weltef6214982017-09-24 10:23:24 +0800374 malloc(sizeof(struct cmdline_parser_params));
375 cmdline_parser_params_init(params);
376 return params;
377}
378
379static void free_string_field(char **s)
380{
381 if (*s) {
382 free(*s);
383 *s = 0;
384 }
385}
386
Harald Weltebed35df2011-11-02 13:06:18 +0100387static void cmdline_parser_release(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200388{
Harald Weltebed35df2011-11-02 13:06:18 +0100389
Harald Weltef6214982017-09-24 10:23:24 +0800390 free_string_field(&(args_info->conf_arg));
391 free_string_field(&(args_info->conf_orig));
392 free_string_field(&(args_info->pidfile_arg));
393 free_string_field(&(args_info->pidfile_orig));
394 free_string_field(&(args_info->statedir_arg));
395 free_string_field(&(args_info->statedir_orig));
396 free_string_field(&(args_info->dns_arg));
397 free_string_field(&(args_info->dns_orig));
398 free_string_field(&(args_info->listen_arg));
399 free_string_field(&(args_info->listen_orig));
400 free_string_field(&(args_info->remote_arg));
401 free_string_field(&(args_info->remote_orig));
402 free_string_field(&(args_info->contexts_orig));
403 free_string_field(&(args_info->timelimit_orig));
404 free_string_field(&(args_info->gtpversion_orig));
405 free_string_field(&(args_info->apn_arg));
406 free_string_field(&(args_info->apn_orig));
407 free_string_field(&(args_info->selmode_orig));
408 free_string_field(&(args_info->rattype_orig));
409 free_string_field(&(args_info->userloc_arg));
410 free_string_field(&(args_info->userloc_orig));
411 free_string_field(&(args_info->rai_arg));
412 free_string_field(&(args_info->rai_orig));
413 free_string_field(&(args_info->mstz_arg));
414 free_string_field(&(args_info->mstz_orig));
415 free_string_field(&(args_info->imeisv_arg));
416 free_string_field(&(args_info->imeisv_orig));
417 free_string_field(&(args_info->imsi_arg));
418 free_string_field(&(args_info->imsi_orig));
419 free_string_field(&(args_info->nsapi_orig));
420 free_string_field(&(args_info->msisdn_arg));
421 free_string_field(&(args_info->msisdn_orig));
422 free_string_field(&(args_info->qos_orig));
423 free_string_field(&(args_info->qose1_orig));
424 free_string_field(&(args_info->qose2_orig));
425 free_string_field(&(args_info->qose3_orig));
426 free_string_field(&(args_info->qose4_orig));
427 free_string_field(&(args_info->charging_orig));
428 free_string_field(&(args_info->uid_arg));
429 free_string_field(&(args_info->uid_orig));
430 free_string_field(&(args_info->pwd_arg));
431 free_string_field(&(args_info->pwd_orig));
432 free_string_field(&(args_info->net_arg));
433 free_string_field(&(args_info->net_orig));
434 free_string_field(&(args_info->ipup_arg));
435 free_string_field(&(args_info->ipup_orig));
436 free_string_field(&(args_info->ipdown_arg));
437 free_string_field(&(args_info->ipdown_orig));
Harald Welte73abc382017-10-10 08:50:11 +0800438 free_string_field(&(args_info->tun_device_arg));
439 free_string_field(&(args_info->tun_device_orig));
Andreas Schultzb6292402018-10-05 13:58:45 +0100440 free_string_field(&(args_info->netns_arg));
441 free_string_field(&(args_info->netns_orig));
Harald Weltef6214982017-09-24 10:23:24 +0800442 free_string_field(&(args_info->pinghost_arg));
443 free_string_field(&(args_info->pinghost_orig));
444 free_string_field(&(args_info->pingrate_orig));
445 free_string_field(&(args_info->pingsize_orig));
446 free_string_field(&(args_info->pingcount_orig));
Harald Welte840a8e92017-09-24 18:12:40 +0800447 free_string_field(&(args_info->pdp_type_arg));
448 free_string_field(&(args_info->pdp_type_orig));
Harald Weltebed35df2011-11-02 13:06:18 +0100449
450 clear_given(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200451}
452
Harald Weltef6214982017-09-24 10:23:24 +0800453static void
454write_into_file(FILE * outfile, const char *opt, const char *arg,
455 const char *values[])
456{
457 FIX_UNUSED(values);
458 if (arg) {
459 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
460 } else {
461 fprintf(outfile, "%s\n", opt);
462 }
463}
464
465int cmdline_parser_dump(FILE * outfile, struct gengetopt_args_info *args_info)
466{
467 int i = 0;
468
469 if (!outfile) {
470 fprintf(stderr, "%s: cannot dump options to stream\n",
471 CMDLINE_PARSER_PACKAGE);
472 return EXIT_FAILURE;
473 }
474
475 if (args_info->help_given)
476 write_into_file(outfile, "help", 0, 0);
477 if (args_info->version_given)
478 write_into_file(outfile, "version", 0, 0);
479 if (args_info->debug_given)
480 write_into_file(outfile, "debug", 0, 0);
481 if (args_info->conf_given)
482 write_into_file(outfile, "conf", args_info->conf_orig, 0);
483 if (args_info->pidfile_given)
484 write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0);
485 if (args_info->statedir_given)
486 write_into_file(outfile, "statedir", args_info->statedir_orig,
487 0);
488 if (args_info->dns_given)
489 write_into_file(outfile, "dns", args_info->dns_orig, 0);
490 if (args_info->listen_given)
491 write_into_file(outfile, "listen", args_info->listen_orig, 0);
492 if (args_info->remote_given)
493 write_into_file(outfile, "remote", args_info->remote_orig, 0);
494 if (args_info->contexts_given)
495 write_into_file(outfile, "contexts", args_info->contexts_orig,
496 0);
497 if (args_info->timelimit_given)
498 write_into_file(outfile, "timelimit", args_info->timelimit_orig,
499 0);
500 if (args_info->gtpversion_given)
501 write_into_file(outfile, "gtpversion",
502 args_info->gtpversion_orig, 0);
503 if (args_info->apn_given)
504 write_into_file(outfile, "apn", args_info->apn_orig, 0);
505 if (args_info->selmode_given)
506 write_into_file(outfile, "selmode", args_info->selmode_orig, 0);
507 if (args_info->rattype_given)
508 write_into_file(outfile, "rattype", args_info->rattype_orig, 0);
509 if (args_info->userloc_given)
510 write_into_file(outfile, "userloc", args_info->userloc_orig, 0);
511 if (args_info->rai_given)
512 write_into_file(outfile, "rai", args_info->rai_orig, 0);
513 if (args_info->mstz_given)
514 write_into_file(outfile, "mstz", args_info->mstz_orig, 0);
515 if (args_info->imeisv_given)
516 write_into_file(outfile, "imeisv", args_info->imeisv_orig, 0);
517 if (args_info->norecovery_given)
518 write_into_file(outfile, "norecovery", 0, 0);
519 if (args_info->imsi_given)
520 write_into_file(outfile, "imsi", args_info->imsi_orig, 0);
521 if (args_info->nsapi_given)
522 write_into_file(outfile, "nsapi", args_info->nsapi_orig, 0);
523 if (args_info->msisdn_given)
524 write_into_file(outfile, "msisdn", args_info->msisdn_orig, 0);
525 if (args_info->qos_given)
526 write_into_file(outfile, "qos", args_info->qos_orig, 0);
527 if (args_info->qose1_given)
528 write_into_file(outfile, "qose1", args_info->qose1_orig, 0);
529 if (args_info->qose2_given)
530 write_into_file(outfile, "qose2", args_info->qose2_orig, 0);
531 if (args_info->qose3_given)
532 write_into_file(outfile, "qose3", args_info->qose3_orig, 0);
533 if (args_info->qose4_given)
534 write_into_file(outfile, "qose4", args_info->qose4_orig, 0);
535 if (args_info->charging_given)
536 write_into_file(outfile, "charging", args_info->charging_orig,
537 0);
538 if (args_info->uid_given)
539 write_into_file(outfile, "uid", args_info->uid_orig, 0);
540 if (args_info->pwd_given)
541 write_into_file(outfile, "pwd", args_info->pwd_orig, 0);
542 if (args_info->createif_given)
543 write_into_file(outfile, "createif", 0, 0);
544 if (args_info->net_given)
545 write_into_file(outfile, "net", args_info->net_orig, 0);
546 if (args_info->defaultroute_given)
547 write_into_file(outfile, "defaultroute", 0, 0);
548 if (args_info->ipup_given)
549 write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
550 if (args_info->ipdown_given)
551 write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0);
Harald Welte73abc382017-10-10 08:50:11 +0800552 if (args_info->tun_device_given)
553 write_into_file(outfile, "tun-device",
554 args_info->tun_device_orig, 0);
Andreas Schultzb6292402018-10-05 13:58:45 +0100555 if (args_info->netns_given)
556 write_into_file(outfile, "netns", args_info->netns_orig, 0);
Harald Weltef6214982017-09-24 10:23:24 +0800557 if (args_info->pinghost_given)
558 write_into_file(outfile, "pinghost", args_info->pinghost_orig,
559 0);
560 if (args_info->pingrate_given)
561 write_into_file(outfile, "pingrate", args_info->pingrate_orig,
562 0);
563 if (args_info->pingsize_given)
564 write_into_file(outfile, "pingsize", args_info->pingsize_orig,
565 0);
566 if (args_info->pingcount_given)
567 write_into_file(outfile, "pingcount", args_info->pingcount_orig,
568 0);
569 if (args_info->pingquiet_given)
570 write_into_file(outfile, "pingquiet", 0, 0);
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800571 if (args_info->no_tx_gpdu_seq_given)
572 write_into_file(outfile, "no-tx-gpdu-seq", 0, 0);
Harald Welte840a8e92017-09-24 18:12:40 +0800573 if (args_info->pdp_type_given)
574 write_into_file(outfile, "pdp-type", args_info->pdp_type_orig,
575 0);
Harald Weltef6214982017-09-24 10:23:24 +0800576
577 i = EXIT_SUCCESS;
578 return i;
579}
580
Harald Welte1b3e5772010-05-04 11:13:56 +0200581int
Harald Weltebed35df2011-11-02 13:06:18 +0100582cmdline_parser_file_save(const char *filename,
583 struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200584{
Harald Weltebed35df2011-11-02 13:06:18 +0100585 FILE *outfile;
586 int i = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200587
Harald Weltebed35df2011-11-02 13:06:18 +0100588 outfile = fopen(filename, "w");
Harald Welte1b3e5772010-05-04 11:13:56 +0200589
Harald Weltebed35df2011-11-02 13:06:18 +0100590 if (!outfile) {
591 fprintf(stderr, "%s: cannot open file for writing: %s\n",
592 CMDLINE_PARSER_PACKAGE, filename);
593 return EXIT_FAILURE;
594 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200595
Harald Weltef6214982017-09-24 10:23:24 +0800596 i = cmdline_parser_dump(outfile, args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100597 fclose(outfile);
598
Harald Weltebed35df2011-11-02 13:06:18 +0100599 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200600}
601
Harald Weltebed35df2011-11-02 13:06:18 +0100602void cmdline_parser_free(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200603{
Harald Weltebed35df2011-11-02 13:06:18 +0100604 cmdline_parser_release(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200605}
606
Harald Weltef6214982017-09-24 10:23:24 +0800607/** @brief replacement of strdup, which is not standard */
Harald Weltebed35df2011-11-02 13:06:18 +0100608char *gengetopt_strdup(const char *s)
Harald Welte1b3e5772010-05-04 11:13:56 +0200609{
Harald Weltef6214982017-09-24 10:23:24 +0800610 char *result = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100611 if (!s)
612 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200613
Harald Weltebed35df2011-11-02 13:06:18 +0100614 result = (char *)malloc(strlen(s) + 1);
615 if (result == (char *)0)
616 return (char *)0;
617 strcpy(result, s);
618 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200619}
620
Harald Weltef6214982017-09-24 10:23:24 +0800621int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200622{
Harald Weltebed35df2011-11-02 13:06:18 +0100623 return cmdline_parser2(argc, argv, args_info, 0, 1, 1);
Harald Welte1b3e5772010-05-04 11:13:56 +0200624}
625
626int
Harald Weltef6214982017-09-24 10:23:24 +0800627cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info,
628 struct cmdline_parser_params *params)
Harald Welte1b3e5772010-05-04 11:13:56 +0200629{
Harald Weltebed35df2011-11-02 13:06:18 +0100630 int result;
Harald Weltef6214982017-09-24 10:23:24 +0800631 result = cmdline_parser_internal(argc, argv, args_info, params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200632
Harald Weltef6214982017-09-24 10:23:24 +0800633 if (result == EXIT_FAILURE) {
634 cmdline_parser_free(args_info);
635 exit(EXIT_FAILURE);
636 }
637
638 return result;
639}
640
641int
642cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info,
643 int override, int initialize, int check_required)
644{
645 int result;
646 struct cmdline_parser_params params;
647
648 params.override = override;
649 params.initialize = initialize;
650 params.check_required = check_required;
651 params.check_ambiguity = 0;
652 params.print_errors = 1;
653
654 result = cmdline_parser_internal(argc, argv, args_info, &params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200655
Harald Weltebed35df2011-11-02 13:06:18 +0100656 if (result == EXIT_FAILURE) {
657 cmdline_parser_free(args_info);
658 exit(EXIT_FAILURE);
659 }
660
661 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200662}
663
664int
Harald Weltebed35df2011-11-02 13:06:18 +0100665cmdline_parser_required(struct gengetopt_args_info *args_info,
666 const char *prog_name)
Harald Welte1b3e5772010-05-04 11:13:56 +0200667{
Harald Welte73abc382017-10-10 08:50:11 +0800668 int result = EXIT_SUCCESS;
669
670 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
671 result = EXIT_FAILURE;
672
673 if (result == EXIT_FAILURE) {
674 cmdline_parser_free(args_info);
675 exit(EXIT_FAILURE);
676 }
677
678 return result;
679}
680
681int
682cmdline_parser_required2(struct gengetopt_args_info *args_info,
683 const char *prog_name, const char *additional_error)
684{
685 int error_occurred = 0;
686 FIX_UNUSED(additional_error);
687
688 /* checks for required options */
689
690 /* checks for dependences among options */
Harald Welteea0c26a2017-10-10 08:54:04 +0800691 if (args_info->net_given && !args_info->createif_given) {
692 fprintf(stderr,
693 "%s: '--net' ('-n') option depends on option 'createif'%s\n",
694 prog_name, (additional_error ? additional_error : ""));
695 error_occurred = 1;
696 }
697 if (args_info->defaultroute_given && !args_info->createif_given) {
698 fprintf(stderr,
699 "%s: '--defaultroute' option depends on option 'createif'%s\n",
700 prog_name, (additional_error ? additional_error : ""));
701 error_occurred = 1;
702 }
703 if (args_info->ipup_given && !args_info->createif_given) {
704 fprintf(stderr,
705 "%s: '--ipup' option depends on option 'createif'%s\n",
706 prog_name, (additional_error ? additional_error : ""));
707 error_occurred = 1;
708 }
709 if (args_info->ipdown_given && !args_info->createif_given) {
710 fprintf(stderr,
711 "%s: '--ipdown' option depends on option 'createif'%s\n",
712 prog_name, (additional_error ? additional_error : ""));
713 error_occurred = 1;
714 }
Harald Welte73abc382017-10-10 08:50:11 +0800715 if (args_info->tun_device_given && !args_info->createif_given) {
716 fprintf(stderr,
717 "%s: '--tun-device' option depends on option 'createif'%s\n",
718 prog_name, (additional_error ? additional_error : ""));
719 error_occurred = 1;
720 }
Andreas Schultzb6292402018-10-05 13:58:45 +0100721 if (args_info->netns_given && !args_info->createif_given) {
722 fprintf(stderr,
723 "%s: '--netns' option depends on option 'createif'%s\n",
724 prog_name, (additional_error ? additional_error : ""));
725 error_occurred = 1;
726 }
Harald Welteea0c26a2017-10-10 08:54:04 +0800727 if (args_info->pingrate_given && !args_info->pinghost_given) {
728 fprintf(stderr,
729 "%s: '--pingrate' option depends on option 'pinghost'%s\n",
730 prog_name, (additional_error ? additional_error : ""));
731 error_occurred = 1;
732 }
733 if (args_info->pingsize_given && !args_info->pinghost_given) {
734 fprintf(stderr,
735 "%s: '--pingsize' option depends on option 'pinghost'%s\n",
736 prog_name, (additional_error ? additional_error : ""));
737 error_occurred = 1;
738 }
739 if (args_info->pingcount_given && !args_info->pinghost_given) {
740 fprintf(stderr,
741 "%s: '--pingcount' option depends on option 'pinghost'%s\n",
742 prog_name, (additional_error ? additional_error : ""));
743 error_occurred = 1;
744 }
745 if (args_info->pingquiet_given && !args_info->pinghost_given) {
746 fprintf(stderr,
747 "%s: '--pingquiet' option depends on option 'pinghost'%s\n",
748 prog_name, (additional_error ? additional_error : ""));
749 error_occurred = 1;
750 }
Harald Welte73abc382017-10-10 08:50:11 +0800751
752 return error_occurred;
Harald Welte1b3e5772010-05-04 11:13:56 +0200753}
754
Harald Weltef6214982017-09-24 10:23:24 +0800755static char *package_name = 0;
756
757/**
758 * @brief updates an option
759 * @param field the generic pointer to the field to update
760 * @param orig_field the pointer to the orig field
761 * @param field_given the pointer to the number of occurrence of this option
762 * @param prev_given the pointer to the number of occurrence already seen
763 * @param value the argument for this option (if null no arg was specified)
764 * @param possible_values the possible values for this option (if specified)
765 * @param default_value the default value (in case the option only accepts fixed values)
766 * @param arg_type the type of this option
767 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
768 * @param override @see cmdline_parser_params.override
769 * @param no_free whether to free a possible previous value
770 * @param multiple_option whether this is a multiple option
771 * @param long_opt the corresponding long option
772 * @param short_opt the corresponding short option (or '-' if none)
773 * @param additional_error possible further error specification
774 */
775static
776int update_arg(void *field, char **orig_field,
777 unsigned int *field_given, unsigned int *prev_given,
778 char *value, const char *possible_values[],
779 const char *default_value,
780 cmdline_parser_arg_type arg_type,
781 int check_ambiguity, int override,
782 int no_free, int multiple_option,
783 const char *long_opt, char short_opt,
784 const char *additional_error)
785{
786 char *stop_char = 0;
787 const char *val = value;
788 int found;
789 char **string_field;
790 FIX_UNUSED(field);
791
792 stop_char = 0;
793 found = 0;
794
795 if (!multiple_option && prev_given
796 && (*prev_given || (check_ambiguity && *field_given))) {
797 if (short_opt != '-')
798 fprintf(stderr,
799 "%s: `--%s' (`-%c') option given more than once%s\n",
800 package_name, long_opt, short_opt,
801 (additional_error ? additional_error : ""));
802 else
803 fprintf(stderr,
804 "%s: `--%s' option given more than once%s\n",
805 package_name, long_opt,
806 (additional_error ? additional_error : ""));
807 return 1; /* failure */
808 }
809
810 FIX_UNUSED(default_value);
811
812 if (field_given && *field_given && !override)
813 return 0;
814 if (prev_given)
815 (*prev_given)++;
816 if (field_given)
817 (*field_given)++;
818 if (possible_values)
819 val = possible_values[found];
820
821 switch (arg_type) {
822 case ARG_FLAG:
823 *((int *)field) = !*((int *)field);
824 break;
825 case ARG_INT:
826 if (val)
827 *((int *)field) = strtol(val, &stop_char, 0);
828 break;
829 case ARG_STRING:
830 if (val) {
831 string_field = (char **)field;
832 if (!no_free && *string_field)
833 free(*string_field); /* free previous string */
834 *string_field = gengetopt_strdup(val);
835 }
836 break;
837 default:
838 break;
839 };
840
841 /* check numeric conversion */
842 switch (arg_type) {
843 case ARG_INT:
844 if (val && !(stop_char && *stop_char == '\0')) {
845 fprintf(stderr, "%s: invalid numeric value: %s\n",
846 package_name, val);
847 return 1; /* failure */
848 }
849 break;
850 default:
851 ;
852 };
853
854 /* store the original value */
855 switch (arg_type) {
856 case ARG_NO:
857 case ARG_FLAG:
858 break;
859 default:
860 if (value && orig_field) {
861 if (no_free) {
862 *orig_field = value;
863 } else {
864 if (*orig_field)
865 free(*orig_field); /* free previous string */
866 *orig_field = gengetopt_strdup(value);
867 }
868 }
869 };
870
871 return 0; /* OK */
872}
873
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +0200874static int check_modes(int given1[], const char *options1[],
875 int given2[], const char *options2[])
876{
877 int i = 0, j = 0, errors = 0;
878
879 while (given1[i] >= 0) {
880 if (given1[i]) {
881 while (given2[j] >= 0) {
882 if (given2[j]) {
883 ++errors;
884 fprintf(stderr,
885 "%s: option %s conflicts with option %s\n",
886 package_name, options1[i],
887 options2[j]);
888 }
889 ++j;
890 }
891 }
892 ++i;
893 }
894
895 return errors;
896}
897
Harald Welte1b3e5772010-05-04 11:13:56 +0200898int
Harald Weltef6214982017-09-24 10:23:24 +0800899cmdline_parser_internal(int argc, char **argv,
900 struct gengetopt_args_info *args_info,
901 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +0100902 const char *additional_error)
Harald Welte1b3e5772010-05-04 11:13:56 +0200903{
Harald Weltebed35df2011-11-02 13:06:18 +0100904 int c; /* Character of the parsed option. */
Harald Welte1b3e5772010-05-04 11:13:56 +0200905
Harald Weltef6214982017-09-24 10:23:24 +0800906 int error_occurred = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100907 struct gengetopt_args_info local_args_info;
Harald Welte1b3e5772010-05-04 11:13:56 +0200908
Harald Weltef6214982017-09-24 10:23:24 +0800909 int override;
910 int initialize;
911 int check_required;
912 int check_ambiguity;
913
914 package_name = argv[0];
915
916 override = params->override;
917 initialize = params->initialize;
918 check_required = params->check_required;
919 check_ambiguity = params->check_ambiguity;
920
Harald Weltebed35df2011-11-02 13:06:18 +0100921 if (initialize)
922 cmdline_parser_init(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200923
Harald Weltebed35df2011-11-02 13:06:18 +0100924 cmdline_parser_init(&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000925
Harald Weltebed35df2011-11-02 13:06:18 +0100926 optarg = 0;
927 optind = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800928 opterr = params->print_errors;
Harald Weltebed35df2011-11-02 13:06:18 +0100929 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000930
Harald Weltebed35df2011-11-02 13:06:18 +0100931 while (1) {
932 int option_index = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200933
Harald Weltebed35df2011-11-02 13:06:18 +0100934 static struct option long_options[] = {
935 {"help", 0, NULL, 'h'},
936 {"version", 0, NULL, 'V'},
937 {"debug", 0, NULL, 'd'},
938 {"conf", 1, NULL, 'c'},
939 {"pidfile", 1, NULL, 0},
940 {"statedir", 1, NULL, 0},
941 {"dns", 1, NULL, 0},
942 {"listen", 1, NULL, 'l'},
943 {"remote", 1, NULL, 'r'},
944 {"contexts", 1, NULL, 0},
945 {"timelimit", 1, NULL, 0},
946 {"gtpversion", 1, NULL, 0},
947 {"apn", 1, NULL, 'a'},
948 {"selmode", 1, NULL, 0},
949 {"rattype", 1, NULL, 0},
950 {"userloc", 1, NULL, 0},
951 {"rai", 1, NULL, 0},
952 {"mstz", 1, NULL, 0},
953 {"imeisv", 1, NULL, 0},
Harald Weltef6214982017-09-24 10:23:24 +0800954 {"norecovery", 0, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100955 {"imsi", 1, NULL, 'i'},
956 {"nsapi", 1, NULL, 0},
957 {"msisdn", 1, NULL, 'm'},
958 {"qos", 1, NULL, 'q'},
959 {"qose1", 1, NULL, 0},
960 {"qose2", 1, NULL, 0},
961 {"qose3", 1, NULL, 0},
962 {"qose4", 1, NULL, 0},
963 {"charging", 1, NULL, 0},
964 {"uid", 1, NULL, 'u'},
965 {"pwd", 1, NULL, 'p'},
966 {"createif", 0, NULL, 0},
967 {"net", 1, NULL, 'n'},
968 {"defaultroute", 0, NULL, 0},
969 {"ipup", 1, NULL, 0},
970 {"ipdown", 1, NULL, 0},
Harald Welte73abc382017-10-10 08:50:11 +0800971 {"tun-device", 1, NULL, 0},
Andreas Schultzb6292402018-10-05 13:58:45 +0100972 {"netns", 1, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100973 {"pinghost", 1, NULL, 0},
974 {"pingrate", 1, NULL, 0},
975 {"pingsize", 1, NULL, 0},
976 {"pingcount", 1, NULL, 0},
977 {"pingquiet", 0, NULL, 0},
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800978 {"no-tx-gpdu-seq", 0, NULL, 0},
Harald Welte840a8e92017-09-24 18:12:40 +0800979 {"pdp-type", 1, NULL, 't'},
Harald Weltef6214982017-09-24 10:23:24 +0800980 {0, 0, 0, 0}
Harald Weltebed35df2011-11-02 13:06:18 +0100981 };
jjako52c24142002-12-16 13:33:51 +0000982
Harald Welte840a8e92017-09-24 18:12:40 +0800983 c = getopt_long(argc, argv, "hVdc:l:r:a:i:m:q:u:p:n:t:",
Harald Weltebed35df2011-11-02 13:06:18 +0100984 long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000985
Harald Weltebed35df2011-11-02 13:06:18 +0100986 if (c == -1)
987 break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000988
Harald Weltebed35df2011-11-02 13:06:18 +0100989 switch (c) {
990 case 'h': /* Print help and exit. */
991 cmdline_parser_print_help();
992 cmdline_parser_free(&local_args_info);
993 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000994
Harald Weltebed35df2011-11-02 13:06:18 +0100995 case 'V': /* Print version and exit. */
996 cmdline_parser_print_version();
997 cmdline_parser_free(&local_args_info);
998 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000999
Harald Weltebed35df2011-11-02 13:06:18 +01001000 case 'd': /* Run in debug mode. */
jjako52c24142002-12-16 13:33:51 +00001001
Harald Weltef6214982017-09-24 10:23:24 +08001002 if (update_arg
1003 ((void *)&(args_info->debug_flag), 0,
1004 &(args_info->debug_given),
1005 &(local_args_info.debug_given), optarg, 0, 0,
1006 ARG_FLAG, check_ambiguity, override, 1, 0, "debug",
1007 'd', additional_error))
1008 goto failure;
1009
1010 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001011 case 'c': /* Read configuration file. */
jjako52c24142002-12-16 13:33:51 +00001012
Harald Weltef6214982017-09-24 10:23:24 +08001013 if (update_arg((void *)&(args_info->conf_arg),
1014 &(args_info->conf_orig),
1015 &(args_info->conf_given),
1016 &(local_args_info.conf_given), optarg, 0,
1017 0, ARG_STRING, check_ambiguity, override,
1018 0, 0, "conf", 'c', additional_error))
1019 goto failure;
1020
1021 break;
Pau Espin Pedrol5fdda132018-02-12 18:03:09 +01001022 case 'l': /* Local host. */
jjako52c24142002-12-16 13:33:51 +00001023
Harald Weltef6214982017-09-24 10:23:24 +08001024 if (update_arg((void *)&(args_info->listen_arg),
1025 &(args_info->listen_orig),
1026 &(args_info->listen_given),
1027 &(local_args_info.listen_given), optarg,
1028 0, 0, ARG_STRING, check_ambiguity,
1029 override, 0, 0, "listen", 'l',
1030 additional_error))
1031 goto failure;
1032
1033 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001034 case 'r': /* Remote host. */
jjako52c24142002-12-16 13:33:51 +00001035
Harald Weltef6214982017-09-24 10:23:24 +08001036 if (update_arg((void *)&(args_info->remote_arg),
1037 &(args_info->remote_orig),
1038 &(args_info->remote_given),
1039 &(local_args_info.remote_given), optarg,
1040 0, 0, ARG_STRING, check_ambiguity,
1041 override, 0, 0, "remote", 'r',
1042 additional_error))
1043 goto failure;
1044
1045 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001046 case 'a': /* Access point name. */
jjako52c24142002-12-16 13:33:51 +00001047
Harald Weltef6214982017-09-24 10:23:24 +08001048 if (update_arg((void *)&(args_info->apn_arg),
1049 &(args_info->apn_orig),
1050 &(args_info->apn_given),
1051 &(local_args_info.apn_given), optarg, 0,
1052 "internet", ARG_STRING, check_ambiguity,
1053 override, 0, 0, "apn", 'a',
1054 additional_error))
1055 goto failure;
1056
1057 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001058 case 'i': /* IMSI. */
jjako52c24142002-12-16 13:33:51 +00001059
Harald Weltef6214982017-09-24 10:23:24 +08001060 if (update_arg((void *)&(args_info->imsi_arg),
1061 &(args_info->imsi_orig),
1062 &(args_info->imsi_given),
1063 &(local_args_info.imsi_given), optarg, 0,
1064 "240010123456789", ARG_STRING,
1065 check_ambiguity, override, 0, 0, "imsi",
1066 'i', additional_error))
1067 goto failure;
1068
1069 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001070 case 'm': /* Mobile Station ISDN number. */
jjako52c24142002-12-16 13:33:51 +00001071
Harald Weltef6214982017-09-24 10:23:24 +08001072 if (update_arg((void *)&(args_info->msisdn_arg),
1073 &(args_info->msisdn_orig),
1074 &(args_info->msisdn_given),
1075 &(local_args_info.msisdn_given), optarg,
1076 0, "46702123456", ARG_STRING,
1077 check_ambiguity, override, 0, 0,
1078 "msisdn", 'm', additional_error))
1079 goto failure;
1080
1081 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001082 case 'q': /* Requested quality of service. */
jjako52c24142002-12-16 13:33:51 +00001083
Harald Weltef6214982017-09-24 10:23:24 +08001084 if (update_arg((void *)&(args_info->qos_arg),
1085 &(args_info->qos_orig),
1086 &(args_info->qos_given),
1087 &(local_args_info.qos_given), optarg, 0,
1088 "0x000b921f", ARG_INT, check_ambiguity,
1089 override, 0, 0, "qos", 'q',
1090 additional_error))
1091 goto failure;
1092
1093 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001094 case 'u': /* Login user ID. */
jjako52c24142002-12-16 13:33:51 +00001095
Harald Weltef6214982017-09-24 10:23:24 +08001096 if (update_arg((void *)&(args_info->uid_arg),
1097 &(args_info->uid_orig),
1098 &(args_info->uid_given),
1099 &(local_args_info.uid_given), optarg, 0,
1100 "mig", ARG_STRING, check_ambiguity,
1101 override, 0, 0, "uid", 'u',
1102 additional_error))
1103 goto failure;
1104
1105 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001106 case 'p': /* Login password. */
jjako52c24142002-12-16 13:33:51 +00001107
Harald Weltef6214982017-09-24 10:23:24 +08001108 if (update_arg((void *)&(args_info->pwd_arg),
1109 &(args_info->pwd_orig),
1110 &(args_info->pwd_given),
1111 &(local_args_info.pwd_given), optarg, 0,
1112 "hemmelig", ARG_STRING, check_ambiguity,
1113 override, 0, 0, "pwd", 'p',
1114 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001115 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001116
1117 break;
1118 case 'n': /* Network address for local interface. */
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001119 args_info->createif_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001120
1121 if (update_arg((void *)&(args_info->net_arg),
1122 &(args_info->net_orig),
1123 &(args_info->net_given),
1124 &(local_args_info.net_given), optarg, 0,
1125 0, ARG_STRING, check_ambiguity, override,
1126 0, 0, "net", 'n', additional_error))
1127 goto failure;
1128
Harald Weltebed35df2011-11-02 13:06:18 +01001129 break;
Harald Welte840a8e92017-09-24 18:12:40 +08001130 case 't': /* PDP Type. */
1131
1132 if (update_arg((void *)&(args_info->pdp_type_arg),
1133 &(args_info->pdp_type_orig),
1134 &(args_info->pdp_type_given),
1135 &(local_args_info.pdp_type_given),
1136 optarg, 0, "v4", ARG_STRING,
1137 check_ambiguity, override, 0, 0,
1138 "pdp-type", 't', additional_error))
1139 goto failure;
1140
1141 break;
jjako193e8b12003-11-10 12:31:41 +00001142
Harald Weltebed35df2011-11-02 13:06:18 +01001143 case 0: /* Long option with no short option */
1144 /* Filename of process id file. */
1145 if (strcmp(long_options[option_index].name, "pidfile")
1146 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001147
1148 if (update_arg
1149 ((void *)&(args_info->pidfile_arg),
1150 &(args_info->pidfile_orig),
1151 &(args_info->pidfile_given),
1152 &(local_args_info.pidfile_given), optarg,
1153 0, "./sgsnemu.pid", ARG_STRING,
1154 check_ambiguity, override, 0, 0, "pidfile",
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 /* Directory of nonvolatile data. */
1160 else if (strcmp
1161 (long_options[option_index].name,
1162 "statedir") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001163
1164 if (update_arg
1165 ((void *)&(args_info->statedir_arg),
1166 &(args_info->statedir_orig),
1167 &(args_info->statedir_given),
1168 &(local_args_info.statedir_given), optarg,
1169 0, "./", ARG_STRING, check_ambiguity,
1170 override, 0, 0, "statedir", '-',
1171 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001172 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001173
Harald Weltebed35df2011-11-02 13:06:18 +01001174 }
1175 /* DNS Server to use. */
1176 else if (strcmp(long_options[option_index].name, "dns")
1177 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001178
1179 if (update_arg((void *)&(args_info->dns_arg),
1180 &(args_info->dns_orig),
1181 &(args_info->dns_given),
1182 &(local_args_info.dns_given),
1183 optarg, 0, 0, ARG_STRING,
1184 check_ambiguity, override, 0, 0,
1185 "dns", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001186 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001187
Harald Weltebed35df2011-11-02 13:06:18 +01001188 }
1189 /* Number of contexts. */
1190 else if (strcmp
1191 (long_options[option_index].name,
1192 "contexts") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001193
1194 if (update_arg
1195 ((void *)&(args_info->contexts_arg),
1196 &(args_info->contexts_orig),
1197 &(args_info->contexts_given),
1198 &(local_args_info.contexts_given), optarg,
1199 0, "1", ARG_INT, check_ambiguity, override,
1200 0, 0, "contexts", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001201 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001202
Harald Weltebed35df2011-11-02 13:06:18 +01001203 }
1204 /* Exit after timelimit seconds. */
1205 else if (strcmp
1206 (long_options[option_index].name,
1207 "timelimit") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001208
1209 if (update_arg
1210 ((void *)&(args_info->timelimit_arg),
1211 &(args_info->timelimit_orig),
1212 &(args_info->timelimit_given),
1213 &(local_args_info.timelimit_given), optarg,
1214 0, "0", ARG_INT, check_ambiguity, override,
1215 0, 0, "timelimit", '-', 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 /* GTP version to use. */
1220 else if (strcmp
1221 (long_options[option_index].name,
1222 "gtpversion") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001223
1224 if (update_arg
1225 ((void *)&(args_info->gtpversion_arg),
1226 &(args_info->gtpversion_orig),
1227 &(args_info->gtpversion_given),
1228 &(local_args_info.gtpversion_given),
1229 optarg, 0, "1", ARG_INT, check_ambiguity,
1230 override, 0, 0, "gtpversion", '-',
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 /* Selection mode. */
1236 else if (strcmp
1237 (long_options[option_index].name,
1238 "selmode") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001239
1240 if (update_arg
1241 ((void *)&(args_info->selmode_arg),
1242 &(args_info->selmode_orig),
1243 &(args_info->selmode_given),
1244 &(local_args_info.selmode_given), optarg,
1245 0, "0x01", ARG_INT, check_ambiguity,
1246 override, 0, 0, "selmode", '-',
1247 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001248 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001249
Harald Weltebed35df2011-11-02 13:06:18 +01001250 }
1251 /* Radio Access Technology Type. */
1252 else if (strcmp
1253 (long_options[option_index].name,
1254 "rattype") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001255
1256 if (update_arg
1257 ((void *)&(args_info->rattype_arg),
1258 &(args_info->rattype_orig),
1259 &(args_info->rattype_given),
1260 &(local_args_info.rattype_given), optarg,
1261 0, "1", ARG_INT, check_ambiguity, override,
1262 0, 0, "rattype", '-', additional_error))
1263 goto failure;
1264
Harald Weltebed35df2011-11-02 13:06:18 +01001265 }
1266 /* User Location Information. */
1267 else if (strcmp
1268 (long_options[option_index].name,
1269 "userloc") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001270
1271 if (update_arg
1272 ((void *)&(args_info->userloc_arg),
1273 &(args_info->userloc_orig),
1274 &(args_info->userloc_given),
1275 &(local_args_info.userloc_given), optarg,
1276 0, "02509946241207", ARG_STRING,
1277 check_ambiguity, override, 0, 0, "userloc",
1278 '-', additional_error))
1279 goto failure;
1280
Harald Weltebed35df2011-11-02 13:06:18 +01001281 }
1282 /* Routing Area Information. */
1283 else if (strcmp(long_options[option_index].name, "rai")
1284 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001285
1286 if (update_arg((void *)&(args_info->rai_arg),
1287 &(args_info->rai_orig),
1288 &(args_info->rai_given),
1289 &(local_args_info.rai_given),
1290 optarg, 0, "02509946241207",
1291 ARG_STRING, check_ambiguity,
1292 override, 0, 0, "rai", '-',
1293 additional_error))
1294 goto failure;
1295
Harald Weltebed35df2011-11-02 13:06:18 +01001296 }
Harald Weltef6214982017-09-24 10:23:24 +08001297 /* MS Time Zone. */
Harald Weltebed35df2011-11-02 13:06:18 +01001298 else if (strcmp(long_options[option_index].name, "mstz")
1299 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001300
1301 if (update_arg((void *)&(args_info->mstz_arg),
1302 &(args_info->mstz_orig),
1303 &(args_info->mstz_given),
1304 &(local_args_info.mstz_given),
1305 optarg, 0, "0", ARG_STRING,
1306 check_ambiguity, override, 0, 0,
1307 "mstz", '-', additional_error))
1308 goto failure;
1309
Harald Weltebed35df2011-11-02 13:06:18 +01001310 }
Harald Weltef6214982017-09-24 10:23:24 +08001311 /* IMEI(SV) International Mobile Equipment Identity (and Software Version). */
Harald Weltebed35df2011-11-02 13:06:18 +01001312 else if (strcmp
1313 (long_options[option_index].name,
1314 "imeisv") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001315
1316 if (update_arg((void *)&(args_info->imeisv_arg),
1317 &(args_info->imeisv_orig),
1318 &(args_info->imeisv_given),
1319 &(local_args_info.imeisv_given),
1320 optarg, 0, "2143658709214365",
1321 ARG_STRING, check_ambiguity,
1322 override, 0, 0, "imeisv", '-',
1323 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001324 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001325
Harald Weltebed35df2011-11-02 13:06:18 +01001326 }
1327 /* Do not send recovery. */
1328 else if (strcmp
1329 (long_options[option_index].name,
1330 "norecovery") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001331
1332 if (update_arg
1333 ((void *)&(args_info->norecovery_flag), 0,
1334 &(args_info->norecovery_given),
1335 &(local_args_info.norecovery_given),
1336 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1337 override, 1, 0, "norecovery", '-',
1338 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001339 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001340
1341 }
1342 /* NSAPI. */
1343 else if (strcmp
1344 (long_options[option_index].name,
1345 "nsapi") == 0) {
1346
1347 if (update_arg((void *)&(args_info->nsapi_arg),
1348 &(args_info->nsapi_orig),
1349 &(args_info->nsapi_given),
1350 &(local_args_info.nsapi_given),
1351 optarg, 0, "0", ARG_INT,
1352 check_ambiguity, override, 0, 0,
1353 "nsapi", '-', additional_error))
1354 goto failure;
1355
1356 }
1357 /* Requested quality of service Extension 1. */
1358 else if (strcmp
1359 (long_options[option_index].name,
1360 "qose1") == 0) {
1361
1362 if (update_arg((void *)&(args_info->qose1_arg),
1363 &(args_info->qose1_orig),
1364 &(args_info->qose1_given),
1365 &(local_args_info.qose1_given),
1366 optarg, 0, "0x9396404074f9ffff",
1367 ARG_INT, check_ambiguity,
1368 override, 0, 0, "qose1", '-',
1369 additional_error))
1370 goto failure;
1371
1372 }
1373 /* Requested quality of service Extension 2. */
1374 else if (strcmp
1375 (long_options[option_index].name,
1376 "qose2") == 0) {
1377
1378 if (update_arg((void *)&(args_info->qose2_arg),
1379 &(args_info->qose2_orig),
1380 &(args_info->qose2_given),
1381 &(local_args_info.qose2_given),
1382 optarg, 0, "0x11", ARG_INT,
1383 check_ambiguity, override, 0, 0,
1384 "qose2", '-', additional_error))
1385 goto failure;
1386
1387 }
1388 /* Requested quality of service Extension 3. */
1389 else if (strcmp
1390 (long_options[option_index].name,
1391 "qose3") == 0) {
1392
1393 if (update_arg((void *)&(args_info->qose3_arg),
1394 &(args_info->qose3_orig),
1395 &(args_info->qose3_given),
1396 &(local_args_info.qose3_given),
1397 optarg, 0, "0x0101", ARG_INT,
1398 check_ambiguity, override, 0, 0,
1399 "qose3", '-', additional_error))
1400 goto failure;
1401
1402 }
1403 /* Requested quality of service Extension 4. */
1404 else if (strcmp
1405 (long_options[option_index].name,
1406 "qose4") == 0) {
1407
1408 if (update_arg((void *)&(args_info->qose4_arg),
1409 &(args_info->qose4_orig),
1410 &(args_info->qose4_given),
1411 &(local_args_info.qose4_given),
1412 optarg, 0, "0x4040", ARG_INT,
1413 check_ambiguity, override, 0, 0,
1414 "qose4", '-', additional_error))
1415 goto failure;
1416
1417 }
1418 /* Charging characteristics. */
1419 else if (strcmp
1420 (long_options[option_index].name,
1421 "charging") == 0) {
1422
1423 if (update_arg
1424 ((void *)&(args_info->charging_arg),
1425 &(args_info->charging_orig),
1426 &(args_info->charging_given),
1427 &(local_args_info.charging_given), optarg,
1428 0, "0x0800", ARG_INT, check_ambiguity,
1429 override, 0, 0, "charging", '-',
1430 additional_error))
1431 goto failure;
1432
1433 }
1434 /* Create local network interface. */
1435 else if (strcmp
1436 (long_options[option_index].name,
1437 "createif") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001438 args_info->createif_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001439
1440 if (update_arg
1441 ((void *)&(args_info->createif_flag), 0,
1442 &(args_info->createif_given),
1443 &(local_args_info.createif_given), optarg,
1444 0, 0, ARG_FLAG, check_ambiguity, override,
1445 1, 0, "createif", '-', additional_error))
1446 goto failure;
1447
1448 }
1449 /* Create default route. */
1450 else if (strcmp
1451 (long_options[option_index].name,
1452 "defaultroute") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001453 args_info->createif_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001454
1455 if (update_arg
1456 ((void *)&(args_info->defaultroute_flag), 0,
1457 &(args_info->defaultroute_given),
1458 &(local_args_info.defaultroute_given),
1459 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1460 override, 1, 0, "defaultroute", '-',
1461 additional_error))
1462 goto failure;
1463
1464 }
1465 /* Script to run after link-up. */
1466 else if (strcmp(long_options[option_index].name, "ipup")
1467 == 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->ipup_arg),
1471 &(args_info->ipup_orig),
1472 &(args_info->ipup_given),
1473 &(local_args_info.ipup_given),
1474 optarg, 0, 0, ARG_STRING,
1475 check_ambiguity, override, 0, 0,
1476 "ipup", '-', additional_error))
1477 goto failure;
1478
1479 }
1480 /* Script to run after link-down. */
1481 else if (strcmp
1482 (long_options[option_index].name,
1483 "ipdown") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001484 args_info->createif_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001485
1486 if (update_arg((void *)&(args_info->ipdown_arg),
1487 &(args_info->ipdown_orig),
1488 &(args_info->ipdown_given),
1489 &(local_args_info.ipdown_given),
1490 optarg, 0, 0, ARG_STRING,
1491 check_ambiguity, override, 0, 0,
1492 "ipdown", '-', additional_error))
1493 goto failure;
1494
1495 }
Harald Welte73abc382017-10-10 08:50:11 +08001496 /* Name of the local network interface. */
1497 else if (strcmp
1498 (long_options[option_index].name,
1499 "tun-device") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001500 args_info->createif_mode_counter += 1;
Harald Welte73abc382017-10-10 08:50:11 +08001501
1502 if (update_arg
1503 ((void *)&(args_info->tun_device_arg),
1504 &(args_info->tun_device_orig),
1505 &(args_info->tun_device_given),
1506 &(local_args_info.tun_device_given),
1507 optarg, 0, 0, ARG_STRING, check_ambiguity,
1508 override, 0, 0, "tun-device", '-',
1509 additional_error))
1510 goto failure;
1511
1512 }
Andreas Schultzb6292402018-10-05 13:58:45 +01001513 /* Network namespace to use. */
1514 else if (strcmp
1515 (long_options[option_index].name,
1516 "netns") == 0) {
1517 args_info->createif_mode_counter += 1;
1518
1519 if (update_arg((void *)&(args_info->netns_arg),
1520 &(args_info->netns_orig),
1521 &(args_info->netns_given),
1522 &(local_args_info.netns_given),
1523 optarg, 0, 0, ARG_STRING,
1524 check_ambiguity, override, 0, 0,
1525 "netns", '-', additional_error))
1526 goto failure;
1527
1528 }
Harald Weltef6214982017-09-24 10:23:24 +08001529 /* Ping remote host. */
1530 else if (strcmp
1531 (long_options[option_index].name,
1532 "pinghost") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001533 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001534
1535 if (update_arg
1536 ((void *)&(args_info->pinghost_arg),
1537 &(args_info->pinghost_orig),
1538 &(args_info->pinghost_given),
1539 &(local_args_info.pinghost_given), optarg,
1540 0, 0, ARG_STRING, check_ambiguity,
1541 override, 0, 0, "pinghost", '-',
1542 additional_error))
1543 goto failure;
1544
1545 }
1546 /* Number of ping req per second. */
1547 else if (strcmp
1548 (long_options[option_index].name,
1549 "pingrate") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001550 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001551
1552 if (update_arg
1553 ((void *)&(args_info->pingrate_arg),
1554 &(args_info->pingrate_orig),
1555 &(args_info->pingrate_given),
1556 &(local_args_info.pingrate_given), optarg,
1557 0, "1", ARG_INT, check_ambiguity, override,
1558 0, 0, "pingrate", '-', additional_error))
1559 goto failure;
1560
1561 }
1562 /* Number of ping data bytes. */
1563 else if (strcmp
1564 (long_options[option_index].name,
1565 "pingsize") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001566 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001567
1568 if (update_arg
1569 ((void *)&(args_info->pingsize_arg),
1570 &(args_info->pingsize_orig),
1571 &(args_info->pingsize_given),
1572 &(local_args_info.pingsize_given), optarg,
1573 0, "56", ARG_INT, check_ambiguity,
1574 override, 0, 0, "pingsize", '-',
1575 additional_error))
1576 goto failure;
1577
1578 }
1579 /* Number of ping req to send. */
1580 else if (strcmp
1581 (long_options[option_index].name,
1582 "pingcount") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001583 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001584
1585 if (update_arg
1586 ((void *)&(args_info->pingcount_arg),
1587 &(args_info->pingcount_orig),
1588 &(args_info->pingcount_given),
1589 &(local_args_info.pingcount_given), optarg,
1590 0, "0", ARG_INT, check_ambiguity, override,
1591 0, 0, "pingcount", '-', additional_error))
1592 goto failure;
1593
1594 }
1595 /* Do not print ping packet info. */
1596 else if (strcmp
1597 (long_options[option_index].name,
1598 "pingquiet") == 0) {
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001599 args_info->pinghost_mode_counter += 1;
Harald Weltef6214982017-09-24 10:23:24 +08001600
1601 if (update_arg
1602 ((void *)&(args_info->pingquiet_flag), 0,
1603 &(args_info->pingquiet_given),
1604 &(local_args_info.pingquiet_given), optarg,
1605 0, 0, ARG_FLAG, check_ambiguity, override,
1606 1, 0, "pingquiet", '-', additional_error))
1607 goto failure;
1608
Harald Weltebed35df2011-11-02 13:06:18 +01001609 }
Harald Weltefbb9c7f2017-09-24 11:50:20 +08001610 /* Don't transmit G-PDU sequence nums. */
1611 else if (strcmp
1612 (long_options[option_index].name,
1613 "no-tx-gpdu-seq") == 0) {
1614
1615 if (update_arg
1616 ((void *)&(args_info->no_tx_gpdu_seq_flag),
1617 0, &(args_info->no_tx_gpdu_seq_given),
1618 &(local_args_info.no_tx_gpdu_seq_given),
1619 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1620 override, 1, 0, "no-tx-gpdu-seq", '-',
1621 additional_error))
1622 goto failure;
1623
1624 }
jjako52c24142002-12-16 13:33:51 +00001625
Harald Weltebed35df2011-11-02 13:06:18 +01001626 break;
1627 case '?': /* Invalid option. */
1628 /* `getopt_long' already printed an error message. */
1629 goto failure;
jjako52c24142002-12-16 13:33:51 +00001630
Harald Weltebed35df2011-11-02 13:06:18 +01001631 default: /* bug: option not considered. */
1632 fprintf(stderr, "%s: option unknown: %c%s\n",
1633 CMDLINE_PARSER_PACKAGE, c,
1634 (additional_error ? additional_error : ""));
1635 abort();
1636 } /* switch */
1637 } /* while */
jjako52c24142002-12-16 13:33:51 +00001638
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001639 if (args_info->createif_mode_counter
1640 && args_info->pinghost_mode_counter) {
1641 int createif_given[] =
1642 { args_info->createif_given, args_info->net_given,
1643 args_info->defaultroute_given, args_info->ipup_given,
Andreas Schultzb6292402018-10-05 13:58:45 +01001644 args_info->ipdown_given, args_info->tun_device_given,
1645 args_info->netns_given, -1
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001646 };
1647 const char *createif_desc[] =
1648 { "--createif", "--net", "--defaultroute", "--ipup",
Andreas Schultzb6292402018-10-05 13:58:45 +01001649 "--ipdown", "--tun-device", "--netns", 0
Viktor Tsymbalyukab4db102018-01-30 14:40:12 +02001650 };
1651 int pinghost_given[] =
1652 { args_info->pinghost_given, args_info->pingrate_given,
1653 args_info->pingsize_given, args_info->pingcount_given,
1654 args_info->pingquiet_given, -1
1655 };
1656 const char *pinghost_desc[] =
1657 { "--pinghost", "--pingrate", "--pingsize", "--pingcount",
1658 "--pingquiet", 0
1659 };
1660 error_occurred +=
1661 check_modes(createif_given, createif_desc, pinghost_given,
1662 pinghost_desc);
1663 }
1664
Harald Welte73abc382017-10-10 08:50:11 +08001665 if (check_required) {
1666 error_occurred +=
1667 cmdline_parser_required2(args_info, argv[0],
1668 additional_error);
1669 }
1670
Harald Weltebed35df2011-11-02 13:06:18 +01001671 cmdline_parser_release(&local_args_info);
jjako52c24142002-12-16 13:33:51 +00001672
Harald Weltef6214982017-09-24 10:23:24 +08001673 if (error_occurred)
Harald Weltebed35df2011-11-02 13:06:18 +01001674 return (EXIT_FAILURE);
Harald Welte1b3e5772010-05-04 11:13:56 +02001675
Harald Weltebed35df2011-11-02 13:06:18 +01001676 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001677
1678failure:
Harald Weltebed35df2011-11-02 13:06:18 +01001679
1680 cmdline_parser_release(&local_args_info);
1681 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +00001682}
1683
Harald Welte1b3e5772010-05-04 11:13:56 +02001684#ifndef CONFIG_FILE_LINE_SIZE
1685#define CONFIG_FILE_LINE_SIZE 2048
1686#endif
1687#define ADDITIONAL_ERROR " in configuration file "
1688
1689#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1690/* 3 is for "--" and "=" */
1691
Harald Weltef6214982017-09-24 10:23:24 +08001692static int _cmdline_parser_configfile(const char *filename, int *my_argc)
jjako52c24142002-12-16 13:33:51 +00001693{
Harald Weltebed35df2011-11-02 13:06:18 +01001694 FILE *file;
Harald Weltef6214982017-09-24 10:23:24 +08001695 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE + 1];
Harald Weltebed35df2011-11-02 13:06:18 +01001696 char linebuf[CONFIG_FILE_LINE_SIZE];
1697 int line_num = 0;
Harald Weltef6214982017-09-24 10:23:24 +08001698 int result = 0, equal;
Harald Weltebed35df2011-11-02 13:06:18 +01001699 char *fopt, *farg;
1700 char *str_index;
1701 size_t len, next_token;
1702 char delimiter;
Harald Welte1b3e5772010-05-04 11:13:56 +02001703
Harald Weltef6214982017-09-24 10:23:24 +08001704 if ((file = fopen(filename, "r")) == 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001705 fprintf(stderr, "%s: Error opening configuration file '%s'\n",
1706 CMDLINE_PARSER_PACKAGE, filename);
Harald Weltef6214982017-09-24 10:23:24 +08001707 return EXIT_FAILURE;
Harald Weltebed35df2011-11-02 13:06:18 +01001708 }
jjako52c24142002-12-16 13:33:51 +00001709
Harald Weltef6214982017-09-24 10:23:24 +08001710 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001711 ++line_num;
1712 my_argv[0] = '\0';
1713 len = strlen(linebuf);
1714 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE - 1)) {
1715 fprintf(stderr,
1716 "%s:%s:%d: Line too long in configuration file\n",
1717 CMDLINE_PARSER_PACKAGE, filename, line_num);
1718 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001719 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001720 }
jjako52c24142002-12-16 13:33:51 +00001721
Harald Weltebed35df2011-11-02 13:06:18 +01001722 /* find first non-whitespace character in the line */
1723 next_token = strspn(linebuf, " \t\r\n");
1724 str_index = linebuf + next_token;
jjako52c24142002-12-16 13:33:51 +00001725
Harald Weltebed35df2011-11-02 13:06:18 +01001726 if (str_index[0] == '\0' || str_index[0] == '#')
1727 continue; /* empty line or comment line is skipped */
Harald Welte1b3e5772010-05-04 11:13:56 +02001728
Harald Weltebed35df2011-11-02 13:06:18 +01001729 fopt = str_index;
Harald Welte1b3e5772010-05-04 11:13:56 +02001730
Harald Weltebed35df2011-11-02 13:06:18 +01001731 /* truncate fopt at the end of the first non-valid character */
1732 next_token = strcspn(fopt, " \t\r\n=");
Harald Welte1b3e5772010-05-04 11:13:56 +02001733
Harald Weltebed35df2011-11-02 13:06:18 +01001734 if (fopt[next_token] == '\0') { /* the line is over */
Harald Weltef6214982017-09-24 10:23:24 +08001735 farg = 0;
Harald Weltebed35df2011-11-02 13:06:18 +01001736 equal = 0;
1737 goto noarg;
1738 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001739
Harald Weltebed35df2011-11-02 13:06:18 +01001740 /* remember if equal sign is present */
1741 equal = (fopt[next_token] == '=');
1742 fopt[next_token++] = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001743
Harald Weltebed35df2011-11-02 13:06:18 +01001744 /* advance pointers to the next token after the end of fopt */
1745 next_token += strspn(fopt + next_token, " \t\r\n");
Harald Weltef6214982017-09-24 10:23:24 +08001746
Harald Weltebed35df2011-11-02 13:06:18 +01001747 /* check for the presence of equal sign, and if so, skip it */
1748 if (!equal)
1749 if ((equal = (fopt[next_token] == '='))) {
1750 next_token++;
1751 next_token +=
1752 strspn(fopt + next_token, " \t\r\n");
1753 }
1754 str_index += next_token;
Harald Welte1b3e5772010-05-04 11:13:56 +02001755
Harald Weltebed35df2011-11-02 13:06:18 +01001756 /* find argument */
1757 farg = str_index;
1758 if (farg[0] == '\"' || farg[0] == '\'') { /* quoted argument */
1759 str_index = strchr(++farg, str_index[0]); /* skip opening quote */
1760 if (!str_index) {
1761 fprintf
1762 (stderr,
1763 "%s:%s:%d: unterminated string in configuration file\n",
1764 CMDLINE_PARSER_PACKAGE, filename,
1765 line_num);
1766 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001767 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001768 }
1769 } else { /* read up the remaining part up to a delimiter */
1770 next_token = strcspn(farg, " \t\r\n#\'\"");
1771 str_index += next_token;
1772 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001773
Harald Weltebed35df2011-11-02 13:06:18 +01001774 /* truncate farg at the delimiter and store it for further check */
1775 delimiter = *str_index, *str_index++ = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001776
Harald Weltebed35df2011-11-02 13:06:18 +01001777 /* everything but comment is illegal at the end of line */
1778 if (delimiter != '\0' && delimiter != '#') {
1779 str_index += strspn(str_index, " \t\r\n");
1780 if (*str_index != '\0' && *str_index != '#') {
1781 fprintf
1782 (stderr,
1783 "%s:%s:%d: malformed string in configuration file\n",
1784 CMDLINE_PARSER_PACKAGE, filename,
1785 line_num);
1786 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001787 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001788 }
1789 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001790
Harald Weltebed35df2011-11-02 13:06:18 +01001791noarg:
Harald Weltef6214982017-09-24 10:23:24 +08001792 if (!strcmp(fopt, "include")) {
1793 if (farg && *farg) {
1794 result =
1795 _cmdline_parser_configfile(farg, my_argc);
1796 } else {
1797 fprintf(stderr,
1798 "%s:%s:%d: include requires a filename argument.\n",
1799 CMDLINE_PARSER_PACKAGE, filename,
1800 line_num);
1801 }
1802 continue;
1803 }
Harald Weltebed35df2011-11-02 13:06:18 +01001804 len = strlen(fopt);
Harald Weltebed35df2011-11-02 13:06:18 +01001805 strcat(my_argv, len > 1 ? "--" : "-");
1806 strcat(my_argv, fopt);
1807 if (len > 1 && ((farg && *farg) || equal))
1808 strcat(my_argv, "=");
1809 if (farg && *farg)
1810 strcat(my_argv, farg);
Harald Weltef6214982017-09-24 10:23:24 +08001811 ++(*my_argc);
Harald Welte1b3e5772010-05-04 11:13:56 +02001812
Harald Weltebed35df2011-11-02 13:06:18 +01001813 cmd_line_list_tmp =
1814 (struct line_list *)malloc(sizeof(struct line_list));
1815 cmd_line_list_tmp->next = cmd_line_list;
1816 cmd_line_list = cmd_line_list_tmp;
1817 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1818 } /* while */
jjako52c24142002-12-16 13:33:51 +00001819
Harald Weltebed35df2011-11-02 13:06:18 +01001820 if (file)
1821 fclose(file);
Harald Weltef6214982017-09-24 10:23:24 +08001822 return result;
1823}
1824
1825int
1826cmdline_parser_configfile(const char *filename,
1827 struct gengetopt_args_info *args_info,
1828 int override, int initialize, int check_required)
1829{
1830 struct cmdline_parser_params params;
1831
1832 params.override = override;
1833 params.initialize = initialize;
1834 params.check_required = check_required;
1835 params.check_ambiguity = 0;
1836 params.print_errors = 1;
1837
1838 return cmdline_parser_config_file(filename, args_info, &params);
1839}
1840
1841int
1842cmdline_parser_config_file(const char *filename,
1843 struct gengetopt_args_info *args_info,
1844 struct cmdline_parser_params *params)
1845{
1846 int i, result;
1847 int my_argc = 1;
1848 char **my_argv_arg;
1849 char *additional_error;
1850
1851 /* store the program name */
1852 cmd_line_list_tmp =
1853 (struct line_list *)malloc(sizeof(struct line_list));
1854 cmd_line_list_tmp->next = cmd_line_list;
1855 cmd_line_list = cmd_line_list_tmp;
1856 cmd_line_list->string_arg = gengetopt_strdup(CMDLINE_PARSER_PACKAGE);
1857
1858 result = _cmdline_parser_configfile(filename, &my_argc);
1859
1860 if (result != EXIT_FAILURE) {
1861 my_argv_arg = (char **)malloc((my_argc + 1) * sizeof(char *));
1862 cmd_line_list_tmp = cmd_line_list;
1863
1864 for (i = my_argc - 1; i >= 0; --i) {
1865 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1866 cmd_line_list_tmp = cmd_line_list_tmp->next;
1867 }
1868
1869 my_argv_arg[my_argc] = 0;
1870
1871 additional_error =
1872 (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) +
1873 1);
1874 strcpy(additional_error, ADDITIONAL_ERROR);
1875 strcat(additional_error, filename);
1876 result =
1877 cmdline_parser_internal(my_argc, my_argv_arg, args_info,
1878 params, additional_error);
1879
1880 free(additional_error);
1881 free(my_argv_arg);
1882 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001883
Harald Weltebed35df2011-11-02 13:06:18 +01001884 free_cmd_list();
1885 if (result == EXIT_FAILURE) {
1886 cmdline_parser_free(args_info);
1887 exit(EXIT_FAILURE);
1888 }
1889
1890 return result;
jjako52c24142002-12-16 13:33:51 +00001891}