在Ubuntu上使用TCP_REPAIR套接字选项编译代码

时间:2014-01-30 02:08:40

标签: c linux sockets ubuntu

在这篇文章之后,我正在尝试使用TPC_REPAIR来编译一个C程序。

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

TCP_REPAIR在

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

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

这是我的代码

#include <stdio.h>
#include <unistd.h>
#include <linux/socket.h>
#include <uapi/linux/in.h>
#include <netinet/tcp.h>
#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;
}

这是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 个答案:

答案 0 :(得分:4)

这实在没用

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

因为内核支持它或不支持它。您可以这样检查:

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

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

这是一个拥有它的Ubuntu

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

看起来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 */

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

#ifndef SOL_TCP
#define SOL_TCP 6
#endif

但是,setsockopt的手册页提供了一种更好的方法:

  

要操纵任何其他级别的选项,请提供控制该选项的相应协议的协议编号。例如,要指示TCP协议要解释选项,应将级别设置为TCP的协议号;见getprotoent(3)。