Разработка программы для работы по TCP/IP, страница 2

goto DEFPROC;

break;

}

//=============================================================================

case WM_CREATE:

{

WSADATA wsadata;

SOCKADDR_IN sa,st;

for (r=0;r<SOCK_NUM;r++)

{

ar_socks_client[r]=0;

ar_socks_local[r]=0;

};

recv_size=0;

WSAStartup(101,&wsadata);

hsock_server=socket(AF_INET,SOCK_STREAM,PF_UNSPEC);

st.sin_family     =AF_INET;

st.sin_addr.s_addr=INADDR_ANY;

st.sin_port       =htons(LOWORD(port));

bind(hsock_server,(const sockaddr *)&st,sizeof(st));

listen(hsock_server,SOCK_NUM);

WSAAsyncSelect(hsock_server,hWnd,WM_SOCK_ACCEPT,FD_ACCEPT);

hDC=GetWindowDC(hWnd);

SetTextColor(hDC,0x008800);

SelectObject(hDC,(HBRUSH)GetStockObject(LTGRAY_BRUSH));

SetBkColor(hDC,0xC0C0C0);//LTGRAY_BRUSH

break;

}

//=============================================================================

case WM_SOCK_ACCEPT:

{

// connect with main socket

if (LOWORD(lParam) == FD_ACCEPT)

{

q=sizeof(sa);

r=accept(wParam,(struct sockaddr*)&sa,(int*)&q); // get accept socket

for (q=0;q<SOCK_NUM;q++)

if (ar_socks_client[q] == 0)

{

ar_socks_client[q]=r;

out("SOCK_ACCEPT");

break;

};

WSAAsyncSelect(r,hWnd,WM_SOCK_READ_CLOSE_FIRST,FD_READ|FD_CLOSE);

}

break;

}

//=============================================================================

case WM_SOCK_READ_CLOSE_FIRST:

{

lowParam=LOWORD(lParam);

for (r=0;r<SOCK_NUM;r++)

if (ar_socks_client[r] == wParam)

break;

if (lowParam == FD_CLOSE)

{

closesocket(ar_socks_local[r]);

ar_socks_local[r]=0;

out("SOCK_CLOSE");

break;

};

// sock read

if (lowParam == FD_READ)

{

recv_size=recv(ar_socks_client[r],recv_buf,RECV_BUF_SIZE,0);  // ñîáñòâåííî ÷èòàåì

out("SOCK_RECV");

// is sock4 request

//      +----+----+----+----+----+----+----+----+

//      | VN | CD | DSTPORT |      DSTIP        |

//      +----+----+----+----+----+----+----+----+

// # of bytes:     1    1      2              4

//                   /- sock ver          /- cmd connect

if ((recv_buf[0] == 4) && (recv_buf[1] == 1))

{

closesocket(ar_socks_local[r]);

ar_socks_local[r]=socket(AF_INET,SOCK_STREAM,AF_UNSPEC);

out("SOCK_CREATE");

// create client socket

sa.sin_family=AF_INET;

// get ip

sa.sin_addr.s_addr=MAKELONG(MAKEWORD(recv_buf[4],recv_buf[5]),MAKEWORD(recv_buf[6],recv_buf[7]));

// get port

sa.sin_port=htons(MAKEWORD(recv_buf[3],recv_buf[2]));

out(inet_ntoa(sa.sin_addr));

WSAAsyncSelect(ar_socks_local[r],hWnd,WM_SOCK_READ_CLOSE_CONNECT,FD_READ|FD_CLOSE|FD_CONNECT);

connect(ar_socks_local[r],(const sockaddr *)&sa,sizeof(sa));

out("SOCK_TRY_CONNECT");

}

else

{

// send data to client

send(ar_socks_local[r],recv_buf,recv_size,0);

out("SOCK_SEND");

};

}

break;

}

//=============================================================================

case WM_SOCK_READ_CLOSE_CONNECT:

{

int i=HIWORD(lParam); // error code

lowParam=LOWORD(lParam);

for (r=0;r<SOCK_NUM;r++)

if (ar_socks_local[r] == wParam)

break;

if (lowParam == FD_CONNECT)

{

if (i != 0) // if error

{

closesocket(ar_socks_local[r]);

ar_socks_local[r]=0;

closesocket(ar_socks_client[r]);

ar_socks_client[r]=0;

out("SOCK_CLOSE");

}

// all right

else

{

out("SOCK_CONNECT");

// send reply

// RFC: VN is the version of the reply code and should be 0

send(ar_socks_client[r],"\0x0\132\0\0\0\0\0\0",8,0);

recv_size=0;

out("SOCK_AUTH_SOCK4");

};

}

// client off

if (lParam == FD_CLOSE)

{

closesocket(ar_socks_client[r]);

ar_socks_client[r]=0;

out("SOCK_CLOSE");

};

if (lParam == FD_READ)

{

recv_size=recv(ar_socks_local[r],recv_buf,RECV_BUF_SIZE,0);

out("SOCK_RECV");

send(ar_socks_client[r],recv_buf,recv_size,0);

out("SOCK_SEND");

}

break;

}

//=============================================================================

case WM_NCRBUTTONDBLCLK:

{

SendMessage(hWnd,WM_CLOSE,0,0);

break;

};

//=============================================================================

default:

{

DEFPROC:

return DefWindowProc(hWnd,msg,wParam,lParam);

};

//=============================================================================

}//switch

//return DefWindowProc(hWnd,msg,wParam,lParam);

return 0;

}

//=============================================================================