cf_void WritePid()
    {
        char p [MAX_SIZE];
        GetPathByPathNameExt(_filePathName.c_str(), _filePathName.size() , p);
        if(0==IsFileExist(p))
        {
            if(-1==mkdir(p, S_IRWXU | S_IRWXG | S_IROTH))
            {
                _THROW(SyscallExecuteError, "Failed to execute mkdir !");
            }
        }

        _fd =cf_open(_filePathName.c_str(),O_RDWR|O_CREAT|O_TRUNC,
                     S_IRWXU | S_IRWXG | S_IROTH);
        if(-1==_fd)
        {
            _THROW(SyscallExecuteError, "Failed to execute cf_open !");
        }
        char buf[8] = {0};
        int n =snprintf(buf,sizeof(buf),"%u",unsigned(getpid()));
        ssize_t nw =cf_write(_fd,buf,n);
        if(nw!=n)
        {
            _THROW_FMT(ValueError, "nw{%d}!=n{%d} !",int(nw),int(n));
        }
    }
Beispiel #2
0
MemMappedFile::MemMappedFile(cf_const std::string & name, size_t size,
                             cf_int prot, bool lock,bool autoUnmap)
    :_pShm(NULL),
     _size(size),
     _isLocked(lock),
     _autoUnmap(autoUnmap)
{
    if (size<=0)
        _THROW(ValueError, "size !");

    cf_int fd =cf_open(name.c_str(), ipcdefs::FLAG_CREATE, ipcdefs::MODE_DEFAULT);
    if(0!=fd)
        _THROW(SyscallExecuteError, "Failed to execute cf_open !");

    struct stat st_buf = {0};
    if(0!=cf_fstat(fd,&st_buf))
        _THROW(SyscallExecuteError, "Failed to execute cf_fstat !");

    if (st_buf.st_size < _size)
    {
        if(0!=cf_ftruncate(fd,size))
            _THROW(SyscallExecuteError, "Failed to execute cf_ftruncate !");
    }

    if (size == 0)
        _size = st_buf.st_size;
    _pShm = Mmap(fd, prot);
    if (lock && 0!=cf_mlock(_pShm, _size))
        _THROW(SyscallExecuteError, "Failed to execute cf_mlock !");
}
cf_void SetProcessDaemon()
{
    cf_int i,fd0,fd1,fd2;
    pid_t pid;
    struct rlimit rl;

    cf_umask(0);
    if(cf_getrlimit(RLIMIT_NOFILE,&rl)<0)
    {
        printf("Can't get file limit! errno=%d.file:%s,function:%s,line:%d.\n",
               cf_int(errno),__FILE__,__PRETTY_FUNCTION__,__LINE__);
        cf_exit(1);
    }
    if ((pid=cf_fork())<0)
    {
        printf("Can't fork! errno=%d.file:%s,function:%s,line:%d.\n",cf_int(errno),
               __FILE__,__PRETTY_FUNCTION__,__LINE__);
        cf_exit(1);
    }
    else if(pid != 0) // parent will exit normally.
    {
        cf_exit(0);
    }

    cf_setsid();

    if ((pid=cf_fork())<0)
    {
        printf("Can't fork!! errno=%d.file:%s,function:%s,line:%d.\n",cf_int(errno),
               __FILE__,__PRETTY_FUNCTION__,__LINE__);
        exit(1);
    }
    else if (pid!=0)
    {
        cf_exit(0);
    }

    if (cf_chdir("/tmp")<0)
    {
        printf("Can't change directory to /tmp! errno=%d.file:%s,function:%s,line:%d.\n",
               cf_int(errno),__FILE__,__PRETTY_FUNCTION__,__LINE__);
        cf_exit(1);
    }

    if (rl.rlim_max == RLIM_INFINITY)
        rl.rlim_max =1024;
    for (i=0; i < cf_int(rl.rlim_max); i++)
        cf_close(i);

    fd0 =cf_open("/dev/null",O_RDWR,0);
    fd1 =cf_dup(0);
    fd2 =cf_dup(0);
}
static void
exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
{
    int   import_file_fd;
    char *tmpname, *capfile_name, *comment;
    int   err;

    /* Choose a random name for the temporary import buffer */
    import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_", NULL);
    capfile_name = g_strdup(tmpname);

    comment = g_strdup_printf("Dump of PDUs from %s", cfile.filename);
    err = exp_pdu_open(exp_pdu_tap_data, import_file_fd, comment);
    if (err != 0) {
        g_free(comment);
        cfile_dump_open_failure_alert_box(capfile_name ? capfile_name : "temporary file",
                                          err, WTAP_FILE_TYPE_SUBTYPE_PCAPNG);
        goto end;
    }

    /* Run the tap */
    cf_retap_packets(&cfile);

    err = exp_pdu_close(exp_pdu_tap_data);
    if (err!= 0) {
        cfile_close_failure_alert_box(capfile_name, err);
    }

    /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
    if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
        /* cf_open() has put up a dialog box for the error */
        goto end;
    }

    switch (cf_read(&cfile, FALSE)) {
    case CF_READ_OK:
    case CF_READ_ERROR:
        /* Just because we got an error, that doesn't mean we were unable
        to read any of the file; we handle what we could get from the
        file. */
        break;

    case CF_READ_ABORTED:
        /* The user bailed out of re-reading the capture file; the
        capture file has been closed - just free the capture file name
        string and return (without changing the last containing
        directory). */
        break;
    }

