rem
stringlengths
0
274k
add
stringlengths
0
169k
context
stringlengths
9
471k
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;}
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;}
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));}
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);}
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;}
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;}
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;}
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));}
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));}
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));}
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));}
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;}
}
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;}
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 );}
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();}
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;}
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();}
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;}
len = strlen(src) + 1;
len = src ? strlen(src) + 1 : 0;
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;}
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);}
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;}
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;}
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;}
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;}
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;}
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;}
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;}
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;}
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 */}
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;}
printf( "gettimeofday: %s", ctime( &tv.tv_sec ) );
a_time_t = tv.tv_sec; 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 );}
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;}
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;}
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;}
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); }
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;}
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;}
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;}
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;}
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;}
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;}
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;}

No dataset card yet

Downloads last month
22

Collection including h4iku/coconut_c2005_preprocessed