正确处理TCP带外数据

m4pnthwp  于 2022-12-11  发布在  其他
关注(0)|答案(1)|浏览(182)

我写了一个简单的客户端和服务器来处理带外数据。客户端只向服务器发送一个带外数据,服务器使用SIGURG来处理这个字节。服务器还应该在无限循环中处理正常的流量。代码有一个争用条件,它不能像预期的那样工作。有时我从调用recv得到一个“无效参数”。()。我的另一个问题是,当调用accept时,我应该阻塞SIGURG信号吗?另外,哪一个是首选方案:

  • 安装SIGURG处理程序并在调用accept * 之前 * 设置侦听套接字的套接字所有者。
  • 安装SIGURG处理程序并在调用accept后为连接的套接字设置套接字所有者。
  • 如果以上都不是,请写下你的建议。

我的最后一个问题是,由于客户端立即发送带外数据,服务器是否有机会在三次握手完成后、从accept返回之前接收SIGURG?如果是,我认为“clifd”var在SIGURG处理程序中使用时可能具有无效值。
客户端的代码:

#include "myheader.h"

int main(int argc, char *argv[])
{
    struct sockaddr_in saddr;
    int sockfd;

    const char c = 'a';

    if (2 != argc)
    {
        fprintf(stderr, "Usage: %s ipaddr\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0)))
        die("sockfd()");

    (void)memset(&saddr, 0, sizeof(saddr));

    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(PORT);

    if (-1 == inet_pton(AF_INET, argv[1], &saddr.sin_addr))
        die("inet_pton()");

    if (-1 == connect(sockfd, (struct sockaddr*)&saddr, sizeof(saddr)))
        die("connect()");

//  if (-1 == send(sockfd, "HELLO\n", 6, 0))
//      die("send()");

    if (-1 == send(sockfd, &c, 1, MSG_OOB))
        die("send()");

    close(sockfd);

    return 0;
}

和服务器的代码:

#include "myheader.h"

void sigurg_handler(int);

char    oob;
int sockfd, clifd;

int main(void)
{
    struct sockaddr_in myaddr;
    char buf[BUFSIZ];
    ssize_t nbytes;
    sigset_t sset, oset;
    sigemptyset(&sset);
    sigaddset(&sset, SIGURG);

    if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0)))
        die("socket()");

    (void)memset(&myaddr, 0, sizeof(myaddr));

    myaddr.sin_family = AF_INET;
    myaddr.sin_port = htons(PORT);
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (-1 == bind(sockfd, (struct sockaddr*)&myaddr, sizeof(myaddr)))
        die("bind()");

    if (-1 == listen(sockfd, BACKLOG))
        die("listen()");

    if (-1 == fcntl(sockfd, F_SETOWN, getpid()))
        die("fcntl()");

    if (SIG_ERR == signal(SIGURG, sigurg_handler))
            die("signal()");
    for (;;)
    {
        /* block SIGURG when accepting the connection */
//      sigprocmask(SIG_SETMASK, &sset, &oset);
        printf("bloking in accept()\n");
        if (-1 == (clifd = accept(sockfd, NULL, NULL)))
            die("accept()");

        /* unblock SIGURG */
//      sigprocmask(SIG_SETMASK, &oset, NULL);

        printf("recv()ing normal data\n");
        nbytes = recv(clifd, buf, sizeof(buf), 0);
        buf[nbytes] = 0; /* null-terminate */

        printf("%s", buf);

    }

    close(sockfd);
}

void
sigurg_handler(int signo)
{
    char buff[100];
    ssize_t nbytes;

    printf("SIGURG received\n");
    if (clifd != 0)
    {
        if (-1 == (nbytes = recv(clifd, buff, sizeof(buff) - 1, MSG_OOB)))
            die("recv() in sigurg_handler()");

        buff[nbytes] = 0;
        printf("from sigurg_handler: received \"%s\"\n", buff);
    }
    else
    {
        printf("clifd = %d\n", clifd);
        exit(1);
    }
}

示例:

> ./serv 
bloking in accept()         /* first client */
SIGURG received
from sigurg_handler: received "a"
recv()ing normal data
bloking in accept()         /* second client */
SIGURG received
recv() in sigurg_handler(): Invalid argument
> ./serv             /* third client */
bloking in accept()
SIGURG received
clifd = 0
>
fdbelqdn

fdbelqdn1#

我听说select()的第三个参数可以处理tcp OOB

ret = select(connfd+1,&read_fds,NULL,&exceptions_fds,NULL);

https://blog.csdn.net/ty_laurel/article/details/52164669
https://github.com/ty92/OOB

select()异常错误

使用select确实可以避免信号建立步骤,
以便您不会错过OOB(信号设置之前的OOB)。
https://man7.org/linux/man-pages/man7/tcp.7.html#:~:text=out%2Dof%2Dband%20data%20is%20present
man 2 select_tut有一个演示代码
https://man7.org/linux/man-pages/man2/select_tut.2.html#:~:text=read%20OOB%20data,-before

限制

但如果您没有及时读取OOB字节,当新OOB到达时,旧OOB字节将变为正常数据(inserted as normal data into the stream),即使SO_OOBINLINE未设置(在Linux上)
//该行为在各种TCP栈中可能不同。
https://man7.org/linux/man-pages/man7/tcp.7.html#:~:text=limited%20support%20for%20out%2Dof%2Dband
PS:你最好用:~:text=手动复制链接,它会在chrome中突出显示关键字。
//或在编辑预览模式下单击。
//在正常页面堆栈中溢出始终在url中编码~,这将使锚无效
//那些男人页到今天还不支持压阵,真是可惜。

相关问题