⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client_handler.c

📁 NAT打洞
💻 C
📖 第 1 页 / 共 2 页
字号:
    ){
        return proxy_bound_ok_replay(cd, nsize, sa);
    }
    else if(
        (sa.sin_addr.s_addr == cd->addr.sin_addr.s_addr)
        && (sa.sin_port == cd->addr.sin_port)
    ){ // from our client
#ifdef AUTH
            if(!cd->type.udp.control->authorized){
                    return unauthorized(cd, proxyAuth1);
        }
#endif
        return resend_packet(cd, buf, nsize);
    }
    else { // to our client
if(verbose){
printf("[%s,%d] %s:%d\n", __FILE__, __LINE__, inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
}
        return receive_packet(cd, nsize, sa);
    }
  }
  return 0;

}

int timer_event(void* arg){
        if(!timer_disabled){
        if((now - last_registration) > registration_period){
                timer_disabled = 1;
          last_registration = now;
                register_on_server();
    }
  }
  else {
        if((now - last_registration) > registration_timeout){
                timer_disabled = 0;
                closesocket(server->sock);
      rm_socket(server->sock, sock_data, &master, MASTER_ALL);
    }
  }
        return 0;
}

int proxy_ping_reply(CLIENT_DATA *cd){
        int nsize;

  delay(FW_DELAY_MSEC);   /* delay 10 ms */
  if(send(cd->sock, buf, strlen(buf), 0)
#if defined(WIN32) || defined(__WIN32__)
      != SOCKET_ERROR
#else
      >= 0
#endif
  ){
    if(verbose){
            printf("[%s:%d] <<< %s", inet_ntoa(cd->addr.sin_addr), ntohs(cd->addr.sin_port), buf);
    }
  }
  return 0;
}

#if defined(WIN32) || defined(__WIN32__)
unsigned __stdcall
#else
void*
#endif
binding(void* arg){
        int nsize;
  char buff[1024];
        CLIENT_DATA *cd = (CLIENT_DATA *)arg;
  CLIENT_DATA *control = cd->type.udp.control;
  struct sockaddr_in my_addr;
  unsigned short proxy_port;
  my_addr.sin_family = AF_INET;
  my_addr.sin_addr.s_addr = INADDR_ANY;
  for(proxy_port = PROXY_SRC_PORT_MIN; proxy_port < PROXY_SRC_PORT_MAX; proxy_port++){

    my_addr.sin_port = htons(proxy_port);
    if (bind(cd->sock,(struct sockaddr *)&my_addr,
      sizeof(struct sockaddr_in)) == 0){
      break;
    }
    if(
#if defined(WIN32) || defined(__WIN32__)
    WSAGetLastError() != WSAEADDRINUSE
#else
    errno != EADDRINUSE
#endif
    ){
      sprintf(buff, "%s: %s; %d\n", proxyBindError, error_id(), __LINE__);
            closesocket(cd->sock);
          free(cd);
      goto end;
    }
  }
  if(proxy_port == PROXY_SRC_PORT_MAX){
#if defined(WIN32) || defined(__WIN32__)
      WSASetLastError(WSAEADDRINUSE)
#else
      errno = EADDRINUSE
#endif
    ;
    sprintf(buff, "%s: %s; %d\n", proxyBindError, error_id(), __LINE__);
    closesocket(cd->sock);
    free(cd);
  }
  else {
    sprintf(buff, "%s: %d; %d; %s\n", proxyBound, cd->sock, proxy_port,
#ifdef OVER_TCP
                        "OVER_TCP=Y"
#else
                        "OVER_TCP=N"
#endif
    );
    cd->step = CLIENT_REQUEST;
    add_socket(cd->sock, sock_data, cd, &master, MASTER_READ);
  }
  end:
#if defined(WIN32) || defined(__WIN32__)
  memcpy(control->type.client.buff, buff, strlen(buff));
#else
  bcopy(buff, control->type.client.buff, strlen(buff));
#endif

  control->type.client.nsize = strlen(buff);
  add_socket(control->sock, sock_data, control, &master, MASTER_WRITE);
  return
#if defined(WIN32) || defined(__WIN32__)
                0
#else
                NULL
#endif
        ;
}

