I research the source of PPTP program, and I want to make clear that how does PPTP assign a call ID and peer call ID to the session. i find the code as follows, who can explain the code for me ? Actually i am not clear with the logic. thanks!
/*
* ctrlpacket.c
*
* PPTP Control Message packet reading, formatting and writing.
*
* $Id: ctrlpacket.c,v 1.6 2005/08/03 09:10:59 quozl Exp $
*/
#define C_BITS (sizeof(unsigned int) * 8)
#define C_SEG(x) (x/C_BITS)
#define C_BIT(x) ((1U)<<(x%C_BITS))
static unsigned int activeCalls[(MAX_CALLS / C_BITS) + 1];
#define MAX_CALLS_PER_TCP_LINK 128
#define MAX_CALLS 60
/*
* get_call_id
*
* Assigns a call ID and peer call ID to the session.
*
* args: call_id (OUT) - the call ID for the session
* retn: 0 on success, -1 on failure
*/
int get_call_id(u_int16_t * loc)
{
for (i = 0; i < MAX_CALLS; i++) {
if (!(activeCalls[C_SEG(i)] & C_BIT(i))) {
activeCalls[C_SEG(i)] |= C_BIT(i);
*loc = i;
return 0;
}
}
return -1;
}
activeCallsis a bit map withMAX_CALLSentries. It is composed of an array ofunsigned int, each of which holdsC_BITSbits.C_SEG()finds the rightunsigned intentry in the array for a given call number, andC_BIT()identifies the right bit.The code scans through the bit map, and finds the first un-set bit. It then sets that bit, and returns the index.