Exemplo n.º 1
0
static void usage()
{ int i,width=0;
  PRINT("Usage: %s [options]",basename(ARGV[0]));
  for(i=0;i<countof(ARGS);++i)
    PRINT(" <%s>",ARGS[i].name);
  
  for(i=0;i<countof(ARGS);++i)
  { int c=0;
#define WRITE(...) c+=snprintf(ARGS[i].state.buf+c,sizeof(ARGS[i].state.buf)-c,__VA_ARGS__)
    WRITE("    %s",ARGS[i].name);
#undef WRITE
  }
  

  // left column of help text
  for(i=0;i<countof(SPEC);++i)
  { int c=0;
#define WRITE(...) c+=snprintf(SPEC[i].state.buf+c,sizeof(SPEC[i].state.buf)-c,__VA_ARGS__)
    WRITE("    %s",SPEC[i].shortname);
    if(SPEC[i].longname)
      WRITE(" [%s]",SPEC[i].longname);
    if(!SPEC[i].is_flag)
    { WRITE(" arg");
      if(SPEC[i].def)
        WRITE(" (=%s)",SPEC[i].def);
    }
#undef WRITE
  }
  // width of left column
  for(i=0;i<countof(ARGS);++i)
  { int n=(int)strlen(ARGS[i].state.buf);
    width=(n>width)?n:width;
  }
  for(i=0;i<countof(SPEC);++i)
  { int n=(int)strlen(SPEC[i].state.buf);
    width=(n>width)?n:width;
  }

  PRINT("\nArguments:\n");
  for(i=0;i<countof(ARGS);++i)
    writehelp(MAXWIDTH,ARGS[i].state.buf,width,ARGS[i].help);

  PRINT("\nOptions:\n");
  for(i=0;i<countof(SPEC);++i)
    writehelp(MAXWIDTH,SPEC[i].state.buf,width,SPEC[i].help);
  
  printf("\n");
}
Exemplo n.º 2
0
takeclient(struct sockaddr_in destination, int SDfromClient)
{
 int newSD;
  char messageFromClient[255]; char messageToClient[255];//aka pid #
  if ( (newSD=accept(SDfromClient, NULL, NULL)) < 0  )
    {
      printf ("accept failed\n");
      return 0;
    } 
  else {
    printf("Accepting connections...\n");  
  }
  while(1)
    {
      strcpy(&messageFromClient,(char *)readhelp(newSD,&messageFromClient,255));
      printf("Message from client: %s\n", messageFromClient);
      // strcpy(&messageToClient, &messageFromClient);
      // printf("messageToClient is: %s and length %d \n", messageToClient, strlen(messageToClient)+1);
      fgets(messageToClient,255,stdin);
      writehelp(newSD, messageToClient, strlen(messageToClient)+1);
    }
  close(newSD);

}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
	//deal with commandline arguments
	char incmd[200];


	//analyze commandline args
	if( (argc<2) || strcmp(argv[1],"-help")==0 || strcmp(argv[1],"--help")==0  ) {
		printf("argc=%d\n", argc);
		writehelp();
		exit(0);
	}
	int i;
	strcpy(incmd, argv[1]);
	if( strcmp(argv[1], "stop")==0) strcpy(incmd, "-Q");
	for(i=2; i<argc; i++) {
		strcat(incmd," ");
		strcat(incmd,argv[i]);
	}

	if(argc > 1 && strcmp(argv[1], "start")==0) system("mptcp_proxy &");

	//make down FIFO
	mkfifo(FIFO_NAME_DOWN, (mode_t) 0666);

	//open fifo
	int fd_down = open(FIFO_NAME_DOWN, O_WRONLY | O_NONBLOCK);

	//send data
	int ret = write(fd_down, incmd, strlen(incmd));
	if(ret < -1){
		printf("mpproxy: writing to pipe failed\n");
		exit(0);
	}
	close(fd_down);


	//make up FIFO
	mkfifo(FIFO_NAME_UP, (mode_t) 0666);

	//open fifo
	int fd_up = open(FIFO_NAME_UP, O_RDONLY | O_NONBLOCK);

	fd_set fds_test;
	fd_set fds_input;
	FD_ZERO(&fds_input);		 
	FD_SET(fd_up, &fds_input);
	fds_test = fds_input;
	struct timeval timeout;
	timeout.tv_sec = 1; 
	timeout.tv_usec = 0;

	char buf[LEN_FIFO_MSG+1];
	int fd;
	int rtn = select(FD_SETSIZE, &fds_test, (fd_set *) NULL, (fd_set *) NULL, &timeout);
	if(rtn == -1) {
		printf("mpproxy: select returns=%d, exit program!\n",rtn);	
		exit(1);
	}
	
	for(fd = 0; fd < FD_SETSIZE; fd++){
		if(FD_ISSET(fd, &fds_test)){

			if (fd == fd_up){
				ret = read(fd_up, buf, LEN_FIFO_MSG);
				buf[ret] = '\0';
				if(ret < -1){
					printf("mpproxy: reading from pipe failed\n");
					close(fd_up);
					exit(0);
				}
				close(fd_up);
				if(buf[0] != '\0') printf("%s\n", buf);
			}
		}//end if F
	}
}