Browse Source

Create parse_host_and_port() function.

Refactor host and port parsing code into a function. It'll be used
when setting up input, output and camd server.
Georgi Chorbadzhiyski 11 years ago
parent
commit
4853db457b
3 changed files with 32 additions and 25 deletions
  1. 1
    25
      tsdecrypt.c
  2. 30
    0
      util.c
  3. 1
    0
      util.h

+ 1
- 25
tsdecrypt.c View File

@@ -290,7 +290,6 @@ static void parse_options(struct ts *ts, int argc, char **argv) {
290 290
 	int j, i, ca_err = 0, server_err = 1, input_addr_err = 0, output_addr_err = 0, output_intf_err = 0, ident_err = 0, port_set = 0;
291 291
 	opterr = 0; // Prevent printing of error messages for unknown options in getopt()
292 292
 	while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
293
-		char *p = NULL;
294 293
 		if (j == '?') {
295 294
 			fprintf(stderr, "ERROR: Unknown parameter '%s'.\n", argv[optind - 1]);
296 295
 			exit(EXIT_FAILURE);
@@ -461,30 +460,7 @@ static void parse_options(struct ts *ts, int argc, char **argv) {
461 460
 				}
462 461
 				break;
463 462
 			case 's': // --camd-server
464
-				server_err = 0;
465
-				ts->camd.hostname = optarg;
466
-				if (optarg[0] == '[') { // Detect IPv6 static address
467
-					p = strrchr(optarg, ']');
468
-					if (!p) {
469
-						fprintf(stderr, "ERROR: Invalid IPv6 address format: %s\n", optarg);
470
-						exit(EXIT_FAILURE);
471
-					}
472
-					ts->camd.hostname = optarg + 1; // Remove first [
473
-					*p = 0x00; // Remove last ]
474
-					char *p2 = strchr(p + 1, ':');
475
-					if (p2) {
476
-						*p2 = 0x00;
477
-						ts->camd.service = p2 + 1;
478
-						port_set = 1;
479
-					}
480
-				} else {
481
-					p = strrchr(optarg, ':');
482
-					if (p) {
483
-						*p = 0x00;
484
-						ts->camd.service = p + 1;
485
-						port_set = 1;
486
-					}
487
-				}
463
+				server_err = !parse_host_and_port(optarg, &ts->camd.hostname, &ts->camd.service, &port_set);
488 464
 				break;
489 465
 			case 'U': // --camd-user
490 466
 				if (strlen(optarg) < 64)

+ 30
- 0
util.c View File

@@ -281,6 +281,36 @@ OUT:
281 281
 	return buf_pos;
282 282
 }
283 283
 
284
+int parse_host_and_port(char *input, char **hostname, char **service, int *port_set) {
285
+	char *p;
286
+	if (strlen(input) == 0)
287
+		return 0;
288
+	*hostname = input;
289
+	if (input[0] == '[') { // Detect IPv6 static address
290
+		p = strrchr(input, ']');
291
+		if (!p) {
292
+			fprintf(stderr, "ERROR: Invalid IPv6 address format: %s\n", input);
293
+			exit(EXIT_FAILURE);
294
+		}
295
+		*hostname = input + 1; // Remove first [
296
+		*p = 0x00; // Remove last ]
297
+		char *p2 = strchr(p + 1, ':');
298
+		if (p2) {
299
+			*p2 = 0x00;
300
+			*service = p2 + 1;
301
+			*port_set = 1;
302
+		}
303
+	} else {
304
+		p = strrchr(input, ':');
305
+		if (p) {
306
+			*p = 0x00;
307
+			*service = p + 1;
308
+			*port_set = 1;
309
+		}
310
+	}
311
+	return 1;
312
+}
313
+
284 314
 char *my_inet_ntop(int family, struct sockaddr *addr, char *dest, int dest_len) {
285 315
 	struct sockaddr_in  *addr_v4 = (struct sockaddr_in  *)addr;
286 316
 	struct sockaddr_in6 *addr_v6 = (struct sockaddr_in6 *)addr;

+ 1
- 0
util.h View File

@@ -27,6 +27,7 @@ void set_thread_name(char *thread_name);
27 27
 int decode_hex_string(char *hex, uint8_t *bin, int asc_len);
28 28
 int64_t get_time(void);
29 29
 unsigned int file_hex2buf(char *filename, uint8_t *buffer, unsigned int buf_size);
30
+int parse_host_and_port(char *input, char **hostname, char **service, int *port_set);
30 31
 char *my_inet_ntop(int family, struct sockaddr *addr, char *dest, int dest_len);
31 32
 
32 33
 #endif

Loading…
Cancel
Save