1

こんにちは私はrawソケットを使用してパケットインジェクションを実行しようとしていますが、ioctlのSIOCGIFINDEXコマンドを使用してインターフェイスインデックスを取得する際に問題があります。OSとしてubuntu12.04を使用しています。コードを手伝ってください:

int BindRawSocketToInterface(char *device, int rawsock, int protocol)
{
struct sockaddr_ll sll;
struct ifreq ifr;
bzero(&sll, sizeof(sll));
bzero(&ifr, sizeof(ifr));

/* First Get the Interface Index */

strncpy ((char*) ifr.ifr_name, device, IFNAMSIZ);
if ((ioctl(rawsock, SIOCGIFINDEX, &ifr))== -1)
{
printf ("Error getting interface index!\n");
exit(-1);
}

/* Bind our rawsocket to this interface */

sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifr.ifr_ifindex;
sll.sll_protocol = htons(protocol);

if ((bind(rawsock, (struct sockaddr*)&sll,sizeof(sll)))== -1)
{
perror("Error binding raw socket to interface \n");
exit(-1);
}
return 1;
}
4

2 に答える 2

3

次に例を示します。

http://austinmarton.wordpress.com/2011/09/14/sending-raw-ethernet-packets-from-a-specific-interface-in-c-on-linux/

これが役立つことを願っています

于 2013-02-23T08:32:32.233 に答える