next up previous contents
Next: Opening Connections Up: Example Protocol Previous: Header Files

Initialization

 

The C functions that implement a given protocol can be distributed across multiple .c files. In the case of ASP, they are all contained in a single file named asp.c. The rest of this section walks through these functions one at at time.

We begin with function asp_init, which is called at system startup time. This function initializes the ASP protocol object, including protocol state and maps. It then does an xOpenEnable call on the protocol configured below it (usually IP) to inform it that ASP is willing to accept messages from any host. xGetDown is used to obtain a handle for the protocol that was configured below ASP. (In theory, ASP may have more than one protocol configured below it; the second argument to xGetDown asks for the first of these.)

void
asp_init(Protl self)
{
    ProtlState *pstate;
    Protl      llp;
    Part       part;

    getproc_protl(self);

    /* create and initialize protocol state */
    pstate = X_NEW(ProtlState);
    bzero((char *)pstate, sizeof(ProtlState));
    self->state = (void *)pstate;
    pstate->activemap  = mapCreate(ACTIVE_MAP_SIZE, sizeof(ActiveId));
    pstate->passivemap = mapCreate(PASSIVE_MAP_SIZE, sizeof(PassiveId));

    /* find lower level protocol and do a passive open on it */
    llp = xGetProtlDown(self, 0);
    if (!xIsProtl(llp))
        Kabort("ASP could not get lower protocol");
    partInit(&part, 1);
    partPush(part, ANY_HOST, 0);
    if (xOpenEnable(self, self, llp, &part) == XK_FAILURE) {
        xTrace0(aspp, TR_ALWAYS,
               "asp_init: openenable on lower protocol failed");
        xFree((char *) pstate);
        return;
    }
}

Because in this example we are dealing not just with the interfaces between objects but with their implementation, we need to deal with some of the internal structure of the Protl and Sessn objects, which we have glossed over up until now. For example, in the preceding code, we initialized the state data structure in the ASP Protl object. One important part of initialization of the Protl object is to fill in the table that will cause operations on the object (e.g., xOpen) to invoke the appropriate function to implement that operation (e.g., aspOpen). In the case of ASP, the subroutine getproc_protl fills out this operation table:

static void
getproc_protl(Protl p)
{
    /* fill in the function pointers to implement protocol operations */
    p->open         = aspOpen;
    p->openenable   = aspOpenEnable;
    p->demux        = aspDemux;
    p->controlprotl = aspControlProtl;
}



Larry Peterson
Wed Feb 21 13:58:06 MST 1996