Beispiel #1
1
static int fill_packet_struct_bb(struct emwin_packet *ep, 
				      char *bbdata, int datasize){
  /*
   * The width of the string fields in fmt depend on what is defined
   * in emwin.h, for the length of the file name and the time stamp.
   * If the file name has exactly 12 characters this format PF%12s/PN is
   * correct. But if it has less, and the rest are blanks, the /PN
   * will not match.  For that reason we write it as "PF%12s /PN"
   * because that space before the /PN is actually discarded.
   */  
  int i;
  /*   char *fmt = "/PF%12s/PN %d /PT %d /CS %d /FD%31s"; */
  char *fmt = "/PF%12s /PN %d /PT %d /CS %d /FD%22c";
  char *b;

  /*
   * This (ep->bbdata) is the bb stuff that we get and what we will retransmit.
   * We should really retransmit the cleaned stuff (but of course
   * that would not a BB server). On the other hand, if we retransmit
   * just the cleaned data (and write the program to be prepared
   * to get the data as such) we can reduce bandwidth significantly
   * by eliminating the transmission of unnecessary stuff
   * (NULL's, padding, etc) that these packets contain.
   */
  memcpy(ep->bbdata, bbdata, datasize);
  ep->bbdata_size = datasize;

  /* 
   * Get the unxored header + data block.
   * Get rid of the initial and final 6 NULL's
   * that are always present, and decode each character.
   * ep->rawdata then contains the unxored header + data block.
   */
  datasize -= (EMWIN_NULLPAD_SIZE * 2);
  for(i = 0; i < datasize; ++i){
    ep->rawdata[i] = bbdata[i + EMWIN_NULLPAD_SIZE] ^ 0xff;
  }
  ep->rawdata_size = datasize;
  
  /*
   * ep->data points to the start of the data block.
   */
  ep->data = &(ep->rawdata[EMWIN_HEADER_SIZE]);
  ep->data_size = datasize - EMWIN_HEADER_SIZE;

  /*
   * The V1 queue packet is just the bbdata, prepended by the size. The V2
   * depends on the raw (unxored) data.
   */
  memcpy(&ep->queue_data_v1[EMWIN_QUEUE_ENVELOPE_SIZE],
	 ep->bbdata, ep->bbdata_size);
  pack_uint32(ep->queue_data_v1, (uint32_t)ep->bbdata_size, 0);
  ep->queue_data_v1_size = EMWIN_QUEUE_ENVELOPE_SIZE + ep->bbdata_size;

  /*
   * For V2 we use the function in v2.c. If it fails, fall back to a V1
   * style queue packet as above.
   *
   * Moreover, if the configuration has specified a V1 only server, V2 clients
   * get V1 packages only.
   */
  if((g.serverprotocol == PROTOCOL_EMWIN1) || (build_queue_data_v2(ep) != 0) ||
     (ep->compress_ratio >= g.min_compress_ratio)){
    memcpy(&ep->queue_data_v2[EMWIN_QUEUE_ENVELOPE_SIZE],
	   ep->bbdata, ep->bbdata_size);
    pack_uint32(ep->queue_data_v2, (uint32_t)ep->bbdata_size, 0);
    ep->queue_data_v2_size = EMWIN_QUEUE_ENVELOPE_SIZE + ep->bbdata_size;
  }

  update_stats_frames_received((unsigned int)ep->bbdata_size);

  /*
   * Get the elements of the header.
   */  
  i = sscanf(ep->rawdata, fmt, ep->header.filename, &ep->header.blockno, 
	     &ep->header.numblocks, &ep->header.checksum, &ep->header.dtstamp);

  if(i < 5){
    update_stats_frames(1);
    return(2);	/* error in header format */
  }

  ep->header.dtstamp[EMWIN_TIMESTAMP_LEN - 1] = '\0';

  if(checksum(ep) != 0){
    update_stats_frames(1);
    return(3);
  }

  /*
   * This next function checks the file name and also converts it
   * to lower case.
   */
  if(checkfilename(ep) != 0){
    update_stats_frames(1);
    return(4);
  }

  /*
   *  log_verbose("%s: %d of %d", ep->header.filename,
   *	ep->header.blockno, ep->header.numblocks); 
   */

  /*
   * Sometimes, if the 1024 block does not contain enough data,
   * it is filled with NULL's. We get rid of them, but only if
   * it is txt file.
   */
  if(is_text_file(ep->header.filename)){

    b = &(ep->data[0]);
    i = 0;
    while((*b != '\0') && (i < ep->data_size)){
      ++i;
      ++b;
    }

    ep->data_size = i;
  }

  update_stats_frames(0);

  return(0);
}
Beispiel #2
1
static int fill_packet_struct_serial(struct emwin_packet *ep, 
				     char *serialdata, int datasize){
  int i;
  char *fmt = "/PF%12s /PN %d /PT %d /CS %d /FD%22c";
  char *b;

  /*
   * This (ep->bbdata) is the bb stuff that we get and what we will retransmit.
   * We should really retransmit the cleaned stuff (but of course
   * that would not a BB server).
   *
   * Note: Encrypt the data portion as well as the first and last six bytes.
   *       If those should not be encrypted, then the loop below should read
   *
   *	 memset(ep->bbdata, '\0', EMWIN_PACKET_SIZE);
   *     for(i = EMWIN_NULLPAD_SIZE;
   *		i < datasize - EMWIN_NULLPAD_SIZE; ++i){
   *		ep->bbdata[i] = serialdata[i] ^ 0xff;
   *	 }
   */
  /*  bb_xor(ep->bbdata, serialdata, data_size); */
  for(i = 0; i < datasize; ++i){
    ep->bbdata[i] = serialdata[i] ^ 0xff;
  }
  ep->bbdata_size = datasize;

  /* 
   * Get rid of the initial and final 6 NULL's that are always present.
   */
  datasize -= EMWIN_NULLPAD_SIZE * 2;
  for(i = 0; i < datasize; ++i){
    ep->rawdata[i] = serialdata[i + EMWIN_NULLPAD_SIZE];
  }
  ep->rawdata_size = datasize;

  /*
   * let ep->data point to the real data.
   */
  ep->data = &(ep->rawdata[EMWIN_HEADER_SIZE]);
  ep->data_size = datasize - EMWIN_HEADER_SIZE;

  /*
   * Form the V1 and V2 queue packets, as for the network case.
   */
  memcpy(&ep->queue_data_v1[EMWIN_QUEUE_ENVELOPE_SIZE],
	 ep->bbdata, ep->bbdata_size);
  pack_uint32(ep->queue_data_v1, (uint32_t)ep->bbdata_size, 0);
  ep->queue_data_v1_size = EMWIN_QUEUE_ENVELOPE_SIZE + ep->bbdata_size;

  if((g.serverprotocol == PROTOCOL_EMWIN1) || (build_queue_data_v2(ep) != 0) ||
     (ep->compress_ratio >= g.min_compress_ratio)){
    memcpy(&ep->queue_data_v2[EMWIN_QUEUE_ENVELOPE_SIZE],
	   ep->bbdata, ep->bbdata_size);
    pack_uint32(ep->queue_data_v2, (uint32_t)ep->bbdata_size, 0);
    ep->queue_data_v2_size = EMWIN_QUEUE_ENVELOPE_SIZE + ep->bbdata_size;
  }

  update_stats_frames_received((unsigned int)ep->bbdata_size);
  
  i = sscanf(ep->rawdata, fmt, ep->header.filename, &ep->header.blockno, 
	     &ep->header.numblocks, &ep->header.checksum, &ep->header.dtstamp);
  /*
   *  log_info("%d of %d", ep->header.blockno, ep->header.numblocks); 
   */

  if(i < 5){
    update_stats_frames(1);
    return(2);	/* error in header format */
  }

  ep->header.dtstamp[EMWIN_TIMESTAMP_LEN - 1] = '\0';

  if(checksum(ep) != 0){
    update_stats_frames(1);
    return(3);
  }

  /*
   * This next function checks the file name and also converts it
   * to lower case.
   */
  if(checkfilename(ep) != 0){
    update_stats_frames(1);
    return(4);
  }

  /*
   * Sometimes, if the 1024 block does not contain enough data,
   * it is filled with NULL's. We get rid of them, but only if
   * it is txt file.
   */
  if(is_text_file(ep->header.filename)){

    b = &(ep->data[0]);
    i = 0;
    while((*b != '\0') && (i < ep->data_size)){
      ++i;
      ++b;
    }

    ep->data_size = i;
  }

  update_stats_frames(0);

  return(0);
}
Hdf5Dataset::Hdf5Dataset(std::string path, std::string filename)
{
  this->path_ = path;
  this->filename_ = filename;
  checkPath(this->path_);
  checkfilename(this->filename_);
}
Beispiel #4
0
/* -------------------------------------------------------------------- */
void tutorial(char *filename)
{
    int  i;
    char temp[14];
    char oldverbose;
    
    outFlag     = OUTOK;
    setio(whichIO, echo, outFlag);

    if (!expert)  mPrintf("\n <3J0>ump <3N0>ext <3P0>ause <3S0>top\n");
    /* doCR(); */

    if (changedir(cfg.helppath) == -1 ) return;

    /* no bad files */
    if (checkfilename(filename, 0) == ERROR)
    {
        mPrintf(" No helpfile %s", filename);
        changedir(cfg.homepath);
        return;
    }

    if (ambig(filename))
    {
        /* fill our directory array according to filename */
        oldverbose = verbose;
        verbose = FALSE;
        filldirectory(filename);
        verbose = oldverbose;

        /* print out all the files */
        for (i = 0; filedir[i].entry[0] && 
        ( dumpf(filedir[i].entry) != ERROR) ; i++);

        if ( !i) mPrintf(" No helpfile %s", filename);

        /* free file directory structure */
        if(filedir != NULL)
        _ffree((void *)filedir);
    }
    else
    {
       strcpy(temp, filename);
       temp[strlen(temp)-1] = '@';

       if (filexists(temp) && *term.bold)
         dump(temp);
       else
         dumpf(filename);
    }

    /* go to our home-path */
    changedir(cfg.homepath);
}
Beispiel #5
0
    void action(const gcn::ActionEvent& actionEvent) {
        int selected_item;
        char filename[256]="";

        selected_item = listBox->getSelected();
        lastSelectedIndex = selected_item;
        strcpy(filename, "");
        strcat(filename, currentDir);
        strcat(filename, "/");
        strcat(filename, dirList.getElementAt(selected_item).c_str());
        checkfilename(filename);
    }
