当我编译程序时,它给出了
In file included from /usr/include/linux/netdevice.h:29,
from /usr/include/linux/if_arp.h:27,
from rcping.c:6:
/usr/include/net/if.h:44:5: error: redeclaration of enumerator 'IFF_UP'
44 | IFF_UP = 0x1, /* Interface is up. */
| ^~~~~~
/usr/include/linux/if.h:83:9: note: previous definition of 'IFF_UP' with type 'enum net_device_flags'
83 | IFF_UP = 1<<0, /* sysfs */
| ^~~~~~
/usr/include/net/if.h:46:5: error: redeclaration of enumerator 'IFF_BROADCAST'
46 | IFF_BROADCAST = 0x2, /* Broadcast address valid. */
| ^~~~~~~~~~~~~
/usr/include/linux/if.h:84:9: note: previous definition of 'IFF_BROADCAST' with type 'enum net_device_flags'
84 | IFF_BROADCAST = 1<<1, /* __volatile__ */
| ^~~~~~~~~~~~~
/usr/include/net/if.h:48:5: error: redeclaration of enumerator 'IFF_DEBUG'
48 | IFF_DEBUG = 0x4, /* Turn on debugging. */
| ^~~~~~~~~
/usr/include/linux/if.h:85:9: note: previous definition of 'IFF_DEBUG' with type 'enum net_device_flags'
85 | IFF_DEBUG = 1<<2, /* sysfs */
| ^~~~~~~~~
/usr/include/net/if.h:50:5: error: redeclaration of enumerator 'IFF_LOOPBACK'
50 | IFF_LOOPBACK = 0x8, /* Is a loopback net. */
| ^~~~~~~~~~~~
/usr/include/linux/if.h:86:9: note: previous definition of 'IFF_LOOPBACK' with type 'enum net_device_flags'
86 | IFF_LOOPBACK = 1<<3, /* __volatile__ */
| ^~~~~~~~~~~~
/usr/include/net/if.h:52:5: error: redeclaration of enumerator 'IFF_POINTOPOINT'
52 | IFF_POINTOPOINT = 0x10, /* Interface is point-to-point link. */
| ^~~~~~~~~~~~~~~
/usr/include/linux/if.h:87:9: note: previous definition of 'IFF_POINTOPOINT' with type 'enum net_device_flags'
87 | IFF_POINTOPOINT = 1<<4, /* __volatile__ */
| ^~~~~~~~~~~~~~~
/usr/include/net/if.h:54:5: error: redeclaration of enumerator 'IFF_NOTRAILERS'
54 | IFF_NOTRAILERS = 0x20, /* Avoid use of trailers. */
| ^~~~~~~~~~~~~~
/usr/include/linux/if.h:88:9: note: previous definition of 'IFF_NOTRAILERS' with type 'enum net_device_flags'
88 | IFF_NOTRAILERS = 1<<5, /* sysfs */
| ^~~~~~~~~~~~~~
/usr/include/net/if.h:56:5: error: redeclaration of enumerator 'IFF_RUNNING'
56 | IFF_RUNNING = 0x40, /* Resources allocated. */
| ^~~~~~~~~~~
/usr/include/linux/if.h:89:9: note: previous definition of 'IFF_RUNNING' with type 'enum net_device_flags'
89 | IFF_RUNNING = 1<<6, /* __volatile__ */
| ^~~~~~~~~~~
/usr/include/net/if.h:58:5: error: redeclaration of enumerator 'IFF_NOARP'
58 | IFF_NOARP = 0x80, /* No address resolution protocol. */
| ^~~~~~~~~
/usr/include/linux/if.h:90:9: note: previous definition of 'IFF_NOARP' with type 'enum net_device_flags'
90 | IFF_NOARP = 1<<7, /* sysfs */
| ^~~~~~~~~
/usr/include/net/if.h:60:5: error: redeclaration of enumerator 'IFF_PROMISC'
60 | IFF_PROMISC = 0x100, /* Receive all packets. */
| ^~~~~~~~~~~
/usr/include/linux/if.h:91:9: note: previous definition of 'IFF_PROMISC' with type 'enum net_device_flags'
91 | IFF_PROMISC = 1<<8, /* sysfs */
| ^~~~~~~~~~~
/usr/include/net/if.h:64:5: error: redeclaration of enumerator 'IFF_ALLMULTI'
64 | IFF_ALLMULTI = 0x200, /* Receive all multicast packets. */
| ^~~~~~~~~~~~
/usr/include/linux/if.h:92:9: note: previous definition of 'IFF_ALLMULTI' with type 'enum net_device_flags'
92 | IFF_ALLMULTI = 1<<9, /* sysfs */
| ^~~~~~~~~~~~
/usr/include/net/if.h:67:5: error: redeclaration of enumerator 'IFF_MASTER'
67 | IFF_MASTER = 0x400, /* Master of a load balancer. */
| ^~~~~~~~~~
/usr/include/linux/if.h:93:9: note: previous definition of 'IFF_MASTER' with type 'enum net_device_flags'
93 | IFF_MASTER = 1<<10, /* __volatile__ */
| ^~~~~~~~~~
/usr/include/net/if.h:69:5: error: redeclaration of enumerator 'IFF_SLAVE'
69 | IFF_SLAVE = 0x800, /* Slave of a load balancer. */
| ^~~~~~~~~
/usr/include/linux/if.h:94:9: note: previous definition of 'IFF_SLAVE' with type 'enum net_device_flags'
94 | IFF_SLAVE = 1<<11, /* __volatile__ */
| ^~~~~~~~~
/usr/include/net/if.h:72:5: error: redeclaration of enumerator 'IFF_MULTICAST'
72 | IFF_MULTICAST = 0x1000, /* Supports multicast. */
| ^~~~~~~~~~~~~
/usr/include/linux/if.h:95:9: note: previous definition of 'IFF_MULTICAST' with type 'enum net_device_flags'
95 | IFF_MULTICAST = 1<<12, /* sysfs */
| ^~~~~~~~~~~~~
/usr/include/net/if.h:75:5: error: redeclaration of enumerator 'IFF_PORTSEL'
75 | IFF_PORTSEL = 0x2000, /* Can set media type. */
| ^~~~~~~~~~~
/usr/include/linux/if.h:96:9: note: previous definition of 'IFF_PORTSEL' with type 'enum net_device_flags'
96 | IFF_PORTSEL = 1<<13, /* sysfs */
| ^~~~~~~~~~~
/usr/include/net/if.h:77:5: error: redeclaration of enumerator 'IFF_AUTOMEDIA'
77 | IFF_AUTOMEDIA = 0x4000, /* Auto media select active. */
| ^~~~~~~~~~~~~
/usr/include/linux/if.h:97:9: note: previous definition of 'IFF_AUTOMEDIA' with type 'enum net_device_flags'
97 | IFF_AUTOMEDIA = 1<<14, /* sysfs */
| ^~~~~~~~~~~~~
/usr/include/net/if.h:79:5: error: redeclaration of enumerator 'IFF_DYNAMIC'
79 | IFF_DYNAMIC = 0x8000 /* Dialup device with changing addresses. */
| ^~~~~~~~~~~
/usr/include/linux/if.h:98:9: note: previous definition of 'IFF_DYNAMIC' with type 'enum net_device_flags'
98 | IFF_DYNAMIC = 1<<15, /* sysfs */
| ^~~~~~~~~~~
In file included from rcping.c:7:
/usr/include/net/if.h:111:8: error: redefinition of 'struct ifmap'
111 | struct ifmap
| ^~~~~
/usr/include/linux/if.h:194:8: note: originally defined here
194 | struct ifmap {
| ^~~~~
/usr/include/net/if.h:126:8: error: redefinition of 'struct ifreq'
126 | struct ifreq
| ^~~~~
/usr/include/linux/if.h:232:8: note: originally defined here
232 | struct ifreq {
| ^~~~~
/usr/include/net/if.h:176:8: error: redefinition of 'struct ifconf'
176 | struct ifconf
| ^~~~~~
/usr/include/linux/if.h:284:8: note: originally defined here
284 | struct ifconf {
我正在编译的代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/if_arp.h>
#include <net/if.h>
#include <asm/types.h>
#include <libcap.h>
#include <string.h>
#define RCP 49152
#define sizepkt 512
struct RCP_packet {
uint16_t srcport;
uint16_t extport;
char* payload;
};
int main(int argc, char* argv[]) {
int intextport = atoi(argv[1]);
cap_t caps = cap_init();
cap_value_t *capvl = 0;
cap_set_flag(caps, CAP_NET_RAW, 1, capvl, CAP_SET);
int sockfd = socket(AF_PACKET, SOCK_RAW, RCP);
if (sockfd < 0) {
printf("Socket not created\n");
printf("%d\n", sockfd);
return -1;
} else {
printf("Socket Created successfully\n");
}
struct sockaddr_in *dst_addr;
char buffer[sizepkt];
dst_addr->sin_family = AF_INET;
dst_addr->sin_port = htons(intextport);
inet_pton(AF_INET, "127.0.0.1", &dst_addr->sin_addr);
struct sockaddr_ll *src_addr;
src_addr->sll_family = AF_PACKET;
src_addr->sll_protocol = htons(1560);
src_addr->sll_ifindex = if_nametoindex("wlan0");
struct sockaddr *addr = (struct sockaddr *)&dst_addr;
struct sockaddr *interlcal = (struct sockaddr *)&src_addr;
struct RCP_packet *packet;
packet->srcport = htons(49152);
packet->extport = htons(intextport);
strcpy(packet->payload, "This packet was sent using RCPing under the Really Crappy Protocol");
memcpy(buffer, &packet->srcport, sizeof(packet->srcport));
memcpy(buffer + sizeof(packet->srcport), &packet->extport, sizeof(packet->extport));
memcpy(buffer + sizeof(packet->srcport) + sizeof(packet->extport), packet->payload, 8);
struct RCP_packet *rcp;
rcp->srcport = htons(1560);
rcp->extport = htons(intextport);
int miku = bind(sockfd, interlcal, sizeof(interlcal));
if (miku < 0) {
printf("Local interface couldn't be set up\n");
printf("%d", miku);
return -1;
}
int jumanji = connect(sockfd, addr, sizeof(addr));
if (jumanji < 0) {
printf("Couldn't establish a connection using RCP, unsurprisingly\n");
printf("%d", jumanji);
return -1;
} else {
while (main) {
ssize_t senresult = send(sockfd, buffer, sizeof(buffer), 0);
if (senresult < 0) {
printf("Package wasn't sent");
}
}
}
}
发生这种情况是因为系统中有两组网络标头:一组位于
/usr/include/linux
下,另一组位于 /usr/include/net
下。标头实际上是相同的,但它们的包含保护定义了不同的预处理器符号。 #include <linux/if_arp.h>
指前者,#include <net/if.h>
指后者。
在这个问题的答案中探讨了两组标题之间的差异。
应通过对包含内容进行以下更改来解决问题:
// #include <linux/if_arp.h>
#include <net/if_arp.h>