[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Communications Programming Concepts

Using XDR Array Examples

The following four examples illustrate eXternal Data Representation (XDR) arrays.

Example A

A user on a networked machine can be identified by the machine name (using the gethostname subroutine), the user's UID (using the geteuid subroutine), and the numbers of the group to which the user belongs (using the getgroups subroutine). A structure with this information and its associated XDR subroutine could be coded as follows:

struct netuser {
   char    *nu_machinename;
   int     nu_uid;
   u_int   nu_glen;
   int     *nu_gids;
};
#define NLEN 255    /*  machine names < 256 chars  */
#define NGRPS 20    /*  user can't be in > 20 groups  */
bool_t
xdr_netuser(xdrs, nup)
   XDR *xdrs;
   struct netuser *nup;
{
   return(xdr_string(xdrs, &nup->nu_machinename, NLEN) &&
      xdr_int(xdrs, &nup->nu_uid) &&
      xdr_array(xdrs, &nup->nu_gids, &nup->nu_glen,
          NGRPS, sizeof (int), xdr_int));
}

Example B

To code a subroutine to use fixed-length arrays, rewrite Example A as follows:

#define NLEN 255
#define NGRPS 20
struct netuser {
       char *NUMachineName;
       int nu_uid;
       int nu_gids;
};
bool_t
xdr_netuser (XDRS, nup
       XDR *xdrs;
       struct netuser *nup;
{
       int i;
       if (!xdr_string(xdrs,&nup->NUMachineName, NLEN))
        return (FALSE);
       if (!xdr_int (xdrs, &nup->nu_uid))
        return (FALSE);
       for (i = 0; i < NGRPS; i+++) {
         if (!xdr_int (xdrs, &nup->nu_uids[i]))
             return (FALSE);
       }
       return (TRUE);
}

Example C

A party of network users can be implemented as an array in the netuser structure. The declaration and its associated XDR routines are as follows:

struct party {
    u_int p_len;
    struct netuser *p_nusers;
};
#define PLEN 500    /*  max number of users in a party  */
bool_t
xdr_party(xdrs, pp)
    XDR *xdrs;
    struct party *pp;
{
    return(xdr_array(xdrs, &pp->p_nusers, &pp->p_len, PLEN,
        sizeof (struct netuser), xdr_netuser));
}

Example D

The main function's well-known parameters, argc and argv, can be combined into a structure. An array of these structures can make up a history of commands. The declarations and XDR routines can have the following syntax:

struct cmd {
    u_int c_argc;
    char **c_argv;
};
#define ALEN 1000   /*  args cannot be > 1000 chars  */
#define NARGC 100   /*  commands cannot have > 100 args  */
struct history {
    u_int h_len;
    struct cmd *h_cmds;
};
#define NCMDS 75    /*  history is no more than 75 commands  */
bool_t
xdr_wrap_string(xdrs, sp)
    XDR *xdrs;
    char **sp;
{
    return(xdr_string(xdrs, sp, ALEN));
}
bool_t
xdr_cmd(xdrs, cp)
    XDR *xdrs;
    struct cmd *cp;
{
    return(xdr_array(xdrs, &cp->c_argv, &cp->c_argc, NARGC,
        sizeof (char *), xdr_wrap_string));
}
bool_t
xdr_history(xdrs, hp)
    XDR *xdrs;
    struct history *hp;
{
    return(xdr_array(xdrs, &hp->h_cmds, &hp->h_len, NCMDS,
        sizeof (struct cmd), xdr_cmd));
}

[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]