Simple proxy server C code implementation (Solaris)

xiaoxiao2021-03-06  43

/ *

** Writing: Nothing

* Source: www.20cn.net

** Note: Please indicate the reposted source

* /

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

INT M_MAINID = 0; // Main Process ID

INT m_listensocket = 0; // Saard socket

Char M_ConnectAddr [256] = {0}; // destination address

Char m_connectport [256] = {0}; // Target Port

/ *

** Function Name: getListensocket

** Function function: Generate listening socket

** Incoming Parameters: Port: Listening Port

** Outlet Parameters: None

** Reference function: no

** Return Value: Listening Socket, indicating that the generated socket failed, and the other set

** Note: None

* /

INT GetListensocket (int port)

{

Struct SockAddr_in M_Lisaddr = {0};

INT m_socket = 0;

INT M_ADDRLEN = SizeOf (struct sockaddr_in);

// Configure port information

m_lisaddr.sin_family = af_INet;

M_Lisaddr.sin_Port = HTONS (port);

m_lisaddr.sin_addr.s_addr = incdr_any;

// Create a socket

IF ((m_socket = socket (AF_INET, SOCK_STREAM, 0) <0)

{

// Creating a socket failed

Return 0;

}

// Binding socket

IF (bind (m_socket, (sockaddr *) & m_lisaddr, m_addrlen) <0)

{

// Binding socket failed

Close (m_socket);

Return 0;

}

// Listening socket

IF (Listen (M_Socket, 5))

{

// Listening socket failed

Close (m_socket);

Return 0;

}

// Listening socket generation success

Return m_socket;

}

/ *

** Function Name: getConnectSocket

** Function function: Generate connection socket

** Introduction parameters: PServeraddd: Connection address PServerport: Connection port

** Outlet Parameters: None

** Reference function: no

** Return Value: Connecting socket, indicating that the generated socket failed, and other set

** Note: None

* /

Int getConnectSocket (Char * PServeraddr, Char * PServerport)

{

Struct SockAddr_in m_serveraddr = {0};

INT M_ADDRLEN = SizeOf (struct sockaddr_in);

INT m_socket = 0;

// Initialize connection information

m_serveraddr.sin_addr.s_un.s_addr = inet_addr; m_serveraddr.sin_port = htons (atoi (atoi (pserverport);

m_serveradddr.sin_family = af_INET;

// Create a send socket

m_socket = socket (AF_INET, SOCK_STREAM, 0);

IF (m_socket <= 0)

{

//failure

Return NULL;

}

/ / Connect the client computer

IF (Connect (m_socket, (sockaddr *) & m_serveradddr, m_addrlen) <0)

{

Close (m_socket);

Return NULL;

}

//connection succeeded

Return m_socket;

}

/ *

** Function Name: Transsocket

** Function function: Complete the socket data forwarding

** Introduction parameters: m_sendsocket: Send socket M_Recvsocket: Receive socket

** Outlet Parameters: None

** Reference function: no

** Return value: no

** Note: Reverse completion of the full duplex

* /

Void Transsocket (int m_sendsocket, int m_recvsocket)

{

CHAR M_BUF [512 * 1024] = {0};

INT RET = 0;

FD_set readset;

Struct TimeValTM = {0};

TM.TV_SEC = 3600 * 24;

FD_ZERO (& Readset);

FD_set (M_Recvsocket, & Readset);

While (1)

{

IF ((SELECT (M_Recvsocket 1, & Readset, NULL, NULL, & TM)

<= 0)))

{

// error

Break;

}

IF (! fd_isset (m_recvsocket, & readset) Continue;

RET = Recv (M_Recvsocket, M_BUF, 512 * 1024 - 1, 0);

IF (RET <0)

{

// error

Break;

}

Send (m_sendsocket, m_buf, ret, 0);

}

Close (m_sendsocket);

Close (M_Recvsocket);

}

/ *

** Function Name: SocketTrans

** Function function: Work main function, complete data forwarding, new process start

** Introduction parameters: m_sendsocket: Send socket M_Recvsocket: Receive socket

** Outlet Parameters: None

** Reference function: no

** Return value: no

** Note: Reverse completion of the full duplex

* /

Void sockettrans ()

{

Struct SockAddr_in M_WORKADDR = {0};

INT m_191socket = 0;

INT m_147socket = 0;

INT M_WORKADDRLEN = 0;

/ / Start the task

While (1)

{

/ / Accept 147 connection

m_workaddrlen = sizeof (struct sockaddr_in);

m_147socket = accept (m_listensocket,

(SockAddr *) & m_workaddr, & m_workaddrlen;

// Check the legality of socket

IF (M_147Socket <0) Continue;

// Connect 191

M_191socket = getConnectSocket (m_connectaddr, m_connectport); if (m_191socket == null)

{

Close (M_147Socket);

CONTINUE;

}

int RET = fork ();

IF (RET <0)

{

// Establish a new process failed

Printf ("" Fatal Error, Unable to establish a new process! / n ");

Fflush (stdout);

Close (m_191socket);

Close (M_147Socket);

Break;

}

Else IF (RET == 0)

{

// Turn off the original port

Close (m_listensocket);

/ / Establish a second sub-process to prevent zombie process

Ret = fork ();

IF (RET <0)

{

Close (m_191socket);

Close (M_147Socket);

_exit (0);

}

Else IF (RET == 0)

{

// Receive process

Transsocket (M_191 Socket, M_147Socket);

_exit (0);

}

Ret = fork ();

IF (RET <0)

{

Close (m_191socket);

Close (M_147Socket);

_exit (0);

}

Else IF (RET == 0)

{

// Sending Process

Transsocket (M_147Socket, M_191Socket);

_exit (0);

}

Close (m_191socket);

Close (M_147Socket);

_exit (0);

}

// Waiting for the end of the sub-thread

Close (m_191socket);

Close (M_147Socket);

Waitpid (RET, NULL, 0);

}

}

/ *

** Function Name: SIG_USR

** Function function: process signal processing function

** Incoming parameters: no

** Outlet Parameters: None

** Reference function: no

** Return value: no

** Note: Test Process Termination Event

* /

Static void Sig_USR (Int Signo)

{

Close (m_listensocket);

IF (m_mainid == getpid ())

Kill (0, SIGKILL);

exit (0);

}

Static void Sig_ign (int Signo)

{

FPRINTF (stderr, "signalr% d catched, ignoring / n", sIGNO);

}

INT daemon_init ()

{

PID_T PID;

IF ((pID = for ()) <0) {

Return -1;

} else if (pID! = 0) {

exit (0);

}

setsid ();

Umask (0);

Return 0;

}

/ *

** Function Name: main

** Function function: Process main function

** Incoming parameters: no

** Outlet Parameters: None

** Reference function: makefilepath, getMyinitinfo, SocketTrans

** Return value: no

** Note: Receive process principles for customers

* /

Int main (int Argc, char * argv [])

{

// Check the legality of parameters

IF (argc! = 4)

{

Printf ("format: local port destination address destination port / n");

Fflush (stdout);

Return 0;

}

Daemon_init ();

// Register signal event

Signal (SIGTERM, SIG_USR);

Signal (SIGINT, SIG_USR);

Signal (SIGQUIT, SIG_USR); SIGNAL (SIGPIPE, SIG_IGN);

Signal (SIGALRM, SIG_IGN);

Signal (SIGQUIT, SIG_IGN);

Signal (SIGFPE, SIG_IGN);

Signal (SIGILL, SIG_IGN);

Signal (SIGPIPE, SIG_IGN);

Signal (SIGSEGV, SIG_IGN);

Signal (SigTrap, SIG_IGN);

Signal (SIGTSTP, SIG_IGN);

// Take parameters

STRCPY (M_ConnectAddr, Argv [2]);

STRCPY (M_Connectport, Argv [3]);

// Get listening socket

M_Listensocket = GetListensocket (ATOI (ARGV [1]));

IF (m_listensocket == 0)

{

Printf ("Listening Port [% S] failed! / n", Argv [1]);

Fflush (stdout);

Return 0;

}

m_mainid = get pid ();

// Start file receiving listening thread

SocketTrans ();

Close (m_listensocket);

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-51242.html

New Post(0)