end:
    g_free(capfile_name);
}
Beispiel #5
0
static void
dnd_merge_files(int in_file_count, char **in_filenames)
{
    char *tmpname;
    cf_status_t merge_status;
    int err;

    /* merge the files in chonological order */
    tmpname = NULL;
    merge_status = cf_merge_files(&tmpname, in_file_count, in_filenames,
                              WTAP_FILE_PCAP, FALSE);

    if (merge_status != CF_OK) {
        /* merge failed */
        g_free(tmpname);
        return;
    }

    cf_close(&cfile);

    /* Try to open the merged capture file. */
    if (cf_open(&cfile, tmpname, TRUE /* temporary file */, &err) != CF_OK) {
        /* We couldn't open it; don't dismiss the open dialog box,
           just leave it around so that the user can, after they
           dismiss the alert box popped up for the open error,
           try again. */
        g_free(tmpname);
        return;
    }
    g_free(tmpname);

    switch (cf_read(&cfile, FALSE)) {

    case CF_READ_OK:
    case CF_READ_ERROR:
        /* Just because we got an error, that doesn't mean we were unable
           to read any of the file; we handle what we could get from the
           file. */
        break;

    case CF_READ_ABORTED:
        /* The user bailed out of re-reading the capture file; the
           capture file has been closed - just free the capture file name
           string and return (without changing the last containing
           directory). */
        return;
    }
}
Beispiel #6
0
/* open the file corresponding to the given fileset entry */
static void
fs_open_entry(fileset_entry *entry)
{
    char *fname;
    int   err;


    /* make a copy of the filename (cf_close will indirectly destroy it right now) */
    fname = g_strdup(entry->fullname);

    /* close the old and open the new file */
    cf_close(&cfile);
    if (cf_open(&cfile, fname, FALSE, &err) == CF_OK) {
        cf_read(&cfile, FALSE);
    }

    g_free(fname);
}
double
se_get_avg_document_len(char *filename) {
  FILE *infile;
  assoc_rec address;
  double total_docsize, final_result;
  int nrecs, i;
  config_file_info *cf;


  cf = find_file_config(filename);
  
  if (cf->file_type > 99) {
    return(AvgDataStoreSize(filename, cf));
  }

  infile = cf_open(filename, ASSOCFILE);
  total_docsize = 0.0;

  if (infile) {
    fseek(infile, 0, 0);
    fread(&address, sizeof(assoc_rec), 1, infile);
    nrecs = address.offset;
    /* new version uses buildassoc created total in second long */
    if (address.recsize > 0)
      total_docsize = (double)address.recsize;
    else {
      /* OLD inefficient version... */
      for (i = 0; i < nrecs; i++) {
	fread(&address, sizeof(assoc_rec), 1, infile);
	total_docsize += (double)address.recsize;
      }
    } 

    final_result = total_docsize/(double)nrecs;
      
    return (final_result);

  }
  else
    return (total_docsize);
  
}
    cf_uint32 ReadPid()
    {
        char p [MAX_SIZE];
        GetPathByPathNameExt(_filePathName.c_str(), _filePathName.size() , p);
        if(0==IsFileExist(p))
        {
            _THROW_FMT(RuntimeWarning, "Failed access file %s !",p);
        }

        _fd =cf_open(_filePathName.c_str(),O_RDWR,S_IRWXU | S_IRWXG | S_IROTH);
        if(-1==_fd)
        {
            _THROW(SyscallExecuteError, "Failed to execute cf_open !");
        }
        char buf[8] = {0};
        ssize_t nr =cf_read(_fd,buf,sizeof(buf));
        if(nr<=0)
        {
            _THROW_FMT(ValueError, "Failed cf_read ! nr=%d ",int(nr));
        }
        return cf_uint32(atoi(buf));
    }