Beispiel #6
0
// this function is used for copying files
void listdir2(const char* filename, const char* destname)
{
	struct dirent *pDirent;
	struct stat st;
	DIR *pDir;
    pDir = opendir(filename);

	char destfile[1024];
	char sourcefile[1024];

	if (pDir == NULL)
	{
		perror("Source error");
        return;
    }
	
    while ((pDirent = readdir(pDir)) != NULL)
	{
		int len = snprintf (destfile, sizeof(destfile)-1, "%s/%s/%s", destname, filename, pDirent->d_name);
		int len2 = snprintf(sourcefile, sizeof(sourcefile)-1, "%s/%s", filename, pDirent->d_name);
		destfile[len] = 0;
		sourcefile[len2] = 0;
		
		if (pDirent->d_type == DT_REG)
		{

			if (checkfilename(sourcefile) == 1)
			{
				continue;
			}

			FILE* dFile = fopen(destfile, "r");
			if (dFile == NULL)
				destNotExist(sourcefile, destfile);
			else
				destExist(sourcefile, destfile);
		}
		else
		{
			if (strcmp(pDirent->d_name, ".") == 0 || strcmp(pDirent->d_name, "..") == 0)
				continue;
			else
			{
				listdir2(sourcefile, destname);
			}
		}
    }
    closedir (pDir);
}
bool SelectFile(const char *title, char *value, const char *filter[], bool create)
{
  dialogResult = false;
  dialogFinished = false;
  createNew = create;
  filefilter = filter;
  dialogCreated = false;
  selectedOnStart = -1;

  #ifdef FILE_SELECT_KEEP_POSITION
  if (Already_init == 0)
  {
    InitSelectFile(title);
    Already_init = 1;
  } else
  {
    strncpy(value,workingDir,MAX_PATH);
    gui_top->add(wndSelectFile);
    wndSelectFile->setCaption(title);
    wndSelectFile->requestModalFocus();
    wndSelectFile->setVisible(true);
    gui_top->moveToTop(wndSelectFile);
  }
  #else
  InitSelectFile(title);
  #endif

  extractPath(value, workingDir);
  checkfoldername(workingDir);
  checkfilename(value);
  SelectFileLoop();
#ifdef FILE_SELECT_KEEP_POSITION
  wndSelectFile->releaseModalFocus();
  wndSelectFile->setVisible(false);
#else
  ExitSelectFile();
#endif
  if(dialogResult)
    strncpy(value, workingDir, MAX_PATH);
#ifdef FILE_SELECT_KEEP_POSITION
  else
    strncpy(workingDir,value, MAX_PATH);
#endif
  return dialogResult;
}
Beispiel #8
0
    void action(const gcn::ActionEvent& actionEvent) {
        confirmselection=false;
#if defined(WIN32) || defined(ANDROID) || defined(AROS) || defined(RASPBERRY)
        if (menu_load_type != MENU_ADD_DIR) {
#endif

            int selected_item;
            char filename[256]="";

            selected_item = listBox->getSelected();
            lastSelectedIndex = selected_item;
            strcpy(filename, "");
            strcat(filename, currentDir);
            strcat(filename, "/");
            strcat(filename, dirList.getElementAt(selected_item).c_str());
            checkfilename(filename);
#if defined(WIN32) || defined(ANDROID) || defined(AROS) || defined(RASPBERRY)
        }
#endif
    }
