summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
2bf7cf8)
* Set errno to 0 before calling strtol()
* Fix warnings in gssconn_{server,client}.c
* Add tr_parse_port() to tr_inet_util.[ch] and use throughout the
codebase for parsing port numbers
#include <talloc.h>
#include <tr_inet_util.h>
#include <talloc.h>
#include <tr_inet_util.h>
/**
* Determine whether a string is a valid address of a given family
/**
* Determine whether a string is a valid address of a given family
{
const char *colon;
char *hostname;
{
const char *colon;
char *hostname;
if (s == NULL)
return NULL;
if (s == NULL)
return NULL;
if (colon == NULL) {
*port_out = 0;
} else {
if (colon == NULL) {
*port_out = 0;
} else {
- port = strtol(colon+1, NULL, 10);
- if (tr_str_all_digits(colon+1) && (port > 0) && (port <= 65535))
- *port_out = (int) port;
+ port = tr_parse_port(colon+1);
+ if ((port > 0) && tr_str_all_digits(colon+1))
+ *port_out = port;
+}
+
+/**
+ * Parse a string containing a port
+ *
+ * Returns the port number, which is always in the range 1-65535.
+ * On error, returns < 0. The absolute value is an error code from errno.h
+ *
+ * @param s
+ * @return port number or < 0 on error
+ */
+int tr_parse_port(const char *s)
+{
+ long port;
+ char *end;
+
+ errno = 0; /* strtol sets this, make sure it's zero to avoid false positives */
+ port = strtol(s, &end, 10);
+ if (errno) {
+ return -errno;
+ }
+
+ if (*end != '\0') {
+ return -EINVAL;
+ }
+
+ if ((port <= 0) || (port > 65535)) {
+ return -ERANGE;
+ }
+
+ return (int) port;
}
\ No newline at end of file
}
\ No newline at end of file
int err = 0;
int fd = -1;
int port = kDefaultPort;
int err = 0;
int fd = -1;
int port = kDefaultPort;
const char *server = "127.0.0.1";
const char *clientName = NULL;
const char *serviceName = "host";
const char *server = "127.0.0.1";
const char *clientName = NULL;
const char *serviceName = "host";
for (i = 1; (i < argc) && !err; i++) {
if ((strcmp (argv[i], "--port") == 0) && (i < (argc - 1))) {
for (i = 1; (i < argc) && !err; i++) {
if ((strcmp (argv[i], "--port") == 0) && (i < (argc - 1))) {
- port = strtol (argv[++i], NULL, 0);
- if (port == 0) { err = errno; }
+ errno = 0; /* ensure this starts off as 0 */
+ tmp = strtol (argv[++i], NULL, 0);
+ if (errno)
+ err = errno;
+ else if ((tmp <= 0) || (tmp > 65535))
+ err = ERANGE;
+ else
+ port = (int) tmp;
} else if ((strcmp (argv[i], "--server") == 0) && (i < (argc - 1))) {
server = argv[++i];
} else if ((strcmp(argv[i], "--cprinc") == 0) && (i < (argc - 1))) {
} else if ((strcmp (argv[i], "--server") == 0) && (i < (argc - 1))) {
server = argv[++i];
} else if ((strcmp(argv[i], "--cprinc") == 0) && (i < (argc - 1))) {
if (!err) {
printf ("%s: Starting up...\n", argv[0]);
if (!err) {
printf ("%s: Starting up...\n", argv[0]);
- err = gsscon_connect (server, port, serviceName, &fd, &gssContext);
+ err = gsscon_connect (server, (unsigned int) port, serviceName, &fd, &gssContext);
int err = 0;
OM_uint32 minorStatus;
int port = kDefaultPort;
int err = 0;
OM_uint32 minorStatus;
int port = kDefaultPort;
int listenFD = -1;
gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
int i = 0;
int listenFD = -1;
gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
int i = 0;
for (i = 1; (i < argc) && !err; i++) {
if ((strcmp (argv[i], "--port") == 0) && (i < (argc - 1))) {
for (i = 1; (i < argc) && !err; i++) {
if ((strcmp (argv[i], "--port") == 0) && (i < (argc - 1))) {
- port = strtol (argv[++i], NULL, 0);
- if (port == 0) { err = errno; }
+ errno = 0; /* ensure this starts off at 0 */
+ tmp = strtol (argv[++i], NULL, 0);
+ if (errno)
+ err = errno;
+ else if ((tmp <= 0) || (tmp > 65535))
+ err = ERANGE;
+ else
+ port = (int) tmp;
} else if ((strcmp(argv[i], "--service") == 0) && (i < (argc - 1))) {
gServiceName = argv[++i];
} else {
} else if ((strcmp(argv[i], "--service") == 0) && (i < (argc - 1))) {
gServiceName = argv[++i];
} else {
char *tr_parse_host(TALLOC_CTX *mem_ctx, const char *s, int *port_out);
TR_NAME *tr_hostname_and_port_to_name(TR_NAME *hn, int port);
char *tr_parse_host(TALLOC_CTX *mem_ctx, const char *s, int *port_out);
TR_NAME *tr_hostname_and_port_to_name(TR_NAME *hn, int port);
+int tr_parse_port(const char *s);
#endif //TRUST_ROUTER_TR_INET_UTIL_H
#endif //TRUST_ROUTER_TR_INET_UTIL_H
#include <tid_internal.h>
#include <trust_router/tr_dh.h>
#include <trust_router/tid.h>
#include <tid_internal.h>
#include <trust_router/tr_dh.h>
#include <trust_router/tid.h>
+#include <tr_inet_util.h>
static void tidc_resp_handler (TIDC_INSTANCE * tidc,
TID_REQ *req,
static void tidc_resp_handler (TIDC_INSTANCE * tidc,
TID_REQ *req,
- arguments->port=strtol(arg, NULL, 10); /* optional */
+ arguments->port=tr_parse_port(arg); /* optional */
+ if (arguments->port < 0) {
+ switch(-(arguments->port)) {
+ case ERANGE:
+ printf("\nError parsing port (%s): port must be an integer in the range 1 - 65535\n\n", arg);
+ break;
+
+ default:
+ printf("\nError parsing port (%s): %s\n\n", arg, strerror(-arguments->port));
+ break;
+ }
+ argp_usage(state);
+ }
#include <mon_internal.h>
#include <tr_debug.h>
#include <mon_internal.h>
#include <tr_debug.h>
+#include <tr_inet_util.h>
/* command-line option setup */
/* command-line option setup */
/* parser for individual options - fills in a struct cmdline_args */
static error_t parse_option(int key, char *arg, struct argp_state *state)
{
/* parser for individual options - fills in a struct cmdline_args */
static error_t parse_option(int key, char *arg, struct argp_state *state)
{
/* get a shorthand to the command line argument structure, part of state */
struct cmdline_args *arguments=state->input;
/* get a shorthand to the command line argument structure, part of state */
struct cmdline_args *arguments=state->input;
case 'v':
print_version_info();
exit(0);
case 'v':
print_version_info();
exit(0);
case ARGP_KEY_ARG: /* handle argument (not option) */
switch (state->arg_num) {
case ARGP_KEY_ARG: /* handle argument (not option) */
switch (state->arg_num) {
- tmp_l = strtol(arg, NULL, 10);
- if (errno || (tmp_l < 0) || (tmp_l > 65535)) /* max valid port */
+ arguments->port=tr_parse_port(arg); /* optional */
+ if (arguments->port < 0) {
+ switch(-(arguments->port)) {
+ case ERANGE:
+ printf("\nError parsing port (%s): port must be an integer in the range 1 - 65535\n\n", arg);
+ break;
+
+ default:
+ printf("\nError parsing port (%s): %s\n\n", arg, strerror(-arguments->port));
+ break;
+ }
-
- arguments->port = (int) tmp_l; /* we already checked the range */
break;
case 2:
arguments->command=mon_cmd_from_string(arg);
if (arguments->command == MON_CMD_UNKNOWN) {
break;
case 2:
arguments->command=mon_cmd_from_string(arg);
if (arguments->command == MON_CMD_UNKNOWN) {
- printf("\nUnknown command '%s'\n", arg);
- argp_usage(state);
+ printf("\nUnknown command '%s'\n\n", arg);
+ err = 1;
}
break;
default:
if (arguments->n_options >= MAX_OPTIONS) {
}
break;
default:
if (arguments->n_options >= MAX_OPTIONS) {
- printf("\nToo many command options given, limit is %d\n", MAX_OPTIONS);
- argp_usage(state);
+ printf("\nToo many command options given, limit is %d\n\n", MAX_OPTIONS);
+ err = 1;
+ break;
}
arguments->options[arguments->n_options] = mon_opt_type_from_string(arg);
if (arguments->options[arguments->n_options] == OPT_TYPE_UNKNOWN) {
}
arguments->options[arguments->n_options] = mon_opt_type_from_string(arg);
if (arguments->options[arguments->n_options] == OPT_TYPE_UNKNOWN) {
- printf("\nUnknown command option '%s'\n", arg);
- argp_usage(state);
+ printf("\nUnknown command option '%s'\n\n", arg);
+ err = 1;
}
arguments->n_options++;
break;
}
arguments->n_options++;
break;
case ARGP_KEY_END: /* no more arguments */
if (state->arg_num < 3) {
/* not enough arguments encountered */
case ARGP_KEY_END: /* no more arguments */
if (state->arg_num < 3) {
/* not enough arguments encountered */
return ARGP_ERR_UNKNOWN;
}
return ARGP_ERR_UNKNOWN;
}
+ if (err) {
+ argp_usage(state);
+ return EINVAL; /* argp_usage() usually does not return, but just in case */
+ }
+
return 0; /* success */
}
return 0; /* success */
}
#include <gsscon.h>
#include <tr_debug.h>
#include <tr_trp.h>
#include <gsscon.h>
#include <tr_debug.h>
#include <tr_trp.h>
+#include <tr_inet_util.h>
/* command-line option setup */
/* command-line option setup */
- arguments->port=strtol(arg, NULL, 10); /* optional */
+ arguments->port=tr_parse_port(arg); /* optional */
+ if (arguments->port < 0) {
+ switch(-(arguments->port)) {
+ case ERANGE:
+ printf("\nError parsing port (%s): port must be an integer in the range 1 - 65535\n\n", arg);
+ break;
+
+ default:
+ printf("\nError parsing port (%s): %s\n\n", arg, strerror(-arguments->port));
+ break;
+ }
+ argp_usage(state);
+ }