Beispiel #9
0
static void
exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
{
    int   import_file_fd;
    char *tmpname, *capfile_name;
    int   err;

    /* pcapng defs */
    wtapng_section_t            *shb_hdr;
    wtapng_iface_descriptions_t *idb_inf;
    wtapng_if_descr_t            int_data;
    GString                     *os_info_str;
    char                        *appname;

    /* Choose a random name for the temporary import buffer */
    import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_");
    capfile_name = g_strdup(tmpname);

    /* Create data for SHB  */
    os_info_str = g_string_new("");
    get_os_version_info(os_info_str);

    appname = g_strdup_printf("Wireshark %s", get_ws_vcs_version_info());

    shb_hdr = g_new(wtapng_section_t,1);
    shb_hdr->section_length = -1;
    /* options */
    shb_hdr->opt_comment    = g_strdup_printf("Dump of PDUs from %s", cfile.filename);
    shb_hdr->shb_hardware   = NULL;                    /* UTF-8 string containing the
                                                       * description of the hardware used to create this section.
                                                       */
    shb_hdr->shb_os         = os_info_str->str;        /* UTF-8 string containing the name
                                                       * of the operating system used to create this section.
                                                       */
    g_string_free(os_info_str, FALSE);                /* The actual string is not freed */
    shb_hdr->shb_user_appl  = appname;                /* UTF-8 string containing the name
                                                       *  of the application used to create this section.
                                                       */


    /* Create fake IDB info */
    idb_inf = g_new(wtapng_iface_descriptions_t,1);
    idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));

    /* create the fake interface data */
    int_data.wtap_encap            = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
    int_data.time_units_per_second = 1000000; /* default microsecond resolution */
    int_data.link_type             = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
    int_data.snap_len              = WTAP_MAX_PACKET_SIZE;
    int_data.if_name               = g_strdup("Fake IF, PDU->Export");
    int_data.opt_comment           = NULL;
    int_data.if_description        = NULL;
    int_data.if_speed              = 0;
    int_data.if_tsresol            = 6;
    int_data.if_filter_str         = NULL;
    int_data.bpf_filter_len        = 0;
    int_data.if_filter_bpf_bytes   = NULL;
    int_data.if_os                 = NULL;
    int_data.if_fcslen             = -1;
    int_data.num_stat_entries      = 0;          /* Number of ISB:s */
    int_data.interface_statistics  = NULL;

    g_array_append_val(idb_inf->interface_data, int_data);

    exp_pdu_tap_data->wdh = wtap_dump_fdopen_ng(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE, FALSE, shb_hdr, idb_inf, &err);
    if (exp_pdu_tap_data->wdh == NULL) {
        open_failure_alert_box(capfile_name, err, TRUE);
        goto end;
    }


    /* Run the tap */
    cf_retap_packets(&cfile);


    if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
        write_failure_alert_box(capfile_name, err);
    }

    remove_tap_listener(exp_pdu_tap_data);

    /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
    if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
        open_failure_alert_box(capfile_name, err, FALSE);
        goto end;
    }

    switch (cf_read(&cfile, FALSE)) {
    case CF_READ_OK:
    case CF_READ_ERROR:
    /* Just because we got an error, that doesn't mean we were unable
       to read any of the file; we handle what we could get from the
       file. */
    break;

    case CF_READ_ABORTED:
    /* The user bailed out of re-reading the capture file; the
       capture file has been closed - just free the capture file name
       string and return (without changing the last containing
       directory). */
    break;
    }

end:
    g_free(capfile_name);
    g_free(shb_hdr);
    g_free(appname);
}
Beispiel #10
0
/**
 * This function processes a specified capture file with the specified fields of interest
 * and display filter.  This function calls the appropriate language-specific callback
 * function in <cb> to manipulate data structures in the caller's scope.
 * 
 * @param filename valid pcap file
 * @param nfields a positive integer describing the number of fields
 * @param fields an array of strings
 * @return 0 on success, else error.
 */
