示例#1
0
int
tr_blocklistFileSetContent (tr_blocklistFile * b, const char * filename)
{
  tr_sys_file_t in;
  tr_sys_file_t out;
  int inCount = 0;
  char line[2048];
  const char * err_fmt = _("Couldn't read \"%1$s\": %2$s");
  struct tr_ipv4_range * ranges = NULL;
  size_t ranges_alloc = 0;
  size_t ranges_count = 0;
  tr_error * error = NULL;

  if (!filename)
    {
      blocklistDelete (b);
      return 0;
    }

  in = tr_sys_file_open (filename, TR_SYS_FILE_READ, 0, &error);
  if (in == TR_BAD_SYS_FILE)
    {
      tr_logAddError (err_fmt, filename, error->message);
      tr_error_free (error);
      return 0;
    }

  blocklistClose (b);

  out = tr_sys_file_open (b->filename,
                          TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE,
                          0666, &error);
  if (out == TR_BAD_SYS_FILE)
    {
      tr_logAddError (err_fmt, b->filename, error->message);
      tr_error_free (error);
      tr_sys_file_close (in, NULL);
      return 0;
    }

  /* load the rules into memory */
  while (tr_sys_file_read_line (in, line, sizeof (line), NULL))
    {
      struct tr_ipv4_range range;

      ++inCount;

      if (!parseLine (line, &range))
        {
          /* don't try to display the actual lines - it causes issues */
          tr_logAddError (_("blocklist skipped invalid address at line %d"), inCount);
          continue;
        }

      if (ranges_alloc == ranges_count)
        {
          ranges_alloc += 4096; /* arbitrary */
          ranges = tr_renew (struct tr_ipv4_range, ranges, ranges_alloc);
        }

      ranges[ranges_count++] = range;
    }
示例#2
0
int
tr_blocklistFileSetContent (tr_blocklistFile * b, const char * filename)
{
  FILE * in;
  FILE * out;
  int inCount = 0;
  char line[2048];
  const char * err_fmt = _("Couldn't read \"%1$s\": %2$s");
  struct tr_ipv4_range * ranges = NULL;
  size_t ranges_alloc = 0;
  size_t ranges_count = 0;

  if (!filename)
    {
      blocklistDelete (b);
      return 0;
    }

  in = fopen (filename, "rb");
  if (in == NULL)
    {
      tr_logAddError (err_fmt, filename, tr_strerror (errno));
      return 0;
    }

  blocklistClose (b);

  out = fopen (b->filename, "wb+");
  if (out == NULL)
    {
      tr_logAddError (err_fmt, b->filename, tr_strerror (errno));
      fclose (in);
      return 0;
    }

  /* load the rules into memory */
  while (fgets (line, sizeof (line), in) != NULL)
    {
      char * walk;
      struct tr_ipv4_range range;

      ++inCount;

      /* zap the linefeed */
      if ((walk = strchr (line, '\r'))) *walk = '\0';
      if ((walk = strchr (line, '\n'))) *walk = '\0';

      if (!parseLine (line, &range))
        {
          /* don't try to display the actual lines - it causes issues */
          tr_logAddError (_("blocklist skipped invalid address at line %d"), inCount);
          continue;
        }

      if (ranges_alloc == ranges_count)
        {
          ranges_alloc += 4096; /* arbitrary */
          ranges = tr_renew (struct tr_ipv4_range, ranges, ranges_alloc);
        }

      ranges[ranges_count++] = range;
    }
示例#3
0
int
_tr_blocklistSetContent( tr_blocklist * b,
                         const char *   filename )
{
    FILE * in;
    FILE * out;
    int inCount = 0;
    int outCount = 0;
    char line[2048];
    const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );

    if( !filename )
    {
        blocklistDelete( b );
        return 0;
    }

    in = fopen( filename, "rb" );
    if( !in )
    {
        tr_err( err_fmt, filename, tr_strerror( errno ) );
        return 0;
    }

    blocklistClose( b );

    out = fopen( b->filename, "wb+" );
    if( !out )
    {
        tr_err( err_fmt, b->filename, tr_strerror( errno ) );
        fclose( in );
        return 0;
    }

    while( fgets( line, sizeof( line ), in ) != NULL )
    {
        char * walk;
        struct tr_ip_range range;

        ++inCount;

        /* zap the linefeed */
        if(( walk = strchr( line, '\r' ))) *walk = '\0';
        if(( walk = strchr( line, '\n' ))) *walk = '\0';

        if( !parseLine( line, &range ) )
        {
            /* don't try to display the actual lines - it causes issues */
            tr_err( _( "blocklist skipped invalid address at line %d" ), inCount );
            continue;
        }

        if( fwrite( &range, sizeof( struct tr_ip_range ), 1, out ) != 1 )
        {
            tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), b->filename,
                   tr_strerror( errno ) );
            break;
        }

        ++outCount;
    }

    {
        char * base = tr_basename( b->filename );
        tr_inf( _( "Blocklist \"%s\" updated with %d entries" ), base, outCount );
        tr_free( base );
    }

    fclose( out );
    fclose( in );

    blocklistLoad( b );

    return outCount;
}
示例#4
0
int
_tr_blocklistSetContent( tr_blocklist * b,
                         const char *   filename )
{
    FILE *       in;
    FILE *       out;
    char *       line;
    int          lineCount = 0;
    const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );

    if( !filename )
    {
        blocklistDelete( b );
        return 0;
    }

    in = fopen( filename, "r" );
    if( !in )
    {
        tr_err( err_fmt, filename, tr_strerror( errno ) );
        return 0;
    }

    blocklistClose( b );

    out = fopen( b->filename, "wb+" );
    if( !out )
    {
        tr_err( err_fmt, b->filename, tr_strerror( errno ) );
        fclose( in );
        return 0;
    }

    while( !fggets( &line, in ) )
    {
        char * rangeBegin;
        char * rangeEnd;
        char * crpos;
        tr_address  addr;
        struct tr_ip_range range;

        rangeBegin = strrchr( line, ':' );
        if( !rangeBegin ) {
            free( line );
            continue;
        }
        ++rangeBegin;

        rangeEnd = strchr( rangeBegin, '-' );
        if( !rangeEnd ) {
            free( line );
            continue;
        }
        *rangeEnd++ = '\0';
        if(( crpos = strchr( rangeEnd, '\r' )))
            *crpos = '\0';

        if( !tr_pton( rangeBegin, &addr ) )
            tr_err( "blocklist skipped invalid address [%s]\n", rangeBegin );
        range.begin = ntohl( addr.addr.addr4.s_addr );

        if( !tr_pton( rangeEnd, &addr ) )
            tr_err( "blocklist skipped invalid address [%s]\n", rangeEnd );
        range.end = ntohl( addr.addr.addr4.s_addr );

        free( line );

        if( fwrite( &range, sizeof( struct tr_ip_range ), 1, out ) != 1 )
        {
            tr_err( _(
                        "Couldn't save file \"%1$s\": %2$s" ), b->filename,
                    tr_strerror( errno ) );
            break;
        }

        ++lineCount;
    }

    {
        char * base = tr_basename( b->filename );
        tr_inf( _( "Blocklist \"%1$s\" updated with %2$'d entries" ), base, lineCount );
        tr_free( base );
    }


    fclose( out );
    fclose( in );

    blocklistLoad( b );

    return lineCount;
}