Hdf5Dataset::Hdf5Dataset(std::string fullpath)
{
  std::stringstream fp(fullpath);
  std::string segment;
  std::vector<std::string> seglist;
  char the_path[256];

  while(std::getline(fp, segment, '/'))
  {
     seglist.push_back(segment);
  }

  std::ostringstream oss_file;
  oss_file<< seglist.back();
  this->filename_ = oss_file.str();


  seglist.pop_back();
  std::ostringstream oss_path;
  if (!seglist.empty())
  {
    std::copy(seglist.begin(), seglist.end()-1,
    std::ostream_iterator<std::string>(oss_path, "/"));
    oss_path << seglist.back();
    oss_path<<"/";
  }
  else
  {
    getcwd(the_path, 255);
    strcat(the_path, "/");
    oss_path << the_path;
  }

 this->path_ = oss_path.str();
 checkPath(this->path_);
 checkfilename(this->filename_);

}
Beispiel #10
0
/* -------------------------------------------------------------------- */
void setfileinfo(void)
{
    label filename;
    label uploader;
    char comments[64];
    char path[80];
    struct fInfo old;

    getNormStr("filename", filename, FILESIZE, ECHO);

    sprintf(path, "%s\\%s", roomBuf.rbdirname, filename);

    /* no bad file names */
    if (checkfilename(filename, 0) == ERROR) {
        mPrintf("\n No file %s.\n ", filename);
        return;
    }
    /* no file name? */
    if (!filexists(path)) {
        mPrintf("\n No file %s.\n ", filename);
        return;
    }
    if (!getInfo(filename, &old)) {
        strcpy(uploader, logBuf.lbname);
    } else {
        strcpy(uploader, old.uploader);
    }

    getString("comments", comments, 64, FALSE, TRUE, "");

    entercomment(filename, uploader, comments);

    sprintf(msgBuf->mbtext, "File info changed for file %s by %s",
    filename, logBuf.lbname);

    trap(msgBuf->mbtext, T_AIDE);
}
Beispiel #11
0
int main (int argc, const char * argv [])