int proxy_bind_reply(CLIENT_DATA *cd){
  SOCKET client_socket;
  SOCKET sock;
  int type;
  int protocol;
  struct sockaddr_in client_sa;

  CLIENT_DATA *udp_client;
  int i = 0;
  char *p = tokenize_cmd(buf);
  while((p != NULL) && (i < 3)){
    switch(i){
      case 0:
                          client_socket = atoi(p);
        break;
      case 1:
                          type = atoi(p);
        break;
      case 2:
                          protocol = atoi(p);
        break;
    }
    i++;
    p = tokenize_cmd(NULL);
  }
  client_sa.sin_family = AF_INET;
  client_sa.sin_addr = cd->addr.sin_addr;
  client_sa.sin_port = htons(0);
  if((sock = socket(AF_INET, type, protocol))
#if defined(WIN32) || defined(__WIN32__)
    == INVALID_SOCKET
#else
    < 0
#endif
  ){
    sprintf(buf, "%s: %s; %d\n", proxyBindError, error_id(), __LINE__);
    delay(FW_DELAY_MSEC);   /* delay 10 ms */
    if(send(cd->sock, buf, strlen(buf), 0) < 0){
            if(verbose){
            handle_error(__FILE__, __LINE__);
      }
    }
    else {
            if(verbose){
            printf("[%s:%d] <<< %s", inet_ntoa(cd->addr.sin_addr), ntohs(cd->addr.sin_port), buf);
        }
    }
    return 0;
  }

  udp_client = (CLIENT_DATA *)get_client_data(sock, client_sa);
  udp_client->type.udp.control = cd;
  udp_client->type.udp.client_socket = client_socket;
  udp_client->client_type = CLIENT_TYPE_UDP;
#if defined(WIN32) || defined(__WIN32__)
        {
    unsigned thid;
                _beginthreadex(NULL, 0, &binding, udp_client, 0, &thid);
  }
#else
        {
        pthread_t thread;
        pthread_create(&thread, NULL, &binding, udp_client);
  }
#endif
  return 0;
}

int proxy_close_reply(CLIENT_DATA *cd){
  int i = 0;
  CLIENT_DATA *udp;
  char *p = tokenize_cmd(buf);
  while((p != NULL) && (i < 1)){
    switch(i){
      case 0:
                          udp = (CLIENT_DATA *)hashmap_get(sock_data, atoi(p));
        break;
    }
    i++;
    p = tokenize_cmd(NULL);
  }
  if(udp != NULL){
    if(verbose){
      printf("close: %d\n", udp->sock);
    }
    rm_socket(udp->sock, sock_data, &master, MASTER_ALL);
  }
  return 0;
}

int proxy_bound_ok_replay(CLIENT_DATA *cd, int nsize, struct sockaddr_in sa){
  cd->addr = sa;
#ifdef AUTH
  if(!cd->type.udp.control->authorized){
    return unauthorized(cd, proxyAuth1);
        }
#endif
  delay(FW_DELAY_MSEC);   /* delay 10 ms */
  if((nsize = send(cd->type.udp.control->sock, buf + sizeof(struct sockaddr_in),
    strlen(buf + sizeof(struct sockaddr_in)), 0))
#if defined(WIN32) || defined(__WIN32__)
      != SOCKET_ERROR
#else
      >= 0
#endif
  ){
    if(verbose){
      printf("[%s:%d] <<< %s\n",
        inet_ntoa(cd->type.udp.control->addr.sin_addr),
        ntohs(cd->type.udp.control->addr.sin_port), buf + sizeof(struct sockaddr_in)
      );
    }
  }
  else {
    if(verbose){
        handle_error(__FILE__, __LINE__);
    }
  }
  return 0;
}

int resend_packet(CLIENT_DATA *cd, char *buff, int nsize){
  struct sockaddr_in sa;

#if defined(WIN32) || defined(__WIN32__)
  memcpy(&sa, buff, sizeof(struct sockaddr_in));
#else
  bcopy(buff, &sa, sizeof(struct sockaddr_in));
#endif

        if(ntohs(sa.sin_port) <= 1024){
          return 0;
  }

  nsize = sendto(cd->sock, buff + sizeof(struct sockaddr_in),
      nsize - sizeof(struct sockaddr_in), 0,
      (struct sockaddr*)&sa, sizeof(struct sockaddr_in));
  if(nsize
#if defined(WIN32) || defined(__WIN32__)
      !=SOCKET_ERROR
#else
      >= 0
#endif
  ){
    if(verbose){
      printf("[%s,%d] [%s:%d] <<< (%d)\n", __FILE__, __LINE__, inet_ntoa(sa.sin_addr), ntohs(sa.sin_port), nsize);
    }
  }
  else {
    if(verbose){
                        handle_error(__FILE__, __LINE__);
    }
  }
  return 0;
}

int receive_packet(CLIENT_DATA *cd, int nsize, struct sockaddr_in sa){
#ifdef OVER_TCP
        if(cd->type.udp.control->type.client.over_tcp){
        return receive_over_tcp(cd, nsize, sa);
  }
  else
#endif
  {
    memmove(buf + sizeof(struct sockaddr_in), buf, nsize);
#if defined(WIN32) || defined(__WIN32__)
    memcpy(buf, &sa, sizeof(struct sockaddr_in));
#else
    bcopy(&sa, buf, sizeof(struct sockaddr_in));
#endif
    if(
      (
      nsize = sendto(cd->sock, buf,
      nsize + sizeof(struct sockaddr_in)
      , 0,
        (struct sockaddr*)&cd->addr, sizeof(struct sockaddr_in))
    )
        #if defined(WIN32) || defined(__WIN32__)
        !=SOCKET_ERROR
  #else
        >= 0
  #endif
    ){
            if(verbose){
            printf("[%s:%d] <<< (%d)\n", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port), nsize);
        }
    }
    else {
        if(verbose){
                                handle_error(__FILE__, __LINE__);
      }
    }
  }
  return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -