开发者

Question with one c function in PPTP

开发者 https://www.devze.com 2023-02-03 18:59 出处:网络
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 ? Actua

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++) {
  开发者_JAVA百科if (!(activeCalls[C_SEG(i)] & C_BIT(i))) {
   activeCalls[C_SEG(i)] |= C_BIT(i);
   *loc = i;
   return 0;
  }
 }
 return -1;
}


activeCalls is a bit map with MAX_CALLS entries. It is composed of an array of unsigned int, each of which holds C_BITS bits.

C_SEG() finds the right unsigned int entry in the array for a given call number, and C_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.


It just implements an array of booleans.

A boolean value only uses one bit and would ordinarily waste the rest, so this code does some bit twiddling in order to fit more than one boolean per array element.

0

精彩评论

暂无评论...
验证码 换一张
取 消