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

📄 util.c

📁 linux 上端口映射程序源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
connect_host_to_proxy (int connection, char *remote_host, char *remote_port){  int count;  char proxy_user[256];  char User_Agent[256];  debug_printf (">connect_host_to_proxy(%d,%s,%s)\n", connection, remote_host,		remote_port);  debug_printf (">socket(AF_INET,SOCK_STREAM,0)\n");  if ((proxy_socket[connection] = socket (AF_INET, SOCK_STREAM, 0)) < 0)    {      perror ("socket");      return -1;    }  debug_printf ("socket> (%d)\n", proxy_socket[connection]);  if ((proxy_hostent = gethostbyname (proxy_host)) == NULL)    {      switch (h_errno)	{	case TRY_AGAIN:	  {	    printf ("gethostbyname:");	    printf (gettext (" temporary error"));	    printf (gettext (" in name resolution\n"));	    break;	  }	case HOST_NOT_FOUND:	  {	    printf ("gethostbyname:");	    printf (gettext (" unknown host\n"));	    break;	  }	default:	  {	    printf ("gethostbyname:");	    printf (gettext (" non-recoverable"));	    printf (gettext (" name server error\n"));	  }	}      return -2;    }  memset (&proxy, 0, sizeof proxy);  proxy.sin_family = AF_INET;  memcpy (&proxy.sin_addr, proxy_hostent->h_addr, proxy_hostent->h_length);  proxy.sin_port = htons (atoi (proxy_port));  debug_printf (">connect\n");  if (connect (proxy_socket[connection],	       (struct sockaddr *) &proxy, sizeof proxy) < 0)    {      perror ("connect");      return -3;    }  debug_printf ("connect>\n");  status = PROXY_OK;  strcpy (string, "CONNECT ");  strcat (string, remote_host);  strcat (string, ":");  strcat (string, remote_port);  strcat (string, " HTTP/1.1\r\nHost: ");  strcat (string, remote_host);  strcat (string, ":");  strcat (string, remote_port);  strcat (string, "\r\nUser-Agent: ");  if (getenv ("USER_AGENT") != NULL)    {       strncpy (User_Agent, getenv ("USER_AGENT"), 255);    }  else    {       strcpy (User_Agent, "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)");    }  strcat (string, User_Agent);  if (getenv ("PROXY_USER") != NULL)    {      char proxy_authorization_base64[257];      strncpy (proxy_user, getenv ("PROXY_USER"), 255);      base64_encode (proxy_user, proxy_authorization_base64);      strcat (string, "\r\nProxy-authorization: Basic ");      strcat (string, proxy_authorization_base64);      debug_printf ("Proxy-authorization: Basic %s\n",		    proxy_authorization_base64);    }  strcat (string, "\r\n\r\n");  strsend (proxy_socket[connection], string);  while (status == PROXY_OK)    {      if (wait_for_crlf (proxy_socket[connection]) < 0)      {	      EOC (connection);	      return -4;      }      parse_HTTP_return_code ();      if (!strcmp (HTTP_return_code, "200"))	status = BICONNECTED;      else	status = PROXY_FAULT;    }  if (status == PROXY_FAULT)    {      /*       * if PROXY_FAULT then write HTTP response to stdout       */      while ((count = read (proxy_socket[connection],			    buffer, sizeof (buffer))) != 0)	write (1, buffer, count);      return -5;    }  /*   * discard the rest of HTTP header until CR LF CR LF   * (that is, to the beginning of the real connection)   */  if (wait_for_2crlf (proxy_socket[connection]) < 0)    {      return -6;    }  print_connection (connection,		    gettext ("bidirectional connection stablished\n\n"));  if (proxy_socket[connection] > maxfd)    maxfd = proxy_socket[connection];  FD_SET (proxy_socket[connection], &mask);  debug_printf ("connect_host_to_proxy> (0)\n");  return 0;}/* * Function : void initialize_gettext(void) * Purpose  : initializes gettext (i18n) extension * Params   : none */voidinitialize_gettext (void){  debug_printf (">initialize_gettext()\n");  setlocale (LC_ALL, "");  bindtextdomain ("desproxy", LOCALEDIR);  textdomain ("desproxy");  debug_printf ("initilize_gettext>\n");}/* * Function : int bind_UDP_port(unsigned int request_port) * Purpose  : binds requested UDP port * Params   : unsigned int request_port - requested port */intbind_UDP_port (unsigned int request_port){  int UDP_socket;  debug_printf ("bind_UDP_port(%d)\n", request_port);  if ((UDP_socket = socket (PF_INET, SOCK_DGRAM, 0)) < 0)    {      perror ("socket");      exit (1);    }  memset (&server, 0, sizeof server);  server.sin_family = AF_INET;  server.sin_addr.s_addr = htonl (INADDR_ANY);  server.sin_port = htons (request_port);  if ((bind (UDP_socket, (struct sockaddr *) &server, sizeof server)) < 0)    {      perror ("bind");      exit (1);    }  printf (gettext ("UDP port "));  printf ("%d", request_port);  printf (gettext (" Bound\n"));  debug_printf ("bind_UDP_port> (%d)\n", UDP_socket);  return (UDP_socket);}/* * Function : int listen_in_TCP_port(unsigned int request_port) * Purpose  : listens in requested TCP port for incoming connections * Parmas   : unsigned int request_port - requested port */intlisten_in_TCP_port (unsigned int request_port){  int request_socket;  debug_printf ("listen_in_TCP_port(%d)\n", request_port);  if ((request_socket = socket (PF_INET, SOCK_STREAM, 0)) < 0)    {      perror ("socket");      exit (1);    }  memset (&server, 0, sizeof server);  server.sin_family = AF_INET;  server.sin_addr.s_addr = htonl (INADDR_ANY);  server.sin_port = htons (request_port);  if ((bind (request_socket, (struct sockaddr *) &server, sizeof server)) < 0)    {      perror ("bind");      exit (1);    }  if (listen (request_socket, SOMAXCONN) < 0)    {      perror ("listen");      exit (1);    }  printf (gettext ("TCP port "));  printf ("%d", request_port);  printf (gettext (" Bound & Listening\n"));  debug_printf ("listen_in_TCP_port> (%d)\n", request_socket);  return (request_socket);}/* * Function : int look_for_desproxy_conf(void) * NYI */intlook_for_desproxy_conf (void){  FILE *desproxy_conf;  if ((desproxy_conf = fopen ("desproxy.conf", "r")) == NULL)    {      return 0;    }  fclose (desproxy_conf);  return (1);}/* * Function : void turn_console_echo_off(void) * Purpose  : turns off console echo *          : stores old tty status in global variable old_tty * Params   : none */voidturn_console_echo_off (void){  struct termios tty;  /*   * Save the old tty settings   */  tcgetattr (0, &old_tty);  /*   * get rid of echo for the new tty settings.   *   * (from man tcgetattr)   *   * ICANON Enable canonical mode.  This  enables  the  special   *        characters  EOF,  EOL,  EOL2,  ERASE,  KILL, LNEXT,   *        REPRINT, STATUS, and WERASE, and buffers by  lines.   *   * ECHO   Echo input characters.   *    * ECHOE  If ICANON is also set, the ERASE  character  erases   *        the  preceding  input  character, and WERASE erases   *        the preceding word.   *   * ECHOK  If ICANON is also set, the  KILL  character  erases   *        the current line.   *   * ECHONL If  ICANON  is also set, echo the NL character even   *        if ECHO is not set.   */  tty = old_tty;  tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL);  /*   * set new tty settings    *   * (from man tcsetattr)   *   * TCSAFLUSH   *   *  the  change  occurs after all output written to the   *  object referred by fd has been transmitted, and all   *  input  that  has been received but not read will be   *  discarded before the change is made.   */  tcsetattr (0, TCSAFLUSH, &tty);}/* * Function : void turn_console_echo_on(void) * Purpose  : turns on console echo *          : restores old tty status stored in global variable old_tty * Params   : none */voidturn_console_echo_on (void){  /*   * Now reset the old settings   */  tcsetattr (0, TCSAFLUSH, &old_tty);}/* * Function : void get_username_and_password(void) * Purpose  : requests user to give username and password *          : strores them in global variables username & password * Params   : none */voidget_username_and_password (void){  strcpy (console_line, "");  while (!strcmp (console_line, ""))    {      printf (gettext ("Username: "));      strcpy (username, get_console_line ());    }  printf (gettext ("Password: "));  /*   * turn out echo, so password is not displayed when typed   */  turn_console_echo_off ();  strcpy (password, get_console_line ());  /*   * turn on echo again   */  turn_console_echo_on ();  /*   * send \n because the one in the passwd didn't echo :)   */  printf ("\n");}

⌨️ 快捷键说明

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