glong sharktools_get_cb(gchar *filename, gulong nfields, const gchar **fields,
                       gchar *dfilterorig, sharktools_callbacks *cb)
{
  gsize i;
  capture_file cfile;
  gchar *cf_name = NULL;
  char *dfilter;
  dfilter_t *rfcode = NULL;

  // Create an stdata struct on the stack
  st_data_t stdata;

  dprintf("%s: entering...\n", __FUNCTION__);

  dprintf("%s: dfilterorig: %s\n", __FUNCTION__, dfilterorig);

  dfilter = strdup(dfilterorig);

  dprintf("%s: dfilter: %s\n", __FUNCTION__, dfilter);

  if(!dfilter_compile(dfilter, &rfcode))
    {
      sprintf(errmsg, "%s", dfilter_error_msg);
      printf("errmsg");
      if(rfcode)
        dfilter_free(rfcode);
      return -1;
    }

  // Defined in cfile.c, looks easy enough to use
  cap_file_init(&cfile);

  cf_name = filename;

  // Open pcap file
  int err;
  if(cf_open(&cfile, cf_name, FALSE, &err) != CF_OK)
    {
      //sprintf(errmsg, "%s", dfilter_error_msg);
      if(rfcode)
        dfilter_free(rfcode);
      return -1;
    }

  dprintf("nfields = %ld\n", nfields);

  stdata_init(&stdata, nfields);

  stdata_add_fields(&stdata, fields, nfields);

  dprintf("stdata.fields->len = %d\n", stdata.fields->len);

  dprintf("stdata.field_values_str = %lX\n", (glong)stdata.field_values_str);
  dprintf("stdata.field_types = %lX\n", (glong)stdata.field_types);
  
  dprintf("%s: opened file\n", __FUNCTION__);

  cfile.rfcode = rfcode;

  gchar        *err_info;
  gint64       data_offset;

  // Read and process each packet one at a time
  while(wtap_read(cfile.wth, &err, &err_info, &data_offset))
    {
      dprintf("*******************************\n");

      // (Re)-set all the stdata.field_{values,types} fields
      for(i = 0; i < nfields; i++)
        {
          stdata.field_values_str[i] = 0;
          stdata.field_types[i] = FT_NONE;
        }

      gboolean passed = FALSE;
      
      passed = process_packet(&cfile, data_offset, &stdata);

      if(passed)
	{
          gpointer row = cb->row_new(cb);

	  for(i = 0; i < nfields; i++)
	    {
              gpointer key;
              key = cb->keys[i];

              dprintf("key = %p\n", key);

	      dprintf("values[%ld] = %p\n", i, stdata.field_values_str[i]);
	      dprintf("types[%ld] = %ld\n", i, stdata.field_types[i]);

              cb->row_set(cb, row, key,
                          stdata.field_types[i],
                          stdata.field_values_native[i],
                          stdata.field_values_str[i]
                          );
            }

          cb->row_add(cb, row);
        }
    }

  if(rfcode)
    dfilter_free(rfcode);
  wtap_close(cfile.wth);
  cfile.wth = NULL;

  stdata_cleanup(&stdata);

  dprintf("%s: ...leaving.\n", __FUNCTION__);

  return 0;
}
Beispiel #11
0
/**
 * This function returns the number of packets in <filename> that would
 * pass through the filter <dfilter>.  This was necessary to mitigate memory
 * usage in Matlab, where dynamically-growing or shrinking data structures
 * (apparently) don't exist.  If it looks hacky, it is :-)
 *
 * NB: there is a race condition between running sharktools_count() and
 * sharktools_get_cb(), since we close and reopen the pcap file in between.
 */
