0

how can i correctly send a struct from the server to the client in ansi-c sun-rpc?

in my test.x IDL file i defined a struct cluster with a string and an int and a type clusters which is a variable-length array of cluster elements:

struct cluster {
    string name<255>;
    int debuglevel;
};

typedef cluster clusters<32>;

i then changed the stubs generated by rpcgen like

test_server.c

clusters *
test_1_svc(void *argp, struct svc_req *rqstp)
{

    static clusters result;

    cluster cl1, cl2;

    cl1.name="cl1";
    cl1.debuglevel="1";
    cl2.name="cl2";
    cl2.debuglevel="2";

    cluster clist[2];

    clist[0]=cl1;
    clist[1]=cl2;

    result.clusters_len = 2;
    result.clusters_val = &clist;

    /*
     * insert server code here
     */

    return(&result);
}

and test_client.c

test_prog_1( char* host )
{
    CLIENT *clnt;
    clusters  *result_1;
    char*  test_1_arg;
    clnt = clnt_create(host, test_PROG, test_VERS, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror(host);
        exit(1);
    }
    result_1 = test_1((void*)&test_1_arg, clnt);
    if (result_1 == NULL) {
        clusters* rec_cls = malloc(2*sizeof(struct cluster));
        if(xdr_clusters(&result_1, rec_cls)){
                printf("got xdr_clusters");
        }
        clnt_perror(clnt, "call failed:");
    }
    clnt_destroy( clnt );
}

Both compile, but the server often segfaults after one or two request runs by the client and on the clientside the xdr_clusters function never returns true. It seems like some kind of memory mismanagement and I'm also not sure if I'm handling the serialization on the server-side correctly.

I just filled result.clusters_len and result.clusters_val with the appropiate values like they are defined in test.h (by rpcgen):

typedef struct {
    u_int clusters_len;
    cluster *clusters_val;
} clusters;

Do I have to make use of xdr_clusters on the server side for this to correctly serialize the result?

thank you

larso
  • 3
  • 1
  • 5

1 Answers1

0

okay, i figured my mistakes, lets summarize them:

  • know how to initialize an int correctly (without the quotes of course...)
  • forget about that clist nonsense, just malloc the internal pointer of the result struct directly
  • read the damn compiler warnings: when it tells you, that there are functions declared implicitly and you didn't want implicit declarations, then there is possibly something missing, in my case i needed to include stdlib.h and stdio.h to get malloc, printf and exit functions for server and client stubs.
  • on the clientside: why should we do anything except throwing an error if the result is NULL? see the new client code below to check correct result printing

test_server.c

    test_1_svc(void *argp, struct svc_req *rqstp){

        static clusters result;

        cluster cl1, cl2;

        cl1.name="cl1";
        cl1.debuglevel=1;
        cl2.name="cl2";
        cl2.debuglevel=2;

        result.clusters_len = 2;
        result.clusters_val = malloc(2*sizeof(struct cluster));

        result.clusters_val[0]=cl1;
        result.clusters_val[1]=cl2;

        return(&result);
    }

test_client.c

test_prog_1( char* host )
{
    CLIENT *clnt;
    clusters  *result_1;
    char*  test_1_arg;
    clnt = clnt_create(host, test_PROG, test_VERS, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror(host);
        exit(1);
    }
    result_1 = test_1((void*)&test_1_arg, clnt);
    if (result_1 == NULL) {
        clnt_perror(clnt, "call failed:");
    }else{
        printf("I got %d cluster structs in an array\n",result_1->clusters_len);
        int j;
        for(j=0;j<result_1->clusters_len;j++){
            printf("cluster #%d: %s@runlevel %d\n",j,result_1->clusters_val[j].name,result_1->clusters_val[j].debuglevel);
        }
    }
    clnt_destroy( clnt );
}

as a result, we get some nice values on the clientside printed and of course no segfaults anymore on the serverside:

lars$ ./test_client localhost
I got 2 cluster structs in an array
cluster #0: cl1@runlevel 1
cluster #1: cl2@runlevel 2
larso
  • 3
  • 1
  • 5