2014-01-30 77 views
1

按照这篇文章,我试图获得一个C程序来编译使用TPC_REPAIR。在Ubuntu上使用TCP_REPAIR套接字选项编译代码

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 
+3

你应该与你信任的人在你的系统上混淆。我们不是你,我们没有你的内核头文件。 – tmyklebu

回答

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 */ 

为了克服不幸没有SOL_TCP的/usr/include/linux/tcp.h。你可以尝试从netinet/tcp.h简单地复制它,毕竟它只是一个数字。

#ifndef SOL_TCP 
#define SOL_TCP 6 
#endif 

然而,是setsockopt man页面提供了一个更好的方法:

要在提供相应的协议控制选项的协议号的任何其他级别操作选项。例如,要指示一个选项要由TCP协议解释,则应将级别设置为TCP的协议号;见getprotoent(3)。

+0

将Ubuntu 12.04升级到12.10,现在我在正确的地方看到了TCP_REPAIR(/usr/include/linux/tcp.h)。谢谢。 – akirekadu

+0

当我包含linux/tcp.h时,它与netinet/tcp.h冲突。当我删除netinet/tcp.h时,编译器找不到SOL_TCP。有小费吗? – akirekadu

+0

是的,这是一个无赖,我已经更新了几个建议的回答 – waTeim