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
 	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;
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
 	opterr = 0; // Prevent printing of error messages for unknown options in getopt()
291
 	opterr = 0; // Prevent printing of error messages for unknown options in getopt()
292
 	while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
292
 	while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
293
-		char *p = NULL;
294
 		if (j == '?') {
293
 		if (j == '?') {
295
 			fprintf(stderr, "ERROR: Unknown parameter '%s'.\n", argv[optind - 1]);
294
 			fprintf(stderr, "ERROR: Unknown parameter '%s'.\n", argv[optind - 1]);
296
 			exit(EXIT_FAILURE);
295
 			exit(EXIT_FAILURE);
461
 				}
460
 				}
462
 				break;
461
 				break;
463
 			case 's': // --camd-server
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
 				break;
464
 				break;
489
 			case 'U': // --camd-user
465
 			case 'U': // --camd-user
490
 				if (strlen(optarg) < 64)
466
 				if (strlen(optarg) < 64)

+ 30
- 0
util.c View File

281
 	return buf_pos;
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
 char *my_inet_ntop(int family, struct sockaddr *addr, char *dest, int dest_len) {
314
 char *my_inet_ntop(int family, struct sockaddr *addr, char *dest, int dest_len) {
285
 	struct sockaddr_in  *addr_v4 = (struct sockaddr_in  *)addr;
315
 	struct sockaddr_in  *addr_v4 = (struct sockaddr_in  *)addr;
286
 	struct sockaddr_in6 *addr_v6 = (struct sockaddr_in6 *)addr;
316
 	struct sockaddr_in6 *addr_v6 = (struct sockaddr_in6 *)addr;

+ 1
- 0
util.h View File

27
 int decode_hex_string(char *hex, uint8_t *bin, int asc_len);
27
 int decode_hex_string(char *hex, uint8_t *bin, int asc_len);
28
 int64_t get_time(void);
28
 int64_t get_time(void);
29
 unsigned int file_hex2buf(char *filename, uint8_t *buffer, unsigned int buf_size);
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
 char *my_inet_ntop(int family, struct sockaddr *addr, char *dest, int dest_len);
31
 char *my_inet_ntop(int family, struct sockaddr *addr, char *dest, int dest_len);
31
 
32
 
32
 #endif
33
 #endif

Loading…
Cancel
Save