Select Git revision
Erik Alexandre Pucci
authored and
Danilo K. S. Yorinori
committed
Now the client checks to see if the GSOAP error is the network device bind, exiting with it (value 50) or exiting with 2 otherwise. Signed-off-by:Erik Alexandre Pucci <eap08@c3sl.ufpr.br> Acked-by:
Ricardo Tavares de Oliveira <rto07@c3sl.ufpr.br> Signed-off-by:
Danilo K. S. Yorinori <danilok@c3sl.ufpr.br>
client.c 14.85 KiB
/* Copyright (C) 2004-2009 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of client
*
* client is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include "client.h"
/* Summary: Get parameters passed by user and contact webservice server
* to verify client version and send availability information.
* Usage: client <url> <inep> <mac> [proxy_host [proxy_port [proxy_user
* proxy_passwd]]]
* Return: Exit status:
* 1) Parameter error
* 2) Soap error
* 3) Version file error (open, memory allocation)
* 4) Client out-of-date
* 5) Server returned an error
* 6) Unexpected server result
* 7) Server returned an empty version string
* 8) Server returned an invalid version string
* 9) Server did not find the version file
* 10) Server returned an empty update link
* 11) Invalid INEP error
* 12) XML file error (open, memory allocation)
*/
int main(int argc, char **argv)
{
struct soap *soap = soap_new();
char *url, *inep, *basedir, *clientVersion;
int arglen, len, option;
url = inep = NULL;
/* Initialization *
*-----------------------------------------------------------------------
* Verify parameters */
option = verify_parameters(argc, argv, soap, &url, &inep);
/* Timeout settings */
soap->send_timeout = TIMEOUT;
soap->recv_timeout = TIMEOUT;
/* Get basedir */
arglen = strlen(argv[0]);
len = strlen(EXEFILE);
basedir = (char *) malloc(sizeof(char) * (arglen - len + 1));
strncpy(basedir, argv[0], (arglen - len));
DEBUG("Arg: %s basedir: %s\n", argv[0], basedir);
if ((option != 1) && (inep) && (inep[0] == '\0'))
{
ERROR("ERROR (11): Invalid INEP error (empty string).\n");
/* Invalid INEP error */
exit(11);
}
/* End of Initialization
*-----------------------------------------------------------------------
* Update option */
if (option == 1)
{
/* Get client version from file */
clientVersion = read_version_file(basedir);
/* Agent Version method */
check_version(soap, url, clientVersion);
}
/* End of Update option
*------------------------------------------------------------------------
* Inventory option */
else
{
/* Inventory method */
inventory(soap, url, basedir);
}
/* End of update inventory option
*------------------------------------------------------------------------
* Finalize execution */
soap_end(soap);
soap_free(soap);
/* Successful */
exit(0);
/* End of execution */
}
/*----------------------------------------------------------------------------
* Summary: Print to standard error the help messages.
* Parameters: void
* Return: void
*/
void print_help(void)
{
ERROR("USAGE: client URL [OPTION] [INEP] [MAC] [PROXY_");
ERROR("HOST [PROXY_PORT [PROXY_USERID PROXY_PASSWD]]]");
ERROR("\n\n");
ERROR("-i, --%s Inventory option ", INVENTORYOPTION);
ERROR("which sends the XML data\n");
ERROR("-u, --%s Update option ", UPDATEOPTION);
ERROR("which checks for updates\n");
ERROR("-l, --url=URL URL used to connect ");
ERROR("to the server\n");
ERROR("-I, --inep=INEP INEP of the school\n");
ERROR("-H, --phost=host Proxy host\n");
ERROR("-p, --pport=port Proxy port\n");
ERROR("-U, --puid=userid Proxy user id\n");
ERROR("-w, --ppasswd=pswd Proxy password\n");
ERROR("-h, --help Show this help\n");
}
/*----------------------------------------------------------------------------
* Summary: Parse the command line and obtain the parameters.
* Parameters:
* argc Number of arguments in command line (main argc)
* argv Vector of arguments strings (main argv)
* soap SOAP struct
* url URL of the contacted webservice server
* inep INEP of the school
* Return: An int value indicating the choosen option: inventory (0) or
* update (1).
*/
int verify_parameters(int argc, char **argv, struct soap *soap, char **url,
char **inep)
{
extern char *optarg;
extern int optind, opterr, optopt;
int option, option_index;
int inventory, update, help, invalid_option;
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{INVENTORYOPTION, no_argument, 0, 'i'},
{UPDATEOPTION, no_argument, 0, 'u'},
{"url", required_argument, 0, 'l'},
{"inep", required_argument, 0, 'I'},
{"phost", required_argument, 0, 'H'},
{"pport", required_argument, 0, 'p'},
{"puid", required_argument, 0, 'U'},
{"ppasswd", required_argument, 0, 'w'},
{0, 0, 0, 0}
};
inventory = update = help = invalid_option = 0;
/* Parse command line */
while ((option = getopt_long(argc, argv, "hiul:I:H:p:U:w:", long_options,
&option_index)) != -1)
{
switch(option)
{
case 'i':
/* Inventory option */
inventory = 1;
break;
case 'u':
/* Update option */
update = 1;
break;
case 'l':
/* Server URL */
*url = optarg;
break;
case 'I':
/* School INEP */
*inep = optarg;
break;
case 'H':
/* Proxy host information */
soap->proxy_host = optarg;
break;
case 'p':
/* Proxy port information */
soap->proxy_port = atoi(optarg);
break;
case 'U':
/* Proxy user id information */
soap->proxy_userid = optarg;
break;
case 'w':
/* Proxy password information */
soap->proxy_passwd = optarg;
break;
case 'h':
/* Print help message */
help = 1;
break;
case '?':
/* Invalid option(s) passed in command line */
invalid_option = 1;
break;
default:
break;
}
}
/* Invalid option received */
if (invalid_option)
{
ERROR("ERROR (1): ");
print_help();
/* Parameter error */
exit(1);
}
/* Help menu option received */
if (help)
{
print_help();
exit(0);
}
/* Check for url option */
if (*url == NULL)
{
ERROR("ERROR (1): Missing URL parameter.\n");
print_help();
/* Parameter error */
exit(1);
}
/* Check number of options called */
if (inventory + update != 1)
{
ERROR("ERROR (1): Use one, and only one of the following options:\n");
ERROR("-i, %s\n-u, %s\n", INVENTORYOPTION, UPDATEOPTION);
print_help();
/* Parameter error */
exit(1);
}
if (optind < argc)
{
ERROR("ERROR (1): Unknown option(s): ");
while (optind < argc - 1)
{
ERROR("%s, ", argv[optind++]);
}
ERROR("%s\n", argv[optind++]);
print_help();
/* Parameter error */
exit(1);
}
return ((update) ? 1 : 0);
}
/*----------------------------------------------------------------------------
* Summary: Read client version file and return the version string.
* Parameters:
* basedir String with basedir path
* Return: String with version string.
*/
static char *read_version_file(char *basedir)
{
FILE *f;
char *version;
int baselen = strlen(basedir);
int filelen = strlen(VERSIONFILE);
char *filepath = (char *) malloc(sizeof(char) * (baselen + filelen + 1));
/* Construct filepath */
strcpy(filepath, basedir);
strcat(filepath, VERSIONFILE);
DEBUG("Basedir: %s Versionfile: %s ", basedir, VERSIONFILE);
DEBUG("Filepath %s\n", filepath);
/* Allocate memory for version text */
if (! (version = (char *) malloc(11 * sizeof(char))))
{
ERROR("ERROR (3): Memory allocation error.\n");
/* Error while trying to allocate memory for version string */
exit(3);
}
/* Open version file */
if (! (f = fopen(filepath, "r")))
{
ERROR("ERROR (3): Problem while trying to open version file.\n");
ERROR("File: %s.\n", filepath);
/* Error while trying to open version file */
exit(3);
}
/* Read from version file */
fscanf(f, "%s", version);
/* Close version file */
fclose(f);
return version;
}
/*----------------------------------------------------------------------------
* Summary: Contact webservice server to receive the update link.
* Parameters:
* soap SOAP struct
* url URL of the contacted webservice server
* Return: void
*/
void print_update_link(struct soap *soap, char *url)
{
char *updateLink;
int error;
error = soap_call_ns1__getUpdateLink(soap, url, "", &updateLink);
if (error != SOAP_OK)
{
soap_print_fault(soap, stderr);
exit(error == NIC_ERROR ? error : 2);
}
/* Get update link from response */
DEBUG("UpdateLink: %s\n", updateLink);
/* Verify received update link and print appropriate messages */
if (updateLink == NULL)
{
ERROR("ERROR (10): Get update link error (server returned NULL).\n");
/* Server returned an empty update link */
exit(10);
}
/* Print to stdout the update link string */
fprintf(stdout, "%s\n", updateLink);
}
/*----------------------------------------------------------------------------
* Summary: Contact webservice server to verify client version.
* Parameters:
* soap SOAP struct
* url URL of the contacted webservice server
* clientVersion String with directory path
* Return: void
*/
void check_version(struct soap *soap, char *url, char *clientVersion)
{
char *versionFromServer;
int error;
error = soap_call_ns1__getAgentVersion(soap, url, "", &versionFromServer);
if (error != SOAP_OK)
{
soap_print_fault(soap, stderr);
exit(error == NIC_ERROR ? error : 2);
}
DEBUG("Version from server: %s\n", versionFromServer);
/* Check server version string for errors */
if (versionFromServer == NULL)
{
ERROR("ERROR (7): Server returned an empty version string.\n");
/* Empty version */
exit(7);
}
else if (strcmp(versionFromServer, "Invalid version") == 0)
{
ERROR("ERROR (8): Server returned an invalid version string.\n");
/* Invalid version */
exit(8);
}
else if (strcmp(versionFromServer, "Version file not found") == 0)
{
ERROR("ERROR (9): Server did not find the version file.\n");
/* Version file not found (server) */
exit(9);
}
/* Compare client and server versions */
else if (strcmp(clientVersion, versionFromServer) != 0)
{
ERROR("ERROR (4): Client version out-of-date. Update client.\n");
print_update_link(soap, url);
/* Client out-of-date version */
exit(4);
}
/* Client version up-to-date */
fprintf(stderr, "Client version up-to-date\n");
}
/*----------------------------------------------------------------------------
* Summary: Read XML data file and return the xmlInventory string.
* Parameters:
* basedir String with directory path
* Return: String with xml data.
*/
char *read_xml_file(char *basedir)
{
FILE *xmlFile;
int aux;
int i = 0;
int tam = 1;
char filePath[strlen(basedir) + strlen(COLLECTDATAFILE) + 1];
char *xmlInventory = NULL;
/* Construct filePath */
strcpy(filePath, basedir);
strcat(filePath, COLLECTDATAFILE);
DEBUG("%s %s %s\n", basedir, COLLECTDATAFILE, filePath);
/* Open XML collect data file and check for errors */
xmlFile = fopen(filePath, "r");
if (xmlFile == NULL)
{
ERROR("ERROR (12): Problem while trying to open the ");
ERROR("collect_data.xml file.\n");
/* Error while trying to open XML data file */
exit(12);
}
/* Read and copy from data file to string */
xmlInventory = (char *) malloc(sizeof(char));
while ((aux = fgetc(xmlFile)) != EOF)
{
if (tam <= i)
{
tam *= 2;
xmlInventory = (char *) realloc(xmlInventory, tam * sizeof(char));
}
if (xmlInventory == NULL)
{
ERROR("ERROR (12): Memory allocation error for xmlInventory.\n");
/* Error while trying to allocate memory for XML data string */
exit(12);
}
xmlInventory[i++] = aux;
}
/* Close file and return a pointer to "xmlInventory" */
fclose(xmlFile);
return xmlInventory;
}
/*----------------------------------------------------------------------------
* Summary: Contact webservice server, sending the collected XML data.
* Parameters:
* soap SOAP struct
* url URL of the contacted webservice server
* basedir String with basedir path
* Return: void
*/
void inventory(struct soap *soap, char *url, char *basedir)
{
char *invResult, *xmlInventory;
int error;
xmlInventory = read_xml_file(basedir);
error = soap_call_ns1__setInventory(soap, url, "", xmlInventory,
&invResult);
if (error != SOAP_OK)
{
soap_print_fault(soap, stderr);
exit(error == NIC_ERROR ? error : 2);
}
/* Get inventory result from response */
DEBUG("InvResult: %s\n", invResult);
/* Verify response and print appropriate message */
if (strcmp(invResult, "Success") == 0)
{
fprintf(stdout, "XML data sent successful\n");
}
else if (strcmp(invResult, "Error") == 0)
{
ERROR("ERROR (5): XML data sent error\n");
exit(5);
}
else
{
ERROR("ERROR (6): %s\nUnexpected result from server\n", invResult);
exit(6);
}
}