热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Ubuntu上使用TCP_REPAIRsocket选项编译代码-CompilingcodewithTCP_REPAIRsocketoptiononUbuntu

Followingthisarticle,IamtryingtogetaCprogramtocompilethatusesTPC_REPAIR.在本文之后,我尝试使用T

Following this article, I am trying to get a C program to compile that uses TPC_REPAIR.

在本文之后,我尝试使用TPC_REPAIR编译一个C程序。

http://oroboro.com/dealing-with-network-port-abuse-in-sockets-in-c/

http://oroboro.com/dealing-with-network-port-abuse-in-sockets-in-c/

TCP_REPAIR is defined in

TCP_REPAIR定义在

/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h

Including this header conflicts with the ones required for other socket APIs.

包含此头与其他套接字api所需的头冲突。

Here's my code

这是我的代码

#include 
#include 
#include 
#include 
#include 
#include "/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h"
int main(int argc, char* argv[])
{
    int cfd;
    struct sockaddr_in addr;
    char ch='r';
    cfd=socket(AF_INET, SOCK_STREAM, 0);
    addr.sin_family=AF_INET;
    addr.sin_addr.s_addr=inet_addr("10.160.102.40");
    addr.sin_port=htons(9988);
    if(connect(cfd, (struct sockaddr *)&addr,
    sizeof(addr))<0) {
        perror("connect error");
        return -1;
    }
    if(write(cfd, &ch, 1)<0) perror("write");
    if(read(cfd, &ch, 1)<0) perror("read");
    printf("\nReply from Server: %c\n",ch);
    //close(cfd);

    int aux = 1;
    if(setsockopt(cfd, SOL_TCP, TCP_REPAIR, &aux, sizeof( aux )) <0 )
    {
        perror( "could not turn on repair mode" );
    }
   return 0;
}

And here's gcc output

这里是gcc输出

In file included from /usr/include/netinet/tcp.h:56:0,
                 from new_client.c:5:
/usr/include/x86_64-linux-gnu/sys/types.h:45:18: error: conflicting types for ‘loff_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:45:26: note: previous declaration of ‘loff_t’ was here
/usr/include/x86_64-linux-gnu/sys/types.h:61:17: error: conflicting types for ‘dev_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:15:25: note: previous declaration of ‘dev_t’ was here
/usr/include/x86_64-linux-gnu/sys/types.h:76:19: error: conflicting types for ‘nlink_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:19:17: note: previous declaration of ‘nlink_t’ was here
In file included from /usr/include/x86_64-linux-gnu/sys/types.h:133:0,
                 from /usr/include/netinet/tcp.h:56,
                 from new_client.c:5:
/usr/include/time.h:104:19: error: conflicting types for ‘timer_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:25:26: note: previous declaration of ‘timer_t’ was here
In file included from /usr/include/netinet/tcp.h:56:0,
                 from new_client.c:5:
/usr/include/x86_64-linux-gnu/sys/types.h:198:1: error: conflicting types for ‘int64_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:113:17: note: previous declaration of ‘int64_t’ was here
/usr/include/x86_64-linux-gnu/sys/types.h:204:1: error: conflicting types for ‘u_int64_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:112:17: note: previous declaration of ‘u_int64_t’ was here
In file included from /usr/include/x86_64-linux-gnu/sys/types.h:220:0,
                 from /usr/include/netinet/tcp.h:56,
                 from new_client.c:5:
/usr/include/x86_64-linux-gnu/sys/select.h:76:5: error: conflicting types for ‘fd_set’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:14:26: note: previous declaration of ‘fd_set’ was here
In file included from /usr/include/netinet/tcp.h:56:0,
                 from new_client.c:5:
/usr/include/x86_64-linux-gnu/sys/types.h:236:20: error: conflicting types for ‘blkcnt_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:134:23: note: previous declaration of ‘blkcnt_t’ was here
In file included from /usr/include/x86_64-linux-gnu/sys/uio.h:29:0,
                 from /usr/include/x86_64-linux-gnu/sys/socket.h:28,
                 from /usr/include/netinet/tcp.h:57,
                 from new_client.c:5:
/usr/include/x86_64-linux-gnu/bits/uio.h:44:8: error: redefinition of ‘struct iovec’
/usr/src/linux-headers-3.8.0-29-generic/include/uapi/linux/uio.h:16:8: note: originally defined here
In file included from /usr/include/x86_64-linux-gnu/sys/socket.h:40:0,
                 from /usr/include/netinet/tcp.h:57,
                 from new_client.c:5:
