简体   繁体   English

TCP套接字从错误的端口接收

[英]TCP socket receives from wrong port

I have problem with a TCP socket receiving messages with wrong destination port. 我有一个TCP套接字接收错误的目标端口消息的问题。

The OS is Ubuntu Linux 10.10 and kernel version is 2.6.31-11-rt, but this happens with other kernels, too. 操作系统是Ubuntu Linux 10.10,内核版本是2.6.31-11-rt,但这也适用于其他内核。 The C/C++ program with this problem does this: 具有此问题的C / C ++程序执行此操作:

  1. A TCP server socket is listening to connections in INADDR_ANY at port 9000. TCP服务器套接字正在侦听端口9000处INADDR_ANY中的连接。

  2. Messages are received with recv(2) by a TCP message receiver thread. 通过TCP消息接收器线程用recv(2)接收消息。 Connection is not closed after reading message, but the thread continues to read from the same connection forever. 读取消息后,连接未关闭,但线程将继续从同一连接中继续读取。

  3. Error: also messages to other ports than 9000 are received by the TCP message receiver. 错误:TCP消息接收器也接收到9000以外的其他端口的消息。 For example, when a remote SFTP client connects to the PC where TCP message receiver is listening, it causes the TCP message receiver to receive also the SFTP messages. 例如,当远程SFTP客户端连接到TCP消息接收器正在侦听的PC时,它会使TCP消息接收器也接收SFTP消息。 How is this EVER possible? 这怎么可能? How can the TCP ports "leak" this way? TCP端口如何以这种方式“泄漏”? I think SFTP should use port 22, right? 我认为SFTP应该使用22端口,对吧? Then how it's possible those messages are visible in port 9000? 那么这些消息在端口9000中是如何可见的呢?

More info: 更多信息:

  • At the same time there's a raw socket listening on another network interface, and the interface is in promiscuous mode. 同时有一个原始套接字侦听另一个网络接口,并且该接口处于混杂模式。 Could this have an effect? 这会产生影响吗?

  • The TCP connection is not closed in between message receptions. 消息接收之间未关闭TCP连接。 The message listener just keeps reading data from the socket. 消息侦听器只是继续从套接字读取数据。 Is this really a correct way to implement a TCP message receiver? 这真的是实现TCP消息接收器的正确方法吗?

Has anyone seen this kind of problem? 有没有人见过这种问题? Thanks in advance. 提前致谢。

EDIT: 编辑:

Ok, here is some code. 好的,这是一些代码。 The code looks to be allright, so the main strange thing is, how a TCP socket can ever receive data sent to another port? 代码看起来没那么好,所以最奇怪的是,TCP套接字如何接收发送到另一个端口的数据?

/// Create TCP socket and make it listen to defined port
TcpSocket::listen() {
    m_listenFd = socket(AF_INET, SOCK_STREAM, 0)
    ...
    bzero(&m_servaddr, sizeof(sockaddr_in));
    m_servaddr.sin_family = AF_INET;
    m_servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    m_servaddr.sin_port = htons(9000);
    bind(m_listenFd, (struct sockaddr *)&m_servaddr, sizeof(sockaddr_in);
    ...
    listen(m_listenFd, 1024);
    ...
    m_connectFd = accept(m_listenFd, NULL, NULL);
}

/// Receive message from TCP socket.
TcpSocket::receiveMessage() {
    Uint16 receivedBytes = 0;
    // get the common fixed-size message header (this is an own message structure)
    Uint16 numBytes = recv(m_connectFd, msgPtr + receivedBytes, sizeof(SCommonTcpMSGHeader), MSG_WAITALL);
    ...
    receivedBytes = numBytes;
    expectedMsgLength = commonMsgHeader->m_msgLength;   // commonMsgHeader is mapped to received header bytes
    ...
    // ok to get message body
    numBytes = recv(m_connectFd, msgPtr + receivedBytes, expectedMsgLength - receivedBytes, MSG_WAITALL);
}

The TCP connection is not closed in between message receptions. 消息接收之间未关闭TCP连接。 The message listener just keeps reading data from the socket. 消息侦听器只是继续从套接字读取数据。 Is this really a correct way to implement a TCP message receiver? 这真的是实现TCP消息接收器的正确方法吗?

Yes but it must close the socket and exit when it receives the EOS indication (recv() returns zero). 是的但它必须关闭套接字并在收到EOS指示时退出(recv()返回零)。

I think it's ten to one your raw socket and TCP socket FDs are getting mixed up somewhere. 我认为你的原始套接字和TCP套接字FD在十几分之一的地方混淆了。

Umm... It appears that it was the raw socket after all which has received the messages. 嗯......看来它是接收消息的原始套接字。 Can see from the log that it's the raw message handler printing out message reception things, not the TCP message handler. 从日志中可以看出,它是原始消息处理程序打印出消息接收的东西,而不是TCP消息处理程序。 Duh...! 咄...! :S Sorry. :S抱歉。 So it seems the binding of the raw socket into the other network interface doesn't work correctly. 所以看起来原始套接字绑定到其他网络接口不能正常工作。 Need to fix that. 需要解决这个问题。 Funny though that sometimes it works with SSH/SFTP, sometimes it does not. 有趣但有时它适用于SSH / SFTP,有时它不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM