2014-01-30 100 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