{
	extern struct channel channel;
	static const char *optv [] =
	{
		"C:i:eFN:p:P:qt:vx",
		"-C file -P file -N file",
		"Atheros Powerline Device Flash Utility for INT6300",
		"C f\twrite CFG file to device using VS_SET_SDRAM",
		"e\tredirect stderr messages to stdout",

#if defined (WINPCAP) || defined (LIBPCAP)

		"i n\thost interface number [2]",

#else

		"i s\thost interface name [" CHANNEL_ETHDEVICE "]",

#endif

		"F[F]\tflash [force] NVRAM after firmware start using VS_MOD_NVM",
		"N f\twrite NVM file to device using VS_WR_MEM",
		"P f\twrite PIB file to device using VS_WR_MEM",
		"q\tquiet mode",

#if defined (WINPCAP) || defined (LIBPCAP)

		"t n\tread capture time is (n) milliseconds [50]",

#else

		"t n\tread timeout is (n) milliseconds [50]",

#endif

		"v\tverbose mode",
		"x\texit on error",
		(const char *) (0)
	};

#include "../plc/plc.c"

	char firmware [PLC_VERSION_STRING];
	signed c;
	if (getenv (PLCDEVICE))
	{

#if defined (WINPCAP) || defined (LIBPCAP)

		channel.ifindex = atoi (getenv (PLCDEVICE));

#else

		channel.ifname = strdup (getenv (PLCDEVICE));

#endif

	}
	optind = 1;
	opterr = 1;
	while ((c = getoptv (argc, argv, optv)) != -1)
	{
		switch ((char) (c))
		{
		case 'i':

#if defined (WINPCAP) || defined (LIBPCAP)

			channel.ifindex = atoi (optarg);

#else

			channel.ifname = optarg;

#endif

			break;
		case 'q':
			_setbits (channel.flags, CHANNEL_SILENCE);
			break;
		case 't':
			channel.timeout = (unsigned)(uintspec (optarg, 0, UINT_MAX));
			break;
		case 'v':
			_setbits (channel.flags, CHANNEL_VERBOSE);
			break;
		}
	}
	openchannel (&channel);
	desuid ();
	optind = 1;
	opterr = 1;
	while ((c = getoptv (argc, argv, optv)) != -1)
	{
		switch ((char) (c))
		{
		case 'C':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.CFG.file = open (optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", optarg);
			}
			if (sdramfile (plc.CFG.file, optarg, plc.flags))
			{
				error (1, ECANCELED, "CFG file %s is corrupt", optarg);
			}
			_setbits (plc.flags, PLC_SDRAM_CONFIG);
			plc.CFG.name = optarg;
			break;
		case 'e':
			dup2 (STDOUT_FILENO, STDERR_FILENO);
			break;
		case 'F':
			_setbits (plc.module, PLC_MODULE_NVM_PIB);
			if (_anyset (plc.flags, PLC_FLASH_DEVICE))
			{
				_setbits (plc.module, VS_MODULE_FORCE);
			}
			_setbits (plc.flags, PLC_FLASH_DEVICE);
			break;
		case 'N':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.NVM.file = open (optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", optarg);
			}
			plc.NVM.name = optarg;
			if (nvmfile1 (&plc.NVM))
			{
				error (1, errno, "Bad firmware file: %s", plc.NVM.name);
			}
			_setbits (plc.flags, PLC_WRITE_MAC);
			break;
		case 'P':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.PIB.file = open (optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", optarg);
			}
			plc.PIB.name = optarg;
			if (pibfile1 (&plc.PIB))
			{
				error (1, errno, "Bad parameter file: %s", plc.PIB.name);
			}
			_setbits (plc.flags, PLC_WRITE_PIB);
			break;
		case 'q':
			_setbits (plc.flags, PLC_SILENCE);
			break;
		case 'v':
			_setbits (plc.flags, PLC_VERBOSE);
			break;
		case 'x':
			_setbits (plc.flags, PLC_BAILOUT);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if (argc)
	{
		error (1, ECANCELED, "Too many arguments");
	}
	if (plc.CFG.file == -1)
	{
		error (1, ECANCELED, "No CFG file specified");
	}
	if (plc.PIB.file == -1)
	{
		error (1, ECANCELED, "No PIB file specified");
	}
	if (plc.NVM.file == -1)
	{
		error (1, ECANCELED, "No NVM file specified");
	}
	if (!(plc.message = malloc (sizeof (struct message))))
	{
		error (1, errno, PLC_NOMEMORY);
	}
	if (WaitForStart (&plc, firmware, sizeof (firmware)))
	{
		Failure (&plc, "Device must be connected");
		return (-1);
	}
	if (plc.hardwareID > CHIPSET_INT6300)
	{
		Failure (&plc, "Device must be %s or earlier; try using int6kboot.", chipsetname (CHIPSET_INT6300));
		return (-1);
	}
	if (strcmp (firmware, "BootLoader"))
	{
		Failure (&plc, "Bootloader must be running");
		return (-1);
	}
	if (!StartDevice1 (&plc))
	{
		if (_anyset (plc.flags, PLC_FLASH_DEVICE))
		{
			UpgradeDevice1 (&plc);
		}
	}
	free (plc.message);
	closechannel (&channel);
	exit (0);
}
Beispiel #12
0
int main(int argc, char *argv[])
{
	if (argc < 3)
	{
		printf("Correct usage: lcopy [-r] source dest\n");
		return 0;
	}
	if (argc == 3 && (strcmp(argv[1], "-r") == 0 || strcmp(argv[2], "-r") == 0))
	{
		printf("Correct usage: lcopy [-r] source dest\n");
		return 0;
	}
	
	int i, j;
	int destType = -1;
	int destStat;
	struct stat destBuffer;

	destStat = stat(argv[argc-1], &destBuffer);
	
	// if destination is a regular file
	if (S_ISREG(destBuffer.st_mode))
		destType = 0;

	// if destination is a directory
	else if (S_ISDIR(destBuffer.st_mode))
		destType = 1;

	// destination is a regular file
	if (destType == 0 || destType == -1)
	{
		if (argc > 3 || strcmp(argv[1], "-r") == 0)
		{
			printf("Destination does not exist or is a file.\n");
			return 0;
		}
		else
		{
			int sourceStat;
			struct stat sourceBuffer;

			sourceStat = stat(argv[1], &sourceBuffer);
			if (sourceStat != 0)
			{
				perror("Source Error");
				return 0;
			}
			if(S_ISDIR(sourceBuffer.st_mode))
			{
				printf("Destination is a file, directories cannot be copied.\n");
				return 0;
			}
			if (checkfilename(argv[1]) == 1)
			{
				printf("%s file is 'dig'. Please choose another file.\n", argv[1]);
				return;
			}
			if (destType == -1)
				destNotExist(argv[1], argv[2]);
			else
				destExist(argv[1], argv[2]);
		}
	}

	// if destination is a directory
	else if (S_ISDIR(destBuffer.st_mode))
	{
		char currentpath[1024];
		getcwd(currentpath, 1024);
		
		int recornot = -1;

		DIR *pDir;
		struct dirent *pDirent;
		pDir = opendir(argv[argc-1]);
				
		if (pDir == NULL)
		{
			printf("Cannot open directory.\n");
			return 0;
		}

		if (strcmp(argv[1], "-r") == 0)
		{
			recornot = 1;
			i = 2;
		}
		else
		{
			recornot = 0;
			i = 1;
		}
		for (; i < argc - 1; i++)
		{
			if (checkfilename(argv[i]) == 1)
			{
				printf("%s is 'dig'. Please choose another file.\n", argv[i]);
				continue;
			}
			chdir(currentpath);

			int sourceStat;
			struct stat sourceBuffer;
			sourceStat = stat(argv[i], &sourceBuffer);

			if (sourceStat != 0)
			{
				perror("Source error");
				continue;
			}
			else if (S_ISDIR(sourceBuffer.st_mode))
			{
				if (recornot == 0)
				{
					printf("%s is a directory. Use [-r] for copying it.\n", argv[i]);
					continue;
				}
				else
				{
					chdir(argv[argc-1]);
					char filepath[1024];
					getcwd(filepath, 1024);
					chdir(currentpath);

					chdir(argv[i]);
					char filepath2[1024];
					getcwd(filepath2, 1024);
					chdir(currentpath);

					char filewrite[1024];
					char* relative;
					relative = strrchr(filepath2, '/')+1;
					int len1 = snprintf(filewrite, sizeof(filewrite) - 1, "%s/%s", filepath, relative);
					filewrite[len1] = 0;
					struct stat st;
					if (stat (filewrite, &st) == -1)
					{
						mkdir(filewrite, 0700);
					}
					listdir(relative, argv[argc-1]);
					listdir2(relative, argv[argc-1]);
				}
			}
			else if (S_ISREG(sourceBuffer.st_mode))
			{
				chdir(argv[argc-1]);
				char filepath[1024];
				getcwd(filepath, 1024);
				chdir(currentpath);

				char filewrite[1024];

				char* relative;
				relative = strrchr(argv[i], '/');
				int len1 = snprintf(filewrite, sizeof(filewrite) - 1, "%s%s", filepath, relative);
				filewrite[len1] = 0;
				if (relative == NULL)
				{
					len1 = snprintf(filewrite, sizeof(filewrite) - 1, "%s/%s", filepath, argv[i]);
					filewrite[len1] = 0;
				}
				FILE* destFile = fopen(filewrite, "r");
				if (destFile == NULL)
					destNotExist(argv[i], filewrite);
				else
					destExist(argv[i], filewrite);
			}
		}
	}
	return 0;
}
Beispiel #13
0
int main (int argc, char const * argv []) 

{
	extern struct channel channel;
	static char const * optv [] = 
	{
		"eFi:N:p:P:qS:t:vx",
		"-N file -P file [device] [device] [...]",
		"Qualcomm Atheros Panther/Lynx Powerline Device Bootstrapper",
		"e\tredirect stderr to stdout",
		"F[F]\tFlash [Force] non-volatile memory after boot",

#if defined (WINPCAP) || defined (LIBPCAP)

		"i n\thost interface is (n) [" OPTSTR (CHANNEL_ETHNUMBER) "]",

#else

		"i s\thost interface is (s) [" OPTSTR (CHANNEL_ETHDEVICE) "]",

#endif

		"N f\tfirmware file is (f)",
		"P f\tparameter file is (f)",
		"q\tquiet mode",
		"S f\tsoftloader file is (f)",
		"t n\tread timeout is (n) milliseconds [" OPTSTR (CHANNEL_TIMEOUT) "]",
		"v\tverbose mode",
		"x\texit on error",
		(char const *) (0)
	};

#include "../plc/plc.c"

	char firmware [PLC_VERSION_STRING];
	signed c;
	if (getenv (PLCDEVICE)) 
	{

#if defined (WINPCAP) || defined (LIBPCAP)

		channel.ifindex = atoi (getenv (PLCDEVICE));

#else

		channel.ifname = strdup (getenv (PLCDEVICE));

#endif

	}
	optind = 1;
	while ((c = getoptv (argc, argv, optv)) != -1) 
	{
		switch (c) 
		{
		case 'e':
			dup2 (STDOUT_FILENO, STDERR_FILENO);
			break;
		case 'F':
			_setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
			if (_anyset (plc.flags, PLC_FLASH_DEVICE)) 
			{
				_setbits (plc.module, VS_MODULE_FORCE);
			}
			_setbits (plc.flags, PLC_FLASH_DEVICE);
			break;
		case 'i':

#if defined (WINPCAP) || defined (LIBPCAP)

			channel.ifindex = atoi (optarg);

#else

			channel.ifname = optarg;

#endif

			break;
		case 'N':
			if (!checkfilename (optarg)) 
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1) 
			{
				error (1, errno, "%s", plc.NVM.name);
			}
			if (nvmfile2 (&plc.NVM)) 
			{
				error (1, errno, "Bad NVM file: %s", plc.NVM.name);
			}
			_setbits (plc.flags, PLC_WRITE_MAC);
			break;
		case 'P':
			if (!checkfilename (optarg)) 
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1) 
			{
				error (1, errno, "%s", plc.PIB.name);
			}
			if (pibfile2 (&plc.PIB)) 
			{
				error (1, errno, "Bad PIB file: %s", plc.PIB.name);
			}
			_setbits (plc.flags, PLC_WRITE_PIB);
			break;
		case 'q':
			_setbits (channel.flags, CHANNEL_SILENCE);
			_setbits (plc.flags, PLC_SILENCE);
			break;
		case 'S':
			if (!checkfilename (optarg)) 
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.CFG.file = open (plc.CFG.name = optarg, O_BINARY|O_RDONLY)) == -1) 
			{
				error (1, errno, "%s", plc.CFG.name);
			}
			if (nvmfile2 (&plc.CFG)) 
			{
				error (1, errno, "Bad NVM file: %s", plc.CFG.name);
			}
			break;
		case 't':
			channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
			break;
		case 'v':
			_setbits (channel.flags, CHANNEL_VERBOSE);
			_setbits (plc.flags, PLC_VERBOSE);
			break;
		case 'x':
			_setbits (plc.flags, PLC_BAILOUT);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if (argc) 
	{
		error (1, ENOTSUP, ERROR_TOOMANY);
	}
	openchannel (&channel);
	if (!(plc.message = malloc (sizeof (* plc.message)))) 
	{
		error (1, errno, PLC_NOMEMORY);
	}
	if (WaitForStart (&plc, firmware, sizeof (firmware))) 
	{
		Failure (&plc, PLC_NODETECT);
		exit (1);
	}
	if (plc.hardwareID < CHIPSET_INT6400) 
	{
		Failure (&plc, "Device must be %s or later; Use program int6kboot instead.", chipsetname (CHIPSET_INT6400));
		exit (1);
	}
	if (strcmp (firmware, "BootLoader")) 
	{
		Failure (&plc, "Bootloader must be running");
		exit (1);
	}
	if (plc.PIB.file == -1) 
	{
		error (1, ECANCELED, "No Parameter file named");
	}
	if (plc.NVM.file == -1) 
	{
		error (1, ECANCELED, "No Firmware file named");
	}
	if (plc.CFG.file == -1) 
	{
		if (_anyset (plc.flags, PLC_FLASH_DEVICE)) 
		{
			error (1, ECANCELED, "No Softloader file named");
		}
	}
	if (!InitDevice2 (&plc)) 
	{
		if (!BootDevice2 (&plc)) 
		{
			if (_anyset (plc.flags, PLC_FLASH_DEVICE)) 
			{
				FlashDevice2 (&plc);
			}
		}
	}
	free (plc.message);
	closechannel (&channel);
	exit (0);
}
Beispiel #14
0
int main (int argc, char const * argv [])