glong sharktools_count(char *filename, char *dfilter)
{
  capture_file cfile;
  gchar *cf_name = NULL;
  dfilter_t *rfcode = NULL;
  glong count = 0;

  dprintf("%s: entering...\n", __FUNCTION__);

  dprintf("%s: dfilter: %s\n", __FUNCTION__, dfilter);
  if(!dfilter_compile(dfilter, &rfcode))
    {
      sprintf(errmsg, "%s", dfilter_error_msg);
      printf("errmsg");
      if(rfcode)
        dfilter_free(rfcode);
      return -1;
    }

  // Defined in cfile.c, looks easy enough to use
  cap_file_init(&cfile);

  cf_name = filename;

  // Open pcap file
  int err;
  if(cf_open(&cfile, cf_name, FALSE, &err) != CF_OK)
    {
      //sprintf(errmsg, "%s", dfilter_error_msg);
      if(rfcode)
        dfilter_free(rfcode);
      return -1;
    }

  dprintf("%s: opened file\n", __FUNCTION__);

  cfile.rfcode = rfcode;

  gchar        *err_info;
  gint64       data_offset;

  // Read and process each packet one at a time
  while(wtap_read(cfile.wth, &err, &err_info, &data_offset))
    {
      gboolean passed = TRUE;
      
      // Only process packets if there's a display filter specified
      if(dfilter != NULL && *dfilter != '\0')
        {
          // Passing in NULL for st_data_t means we're just counting
          passed = process_packet(&cfile, data_offset, NULL);
        }

      if(passed)
	{
          count++;
          //dprintf("count! %d\n", count);
        }
    }

  if(rfcode)
    dfilter_free(rfcode);
  wtap_close(cfile.wth);
  cfile.wth = NULL;

  return count;
}
Beispiel #12
0
int fil_add(char *indata, char *filename) 
{
  FILE *afile;        /* pointer to associator file */
  FILE *sgmlfile;     /* pointer to sgml file */

  assoc_rec assocrec; /* associator output record */
  int offset;        /* address in marc file */
  int slength;       /* length of sgml record */		
  int high_recno;    /* current highest logical record number */
  int recno;         /* logical record number of the new record    */
                      /* added by this function and passed to index */
		      /* function. Also the value of the 0th record */
		      /* in assocfile to keep track of highest      */
		      /* logical recno in use.                      */
  int i = 0;

  /* call function to get assocfile pointer */
  afile = cf_open(filename, ASSOCFILE);
  if (afile == NULL) {
    fprintf(LOGFILE, "Could not open associator file in fil_add\n");
    return(FAIL);
  }

  /* get new highest logical record number */
  fseek(afile, 0, 0);
  fread(&high_recno,sizeof(int),1, afile); 
  recno = high_recno + 1;

  /* call function to get sgmlfile pointer */
  sgmlfile = cf_open_cont(filename, MAINFILE, recno);
  if (sgmlfile == NULL) {
    fprintf(LOGFILE, "Could not open main file in fil_add\n");
      return(FAIL);
  }

  /* append to the end of the data file */
  fseek(sgmlfile, 0, 2);
  assocrec.offset = offset = ftell(sgmlfile);
  fprintf(sgmlfile, "%s\n", indata);

  /* record offset and length of sgml record in assoc file */
  assocrec.recsize = slength = strlen(indata);
  fseek(afile,recno*sizeof(assoc_rec), 0);
  fwrite(&assocrec, sizeof(assoc_rec), 1, afile);

  /* increment the zeroth record to record the */
  /* highest record number now in use          */
  fseek(afile, 0, 0);
  fwrite(&recno, sizeof(int), 1, afile);

  /* update history file */
  if (fil_hist(H_ADD, offset, recno, slength, filename) == FAIL) {
    fprintf(LOGFILE, "Could not update history file in fil_add\n");
    return(FAIL);
  }

  /* make sure the writes are done */
  fflush(sgmlfile);
  fflush(afile);

  return(recno);
}
Beispiel #13
0
/* open/merge the dnd file */
void
dnd_open_file_cmd(gchar *cf_names_freeme)
{
    int       err;
    gchar     *cf_name;
    int       in_file_count;
    int       files_work;
    char      **in_filenames;
    char      *tmpname;

    if (cf_names_freeme == NULL) return;

    /* DND_TARGET_URL:
     * The cf_name_freeme is a single string, containing one or more URI's,
     * terminated by CR/NL chars. The length of the whole field can be found
     * in the selection_data->length field. If it contains one file, simply open it,
     * If it contains more than one file, ask to merge these files. */

    /* count the number of input files */
    cf_name = cf_names_freeme;
    for(in_file_count = 0; (cf_name = strstr(cf_name, "\r\n")) != NULL; ) {
        cf_name += 2;
        in_file_count++;
    }
    if (in_file_count == 0) {
      g_free(cf_names_freeme);
      return;
    }

    in_filenames = (char **)g_malloc(sizeof(char*) * in_file_count);

    /* store the starts of the file entries in a gchar array */
    cf_name = cf_names_freeme;
    in_filenames[0] = cf_name;
    for(files_work = 1; (cf_name = strstr(cf_name, "\r\n")) != NULL && files_work < in_file_count; ) {
        cf_name += 2;
        in_filenames[files_work] = cf_name;
        files_work++;
    }

    /* replace trailing CR NL simply with zeroes (in place), so we get valid terminated strings */
    cf_name = cf_names_freeme;
    g_strdelimit(cf_name, "\r\n", '\0');

    /* convert all filenames from URI to local filename (in place) */
    for(files_work = 0; files_work < in_file_count; files_work++) {
        in_filenames[files_work] = dnd_uri2filename(in_filenames[files_work]);
    }

    if (in_file_count == 1) {
        /* open and read the capture file (this will close an existing file) */
        if (cf_open(&cfile, in_filenames[0], FALSE, &err) == CF_OK) {
            /* XXX - add this to the menu if the read fails? */
            cf_read(&cfile, FALSE);
            add_menu_recent_capture_file(in_filenames[0]);
        } else {
            /* the capture file couldn't be read (doesn't exist, file format unknown, ...) */
        }
    } else {
        /* merge the files in chronological order */
        tmpname = NULL;
        if (cf_merge_files(&tmpname, in_file_count, in_filenames,
                           WTAP_FILE_TYPE_SUBTYPE_PCAP, FALSE) == CF_OK) {
            /* Merge succeeded; close the currently-open file and try
               to open the merged capture file. */
            cf_close(&cfile);
            if (cf_open(&cfile, tmpname, TRUE /* temporary file */, &err) == CF_OK) {
                g_free(tmpname);
                cf_read(&cfile, FALSE);
            } else {
                /* The merged file couldn't be read. */
                g_free(tmpname);
            }
        } else {
            /* merge failed */
            g_free(tmpname);
        }
    }

    g_free(in_filenames);
    g_free(cf_names_freeme);
}
Beispiel #14
0
/* open/merge the dnd file */
void
dnd_open_file_cmd(gchar *cf_names_freeme)
{
    int       err;
    gchar     *cf_name;
    int       in_files;
    GString   *dialog_text;
    int       files_work;
    char      **in_filenames;

    if (cf_names_freeme == NULL) return;

    /* DND_TARGET_URL:
     * The cf_name_freeme is a single string, containing one or more URI's,
     * terminated by CR/NL chars. The length of the whole field can be found
     * in the selection_data->length field. If it contains one file, simply open it,
     * If it contains more than one file, ask to merge these files. */

    /* count the number of input files */
    cf_name = cf_names_freeme;
    for(in_files = 0; (cf_name = strstr(cf_name, "\r\n")) != NULL; ) {
        cf_name += 2;
        in_files++;
    }
    if (in_files == 0) {
      g_free(cf_names_freeme);
      return;
    }

    in_filenames = g_malloc(sizeof(char*) * in_files);

    /* store the starts of the file entries in a gchar array */
    cf_name = cf_names_freeme;
    in_filenames[0] = cf_name;
    for(files_work = 1; (cf_name = strstr(cf_name, "\r\n")) != NULL && files_work < in_files; ) {
        cf_name += 2;
        in_filenames[files_work] = cf_name;
        files_work++;
    }

    /* replace trailing CR NL simply with zeroes (in place), so we get valid terminated strings */
    cf_name = cf_names_freeme;
    g_strdelimit(cf_name, "\r\n", '\0');

    /* convert all filenames from URI to local filename (in place) */
    for(files_work = 0; files_work < in_files; files_work++) {
        in_filenames[files_work] = dnd_uri2filename(in_filenames[files_work]);
    }

    if (in_files == 1) {
        /* open and read the capture file (this will close an existing file) */
        if (cf_open(&cfile, in_filenames[0], FALSE, &err) == CF_OK) {
            /* XXX - add this to the menu if the read fails? */
            cf_read(&cfile, FALSE);
            add_menu_recent_capture_file(in_filenames[0]);
        } else {
            /* the capture file couldn't be read (doesn't exist, file format unknown, ...) */
        }
    } else {
        /* build and show the info dialog */
        dialog_text = g_string_sized_new(200);
        g_string_printf(dialog_text, "%sMerging the following files:%s\n\n",
            simple_dialog_primary_start(), simple_dialog_primary_end());
        for(files_work = 0; files_work < in_files; files_work++) {
            g_string_append(dialog_text, in_filenames[files_work]);
            g_string_append(dialog_text, "\n");
        }
        g_string_append(dialog_text, "\nThe packets in these files will be merged chronologically into a new temporary file.");
        simple_dialog(ESD_TYPE_CONFIRMATION, ESD_BTN_OK, "%s",
                      dialog_text->str);
        g_string_free(dialog_text, TRUE);

        /* actually merge the files now */
        dnd_merge_files(in_files, in_filenames);
    }

    g_free(in_filenames);
    g_free(cf_names_freeme);
}
Beispiel #15
0
 *       INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
 *       INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE
 *       AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE
 *       POSSIBILITY OF SUCH DAMAGE. 
 *    
 *       REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
 *       NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 *       FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
 *       ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS
 *       PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
 *       MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

  if (argc == 3) {
    printf("Testing LCC tree\n");
    printf("Opening lcctree\n");
    test = cf_open("lcctree",MAINFILE);

    if( sscanf(argv[2],
		    "%[ABCDEFGHIJKLMNOPQRSTUVWXYZ]%f",
		    lcalpha,
		    &lcnumber) == 2 ) {
      *buf = '\0';
      lcc_getdesc( buf, lccroot, lcalpha, lcnumber );
      printf("\n%s\n",buf);
    
    }
  }
  

