/*
- * Copyright (C) 2012-2013 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ * Copyright (C) 2012-2014 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ * Copyright (C) 2014 Alexandre Oliva <lxoliva@fsfla.org>
+ * Copyright (C) 2014 Sergio Durigan Junior <sergiodj@sergiodj.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include <netdb.h>
#include <gnutls/gnutls.h>
#include <zlib.h>
+#include <argp.h>
+#include "config.h"
#include "decfile.h"
#include "rnet_message.h"
#include "rnet_encode.h"
+/* Program version and bug report address. */
+
+const char *argp_program_version = PACKAGE_VERSION;
+const char *argp_program_bug_address = PACKAGE_BUGREPORT;
+
+/* Documentation strings. */
+
+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";
+
+/* Description and definition of each option accepted by the program. */
+
+static const struct argp_option rnetclient_options_desc[] = {
+ { "declaration", 'd', "FILE", OPTION_ARG_OPTIONAL,
+ "The Income Tax Report file that will be sent.",
+ 0 },
+
+ { NULL },
+};
+
+struct rnetclient_args {
+ /* File representing the declaration. */
+ char *input_file;
+};
+
+/* Parser for command line arguments. */
+
+static error_t rnetclient_parse_opt(int key, char *arg, struct argp_state *state)
+{
+ struct rnetclient_args *a = state->input;
+ switch (key) {
+ case 'd':
+ /* The user has explicitly provided a filename through
+ the '-d' switch. */
+ a->input_file = arg;
+ break;
+
+ case ARGP_KEY_ARG:
+ /* The user has possibly provided a filename without
+ using any switches (e.g., by running './rnetclient
+ file'). */
+ a->input_file = arg;
+ break;
+
+ case ARGP_KEY_END:
+ /* We have reached the end of the argument parsing.
+ Let's check if the user has provided a filename. */
+ if (a->input_file == NULL)
+ argp_error(state,
+ "You need to provide the Income Tax Declaration "
+ "filename.");
+ }
+
+ return 0;
+}
+
+/* Control struct used by argp. */
+
+static struct argp rnetclient_argp = {
+ rnetclient_options_desc,
+ rnetclient_parse_opt,
+ rnetclient_args_doc,
+ rnetclient_doc,
+ NULL, NULL, NULL
+};
+
static size_t chars2len (unsigned char buf[2]) {
return (buf[0] << 8 | buf[1]);
}
char buffer[16];
int r;
buffer[0] = 1;
- write(c, buffer, 1);
- write(c, "00000000000000", 14);
+ r = write(c, buffer, 1);
+ if (r < 1)
+ return -1;
+ r = write(c, "00000000000000", 14);
+ if (r < 14)
+ return -1;
r = read(c, buffer, 1);
if (r != 1 && buffer[0] != 'E')
return -1;
return 0;
}
-static void usage(void)
-{
- fprintf(stderr, "rnetclient [filename]\n");
- exit(1);
-}
-
static int rnet_send(gnutls_session_t session, char *buffer, size_t len, int header)
{
- char *out;
- size_t olen;
- deflateRecord(buffer, len, &out, &olen, header);
- gnutls_record_send(session, out, olen);
- free(out);
- return 0;
+ int r = 0;
+ /* Large files have to be uploaded as multiple
+ separately-deflated chunks, because the compressed and
+ uncompressed lengths in each record are encoded in unsigned
+ 16-bit integers each.
+
+ The header can't be split into multiple chunks, and it
+ should never have to, since it won't ever get even close to
+ 64KiB.
+
+ The uploaded file may be larger: to upload such large
+ files, it suffices to send multiple records till the entire
+ file is transferred, without waiting for a response. Since
+ we've alread informed the server of the file size in the
+ header, it knows exactly how much data to expect before
+ sending a response. It will only send an error message
+ before that if it times us out.
+
+ Odds are that any reasonably large size will do, but it
+ can't be too close to 64KiB, otherwise there won't be room
+ for the compressed length should it not compress well,
+ which should never happen for capital-ASCII-only
+ declaration files, but who knows?
+
+ This chunk size worked at the first try, uploading a
+ ~100KiB file, so let's stick with it. */
+ const int maxc = 64472;
+ if (header && len > maxc)
+ return -1;
+
+ do {
+ char *out = NULL;
+ size_t olen;
+ size_t clen = len < maxc ? len : maxc;
+ r = deflateRecord(buffer, clen, &out, &olen, header);
+ if (!r) {
+ size_t n = gnutls_record_send(session, out, olen);
+ if (n != olen)
+ r = -1;
+ }
+ free(out);
+ buffer += clen;
+ len -= clen;
+ } while (len && !r);
+ return r;
}
static int rnet_recv(gnutls_session_t session, struct rnet_message **message)
int r;
struct rnet_decfile *decfile;
struct rnet_message *message = NULL;
+ struct rnetclient_args rnet_args;
gnutls_session_t session;
int finish = 0;
char *cpf;
-
- if (argc < 2) {
- usage();
- }
+ error_t err;
+
+ /* Parsing the command line arguments. The argp_parse
+ function calls exit() if there is some error during the
+ parsing process (e.g., the user has provided an unknown
+ flag or the parsing function has called argp_error).
+ However, if our internal parsing function returns something
+ different than zero, then argp_parse returns this value to
+ us. This is a bug, and should not happen in the current
+ state. */
+ memset(&rnet_args, 0, sizeof (rnet_args));
+ err = argp_parse (&rnetclient_argp, argc, argv, 0, NULL, &rnet_args);
+ if (err != 0)
+ fprintf(stderr, "internal error while parsing command line arguments.");
- decfile = rnet_decfile_open(argv[1]);
+ decfile = rnet_decfile_open(rnet_args.input_file);
if (!decfile) {
- fprintf(stderr, "could not parse %s: %s\n", argv[1], strerror(errno));
+ fprintf(stderr, "could not parse file \"%s\": %s\n", rnet_args.input_file, strerror(errno));
exit(1);
}