http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / userapps / opensource / ppp / pppoe / sys-linux.c
index f8f530f..3b67e88 100755 (executable)
@@ -202,6 +202,32 @@ extern u_char      inpacket_buf[]; /* borrowed from main.c */
 
 extern int hungup;
 
+//#ifdef BBB_XML_API //Wilson add, (02/13/2006)
+#if defined(SUPPORT_XML_API) //Wilson add, (03/14/2006)
+extern int ppp_session;
+#endif
+
+#ifdef DEBUG_PRINT
+void dumpHex(char *direct, char *buff, int len)
+{
+   if(len<=0) return;
+
+   int i;
+   static struct timeval dumpTime;     
+
+   gettimeofday(&dumpTime, NULL);
+
+   printf("(%d.%d) %s:\n", dumpTime.tv_sec, dumpTime.tv_usec, direct);
+   for(i=0;i<len;i++) {
+      printf("%02X ", buff[i]&0xff);
+      if(i%16==15) printf("\n");
+   }
+   printf("\n");
+
+   return;
+}
+#endif
+
 /* new_fd is the fd of a tty */
 static void set_ppp_fd (int new_fd)
 {
@@ -403,9 +429,19 @@ int tty_establish_ppp (int tty_fd)
  *
  * generic_establish_ppp - Turn the fd into a ppp interface.
  */
+#if defined(ODM_LANG_LLL) 
+extern int ppp_session;
+extern int glbppppid; 
+extern int got_ppp_down;
+#endif
 int generic_establish_ppp (int fd)
 {
     int x;
+//#ifdef BBB_XML_API //Wilson add, (02/13/2006)
+#if defined(SUPPORT_XML_API) //Wilson add, (03/14/2006)
+    FILE *fp;
+#endif
+    //char cmd[30];
 /*
  * Demand mode - prime the old ppp device to relinquish the unit.
  */
@@ -422,7 +458,42 @@ int generic_establish_ppp (int fd)
 
        if (ioctl(fd, PPPIOCGCHAN, &chindex) == -1) {
            error("Couldn't get channel number: %m");
+               //printf("PVC error, Bye!!\n");
+               //sprintf(cmd,"kill -9 %d",glbppppid);
+               //system(cmd);
+//#ifdef BBB_XML_API //Wilson add, (02/13/2006)
+#if defined(SUPPORT_XML_API) //Wilson add, (03/14/2006)
+            if (ppp_session == PPPOE){
+                fp = fopen("/var/pppStatus", "w+");
+                if(fp){
+                    fprintf(fp, "2,2");
+                    fclose(fp);
+                }
+            }
+#endif //endif BBB_XML_API
+           //goto err;
+           //if (ppp_session == PPPOE){
+           //  if (ioctl(fd, TIOCSETD, &tty_disc) < 0 && !ok_error(errno))
+               //              warn("Couldn't reset tty to normal line discipline: %m");
+       //      return -2;
+       //} else {
+#if defined(ODM_LANG_LLL)
+                       char cmd[32]="";
+                     FILE *wsnfp = NULL;
+                       if (ppp_session == PPPOE){
+                        wsnfp = fopen("/var/btaolstatus", "w");
+                        if(wsnfp){
+                            fprintf(wsnfp, "%d", 1);
+                            fclose(wsnfp);
+                        }
+                               //printf("PVC error, Bye!!\n");
+                               sprintf(cmd,"kill -9 %d",glbppppid);
+                               system(cmd);
+                       }       
+#else          
            goto err;
+#endif                 
+       //}     
        }
        dbglog("using channel %d", chindex);
        fd = open("/dev/ppp", O_RDWR);
@@ -446,7 +517,8 @@ int generic_establish_ppp (int fd)
             * Create a new PPP unit.
             */
            if (make_ppp_unit() < 0)
-               goto err_close;
+               die(1);
+               //goto err_close;
        }
 
        if (looped)
@@ -595,7 +667,7 @@ static int make_ppp_unit()
        if (x < 0 && req_unit >= 0 && errno == EEXIST) {
                warn("Couldn't allocate PPP unit %d as it is already in use");
                ifunit = -1;
-               x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
+               //x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
        }
        if (x < 0)
                error("Couldn't create new ppp unit: %m");
@@ -962,12 +1034,25 @@ void output (int unit, unsigned char *p, int len)
 {
     int fd = ppp_fd;
     int proto;
+#if defined(SUPPORT_PPPDBG_SYSLOG)
+       char *q;
+#endif
 
     if (debug)
        dbglog("sent %P", p, len);
 
     if (len < PPP_HDRLEN)
        return;
+
+#if defined(SUPPORT_PPPDBG_SYSLOG)//Dump ascii
+       q=p;
+       if (logstatus == 1){
+       if (((q[2]&0xff)!=0xC0) || ((q[3]&0xff)!=0x21)|| (((q[4]&0xff)!=0x09) && ((q[4]&0xff)!=0x0A))){//Don't log LCP echo request/and echo response
+               andydbglog("Sent %P", q, len);
+       }       
+       }                       
+#endif
+
     if (new_style_driver) {
        p += 2;
        len -= 2;
@@ -975,9 +1060,23 @@ void output (int unit, unsigned char *p, int len)
        if (ifunit >= 0 && !(proto >= 0xc000 || proto == PPP_CCPFRAG))
            fd = ppp_dev_fd;
     }
+
+#if 0
+#if defined(SUPPORT_PPPDBG_SYSLOG)//Dump binary code
+       
+       if (len <= 128 && (((p[0]&0xff)!=0xC0) || ((p[1]&0xff)!=0x21)
+               || (((p[2]&0xff)!=0x09) && ((p[2]&0xff)!=0x0A))) ){//Don't log LCP echo request/and echo response
+               andydbglog("sent(binary) %P", p, len);
+       }       
+#endif
+#endif
+
+#ifdef DEBUG_PRINT
+       dumpHex("OUTPUT", p, len);
+#endif
+
     if (write(fd, p, len) < 0) {
-       if (errno == EWOULDBLOCK || errno == ENOBUFS
-           || errno == ENXIO || errno == EIO || errno == EINTR)
+               if (errno == EWOULDBLOCK || errno == ENOBUFS || errno == ENXIO || errno == EIO || errno == EINTR)
            warn("write: warning: %m (%d)", errno);
        else
            error("write: %m (%d)", errno);
@@ -1044,8 +1143,10 @@ int read_packet (unsigned char *buf)
     }
     nr = -1;
     if (ppp_fd >= 0) {
-       if (!(nr = read(ppp_fd, buf, len)))
-           nr = -1;
+               nr = read(ppp_fd, buf, len);
+#ifdef DEBUG_PRINT
+               dumpHex("1. INPUT", buf, nr);
+#endif
        if (nr < 0 && errno != EWOULDBLOCK && errno != EIO && errno != EINTR)
            error("read: %m");
        if (nr < 0 && errno == ENXIO)
@@ -1053,9 +1154,10 @@ int read_packet (unsigned char *buf)
     }
     if (nr < 0 && new_style_driver && ifunit >= 0) {
        /* N.B. we read ppp_fd first since LCP packets come in there. */
-       // To avoid nonppp ctl traffic
-       if (!(nr = read(ppp_dev_fd, buf, len)))
-           nr = -1;
+               nr = read(ppp_dev_fd, buf, len);
+#ifdef DEBUG_PRINT
+               dumpHex("2. INPUT", buf, nr);
+#endif
        if (nr < 0 && errno != EWOULDBLOCK && errno != EIO && errno != EINTR)
            error("read /dev/ppp: %m");
        if (nr < 0 && errno == ENXIO)
@@ -1075,24 +1177,26 @@ get_loop_output(void)
 {
     int rv = 0;
     int n;
-
     if (new_style_driver) {
-       while ((n = read_packet(inpacket_buf)) > 0)
-           if (loop_frame(inpacket_buf, n))
+               while ((n = read_packet(inpacket_buf)) > 0){
+               if (loop_frame(inpacket_buf, n)){
                rv = 1;
+               }       
+           }           
        return rv;
     }
 
-    while ((n = read(master_fd, inbuf, sizeof(inbuf))) > 0)
-       if (loop_chars(inbuf, n))
+       while ((n = read(master_fd, inbuf, sizeof(inbuf))) > 0){
+               if (loop_chars(inbuf, n)){
            rv = 1;
+           }   
+       }               
 
     if (n == 0)
        fatal("eof on loopback");
 
     if (errno != EWOULDBLOCK)
        fatal("read from loopback: %m(%d)", errno);
-
     return rv;
 }
 
@@ -1571,7 +1675,6 @@ int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
 {
     struct rtentry rt;
-
     default_route_gateway = 0;
 
     memset (&rt, '\0', sizeof (rt));
@@ -1593,7 +1696,6 @@ int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
            return 0;
        }
     }
-
     return 1;
 }
 
@@ -2255,7 +2357,6 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
 {
     struct ifreq   ifr;
     struct rtentry rt;
-
     memset (&ifr, '\0', sizeof (ifr));
     memset (&rt,  '\0', sizeof (rt));
 
@@ -2356,7 +2457,7 @@ int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
     struct ifreq ifr;
 
     create_msg(BCM_PPPOE_CLIENT_STATE_DOWN);
-    syslog(LOG_CRIT,"Clear IP addresses.  PPP connection DOWN.\n");   
+    //syslog(LOG_CRIT,"Clear IP addresses.  PPP connection DOWN.\n");   
 
     if (kernel_version < KVERSION(2,1,16)) {
 /*