Beispiel #16
0
static void
exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
{
    char *tmpname, *capfile_name;
    int   err;

    /* pcapng defs */
    wtap_optionblock_t           shb_hdr;
    wtapng_iface_descriptions_t *idb_inf;
    wtap_optionblock_t           int_data;
    wtapng_if_descr_mandatory_t *int_data_mand;
    GString                     *os_info_str;
    gchar                       *opt_comment, *wireshark_ver;

    /* Create data for SHB  */
    os_info_str = g_string_new("");
    get_os_version_info(os_info_str);

    shb_hdr = wtap_optionblock_create(WTAP_OPTION_BLOCK_NG_SECTION);

    /* options */
    opt_comment = g_strdup_printf("Dump of PDUs from %s", cfile.filename);
    wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, opt_comment);
    g_free(opt_comment);

    /*
     * UTF-8 string containing the name of the operating system used to create
     * this section.
     */
    wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE));
    /*
     * UTF-8 string containing the name of the application used to create
     * this section.
     */
    wireshark_ver = g_strdup_printf("Wireshark %s", get_ws_vcs_version_info());
    wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, wireshark_ver);
    g_free(wireshark_ver);

    /* Create fake IDB info */
    idb_inf = g_new(wtapng_iface_descriptions_t,1);
    idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtap_optionblock_t));

    /* create the fake interface data */
    int_data = wtap_optionblock_create(WTAP_OPTION_BLOCK_IF_DESCR);
    int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_optionblock_get_mandatory_data(int_data);
    int_data_mand->wtap_encap      = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
    int_data_mand->time_units_per_second = 1000000000; /* default nanosecond resolution */
    int_data_mand->link_type       = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
    int_data_mand->snap_len        = WTAP_MAX_PACKET_SIZE;

    wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF, PDU->Export");
    wtap_optionblock_set_option_uint8(int_data, OPT_IDB_TSRESOL, 9);

    g_array_append_val(idb_inf->interface_data, int_data);

    /* Use a random name for the temporary import buffer */
    exp_pdu_tap_data->wdh = wtap_dump_open_tempfile_ng(&tmpname, "Wireshark_PDU_",
                            WTAP_FILE_TYPE_SUBTYPE_PCAPNG,
                            WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE,
                            FALSE, shb_hdr, idb_inf, NULL, &err);
    capfile_name = g_strdup(tmpname);
    if (exp_pdu_tap_data->wdh == NULL) {
        open_failure_alert_box(capfile_name ? capfile_name : "temporary file", err, TRUE);
        goto end;
    }

    /* Run the tap */
    cf_retap_packets(&cfile);


    if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
        write_failure_alert_box(capfile_name, err);
    }

    remove_tap_listener(exp_pdu_tap_data);

    /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
    if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
        open_failure_alert_box(capfile_name, err, FALSE);
        goto end;
    }

    switch (cf_read(&cfile, FALSE)) {
    case CF_READ_OK:
    case CF_READ_ERROR:
        /* Just because we got an error, that doesn't mean we were unable
           to read any of the file; we handle what we could get from the
           file. */
        break;

    case CF_READ_ABORTED:
        /* The user bailed out of re-reading the capture file; the
           capture file has been closed - just free the capture file name
           string and return (without changing the last containing
           directory). */
        break;
    }