{
	extern struct channel channel;
	extern struct key const keys [];
	static char const * optv [] =
	{
		"aB:C:d:D:efFHi:IJ:K:l:mMn:N:p:P:QqrRS:st:Tvw:x",
		"device [device] [...]",
		"Qualcomm Atheros INT6x00 Powerline Device Manager",
		"a\tread device attributes using VS_OP_ATTRIBUTES",
		"B n\tperform pushbutton action (n) using MS_PB_ENC [1|2|3|'join'|'leave'|'status']",
		"C n\tflash NVRAM with module (n) using VS_MOD_NVM [1|2|3|'nvm'|'pib'|'both']",
		"d f\tdump and clear watchdog report to file (f) using VS_WD_RPT",
		"D x\tset DAK to (x) for option -J [" DAK1 "]",
		"e\tredirect stderr to stdout",
		"f\tread NVRAM Configuration using VS_GET_NVM",
		"F[F]\tflash [force] NVRAM with PIB and firmware using VS_MOD_NVM",
		"H\tstop host action requests using VS_HOST_ACTION.IND",

#if defined (WINPCAP) || defined (LIBPCAP)

		"i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",

#else

		"i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",

#endif

		"I\tread device identity using VS_RD_MOD",
		"J x\tset NMK on remote device (x) via local device using VS_SET_KEY (see -K)",
		"K x\tset NMK to (x) for options -J and -M [" NMK1 "]",
		"l n\tloop (n) times [" LITERAL (INT6K_LOOP) "]",
		"m\tread network membership information using VS_NW_INFO",
		"M\tset NMK on local device using VS_SET_KEY (see -K)",
		"n f\tread NVM from SDRAM to file (f) using VS_RD_MOD",
		"N f\twrite NVM file (f) to SDRAM using VS_WR_MOD",
		"p f\tread PIB from SDRAM to file (f) using VS_RD_MOD",
		"P f\twrite PIB file (f) to SDRAM using VS_WR_MOD",
		"q\tquiet mode",
		"Q\tquick flash (return immediately)",
		"r\tread hardware and firmware revision using VS_SW_VER",
		"R\treset device using VS_RS_DEV",
		"s\tread SDRAM Configuration using VS_RD_CBLOCK",
		"S f\twrite an SDRAM Configuration file (f) using VS_SET_SDRAM",
		"t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
		"T\trestore factory defaults using VS_FAC_DEFAULTS",
		"v\tverbose mode",
		"w n\tpause (n) seconds [" LITERAL (INT6K_WAIT) "]",
		"x\texit on error",
		(char const *) (0)
	};

#include "../plc/plc.c"

	signed loop = INT6K_LOOP;
	signed wait = INT6K_WAIT;
	signed c;
	if (getenv (PLCDEVICE))
	{

#if defined (WINPCAP) || defined (LIBPCAP)

		channel.ifindex = atoi (getenv (PLCDEVICE));

#else

		channel.ifname = strdup (getenv (PLCDEVICE));

#endif

	}
	optind = 1;
	while ((c = getoptv (argc, argv, optv)) != -1)
	{
		switch (c)
		{
		case 'a':
			_setbits (plc.flags, PLC_ATTRIBUTES);
			break;
		case 'B':
			_setbits (plc.flags, PLC_PUSH_BUTTON);
			plc.pushbutton = (unsigned)(uintspec (synonym (optarg, buttons, SIZEOF (buttons)), 0, UCHAR_MAX));
			break;
		case 'C':
			_setbits (plc.flags, PLC_FLASH_DEVICE);
			plc.module = (unsigned)(uintspec (synonym (optarg, modules, SIZEOF (modules)), 0, UCHAR_MAX));
			break;
		case 'd':
			_setbits (plc.flags, PLC_WATCHDOG_REPORT);
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.rpt.file = open (plc.rpt.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
			{
				error (1, errno, "%s", plc.rpt.name);
			}

#ifndef WIN32

			chown (optarg, getuid (), getgid ());

#endif

			plc.readaction = 3;
			break;
		case 'D':
			if (!strcmp (optarg, "none"))
			{
				memcpy (plc.DAK, keys [0].DAK, sizeof (plc.DAK));
				break;
			}
			if (!strcmp (optarg, "key1"))
			{
				memcpy (plc.DAK, keys [1].DAK, sizeof (plc.DAK));
				break;
			}
			if (!strcmp (optarg, "key2"))
			{
				memcpy (plc.DAK, keys [2].DAK, sizeof (plc.DAK));
				break;
			}
			if (!hexencode (plc.DAK, sizeof (plc.DAK), (char const *)(optarg)))
			{
				error (1, errno, PLC_BAD_DAK, optarg);
			}
			break;
		case 'e':
			dup2 (STDOUT_FILENO, STDERR_FILENO);
			break;
		case 'f':
			_setbits (plc.flags, PLC_NVRAM_INFO);
			break;
		case 'F':
			_setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
			if (_anyset (plc.flags, PLC_FLASH_DEVICE))
			{
				_setbits (plc.module, VS_MODULE_FORCE);
			}
			_setbits (plc.flags, PLC_FLASH_DEVICE);
			break;
		case 'H':
			_setbits (plc.flags, PLC_HOST_ACTION);
			break;
		case 'I':
			_setbits (plc.flags, PLC_READ_IDENTITY);
			break;
		case 'i':

#if defined (WINPCAP) || defined (LIBPCAP)

			channel.ifindex = atoi (optarg);

#else

			channel.ifname = optarg;

#endif

			break;
		case 'J':
			if (!hexencode (plc.RDA, sizeof (plc.RDA), (char const *)(optarg)))
			{
				error (1, errno, PLC_BAD_MAC, optarg);
			}
			_setbits (plc.flags, PLC_SETREMOTEKEY);
			break;
		case 'K':
			if (!strcmp (optarg, "none"))
			{
				memcpy (plc.NMK, keys [0].NMK, sizeof (plc.NMK));
				break;
			}
			if (!strcmp (optarg, "key1"))
			{
				memcpy (plc.NMK, keys [1].NMK, sizeof (plc.NMK));
				break;
			}
			if (!strcmp (optarg, "key2"))
			{
				memcpy (plc.NMK, keys [2].NMK, sizeof (plc.NMK));
				break;
			}
			if (!hexencode (plc.NMK, sizeof (plc.NMK), (char const *)(optarg)))
			{
				error (1, errno, PLC_BAD_NMK, optarg);
			}
			break;
		case 'M':
			_setbits (plc.flags, PLC_SETLOCALKEY);
			break;
		case 'l':
			loop = (unsigned)(uintspec (optarg, 0, UINT_MAX));
			break;
		case 'm':
			_setbits (plc.flags, PLC_NETWORK);
			break;
		case 'N':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", plc.NVM.name);
			}
			if (nvmfile1 (&plc.NVM))
			{
				error (1, errno, "Bad firmware file: %s", plc.NVM.name);
			}
			_setbits (plc.flags, PLC_WRITE_MAC);
			break;
		case 'n':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
			{
				error (1, errno, "%s", plc.nvm.name);
			}

#ifndef WIN32

			chown (optarg, getuid (), getgid ());

#endif

			_setbits (plc.flags, PLC_READ_MAC);
			break;
		case 'P':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", plc.PIB.name);
			}
			if (pibfile1 (&plc.PIB))
			{
				error (1, errno, "Bad parameter file: %s", plc.PIB.name);
			}
			_setbits (plc.flags, PLC_WRITE_PIB);
			break;
		case 'p':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
			{
				error (1, errno, "%s", plc.pib.name);
			}

#ifndef WIN32

			chown (optarg, getuid (), getgid ());

#endif

			_setbits (plc.flags, PLC_READ_PIB);
			break;
		case 'Q':
			_setbits (plc.flags, PLC_QUICK_FLASH);
			break;
		case 'q':
			_setbits (channel.flags, CHANNEL_SILENCE);
			_setbits (plc.flags, PLC_SILENCE);
			break;
		case 'R':
			_setbits (plc.flags, PLC_RESET_DEVICE);
			break;
		case 'r':
			_setbits (plc.flags, PLC_VERSION);
			break;
		case 'S':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.CFG.file = open (plc.CFG.name = optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", plc.CFG.name);
			}
			if (sdramfile (plc.CFG.file, optarg, plc.flags))
			{
				error (1, ECANCELED, "SDRAM config file %s is corrupt", optarg);
			}
			_setbits (plc.flags, PLC_SDRAM_CONFIG);
			break;
		case 's':
			_setbits (plc.flags, PLC_SDRAM_INFO);
			break;
		case 't':
			channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
			break;
		case 'T':
			_setbits (plc.flags, PLC_FACTORY_DEFAULTS);
			break;
		case 'v':
			_setbits (channel.flags, CHANNEL_VERBOSE);
			_setbits (plc.flags, PLC_VERBOSE);
			break;
		case 'V':
			_setbits (plc.flags, PLC_SNIFFER);
			plc.action = (uint8_t)(uintspec (optarg, 0, UCHAR_MAX));
			break;
		case 'w':
			wait = (unsigned)(uintspec (optarg, 0, 3600));
			break;
		case 'x':
			_setbits (plc.flags, PLC_BAILOUT);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if (argc != 1)
	{
		if (plc.nvm.file != -1)
		{
			error (1, ECANCELED, PLC_NODEVICE);
		}
		if (plc.pib.file != -1)
		{
			error (1, ECANCELED, PLC_NODEVICE);
		}
		if (plc.rpt.file != -1)
		{
			error (1, ECANCELED, PLC_NODEVICE);
		}
	}
	openchannel (&channel);
	if (!(plc.message = malloc (sizeof (* plc.message))))
	{
		error (1, errno, PLC_NOMEMORY);
	}
	if (!argc)
	{
		manager (&plc, loop, wait);
	}
	while ((argc) && (* argv))
	{
		if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
		{
			error (1, errno, PLC_BAD_MAC, * argv);
		}
		manager (&plc, loop, wait);
		argc--;
		argv++;
	}
	free (plc.message);
	closechannel (&channel);
	exit (0);
}
Beispiel #15
0
int main (int argc, char const * argv [])

{
	extern struct channel channel;
	extern void terminate (signo_t);
	static char const * optv [] =
	{
		"dFi:n:N:p:P:qS:t:vw:x",
		"-N file -P file [-S file]",
		"Qualcomm Atheros Panther/Lynx PLC Host Daemon",
		"d\texecute in background as daemon",
		"F [F]\tflash [force] non-volatile memory",

#if defined (WINPCAP) || defined (LIBPCAP)

		"i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",

#else

		"i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",

#endif

		"n f\tread firmware from device into file (f)",
		"N f\tfirmware file is (f)",
		"p f\tread parameters from device into file (f)",
		"P f\tparameter file is (f)",
		"q\tquiet mode",
		"S f\tsoftloader file is (f)",
		"t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
		"v\tverbose mode",
		"w n\twakeup every (n) seconds [" LITERAL (PLC_LONGTIME) "]",
		"x\texit on error",
		(char const *) (0)
	};

#include "../plc/plc.c"

	struct sigaction sa;
	char const * socketname = SOCKETNAME;
	signed c;
	if (getenv (PLCDEVICE))
	{

#if defined (WINPCAP) || defined (LIBPCAP)

		channel.ifindex = atoi (getenv (PLCDEVICE));

#else

		channel.ifname = strdup (getenv (PLCDEVICE));

#endif

	}
	optind = 1;
	plc.timer = PLC_LONGTIME;
	while ((c = getoptv (argc, argv, optv)) != -1)
	{
		switch (c)
		{
		case 'd':
			_setbits (plc.flags, PLC_DAEMON);
			break;
		case 'F':
			_setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
			if (_anyset (plc.flags, PLC_FLASH_DEVICE))
			{
				_setbits (plc.module, VS_MODULE_FORCE);
			}
			_setbits (plc.flags, PLC_FLASH_DEVICE);
			break;
		case 'i':

#if defined (WINPCAP) || defined (LIBPCAP)

			channel.ifindex = atoi (optarg);

#else

			channel.ifname = optarg;

#endif

			break;
		case 'N':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", plc.NVM.name);
			}
			if (nvmfile2 (&plc.NVM))
			{
				error (1, errno, "Bad firmware file: %s", plc.NVM.name);
			}
			_setbits (plc.flags, PLC_WRITE_MAC);
			break;
		case 'n':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
			{
				error (1, errno, "%s", plc.nvm.name);
			}
			break;
		case 'P':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", plc.PIB.name);
			}
			if (pibfile2 (&plc.PIB))
			{
				error (1, errno, "Bad parameter file: %s", plc.PIB.name);
			}
			_setbits (plc.flags, PLC_WRITE_PIB);
			break;
		case 'p':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
			{
				error (1, errno, "%s", plc.pib.name);
			}
			break;
		case 'q':
			_setbits (channel.flags, CHANNEL_SILENCE);
			_setbits (plc.flags, PLC_SILENCE);
			break;
		case 'S':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.CFG.file = open (plc.CFG.name = optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", plc.CFG.name);
			}
			if (nvmfile2 (&plc.CFG))
			{
				error (1, errno, "Bad firmware file: %s", plc.CFG.name);
			}
			break;
		case 't':
			channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
			break;
		case 'v':
			_setbits (channel.flags, CHANNEL_VERBOSE);
			_setbits (plc.flags, PLC_VERBOSE);
			break;
		case 'w':
			plc.timer = (unsigned)(uintspec (optarg, 1, 3600));
			break;
		case 'x':
			_setbits (plc.flags, PLC_BAILOUT);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if (argc)
	{
		error (1, ENOTSUP, ERROR_TOOMANY);
	}
	if (plc.NVM.file == -1)
	{
		error (1, ECANCELED, "No host NVM file named");
	}
	if (plc.PIB.file == -1)
	{
		error (1, ECANCELED, "No host PIB file named");
	}
	if (plc.CFG.file == -1)
	{
		if (_anyset (plc.flags, PLC_FLASH_DEVICE))
		{
			error (1, ECANCELED, "No Softloader file named");
		}
	}
	if (plc.nvm.file == -1)
	{
		if ((plc.nvm.file = open (plc.nvm.name = NEWNVM, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
		{
			error (1, errno, "%s", plc.nvm.name);
		}
	}
	if (plc.pib.file == -1)
	{
		if ((plc.pib.file = open (plc.pib.name = NEWPIB, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
		{
			error (1, errno, "%s", plc.pib.name);
		}
	}

#ifndef WIN32

	if (_anyset (plc.flags, PLC_DAEMON))
	{
		pid_t pid = fork ();
		if (pid < 0)
		{
			error (1, errno, "Can't start daemon");
		}
		if (pid > 0)
		{
			exit (0);
		}
	}
	memset (&sa, 0, sizeof (struct sigaction));
	sa.sa_handler = terminate;
	sigaction (SIGTERM, &sa, (struct sigaction *)(0));
	sigaction (SIGQUIT, &sa, (struct sigaction *)(0));
	sigaction (SIGTSTP, &sa, (struct sigaction *)(0));
	sigaction (SIGINT, &sa, (struct sigaction *)(0));
	sigaction (SIGHUP, &sa, (struct sigaction *)(0));

#endif

	openchannel (&channel);
	if (!(plc.message = malloc (sizeof (* plc.message))))
	{
		error (1, errno, PLC_NOMEMORY);
	}
	function (&plc, socketname);
	free (plc.message);
	closechannel (&channel);
	exit (0);
}
Beispiel #16
0
int main (int argc, char const * argv [])

{
	extern struct channel channel;
	static char const * optv [] =
	{
		"Fi:n:N:p:P:qt:vx",
		"-N file -P file [-n file] [-p file]",
		"Qualcomm Atheros INT6400 Host Emulator",
		"F[F]\tflash (force) NVRAM using VS_MOD_NVM",

#if defined (WINPCAP) || defined (LIBPCAP)

		"i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",

#else

		"i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",

#endif

		"n f\tread NVM from device into file using VS_RD_MOD",
		"N f\twrite NVM file to device using VS_WR_MEM",
		"p f\tread PIB from device into file using VS_RD_MOD",
		"P f\twrite PIB file to device using VS_WR_MEM",
		"q\tquiet mode",
		"t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
		"v\tverbose mode",
		"x\texit on error",
		(char const *) (0)
	};

#include "../plc/plc.c"

	signed c;
	if (getenv (PLCDEVICE))
	{

#if defined (WINPCAP) || defined (LIBPCAP)

		channel.ifindex = atoi (getenv (PLCDEVICE));

#else

		channel.ifname = strdup (getenv (PLCDEVICE));

#endif

	}
	optind = 1;
	while ((c = getoptv (argc, argv, optv)) != -1)
	{
		switch (c)
		{
		case 'i':

#if defined (WINPCAP) || defined (LIBPCAP)

			channel.ifindex = atoi (optarg);

#else

			channel.ifname = optarg;

#endif

			break;
		case 'q':
			_setbits (channel.flags, CHANNEL_SILENCE);
			break;
		case 't':
			channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
			break;
		case 'v':
			_setbits (channel.flags, CHANNEL_VERBOSE);
			break;
		}
	}
	openchannel (&channel);
	desuid ();
	optind = 1;
	while ((c = getoptv (argc, argv, optv)) != -1)
	{
		switch (c)
		{
		case 'F':
			_setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
			if (_anyset (plc.flags, PLC_FLASH_DEVICE))
			{
				_setbits (plc.module, VS_MODULE_FORCE);
			}
			_setbits (plc.flags, PLC_FLASH_DEVICE);
			break;
		case 'N':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", plc.NVM.name);
			}
			if (nvmfile1 (&plc.NVM))
			{
				error (1, errno, "Bad firmware file: %s", plc.NVM.name);
			}
			_setbits (plc.flags, PLC_WRITE_MAC);
			break;
		case 'n':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
			{
				error (1, errno, "%s", plc.nvm.name);
			}
			break;
		case 'P':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
			{
				error (1, errno, "%s", plc.PIB.name);
			}
			if (pibfile1 (&plc.PIB))
			{
				error (1, errno, "Bad parameter file: %s", plc.PIB.name);
			}
			_setbits (plc.flags, PLC_WRITE_PIB);
			break;
		case 'p':
			if (!checkfilename (optarg))
			{
				error (1, EINVAL, "%s", optarg);
			}
			if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
			{
				error (1, errno, "%s", plc.pib.name);
			}
			break;
		case 'q':
			_setbits (plc.flags, PLC_SILENCE);
			break;
		case 'v':
			_setbits (plc.flags, PLC_VERBOSE);
			break;
		case 'x':
			_setbits (plc.flags, PLC_BAILOUT);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if (argc)
	{
		error (1, ENOTSUP, ERROR_TOOMANY);
	}
	if (plc.NVM.file == -1)
	{
		error (1, ECANCELED, "No host NVM file named");
	}
	if (plc.PIB.file == -1)
	{
		error (1, ECANCELED, "No host PIB file named");
	}
	if (plc.nvm.file == -1)
	{
		if ((plc.nvm.file = open (plc.nvm.name = NEWNVM, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
		{
			error (1, errno, "%s", plc.nvm.name);
		}
	}
	if (plc.pib.file == -1)
	{
		if ((plc.pib.file = open (plc.pib.name = NEWPIB, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
		{
			error (1, errno, "%s", plc.pib.name);
		}
	}
	if (!(plc.message = malloc (sizeof (* plc.message))))
	{
		error (1, errno, PLC_NOMEMORY);
	}
	function (&plc);
	free (plc.message);
	closechannel (&channel);
	exit (0);
}