Skip to content
Snippets Groups Projects
Select Git revision
  • 8ce899b084bbdb51429e4dc7d0e037fe62f6cb6e
  • master default
  • build-env
  • bz2
  • portal
  • db-rlp09
  • proinfo_rlp09
  • v1.4.0
  • v1.3.13-buildpackage
  • v1.3.13-client
  • v1.3.12-buildpackage
  • v1.3.12-collect
  • v1.3.13-collect
  • v1.3.11-collect
  • v1.3.10-collect
  • v1.3.11-buildpackage
  • v1.3.10-client
  • v1.3.11-client
  • v1.3.12-client
  • v1.3.10-buildpackage
  • v1.2.0-buildpackage
  • v1.2.0-collect
  • v1.2.0-client
  • v1.1.1-buildpackage
  • v1.1.1-client
  • v1.1.1-collect
  • v1.1.0-client
27 results

client.c

Blame
  • user avatar
    Erik Alexandre Pucci authored and Danilo K. S. Yorinori committed
    print_update_link() calls the server to get the link needed by the collect
    agent to update, printing the link to the standard output.
    
    Signed-off-by: default avatarErik Alexandre Pucci <eap08@c3sl.ufpr.br>
    Acked-by: default avatarDiego Giovane Pasqualin <dgp06@c3sl.ufpr.br>
    Signed-off-by: default avatarDanilo K. S. Yorinori <danilok@c3sl.ufpr.br>
    8ce899b0
    History
    client.c 12.12 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, read)
     *          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
     */
    int main(int argc, char **argv)
    {
        struct soap *soap = soap_new();
        char *url, *inep, *macAddr, *basedir, *clientVersion;
        int arglen, len;
        int update = 0;
        int inv = 0;
    
        /* Initialization *
         *-----------------------------------------------------------------------
         * Verify parameters */
        if ( (argc < 4 || argc > 9) ||
             (((update = !strcmp(argv[1], UPDATEOPTION)) ||
             (inv = !strcmp(argv[1], INVENTORYOPTION)))
             && argc == 8) || (!update && !inv && argc == 7) )
        {
            fprintf(stderr, "Wrong number of parameters.\n");
            fprintf(stderr, "Usage: client [option] url INEP MacAddress ");
            fprintf(stderr, "[proxy_host [proxy_port [proxy_userid ");
            fprintf(stderr, "proxy_passwd]]]\nUse the option ");
            fprintf(stderr, "\"--update\" to check for updates\n");
            fprintf(stderr, "Or use the option \"--inventory\" to send");
            fprintf(stderr, "the XML data\n");
            /* Parameter error */
            exit(1);
        }
    
        /* Set parameters passed by user
         * Basic information */
        url = argv[1 + update + inv];
        inep = argv[2 + update + inv];
        macAddr = argv[3 + update + inv];
    
        /* Proxy informations */
        if (argc >= 5 + update + inv)
        {
            soap->proxy_host = argv[4 + update + inv];
        }
        if (argc >= 6 + update + inv)
        {
            soap->proxy_port = atoi(argv[5 + update + inv]);
        }
        if (argc >= 8 + update + inv)
        {
            soap->proxy_userid = argv[6 + update + inv];
            soap->proxy_passwd = argv[7 + update + inv];
        }
    
        /* Get basedir */
        arglen = strlen(argv[0]);
        len = strlen(exe);
        basedir = (char *) malloc(sizeof(char) * (arglen - len + 1));
        strncpy(basedir, argv[0], (arglen - len));
    
        if (DEBUG)
        {
            printf("Arg: %s basedir: %s\n", argv[0], basedir);
        }
    
        /* End of Initialization
         *-----------------------------------------------------------------------
         * Update option */
        if (update)
        {
            /* Get client version from file */
            if ( ! (clientVersion = read_version_file(basedir)) )
            {
                /* Error when open version file */
                exit(3);
            }
    
            /* Agent Version method */
            check_version(soap, url, clientVersion);
        }
        /* End of Update option
         *------------------------------------------------------------------------
         * Inventory option */
        else if (inv)
        {
            /* Inventory method */
            inventory(soap, url, basedir);
        }
        /* End of update inventory option
         *------------------------------------------------------------------------
         * Options off */
        else
        {
            /* Availability method */
            availability(soap, url, inep, macAddr);
        }
        /* End of options off
         *------------------------------------------------------------------------
         * Finalize execution */
        soap_end(soap);
        soap_free(soap);
        /* Successful */
        exit(0);
        /* End of execution */
    }
    
    /*----------------------------------------------------------------------------
     * 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);
    
        if (DEBUG)
        {
            printf("Basedir: %s Versionfile: %s ", basedir, VERSIONFILE);
            printf("Filepath %s\n", filepath);
        }
    
        /* Allocate memory for version text */
        if ( ! (version = (char *) malloc(11 * sizeof(char))) )
        {
            fprintf(stderr, "Memory allocation error.\n");
            return NULL;
        }
    
        /* Open version file */
        if ( ! (f = fopen(filepath, "r")) )
        {
            fprintf(stderr, "Problem while trying to open version file.\n");
            fprintf(stderr, "File: %s.\n", filepath);
            return NULL;
        }
    
        /* 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;
    
        if (soap_call_ns1__getUpdateLink(soap, url, "", &updateLink) != SOAP_OK)
        {
            soap_print_fault(soap, stderr);
            exit(2);
        }
    
        /* Get update link from response */
        if (DEBUG)
        {
            printf("UpdateLink: %s\n", updateLink);
        }
    
        /* Verify received update link and print appropriate messages */
        if (updateLink == NULL)
        {
            fprintf(stderr, "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;
    
        if (soap_call_ns1__getAgentVersion(soap, url, "", &versionFromServer) !=
            SOAP_OK)
        {
            soap_print_fault(soap, stderr);
            exit(2);
        }
    
        if (DEBUG)
        {
            printf("Version from server: %s\n", versionFromServer);
        }
    
        /* Check server version string for errors */
        if (versionFromServer == NULL)
        {
            fprintf(stderr, "Server returned an empty version string.\n");
            /* Empty version */
            exit(7);
        }
        else if (strcmp(versionFromServer, "Invalid version") == 0)
        {
            fprintf(stderr, "Server returned an invalid version string.\n");
            /* Invalid version */
            exit(8);
        }
        else if (strcmp(versionFromServer, "Version file not found") == 0)
        {
            fprintf(stderr, "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)
        {
            fprintf(stderr, "Client version out-of-date. Update client.\n");
            /* Client out-of-date version */
            exit(4);
        }
    
        /* Client version up-to-date */
        fprintf(stdout, "Client version up-to-date\n");
    }
    
    /*----------------------------------------------------------------------------
     * Summary: Contact webservice server, sending the INEP and MacAddress of the
     *          machine.
     * Parameters:
     *      soap        SOAP struct
     *      url         URL of the contacted webservice server
     *      inep        INEP of the school
     *      macAddr     Machine address
     * Return: void
     */
    void availability(struct soap *soap, char *url, char *inep, char *macAddr)
    {
        char *availResult;
    
        if (soap_call_ns1__setAvailability(soap, url, "", inep, macAddr,
            &availResult) != SOAP_OK)
        {
            soap_print_fault(soap, stderr);
            exit(2);
        }
    
        /* Get availability result from response */
        if (DEBUG)
        {
            printf("AvailResult: %s\n",availResult);
        }
    
        /* Verify response and print approriate message */
        if (strcmp(availResult, "Success") == 0)
        {
            fprintf(stdout, "Data sent successful\n");
        }
        else if (strcmp(availResult, "Error") == 0)
        {
            fprintf(stdout, "Data sent error\n");
            exit(5);
        }
        else
        {
            fprintf(stdout, "%s\nUnexpected result from server\n", availResult);
            exit(6);
        }
    }
    
    /*----------------------------------------------------------------------------
     * 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);
        if (DEBUG)
        {
            printf("%s %s %s\n", basedir, COLLECTDATAFILE, filePath);
        }
    
        /* Open XML collect data file and check for errors */
        xmlFile = fopen(filePath, "r");
        if ( xmlFile == NULL )
        {
            fprintf(stderr, "Problem while trying to open the ");
            fprintf(stderr, "collect_data.xml file.\n");
            return NULL;
        }
    
        /* 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)
            {
                fprintf(stderr, "Memory allocation error for xmlInventory.\n");
                return NULL;
            }
            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;
    
        xmlInventory = read_xml_file(basedir);
    
        if (soap_call_ns1__setInventory(soap, url, "", xmlInventory, &invResult)
            != SOAP_OK)
        {
            soap_print_fault(soap, stderr);
            exit(2);
        }
    
        /* Get inventory result from response */
        if (DEBUG)
        {
            printf("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)
        {
            fprintf(stdout, "XML data sent error\n");
            exit(5);
        }
        else
        {
            fprintf(stdout, "%s\nUnexpected result from server\n", invResult);
            exit(6);
        }
    }