About exclusive port

xiaoxiao2021-03-05  22

I know that there is no new for 2K PlatformSDK, if you installed, you may wish to compare

Some headers and vs98 have the same as different head files, they will find a lot of interesting stuff! In some

Actually, MS has long provided support on the previous platform, but has not written into its header file.

In the case, the following 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 can also be seen, this stuff is to prevent local ports from being reused, I

We know that we can bind two sockets to the same port, as long as the above

The option so_reuseaddr, which is "anti," can be, think about what will happen? I

We can open the POP3 port in a program (the server-side default is 110, the client should set

At the same time, run your own program while binding your own Socket to the same port.

At this time, you can hit the content of the letter while you can accept the content of the letter, including the account.

password! It looks very simple, isn't it? However, many software in reality have not taken any

Effective strategy to deal with this hand, such as domestic famous email client software f ** mail.

So how should we prevent this situation from running in his own procedure? Direct idea is

In real time, monitor this machine, see what port is bound, if you find the eavesdropline

Disconnected immediately. It sounds good, but if you are waiting for you to disconnect the password, the password has been leaked.

To ensure that the connection is turned off before password transmission, you need a lot of real-time processing, even if you leave these sleepy.

Don't talk, go to the Windows platform to access a lot of registry content to determine the current active port

There is enough annoying. Fortunately, now there is this Socket option above, everything becomes 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_EXCLUSIVEADDRUS, / / ​​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;

}

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

New Post(0)