⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap.c

📁 在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LEA_4S的驱动,位置速寻算法,语音芯片ISD4004的录放音驱动,LED页面管理等等.从启动代码到操作系统的移植以及到业
💻 C
📖 第 1 页 / 共 3 页
字号:
   inp = inp;
   len = len;
   //ConPrintf("ChapReceiveSuccess: link:%x Rcvd id %d.", cstate->mppp, id);
   #if CHAPME_DEBUG
   Printu("ChapReceiveSuccess\r\n");
   #endif
   if (cstate->clientstate == CHAPCS_OPEN)
   {
      /* presumably an answer to a duplicate response */
      #if CHAPME_DEBUG
      Printu("a duplicate response\r\n"); 
      #endif
      return;
   }

   if (cstate->clientstate != CHAPCS_RESPONSE) 
   {
      /* don't know what this is */
      #if CHAPME_DEBUG
      Printu("Error Type\r\n");
      #endif
     // ConPrintf("ChapReceiveSuccess: in state %d\n",
        // cstate->clientstate);
      return;
   }
   
   ppp_cleartimer(cstate->mppp);

   cstate->clientstate = CHAPCS_OPEN;

   ppp_lowerup(cstate->mppp, IPCP_STATE);

   return;
}


/* FUNCTION: ChapReceiveFailure()
 *
 * ChapReceiveFailure - Receive failure.
 *
 * PARAM1: chap_state * cstate
 * PARAM2: u_char *inp
 * PARAM3: u_char id
 * PARAM4: int len
 *
 * RETURNS: 
 */

static void
ChapReceiveFailure(chap_state * cstate, u_char id)
{

   //ConPrintf("ChapReceiveFailure: link: %lx, Rcvd id %d.", cstate->mppp, id);

   if (cstate->clientstate != CHAPCS_RESPONSE)
   {
      /* don't know what this is */
      //ConPrintf("ChapReceiveFailure: in state %d\n",
       //cstate->clientstate);
      return;
   }

   ppp_cleartimer(cstate->mppp);

   //ConPrintf("CHAP authentication failed");
   ppp_auth_failed(cstate->mppp, 0);
}


/* FUNCTION: ChapSendChallenge()
 *
 * ChapSendChallenge - Send an Authenticate challenge.
 *
 * PARAM1: chap_state *cstate
 *
 * RETURNS: 
 */

static void
ChapSendChallenge(chap_state * cstate)
{
   u_char * outp;
   int      chal_len, name_len;
   u_short  outlen;

   chal_len = (int)cstate->chal_len;
   name_len = strlen(cstate->chal_name);
   outlen = (u_short)(CHAP_HEADERLEN + 1 + chal_len + name_len);
   outp = cstate->mppp->lastci;

   if (outlen > MAX_CILEN )
      panic("chap msg overflow 1");

   /* build a CHAP header */
   outp = ppp_putshort(outp, PPP_CHAP);
   *outp++ = CHAP_CHALLENGE;
   *outp++ = cstate->chal_id;
   outp = ppp_putshort(outp, outlen);
   *outp++ = (u_char)chal_len;      /* put length of challenge */
   MEMCPY(outp, cstate->challenge, chal_len);
   outp += chal_len;

   MEMCPY(outp, cstate->chal_name, name_len);   /* append hostname */

   ppp_sendctl(cstate->mppp, AUTH_STATE, 0, 
      cstate->mppp->lastci, outlen + 2, 0);

   //ConPrintf("ChapSendChallenge: link: %lx, Sent id %d.", 
      //cstate->mppp, cstate->chal_id);

   ppp_settimer(cstate->mppp, ChapChallengeTimeout, cstate->timeouttime, 0);
   ++cstate->chal_transmits;
}


/* FUNCTION: ChapSendStatus()
 *
 * ChapSendStatus - Send a status response (ack or nak).
 *
 * PARAM1: chap_state *cstate
 * PARAM2: int code
 *
 * RETURNS: 
 */

