[PATCH v2 4/5] Allow client version to be specified.

Thadeu Lima de Souza Cascardo cascardo en cascardo.eti.br
Mar Jul 11 21:50:05 UTC 2017


In case the server from Receita Federal requires an updated version in
the future, the user will be able to use a different version for the
client without any need to rebuild or update rnetclient.
---

v2: Add macro with default version and use it instead of NULL.
    Though NULL is still accepted and default in a cleaner way in
    rnet_encode.

---
 rnet_encode.c |  9 ++++++---
 rnet_encode.h |  4 +++-
 rnetclient.c  | 17 +++++++++++++++--
 t-parse.c     |  2 +-
 4 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/rnet_encode.c b/rnet_encode.c
index 3b522c3..fefee79 100644
--- a/rnet_encode.c
+++ b/rnet_encode.c
@@ -1,5 +1,5 @@
 /*
- *  Copyright (C) 2013-2014  Thadeu Lima de Souza Cascardo <cascardo at minaslivre.org>
+ *  Copyright (C) 2013-2017  Thadeu Lima de Souza Cascardo <cascardo at minaslivre.org>
  *  Copyright (C) 2014  Alexandre Oliva <lxoliva at fsfla.org>
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -27,7 +27,7 @@
 #include "rnet_message.h"
 #include "decfile.h"
 
-int rnet_encode(struct rnet_decfile *decfile, struct rnet_message **msg)
+int rnet_encode(struct rnet_decfile *decfile, struct rnet_message **msg, char *client)
 {
 	int r = -EIO;
 
@@ -46,6 +46,9 @@ int rnet_encode(struct rnet_decfile *decfile, struct rnet_message **msg)
 
 	size_t header_size, header_head, header_tail;
 
+	if (client == NULL)
+		client = RNET_DEFAULT_VERSION;
+
 	*msg = rnet_message_new();
 	if (*msg == NULL) {
 		return -ENOMEM;
@@ -96,7 +99,7 @@ int rnet_encode(struct rnet_decfile *decfile, struct rnet_message **msg)
 	r = rnet_message_add_ascii(msg, "versao_java", "1.5.0-gij;Free Software rnetclient pretending to be GNU Interpreter for Java");
 	r = rnet_message_add_ascii(msg, "origem", "JA2R");
 	r = rnet_message_add_ascii(msg, "so", "GNU");
-	r = rnet_message_add_ascii(msg, "cliente", "201704");
+	r = rnet_message_add_ascii(msg, "cliente", client);
 	r = rnet_message_add_buffer(msg, "dados_val",
 				    header + header_head,
 				    header_size - header_tail - header_head);
diff --git a/rnet_encode.h b/rnet_encode.h
index 4186642..6ebbd9f 100644
--- a/rnet_encode.h
+++ b/rnet_encode.h
@@ -22,6 +22,8 @@
 #include "rnet_message.h"
 #include "decfile.h"
 
-int rnet_encode(struct rnet_decfile *decfile, struct rnet_message **msg);
+#define RNET_DEFAULT_VERSION "201704"
+
+int rnet_encode(struct rnet_decfile *decfile, struct rnet_message **msg, char *client);
 
 #endif
diff --git a/rnetclient.c b/rnetclient.c
index 0728f89..756cee8 100644
--- a/rnetclient.c
+++ b/rnetclient.c
@@ -51,7 +51,8 @@ static const char rnetclient_doc[] =
 	"Tax Authority";
 static const char rnetclient_args_doc[] =
 	"[-d|--declaration] FILE [-o|--output-dir DIRECTORY]"
-	" [-s|--server-name SERVER]";
+	" [-s|--server-name SERVER]"
+	" [-c|--client-version CLIENT]";
 
 #define RNET_ADDRESS "receitanet.receita.fazenda.gov.br"
 
@@ -70,6 +71,10 @@ static const struct argp_option rnetclient_options_desc[] = {
 	  "The server to connect to. Default is " RNET_ADDRESS ".",
 	  0 },
 
+	{ "client-version", 'c', "CLIENT", 0,
+	  "The client version to be used.",
+	  0 },
+
 	{ NULL },
 };
 
@@ -85,6 +90,9 @@ struct rnetclient_args {
 
 	/* Server to connect to. */
 	char *server_name;
+
+	/* Client version to use. */
+	char *client_version;
 };
 
 /* Parser for command line arguments.  */
@@ -107,6 +115,10 @@ static error_t rnetclient_parse_opt(int key, char *arg, struct argp_state *state
 		a->server_name = arg;
 		break;
 
+	case 'c':
+		a->client_version = arg;
+		break;
+
 	case ARGP_KEY_ARG:
 		/* The user has possibly provided a filename without
 		   using any switches (e.g., by running './rnetclient
@@ -466,6 +478,7 @@ int main(int argc, char **argv)
 	   state.  */
 	memset(&rnet_args, 0, sizeof (rnet_args));
 	rnet_args.server_name = RNET_ADDRESS;
+	rnet_args.client_version = RNET_DEFAULT_VERSION;
 	err = argp_parse (&rnetclient_argp, argc, argv, 0, NULL, &rnet_args);
 	if (err != 0)
 		fprintf(stderr, "internal error while parsing command line arguments.");
@@ -526,7 +539,7 @@ int main(int argc, char **argv)
 		goto out_handshake;
 	}
 
-	r = rnet_encode(decfile, &message);
+	r = rnet_encode(decfile, &message, rnet_args.client_version);
 	if (r < 0) {
 		fprintf(stderr, "error encoding message, file not supported?\n");
 		r = 1;
diff --git a/t-parse.c b/t-parse.c
index 827dcec..ac735de 100644
--- a/t-parse.c
+++ b/t-parse.c
@@ -41,7 +41,7 @@ int main(void)
 			return -1;
 		}
 
-		r = rnet_encode(decfile, &message);
+		r = rnet_encode(decfile, &message, NULL);
 		if (r < 0) {
 			fprintf(stderr, "Error encoding message\n");
 			return -1;
-- 
2.13.2



Más información sobre la lista de distribución Softwares-impostos