summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCoren[m] <Break@Ocean>2013-09-11 00:14:20 +0200
committerCoren[m] <Break@Ocean>2013-09-11 00:14:20 +0200
commit4cf0d857bcf87ba271178e7b1734958fb93b018a (patch)
tree415ac36bb195c319418c942ffa31e8a67616818e
parent8ba6a5ff5b05d027bc4c131b208015ea4019026b (diff)
cmdline parsing of --ipv4/6 plucked into util
-rw-r--r--other/DHT_bootstrap.c30
-rw-r--r--testing/DHT_test.c30
-rw-r--r--testing/Lossless_UDP_testclient.c30
-rw-r--r--testing/Lossless_UDP_testserver.c30
-rw-r--r--testing/Messenger_test.c30
-rw-r--r--testing/nTox.c30
-rw-r--r--toxcore/util.c32
-rw-r--r--toxcore/util.h2
8 files changed, 52 insertions, 162 deletions
diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c
index 795e24ac..099aad80 100644
--- a/other/DHT_bootstrap.c
+++ b/other/DHT_bootstrap.c
@@ -89,33 +89,9 @@ int main(int argc, char *argv[])
89 89
90 /* let user override default by cmdline */ 90 /* let user override default by cmdline */
91 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ 91 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
92 92 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
93 int argvoffset = 0, argi; 93 if (argvoffset < 0)
94 for(argi = 1; argi < argc; argi++) 94 exit(1);
95 if (!strncasecmp(argv[argi], "--ipv", 5)) {
96 if (argv[argi][5] && !argv[argi][6]) {
97 char c = argv[argi][5];
98 if (c == '4')
99 ipv6enabled = 0;
100 else if (c == '6')
101 ipv6enabled = 1;
102 else {
103 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
104 exit(1);
105 }
106 }
107 else {
108 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
109 exit(1);
110 }
111
112 if (argvoffset != argi - 1) {
113 printf("Argument must come first: %s.\n", argv[argi]);
114 exit(1);
115 }
116
117 argvoffset++;
118 }
119 95
120 /* Initialize networking - 96 /* Initialize networking -
121 Bind to ip 0.0.0.0 / [::] : PORT */ 97 Bind to ip 0.0.0.0 / [::] : PORT */
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index 61762162..ba8c2f23 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -140,33 +140,9 @@ int main(int argc, char *argv[])
140 140
141 /* let user override default by cmdline */ 141 /* let user override default by cmdline */
142 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ 142 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
143 143 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
144 int argvoffset = 0, argi; 144 if (argvoffset < 0)
145 for(argi = 1; argi < argc; argi++) 145 exit(1);
146 if (!strncasecmp(argv[argi], "--ipv", 5)) {
147 if (argv[argi][5] && !argv[argi][6]) {
148 char c = argv[argi][5];
149 if (c == '4')
150 ipv6enabled = 0;
151 else if (c == '6')
152 ipv6enabled = 1;
153 else {
154 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
155 exit(1);
156 }
157 }
158 else {
159 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
160 exit(1);
161 }
162
163 if (argvoffset != argi - 1) {
164 printf("Argument must come first: %s.\n", argv[argi]);
165 exit(1);
166 }
167
168 argvoffset++;
169 }
170 146
171 //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); 147 //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
172 /* initialize networking */ 148 /* initialize networking */
diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c
index 5d4c8547..70349204 100644
--- a/testing/Lossless_UDP_testclient.c
+++ b/testing/Lossless_UDP_testclient.c
@@ -153,33 +153,9 @@ int main(int argc, char *argv[])
153{ 153{
154 /* let user override default by cmdline */ 154 /* let user override default by cmdline */
155 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ 155 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
156 156 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
157 int argvoffset = 0, argi; 157 if (argvoffset < 0)
158 for(argi = 1; argi < argc; argi++) 158 exit(1);
159 if (!strncasecmp(argv[argi], "--ipv", 5)) {
160 if (argv[argi][5] && !argv[argi][6]) {
161 char c = argv[argi][5];
162 if (c == '4')
163 ipv6enabled = 0;
164 else if (c == '6')
165 ipv6enabled = 1;
166 else {
167 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
168 exit(1);
169 }
170 }
171 else {
172 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
173 exit(1);
174 }
175
176 if (argvoffset != argi - 1) {
177 printf("Argument must come first: %s.\n", argv[argi]);
178 exit(1);
179 }
180
181 argvoffset++;
182 }
183 159
184 if (argc < argvoffset + 4) { 160 if (argc < argvoffset + 4) {
185 printf("Usage: %s [--ipv4|--ipv6] ip port filename\n", argv[0]); 161 printf("Usage: %s [--ipv4|--ipv6] ip port filename\n", argv[0]);
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c
index 52dbcc80..343a662a 100644
--- a/testing/Lossless_UDP_testserver.c
+++ b/testing/Lossless_UDP_testserver.c
@@ -149,33 +149,9 @@ int main(int argc, char *argv[])
149{ 149{
150 /* let user override default by cmdline */ 150 /* let user override default by cmdline */
151 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ 151 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
152 152 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
153 int argvoffset = 0, argi; 153 if (argvoffset < 0)
154 for(argi = 1; argi < argc; argi++) 154 exit(1);
155 if (!strncasecmp(argv[argi], "--ipv", 5)) {
156 if (argv[argi][5] && !argv[argi][6]) {
157 char c = argv[argi][5];
158 if (c == '4')
159 ipv6enabled = 0;
160 else if (c == '6')
161 ipv6enabled = 1;
162 else {
163 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
164 exit(1);
165 }
166 }
167 else {
168 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
169 exit(1);
170 }
171
172 if (argvoffset != argi - 1) {
173 printf("Argument must come first: %s.\n", argv[argi]);
174 exit(1);
175 }
176
177 argvoffset++;
178 }
179 155
180 if (argc < argvoffset + 2) { 156 if (argc < argvoffset + 2) {
181 printf("Usage: %s [--ipv4|--ipv6] filename\n", argv[0]); 157 printf("Usage: %s [--ipv4|--ipv6] filename\n", argv[0]);
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c
index e7a75c8c..14d9ca20 100644
--- a/testing/Messenger_test.c
+++ b/testing/Messenger_test.c
@@ -97,33 +97,9 @@ int main(int argc, char *argv[])
97{ 97{
98 /* let user override default by cmdline */ 98 /* let user override default by cmdline */
99 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ 99 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
100 100 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
101 int argvoffset = 0, argi; 101 if (argvoffset < 0)
102 for(argi = 1; argi < argc; argi++) 102 exit(1);
103 if (!strncasecmp(argv[argi], "--ipv", 5)) {
104 if (argv[argi][5] && !argv[argi][6]) {
105 char c = argv[argi][5];
106 if (c == '4')
107 ipv6enabled = 0;
108 else if (c == '6')
109 ipv6enabled = 1;
110 else {
111 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
112 exit(1);
113 }
114 }
115 else {
116 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
117 exit(1);
118 }
119
120 if (argvoffset != argi - 1) {
121 printf("Argument must come first: %s.\n", argv[argi]);
122 exit(1);
123 }
124
125 argvoffset++;
126 }
127 103
128 /* with optional --ipvx, now it can be 1-4 arguments... */ 104 /* with optional --ipvx, now it can be 1-4 arguments... */
129 if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) { 105 if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) {
diff --git a/testing/nTox.c b/testing/nTox.c
index 6eeec5d3..750970f9 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -544,33 +544,9 @@ int main(int argc, char *argv[])
544 544
545 /* let user override default by cmdline */ 545 /* let user override default by cmdline */
546 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ 546 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
547 547 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
548 int argvoffset = 0, argi; 548 if (argvoffset < 0)
549 for(argi = 1; argi < argc; argi++) 549 exit(1);
550 if (!strncasecmp(argv[argi], "--ipv", 5)) {
551 if (argv[argi][5] && !argv[argi][6]) {
552 char c = argv[argi][5];
553 if (c == '4')
554 ipv6enabled = 0;
555 else if (c == '6')
556 ipv6enabled = 1;
557 else {
558 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
559 exit(1);
560 }
561 }
562 else {
563 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
564 exit(1);
565 }
566
567 if (argvoffset != argi - 1) {
568 printf("Argument must come first: %s.\n", argv[argi]);
569 exit(1);
570 }
571
572 argvoffset++;
573 }
574 550
575 int on = 0; 551 int on = 0;
576 int c = 0; 552 int c = 0;
diff --git a/toxcore/util.c b/toxcore/util.c
index 19d464d4..7b5ddc49 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -43,6 +43,38 @@ void id_cpy(uint8_t *dest, uint8_t *src)
43 memcpy(dest, src, CLIENT_ID_SIZE); 43 memcpy(dest, src, CLIENT_ID_SIZE);
44} 44}
45 45
46int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled)
47{
48 int argvoffset = 0, argi;
49 for(argi = 1; argi < argc; argi++)
50 if (!strncasecmp(argv[argi], "--ipv", 5)) {
51 if (argv[argi][5] && !argv[argi][6]) {
52 char c = argv[argi][5];
53 if (c == '4')
54 *ipv6enabled = 0;
55 else if (c == '6')
56 *ipv6enabled = 1;
57 else {
58 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
59 return -1;
60 }
61 }
62 else {
63 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
64 return -1;
65 }
66
67 if (argvoffset != argi - 1) {
68 printf("Argument must come first: %s.\n", argv[argi]);
69 return -1;
70 }
71
72 argvoffset++;
73 }
74
75 return argvoffset;
76};
77
46#ifdef LOGGING 78#ifdef LOGGING
47char logbuffer[512]; 79char logbuffer[512];
48static FILE *logfile = NULL; 80static FILE *logfile = NULL;
diff --git a/toxcore/util.h b/toxcore/util.h
index 71be84ca..e7be2d51 100644
--- a/toxcore/util.h
+++ b/toxcore/util.h
@@ -16,6 +16,8 @@ uint64_t random_64b();
16bool id_eq(uint8_t *dest, uint8_t *src); 16bool id_eq(uint8_t *dest, uint8_t *src);
17void id_cpy(uint8_t *dest, uint8_t *src); 17void id_cpy(uint8_t *dest, uint8_t *src);
18 18
19int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled);
20
19#undef LOGGING 21#undef LOGGING
20/* #define LOGGING */ 22/* #define LOGGING */
21#ifdef LOGGING 23#ifdef LOGGING