static void
ChapSendStatus(chap_state * cstate, int code)
{
   u_char * outp;
   u_short  outlen;
   u_short  msglen;
   char  * msg;

   if (code == CHAP_SUCCESS)
      msg = "CHAP OK";
   else 
      msg = "CHAP failure";

   msglen = (u_short)strlen(msg);
   outlen = CHAP_HEADERLEN + msglen;
   outp = cstate->mppp->lastci;

   if ( outlen > MAX_CILEN )
      panic("chap msg overflow 2");

   /* build the chap status packet */
   outp = ppp_putshort(outp, PPP_CHAP);
   *outp++ = (u_char)code;
   *outp++ = cstate->chal_id;
   outp = ppp_putshort(outp, outlen);
   MEMCPY(outp, msg, msglen);

   ppp_sendctl(cstate->mppp, AUTH_STATE, 0, cstate->mppp->lastci, 
      outlen + 2, 0);

   //ConPrintf("ChapSendStatus: Sent code %d, id %d.", code,
      //cstate->chal_id);
}


/* FUNCTION: ChapGenChallenge()
 *
 * ChapGenChallenge is used to generate a pseudo-random challenge string of
 * a pseudo-random length between min_len and max_len.  The challenge
 * string and its length are stored in *cstate, and various other fields of
 * *cstate are initialized.
 *
 * PARAM1: chap_state * cstate
 *
 * RETURNS: 
 */

static void
ChapGenChallenge(chap_state * cstate)
{
   unsigned int chal_len;
   u_char * ptr   =  cstate->challenge;
   unsigned int i;

#ifdef MSCHAP_SUPPORT
   if ( cstate->chal_type == CHAP_DIGEST_MSCHAP )
      chal_len = 8;
   else
#endif
   {
      /* pick a random challenge length between MIN_CHALLENGE_LENGTH 
       * and MAX_CHALLENGE_LENGTH. 11/16/96 - This new code assumes 
       * MAX & MIN 
       */
      chal_len = 0;
      while (chal_len == 0)   /* get non-zero chal_len */
         chal_len = (unsigned)rand() & (MAX_CHALLENGE_LENGTH-1);   /* get length < MAX */
      while (chal_len < MIN_CHALLENGE_LENGTH)   /* raise len 'til >= MIN */
         chal_len <<= 1;
   }

   cstate->chal_len = (u_char)chal_len;
   cstate->chal_id = ++cstate->id;
   cstate->chal_transmits = 0;
   cstate->mschap_failures = 0;

   /* generate a random string */
   for (i = 0; i < chal_len; i++ )
      *ptr++ = (char) (rand() & 0x00ff);
}


/* FUNCTION: ChapSendResponse()
 *
 * ChapSendResponse - send a response packet with values as specified
 * in * cstate.
 * 
 * PARAM1: chap_state * cstate
 *
 * RETURNS: 
 */

static void
ChapSendResponse(chap_state * cstate)
{
   u_char *    outp;
   u_short     outlen, md_len, name_len;

   md_len = (u_short)cstate->resp_length;
   name_len = (u_short)strlen(cstate->resp_name);

   outp = cstate->mppp->lastci;
   outlen = (u_short)(CHAP_HEADERLEN  +1+ md_len + name_len);
   if ( outlen > MAX_CILEN ){
      panic("chap msg overflow 3");
      #if CHAPME_DEBUG
      Printu("chap msg overflow 3\r\n");
      #endif	    
   }
   /* build CHAP packet in buffer */
   outp = ppp_putshort(outp, PPP_CHAP);
   *outp++ = CHAP_RESPONSE;      /* we are a response */
   *outp++ = cstate->resp_id;    /* copy id from challenge packet */
   outp = ppp_putshort(outp, outlen);        /* packet length */
   *outp++ = (u_char)md_len;                 /* length of MD */

   MEMCPY(outp, cstate->response, md_len);   /* copy MD to buffer */
   outp += md_len;
   #if CHAPME_DEBUG
   Printu("name_len:%x:%s\r\n",name_len,cstate->resp_name);
   #endif
   MEMCPY(outp, cstate->resp_name, name_len);   /* append our name */

   #if CHAPME_DEBUG
   Printu("\r\n===========CHAP_SEND:=============\r\n");
   PrintString('x',cstate->mppp->lastci,outlen+2);
   Printu("\r\n====================================\r\n");
   #endif
   /* send the packet */
   ppp_sendctl(cstate->mppp, AUTH_STATE, 0, cstate->mppp->lastci, 
      outlen + 2, 0);

   cstate->clientstate = CHAPCS_RESPONSE;
   ppp_settimer(cstate->mppp, ChapResponseTimeout, cstate->timeouttime, 0);
   ++cstate->resp_transmits;
}

#ifdef CHAP_PRINT
/*
 * ChapPrintPkt - print the contents of a CHAP packet.
 */
char *
ChapCodenames[] =
{
   "Challenge", "Response", "Success", "Failure"
};



/* FUNCTION: ChapPrintPkt()
 * 
 * PARAM1: u_char * p
 * PARAM2: int length
 * PARAM3: void (*printer)(void *, char *, ...)
 * PARAM6: void * arg
 *
 * RETURNS: 0 if error, else length passed
 */

int
ChapPrintPkt(u_char * p, 
   int   plen,
   void (*printer) __ARGS((void *, char *, ...)), 
   void *   arg)
{
   int      code, id, len;
   int      clen, nlen;
   u_char   x;

   if (plen < CHAP_HEADERLEN)
      return 0;
   code = *p++;
   id = *p++;
   len = ppp_getshort(p);
   p += 2;
   if (len < CHAP_HEADERLEN || len > plen)
      return 0;

   if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
      printer(arg, " %s", ChapCodenames[code-1]);
   else
      printer(arg, " code=0x%x", code);
   printer(arg, " id=0x%x", id);
   len -= CHAP_HEADERLEN;

   switch (code) 
   {
   case CHAP_CHALLENGE:
   case CHAP_RESPONSE:
      if (len < 1)
         break;
      clen = p[0];
      if (len < clen + 1)
         break;
      ++p;
      nlen = len - clen - 1;
      printer(arg, " <");
      for (; clen > 0; --clen) 
      {
         x = *p++;
         printer(arg, "%.2x", x);
      }
      printer(arg, ">, name = ");
      print_string((char *)p, nlen, printer, arg);
      break;
   case CHAP_FAILURE:
   case CHAP_SUCCESS:
      printer(arg, " ");
      print_string((char *)p, len, printer, arg);
      break;
      default:
      for (clen = len; clen > 0; --clen) 
      {
         x = *p++;
         printer(arg, " %.2x", x);
      }
   }

   return len + CHAP_HEADERLEN;
}
#endif   /* CHAP_PRINT */

#ifdef NET_STATS

char *   chap_client_states[] =
{
   "INITIAL",  /* Lower layer down, not opened */
   "CLOSED",   /* Lower layer up, not opened */
   "PENDING",  /* Auth us to peer when lower up */
   "LISTEN",   /* Listening for a challenge */
   "RESPONSE", /* Sent response, waiting for status */
   "OPEN",     /* We've received Success */
};

/*
 * Server (authenticator) states.
 */
char *   chap_server_states[] =
{
   "INITIAL",     /* Lower layer down, not opened */
   "CLOSED",      /* Lower layer up, not opened */
   "PENDING",     /* Auth peer when lower up */
   "INITIAL_CHAL",/* We've sent the first challenge */
   "OPEN",        /* We've sent a Success msg */
   "RECHALLENGE", /* We've sent another challenge */
   "BADAUTH",     /* We've sent a Failure msg */
};


/* FUNCTION: chap_stat()
 *
 * chap_stat() - Added for calling from netport menu system
 * 
 * PARAM1: M_PPP mppp
 *
 * RETURNS: 
 */

int
chap_stat(M_PPP mppp)
{
   chap_state * cstate = &mppp->chap;

   dprintf("Chap stats for link %lx, iface %s.\n", mppp, mppp->ifp );
   dprintf("client state: %s\n", chap_client_states[cstate->clientstate]);
   dprintf("server state: %s\n", chap_server_states[cstate->serverstate]);
   dprintf("challenge xmits:  %d\n", cstate->chal_transmits);
   dprintf("challenge replys: %d\n", cstate->resp_transmits);

   return 0;
}
#endif   /* NET_STATS */

#endif   /* CHAP_SUPPORT */

/* end of file chap.c */


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -