static void try_to_access_ports()
{
    int i;

    for (i = 0; i <= 65535; i++) {
        if (total_ports_tcp[i] && connect_to_port(IPPROTO_TCP, i)) {
            char port_proto[64];

            if (_ports_open == 0) {
                snprintf(port_proto, 64, "\n      %d (tcp),", i);
            } else {
                snprintf(port_proto, 64, "%d (tcp),", i);
            }
            strncat(open_ports_str, port_proto, open_ports_size);
            open_ports_size -= strlen(port_proto) + 1;

            _ports_open++;
        }

        if (total_ports_udp[i] && connect_to_port(IPPROTO_UDP, i)) {
            char port_proto[64];

            if (_ports_open == 0) {
                snprintf(port_proto, 64, "\n      %d (udp),", i);
            } else {
                snprintf(port_proto, 64, "%d (udp),", i);
            }

            strncat(open_ports_str, port_proto, open_ports_size);
            open_ports_size -= strlen(port_proto) + 1;

            _ports_open++;
        }

        if (_ports_open >= 4) {
            _ports_open = 0;
        }
    }

}
Beispiel #2
0
/* sequence of operations

   PE 0                      PE 1            .......     PE last

   accept                    accept                      NO-OP
   NO-OP                     connect(PE 0)               connect(PE last-1)
   NO-OP                     read(PE 0)                  read(PE last-1)
   write(PE 1)               write(PE 2)                 NO-OP
   read(PE 1)                read(PE 2)                  NO-OP
   NO-OP                     write(PE 0)                 write(PE last-1)
   NO-OP                     close(PE 0)                 close(PE last-1)
   close(PE 1)               close(PE 2)                 NO-OP

*/
int f77name(rpn_comm_softbarrier)(ftnword *comm)   /* perform a soft sync */
{
  char buf[1024];
  int fdesc_up=-1, fdesc_down=-1;
  int status=-1;
  struct set_of_ports *p=chain;
  
  while ( (p!=NULL) && (p->comm != *comm) ) p = p->next ;
  if ( p == NULL ) return(-1);
  
  f77name(save_openmp_state)();
#ifdef DEBUG
  printf("Entering rpn_comm_soft_sync, PE=%d\n",p->pe_me);
  fflush(stdout);
  if(p->pe_me == 0) sleep(1);
#endif
  if(p->pe_me != p->nprocs-1) fdesc_down=accept_from_sock(p->my_server);
#ifdef DEBUG
  if(p->pe_me != p->nprocs-1) printf("PE=%d, accept_from_sock,fdesc_down=%d\n",p->pe_me,fdesc_down);
  fflush(stdout);
#endif
  if(p->pe_me != 0){
#ifdef DEBUG
    printf("PE=%d,connect_to_port %d@%x\n",p->pe_me,p->list_port[p->pe_me-1],p->list_server[p->pe_me-1]);
#endif
    fdesc_up=connect_to_port(p->list_server[p->pe_me-1],p->list_port[p->pe_me-1]);
#ifdef DEBUG
    printf("PE=%d,connected_to_port %d@%x, desc=%d\n",p->pe_me,p->list_port[p->pe_me-1],p->list_server[p->pe_me-1],fdesc_up);
#endif
    read(fdesc_up,buf,4);
    }
  if(p->pe_me != p->nprocs-1) write(fdesc_down,buf,4);

  if(p->pe_me != p->nprocs-1) read(fdesc_down,buf,4);
  if(p->pe_me != 0) write(fdesc_up,buf,4);

  if(fdesc_up != -1) close(fdesc_up);
  if(fdesc_down != -1) close(fdesc_down);
  f77name(restore_openmp_state)();
  return(MPI_SUCCESS);
}
Beispiel #3
0
int main(int argc,char** argv)
{
  if (argc>1) openSerialPort(argv[1]);

  bzero(last_raster_crc,sizeof(unsigned int)*1200);

  rfbScreenInfoPtr rfbScreen = rfbGetScreen(&argc,argv,maxx,maxy,8,3,bpp);
  if(!rfbScreen)
    return 0;
  rfbScreen->desktopName = "C65GS Remote Display";
  rfbScreen->frameBuffer = (char*)malloc(maxx*maxy*bpp);
  rfbScreen->alwaysShared = TRUE;
  rfbScreen->kbdAddEvent = dokey;
  rfbScreen->newClientHook = newclient;
  rfbScreen->httpDir = "../webclients";
  rfbScreen->httpEnableProxyConnect = TRUE;

  initBuffer((unsigned char*)rfbScreen->frameBuffer);

  /* initialize the server */
  rfbInitServer(rfbScreen);

  /* this is the non-blocking event loop; a background thread is started */
  rfbRunEventLoop(rfbScreen,-1,TRUE);
  fprintf(stderr, "Running background loop...\n");

  int sock = connect_to_port(6565);
  if (sock==-1) {
    fprintf(stderr,"Could not connect to video proxy on port 6565.\n");
    exit(-1);
  }

  raster_cache=calloc(sizeof(struct raster_cache),65536);
  {
    int j;
    for(j=0;j<1200;j++) raster_crc[j]=0;
  }

  printf("Allocated raster cache.\n");

  printf("Started.\n"); fflush(stdout);

    int last_colour=0x00;
    int in_vblank=0;
    int firstraster=1;
    int bytes=0;

    int rasternumber;
    int last_raster=0;
    int next_rasternumber=0;

    int raster_low=0;
    int raster_high=1200;

    while(1) {
      int i;
      unsigned char packet[8192];
      int len=read(sock,packet,2132);
      if (len<1) usleep(10000);

      if (1) {
	if (len > 2100) {
	  // probably a C65GS compressed video frame.
	  // printf("."); fflush(stdout);

	  unsigned char *raster_data=NULL;
    
	  // Each should consist of 13 blocks of
	  //   raster_number (16 bits)
	  //   audio left (8 bits)
	  //   audio right (8 bits)
	  //   CRC32 of raster (32 bits)
	  //   dummy byte (8 bits)

	  // One of these should have the MSB set in the raster number 
	  // to indicate that a raw raster follows.

	  unsigned int crc;
	  int offset=0x56;

	  for(i=0;i<13;i++) {
	    rasternumber = packet[offset+0]<<8;
	    rasternumber |= packet[offset+1];
	    rasternumber &= 0xfff;

	    next_rasternumber = packet[offset+9+0]<<8;
	    next_rasternumber |= packet[offset+9+1];
	    next_rasternumber &= 0xfff;

	    if (rasternumber==1199) {
	      updateFrameBuffer(rfbScreen);
	    }

	    crc=packet[offset+4]<<0;
	    crc|=packet[offset+5]<<8;
	    crc|=packet[offset+6]<<16;
	    crc|=packet[offset+7]<<24;
	   
	    // printf("i=% 2d@$%03x: Saw raster $%04x, crc=$%08x\n",i,offset,rasternumber,crc);
	    
	    // check validity of raster number
	    if (rasternumber>=0&&rasternumber<1200) {
	      if (((!i)&&next_rasternumber-1==rasternumber)||(last_raster+1==rasternumber)) { // rasternumber>=raster_low&&rasternumber<=raster_high) {
		// remember CRC for this raster
		raster_crc[rasternumber]=crc;
		if (raster_data) {
		  // we have raster data, so update the cache
		  bcopy(raster_data,raster_cache[crc&0xffff].data,1920);
		  raster_cache[crc&0xffff].crc=crc;
		}
		
		// describe acceptable raster range for next for supressing glitches
		raster_low=rasternumber+1; if (raster_low>=1200) raster_low=0;
		raster_high=rasternumber+50; if (raster_high>=1200) raster_high=1200;
	      } else {
		// printf("  rejected (acceptable range is %d -- %d)\n",raster_low,raster_high);
		break;
	      }
	    }
	    
	    last_raster=rasternumber;

	    // keep pointer to and skip raster data if it follows this line
	    if (packet[offset+0]&0x80) {
	      raster_data=&packet[offset+9];
	      offset+=1920+9;
	    } else {
	      offset+=9;
	      raster_data=NULL;
	    }
	  }
	}
      }      
    }

  free(rfbScreen->frameBuffer);
  rfbScreenCleanup(rfbScreen);

  return(0);
}
Beispiel #4
0
int main(int argn, char** args){
	if(argn!=5){
		print("use this format: ./Client name server_ip router_ip router_port\n");
		return 0;
	}
	char* client_name = args[1];
	char* client_ip=NULL;
	char* server_ip= args[2];
	char* router_port= args[4];
	char server_data [MAX_STR_SIZE];
	clear_buff(server_data,MAX_STR_SIZE);
	int ip_input_tokens_num;
	char ip_input_tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE];
	char ip_input_buffer[MAX_STR_SIZE];
	clear_buff(ip_input_buffer, MAX_STR_SIZE);
	while( read(STDINFD, ip_input_buffer, MAX_STR_SIZE) > 0 ){	
		tokenizer(ip_input_buffer, " ", &ip_input_tokens_num, ip_input_tokens);
		if(mystrcmp(ip_input_tokens[0], "Set") == 0 && mystrcmp(ip_input_tokens[1], "IP") == 0 && ip_input_tokens_num == 3){
			client_ip=ip_input_tokens[2];
			replace_char(client_ip,'\n','\0');
			break;
		}else{
			print("You should set your IP first : Set IP 115.2.2.18\n");
		}
	}
	int fd = connect_to_port(atoi(router_port));
	char input_buffer[MAX_STR_SIZE];
	clear_buff(input_buffer, MAX_STR_SIZE);
	while(read(STDINFD, input_buffer, MAX_STR_SIZE) > 0)
	{	
		int input_tokens_num;
		char input_tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE];
		replace_char(input_buffer,'\n','\0');
		char iden_buff[MAX_STR_SIZE];
		clear_buff(iden_buff, MAX_STR_SIZE);
		strcat(iden_buff, "00");//00 is for client
		//parsing input_buffer
		tokenizer(input_buffer," ", &input_tokens_num, input_tokens);
		
		char frame[MAX_STR_SIZE];
		clear_buff(frame, MAX_STR_SIZE);
		char data[MAX_STR_SIZE];
		clear_buff(data, MAX_STR_SIZE);
		
		//send command for server
		if( (mystrcmp(input_tokens[0], "DC") < 0) && (mystrcmp(input_tokens[0], "Logout") < 0) )
		{
			if( strcmp(input_tokens[0],"Send")==0 && strcmp(input_tokens[1],"message")==0  && input_tokens_num==3){
				strcat(data,"Sent message: ");
				strcat(data,input_tokens[2]);
			} else if(strcmp(input_tokens[0],"Send")==0 && strcmp(input_tokens[1],"file")==0 && input_tokens_num==3){
				if(!file_exist(input_tokens[2])){
					printf("file %s doesn't exist! You should create this file near ./Client 's directory\n", input_tokens[2]);
					continue;
				}
				char file_content[MAX_STR_SIZE];
				clear_buff(file_content,MAX_STR_SIZE);
				read_entire_file(input_tokens[2],file_content);
				replace_char(file_content,'\n','^');
				strcat(data,"Sent file ");
				strcat(data,input_tokens[2]);
				strcat(data," contain:\n");
				strcat(data,file_content);
			}
			else
				strcat(data,input_buffer);

			framing(iden_buff,"0",client_ip,client_name,data,"cccc","cc",frame);
			printf("sent frame is: %s\n",frame );
			int bytes_written = write(fd, frame, strlength(frame));
			if(bytes_written < 0)
				write(STDOUTFD,"Error on writing\n", sizeof("Error on writing\n"));

			//get response from server
			char res_status[MAX_STR_SIZE];
			clear_buff(res_status, MAX_STR_SIZE);
			int read_status = read(fd, res_status, MAX_STR_SIZE);
			//show the response to client
			printf("status of result of first reading ...\n");
			write(STDOUTFD, res_status, strlength(res_status));
			printf("\n");

			char res_buff[MAX_STR_SIZE];
			clear_buff(res_buff, MAX_STR_SIZE);
			read_status = read(fd, res_buff, MAX_STR_SIZE);
			process_client_command(res_buff,server_data );
			printf("result is:\n%s\n",res_buff );
			int input_tokens_num;
			char input_tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE];
			tokenizer(res_buff, "&", &input_tokens_num, input_tokens);
			//show the response to client
			write(STDOUTFD, input_tokens[5], strlength(input_tokens[5]));
			printf("\n");
		}
		else if(mystrcmp(input_tokens[0], "DC") == 0)
		{
			int bytes_written = write(fd, "DC", strlength("DC"));
			if(bytes_written < 0)
				write(STDOUTFD,"Error on writing\n", sizeof("Error on writing\n"));
			break;
		}
		else if(mystrcmp(input_tokens[0], "Logout") == 0)
		{
			int bytes_written = write(fd,"DC", strlength("DC"));
			if(bytes_written < 0)
				write(STDOUTFD,"Error on writing\n", sizeof("Error on writing\n"));
			close(fd);
			return 0;
		}
	}
	close(fd);
	return 0;
}
Beispiel #5
0
static bool _FrExecProgram(const char *hostname, int portnum,
			   int &pipe_in, int &pipe_out,
			   istream *&stream_in, ostream *&stream_out,
			   ostream &err,
			   const char *progname, char **arglist)
{
   set_child_pid(-1) ;
   if (progname && *progname)
      {
      bool result ;
      if (portnum > 0)
	 {
#ifdef FrUSING_POPEN
	 result = popen_program(arglist,err) ;
	 if (result)
#else
	 int sp ;
         sp = spawnvp(P_NOWAIT,arglist[0],(char const * const *)arglist) == 0 ;
	 if (sp == 0)
#endif /* FrUSING_POPEN */
	    {
	    result = true ;
	    // wait for the child program to contact us
	    FrSocket sock = FrAwaitConnection(portnum, FrPOPEN_LOAD_TIMEOUT,
					      err,false) ;
	    if (sock != (FrSocket)INVALID_SOCKET)
	       {
	       pipe_in = pipe_out = sock ;
	       stream_in = new FrISockStream(sock) ;
	       stream_out = new FrOSockStream(sock) ;
	       if (!stream_in || !stream_in->good())
		  result = false ;
	       // the child program is supposed to send us a line identifying
	       // itself
	       if (FramepaC_verbose)
		  {
		  char line[MAX_CMDLINE] ;
		  stream_in->getline(line,sizeof(line)) ;
		  err << "; Connected: " << line << endl ;
		  }
	       else
		  stream_in->ignore(MAX_CMDLINE,'\n') ;
	       }
	    else
	       result = false ;
	    // close the listening socket to avoid having it hang around
	    (void)FrAwaitConnection(-1,0,err,false) ;
	    }
	 }
      else
	 {
	 result = fork_program(arglist,pipe_in,pipe_out) ;
	 if (result)
	    {
	    stream_in = Fr_ifstream(pipe_in) ;
	    stream_out = Fr_ofstream(pipe_out) ;
	    }
	 }
      return result ;
      }
   else
      return connect_to_port(hostname,portnum,pipe_in, pipe_out,
			     stream_in,stream_out) ;
}