About exclusive port

zhaozj2021-02-08  257

About exclusive port

Author: Star

2000.6.12

Welcome to reprint, please indicate the source.

-------------------------------------------------- ------------------------------

I don't know if you have a new for 2K PlatformSDK. If you installed, you may wish to compare some of the universal files and the vs98's original header files, you will find a lot of interesting stuff! Some content actually MS has long provided support on the previous platform, but has not written into its header file, below is an example discovered in Winsock2.h:

In the new version of Winsock2.h, there is more definitions:

#define so_exclusiveaddruse (~ SO_REUSEADDR)) / * Disallow local address reuse * /

From MS's annotation, this stuff is to prevent local ports from being reused. We know that we can bind two sockets to the same port, as long as the above is "anti-" Options so_reuseaddr, think about what happens to see this? We can open the POP3 port in a program (the server-side default is 110, the client should set to random selection), run your own program, tied That's your own socket to the same port, then you can get the content of the letter while you can get the content of the letter, including the account password! It looks very simple, isn't it? But many software in reality do not take any effective strategies to deal with this hand, such as domestic famous email client F ** mail.

So how should we prevent this situation from running in his own procedure? Direct ideas are to monitor local connections in real time, see what port is bound, if you find that the eavesdrop program is disconnected immediately. It sounds good, but I am really waiting for you to disconnect the password has been leaked. To make sure you need a lot of real-time processing before password transmission, you need a lot of real-time processing, even if you don't talk about these difficulties, go to the Windows platform to visit a lot The registry content to determine the current active port is bored. Fortunately, now there is this Socket option, everything is simple, please see the following demo:

//Test.cpp: Test Exclusive with sockets

// if it's ok dam 'star, else i don't know ^ _ ^

//

#include

#include

// if Have no the new placeformsdk, THEN U NEED TO Add.

// define Statement to your Sourcecode, else remove it

//

#define so_exclusiveaddruse (~ so_reuseaddr))

Const u_short exclusive_port = 110; // or ta

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

{

Socket sock1, sock2;

int R;

Bool Val;

WSADATA WS;

Struct SockAddr_in in;

// NEED WINSOCK 2.0!

IF (WSAStartup (2, & WS))

Return;

IF (Lobyte (WS.WVersion) - 2)

Return;

// Make Two Sockets

SOCK1 = Socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCK2 = Socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);

Val = true;

Ret = setsockopt (SOCK1, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, / / ​​SET TO EXCLUSIVE

(LPCSTR) & Val, 4);

IF (re)

{

// failed ....

// Do Something to Clear The Error ....

Return;

}

ZeromeMory (& IN, SIZEOF (in);

IN.SIN_FAMILY = AF_INET;

In.sin_port = htons (exclusive_port);

IN.SIN_ADDR.S_ADDR = INADDR_ADDR

Printf ("Now to Bind THE FIRST Socket To Port% D with so_exclusiveaddruse / n", exclusive_port);

IF (Bind (Sock1, Const Struct SockAddr *) & IN, SIZEOF (in))))))

{

// failed ...

// Do Something to Clear THE ERROR ...

CloseSocket (SOCK1);

Return;

}

Printf ("THE FIRST Socket Has Been Bound To Port% D / N", Exclusive_Port;

Ret = setsockopt (SOCK2, SOL_SOCKET, SO_REUSEADDR, / / ​​TRY TO REUSE

(LPCSTR) & Val, 4);

IF (re)

{

// failed ....

// Do Something to Clear The Error ....

Return;

}

ZeromeMory (& IN, SIZEOF (in);

IN.SIN_FAMILY = AF_INET;

In.sin_port = htons (exclusive_port);

IN.SIN_ADDR.S_ADDR = INADDR_ADDR

Printf ("/ nnow to bind the second socket to port% d / n", exclusive_port);

IF (Bind (Const struct socketdr *) & in, sizeof (in)))))

{

IF (WSAEADDRINUSE == getLastError ())

Printf ("Bind Failed, Take Effect! / N");

Else

{

// failed ...

// Do Something to Clear THE ERROR ...

}

}

Else

Printf ("what ??? I see nothing !!!! / n");

CloseSocket (SOCK1);

CloseSocket (SOCK2);

Return;

}

The above code is compiled under my PNT4SP6. In addition to several conspicuous, many places lack the error detection, and the friends you need to use. I will not be very clear about the situation under the 9X. After all, I haven't used it for a year. I will take it :-) If you know the friend, please tell me :-) I will make up :-)

-------------------------------------------------- ------------------------------ Irrigation, please laugh ^ _ ^

About Internet Programming, I am also a newbie, this stuff written in the question of Lu0 brother, so the content is more junk ... Now I still remember that Lu0 brother asked me what I wrote: "At that time lu0 The brother's fire is rushing out of his throat. It is only 0.01 cm, but it will always be in the next day after a day, because I decided to install a water to this layout. Although I am flating countless water But this article, I think it is the purest. "

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

New Post(0)