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

📄 util.c

📁 linux上实现内网到外网的端口映射和代理程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * util.c: misc functions common to all desproxy binaries * * Copyright (C) 2003 Miguelanxo Otero Salgueiro * * This program is free software; you can redistribute it and/or modify * it under the tems of the GNU General Public License version 2 as * published by the Free Software Foundation. * */#include <stdarg.h>#include "desproxy.h"/* * Function : encode_base64 (const char *string, char *p) * Purpose  : creates a base 64 encoded string * Params   : p - string to encode *          : s - string buffer to write into * * Note     : Taken from transconnect (http://transconnect.sourceforge.net/) */static voidbase64_encode (const char *s, char *p){  char base64[] =    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";  int i, length;  length = strlen (s);  for (i = 0; i < length; i += 3)    {      *p++ = base64[s[0] >> 2];      *p++ = base64[((s[0] & 3) << 4) + (s[1] >> 4)];      *p++ = base64[((s[1] & 0xf) << 2) + (s[2] >> 6)];      *p++ = base64[s[2] & 0x3f];      s += 3;    }  if (i == length + 1)    *(p - 1) = '=';  else if (i == length + 2)    *(p - 1) = *(p - 2) = '=';  *p = '\0';}/* * Function : debug_printf(char *fmt, ...) * Purpose  : prints arguments like printf if DEBUG is set, and *            adds fflush(stdout) * Params   : same as printf, as defined in <stdio.h> *         * Note     : this function obtained via IRC chat in  *            irc.openprojects.net, #c channel. */intdebug_printf (const char *fmt, ...){  int n;  va_list ap;  if (DEBUG)    {      va_start (ap, fmt);      n = vprintf (fmt, ap);      va_end (ap);      fflush (stdout);      return n;    }  return 0;}/* * Function : char *get_console_line (void) * Purpose  : gets a line from console to char console_line[256] * Params   : none */char *get_console_line (void){  int count;  fgets (console_line, 256, stdin);  for (count = 0; count < strlen (console_line); count++)    if (console_line[count] < 32)      console_line[count] = 0;  return console_line;}/* * Function : void strtolower(char *string) * Purpose  : changes char *string to lowercase * Params   : char *string - string to lowercase */voidstrtolower (char *string){  int count;  for (count = 0; count < strlen (string); count++)    {      string[count] = tolower (string[count]);    }}/* * Function : void print_connection(int connection, char *string) * Purpose  : outputs info string for a connection * Parms    : int connection - numbre of connection *            char *string - text to output */voidprint_connection (int connection, char *string){  printf (gettext ("Connection"));  printf (" #%d: %s", connection, string);}/* * Function: void EOC (int connection) * Purpose : terminates connection gracefully * Params  : int connection - connection number to terminate */voidEOC (int connection){  debug_printf (">EOC(%d)\n", connection);  debug_printf ("connection_status[%d]=%d\n",		connection, connection_status[connection]);    client_socket_is_free[connection] = 1;  FD_CLR (client_socket[connection], &mask);  FD_CLR (client_socket[connection], &rmask);  close (client_socket[connection]);  if (connection_status[connection] == BICONNECTED)    {      debug_printf ("connection_status[connection] == BICONNECTED\n");            FD_CLR (proxy_socket[connection], &mask);      FD_CLR (proxy_socket[connection], &rmask);      close (proxy_socket[connection]);    }  connection_status[connection] = TO_RESET;  print_connection (connection, gettext ("end of connection\n"));  debug_printf ("EOC>\n");}/* * Function : mark_all_client_sockets_as_free (void) * Purpose  : initializes client sockets, marking all of them as free * Params   : none */voidmark_all_client_sockets_as_free (void){  int connection;  debug_printf (">mark_all_client_sockets_as_free ()\n");  for (connection = 0; connection < MAX_CONNECTIONS; connection++)    client_socket_is_free[connection] = 1;  debug_printf ("mark_all_client_sockets_as_free>\n");}/* * Function : print_program_version(char *PROGRAM_NAME, char *PROGRAM_VERSION); * Purpose  : outputs program name & version "window" * Params   : char *PROGRAM_NAME - program name *            char *PROGRAM_VERSION - program version */voidprint_program_version (char *PROGRAM_NAME, char *PROGRAM_VERSION){  int count;  printf ("\n-----------------------------------\n");  printf ("%s", PROGRAM_NAME);  for (count = 0; count < 25 - strlen (PROGRAM_NAME); count++)    printf (" ");  for (count = 0; count < 10 - strlen (PROGRAM_VERSION); count++)    printf (" ");  printf ("%s\n\n", PROGRAM_VERSION);  printf ("(C) 2003 Miguelanxo Otero Salgueiro\n");  printf ("-----------------------------------\n\n");}/* * Function : void strnsend (int fd, char *string, int len) * Purpose  : almost same as write () + debugging * Params   : fd - file descriptor *          : char *string - data to write *          : int len - length of data */voidstrnsend (int fd, char *string, int len){  debug_printf (">strnsend(%d,%s,%d)\n", fd, string, len);/*	int towrite,index=0;	towrite=len;	while (towrite>BUFFSIZE) {		memcpy(&buffer,&string[index],BUFFSIZE);		write(fd,buffer,BUFFSIZE);		towrite=towrite-BUFFSIZE;		index=index+BUFFSIZE;	}	memcpy(&buffer,&string[index],towrite);	write(fd,buffer,towrite);	if (DEBUG)	{		write(1,buffer,towrite);		fflush(stdout);	}*/  write (fd, string, len);  debug_printf ("strnsend>\n");}/* * Function : void strsend(int fd, char *string) * Purpose  : send null terminated string to file descriptor * Params   : int fd - file descriptor *          : char *string - data to send */voidstrsend (int fd, char *string){  strnsend (fd, string, strlen (string));}/* * Function : char *parse_HTTP_return_code(void) * Purpose  : gets a parsed HTTP return code from line in buffer *          : (returns a pointer to global variable HTTP_return_code) * Params   : none */char *parse_HTTP_return_code (void){  int count;  debug_printf (">parse_HTTP_return_code\n");  /*   * initialize HTTP_return_code to XXX (undefined/error)   */  strcpy (HTTP_return_code, "XXX");  if (!memcmp (buffer, "HTTP", 4))    {      for (count = 0; buffer[count] != ' '; count++)	if (count == BUFFER_SIZE)	  break;      if (count < BUFFER_SIZE)	{	  memcpy (HTTP_return_code, &buffer[count + 1], 3);	  HTTP_return_code[3] = 0;	  debug_printf ("parse_HTTP_return_code>\n");	  return HTTP_return_code;	}    }  printf ("parse_HTTP_return_code:");  printf (gettext ("bad proxy response.\n"));  exit (1);}/* *  Function : void wait_for_crlf(int fd) *  Purpose  : reads data from file descriptor until sequence CR LF found *           : (data is stored in global variable buffer) *  Params   : int fd - file descriptor *  Returns  : 0 if Ok, -1 otherwise */intwait_for_crlf (int fd){  unsigned char previous_byte = 0;  int count;  debug_printf (">wait_for_crfl(%d)\n", fd);  count = 0;  while (1)    {      read (fd, &buffer[count], 1);      debug_printf ("%c", buffer[count]);      if ((buffer[count] == '\n') && (previous_byte == '\r'))	break;      if (count == BUFFER_SIZE)	{	  printf (" (CASCA)\n\n");	  printf ("wait_for_crlf: BUFFER OVERFLOW!\n");	  return (-1);	}      previous_byte = buffer[count];      count++;    }  buffer[count + 1] = 0;  debug_printf ("wait_for_crfl>\n");  return (0);}/* * Function : void wait_for_2crlf(int fd) * Purpose  : reads file descriptor until sequence CR LR CR LF found *          : (that sequence is used to mark HTTP header end) * Params   : int fd - file descriptor to read from * Returns  : 0 if Ok, -1 otherwise */intwait_for_2crlf (int fd){  debug_printf (">wait_for_2crlf\n");  while (memcmp (buffer, "\r\n", 2))    {      if (wait_for_crlf (fd) < 0)        {	  return (-1);	}    }  debug_printf ("wait_for_2crlf>\n");  return (0);}/* * Function : int connect_host_to_proxy(int connection, char *remote_host *          : char *remote_port) * Purpose  : connects to remote_host:remote_port *          : trough proxy_host:proxy_port  * Params   : int connection - number of connection in use *          : char *remote_host - remote host (name or IP as string) *          : char *remote_port - remote port (number as string) */int

⌨️ 快捷键说明

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