end:
    g_free(capfile_name);
    wtap_optionblock_free(shb_hdr);
    wtap_free_idb_info(idb_inf);
}
Beispiel #17
0
/* We've succeeded in doing a (non real-time) capture; try to read it into a new capture file */
static gboolean
capture_input_read_all(capture_session *cap_session, gboolean is_tempfile,
                       gboolean drops_known, guint32 drops)
{
  capture_options *capture_opts = cap_session->capture_opts;
  int err;

  /* Capture succeeded; attempt to open the capture file. */
  if (cf_open((capture_file *)cap_session->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
    /* We're not doing a capture any more, so we don't have a save file. */
    return FALSE;
  }

  /* Set the read filter to NULL. */
  /* XXX - this is odd here; try to put it somewhere where it fits better */
  cf_set_rfcode((capture_file *)cap_session->cf, NULL);

  /* Get the packet-drop statistics.

     XXX - there are currently no packet-drop statistics stored
     in libpcap captures, and that's what we're reading.

     At some point, we will add support in Wiretap to return
     packet-drop statistics for capture file formats that store it,
     and will make "cf_read()" get those statistics from Wiretap.
     We clear the statistics (marking them as "not known") in
     "cf_open()", and "cf_read()" will only fetch them and mark
     them as known if Wiretap supplies them, so if we get the
     statistics now, after calling "cf_open()" but before calling
     "cf_read()", the values we store will be used by "cf_read()".

     If a future libpcap capture file format stores the statistics,
     we'll put them into the capture file that we write, and will
     thus not have to set them here - "cf_read()" will get them from
     the file and use them. */
  if (drops_known) {
    cf_set_drops_known((capture_file *)cap_session->cf, TRUE);

    /* XXX - on some systems, libpcap doesn't bother filling in
       "ps_ifdrop" - it doesn't even set it to zero - so we don't
       bother looking at it.

       Ideally, libpcap would have an interface that gave us
       several statistics - perhaps including various interface
       error statistics - and would tell us which of them it
       supplies, allowing us to display only the ones it does. */
    cf_set_drops((capture_file *)cap_session->cf, drops);
  }

  /* read in the packet data */
  switch (cf_read((capture_file *)cap_session->cf, FALSE)) {

  case CF_READ_OK:
  case CF_READ_ERROR:
    /* Just because we got an error, that doesn't mean we were unable
       to read any of the file; we handle what we could get from the
       file. */
    break;

  case CF_READ_ABORTED:
    /* User wants to quit program. Exit by leaving the main loop,
       so that any quit functions we registered get called. */
    main_window_nested_quit();
    return FALSE;
  }

  /* if we didn't capture even a single packet, close the file again */
  if(cf_get_packet_count((capture_file *)cap_session->cf) == 0 && !capture_opts->restart) {
    simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
"%sNo packets captured!%s\n"
"\n"
"As no data was captured, closing the %scapture file!\n"
"\n"
"\n"
"Help about capturing can be found at:\n"
"\n"
"       http://wiki.wireshark.org/CaptureSetup"
#ifdef _WIN32
"\n\n"
"Wireless (Wi-Fi/WLAN):\n"
"Try to switch off promiscuous mode in the Capture Options!"
#endif
"",
    simple_dialog_primary_start(), simple_dialog_primary_end(),
    (cf_is_tempfile((capture_file *)cap_session->cf)) ? "temporary " : "");
    cf_close((capture_file *)cap_session->cf);
  }
  return TRUE;
}
Beispiel #18
0
/* capture child tells us we have a new (or the first) capture file */
gboolean
capture_input_new_file(capture_session *cap_session, gchar *new_file)
{
  capture_options *capture_opts = cap_session->capture_opts;
  gboolean is_tempfile;
  int  err;

  if(cap_session->state == CAPTURE_PREPARING) {
    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started");
  }
  g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file);

  g_assert(cap_session->state == CAPTURE_PREPARING || cap_session->state == CAPTURE_RUNNING);

  /* free the old filename */
  if(capture_opts->save_file != NULL) {
    /* we start a new capture file, close the old one (if we had one before). */
    /* (we can only have an open capture file in real_time_mode!) */
    if( ((capture_file *) cap_session->cf)->state != FILE_CLOSED) {
        if(capture_opts->real_time_mode) {
            capture_callback_invoke(capture_cb_capture_update_finished, cap_session);
            cf_finish_tail((capture_file *)cap_session->cf, &err);
            cf_close((capture_file *)cap_session->cf);
        } else {
            capture_callback_invoke(capture_cb_capture_fixed_finished, cap_session);
        }
    }
    g_free(capture_opts->save_file);
    is_tempfile = FALSE;
    cf_set_tempfile((capture_file *)cap_session->cf, FALSE);
  } else {
    /* we didn't have a save_file before; must be a tempfile */
    is_tempfile = TRUE;
    cf_set_tempfile((capture_file *)cap_session->cf, TRUE);
  }

  /* save the new filename */
  capture_opts->save_file = g_strdup(new_file);

  /* if we are in real-time mode, open the new file now */
  if(capture_opts->real_time_mode) {
    /* Attempt to open the capture file and set up to read from it. */
    switch(cf_open((capture_file *)cap_session->cf, capture_opts->save_file, WTAP_TYPE_AUTO, is_tempfile, &err)) {
    case CF_OK:
      break;
    case CF_ERROR:
      /* Don't unlink (delete) the save file - leave it around,
         for debugging purposes. */
      g_free(capture_opts->save_file);
      capture_opts->save_file = NULL;
      return FALSE;
    }
  } else {
    capture_callback_invoke(capture_cb_capture_prepared, cap_session);
  }

  if(capture_opts->show_info) {
    if (!capture_info_new_file(new_file))
      return FALSE;
  }

  if(capture_opts->real_time_mode) {
    capture_callback_invoke(capture_cb_capture_update_started, cap_session);
  } else {
    capture_callback_invoke(capture_cb_capture_fixed_started, cap_session);
  }
  cap_session->state = CAPTURE_RUNNING;

  return TRUE;
}
int se_getdoclen(int docid, char *filename, idx_list_entry *idx) 
{
  FILE *infile;
  assoc_rec address;
  int total_docsize;
  page_assoc_rec *page_assoc_rec_data = NULL;
  component_data_item *comp_data;
  config_file_info *cf;


  if (docid == 0 || filename == NULL || idx == NULL) 
    return 1;

  if (idx->type & PAGEDINDEX) { /* paged files have a doc associator file */
    page_assoc_rec_data = se_get_page_assoc(filename, idx->tag, docid);
    if (page_assoc_rec_data != NULL)
      return(page_assoc_rec_data->recsize);
    else
      return 1;
  } 
  else if (idx->type & EXTERNKEY) { 
    /* external files  and freq files have an associated  DOCSIZES file */
    infile = (FILE *) cf_open(filename, DOCSIZEFILE);
    if (infile) {
      fseek(infile, (docid * sizeof(int)), 0);
      fread(&total_docsize, sizeof(int), 1, infile);
      return(total_docsize);
    }
    else 
      return (0);
  }
  else if (idx->type & COMPONENT_INDEX ) {
    comp_data = get_component_record(docid, idx->comp_parent);
    if (comp_data == NULL)
      return (1);
    total_docsize = comp_data->end_offset - comp_data->start_offset;
    FREE (comp_data);
    return (total_docsize);
  }
  else {
    cf = find_file_config(filename);
    if (cf->file_type > 99) {
      VerifyDataStore(filename, docid, &address);
	return (address.recsize);
    }
    else {
      infile = cf_open(filename, ASSOCFILE);
      
      if (infile) {
	fseek(infile, (docid*sizeof(assoc_rec)), 0);
	fread(&address, sizeof(assoc_rec), 1, infile);
	return ((int)address.recsize);
      }
      else
	fprintf(LOGFILE,"Couldn't get assoc record in getdoclen\n");
      
      /* on errors return 1 */
    }
    return (1);
  }
}