/usr/include/x86_64-linux-gnu/bits/socket.h:180:8: error: redefinition of ‘struct sockaddr’
/usr/src/linux-headers-3.8.0-29-generic/include/uapi/linux/socket.h:19:27: note: originally defined here
/usr/include/x86_64-linux-gnu/bits/socket.h:193:8: error: redefinition of ‘struct __kernel_sockaddr_storage’
/usr/src/linux-headers-3.8.0-29-generic/include/uapi/linux/socket.h:13:8: note: originally defined here
/usr/include/x86_64-linux-gnu/bits/socket.h:204:5: error: expected identifier before numeric constant
/usr/include/x86_64-linux-gnu/bits/socket.h:253:8: error: redefinition of ‘struct msghdr’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/socket.h:47:8: note: originally defined here
/usr/include/x86_64-linux-gnu/bits/socket.h:280:8: error: redefinition of ‘struct cmsghdr’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/socket.h:69:8: note: originally defined here
/usr/include/x86_64-linux-gnu/bits/socket.h:310:24: error: conflicting types for ‘__cmsg_nxthdr’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/socket.h:111:32: note: previous definition of ‘__cmsg_nxthdr’ was here
/usr/include/x86_64-linux-gnu/bits/socket.h:339:5: error: expected identifier before numeric constant
In file included from /usr/include/x86_64-linux-gnu/sys/socket.h:40:0,
                 from /usr/include/netinet/tcp.h:57,
                 from new_client.c:5:
/usr/include/x86_64-linux-gnu/bits/socket.h:417:8: error: redefinition of ‘struct linger’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/socket.h:34:8: note: originally defined here
In file included from new_client.c:6:0:
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:24:8: error: redefinition of ‘struct tcphdr’
/usr/include/netinet/tcp.h:92:8: note: originally defined here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:135:6: error: nested redefinition of ‘enum tcp_ca_state’
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:135:6: error: redeclaration of ‘enum tcp_ca_state’
/usr/include/netinet/tcp.h:179:6: note: originally defined here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:136:2: error: redeclaration of enumerator ‘TCP_CA_Open’
/usr/include/netinet/tcp.h:181:3: note: previous definition of ‘TCP_CA_Open’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:138:2: error: redeclaration of enumerator ‘TCP_CA_Disorder’
/usr/include/netinet/tcp.h:182:3: note: previous definition of ‘TCP_CA_Disorder’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:140:2: error: redeclaration of enumerator ‘TCP_CA_CWR’
/usr/include/netinet/tcp.h:183:3: note: previous definition of ‘TCP_CA_CWR’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:142:2: error: redeclaration of enumerator ‘TCP_CA_Recovery’
/usr/include/netinet/tcp.h:184:3: note: previous definition of ‘TCP_CA_Recovery’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:144:2: error: redeclaration of enumerator ‘TCP_CA_Loss’
/usr/include/netinet/tcp.h:185:3: note: previous definition of ‘TCP_CA_Loss’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:148:8: error: redefinition of ‘struct tcp_info’
/usr/include/netinet/tcp.h:188:8: note: originally defined here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:193:8: error: redefinition of ‘struct tcp_md5sig’
/usr/include/netinet/tcp.h:235:8: note: originally defined here

1 个解决方案

#1


4  

This is really not useful

这真的没有用

#include "/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h"

Because either the kernel supports it or it does not. You can check for it like this:

因为内核要么支持它,要么不支持。你可以这样检查:

find /usr/include -type f -exec grep TCP_REPAIR {} /dev/null \;
/usr/include/linux/tcp.h:#define TCP_REPAIR     19  /* TCP sock is under repair right now */
/usr/include/linux/tcp.h:#define TCP_REPAIR_QUEUE   20
/usr/include/linux/tcp.h:#define TCP_REPAIR_OPTIONS 22

if it's there, great, otherwise you'll have to use the workaround.

如果它在那里,很好,否则你将不得不使用变通方法。

Here's an Ubuntu that has it

这是一个Ubuntu

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.10
Release:        12.10
Codename:       quantal

It looks like SOL_TCP is only defined in /usr/include/netinet/tcp.h

看起来SOL_TCP只在/usr/include/netinet/tcp.h中定义

find /usr/include -type f -exec grep SOL_TCP {} /dev/null \;
/usr/include/netinet/tcp.h:# define SOL_TCP     6   /* TCP level */

