home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / ip / ppp / mac / macppp2.0.1-src.hqx / MacPPP 2.0.1 src / lap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-15  |  5.5 KB  |  206 lines

  1. /*
  2.  *  lap.c
  3.  *
  4.  * This module contains the MacTCP Lap interface routines.
  5.  *
  6.  * Copyright 1992-1993 Merit Network, Inc. and The Regents of the
  7.  *  University of Michigan.  Usage of this source code is restricted
  8.  *  to non-profit, non-commercial purposes.  The source is provided
  9.  *  "as-is", without warranty.
  10.  *
  11.  */
  12. #include "ppp.h"
  13. #include <Folders.h>
  14. #include <GestaltEqu.h>
  15. /*
  16. *   PPPAttachPH
  17. *    MacTCP passes us the address of the IP protocol handler, which we must
  18. *    call after receiving an IP packet over the serial line.
  19. *    Called at application level or system task time.
  20. */
  21.  
  22. OSErr PPPAttachPH(LapInfo *lap, voidProcPtr ptr) {
  23.     lap->ip_ph = ptr;    /* save address of routine to call on receive done */
  24.     return (noErr);
  25. }
  26.  
  27. /*  PPPClose
  28. *    Called by MacTCP to close the LAP.
  29. *    Called at application level or system task time.
  30. */
  31. OSErr PPPClose(LapInfo * lap) {
  32.     fsm_close(&(lap->ppp_fsm[Lcp]));    /* send close event to LCP */
  33.     while (lap->ppp_phase != pppDEAD) {    /* wait for link to close */
  34.         SystemTask();
  35.     }
  36. }
  37.  
  38. /*
  39. *   PPPConfigure
  40. *    Called by MacTCP driver after LAP initialization.  This routine 
  41. *    fills in the IP address and netmask.
  42. */
  43.  
  44. void PPPConfigure(LapInfo *lap) {
  45.     if (lap->ppp_fsm[IPcp].state == fsmOPENED) { /* check if IPCP is opened */
  46.         /* return IP address negotiated by IPCP to MacTCP */
  47.         lap->cur_ip_addr = lap->ipcp_i.local.work.ipcp_option.address;
  48.     
  49.         /*  set netmask to something reasonable */
  50.         if (lap->cur_ip_addr & 0xC0000000)
  51.             lap->cur_net_mask = 0xffffff00;
  52.         else if (lap->cur_ip_addr & 0x80000000)
  53.             lap->cur_net_mask = 0xffff0000;
  54.         else
  55.             lap->cur_net_mask = 0xff000000;
  56.         return;
  57.     }
  58.     /* lap is closed, so try to re-open */
  59.     PPP_DEBUG_CHECKS("\pConfigure on closed lap");
  60.     if (lap->transProc != nil) {
  61.         (*(lap->transProc))(TransitionOpen);    /* try reopening lap */
  62.     }
  63. }
  64.  
  65. /*
  66. *   PPPControl
  67. *    This routine is never called by the current version of MacTCP (1.1.1)
  68. */
  69.  
  70. OSErr PPPControl(LapInfo * lap) {
  71.     return (noErr);
  72. }
  73. /*
  74. *   PPPDetachPH
  75. *    Called when MacTCP driver is closing.  This routine releases binding
  76. *    to the IP packet reception routine.
  77. */
  78. OSErr PPPDetachPH(LapInfo *lap) {
  79.     lap->ip_ph = nil;
  80.     return (noErr);
  81. }
  82.  
  83. /*
  84. *   PPPFault
  85. *    MacTCP detected problem in communication
  86. */
  87. void PPPFault(LapInfo *lap, ip_addr lnd) {
  88.     lap->faults++;
  89. }
  90.  
  91. /*
  92. *   PPPFindGW
  93. *   The concept of a gateway isn't relevent for a point-to-point link,
  94. *   but we need to provide an address to MacTCP.
  95. */
  96. void PPPFindGW(struct LapInfo *lap)
  97. {
  98.     /* Just use our IP address as the gateway address */
  99.     lap->lc.dfl_gateway_addr = lap->cur_ip_addr;
  100.     return;
  101. }
  102. /*
  103. *   PPPInit
  104. *    Called when the MacTCP driver is opened.  We attempt to open the
  105. *   PPP session at this point, by sending an open event to the IPCP
  106. *   protocol.
  107. */
  108. OSErr PPPInit(LapInfo *lap, longProcPtr transitionProc) {
  109.  
  110.     if (lap->ppp_flags & CLOSE_PPP) {
  111.         lap->ppp_flags &= ~CLOSE_PPP;
  112.         lap->transProc = nil;
  113.         return -1;
  114.     }
  115.     if (lap->ppp_flags & (ECHO_FAIL | IDLE_TIMEOUT)) {
  116.         link_open(lap);        /* handle special cases */
  117.         lap->ppp_flags &= ~(ECHO_FAIL | IDLE_TIMEOUT);
  118.     } else {
  119.         fsm_close( &(lap->ppp_fsm[Lcp]) ); /* put in initial state */
  120.         fsm_close( &(lap->ppp_fsm[IPcp]) ); /* put in initial state */
  121.         fsm_open( &(lap->ppp_fsm[IPcp]) ); /* send open event to IPCP */
  122.     }
  123.     if (lap->ppp_fsm[IPcp].state != fsmOPENED) { /* check if IPCP is up */
  124.         lap->transProc = nil;    /* reset tranproc pointer */
  125.         return -1;        /* indicate init failed to MacTCP */
  126.     }
  127.     if ( transitionProc != nil ) {    /* if this is first call */
  128.         lap->ip_ph = nil;                /* reset packet handler pointer */
  129.         lap->savProc = transitionProc;    /* set pointer to transition proc */
  130.     }
  131.     lap->transProc = lap->savProc;    /* refresh transition proc pointer */
  132.     return noErr;        /* PPP is up */
  133. }
  134. /*
  135. *   PPPOpen
  136. *    Not called by current version of MacTCP (1.1.1)
  137. */
  138. OSErr PPPOpen(LapInfo *lap) {
  139.     return (noErr);
  140. }
  141. /*
  142. *   PPPProbe
  143. *    Used only for dynamic addressing.  Not useful for MacPPP.
  144. */
  145. Boolean PPPProbe(LapInfo *lap, ip_addr addr) {
  146.     return (false);
  147. }
  148. /*
  149. *   PPPRegister
  150. *    Assume the remote end gave us a good IP address.
  151. */
  152. Boolean PPPRegister(LapInfo *lap) {
  153.     lap->addressConflict = FALSE;
  154.     return (1);
  155. }
  156. /*
  157. *   PPPStatistics
  158. *    Called by MacTCP to acquire link speed information and for
  159. *    network management.  Decision on protocol timeouts and usable MTU
  160. *    are made with the values returned by this call.
  161. */
  162. OSErr PPPStatistics(struct LapInfo *lap, struct LapStats *statsPtr) {
  163.     statsPtr->ifMaxMTU = lap->outMaxPacketSize;
  164.     statsPtr->ifSpeed = (unsigned long) lap->configdata.baudrate;
  165.     statsPtr->ifPhyAddrLength = 0;
  166.     statsPtr->ifPhysicalAddress = nil;
  167.     return (noErr);
  168. }
  169. /*
  170. *   PPPUnload
  171. *    Called by MacTCP driver to release the memory held by the
  172. *    LAP.
  173. */
  174. void PPPUnload(struct LapInfo *lap) {
  175.     if (lap->ppp_phase != pppDEAD)
  176.         link_close();    /* close the link */
  177.     SetLAPPtr(nil);            /* set lap pointer to nil */
  178.     DisposPtr((Ptr) lap);    /* dispose of memory */
  179.     return;
  180. }
  181. /*
  182. *   PPPWrite
  183. *    MacTCP calls this routine to transmit an IP packet on our link.  
  184. *    The write requests are queued for transmission.
  185. */
  186. void IOCompleted(LapInfo *, struct ipbuf *);
  187.  
  188. OSErr PPPWrite(LapInfo *lap, ip_addr addr, struct ipbuf *ipb) {
  189.  
  190.     lap->idle_timer = 0;    /* clear idle timer */
  191.     if (lap->ppp_fsm[IPcp].state != fsmOPENED) {
  192.         PPP_DEBUG_CHECKS("\pPPPWrite: IPcp not opened");
  193.         IOCompleted(lap, ipb);        /* fake the completion routine */
  194.         /* (*(lap->transProc))(TransitionOpen); */ /* signal open */
  195.         lap->OutError++;
  196.         return (noErr);        /* return no error */
  197.     }
  198.  
  199.     /* save ptr to LapInfo for IP */
  200.     ipb->lap = lap;
  201.     
  202.     /* try to get a free transmit queue element */
  203.     return(QueueFrame(lap, nil, ipb));
  204. }
  205.  
  206.