rem
stringlengths 0
83.5k
| add
stringlengths 0
223k
| context
stringlengths 10
471k
| meta
stringlengths 120
236
|
---|---|---|---|
unsigned32 regno, unsigned32 value | uint32_t regno, uint32_t value | void erc32_sonic_write_register( void *base, unsigned32 regno, unsigned32 value){ volatile unsigned32 *p = base;#if (SONIC_DEBUG & SONIC_DEBUG_PRINT_REGISTERS) printf( "%p Write 0x%04x to %s (0x%02x)\n", &p[regno], value, SONIC_Reg_name[regno], regno ); fflush( stdout );#endif p[regno] = 0x0ffff & value;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1be1e913564b73bf50ce1aa58c003e564ddae83a/erc32sonic.c/buggy/c/src/lib/libbsp/sparc/erc32/erc32sonic/erc32sonic.c |
volatile unsigned32 *p = base; | volatile uint32_t *p = base; | void erc32_sonic_write_register( void *base, unsigned32 regno, unsigned32 value){ volatile unsigned32 *p = base;#if (SONIC_DEBUG & SONIC_DEBUG_PRINT_REGISTERS) printf( "%p Write 0x%04x to %s (0x%02x)\n", &p[regno], value, SONIC_Reg_name[regno], regno ); fflush( stdout );#endif p[regno] = 0x0ffff & value;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1be1e913564b73bf50ce1aa58c003e564ddae83a/erc32sonic.c/buggy/c/src/lib/libbsp/sparc/erc32/erc32sonic/erc32sonic.c |
return Flx_renormalize(res,lg(res)); | return FlxX_renormalize(res,lg(res)); | FlxqX_Flxq_mul(GEN P, GEN U, GEN T, ulong p){ int i, lP = lg(P); GEN res = cgetg(lP,t_POL); res[1] = P[1]; for(i=2; i<lP; i++) res[i] = (long)Flxq_mul(U,(GEN)P[i], T,p); return Flx_renormalize(res,lg(res));} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f3ec8a59d89f1785548e82595e87bd99224ed762/Flx.c/buggy/src/basemath/Flx.c |
for (i=2; i<ly; i++) z[i] = (long) FpX_add((GEN)x[i], (GEN)y[i], p); | for (i=2; i<ly; i++) z[i] = (long) Fq_add((GEN)x[i], (GEN)y[i], NULL, p); | FpXX_add(GEN x, GEN y, GEN p){ long i,lz; GEN z; long lx=lg(x); long ly=lg(y); if (ly>lx) swapspec(x,y, lx,ly); lz = lx; z = cgetg(lz, t_POL); z[1]=x[1]; for (i=2; i<ly; i++) z[i] = (long) FpX_add((GEN)x[i], (GEN)y[i], p); for ( ; i<lx; i++) z[i] = (long) gcopy((GEN)x[i]); return FpXX_renormalize(z, lz);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/819c29b05ce2b9ff19e5d336713ce0753f46d652/polarit3.c/clean/src/basemath/polarit3.c |
#define base vmeUniverse0BaseAddr | cfgUniversePort( unsigned long ismaster, unsigned long port, unsigned long address_space, unsigned long vme_address, unsigned long local_address, unsigned long length){#define base vmeUniverse0BaseAddrvolatile LERegister *preg;unsigned long p=port;unsigned long mode=0; /* check parameters */ if (port >= (ismaster ? UNIV_NUM_MPORTS : UNIV_NUM_SPORTS)) { uprintf(stderr,"invalid port\n"); return -1; } /* port start, bound addresses and offset must lie on 64k boundary * (4k for port 0 and 4) */ if ( PORT_UNALIGNED(local_address,port) ) { uprintf(stderr,"local address misaligned\n"); return -1; } if ( PORT_UNALIGNED(vme_address,port) ) { uprintf(stderr,"vme address misaligned\n"); return -1; } if ( PORT_UNALIGNED(length,port) ) { uprintf(stderr,"length misaligned\n"); return -1; } /* check address space validity */ if (am2mode(ismaster,address_space,&mode)) { uprintf(stderr,"invalid address space\n"); return -1; } /* get the universe base address */ if (!base && vmeUniverseInit()) { return -1; } preg=base; /* find out if we have a rev. II chip */ if ( UNIV_REV(base) < 2 ) { if (port>3) { uprintf(stderr,"Universe rev. < 2 has only 4 ports\n"); return -1; } } /* finally, configure the port */ /* find the register set for our port */ if (port<4) { preg += (ismaster ? UNIV_REGOFF_PCITGT0_CTRL : UNIV_REGOFF_VMESLV0_CTRL)/sizeof(LERegister); } else { preg += (ismaster ? UNIV_REGOFF_PCITGT4_CTRL : UNIV_REGOFF_VMESLV4_CTRL)/sizeof(LERegister); p-=4; } preg += 5 * p; /* temporarily disable the port */ disableUniversePort(ismaster,port,preg,0); /* address_space == 0 means disable */ if (address_space != 0) { unsigned long start,offst; /* set the port starting address; * this is the local address for the master * and the VME address for the slave */ if (ismaster) { start=local_address; /* let it overflow / wrap around 0 */ offst=vme_address-local_address; } else { start=vme_address; /* let it overflow / wrap around 0 */ offst=local_address-vme_address; }#undef TSILL#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 4\n",start,preg);#else WRITE_LE(start,preg,4);#endif /* set bound address */ length+=start;#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 8\n",length,preg);#else WRITE_LE(length,preg,8);#endif /* set offset */#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 12\n",offst,preg);#else WRITE_LE(offst,preg,12);#endif /* calculate configuration word and enable the port */ /* NOTE: reading the CY961 (Echotek ECDR814) with VDW32 * generated bus errors when reading 32-bit words * - very weird, because the registers are 16-bit * AFAIK. * - 32-bit accesses worked fine on vxWorks which * has the port set to 64-bit. * ???????? */ if (ismaster) mode |= UNIV_MCTL_EN | UNIV_MCTL_PWEN | UNIV_MCTL_VDW64 | UNIV_MCTL_VCT; else mode |= UNIV_SCTL_EN | UNIV_SCTL_PWEN | UNIV_SCTL_PREN;#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 0\n",mode,preg);#else EIEIO_REG; /* make sure mode is written last */ WRITE_LE(mode,preg,0); SYNC; /* enforce completion */#endif#ifdef TSILL uprintf(stderr, "universe %s port %lu successfully configured\n", ismaster ? "master" : "slave", port);#endif#ifdef __vxworks if (ismaster) uprintf(stderr, "WARNING: on the synergy, sysMasterPortsShow() may show incorrect settings (it uses cached values)\n");#endif } return 0;#undef base} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/784e792a04c600b79d535ad81a5ba32b4f4de33e/vmeUniverse.c/clean/c/src/lib/libbsp/shared/vmeUniverse/vmeUniverse.c |
|
else | else | cfgUniversePort( unsigned long ismaster, unsigned long port, unsigned long address_space, unsigned long vme_address, unsigned long local_address, unsigned long length){#define base vmeUniverse0BaseAddrvolatile LERegister *preg;unsigned long p=port;unsigned long mode=0; /* check parameters */ if (port >= (ismaster ? UNIV_NUM_MPORTS : UNIV_NUM_SPORTS)) { uprintf(stderr,"invalid port\n"); return -1; } /* port start, bound addresses and offset must lie on 64k boundary * (4k for port 0 and 4) */ if ( PORT_UNALIGNED(local_address,port) ) { uprintf(stderr,"local address misaligned\n"); return -1; } if ( PORT_UNALIGNED(vme_address,port) ) { uprintf(stderr,"vme address misaligned\n"); return -1; } if ( PORT_UNALIGNED(length,port) ) { uprintf(stderr,"length misaligned\n"); return -1; } /* check address space validity */ if (am2mode(ismaster,address_space,&mode)) { uprintf(stderr,"invalid address space\n"); return -1; } /* get the universe base address */ if (!base && vmeUniverseInit()) { return -1; } preg=base; /* find out if we have a rev. II chip */ if ( UNIV_REV(base) < 2 ) { if (port>3) { uprintf(stderr,"Universe rev. < 2 has only 4 ports\n"); return -1; } } /* finally, configure the port */ /* find the register set for our port */ if (port<4) { preg += (ismaster ? UNIV_REGOFF_PCITGT0_CTRL : UNIV_REGOFF_VMESLV0_CTRL)/sizeof(LERegister); } else { preg += (ismaster ? UNIV_REGOFF_PCITGT4_CTRL : UNIV_REGOFF_VMESLV4_CTRL)/sizeof(LERegister); p-=4; } preg += 5 * p; /* temporarily disable the port */ disableUniversePort(ismaster,port,preg,0); /* address_space == 0 means disable */ if (address_space != 0) { unsigned long start,offst; /* set the port starting address; * this is the local address for the master * and the VME address for the slave */ if (ismaster) { start=local_address; /* let it overflow / wrap around 0 */ offst=vme_address-local_address; } else { start=vme_address; /* let it overflow / wrap around 0 */ offst=local_address-vme_address; }#undef TSILL#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 4\n",start,preg);#else WRITE_LE(start,preg,4);#endif /* set bound address */ length+=start;#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 8\n",length,preg);#else WRITE_LE(length,preg,8);#endif /* set offset */#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 12\n",offst,preg);#else WRITE_LE(offst,preg,12);#endif /* calculate configuration word and enable the port */ /* NOTE: reading the CY961 (Echotek ECDR814) with VDW32 * generated bus errors when reading 32-bit words * - very weird, because the registers are 16-bit * AFAIK. * - 32-bit accesses worked fine on vxWorks which * has the port set to 64-bit. * ???????? */ if (ismaster) mode |= UNIV_MCTL_EN | UNIV_MCTL_PWEN | UNIV_MCTL_VDW64 | UNIV_MCTL_VCT; else mode |= UNIV_SCTL_EN | UNIV_SCTL_PWEN | UNIV_SCTL_PREN;#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 0\n",mode,preg);#else EIEIO_REG; /* make sure mode is written last */ WRITE_LE(mode,preg,0); SYNC; /* enforce completion */#endif#ifdef TSILL uprintf(stderr, "universe %s port %lu successfully configured\n", ismaster ? "master" : "slave", port);#endif#ifdef __vxworks if (ismaster) uprintf(stderr, "WARNING: on the synergy, sysMasterPortsShow() may show incorrect settings (it uses cached values)\n");#endif } return 0;#undef base} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/784e792a04c600b79d535ad81a5ba32b4f4de33e/vmeUniverse.c/clean/c/src/lib/libbsp/shared/vmeUniverse/vmeUniverse.c |
#undef base | cfgUniversePort( unsigned long ismaster, unsigned long port, unsigned long address_space, unsigned long vme_address, unsigned long local_address, unsigned long length){#define base vmeUniverse0BaseAddrvolatile LERegister *preg;unsigned long p=port;unsigned long mode=0; /* check parameters */ if (port >= (ismaster ? UNIV_NUM_MPORTS : UNIV_NUM_SPORTS)) { uprintf(stderr,"invalid port\n"); return -1; } /* port start, bound addresses and offset must lie on 64k boundary * (4k for port 0 and 4) */ if ( PORT_UNALIGNED(local_address,port) ) { uprintf(stderr,"local address misaligned\n"); return -1; } if ( PORT_UNALIGNED(vme_address,port) ) { uprintf(stderr,"vme address misaligned\n"); return -1; } if ( PORT_UNALIGNED(length,port) ) { uprintf(stderr,"length misaligned\n"); return -1; } /* check address space validity */ if (am2mode(ismaster,address_space,&mode)) { uprintf(stderr,"invalid address space\n"); return -1; } /* get the universe base address */ if (!base && vmeUniverseInit()) { return -1; } preg=base; /* find out if we have a rev. II chip */ if ( UNIV_REV(base) < 2 ) { if (port>3) { uprintf(stderr,"Universe rev. < 2 has only 4 ports\n"); return -1; } } /* finally, configure the port */ /* find the register set for our port */ if (port<4) { preg += (ismaster ? UNIV_REGOFF_PCITGT0_CTRL : UNIV_REGOFF_VMESLV0_CTRL)/sizeof(LERegister); } else { preg += (ismaster ? UNIV_REGOFF_PCITGT4_CTRL : UNIV_REGOFF_VMESLV4_CTRL)/sizeof(LERegister); p-=4; } preg += 5 * p; /* temporarily disable the port */ disableUniversePort(ismaster,port,preg,0); /* address_space == 0 means disable */ if (address_space != 0) { unsigned long start,offst; /* set the port starting address; * this is the local address for the master * and the VME address for the slave */ if (ismaster) { start=local_address; /* let it overflow / wrap around 0 */ offst=vme_address-local_address; } else { start=vme_address; /* let it overflow / wrap around 0 */ offst=local_address-vme_address; }#undef TSILL#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 4\n",start,preg);#else WRITE_LE(start,preg,4);#endif /* set bound address */ length+=start;#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 8\n",length,preg);#else WRITE_LE(length,preg,8);#endif /* set offset */#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 12\n",offst,preg);#else WRITE_LE(offst,preg,12);#endif /* calculate configuration word and enable the port */ /* NOTE: reading the CY961 (Echotek ECDR814) with VDW32 * generated bus errors when reading 32-bit words * - very weird, because the registers are 16-bit * AFAIK. * - 32-bit accesses worked fine on vxWorks which * has the port set to 64-bit. * ???????? */ if (ismaster) mode |= UNIV_MCTL_EN | UNIV_MCTL_PWEN | UNIV_MCTL_VDW64 | UNIV_MCTL_VCT; else mode |= UNIV_SCTL_EN | UNIV_SCTL_PWEN | UNIV_SCTL_PREN;#ifdef TSILL uprintf(stderr,"writing 0x%08x to 0x%08x + 0\n",mode,preg);#else EIEIO_REG; /* make sure mode is written last */ WRITE_LE(mode,preg,0); SYNC; /* enforce completion */#endif#ifdef TSILL uprintf(stderr, "universe %s port %lu successfully configured\n", ismaster ? "master" : "slave", port);#endif#ifdef __vxworks if (ismaster) uprintf(stderr, "WARNING: on the synergy, sysMasterPortsShow() may show incorrect settings (it uses cached values)\n");#endif } return 0;#undef base} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/784e792a04c600b79d535ad81a5ba32b4f4de33e/vmeUniverse.c/clean/c/src/lib/libbsp/shared/vmeUniverse/vmeUniverse.c |
|
x += 2; y += 2; dz = dx-dy; | dz = dx-dy; | u_FpX_divrem(GEN x, GEN y, ulong p, int malloc, GEN *pr){ GEN z,q,c; long dx,dy,dz,i,j; ulong p1,inv; dy = deg(y); if (!dy) { if (pr) { if (pr == ONLY_REM) return u_zeropol(malloc); *pr = u_zeropol(malloc); } if (y[2] == 1UL) return u_copy(x,malloc); return u_FpX_Fp_mul(x, u_invmod(y[2], p), p, malloc); } dx = deg(x); x += 2; y += 2; dz = dx-dy; if (dz < 0) { if (pr) { c = u_copy(x-2, malloc); if (pr == ONLY_REM) return c; *pr = c; } return u_zeropol(malloc); } z = u_allocpol(dz, malloc || (pr == ONLY_REM)) + 2; inv = y[dy]; if (inv != 1UL) inv = u_invmod(inv,p); if (u_OK_ULONG(p)) { z[dz] = (inv*x[dx]) % p; for (i=dx-1; i>=dy; --i) { p1 = p - x[i]; /* compute -p1 instead of p1 (pb with ulongs otherwise) */ for (j=i-dy+1; j<=i && j<=dz; j++) { p1 += z[j]*y[i-j]; if (p1 & HIGHBIT) p1 = p1 % p; } p1 %= p; z[i-dy] = p1? ((p - p1)*inv) % p: 0; } } else { z[dz] = mulssmod(inv, x[dx], p); for (i=dx-1; i>=dy; --i) { p1 = p - x[i]; /* compute -p1 instead of p1 (pb with ulongs otherwise) */ for (j=i-dy+1; j<=i && j<=dz; j++) p1 = addssmod(p1, mulssmod(z[j],y[i-j],p), p); z[i-dy] = p1? mulssmod(p - p1, inv, p): 0; } } q = u_normalizepol(z-2, dz+3); if (!pr) return q; c = u_allocpol(dy,malloc) + 2; if (u_OK_ULONG(p)) { for (i=0; i<dy; i++) { p1 = z[0]*y[i]; for (j=1; j<=i && j<=dz; j++) { p1 += z[j]*y[i-j]; if (p1 & HIGHBIT) p1 = p1 % p; } c[i] = subssmod(x[i], p1%p, p); } } else { for (i=0; i<dy; i++) { p1 = mulssmod(z[0],y[i],p); for (j=1; j<=i && j<=dz; j++) p1 = addssmod(p1, mulssmod(z[j],y[i-j],p), p); c[i] = subssmod(x[i], p1, p); } } i=dy-1; while (i>=0 && !c[i]) i--; c = u_normalizepol(c-2, i+3); if (pr == ONLY_REM) { free(q); return c; } *pr = c; return q;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/8fb106e433493a99b7b083a32bc824515ed57d1c/polarit3.c/buggy/src/basemath/polarit3.c |
c = u_copy(x-2, malloc); | c = u_copy(x, malloc); | u_FpX_divrem(GEN x, GEN y, ulong p, int malloc, GEN *pr){ GEN z,q,c; long dx,dy,dz,i,j; ulong p1,inv; dy = deg(y); if (!dy) { if (pr) { if (pr == ONLY_REM) return u_zeropol(malloc); *pr = u_zeropol(malloc); } if (y[2] == 1UL) return u_copy(x,malloc); return u_FpX_Fp_mul(x, u_invmod(y[2], p), p, malloc); } dx = deg(x); x += 2; y += 2; dz = dx-dy; if (dz < 0) { if (pr) { c = u_copy(x-2, malloc); if (pr == ONLY_REM) return c; *pr = c; } return u_zeropol(malloc); } z = u_allocpol(dz, malloc || (pr == ONLY_REM)) + 2; inv = y[dy]; if (inv != 1UL) inv = u_invmod(inv,p); if (u_OK_ULONG(p)) { z[dz] = (inv*x[dx]) % p; for (i=dx-1; i>=dy; --i) { p1 = p - x[i]; /* compute -p1 instead of p1 (pb with ulongs otherwise) */ for (j=i-dy+1; j<=i && j<=dz; j++) { p1 += z[j]*y[i-j]; if (p1 & HIGHBIT) p1 = p1 % p; } p1 %= p; z[i-dy] = p1? ((p - p1)*inv) % p: 0; } } else { z[dz] = mulssmod(inv, x[dx], p); for (i=dx-1; i>=dy; --i) { p1 = p - x[i]; /* compute -p1 instead of p1 (pb with ulongs otherwise) */ for (j=i-dy+1; j<=i && j<=dz; j++) p1 = addssmod(p1, mulssmod(z[j],y[i-j],p), p); z[i-dy] = p1? mulssmod(p - p1, inv, p): 0; } } q = u_normalizepol(z-2, dz+3); if (!pr) return q; c = u_allocpol(dy,malloc) + 2; if (u_OK_ULONG(p)) { for (i=0; i<dy; i++) { p1 = z[0]*y[i]; for (j=1; j<=i && j<=dz; j++) { p1 += z[j]*y[i-j]; if (p1 & HIGHBIT) p1 = p1 % p; } c[i] = subssmod(x[i], p1%p, p); } } else { for (i=0; i<dy; i++) { p1 = mulssmod(z[0],y[i],p); for (j=1; j<=i && j<=dz; j++) p1 = addssmod(p1, mulssmod(z[j],y[i-j],p), p); c[i] = subssmod(x[i], p1, p); } } i=dy-1; while (i>=0 && !c[i]) i--; c = u_normalizepol(c-2, i+3); if (pr == ONLY_REM) { free(q); return c; } *pr = c; return q;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/8fb106e433493a99b7b083a32bc824515ed57d1c/polarit3.c/buggy/src/basemath/polarit3.c |
x += 2; y += 2; | u_FpX_divrem(GEN x, GEN y, ulong p, int malloc, GEN *pr){ GEN z,q,c; long dx,dy,dz,i,j; ulong p1,inv; dy = deg(y); if (!dy) { if (pr) { if (pr == ONLY_REM) return u_zeropol(malloc); *pr = u_zeropol(malloc); } if (y[2] == 1UL) return u_copy(x,malloc); return u_FpX_Fp_mul(x, u_invmod(y[2], p), p, malloc); } dx = deg(x); x += 2; y += 2; dz = dx-dy; if (dz < 0) { if (pr) { c = u_copy(x-2, malloc); if (pr == ONLY_REM) return c; *pr = c; } return u_zeropol(malloc); } z = u_allocpol(dz, malloc || (pr == ONLY_REM)) + 2; inv = y[dy]; if (inv != 1UL) inv = u_invmod(inv,p); if (u_OK_ULONG(p)) { z[dz] = (inv*x[dx]) % p; for (i=dx-1; i>=dy; --i) { p1 = p - x[i]; /* compute -p1 instead of p1 (pb with ulongs otherwise) */ for (j=i-dy+1; j<=i && j<=dz; j++) { p1 += z[j]*y[i-j]; if (p1 & HIGHBIT) p1 = p1 % p; } p1 %= p; z[i-dy] = p1? ((p - p1)*inv) % p: 0; } } else { z[dz] = mulssmod(inv, x[dx], p); for (i=dx-1; i>=dy; --i) { p1 = p - x[i]; /* compute -p1 instead of p1 (pb with ulongs otherwise) */ for (j=i-dy+1; j<=i && j<=dz; j++) p1 = addssmod(p1, mulssmod(z[j],y[i-j],p), p); z[i-dy] = p1? mulssmod(p - p1, inv, p): 0; } } q = u_normalizepol(z-2, dz+3); if (!pr) return q; c = u_allocpol(dy,malloc) + 2; if (u_OK_ULONG(p)) { for (i=0; i<dy; i++) { p1 = z[0]*y[i]; for (j=1; j<=i && j<=dz; j++) { p1 += z[j]*y[i-j]; if (p1 & HIGHBIT) p1 = p1 % p; } c[i] = subssmod(x[i], p1%p, p); } } else { for (i=0; i<dy; i++) { p1 = mulssmod(z[0],y[i],p); for (j=1; j<=i && j<=dz; j++) p1 = addssmod(p1, mulssmod(z[j],y[i-j],p), p); c[i] = subssmod(x[i], p1, p); } } i=dy-1; while (i>=0 && !c[i]) i--; c = u_normalizepol(c-2, i+3); if (pr == ONLY_REM) { free(q); return c; } *pr = c; return q;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/8fb106e433493a99b7b083a32bc824515ed57d1c/polarit3.c/buggy/src/basemath/polarit3.c |
|
GEN a = gzero, b = gzero; | GEN a = gzero, b = gzero, run = realun(DEFAULTPREC); | ZY_ZXY_ResBound(GEN A, GEN B){ ulong av = avma; GEN a = gzero, b = gzero; long i , lA = lgef(A), lB = lgef(B); for (i=2; i<lA; i++) a = addii(a, sqri((GEN)A[i])); for (i=2; i<lB; i++) { GEN t = (GEN)B[i]; if (typ(t) != t_INT) t = gnorml1(t, 0); b = addii(b, sqri(t)); } b = mulii(gpowgs(a, deg(B)), gpowgs(b, deg(A))); return gerepileupto(av, addis(racine(b), 1));} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/8fb106e433493a99b7b083a32bc824515ed57d1c/polarit3.c/buggy/src/basemath/polarit3.c |
for (i=2; i<lA; i++) a = addii(a, sqri((GEN)A[i])); | for (i=2; i<lA; i++) a = gadd(a, gmul(gsqr((GEN)A[i]),run)); | ZY_ZXY_ResBound(GEN A, GEN B){ ulong av = avma; GEN a = gzero, b = gzero; long i , lA = lgef(A), lB = lgef(B); for (i=2; i<lA; i++) a = addii(a, sqri((GEN)A[i])); for (i=2; i<lB; i++) { GEN t = (GEN)B[i]; if (typ(t) != t_INT) t = gnorml1(t, 0); b = addii(b, sqri(t)); } b = mulii(gpowgs(a, deg(B)), gpowgs(b, deg(A))); return gerepileupto(av, addis(racine(b), 1));} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/8fb106e433493a99b7b083a32bc824515ed57d1c/polarit3.c/buggy/src/basemath/polarit3.c |
GEN t = (GEN)B[i]; if (typ(t) != t_INT) t = gnorml1(t, 0); b = addii(b, sqri(t)); | GEN t = gmul((GEN)B[i], run); if (typ(t) == t_POL) t = gnorml1(t, 0); b = gadd(b, gsqr(t)); | ZY_ZXY_ResBound(GEN A, GEN B){ ulong av = avma; GEN a = gzero, b = gzero; long i , lA = lgef(A), lB = lgef(B); for (i=2; i<lA; i++) a = addii(a, sqri((GEN)A[i])); for (i=2; i<lB; i++) { GEN t = (GEN)B[i]; if (typ(t) != t_INT) t = gnorml1(t, 0); b = addii(b, sqri(t)); } b = mulii(gpowgs(a, deg(B)), gpowgs(b, deg(A))); return gerepileupto(av, addis(racine(b), 1));} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/8fb106e433493a99b7b083a32bc824515ed57d1c/polarit3.c/buggy/src/basemath/polarit3.c |
b = mulii(gpowgs(a, deg(B)), gpowgs(b, deg(A))); return gerepileupto(av, addis(racine(b), 1)); | b = gmul(gpowgs(a, deg(B)), gpowgs(b, deg(A))); avma = av; return 1 + (gexpo(b)>>1); | ZY_ZXY_ResBound(GEN A, GEN B){ ulong av = avma; GEN a = gzero, b = gzero; long i , lA = lgef(A), lB = lgef(B); for (i=2; i<lA; i++) a = addii(a, sqri((GEN)A[i])); for (i=2; i<lB; i++) { GEN t = (GEN)B[i]; if (typ(t) != t_INT) t = gnorml1(t, 0); b = addii(b, sqri(t)); } b = mulii(gpowgs(a, deg(B)), gpowgs(b, deg(A))); return gerepileupto(av, addis(racine(b), 1));} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/8fb106e433493a99b7b083a32bc824515ed57d1c/polarit3.c/buggy/src/basemath/polarit3.c |
if ( !temp_loc.ops->utime_h ) | if ( !temp_loc.ops->utime_h ){ rtems_filesystem_freenode( &temp_loc ); | int utime( const char *path, const struct utimbuf *times){ rtems_filesystem_location_info_t temp_loc; int result; if ( rtems_filesystem_evaluate_path( path, 0x00, &temp_loc, TRUE ) ) return -1; if ( !temp_loc.ops->utime_h ) set_errno_and_return_minus_one( ENOTSUP ); result = (*temp_loc.ops->utime_h)( &temp_loc, times->actime, times->modtime ); rtems_filesystem_freenode( &temp_loc ); return result;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/efb5450a87d50c53584d361c92d5928098233f58/utime.c/clean/cpukit/libcsupport/src/utime.c |
} | int utime( const char *path, const struct utimbuf *times){ rtems_filesystem_location_info_t temp_loc; int result; if ( rtems_filesystem_evaluate_path( path, 0x00, &temp_loc, TRUE ) ) return -1; if ( !temp_loc.ops->utime_h ) set_errno_and_return_minus_one( ENOTSUP ); result = (*temp_loc.ops->utime_h)( &temp_loc, times->actime, times->modtime ); rtems_filesystem_freenode( &temp_loc ); return result;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/efb5450a87d50c53584d361c92d5928098233f58/utime.c/clean/cpukit/libcsupport/src/utime.c |
|
while (( off < sizeof(buf) ) && ( uread(BSP_UART_COM1, LSR) & DR )) { buf[off++] = uread(BSP_UART_COM1, RBR); | while (( off < sizeof(buf) ) && ( uread(BSP_UART_COM2, LSR) & DR )) { buf[off++] = uread(BSP_UART_COM2, RBR); | BSP_uart_termios_read_com2(int uart){ int off = (int)0; char buf[40]; /* read current byte */ while (( off < sizeof(buf) ) && ( uread(BSP_UART_COM1, LSR) & DR )) { buf[off++] = uread(BSP_UART_COM1, RBR); } /* write out data */ if ( off > 0 ) { rtems_termios_enqueue_raw_characters(termios_ttyp_com2, buf, off); } /* enable receive interrupts */ uart_data[BSP_UART_COM2].ier |= (RECEIVE_ENABLE | RECEIVER_LINE_ST_ENABLE); uwrite(BSP_UART_COM2, IER, uart_data[BSP_UART_COM2].ier); return ( EOF );} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/b9ff276c358906706798417185a88f59facb8293/uart.c/buggy/c/src/lib/libbsp/i386/shared/comm/uart.c |
unsigned32 buf_size = 0; | uint32_t buf_size = 0; | void fileio_read_file(void){ char fname[1024]; char tmp_str[32]; unsigned32 buf_size = 0; size_t curr_pos; int fd = -1; ssize_t n; rtems_interval start_tick,curr_tick,ticks_per_sec; char *bufptr = NULL; boolean failed = FALSE; printf(" =========================\n"); printf(" READ FILE ... \n"); printf(" =========================\n"); fileio_print_free_heap(); /* * get number of ticks per second */ rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec); /* * get path to file to read */ if (!failed) { printf("Enter path/filename ==>"); fgets(fname,sizeof(fname)-1,stdin); while (fname[strlen(fname)-1] == '\n') { fname[strlen(fname)-1] = '\0'; } if (0 == strlen(fname)) { printf("*** no filename entered, aborted\n"); failed = TRUE; } } /* * get block size to read */ if (!failed) { printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n" "Enter block size to use for read calls ==>"); fgets(tmp_str,sizeof(tmp_str)-1,stdin); failed = fileio_str2size(tmp_str,&buf_size); if (failed) { printf("*** illegal block size, aborted\n"); } } /* * allocate buffer */ if (!failed) { printf("... allocating %lu bytes of buffer for write data\n", (unsigned long)buf_size); bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */ if (bufptr == NULL) { printf("*** malloc failed, aborted\n"); failed = TRUE; } } /* * open file */ if (!failed) { printf("... opening file \"%s\"\n",fname); fd = open(fname,O_RDONLY); if (fd < 0) { printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno)); failed = TRUE; } } /* * read file */ if (!failed) { printf("... reading from file\n"); rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick); curr_pos = 0; do { n = read(fd, bufptr, buf_size); if (n > 0) { curr_pos += n; } } while (n > 0); rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick); if (n < 0) { failed = TRUE; printf("*** file read failed, " "%lu bytes read, " "errno = %d(%s)\n", (unsigned long)curr_pos,errno,strerror(errno)); } else { printf("%lu bytes read\n", (unsigned long)curr_pos); printf("time elapsed for read: %g seconds\n", ((double)curr_tick-start_tick)/ticks_per_sec); printf("read data rate: %g KBytes/second\n", (((double)curr_pos) / 1024.0 / (((double)curr_tick-start_tick)/ticks_per_sec))); } } if (fd >= 0) { printf("... closing file\n"); close(fd); } if (bufptr != NULL) { printf("... deallocating buffer\n"); free(bufptr); bufptr = NULL; } printf("\n ******** End of file read\n"); fileio_print_free_heap();} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/4c84d7b760ceb1e140c1cfc0fa64289ca4d243ed/init.c/buggy/testsuites/samples/fileio/init.c |
boolean fileio_str2size(const char *str,unsigned32 *res_ptr) | boolean fileio_str2size(const char *str,uint32_t *res_ptr) | boolean fileio_str2size(const char *str,unsigned32 *res_ptr){ boolean failed = FALSE; unsigned long size; char suffix = ' '; if (1 > sscanf(str,"%lu%c",&size,&suffix)) { failed = TRUE; } else if (toupper(suffix) == 'K') { size *= 1024; } else if (toupper(suffix) == 'M') { size *= 1024UL*1024UL; } else if (isalpha(suffix)) { failed = TRUE; } if (!failed) { *res_ptr = size; } return failed;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/4c84d7b760ceb1e140c1cfc0fa64289ca4d243ed/init.c/buggy/testsuites/samples/fileio/init.c |
unsigned32 flen = 0; | uint32_t flen = 0; | void fileio_list_file(void){ char fname[1024]; char *buf_ptr = NULL; unsigned32 flen = 0; int fd = -1; ssize_t n; size_t buf_size = 100; rtems_interval start_tick,curr_tick,ticks_per_sec; printf(" =========================\n"); printf(" LIST FILE ... \n"); printf(" =========================\n"); fileio_print_free_heap(); printf(" Enter filename to list ==>"); fgets(fname,sizeof(fname)-1,stdin); while (fname[strlen(fname)-1] == '\n') { fname[strlen(fname)-1] = '\0'; } /* * allocate buffer of given size */ if (buf_size > 0) { buf_ptr = malloc(buf_size); } if (buf_ptr != NULL) { printf("\n Trying to open file \"%s\" for read\n",fname); fd = open(fname,O_RDONLY); if (fd < 0) { printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno)); } } if (fd >= 0) { rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick); do { n = read(fd,buf_ptr,buf_size); if (n > 0) { write(1,buf_ptr,n); flen += n; } } while (n > 0); rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick); printf("\n ******** End of file reached, flen = %d\n",flen); close(fd); rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec); printf("time elapsed for read: %g seconds\n", ((double)curr_tick-start_tick)/ticks_per_sec); } /* * free buffer */ if (buf_ptr != NULL) { free(buf_ptr); } fileio_print_free_heap();} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/4c84d7b760ceb1e140c1cfc0fa64289ca4d243ed/init.c/buggy/testsuites/samples/fileio/init.c |
dst = strdup(src); | if (src) dst = strdup(src); | void *bootp_strdup_realloc(char *dst,const char *src){ size_t len; void *realloc(void * __r, size_t __size); if (dst == NULL) { /* first allocation, simply use strdup */ dst = strdup(src); } else { /* already allocated, so use realloc/strcpy */ len = strlen(src) + 1; dst = realloc(dst,len); if (dst != NULL) { strcpy(dst,src); } } return dst;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/7ac73e6ee2af1a0bcdcccf76344e60db558efe9a/bootp_subr.c/buggy/cpukit/libnetworking/nfs/bootp_subr.c |
len = strlen(src) + 1; | len = src ? strlen(src) + 1 : 0; /* src == NULL tells us to effectively free dst */ | void *bootp_strdup_realloc(char *dst,const char *src){ size_t len; void *realloc(void * __r, size_t __size); if (dst == NULL) { /* first allocation, simply use strdup */ dst = strdup(src); } else { /* already allocated, so use realloc/strcpy */ len = strlen(src) + 1; dst = realloc(dst,len); if (dst != NULL) { strcpy(dst,src); } } return dst;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/7ac73e6ee2af1a0bcdcccf76344e60db558efe9a/bootp_subr.c/buggy/cpukit/libnetworking/nfs/bootp_subr.c |
so->so_rcv.sb_flags |= SB_WAIT; | so->so_snd.sb_flags |= SB_WAIT; | socket_select (struct socket *so, int which, rtems_id tid){ switch (which) { case FREAD: if (soreadable(so)) return (1); so->so_rcv.sb_flags |= SB_WAIT; so->so_rcv.sb_sel.si_pid = tid; break; case FWRITE: if (sowriteable(so)) return (1); so->so_rcv.sb_flags |= SB_WAIT; so->so_snd.sb_sel.si_pid = tid; break; case 0: if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) return (1); so->so_rcv.sb_sel.si_pid = tid; break; } return (0);} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/182813e16485b2fba25923cc7be23b3acb73382c/rtems_select.c/clean/cpukit/libnetworking/rtems/rtems_select.c |
unsigned32 count; | uint32_t count; | nvram_driver_write(rtems_device_major_number major, rtems_device_minor_number minor, void *arg){ rtems_libio_rw_args_t *args = arg; unsigned32 count; i2c_bus_number bus = DS1307_I2C_BUS_NUMBER; i2c_address addr = DS1307_I2C_ADDRESS; i2c_message_status status; if (args->offset >= DS1307_NVRAM_SIZE) { count = 0; } else if (args->offset + args->count >= DS1307_NVRAM_SIZE) { count = DS1307_NVRAM_SIZE - args->offset; } else { count = args->count; } if (count > 0) { int try = 0; do { rtems_unsigned8 buf[DS1307_NVRAM_SIZE + 1]; buf[0] = DS1307_NVRAM_START + args->offset; memcpy(buf+1, args->buffer, count); status = i2c_write(bus, addr, buf, count+1); try++; } while ((try < 15) && (status != I2C_SUCCESSFUL)); if (status != I2C_SUCCESSFUL) { errno = EIO; return RTEMS_UNSATISFIED; } } args->bytes_moved = count; return RTEMS_SUCCESSFUL;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/b7044dc94e1e4ee9f69ad6841e60c9602d5ea86b/nvram.c/buggy/c/src/lib/libbsp/m68k/mcf5206elite/nvram/nvram.c |
rtems_unsigned8 buf[DS1307_NVRAM_SIZE + 1]; | uint8_t buf[DS1307_NVRAM_SIZE + 1]; | nvram_driver_write(rtems_device_major_number major, rtems_device_minor_number minor, void *arg){ rtems_libio_rw_args_t *args = arg; unsigned32 count; i2c_bus_number bus = DS1307_I2C_BUS_NUMBER; i2c_address addr = DS1307_I2C_ADDRESS; i2c_message_status status; if (args->offset >= DS1307_NVRAM_SIZE) { count = 0; } else if (args->offset + args->count >= DS1307_NVRAM_SIZE) { count = DS1307_NVRAM_SIZE - args->offset; } else { count = args->count; } if (count > 0) { int try = 0; do { rtems_unsigned8 buf[DS1307_NVRAM_SIZE + 1]; buf[0] = DS1307_NVRAM_START + args->offset; memcpy(buf+1, args->buffer, count); status = i2c_write(bus, addr, buf, count+1); try++; } while ((try < 15) && (status != I2C_SUCCESSFUL)); if (status != I2C_SUCCESSFUL) { errno = EIO; return RTEMS_UNSATISFIED; } } args->bytes_moved = count; return RTEMS_SUCCESSFUL;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/b7044dc94e1e4ee9f69ad6841e60c9602d5ea86b/nvram.c/buggy/c/src/lib/libbsp/m68k/mcf5206elite/nvram/nvram.c |
volatile rtems_unsigned8 *VME_interrupt_enable; rtems_unsigned8 value; | volatile uint8_t *VME_interrupt_enable; uint8_t value; | void VME_interrupt_Enable ( VME_interrupt_Mask mask /* IN */){ volatile rtems_unsigned8 *VME_interrupt_enable; rtems_unsigned8 value;#if 0 VME_interrupt_enable = ACC_VIE;#else VME_interrupt_enable = 0;#endif value = *VME_interrupt_enable; value |= mask; /* turn on interrupts for all levels in mask */ *VME_interrupt_enable = value;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/ed9e44994265d3dccca845c7aade2f634184c339/vmeintr.c/clean/c/src/lib/libbsp/powerpc/dmv177/startup/vmeintr.c |
isprincipalell(GEN bnfz, GEN id, GEN cycgen, GEN uu, GEN gell, long rc) | isprincipalell(GEN bnfz, GEN id, GEN cycgen, GEN u, GEN gell, long rc) | isprincipalell(GEN bnfz, GEN id, GEN cycgen, GEN uu, GEN gell, long rc){ long i, l = lg(cycgen); GEN y, logdisc, b; y = isprincipalgenforce(bnfz,id); logdisc = gmod((GEN)y[1], gell); b = to_famat_all((GEN)y[2], gun); for (i=rc+1; i<l; i++) { GEN e = modii(mulii((GEN)logdisc[i],(GEN)uu[i]), gell); b = famat_mul(b, famat_pow((GEN)cycgen[i], e)); } y = cgetg(3,t_VEC); y[1] = (long)logdisc; setlg(logdisc,rc+1); y[2] = (long)b; return y;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/73929b165be3dc8f342788321bf5a06394a0cf3d/kummer.c/clean/src/modules/kummer.c |
GEN y, logdisc, b; | GEN logdisc, b, y = isprincipalgenforce(bnfz,id); | isprincipalell(GEN bnfz, GEN id, GEN cycgen, GEN uu, GEN gell, long rc){ long i, l = lg(cycgen); GEN y, logdisc, b; y = isprincipalgenforce(bnfz,id); logdisc = gmod((GEN)y[1], gell); b = to_famat_all((GEN)y[2], gun); for (i=rc+1; i<l; i++) { GEN e = modii(mulii((GEN)logdisc[i],(GEN)uu[i]), gell); b = famat_mul(b, famat_pow((GEN)cycgen[i], e)); } y = cgetg(3,t_VEC); y[1] = (long)logdisc; setlg(logdisc,rc+1); y[2] = (long)b; return y;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/73929b165be3dc8f342788321bf5a06394a0cf3d/kummer.c/clean/src/modules/kummer.c |
y = isprincipalgenforce(bnfz,id); | isprincipalell(GEN bnfz, GEN id, GEN cycgen, GEN uu, GEN gell, long rc){ long i, l = lg(cycgen); GEN y, logdisc, b; y = isprincipalgenforce(bnfz,id); logdisc = gmod((GEN)y[1], gell); b = to_famat_all((GEN)y[2], gun); for (i=rc+1; i<l; i++) { GEN e = modii(mulii((GEN)logdisc[i],(GEN)uu[i]), gell); b = famat_mul(b, famat_pow((GEN)cycgen[i], e)); } y = cgetg(3,t_VEC); y[1] = (long)logdisc; setlg(logdisc,rc+1); y[2] = (long)b; return y;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/73929b165be3dc8f342788321bf5a06394a0cf3d/kummer.c/clean/src/modules/kummer.c |
|
GEN e = modii(mulii((GEN)logdisc[i],(GEN)uu[i]), gell); | GEN e = modii(mulii((GEN)logdisc[i],(GEN)u[i]), gell); | isprincipalell(GEN bnfz, GEN id, GEN cycgen, GEN uu, GEN gell, long rc){ long i, l = lg(cycgen); GEN y, logdisc, b; y = isprincipalgenforce(bnfz,id); logdisc = gmod((GEN)y[1], gell); b = to_famat_all((GEN)y[2], gun); for (i=rc+1; i<l; i++) { GEN e = modii(mulii((GEN)logdisc[i],(GEN)uu[i]), gell); b = famat_mul(b, famat_pow((GEN)cycgen[i], e)); } y = cgetg(3,t_VEC); y[1] = (long)logdisc; setlg(logdisc,rc+1); y[2] = (long)b; return y;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/73929b165be3dc8f342788321bf5a06394a0cf3d/kummer.c/clean/src/modules/kummer.c |
return &prototab[i]; | return (struct protoent *) &prototab[i]; | getprotobynumber (int proto){ int i; for (i = 0 ; i < (sizeof prototab / sizeof prototab[0]) ; i++) { if (proto == prototab[i].p_proto) return &prototab[i]; } return NULL;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/ff0f694d466fb114c185bf464811658f97d012f1/getprotoby.c/clean/c/src/libnetworking/lib/getprotoby.c |
if (*analyseur == '"') bp = readstring_i(bp, &buf,&limit); else bp = expand_string(bp, &buf,&limit); | bp = expand_string(bp, &buf,&limit); | identifier(void){ long m, i, matchcomma, deriv; gpmem_t av; char *ch1; entree *ep; GEN res, newfun, ptr; mark.identifier = analyseur; ep = entry(); if (EpVALENCE(ep)==EpVAR || EpVALENCE(ep)==EpGVAR) { /* optimized for simple variables */ switch (*analyseur) { case ')': case ',': return (GEN)ep->value; case '.': { long len, v; analyseur++; ch1 = analyseur; if ((res = read_member((GEN)ep->value))) { if (*analyseur == '[') { matcomp c; res = matcell(res, &c); } return res; } /* define a new member function */ v = varn(initial_value(ep)); len = analyseur - ch1; analyseur++; /* skip = */ ep = installep(NULL,ch1,len,EpMEMBER,0, members_hash + hashvalue(ch1)); ch1 = analyseur; skipseq(); len = analyseur-ch1; newfun=ptr= (GEN) newbloc(1 + (len>>TWOPOTBYTES_IN_LONG) + 4); newfun++; /* this bloc is no GEN, leave the first cell alone ( = 0) */ *newfun++ = v; /* record text */ strncpy((char *)newfun, ch1, len); ((char *) newfun)[len] = 0; ep->value = (void *)ptr; return gnil; } } if (*analyseur != '[') { /* whole variable, no component */ F2GEN fun = affect_block(&res); if (res) { if (fun) res = fun((GEN)ep->value, res); changevalue(ep,res); } return (GEN)ep->value; } return matrix_block((GEN)ep->value); } ep = do_alias(ep); matchcomma = 0;#ifdef STACK_CHECK if (PARI_stack_limit && (void*) &ptr <= PARI_stack_limit) err(talker2, "deep recursion", mark.identifier, mark.start);#endif if (ep->code) { char *s = ep->code, *oldanalyseur = NULL, *buf, *limit, *bp; unsigned int ret, noparen, has_pointer=0; long fake; void *call = ep->value; GEN argvec[9]; matcomp *init[9]; deriv = (*analyseur == '\'' && analyseur[1] == '(') && analyseur++; if (*analyseur == '(') { analyseur++; noparen=0; /* expect matching ')' */ } else { /* if no mandatory argument, no () needed */ if (EpVALENCE(ep)) match('('); /* error */ if (!*s || (!s[1] && *s == 'p')) return ((GEN (*)(long))call)(prec); noparen=1; /* no argument, but valence is ok */ } /* return type */ if (*s == 'v') { ret = RET_VOID; s++; } else if (*s == 'l') { ret = RET_INT; s++; } else ret = RET_GEN; /* Optimized for G and p. */ i = 0; while (*s == 'G') { match_comma(); s++; argvec[i++] = expr(); if (br_status) err(breaker,"here (argument reading)"); } if (*s == 'p') { argvec[i++] = (GEN) prec; s++; } while (*s) switch (*s++) { case 'G': /* GEN */ match_comma(); argvec[i++] = expr(); if (br_status) err(breaker,"here (argument reading)"); break; case 'L': /* long */ match_comma(); argvec[i++] = (GEN) readlong(); break; case 'n': /* var number */ match_comma(); argvec[i++] = (GEN) readvar(); break; case 'S': /* symbol */ match_comma(); mark.symbol=analyseur; argvec[i++] = (GEN)entry(); break; case 'V': /* variable */ match_comma(); mark.symbol=analyseur; { entree *e = entry(); long v = EpVALENCE(e); if (v != EpVAR && v != EpGVAR) err(talker2,"not a variable:",mark.symbol,mark.start); argvec[i++] = (GEN)e; break; } case '&': /* *GEN */ match_comma(); match('&'); mark.symbol=analyseur; { matcomp *c = (matcomp*)malloc(sizeof(matcomp)); entree *ep = entry(); if (*analyseur == '[') (void)matcell((GEN)ep->value, c); else { c->parent = NULL; c->ptcell = (GEN*)&ep->value; } has_pointer |= (1 << i); init[i] = c; argvec[i++] = (GEN)c->ptcell; break; } /* Input position */ case 'E': /* expr */ case 'I': /* seq */ match_comma(); argvec[i++] = (GEN) analyseur; skipseq(); break; case 'r': /* raw */ match_comma(); mark.raw = analyseur; bp = init_buf(256, &buf,&limit); while (*analyseur) { if (*analyseur == ',' || *analyseur == ')') break; if (*analyseur == '"') bp = readstring_i(bp, &buf,&limit); else { if (bp > limit) bp = realloc_buf(bp,1, &buf,&limit); *bp++ = *analyseur++; } } *bp++ = 0; argvec[i++] = (GEN) buf; break; case 's': /* expanded string; empty arg yields "" */ match_comma(); if (*s == '*') /* any number of string objects */ { argvec[i++] = any_string(); s++; break; } bp = init_buf(256, &buf,&limit); while (*analyseur) { if (*analyseur == ',' || *analyseur == ')') break; if (*analyseur == '"') bp = readstring_i(bp, &buf,&limit); else bp = expand_string(bp, &buf,&limit); } *bp++ = 0; argvec[i++] = (GEN)buf; break; case 'p': /* precision */ argvec[i++] = (GEN) prec; break; case '=': match('='); matchcomma = 0; break; case 'D': /* Has a default value */ if (do_switch(noparen,matchcomma)) switch (*s) { case 'G': case '&': case 'I': case 'V': argvec[i++]=DFT_GEN; s++; break; case 'n': argvec[i++]=DFT_VAR; s++; break; default: oldanalyseur = analyseur; analyseur = s; matchcomma = 0; while (*s++ != ','); } else switch (*s) { case 'G': case '&': case 'I': case 'V': case 'n': break; default: while (*s++ != ','); } break; case 'P': /* series precision */ argvec[i++] = (GEN) precdl; break; case 'f': /* Fake *long argument */ argvec[i++] = (GEN) &fake; break; case 'x': /* Foreign function */ argvec[i++] = (GEN) ep; call = foreignHandler; break; case ',': /* Clean up default */ if (oldanalyseur) { analyseur = oldanalyseur; oldanalyseur = NULL; matchcomma=1; } break; default: err(bugparier,"identifier (unknown code)"); }#if 0 /* uncomment if using purify: unitialized read otherwise */ for ( ; i<9; i++) argvec[i]=NULL;#endif if (deriv) { if (!i || (ep->code)[0] != 'G') err(talker2, "can't derive this", mark.identifier, mark.start); res = num_deriv(call, argvec); } else switch (ret) { default: /* case RET_GEN: */ res = ((PFGEN)call)(_ARGS_); break; case RET_INT: m = ((long (*)(ANYARG))call)(_ARGS_); res = stoi(m); break; case RET_VOID: ((void (*)(ANYARG))call)(_ARGS_); res = gnil; break; } if (has_pointer) check_pointers(has_pointer,init); if (!noparen) match(')'); return res; } if (EpPREDEFINED(ep)) { if (*analyseur != '(') { if (EpVALENCE(ep) == 88) return global0(); match('('); /* error */ } analyseur++; switch(EpVALENCE(ep)) { case 50: /* O */ res = truc(); if (br_status) err(breaker,"here (in O()))"); if (*analyseur=='^') { analyseur++; m = readlong(); } else m = 1; res = ggrando(res,m); break; case 80: /* if then else */ av = avma; res = expr(); if (br_status) err(breaker,"test expressions"); m = gcmp0(res); avma = av; match(','); if (m) /* false */ { skipseq(); if (*analyseur == ')') res = gnil; else { match(','); res = seq(); if (br_status) { res = NULL; skipseq(); } } } else /* true */ { res = seq(); if (br_status) { res = NULL; skipseq(); } if (*analyseur != ')') { match(','); skipseq(); } } break; case 81: /* while do */ av = avma; ch1 = analyseur; for(;;) { res = expr(); if (br_status) err(breaker,"test expressions"); if (gcmp0(res)) { match(','); break; } avma = av; match(','); (void)seq(); if (loop_break()) break; analyseur = ch1; } avma = av; skipseq(); res = gnil; break; case 82: /* repeat until */ av = avma; ch1 = analyseur; skipexpr(); for(;;) { avma = av; match(','); (void)seq(); if (loop_break()) break; analyseur = ch1; res = expr(); if (br_status) err(breaker,"test expressions"); if (!gcmp0(res)) { match(','); break; } } avma = av; skipseq(); res = gnil; break; case 88: /* global */ if (*analyseur == ')') return global0(); while (*analyseur != ')') { match_comma(); ch1=analyseur; check_var_name(); ep = skipentry(); switch(EpVALENCE(ep)) { case EpGVAR:#if 0 err(warner,"%s already declared global", ep->name);#endif /* fall through */ case EpVAR: break; default: err(talker2,"symbol already in use",ch1,mark.start); } analyseur=ch1; ep = entry(); if (*analyseur == '=') { gpmem_t av=avma; analyseur++; res = expr(); if (br_status) err(breaker,"here (defining global var)"); changevalue(ep, res); avma=av; } ep->valence = EpGVAR; } res = gnil; break; default: err(valencer1); return NULL; /* not reached */ } match(')'); return res; } switch (EpVALENCE(ep)) { GEN *defarg; /* = default args, and values for local variables */ int narg, nloc; gp_args *f; case EpUSER: /* user-defined functions */ f = (gp_args*)ep->args; defarg = f->arg; narg = f->narg; nloc = f->nloc; deriv = (*analyseur == '\'' && analyseur[1] == '(') && analyseur++; if (*analyseur != '(') /* no args */ { if (*analyseur != '=' || analyseur[1] == '=') { GEN *arglist = (GEN*) new_chunk(narg); for (i=0; i<narg; i++) arglist[i] = make_arg(defarg[i]); return call_fun((GEN)ep->value, arglist, defarg+narg, narg, nloc); } match('('); /* ==> error */ } if (analyseur != redefine_fun) { GEN *arglist = (GEN*) new_chunk(narg); ch1 = analyseur; analyseur++; for (i=0; i<narg; i++) { if (do_switch(0,matchcomma)) { /* default arg */ arglist[i] = make_arg(defarg[i]); matchcomma = 1; } else { /* user supplied */ match_comma(); arglist[i] = expr(); skipdecl(); /* we'd be redefining fun, but don't know it yet */ if (br_status) err(breaker,"here (reading function args)"); } } if (*analyseur++ == ')' && (*analyseur != '=' || analyseur[1] == '=')) { if (deriv) { if (!narg) err(talker2, "can't derive this", mark.identifier, mark.start); return num_derivU((GEN)ep->value, arglist, defarg+narg, narg, nloc); } return call_fun((GEN)ep->value, arglist, defarg+narg, narg, nloc); } /* should happen only in cases like (f()= f()=); f (!!!) */ analyseur--; if (*analyseur != ',' && *analyseur != ')') skipexpr(); while (*analyseur == ',') { analyseur++; skipexpr(); } match(')'); if (*analyseur != '=' || analyseur[1] == '=') err(nparamer1,mark.identifier,mark.start); matchcomma=0; analyseur = ch1; } redefine_fun = NULL; free_args((gp_args*)ep->args); /* Fall through */ case EpNEW: /* new function */ { GEN tmpargs = (GEN)avma; char *start; long len; check_new_fun = ep; /* checking arguments */ match('('); ch1 = analyseur; narg = check_args(); nloc = 0; match(')'); /* Dirty, but don't want to define a local() function */ if (*analyseur != '=' && strcmp(ep->name, "local") == 0) err(talker2, "local() bloc must appear before any other expression", mark.identifier,mark.start); match('='); while (strncmp(analyseur,"local(",6) == 0) { analyseur += 6; nloc += check_args(); match(')'); while(separe(*analyseur)) analyseur++; } { /* checking function definition */ char *oldredef = redefine_fun; skipping_fun_def++; start = analyseur; skipseq(); len = analyseur-start; skipping_fun_def--; redefine_fun = oldredef; } /* function is ok. record it */ newfun = ptr = (GEN) newbloc(narg+nloc + (len>>TWOPOTBYTES_IN_LONG) + 4); newfun++; /* this bloc is no GEN, leave the first cell alone ( = 0) */ /* record default args */ f = (gp_args*) gpmalloc((narg+nloc)*sizeof(GEN) + sizeof(gp_args)); ep->args = (void*) f; f->nloc = nloc; f->narg = narg; f->arg = defarg = (GEN*)(f + 1); narg += nloc; /* record default args and local variables */ for (i = 1; i <= narg; i++) { GEN cell = tmpargs-(i<<1); *newfun++ = cell[0]; *defarg++ = (GEN)cell[1]; } if (narg > 1) { /* check for duplicates */ GEN x = new_chunk(narg), v = ptr+1; long k; for (i=0; i<narg; i++) x[i] = v[i]; qsort(x,narg,sizeof(long),(QSCOMP)pari_compare_long); for (k=x[0],i=1; i<narg; k=x[i],i++) if (x[i] == k) err(talker,"user function %s: variable %Z declared twice", ep->name, polx[k]); } /* record text */ strncpy((char *)newfun, start, len); ((char *) newfun)[len] = 0; if (EpVALENCE(ep) == EpUSER) gunclone((GEN)ep->value); /* have to wait till here because of strncopy above. In pathological * cases, e.g. (f()=f()=x), new text is given by value of old one! */ ep->value = (void *)ptr; ep->valence = EpUSER; check_new_fun=NULL; avma = (gpmem_t)tmpargs; return gnil; } } err(valencer1); return NULL; /* not reached */} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/62a31d7cdb146350310e18b922ba1239df4ca90e/anal.c/buggy/src/language/anal.c |
slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi) | slotFor (Triv121PgTbl pt, unsigned long vsid, unsigned long pi) | slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi){int i;unsigned long hash,api;PTE pte; /* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } }#ifdef DEBUG /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash);#endif hash = PTE_HASH2(hash);#ifdef DEBUG PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash));#endif for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } }#ifdef DEBUG /* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash);#endif return 0;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1b1b43ccc1a6d654199ddb6329366d2e44bdaea3/pte121.c/clean/c/src/lib/libcpu/powerpc/mpc6xx/mmu/pte121.c |
int i; unsigned long hash,api; PTE pte; | int i; unsigned long hash, api; APte pte; | slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi){int i;unsigned long hash,api;PTE pte; /* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } }#ifdef DEBUG /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash);#endif hash = PTE_HASH2(hash);#ifdef DEBUG PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash));#endif for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } }#ifdef DEBUG /* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash);#endif return 0;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1b1b43ccc1a6d654199ddb6329366d2e44bdaea3/pte121.c/clean/c/src/lib/libcpu/powerpc/mpc6xx/mmu/pte121.c |
/* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } } | /* primary hash */ hash = PTE_HASH1 (vsid, pi); api = API (pi); /* linear search thru all buckets for this hash */ for (i = 0, pte = ptegOf (pt, hash); i < PTE_PER_PTEG; i++, pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h = 0; /* found by the primary hash fn */ pte->marked = 1; return pte; } } | slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi){int i;unsigned long hash,api;PTE pte; /* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } }#ifdef DEBUG /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash);#endif hash = PTE_HASH2(hash);#ifdef DEBUG PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash));#endif for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } }#ifdef DEBUG /* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash);#endif return 0;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1b1b43ccc1a6d654199ddb6329366d2e44bdaea3/pte121.c/clean/c/src/lib/libcpu/powerpc/mpc6xx/mmu/pte121.c |
/* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash); | /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF ("## First hash bucket full - "); dumpPteg (vsid, pi, hash); | slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi){int i;unsigned long hash,api;PTE pte; /* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } }#ifdef DEBUG /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash);#endif hash = PTE_HASH2(hash);#ifdef DEBUG PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash));#endif for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } }#ifdef DEBUG /* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash);#endif return 0;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1b1b43ccc1a6d654199ddb6329366d2e44bdaea3/pte121.c/clean/c/src/lib/libcpu/powerpc/mpc6xx/mmu/pte121.c |
hash = PTE_HASH2(hash); | hash = PTE_HASH2 (hash); | slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi){int i;unsigned long hash,api;PTE pte; /* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } }#ifdef DEBUG /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash);#endif hash = PTE_HASH2(hash);#ifdef DEBUG PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash));#endif for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } }#ifdef DEBUG /* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash);#endif return 0;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1b1b43ccc1a6d654199ddb6329366d2e44bdaea3/pte121.c/clean/c/src/lib/libcpu/powerpc/mpc6xx/mmu/pte121.c |
PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash)); | PRINTF (" Secondary pteg is 0x%08x\n", (unsigned) ptegOf (pt, hash)); | slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi){int i;unsigned long hash,api;PTE pte; /* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } }#ifdef DEBUG /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash);#endif hash = PTE_HASH2(hash);#ifdef DEBUG PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash));#endif for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } }#ifdef DEBUG /* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash);#endif return 0;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1b1b43ccc1a6d654199ddb6329366d2e44bdaea3/pte121.c/clean/c/src/lib/libcpu/powerpc/mpc6xx/mmu/pte121.c |
for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } } | for (i = 0, pte = ptegOf (pt, hash); i < PTE_PER_PTEG; i++, pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked = 1; pte->h = 1; return pte; } } | slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi){int i;unsigned long hash,api;PTE pte; /* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } }#ifdef DEBUG /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash);#endif hash = PTE_HASH2(hash);#ifdef DEBUG PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash));#endif for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } }#ifdef DEBUG /* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash);#endif return 0;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1b1b43ccc1a6d654199ddb6329366d2e44bdaea3/pte121.c/clean/c/src/lib/libcpu/powerpc/mpc6xx/mmu/pte121.c |
/* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash); | /* Even more strange - most likely, something is REALLY messed up */ PRINTF ("## Second hash bucket full - "); dumpPteg (vsid, pi, hash); | slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi){int i;unsigned long hash,api;PTE pte; /* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } }#ifdef DEBUG /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash);#endif hash = PTE_HASH2(hash);#ifdef DEBUG PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash));#endif for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } }#ifdef DEBUG /* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash);#endif return 0;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1b1b43ccc1a6d654199ddb6329366d2e44bdaea3/pte121.c/clean/c/src/lib/libcpu/powerpc/mpc6xx/mmu/pte121.c |
return 0; | return 0; | slotFor(Triv121PgTbl pt, unsigned long vsid, unsigned long pi){int i;unsigned long hash,api;PTE pte; /* primary hash */ hash = PTE_HASH1(vsid,pi); api=API(pi); /* linear search thru all buckets for this hash */ for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* found a free PTE; mark it as potentially used and return */ pte->h=0; /* found by the primary hash fn */ pte->marked=1; return pte; } }#ifdef DEBUG /* Strange: if the hash table was allocated big enough, * this should not happen (when using a 1:1 mapping) * Give them some information... */ PRINTF("## First hash bucket full - "); dumpPteg(vsid,pi,hash);#endif hash = PTE_HASH2(hash);#ifdef DEBUG PRINTF(" Secondary pteg is 0x%08x\n", (unsigned)ptegOf(pt,hash));#endif for (i=0, pte=ptegOf(pt,hash); i<PTE_PER_PTEG; i++,pte++) { if (!pte->v && !pte->marked) { /* mark this pte as potentially used */ pte->marked=1; pte->h=1; return pte; } }#ifdef DEBUG /* Even more strange - most likely, something is REALLY messed up */ PRINTF("## Second hash bucket full - "); dumpPteg(vsid,pi,hash);#endif return 0;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/1b1b43ccc1a6d654199ddb6329366d2e44bdaea3/pte121.c/clean/c/src/lib/libcpu/powerpc/mpc6xx/mmu/pte121.c |
printf( "gettimeofday: %s", ctime( &tv.tv_sec ) ); | a_time_t = tv.tv_sec; /* ctime() takes a time_t */ printf( "gettimeofday: %s", ctime( &a_time_t) ); | void check_a_tod( rtems_time_of_day *the_tod){ rtems_status_code status; rtems_time_of_day new_tod; time_t a_time_t; struct timeval tv; struct tm *a_tm; int result; int i = 0; print_time( "rtems_clock_set ", the_tod, "\n" ); status = rtems_clock_set( the_tod ); assert( !status ); do { status = rtems_clock_get( RTEMS_CLOCK_GET_TOD, &new_tod ); assert( !status ); print_time( "rtems_clock_get ", &new_tod, "\n" ); /* now do the posix time gets */ result = gettimeofday( &tv, 0 ); assert( result == 0 ); printf( "gettimeofday: %s", ctime( &tv.tv_sec ) ); a_time_t = time( 0 ); printf( "time: %s", ctime( &a_time_t ) ); a_tm = localtime( &a_time_t ); printf( "localtime: %s", asctime( a_tm ) ); a_tm = gmtime( &a_time_t ); printf( "gmtime: %s\n", asctime( a_tm ) ); status = rtems_task_wake_after( 5 * TICKS_PER_SECOND ); i++; } while( i < 6 );} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/bfded728ec4c912f666df94867c118a1004b0165/test.c/buggy/c/src/tests/psxtests/psxtime/test.c |
ulong M_2 = h->M << 1; long x = 0, count = 0; | long x = 0, count = 0, M_2 = h->M << 1; | mpqs_eval_sieve(mpqs_handle_t *h){ ulong M_2 = h->M << 1; long x = 0, count = 0; /* XX Todo: replace the following by an auto-adjusting threshold driven * XX by histogram yield measurements */ unsigned char th = h->sieve_threshold; unsigned char *sieve_array = h->sieve_array; long *candidates = h->candidates; /* The following variation on the original is marginally faster with a * good optimizing compiler. Exploiting the sentinel, we don't need to * check for x < M_2 in the inner while loop - this more than makes up * for the "lack" of explicit unrolling. Optimizations like loop * unrolling are best left to the compiler anyway... */ while (count < MPQS_CANDIDATE_ARRAY_SIZE - 1) { while (sieve_array[x] < th) x++; if (x >= M_2) break; candidates[count++] = x++; } candidates[count] = 0; return count;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f2ea9bd32cba7fad43082b848508b3e81edfea5d/mpqs.c/clean/src/modules/mpqs.c |
GEN g = mppgcd(Qx, kN); | GEN g = gcdii(Qx, kN); | mpqs_eval_candidates(GEN A, GEN inv_A4, GEN B, GEN kN, long k, double sqrt_kN, long *FB, long *start_1, long *start_2, ulong M, long bin_index, long *candidates, long number_of_candidates, long lp_bound, long start_index_FB_for_A, FILE *FREL, FILE *LPREL) /* NB FREL, LPREL are actually FNEW, LPNEW when we get called */{ double a, b, inv_2_a; pari_sp av; long z1, z2, number_of_relations = 0; char *relations; ulong i, pi, size_of_FB;#ifdef MPQS_DEBUG_VERBOSE static char complaint[256], complaint0[256]; complaint[0] = '\0';#endif /* roots of Q(X), used to find sign(Q(x)) after reducing mod kN */ a = gtodouble(A); inv_2_a = 1 / (2.0 * a); b = gtodouble(B); z1 = (long) ((-b - sqrt_kN) * inv_2_a); z2 = (long) ((-b + sqrt_kN) * inv_2_a); /* Worst case: * + leading " 1 1" * + trailing " 0\n" with final NUL character * + in between up to size_of_FB pairs each consisting of an exponent, a * subscript into FB, and two spaces. * Subscripts into FB fit into 5 digits, and exponents fit into 3 digits * with room to spare -- anything needing 3 or more digits for the * subscript must come with an exponent of at most 2 digits. Moreover the * product of the first 58 primes is larger than 10^110, so there cannot be * more than 60 pairs in all, even if size_of_FB > 10^4. --GN */ size_of_FB = FB[0]; /* one less than usually: don't count FB[1] */ if (size_of_FB > 60) size_of_FB = 60; relations = (char *) gpmalloc((8 + size_of_FB * 9) * sizeof(char)); av = avma; for (i = 0; i < (ulong)number_of_candidates; i++, avma = av) { GEN Qx, A_2x_plus_B, Y; long powers_of_2, p, bi; long x = candidates[i]; long x_minus_M = x - M; char *relations_end = relations; *relations_end = 0;#ifdef MPQS_DEBUG_VERYVERBOSE fprintferr("%c", (char)('0' + i%10));#endif /* A_2x_plus_B = (A*(2x)+B), Qx = (A*(2x)+B)^2/(4*A) = Q(x) */ A_2x_plus_B = modii(addii(mulis(A, x_minus_M << 1), B), kN); Y = subii(kN, A_2x_plus_B); if (absi_cmp(A_2x_plus_B, Y) < 0) Y = A_2x_plus_B; /* absolute value of smallest absolute residue of A_2x_plus_B mod kN */ Qx = modii(sqri(Y), kN); /* Most of the time, gcd(Qx, kN) = 1 or k. However, it may happen that * Qx is a multiple of N, esp. when N is small, leading to havoc below -- * so we have to be a bit careful. Of course we cannot afford to compute * the gcd each time through this loop unless we are debugging... --GN */#ifdef MPQS_DEBUG { long ks; pari_sp av1 = avma; GEN g = mppgcd(Qx, kN); if (is_pm1(g)) { if ((ks = kronecker(Qx, kN)) != 1) { fprintferr("\nMPQS: 4*A*Q(x) = %Z\n", Qx); fprintferr("\tKronecker symbol %ld\n", ks); err(talker, "MPQS: 4*A*Q(x) is not a square (mod kN)"); } }# ifdef MPQS_DEBUG_VERBOSE else if (cmpis(g,k) /* != 0 */ ) { char *gs = GENtostr(g); sprintf(complaint, "\nMPQS: gcd(4*A*Q(x), kN) = %s\n", gs); free(gs); if (strcmp(complaint, complaint0)) { fprintferr(complaint); strcpy(complaint0, complaint); } }# endif avma = av1; }#endif Qx = modii(mulii(Qx, inv_A4), kN); /* check the sign of Qx */ if (z1 < x_minus_M && x_minus_M < z2) { Qx = subii(kN, Qx); mpqs_add_factor(&relations_end, 1, 1); /* i = 1, ei = 1, pi */ } if (!signe(Qx)) {#ifdef MPQS_DEBUG_VERBOSE fprintferr("<+>");#endif continue; } /* divide by powers of 2; we're really dealing with 4*A*Q(x), so we * remember an extra factor 2^2 */ powers_of_2 = vali(Qx); Qx = shifti(Qx, -powers_of_2); mpqs_add_factor(&relations_end, powers_of_2 + 2, 2); /* we handled the case p = 2 already */ bi = bin_index;#ifdef MPQS_DEBUG_VERBOSE fprintferr("a");#endif /* FB[3 .. start_index_FB_for_A] do not divide A. * p = FB[start_index_FB_for_A+j+1] divides A (to the first power) * iff the 2^j bit in bin_index is set */ for (pi = 3; (p = FB[pi]) != 0; pi++) { long tmp_p = x % p; ulong ei = 0; if (bi && pi > (ulong)start_index_FB_for_A) { ei = bi & 1; /* either 0 or 1 */ bi >>= 1; } if (tmp_p == start_1[pi] || tmp_p == start_2[pi]) { /* p divides Q(x) and possibly A */ long remd_p; GEN Qx_div_p = divis_rem(Qx, p, &remd_p); if (remd_p) break; /* useless candidate: abort */ do { ei++; Qx = Qx_div_p; Qx_div_p = divis_rem(Qx, p, &remd_p); } while (remd_p == 0); } if (ei) /* p might divide A but not Q(x) */ mpqs_add_factor(&relations_end, ei, pi); } if (p) {#ifdef MPQS_DEBUG_VERBOSE fprintferr("\b<");#endif continue; /* loop aborted: useless candidate */ }#ifdef MPQS_DEBUG_VERBOSE fprintferr("\bb");#endif if (is_pm1(Qx)) { char *Qxstring = GENtostr(Y); mpqs_add_0(&relations_end); fprintf(FREL, "%s :%s\n", Qxstring, relations); free(Qxstring); number_of_relations++;#ifdef MPQS_DEBUG { pari_sp av1 = avma; GEN rhs = mpqs_factorback(FB, relations, kN); GEN Qx_2 = modii(sqri(Y), kN); if (!egalii(Qx_2, rhs)) {#ifdef MPQS_DEBUG_VERBOSE fprintferr("\b(!)\n");#endif fprintferr("MPQS: %Z @ %Z :%s\n", Y, Qx, relations); fprintferr("\tQx_2 = %Z\n", Qx_2); fprintferr("\t rhs = %Z\n", rhs); err(talker, "MPQS: wrong full relation found!!"); }#ifdef MPQS_DEBUG_VERBOSE else fprintferr("\b(:)");#endif avma = av1; }#endif } else if (cmpis(Qx, lp_bound) > 0) { /* TODO: check for double large prime */#ifdef MPQS_DEBUG_VERBOSE fprintferr("\b.");#endif } else if (k==1 || cgcd(k, itos(Qx)) == 1) { /* if (mpqs_isprime(itos(Qx))) */ char *Qxstring = GENtostr(Y); char *L1string = GENtostr(Qx); mpqs_add_0(&relations_end); fprintf(LPREL, "%s @ %s :%s\n", L1string, Qxstring, relations); free(Qxstring); free(L1string);#ifdef MPQS_DEBUG { pari_sp av1 = avma; GEN rhs = mpqs_factorback(FB, relations, kN); GEN Qx_2 = modii(sqri(Y), kN); rhs = modii(mulii(rhs, Qx), kN); if (!egalii(Qx_2, rhs)) {#ifdef MPQS_DEBUG_VERBOSE fprintferr("\b(!)\n");#endif fprintferr("MPQS: %Z @ %Z :%s\n", Y, Qx, relations); fprintferr("\tQx_2 = %Z\n", Qx_2); fprintferr("\t rhs = %Z\n", rhs); err(talker, "MPQS: wrong large prime relation found!!"); }#ifdef MPQS_DEBUG_VERBOSE else fprintferr("\b(;)");#endif avma = av1; }#endif }#ifdef MPQS_DEBUG_VERBOSE else fprintferr("\b<k>");#endif } /* for */#ifdef MPQS_DEBUG_VERBOSE fprintferr("\n");#endif free(relations); return number_of_relations;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/2edc82b0d172402925241429eca01a236af8dd01/mpqs.c/clean/src/modules/mpqs.c |
if (pi < h->index0_FB) { | if ((mpqs_int32_t)pi < h->index0_FB) { | mpqs_eval_cand(mpqs_handle_t *h, long number_of_cand, FILE *FREL, FILE *LPREL) /* NB FREL, LPREL are actually FNEW, LPNEW when we get called */{ pari_sp av; long number_of_relations = 0; char *relations = h->relations; long *relaprimes = h->relaprimes; ulong i, pi; mpqs_FB_entry_t *FB = h->FB; GEN A = h->A; GEN B = h->B; /* we don't need coefficient C here */ int pii; long *candidates = h->candidates; av = avma;#ifdef MPQS_DEBUG_AVMA fprintferr("MPQS DEBUG: enter eval cand, avma = 0x%lX\n", (ulong)avma);#endif for (i = 0; i < (ulong)number_of_cand; i++, avma = av) { GEN Qx, Qx_part, A_2x_plus_B, Y; long powers_of_2, p; long x = candidates[i]; long x_minus_M = x - h->M; char *relations_end = relations; int relaprpos = 0;#ifdef MPQS_DEBUG_AVMA fprintferr("MPQS DEBUG: eval loop 1, avma = 0x%lX\n", (ulong)avma);#endif *relations_end = 0;#ifdef MPQS_DEBUG_VERYVERBOSE fprintferr("%c", (char)('0' + i%10));#endif /* A_2x_plus_B = (A*(2x)+B), Qx = (A*(2x)+B)^2/(4*A) = Q(x) */ A_2x_plus_B = addii(mulis(A, x_minus_M << 1), B); Y = absi(A_2x_plus_B); Qx = subii(sqri(A_2x_plus_B), h->kN);#ifdef MPQS_DEBUG_AVMA fprintferr("MPQS DEBUG: eval loop 2, avma = 0x%lX\n", (ulong)avma);#endif /* When N is relatively small, it may happen that Qx is outright * divisible by N at this point. In any case, when no extensive prior * trial division / Rho / ECM had been attempted, gcd(Qx,N) may turn * out to be a nontrivial factor of N (larger than what the FB contains * or we'd have found it already, but possibly smaller than the large- * prime bound). This is too rare to check for here in the inner loop, * but it will be caught if such an LP relation is ever combined with * another. */ /* XXX Qx cannot possibly vanish here. */ if (!signe(Qx)) { PRINT_IF_VERBOSE("<+>"); continue; } else if (signe(Qx) < 0) { setsigne(Qx, 1); mpqs_add_factor(&relations_end, 1, 1); /* i = 1, ei = 1, pi */ } /* divide by powers of 2; we're really dealing with 4*A*Q(x), so we * always have at least 2^2 here, and at least 2^3 when kN is 1 mod 4 */ powers_of_2 = vali(Qx); Qx = shifti(Qx, -powers_of_2); mpqs_add_factor(&relations_end, powers_of_2, 2); /* That has dealt with a possible -1 and the power of 2. First pass * over odd primes in FB: pick up all possible divisors of Qx including * those sitting in k or in A, and remember them in relaprimes. Do not * yet worry about possible repeated factors, these will be found in the * second pass. */ Qx_part = A; /* The first pass recognizes divisors of A by their corresponding flags * bit in the FB entry. (Divisors of k require no special treatment at * this stage.) We construct a preliminary table of FB subscripts and * "exponents" of the FB primes which divide Qx. (We store subscripts * rather than the primes themselves because the string representation * of a relation is in terms of the subscripts.) * We must distinguish three cases so we can do the right thing in the * 2nd pass: prime not in A which divides Qx, prime in A which does not * divide Qx/A, prime in A which does divide Qx/A. The first and third * kinds need checking for repeated factors, the second kind doesn't. The * first and second kinds contribute 1 to the exponent in the relation, * the 3rd kind contributes 2. We store 1,0,2 respectively in these three * cases. * Factors in common with k are much simpler - if they occur, they occur * exactly to the first power, and this makes no difference in the first * pass - here they behave just like every normal odd factor base prime. */ for (pi = 3; (p = FB[pi].fbe_p); pi++) { long tmp_p = x % p; ulong ei = 0; /* Here we use that MPQS_FBE_DIVIDES_A equals 1. */ ei = FB[pi].fbe_flags & MPQS_FBE_DIVIDES_A; if (tmp_p == FB[pi].fbe_start1 || tmp_p == FB[pi].fbe_start2) { /* p divides Q(x)/A (and possibly A), 1st or 3rd case */ relaprimes[relaprpos++] = pi; relaprimes[relaprpos++] = 1 + ei; Qx_part = mulis(Qx_part, p); } else if (ei) { /* p divides A but does not divide Q(x)/A, 2nd case */ relaprimes[relaprpos++] = pi; relaprimes[relaprpos++] = 0; } } /* We have now accumulated the known factors of Qx except for possible * repeated factors and for possible large primes. Divide off what we * have. (This is faster than dividing off A and each prime separately.) */ Qx = diviiexact(Qx, Qx_part); /* (ToDo: MPQS_DEBUG sanity check...) */#ifdef MPQS_DEBUG_AVMA fprintferr("MPQS DEBUG: eval loop 3, avma = 0x%lX\n", (ulong)avma);#endif /* second pass - deal with any repeated factors, and write out the string * representation of the tentative relation. At this point, the only * primes which can occur again in the adjusted Qx are those in relaprimes * which are followed by 1 or 2. We must pick up those followed by a 0, * too, though. */ PRINT_IF_VERBOSE("a"); for (pii = 0; pii < relaprpos; pii+=2) { long remd_p; ulong ei = relaprimes[pii+1]; GEN Qx_div_p; pi = relaprimes[pii]; /* Here, prime factors of k go their separate way. We could have * introduced another FB entry flag for marking them, but it is easier * to identify them just by their position before index0_FB. */ if (pi < h->index0_FB) {#ifdef MPQS_DEBUG PRINT_IF_VERBOSE("\bk!");#endif mpqs_add_factor(&relations_end, 1, pi); continue; } if (ei == 0) /* p divides A and that was it */ { mpqs_add_factor(&relations_end, 1, pi); continue; } p = FB[pi].fbe_p;#ifdef MPQS_DEBUG_CANDIDATE_EVALUATION fprintferr("MPQS DEBUG: Qx=%Z p=%ld\n", Qx, (long)p);#endif /* otherwise p might still divide the current adjusted Qx. Try it... */ /* XXX break out of loop when remaining Qx is 1. Or rather, suppress * the trial divisions, since we still need to write our string. * Actually instead of testing for 1, test whether Qx is smaller than * p; cf Karim's mail from 20050124. If it is, without being 1, * then it has a common factor with k. But those factors are soon * going to have disappeared before we get here. However, inserting * an explicit if (!is_pm1(Qx)) here did not help any. */ Qx_div_p = divis_rem(Qx, p, &remd_p); while (remd_p == 0) { ei++; Qx = Qx_div_p; Qx_div_p = divis_rem(Qx, p, &remd_p); } mpqs_add_factor(&relations_end, ei, pi); }#ifdef MPQS_DEBUG_AVMA fprintferr("MPQS DEBUG: eval loop 4, avma = 0x%lX\n", (ulong)avma);#endif PRINT_IF_VERBOSE("\bb"); if (is_pm1(Qx)) { mpqs_add_0(&relations_end); fprintf(FREL, "%s :%s\n", i2str(Y), relations); number_of_relations++;#ifdef MPQS_USE_HISTOGRAMS /* bump full relations counter at candidate's value */ if (h->do_histograms) h->histo_full[sa[x]-128]++;#endif#ifdef MPQS_DEBUG { pari_sp av1 = avma; GEN rhs = mpqs_factorback(h, relations); GEN Qx_2 = remii(sqri(Y), h->N); if (!egalii(Qx_2, rhs)) { PRINT_IF_VERBOSE("\b(!)\n"); fprintferr("MPQS: %Z @ %Z :%s\n", Y, Qx, relations); fprintferr("\tQx_2 = %Z\n", Qx_2); fprintferr("\t rhs = %Z\n", rhs); err(talker, "MPQS: wrong full relation found!!"); } else PRINT_IF_VERBOSE("\b(:)"); avma = av1; }#endif } else if (cmpis(Qx, h->lp_bound) > 0) { /* TODO: check for double large prime */#ifdef MPQS_USE_HISTOGRAMS /* bump useless-candidates counter at candidate's value */ if (h->do_histograms) h->histo_drop[sa[x]-128]++;#endif PRINT_IF_VERBOSE("\b."); } else { /* if (mpqs_isprime(itos(Qx))) */ mpqs_add_0(&relations_end); fprintf(LPREL, "%s @ %s :%s\n", i2str(Qx), i2str(Y), relations);#ifdef MPQS_USE_HISTOGRAMS /* bump LP relations counter at candidate's value */ if (h->do_histograms) h->histo_lprl[sa[x]-128]++;#endif#ifdef MPQS_DEBUG { pari_sp av1 = avma; GEN rhs = mpqs_factorback(h, relations); GEN Qx_2 = remii(sqri(Y), h->N); rhs = modii(mulii(rhs, Qx), h->N); if (!egalii(Qx_2, rhs)) { PRINT_IF_VERBOSE("\b(!)\n"); fprintferr("MPQS: %Z @ %Z :%s\n", Y, Qx, relations); fprintferr("\tQx_2 = %Z\n", Qx_2); fprintferr("\t rhs = %Z\n", rhs); err(talker, "MPQS: wrong large prime relation found!!"); } else PRINT_IF_VERBOSE("\b(;)"); avma = av1; }#endif }#ifdef MPQS_DEBUG_AVMA fprintferr("MPQS DEBUG: eval loop end, avma = 0x%lX\n", (ulong)avma);#endif } /* for */ PRINT_IF_VERBOSE("\n");#ifdef MPQS_DEBUG_AVMA fprintferr("MPQS DEBUG: leave eval cand, avma = 0x%lX\n", (ulong)avma);#endif return number_of_relations;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f2ea9bd32cba7fad43082b848508b3e81edfea5d/mpqs.c/clean/src/modules/mpqs.c |
idpowred(GEN x, GEN n) { return idealpowred(static_OBJ, x, n, 0); } | idpowred(void *nf, GEN x, GEN n) { return idealpowred((GEN) nf, x, n, 0); } | idpowred(GEN x, GEN n) { return idealpowred(static_OBJ, x, n, 0); } | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/d5847452a0a61f4b94c584ee5aed865e7709a077/polarit2.c/clean/src/basemath/polarit2.c |
int minor, const char *buf, | int minor, const char *buf, | MG5UART_STATIC int mg5uart_write_support_polled( int minor, const char *buf, int len){ int nwrite = 0; /* * poll each byte in the string out of the port. */ while (nwrite < len) { /* * transmit character */ mg5uart_write_polled(minor, *buf++); nwrite++; } /* * return the number of bytes written. */ return nwrite;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/2e7ed911d7a99274d5268498a3466de06f580d8a/mg5uart.c/buggy/c/src/lib/libcpu/mips/mongoosev/duart/mg5uart.c |
while (nwrite < len) { /* * transmit character */ | while (nwrite < len) { | MG5UART_STATIC int mg5uart_write_support_polled( int minor, const char *buf, int len){ int nwrite = 0; /* * poll each byte in the string out of the port. */ while (nwrite < len) { /* * transmit character */ mg5uart_write_polled(minor, *buf++); nwrite++; } /* * return the number of bytes written. */ return nwrite;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/2e7ed911d7a99274d5268498a3466de06f580d8a/mg5uart.c/buggy/c/src/lib/libcpu/mips/mongoosev/duart/mg5uart.c |
progname = *argv; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
|
status = EXIT_OK; | pppd_status = EXIT_OK; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
kill_link = 0; | pppd_kill_link = 0; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
status = EXIT_OPEN_FAILED; | pppd_status = EXIT_OPEN_FAILED; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
status = EXIT_INIT_FAILED; | pppd_status = EXIT_INIT_FAILED; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
if (kill_link) | if (pppd_kill_link) | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
status = EXIT_CONNECT_FAILED; | pppd_status = EXIT_CONNECT_FAILED; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
if (kill_link) | if (pppd_kill_link) | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
status = EXIT_OPEN_FAILED; | pppd_status = EXIT_OPEN_FAILED; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
if (!persist || errno != EINTR || hungup || kill_link) | if (!persist || errno != EINTR || hungup || pppd_kill_link) | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
status = EXIT_FATAL_ERROR; | pppd_status = EXIT_FATAL_ERROR; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
status = EXIT_NEGOTIATION_FAILED; | pppd_status = EXIT_NEGOTIATION_FAILED; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
while (phase != PHASE_DEAD) { | while (pppd_phase != PHASE_DEAD) { | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
if (kill_link) { | if (pppd_kill_link) { | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
kill_link = 0; | pppd_kill_link = 0; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { | if (pppd_phase == PHASE_NETWORK || pppd_phase == PHASE_RUNNING) { | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
kill_link = 0; | pppd_kill_link = 0; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
if (kill_link) { kill_link = 0; | if (pppd_kill_link) { pppd_kill_link = 0; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
} while (phase == PHASE_HOLDOFF); | } while (pppd_phase == PHASE_HOLDOFF); | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
die(status); return status; | die(pppd_status); return pppd_status; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
status = EXIT_HANGUP; | pppd_status = EXIT_HANGUP; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
if (phase <= PHASE_AUTHENTICATE | if (pppd_phase <= PHASE_AUTHENTICATE | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
protocol, phase)); | protocol, pppd_phase)); | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
phase = p; | pppd_phase = p; | static struct timeval *timeleft __P((struct timeval *));static void holdoff_end __P((void *));static int device_script __P((int, int, char *));extern char *ttyname __P((int));extern char *getlogin __P((void));int pppdmain __P((int, char *[]));/* * PPP Data Link Layer "protocol" table. * One entry per supported protocol. * The last entry must be NULL. */struct protent *protocols[] = { &lcp_protent, &pap_protent, &chap_protent,#ifdef CBCP_SUPPORT &cbcp_protent,#endif &ipcp_protent,#ifdef INET6 &ipv6cp_protent,#endif &ccp_protent,#ifdef IPX_CHANGE &ipxcp_protent,#endif#ifdef AT_CHANGE &atcp_protent,#endif NULL};intpppdmain(argc, argv) int argc; char *argv[];{ int i, fdflags, t; char *connector; struct timeval timo; struct protent *protp; new_phase(PHASE_INITIALIZE); script_env = NULL; hostname[MAXNAMELEN-1] = 0; privileged = 1; privileged_option = 1; /* * Initialize magic number generator now so that protocols may * use magic numbers in initialization. */ magic_init();#ifdef XXX_XXX /* moved code the the rtems_pppd_reset_options function */ /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0);#endif progname = *argv; if (!ppp_available()) { option_error(no_ppp_msg); return(EXIT_NO_KERNEL_SUPPORT); } /* * Check that the options given are valid and consistent. */ if (!sys_check_options()) { return(EXIT_OPTION_ERROR); } if (!auth_check_options()) { return(EXIT_OPTION_ERROR); } for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); /* default holdoff to 0 if no connect script has been given */ if (connect_script == 0 && !holdoff_specified) holdoff = 0; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff. */ sys_init(); /* if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); */ do_callback = 0; for (;;) { need_holdoff = 1; ttyfd = -1; real_ttyfd = -1; status = EXIT_OK; ++unsuccess; doing_callback = do_callback; do_callback = 0; new_phase(PHASE_SERIALCONN); /* * Get a pty master/slave pair if the pty, notty, or record * options were specified. */ strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); pty_master = -1; pty_slave = -1; /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ hungup = 0; kill_link = 0; connector = doing_callback? callback_script: connect_script; if (devnam[0] != 0) { for (;;) { /* If the user specified the device name, become the user before opening it. */ int err; ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0); err = errno; if (ttyfd >= 0) { break; } errno = err; if (err != EINTR) { error("Failed to open %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || err != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) warn("Couldn't reset non-blocking mode on device: %m"); /* * Set line speed, flow control, etc. * If we have a non-null connection or initializer script, * on most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we could clear CLOCAL at this point. */ set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0) || initializer != NULL)); real_ttyfd = ttyfd; } /* run connection script */ if ((connector && connector[0]) || initializer) { if (real_ttyfd != -1) { /* XXX do this if doing_callback == CALLBACK_DIALIN? */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* in case modem is off hook */ sleep(1); setdtr(real_ttyfd, 1); } } if (initializer && initializer[0]) { if (device_script(ttyfd, DIALER_INIT, initializer) < 0) { error("Initializer script failed"); status = EXIT_INIT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial port initialized."); } if (connector && connector[0]) { if (device_script(ttyfd, DIALER_CONNECT, connector) < 0) { error("Connect script failed"); status = EXIT_CONNECT_FAILED; goto fail; } if (kill_link) goto disconnect; info("Serial connection established."); } /* set line speed, flow control, etc.; clear CLOCAL if modem option */ if (real_ttyfd != -1) set_up_tty(real_ttyfd, 0); if (doing_callback == CALLBACK_DIALIN) connector = NULL; } /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem && devnam[0] != 0) { for (;;) { if ((i = open(devnam, O_RDWR)) >= 0) break; if (errno != EINTR) { error("Failed to reopen %s: %m", devnam); status = EXIT_OPEN_FAILED; } if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } info("Serial connection established."); sleep(1); /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(ttyfd, DIALER_WELCOME, welcomer) < 0) warn("Welcome script failed"); } /* set up the serial device as a ppp interface */ fd_ppp = establish_ppp(ttyfd); if (fd_ppp < 0) { status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand) { info("Using interface ppp%d", pppifunit); slprintf(ifname, sizeof(ifname), "ppp%d", pppifunit); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ notice("Connect: %s <--> %s", ifname, ppp_devnam); gettimeofday(&start_time, NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ open_ccp_flag = 0; status = EXIT_NEGOTIATION_FAILED; new_phase(PHASE_ESTABLISH); while (phase != PHASE_DEAD) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ disconnect: if (disconnect_script && !hungup) { new_phase(PHASE_DISCONNECT); if (real_ttyfd >= 0) set_up_tty(real_ttyfd, 1); if (device_script(ttyfd, DIALER_DISCONNECT, disconnect_script) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } fail: if (pty_master >= 0) close(pty_master); if (pty_slave >= 0) close(pty_slave); if (real_ttyfd >= 0) close_tty(); if (!persist || (maxfail > 0 && unsuccess >= maxfail)) break; kill_link = 0; if (demand) demand_discard(); t = need_holdoff? holdoff: 0; if (holdoff_hook) t = (*holdoff_hook)(); if (t > 0) { new_phase(PHASE_HOLDOFF); TIMEOUT(holdoff_end, NULL, t); do { wait_input(timeleft(&timo)); calltimeout(); if (kill_link) { kill_link = 0; new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ } } while (phase == PHASE_HOLDOFF); if (!persist) break; } } die(status); return status;}/* * holdoff_end - called via a timeout when the holdoff period ends. */static voidholdoff_end(arg) void *arg;{ new_phase(PHASE_DORMANT);}/* List of protocol names, to make our messages a little more informative. */struct protocol_list { u_short proto; const char *name;} protocol_list[] = { { 0x21, "IP" }, { 0x23, "OSI Network Layer" }, { 0x25, "Xerox NS IDP" }, { 0x27, "DECnet Phase IV" }, { 0x29, "Appletalk" }, { 0x2b, "Novell IPX" }, { 0x2d, "VJ compressed TCP/IP" }, { 0x2f, "VJ uncompressed TCP/IP" }, { 0x31, "Bridging PDU" }, { 0x33, "Stream Protocol ST-II" }, { 0x35, "Banyan Vines" }, { 0x39, "AppleTalk EDDP" }, { 0x3b, "AppleTalk SmartBuffered" }, { 0x3d, "Multi-Link" }, { 0x3f, "NETBIOS Framing" }, { 0x41, "Cisco Systems" }, { 0x43, "Ascom Timeplex" }, { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, { 0x47, "DCA Remote Lan" }, { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, { 0x4b, "SNA over 802.2" }, { 0x4d, "SNA" }, { 0x4f, "IP6 Header Compression" }, { 0x6f, "Stampede Bridging" }, { 0xfb, "single-link compression" }, { 0xfd, "1st choice compression" }, { 0x0201, "802.1d Hello Packets" }, { 0x0203, "IBM Source Routing BPDU" }, { 0x0205, "DEC LANBridge100 Spanning Tree" }, { 0x0231, "Luxcom" }, { 0x0233, "Sigma Network Systems" }, { 0x8021, "Internet Protocol Control Protocol" }, { 0x8023, "OSI Network Layer Control Protocol" }, { 0x8025, "Xerox NS IDP Control Protocol" }, { 0x8027, "DECnet Phase IV Control Protocol" }, { 0x8029, "Appletalk Control Protocol" }, { 0x802b, "Novell IPX Control Protocol" }, { 0x8031, "Bridging NCP" }, { 0x8033, "Stream Protocol Control Protocol" }, { 0x8035, "Banyan Vines Control Protocol" }, { 0x803d, "Multi-Link Control Protocol" }, { 0x803f, "NETBIOS Framing Control Protocol" }, { 0x8041, "Cisco Systems Control Protocol" }, { 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(void){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); return;}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup();}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); sys_close();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidppptimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemsmain.c/buggy/c/src/libnetworking/pppd/rtemsmain.c |
#endif | pari_sighandler(int sig){ char *msg; (void)os_signal(sig,pari_sighandler); switch(sig) {#ifdef SIGBREAK case SIGBREAK: pari_handle_SIGINT(); return;#endif#ifdef SIGINT case SIGINT: pari_handle_SIGINT(); return;#endif#ifdef SIGSEGV case SIGSEGV: msg="segmentation fault: bug in PARI or calling program"; break;#endif#ifdef SIGBUS case SIGBUS: msg="bus error: bug in PARI or calling program"; break;#endif#ifdef SIGFPE case SIGFPE: msg="floating point exception: bug in PARI or calling program"; break;#endif#ifdef SIGPIPE case SIGPIPE: msg="broken pipe"; break;#endif default: msg="unknown signal"; } err(talker,msg);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/953ca9aeb195d8eca3f431a684a1b202f3edcd87/init.c/buggy/src/language/init.c |
|
filltabs(Cache *C, Cache *Cp, Red *R, int p, int pk, long ltab) | filltabs(Cache *C, Cache *Cp, Red *R, long p, long pk, long ltab) | filltabs(Cache *C, Cache *Cp, Red *R, int p, int pk, long ltab){ pari_sp av; int i, j; long e; GEN tabt, taba, m; C->cyc = cyclo(pk,0); if (p > 2) { int LE = pk - pk/p + 1; GEN E = cgetg(LE, t_VECSMALL), eta = cgetg(pk+1,t_VEC); for (i=1,j=0; i<pk; i++) if (i%p) E[++j] = i; C->E = E; for (i=1; i<=pk; i++) { GEN z = FpX_rem(gpowgs(polx[0],i-1), C->cyc, R->N); eta[i] = (long)centermod_i(z, R->N, R->N2); } C->eta = eta; } else if (pk >= 8) { int LE = (pk>>2) + 1; GEN E = cgetg(LE, t_VECSMALL); for (i=1,j=0; i<pk; i++) if ((i%8)==1 || (i%8)==3) E[++j] = i; C->E = E; } if (pk > 2 && umodiu(R->N,pk) == 1) { GEN vpa, p1, p2, p3, a2 = NULL, a = finda(Cp, R->N, pk, p); int jj, ph = pk - pk/p; vpa = cgetg(ph+1,t_COL); vpa[1] = (long)a; if (pk > p) a2 = centermodii(sqri(a), R->N, R->N2); jj = 1; for (i=2; i<pk; i++) /* vpa = { a^i, (i,p) = 1 } */ if (i%p) { GEN z = mulii((i%p==1) ? a2 : a, (GEN)vpa[jj]); vpa[++jj] = (long)centermodii(z , R->N, R->N2); } if (!gcmp1( centermodii( mulii(a, (GEN)vpa[ph]), R->N, R->N2) )) return 0; p1 = cgetg(ph+1,t_MAT); p2 = cgetg(ph+1,t_COL); p1[1] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)gen_1; j = 2; p1[j] = (long)vpa; p3 = vpa; for (j++; j <= ph; j++) { p2 = cgetg(ph+1,t_COL); p1[j] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)centermodii(mulii((GEN)vpa[i],(GEN)p3[i]), R->N, R->N2); p3 = p2; } C->matvite = p1; C->matinvvite = FpM_inv(p1, R->N); } tabt = cgetg(ltab+1, t_VECSMALL); taba = cgetg(ltab+1, t_VECSMALL); av = avma; m = divis(R->N, pk); for (e=1; e<=ltab && signe(m); e++) { long s = vali(m); m = shifti(m,-s); tabt[e] = e==1? s: s + R->k; taba[e] = signe(m)? ((modBIL(m) & R->mask)+1)>>1: 0; m = shifti(m, -R->k); } setlg(taba, e); C->aall = taba; setlg(tabt, e); C->tall = tabt; avma = av; return 1;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/b03001ef86c74799b6280212f7a40aee0823ed89/aprcl.c/clean/src/modules/aprcl.c |
int i, j; | long i, j; | filltabs(Cache *C, Cache *Cp, Red *R, int p, int pk, long ltab){ pari_sp av; int i, j; long e; GEN tabt, taba, m; C->cyc = cyclo(pk,0); if (p > 2) { int LE = pk - pk/p + 1; GEN E = cgetg(LE, t_VECSMALL), eta = cgetg(pk+1,t_VEC); for (i=1,j=0; i<pk; i++) if (i%p) E[++j] = i; C->E = E; for (i=1; i<=pk; i++) { GEN z = FpX_rem(gpowgs(polx[0],i-1), C->cyc, R->N); eta[i] = (long)centermod_i(z, R->N, R->N2); } C->eta = eta; } else if (pk >= 8) { int LE = (pk>>2) + 1; GEN E = cgetg(LE, t_VECSMALL); for (i=1,j=0; i<pk; i++) if ((i%8)==1 || (i%8)==3) E[++j] = i; C->E = E; } if (pk > 2 && umodiu(R->N,pk) == 1) { GEN vpa, p1, p2, p3, a2 = NULL, a = finda(Cp, R->N, pk, p); int jj, ph = pk - pk/p; vpa = cgetg(ph+1,t_COL); vpa[1] = (long)a; if (pk > p) a2 = centermodii(sqri(a), R->N, R->N2); jj = 1; for (i=2; i<pk; i++) /* vpa = { a^i, (i,p) = 1 } */ if (i%p) { GEN z = mulii((i%p==1) ? a2 : a, (GEN)vpa[jj]); vpa[++jj] = (long)centermodii(z , R->N, R->N2); } if (!gcmp1( centermodii( mulii(a, (GEN)vpa[ph]), R->N, R->N2) )) return 0; p1 = cgetg(ph+1,t_MAT); p2 = cgetg(ph+1,t_COL); p1[1] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)gen_1; j = 2; p1[j] = (long)vpa; p3 = vpa; for (j++; j <= ph; j++) { p2 = cgetg(ph+1,t_COL); p1[j] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)centermodii(mulii((GEN)vpa[i],(GEN)p3[i]), R->N, R->N2); p3 = p2; } C->matvite = p1; C->matinvvite = FpM_inv(p1, R->N); } tabt = cgetg(ltab+1, t_VECSMALL); taba = cgetg(ltab+1, t_VECSMALL); av = avma; m = divis(R->N, pk); for (e=1; e<=ltab && signe(m); e++) { long s = vali(m); m = shifti(m,-s); tabt[e] = e==1? s: s + R->k; taba[e] = signe(m)? ((modBIL(m) & R->mask)+1)>>1: 0; m = shifti(m, -R->k); } setlg(taba, e); C->aall = taba; setlg(tabt, e); C->tall = tabt; avma = av; return 1;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/b03001ef86c74799b6280212f7a40aee0823ed89/aprcl.c/clean/src/modules/aprcl.c |
int LE = pk - pk/p + 1; | long LE = pk - pk/p + 1; | filltabs(Cache *C, Cache *Cp, Red *R, int p, int pk, long ltab){ pari_sp av; int i, j; long e; GEN tabt, taba, m; C->cyc = cyclo(pk,0); if (p > 2) { int LE = pk - pk/p + 1; GEN E = cgetg(LE, t_VECSMALL), eta = cgetg(pk+1,t_VEC); for (i=1,j=0; i<pk; i++) if (i%p) E[++j] = i; C->E = E; for (i=1; i<=pk; i++) { GEN z = FpX_rem(gpowgs(polx[0],i-1), C->cyc, R->N); eta[i] = (long)centermod_i(z, R->N, R->N2); } C->eta = eta; } else if (pk >= 8) { int LE = (pk>>2) + 1; GEN E = cgetg(LE, t_VECSMALL); for (i=1,j=0; i<pk; i++) if ((i%8)==1 || (i%8)==3) E[++j] = i; C->E = E; } if (pk > 2 && umodiu(R->N,pk) == 1) { GEN vpa, p1, p2, p3, a2 = NULL, a = finda(Cp, R->N, pk, p); int jj, ph = pk - pk/p; vpa = cgetg(ph+1,t_COL); vpa[1] = (long)a; if (pk > p) a2 = centermodii(sqri(a), R->N, R->N2); jj = 1; for (i=2; i<pk; i++) /* vpa = { a^i, (i,p) = 1 } */ if (i%p) { GEN z = mulii((i%p==1) ? a2 : a, (GEN)vpa[jj]); vpa[++jj] = (long)centermodii(z , R->N, R->N2); } if (!gcmp1( centermodii( mulii(a, (GEN)vpa[ph]), R->N, R->N2) )) return 0; p1 = cgetg(ph+1,t_MAT); p2 = cgetg(ph+1,t_COL); p1[1] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)gen_1; j = 2; p1[j] = (long)vpa; p3 = vpa; for (j++; j <= ph; j++) { p2 = cgetg(ph+1,t_COL); p1[j] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)centermodii(mulii((GEN)vpa[i],(GEN)p3[i]), R->N, R->N2); p3 = p2; } C->matvite = p1; C->matinvvite = FpM_inv(p1, R->N); } tabt = cgetg(ltab+1, t_VECSMALL); taba = cgetg(ltab+1, t_VECSMALL); av = avma; m = divis(R->N, pk); for (e=1; e<=ltab && signe(m); e++) { long s = vali(m); m = shifti(m,-s); tabt[e] = e==1? s: s + R->k; taba[e] = signe(m)? ((modBIL(m) & R->mask)+1)>>1: 0; m = shifti(m, -R->k); } setlg(taba, e); C->aall = taba; setlg(tabt, e); C->tall = tabt; avma = av; return 1;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/b03001ef86c74799b6280212f7a40aee0823ed89/aprcl.c/clean/src/modules/aprcl.c |
int LE = (pk>>2) + 1; | long LE = (pk>>2) + 1; | filltabs(Cache *C, Cache *Cp, Red *R, int p, int pk, long ltab){ pari_sp av; int i, j; long e; GEN tabt, taba, m; C->cyc = cyclo(pk,0); if (p > 2) { int LE = pk - pk/p + 1; GEN E = cgetg(LE, t_VECSMALL), eta = cgetg(pk+1,t_VEC); for (i=1,j=0; i<pk; i++) if (i%p) E[++j] = i; C->E = E; for (i=1; i<=pk; i++) { GEN z = FpX_rem(gpowgs(polx[0],i-1), C->cyc, R->N); eta[i] = (long)centermod_i(z, R->N, R->N2); } C->eta = eta; } else if (pk >= 8) { int LE = (pk>>2) + 1; GEN E = cgetg(LE, t_VECSMALL); for (i=1,j=0; i<pk; i++) if ((i%8)==1 || (i%8)==3) E[++j] = i; C->E = E; } if (pk > 2 && umodiu(R->N,pk) == 1) { GEN vpa, p1, p2, p3, a2 = NULL, a = finda(Cp, R->N, pk, p); int jj, ph = pk - pk/p; vpa = cgetg(ph+1,t_COL); vpa[1] = (long)a; if (pk > p) a2 = centermodii(sqri(a), R->N, R->N2); jj = 1; for (i=2; i<pk; i++) /* vpa = { a^i, (i,p) = 1 } */ if (i%p) { GEN z = mulii((i%p==1) ? a2 : a, (GEN)vpa[jj]); vpa[++jj] = (long)centermodii(z , R->N, R->N2); } if (!gcmp1( centermodii( mulii(a, (GEN)vpa[ph]), R->N, R->N2) )) return 0; p1 = cgetg(ph+1,t_MAT); p2 = cgetg(ph+1,t_COL); p1[1] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)gen_1; j = 2; p1[j] = (long)vpa; p3 = vpa; for (j++; j <= ph; j++) { p2 = cgetg(ph+1,t_COL); p1[j] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)centermodii(mulii((GEN)vpa[i],(GEN)p3[i]), R->N, R->N2); p3 = p2; } C->matvite = p1; C->matinvvite = FpM_inv(p1, R->N); } tabt = cgetg(ltab+1, t_VECSMALL); taba = cgetg(ltab+1, t_VECSMALL); av = avma; m = divis(R->N, pk); for (e=1; e<=ltab && signe(m); e++) { long s = vali(m); m = shifti(m,-s); tabt[e] = e==1? s: s + R->k; taba[e] = signe(m)? ((modBIL(m) & R->mask)+1)>>1: 0; m = shifti(m, -R->k); } setlg(taba, e); C->aall = taba; setlg(tabt, e); C->tall = tabt; avma = av; return 1;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/b03001ef86c74799b6280212f7a40aee0823ed89/aprcl.c/clean/src/modules/aprcl.c |
int jj, ph = pk - pk/p; | long jj, ph = pk - pk/p; | filltabs(Cache *C, Cache *Cp, Red *R, int p, int pk, long ltab){ pari_sp av; int i, j; long e; GEN tabt, taba, m; C->cyc = cyclo(pk,0); if (p > 2) { int LE = pk - pk/p + 1; GEN E = cgetg(LE, t_VECSMALL), eta = cgetg(pk+1,t_VEC); for (i=1,j=0; i<pk; i++) if (i%p) E[++j] = i; C->E = E; for (i=1; i<=pk; i++) { GEN z = FpX_rem(gpowgs(polx[0],i-1), C->cyc, R->N); eta[i] = (long)centermod_i(z, R->N, R->N2); } C->eta = eta; } else if (pk >= 8) { int LE = (pk>>2) + 1; GEN E = cgetg(LE, t_VECSMALL); for (i=1,j=0; i<pk; i++) if ((i%8)==1 || (i%8)==3) E[++j] = i; C->E = E; } if (pk > 2 && umodiu(R->N,pk) == 1) { GEN vpa, p1, p2, p3, a2 = NULL, a = finda(Cp, R->N, pk, p); int jj, ph = pk - pk/p; vpa = cgetg(ph+1,t_COL); vpa[1] = (long)a; if (pk > p) a2 = centermodii(sqri(a), R->N, R->N2); jj = 1; for (i=2; i<pk; i++) /* vpa = { a^i, (i,p) = 1 } */ if (i%p) { GEN z = mulii((i%p==1) ? a2 : a, (GEN)vpa[jj]); vpa[++jj] = (long)centermodii(z , R->N, R->N2); } if (!gcmp1( centermodii( mulii(a, (GEN)vpa[ph]), R->N, R->N2) )) return 0; p1 = cgetg(ph+1,t_MAT); p2 = cgetg(ph+1,t_COL); p1[1] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)gen_1; j = 2; p1[j] = (long)vpa; p3 = vpa; for (j++; j <= ph; j++) { p2 = cgetg(ph+1,t_COL); p1[j] = (long)p2; for (i=1; i<=ph; i++) p2[i] = (long)centermodii(mulii((GEN)vpa[i],(GEN)p3[i]), R->N, R->N2); p3 = p2; } C->matvite = p1; C->matinvvite = FpM_inv(p1, R->N); } tabt = cgetg(ltab+1, t_VECSMALL); taba = cgetg(ltab+1, t_VECSMALL); av = avma; m = divis(R->N, pk); for (e=1; e<=ltab && signe(m); e++) { long s = vali(m); m = shifti(m,-s); tabt[e] = e==1? s: s + R->k; taba[e] = signe(m)? ((modBIL(m) & R->mask)+1)>>1: 0; m = shifti(m, -R->k); } setlg(taba, e); C->aall = taba; setlg(tabt, e); C->tall = tabt; avma = av; return 1;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/b03001ef86c74799b6280212f7a40aee0823ed89/aprcl.c/clean/src/modules/aprcl.c |
rtems_unsigned16 index; | uint16_t index; | rtems_isr external_exception_ISR ( rtems_vector_number vector /* IN */){ rtems_unsigned16 index; rtems_boolean is_active=FALSE; rtems_unsigned32 scv64_status; Chain_Node *node; EE_ISR_Type *ee_isr; /* * Get all active interrupts. */ scv64_status = SCV64_Get_Interrupt(); scv64_status &= SCV64_Get_Interrupt_Enable(); /* * Process any set interrupts. */ for (index = 0; index <= 5; index++) { switch(index) { case 0: is_active = SCV64_Is_IRQ0( scv64_status ); break; case 1: is_active = SCV64_Is_IRQ1( scv64_status ); break; case 2: is_active = SCV64_Is_IRQ2( scv64_status ); break; case 3: is_active = SCV64_Is_IRQ3( scv64_status ); break; case 4: is_active = SCV64_Is_IRQ4( scv64_status ); break; case 5: is_active = SCV64_Is_IRQ5( scv64_status ); break; } if (is_active) { /* * Read vector. */ node = ISR_Array[ index ].first; while ( !_Chain_Is_tail( &ISR_Array[ index ], node ) ) { ee_isr = (EE_ISR_Type *) node; (*ee_isr->handler)( ee_isr->vector ); node = node->next; } } }} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/ed9e44994265d3dccca845c7aade2f634184c339/genpvec.c/buggy/c/src/lib/libbsp/powerpc/dmv177/startup/genpvec.c |
rtems_unsigned32 scv64_status; | uint32_t scv64_status; | rtems_isr external_exception_ISR ( rtems_vector_number vector /* IN */){ rtems_unsigned16 index; rtems_boolean is_active=FALSE; rtems_unsigned32 scv64_status; Chain_Node *node; EE_ISR_Type *ee_isr; /* * Get all active interrupts. */ scv64_status = SCV64_Get_Interrupt(); scv64_status &= SCV64_Get_Interrupt_Enable(); /* * Process any set interrupts. */ for (index = 0; index <= 5; index++) { switch(index) { case 0: is_active = SCV64_Is_IRQ0( scv64_status ); break; case 1: is_active = SCV64_Is_IRQ1( scv64_status ); break; case 2: is_active = SCV64_Is_IRQ2( scv64_status ); break; case 3: is_active = SCV64_Is_IRQ3( scv64_status ); break; case 4: is_active = SCV64_Is_IRQ4( scv64_status ); break; case 5: is_active = SCV64_Is_IRQ5( scv64_status ); break; } if (is_active) { /* * Read vector. */ node = ISR_Array[ index ].first; while ( !_Chain_Is_tail( &ISR_Array[ index ], node ) ) { ee_isr = (EE_ISR_Type *) node; (*ee_isr->handler)( ee_isr->vector ); node = node->next; } } }} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/ed9e44994265d3dccca845c7aade2f634184c339/genpvec.c/buggy/c/src/lib/libbsp/powerpc/dmv177/startup/genpvec.c |
errno = ENOSYS; return -1; | rtems_libio_t *iop; /* * Get the file control block structure associated with the file descriptor */ iop = rtems_libio_iop( dirp->dd_fd ); if (iop == NULL) assert(0); return (long)( iop->offset ); | long telldir( DIR *dirp){ errno = ENOSYS; return -1;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/07a3253de2c3f9bc2d96a351680ec72548dadd2d/telldir.c/clean/cpukit/libcsupport/src/telldir.c |
long size = 512, n = size; char *buf = gpmalloc(n), *s = buf; | Buffer *b = new_buffer(); input_method IM; filtre_t F; GEN x; | readGEN(FILE *fi){ long size = 512, n = size; char *buf = gpmalloc(n), *s = buf; while (fgets(s, n, fi)) { if (s[strlen(s)-1] == '\n') { GEN x = freadexpr(buf); free(buf); return x; } buf = gprealloc(buf, size<<1); s = buf + (size-1); n = size+1; size <<= 1; }#if defined(UNIX) || defined(__EMX__) if (!feof(fi))#endif err(talker, "failed read from file"); return NULL;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f7d452c12ec91042db2a51f8cac89be08be1fcaa/es.c/clean/src/language/es.c |
while (fgets(s, n, fi)) { if (s[strlen(s)-1] == '\n') { GEN x = freadexpr(buf); free(buf); return x; } buf = gprealloc(buf, size<<1); s = buf + (size-1); n = size+1; size <<= 1; } #if defined(UNIX) || defined(__EMX__) if (!feof(fi)) #endif err(talker, "failed read from file"); return NULL; | init_filtre(&F, b); IM.file = fi; IM.fgets= &fgets; IM.prompt = NULL; IM.getline= &file_input; IM.free = 0; (void)input_loop(&F,&IM); x = readseq(b->buf); delete_buffer(b); return x; | readGEN(FILE *fi){ long size = 512, n = size; char *buf = gpmalloc(n), *s = buf; while (fgets(s, n, fi)) { if (s[strlen(s)-1] == '\n') { GEN x = freadexpr(buf); free(buf); return x; } buf = gprealloc(buf, size<<1); s = buf + (size-1); n = size+1; size <<= 1; }#if defined(UNIX) || defined(__EMX__) if (!feof(fi))#endif err(talker, "failed read from file"); return NULL;} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f7d452c12ec91042db2a51f8cac89be08be1fcaa/es.c/clean/src/language/es.c |
kill_link = 1; | pppd_kill_link = 1; | static void timeout_terminate(void *arg){ /* set pppd global variables to disconnect */ persist = 0; kill_link = 1;} | 10355 /local/tlutelli/issta_data/temp/c/2005_temp/2005/10355/48cdb95aa2444a386a911be1a8251483d79a9935/rtemspppd.c/clean/cpukit/pppd/rtemspppd.c |
newcrossing(spantab); if(y==spantab->lastlasty) | if(y!=spantab->lastlasty) | static void adddot(struct spantab *spantab, int x, int y){ if(y<0 || y>=MAXLINES) return; if(y<spantab->miny) spantab->miny=y; if(y>spantab->maxy) spantab->maxy=y; if(y==spantab->lasty) { spantab->xout=x; } else { if(spantab->secondy<0) { spantab->secondy=y; if(spantab->secondy > spantab->firsty) --spantab->firsty; } if(spantab->lasty>=0) { newcrossing(spantab); if(y==spantab->lastlasty) newcrossing(spantab); } spantab->lastlasty=spantab->lasty; spantab->lasty=y; spantab->xin=spantab->xout=x; }} | 1561 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1561/de267a11b57ddc24ab28ce52839e112700620daa/render.c/clean/render.c |
else { int t; t=spantab->xout; spantab->xout = spantab->xin; newcrossing(spantab); spantab->xin = spantab->xout = t; newcrossing(spantab); } | static void adddot(struct spantab *spantab, int x, int y){ if(y<0 || y>=MAXLINES) return; if(y<spantab->miny) spantab->miny=y; if(y>spantab->maxy) spantab->maxy=y; if(y==spantab->lasty) { spantab->xout=x; } else { if(spantab->secondy<0) { spantab->secondy=y; if(spantab->secondy > spantab->firsty) --spantab->firsty; } if(spantab->lasty>=0) { newcrossing(spantab); if(y==spantab->lastlasty) newcrossing(spantab); } spantab->lastlasty=spantab->lasty; spantab->lasty=y; spantab->xin=spantab->xout=x; }} | 1561 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1561/de267a11b57ddc24ab28ce52839e112700620daa/render.c/clean/render.c |
|
GEN __x; | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
|
else f = powsell(cp4,f,B,p); /* F */ | else f = powsell(cp4,f,B,p); /* F = B.f */ | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
{ /* we're nearly done: naive method */ | { /* we're nearly done: naive search */ GEN q1 = p1, mf = negsell(f); /* -F */ | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } | p1 = addsell(cp4,p1, f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi( i,B)); goto FOUND; } q1 = addsell(cp4,q1,mf,p); /* h.f - i.F */ if (!q1) { h = addii(h, mulsi(-i,B)); goto FOUND; } | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
_fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); | _fix(p1+1, j); tx[i] = modBIL((GEN)p1[1]); _fix(p1+2, j); ty[i] = modBIL((GEN)p1[2]); | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ | mfh = negsell(fh); fg = addsell(cp4,p1,mfh,p); /* h.f + nb.F - h.f = nb.F */ | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; | if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg == p1 */ h = addii(h, mulsi(k,B)); goto FOUND; | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); | tx[i] = modBIL((GEN)p1[1]); ty[i] = modBIL((GEN)p1[2]); | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ | p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = (s-1).F */ if (!p1) { h = mulsi(s-1,B); goto FOUND; } | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
/* giant steps: fg = f^s */ | /* giant steps: fg = s.F */ | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; | if (!fg) { h = mulsi(s,B); goto FOUND; } pfinal = modBIL(p); av2 = avma; | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); | for (j=2; j<=nb; j++) /* pts[j] = j.fg = (s*j).F */ { p1 = addsell(cp4,(GEN)pts[j-1],fg,p); if (!p1) { h = mulii(mulss(s,j), B); goto FOUND; } gaffect(p1, (GEN)pts[j]); } | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
k = _low((GEN)ftest[1]); | k = modBIL((GEN)ftest[1]); | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |
k2 = _low((GEN)ftest[2]); | k2 = modBIL((GEN)ftest[2]); | apell1(GEN e, GEN p){ long *tx, *ty, *ti, pfinal, i, j, j2, s, k, k1, x, nb; gpmem_t av = avma, av2; GEN p1,p2,p3,h,mfh,f,fh,fg,pordmin,u,v,p1p,p2p,A,B,c4,c6,cp4,pts; GEN __x; tx = ty = ti = NULL; /* gcc -Wall */ if (DEBUGLEVEL) (void)timer2(); c4 = gmod(gdivgs((GEN)e[10], -48), p); c6 = gmod(gdivgs((GEN)e[11], -864), p); pordmin = gceil(gmul2n(gsqrt(p,DEFAULTPREC),2)); /* ceil( sqrt(4p) ) */ p1p = addsi(1,p); p2p = shifti(p1p,1); x = 0; u = c6; k1 = 0; k = kronecker(c6, p); A = gzero; B = gun; h = p1p; for(;;) { while (k == k1 || !k) { x++; u = modii(addii(c6, mulsi(x, addii(c4, mulss(x,x)))), p); k = kronecker(u, p); } k1 = k; f = cgetg(3,t_VEC); f[1] = lmodii(mulsi(x,u), p); f[2] = lmodii(sqri(u), p); cp4 = modii(mulii(c4, (GEN)f[2]), p); fh = powsell(cp4,f,h,p); s = itos(gceil(gsqrt(gdiv(pordmin,B),DEFAULTPREC))) >> 1; /* look for h s.t f^h = 0 */ if (B == gun) { /* first time: initialize */ tx = newbloc(s+1); ty = newbloc(s+1); ti = newbloc(s+1); } else f = powsell(cp4,f,B,p); /* F */ *tx = evaltyp(t_VECSMALL) | evallg(s+1); if (!fh) goto FOUND; p1 = gcopy(fh); if (s < 3) { /* we're nearly done: naive method */ for (i=1;; i++) { p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } } /* Baby Step/Giant Step */ nb = min(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */ pts = cgetg(nb+1, t_VEC); j = lgefint(p); for (i=1; i<=nb; i++) { /* baby steps */ pts[i] = (long)p1; /* h.f + (i-1).F */ _fix(p1+1, j); tx[i] = _low((GEN)p1[1]); _fix(p1+2, j); ty[i] = _low((GEN)p1[2]); p1 = addsell(cp4,p1,f,p); /* h.f + i.F */ if (!p1) { h = addii(h, mulsi(i,B)); goto FOUND; } } mfh = dummycopy(fh); mfh[2] = lnegi((GEN)mfh[2]); fg = addsell(cp4,p1,mfh,p); /* nb.F */ if (!fg) { h = mulsi(nb,B); goto FOUND; } u = cgetg(nb+1, t_VEC); av2 = avma; /* more baby steps, nb points at a time */ while (i <= s) { long maxj; for (j=1; j<=nb; j++) /* adding nb.F (part 1) */ { p1 = (GEN)pts[j]; /* h.f + (i-nb-1+j-1).F */ u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* sum = 0 or doubling */ { long k = i+j-2; if (egalii((GEN)p1[2],(GEN)fg[2])) k -= 2*nb; /* fg = p1 */ h = addii(h, mulsi(k,B)); goto FOUND; } } v = multi_invmod(u, p); maxj = (i-1 + nb <= s)? nb: s % nb; for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */ { p1 = (GEN)pts[j]; addsell_part2(cp4,p1,fg,p, (GEN)v[j]); tx[i] = _low((GEN)p1[1]); ty[i] = _low((GEN)p1[2]); } avma = av2; } p1 = addsell(cp4,(GEN)pts[j-1],mfh,p); /* = f^(s-1) */ if (DEBUGLEVEL) msgtimer("[apell1] baby steps, s = %ld",s); /* giant steps: fg = f^s */ fg = addsell(cp4,p1,f,p); if (!fg) { h = addii(h, mulsi(s,B)); goto FOUND; } pfinal = _low(p); av2 = avma; p1 = gen_sort(tx, cmp_IND | cmp_C, NULL); for (i=1; i<=s; i++) ti[i] = tx[p1[i]]; for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; } for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; } if (DEBUGLEVEL) msgtimer("[apell1] sorting"); avma = av2; gaffect(fg, (GEN)pts[1]); for (j=2; j<=nb; j++) /* pts = first nb multiples of fg */ gaffect(addsell(cp4,(GEN)pts[j-1],fg,p), (GEN)pts[j]); /* replace fg by nb.fg since we do nb points at a time */ avma = av2; fg = gcopy((GEN)pts[nb]); av2 = avma; for (i=1,j=1; ; i++) { GEN ftest = (GEN)pts[j]; ulong m, l = 1, r = s+1; long k, k2; avma = av2; k = _low((GEN)ftest[1]); while (l<r) { m = (l+r) >> 1; if (tx[m] < k) l = m+1; else r = m; } if (r <= (ulong)s && tx[r] == k) { while (tx[r] == k && r) r--; k2 = _low((GEN)ftest[2]); for (r++; tx[r] == k && r <= (ulong)s; r++) if (ty[r] == k2 || ty[r] == pfinal - k2) { /* [h+j2] f == ftest (= [i.s] f)? */ if (DEBUGLEVEL) msgtimer("[apell1] giant steps, i = %ld",i); j2 = ti[r] - 1; p1 = addsell(cp4, powsell(cp4,f,stoi(j2),p),fh,p); if (egalii((GEN)p1[1], (GEN)ftest[1])) { if (egalii((GEN)p1[2], (GEN)ftest[2])) i = -i; h = addii(h, mulii(addis(mulss(s,i), j2), B)); goto FOUND; } } } if (++j > nb) { /* compute next nb points */ long save = 0; /* gcc -Wall */; for (j=1; j<=nb; j++) { p1 = (GEN)pts[j]; u[j] = lsubii((GEN)fg[1], (GEN)p1[1]); if (u[j] == zero) /* occurs once: i = j = nb, p1 == fg */ { u[j] = lshifti((GEN)p1[2],1); save = fg[1]; fg[1] = p1[1]; } } v = multi_invmod(u, p); for (j=1; j<=nb; j++) addsell_part2(cp4, (GEN)pts[j],fg,p, (GEN)v[j]); if (i == nb) { fg[1] = save; } j = 1; } }FOUND: /* found a point of exponent h */ p2 = decomp(h); p1=(GEN)p2[1]; p2=(GEN)p2[2]; for (i=1; i<lg(p1); i++) for (j=itos((GEN)p2[i]); j; j--) { p3 = divii(h,(GEN)p1[i]); if (powsell(cp4,f,p3,p)) break; h = p3; } /* now h is the exact order, and divides E(Fp) = A (mod B) */ if (B == gun) B = h; else { p1 = chinois(gmodulcp(A,B), gmodulsg(0,h)); A = (GEN)p1[2]; B = (GEN)p1[1]; } i = (cmpii(B, pordmin) < 0); if (i) A = centermod(subii(p2p,A), B); p1 = diviiround(gsub(p1p,A), B); h = addii(A, mulii(p1,B)); /* h = A mod B, closest lift to p+1 */ if (!i) break; } gunclone(tx); gunclone(ty); gunclone(ti); p1 = (k==1)? subii(p1p,h): subii(h,p1p); return gerepileuptoint(av,p1);} | 2195 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2195/f85c7b0c1ca7393252f54eb29f56f6b4d77a1c77/elliptic.c/clean/src/modules/elliptic.c |