To overcome the unfortunate absence of SOL_TCP in /usr/include/linux/tcp.h. You can try to simply copy it from netinet/tcp.h, it's just a number after all.

为了克服/usr/include/linux/tcp.h中SOL_TCP的不幸缺失您可以尝试从netinet/tcp复制它。h,它毕竟只是一个数字。

#ifndef SOL_TCP
#define SOL_TCP 6
#endif

However, the man page of setsockopt offers an even better approach:

不过,setsockopt的man页面提供了一种更好的方法:

To manipulate options at any other level the protocol number of the appropriate protocol controlling the option is supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP; see getprotoent(3).

要在任何其他级别操作选项,提供控制选项的适当协议的协议编号。例如,为了表示一个选项将由TCP协议解释,应该将级别设置为TCP的协议号;看到getprotoent(3)。


推荐阅读
  • 本文介绍如何在C++中通过一个实用工具类来调用其他类的方法,具体包括生成UUID和获取当前时间的功能。 ... [详细]
  • PHP 5.4.8 编译安装指南
    本文详细介绍了如何在Linux环境下编译安装PHP 5.4.8,并配置为FastCGI模式运行。包括所需依赖包的安装、源代码下载、编译配置及启动服务等步骤。 ... [详细]
  • 本文介绍如何使用Java实现AC自动机(Aho-Corasick算法),以实现高效的多模式字符串匹配。文章涵盖了Trie树和KMP算法的基础知识,并提供了一个详细的代码示例,包括构建Trie树、设置失败指针以及执行搜索的过程。 ... [详细]
  • VSCode中实现大型项目函数跳转的方法
    在处理大型代码项目时,简单的C/C++插件往往无法满足需求。本文介绍如何通过配置GNU Global等工具,在VSCode中实现高效的函数跳转。 ... [详细]
  • 2020年腾讯PCG后端开发实习生面试经历分享
    本文详细记录了2020年腾讯平台与内容事业群(PCG)后端开发实习生岗位的面试过程,包括初试和复试的主要内容和技术考察点。 ... [详细]
  • 本文探讨了为何在Linux系统上进行项目开发的重要性,并详细介绍了如何在Ubuntu系统上安装PyCharm这一流行的Python集成开发环境(IDE)。通过本文,您将了解在Linux环境下进行Python开发的优势及具体安装步骤。 ... [详细]
  • 圣诞夜的驯鹿之谜:鲁道夫是公是母?
    野生动物专家探讨圣诞老人座驾上的驯鹿性别之谜 ... [详细]
  • Minetest 0.4.9 开源游戏在 Ubuntu 下通过 PPA 安装指南
    本文介绍了如何在 Ubuntu 系统上安装最新版本的 Minetest 0.4.9,包括添加 PPA、更新软件包列表以及安装过程,适合所有 Ubuntu 及其衍生系统的用户。 ... [详细]
  • 万事起于配置开发环境
    万事起于配置开发环境 ... [详细]
  • 本文档提供了详细的MySQL安装步骤,包括解压安装文件、选择安装类型、配置MySQL服务以及设置管理员密码等关键环节,帮助用户顺利完成MySQL的安装。 ... [详细]
  • 本文介绍了如何将Linux系统中的YUM源更换为阿里云镜像源,包括网络连通性测试、原YUM源的移除、阿里云YUM源的配置以及缓存的更新等步骤。 ... [详细]
  • 本文探讨了 Boost 库中的 Program Options 组件,这是一个强大的工具,用于解析命令行参数和配置文件。文章介绍了如何正确设置和使用该组件,包括处理复杂选项和负数值的方法。 ... [详细]
  • Linux文件目录和权限
    Linux文件目录和权限前言:Linux一般将文件可存取的身份分为三个类别,分别是ownergroupothers,根据权限划分,每个目录都可以拥有相对身份的-rwx[可读可写可执 ... [详细]
  • 【重识云原生】第四章云网络4.8.3.2节——Open vSwitch工作原理详解
    2OpenvSwitch架构2.1OVS整体架构ovs-vswitchd:守护程序,实现交换功能,和Linux内核兼容模块一起,实现基于流的交换flow-basedswitchin ... [详细]
  • 在现代移动应用开发中,尤其是iOS应用,处理来自服务器的JSON数据是一项基本技能。无论是使用Swift还是PHP,有效地解析和利用JSON数据对于提升用户体验至关重要。本文将探讨如何在Swift中优雅地处理JSON,以及PHP中处理JSON的一些技巧。 ... [详细]
author-avatar
当初
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有