Allow a server to be specified.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Thu, 20 Apr 2017 09:34:24 +0000 (06:34 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Tue, 11 Jul 2017 21:37:15 +0000 (18:37 -0300)
Allow to connect to a different server. That way, a test server may be
used when debugging rnetclient.

rnetclient.c

index 946fa79..0728f89 100644 (file)
@@ -50,7 +50,10 @@ static const char rnetclient_doc[] =
        "Send the Brazilian Income Tax Report to the Brazilian "
        "Tax Authority";
 static const char rnetclient_args_doc[] =
-       "[-d|--declaration] FILE [-o|--output-dir DIRECTORY]";
+       "[-d|--declaration] FILE [-o|--output-dir DIRECTORY]"
+       " [-s|--server-name SERVER]";
+
+#define RNET_ADDRESS "receitanet.receita.fazenda.gov.br"
 
 /* Description and definition of each option accepted by the program.  */
 
@@ -63,6 +66,10 @@ static const struct argp_option rnetclient_options_desc[] = {
          "The directory where you wish to save the receipt.",
          0 },
 
+       { "server-name", 's', "SERVER", 0,
+         "The server to connect to. Default is " RNET_ADDRESS ".",
+         0 },
+
        { NULL },
 };
 
@@ -75,6 +82,9 @@ struct rnetclient_args {
 
        /* Output filename. */
        char output_file[PATH_MAX];
+
+       /* Server to connect to. */
+       char *server_name;
 };
 
 /* Parser for command line arguments.  */
@@ -93,6 +103,10 @@ static error_t rnetclient_parse_opt(int key, char *arg, struct argp_state *state
                a->output_dir = arg;
                break;
 
+       case 's':
+               a->server_name = arg;
+               break;
+
        case ARGP_KEY_ARG:
                /* The user has possibly provided a filename without
                   using any switches (e.g., by running './rnetclient
@@ -210,9 +224,7 @@ static int inflateRecord(char *buffer, size_t len, char **out, size_t *olen)
        return 0;
 }
 
-#define RNET_ADDRESS "receitanet.receita.fazenda.gov.br"
-
-static int connect_rnet(int *c)
+static int connect_rnet(int *c, char *server_name)
 {
        struct addrinfo *addresses;
        struct addrinfo *addr;
@@ -224,7 +236,7 @@ static int connect_rnet(int *c)
        hint.ai_socktype = SOCK_STREAM;
        hint.ai_protocol = IPPROTO_TCP;
        hint.ai_flags = AI_ADDRCONFIG;
-       r = getaddrinfo(RNET_ADDRESS, "3456", &hint, &addresses);
+       r = getaddrinfo(server_name, "3456", &hint, &addresses);
        if (r) {
                return r;
        }
@@ -453,6 +465,7 @@ int main(int argc, char **argv)
           us.  This is a bug, and should not happen in the current
           state.  */
        memset(&rnet_args, 0, sizeof (rnet_args));
+       rnet_args.server_name = RNET_ADDRESS;
        err = argp_parse (&rnetclient_argp, argc, argv, 0, NULL, &rnet_args);
        if (err != 0)
                fprintf(stderr, "internal error while parsing command line arguments.");
@@ -493,7 +506,7 @@ int main(int argc, char **argv)
        gnutls_global_init();
 
        session_new(&session);
-       r = connect_rnet(&c);
+       r = connect_rnet(&c, rnet_args.server_name);
        if (r) {
                fprintf(stderr, "error connecting to server: %s\n",
                        r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));