client_root_jj.c

来自「PPPoE协议在Psos中的实现源代码」· C语言 代码 · 共 280 行

C
280
字号
/*********************************************************************/
/* lijj 04.5.22                                                      */
/*********************************************************************/

/*Override global tracing setting for this file.*/
#undef  TRACING
#define TRACING 1

/*From CLib*/
#include <stdarg.h>
#include <stdio.h>
#include <tm1/tsaComponent.h>
#include <tmlib/dprintf.h>
#include "c:\trimedia\include\tm1\mmio.h"
#include <tm1/tmBoard.h>
#include <tm1/tmProcessor.h>

/*From pSOSystem*/
#include <pna.h>
#include <dns.h>
/*#include <genif.h>*/

/*#include <drv_intf.h>*/
#include <gsblk.h>
#include <ni_ppp.h>
#include <configs.h>
#include <tm1/tmIntPins.h>

/*Local*/
#include "ppp.h"
#include "sys_conf.h"
#include "pppoe.h"
#include "traceppp.h"
#include "cs8900a.h"


#define ioctl ioctlsocket
#define close closesocket

#define STRLEN(s) (sizeof (s) - 1) /*length of a string constant*/
#define K 1024
#define M (K*K)

extern NODE_CT        NodeCfg;
extern struct ppp_cfg pppcfgtab [];
#define SEC2TICKS(sec) (NodeCfg.psosct->kc_ticks2sec*(sec))


#define PPP_LOCAL_IP 0
#define PPP_PEER_IP  0

#define COUNT(a) (sizeof (a)/sizeof *(a))
#define satosin(sa) ((struct sockaddr_in *)(sa))
/*------------------------------------------------------------------*/
/*SETTING THE CLIENT TASK PARAMETERS                                */
/*The following define the constants for the client task.  Root task*/
/*creates client task with the following parameters.  These are     */
/*passed in as parameters to tmosTaskCreate and tmosTaskStart calls */
/*------------------------------------------------------------------*/
#define CLIENT_NAME     "CLNT" /*Name of the client task*/
#define CLIENT_PRIO     200    /*Priority of the client task*/
#define CLIENT_USRSTACK 80000  /*User stack size of  client task*/
#define CLIENT_SYSSTACK 80000  /*Supervisor stack size*/
#define CLIENT_FLAGS    0x00   /*Flags for client task*/
#define CLIENT_INITMODE 0x2000 /*Initial mode of the client task*/

#define SERVER_PORT 2000       /*Server port*/
#define DATE        0x07C6081C /*Date set by tm_set call*/

#define HTTP_REQUEST         \
   "GET <page> HTTP/1.1\r\n" \
   "Host: <host name>\r\n"   \
   "\r\n"

/********************************************************************/
/*Do a compile-time check to see if pNA is included in the build.   */
/********************************************************************/
#if SC_PNA == NO
#error Need pNA + for this application (see sysgen/sys_conf.h)
#endif

/*------------------------------------------------------------------*/
/*Global Variables                                                  */
/*------------------------------------------------------------------*/
extern long NiPPPoE ();
extern unsigned long pppoe_DisiISLInit();
extern void pppoe_DisiISLSend(char *addr, mblk_t *mp);
extern char BroadcastAddr[6];
static struct ni_init ni_pppoe [] =
{
   (int (*) ()) NiPPPoE,  /*ptr to interface code*/
   htonl (0),             /*IP address*/
   MAX_PPPOE_MTU,         /*maximum transmission unit*/
   4,                     /*length of hardware address*/
   0x0012|0x2000,         /*noarp, bobcast, p-p, RAWMEM*/
   0,                     /*subnet mask*/
   htonl (0),             /*peer IP address*/
   0  
};

PPPoEConnection Connection;
/********************************************************************/
/*error: Error handling routine                                     */
/*                                                                  */
/*INPUT: pointer to character string.                               */
/********************************************************************/
void error (char *str)
{
   TRACEF ("%s 0x%X\n" _ str _ errno);
   close (0);
   k_fatal (0, 0);
}


/********************************************************************/
/*client: This function runs as CLNT and connects to server on      */
/*the remote host.  It creates a connection and sends a             */
/*number of requests, then it shuts down the connection             */
/*in one direction and receives all of the responses.               */
/********************************************************************/
void client (int PPPNiNum)
{
   char *mymsg = "ADSL has connected,cheers!";
   int msgSize;
   int                 i, rc;
   struct sockaddr_in  peeraddr_in; /*for peer socket address*/
   int                 s; /*connected socket descriptor*/
   struct ppp_ioctl    pi;
   struct ifreq        ifr;
   struct sockaddr_in *sin;
   struct rtentry rte;
   unsigned long defgate,des;
   
   /*---------------------------------------------------------------*/
   /*TRACE_k (Need to wait till the P P P driver comes up);*/
   /*---------------------------------------------------------------*/
   if ((s = socket (AF_INET, SOCK_DGRAM, UDP)) == -1)
      error ("SOCKET creation error");

   do
   {
      pi.pi_ifno = PPPNiNum;

      TRACE_k (ioctl (s, SIOCGPPPSTATUS,));
      if (ioctl (s, SIOCGPPPSTATUS, (char *) &pi) < 0)
         error ("ioctl (SIOCGPPPSTATUS) error");

      TRACE_k (wait 15s);
      tm_wkafter(SEC2TICKS (5));
   }
   while (pi.pi_status != PSUP);

   close (s);

   TRACE_k (Create the socket);
   if ((s = socket (AF_INET, SOCK_DGRAM, UDP)) == -1)
      error ("SOCKET creation error");

   TRACE_k (Get the Peer IP address resulted from the negotiation);
   ifr.ifr_ifno = PPPNiNum;
   if (ioctl(s, SIOCGIFDSTADDR, (char *) &ifr) < 0) 
      error("ioctl (SIOCGIFDSTADDR)"); 
   sin = (struct sockaddr_in *) &ifr.ifr_dstaddr;
   defgate = sin->sin_addr.s_addr;
   
   /*if (ioctl(s, SIOCGIFADDR, (char *) &ifr) < 0) 
      error("ioctl (SIOCGIFADDR)"); 
   sin = (struct sockaddr_in *) &ifr.ifr_addr;
   des = sin->sin_addr.s_addr;*/
   
   close (s);
   
   bzero((char *)&rte, sizeof(rte));
   s = socket(AF_INET, SOCK_DGRAM, 0);
   satosin(&rte.rt_dst)->sin_family = AF_INET;
   satosin(&rte.rt_dst)->sin_addr.s_addr = 0x0;
   satosin(&rte.rt_gateway)->sin_family = AF_INET;
   satosin(&rte.rt_gateway)->sin_addr.s_addr = defgate;
   rte.rt_flags = RTF_GATEWAY;
   rc = ioctl(s, SIOCADDRT, &rte);
   
   memset (&peeraddr_in, '\0', sizeof peeraddr_in);
   peeraddr_in.sin_family = AF_INET;
   peeraddr_in.sin_addr.s_addr = htonl(0xca706805);  /*sin->sin_addr.s_addr;*/
   peeraddr_in.sin_port = htons(SERVER_PORT);
   
   /*if ((s = socket (AF_INET, SOCK_DGRAM, UDP)) == -1)
      error ("SOCKET creation error"); */
      
   printf("sock number is %d\n",s);
	
   msgSize = sizeof(mymsg);
   /*for(i=0;i<20;i++)*/
   while(1)
   {	sendto(s,mymsg,72,0,&peeraddr_in,sizeof(struct sockaddr_in));
        TRACEF("send msg\n");
	tm_wkafter(30);
   }	
    /*Close the socket.*/
   close (s);
   close (0);

   TRACEF ("End Client Test\n");
   sendPADT(&Connection,"logout");
   t_suspend (0L);
   while(1);
}

static void Delay( unsigned long usec )
{
	int i;
	for(i=0;i< 133*usec;i++)
	{;}
}

/********************************************************************/
/*tmosMain: Initialize the application                              */
/*                                                                  */
/*NOTE: This executes as the 'ROOT' task.  It suspends itself       */
/*after initializing the I/O drivers and creating the               */
/*client task.                                                      */
/********************************************************************/
void root ()
{
   ULONG  rc, tid, oldprio;
   ULONG  targ [4];
   int    i;
   
            
   DPsize(5000);/*DP_START (4*M, NULL); This line equal to DPsize(5000);*/
   
   TRACE_k (+root);
   
   bzero( (char *)(&Connection), sizeof(PPPoEConnection) );
   
   BroadcastAddr[0]=0xff; BroadcastAddr[1]=0xff; BroadcastAddr[2]=0xff; BroadcastAddr[3]=0xff; BroadcastAddr[4]=0xff; BroadcastAddr[5]=0xff; 
   
   /*---------------------------------------------------------------*/
   /*Lower our priority slightly.                                   */
   /*---------------------------------------------------------------*/
   rc = t_setpri (0, 250, &oldprio);
   if (rc != 0) k_fatal (rc, 0);
   
   /*---------------------------------------------------------------*/
   /*Initialize the I/O devices.*/
   /*---------------------------------------------------------------*/
   /*TRACE_k (wait 2s);
   tm_wkafter (SEC2TICKS (2));*/
   
   if( pppoe_DisiISLInit() != 0 )
       DP(("pppoe_DisiISLInit error"));
       
   /*TRACE_k (wait 2s);
   tm_wkafter (SEC2TICKS (2));*/
       
   /*---------------------------------------------------------------*/
   /*add_ni: returns the pNA+ interface number of the new network   */
   /*        interface if successful; otherwise it returns -1.      */                                      
   /*---------------------------------------------------------------*/
   TRACE_k (Starting P P P o E...);
   if ((i = add_ni(ni_pppoe)) < 0)
      error ("add_ni () error");
   TRACEF ("PPP Interface %d added\n" _ i);
   targ [0] = i;
   
   /*---------------------------------------------------------------*/
   /*Create the client task                                         */
   /*---------------------------------------------------------------*/ 
   if (t_create(CLIENT_NAME, CLIENT_PRIO, CLIENT_SYSSTACK, CLIENT_USRSTACK, CLIENT_FLAGS, &tid) != 0)
       error ("Task creation error");
       
   if (t_start(tid, CLIENT_INITMODE, client, targ) != 0)
       error ("Task start error");

   t_suspend (0L);

   
}

⌨️ 快捷键说明

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