Zreszta masz przyklad kodu, ktory przed chwila napisalem do wysylania pakietow arp, z wysylaniem normalnych danych jest podobnie, ale innych struktur uzywasz wtedy i jest prosciej.
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#define DST_MAC "bb:bb:bb:bb:bb:bb"
#define SRC_MAC "aa:aa:aa:aa:aa:aa"
#define DST_IP "111.111.11.11"
#define SRC_IP "221.122.21.22"
// w /usr/include/linux/if_arp.h masz ta strukture, ale zmienilem jedna rzecz dlatego jest tutaj ;)
struct arphdr_t
{
__be16 ar_hrd; /* format of hardware address */
__be16 ar_pro; /* format of protocol address */
unsigned char ar_hln; /* length of hardware address */
unsigned char ar_pln; /* length of protocol address */
__be16 ar_op; /* ARP opcode (command) */
unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
unsigned char ar_sip[4]; /* sender IP address */
unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
unsigned char ar_tip[4]; /* target IP address */
};
int main(void)
{
int sock;
BYTE buff[1024] = {0};
struct ethhdr *eth = (struct ethhdr*)buff;
struct arphdr_t *arp = (struct arphdr_t*)(buff+sizeof(struct ethhdr));
struct ifreq ifr;
struct sockaddr_ll sll;
in_addr_t tmp;
sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
if(sock == -1)
{
perror("socket");
return -1;
}
memcpy(ð->h_dest, (void*)DST_MAC, 6);
memcpy(ð->h_source, (void*)SRC_MAC, 6);
eth->h_proto = htons(ETHERTYPE_ARP);
arp->ar_hrd = htons(ARPHRD_ETHER);
arp->ar_pro = htons(ETHERTYPE_IP);
arp->ar_hln = 6;
arp->ar_pln = 4;
arp->ar_op = htons(ARPOP_REPLY);
memcpy(&arp->ar_sha, (void*)SRC_MAC, 6);
tmp = inet_addr(SRC_IP);
memcpy(&arp->ar_sip, &tmp, 4);
memcpy(&arp->ar_tha, (void*)DST_MAC, 6);
tmp = inet_addr(DST_IP);
memcpy(&arp->ar_tip, &tmp, 4);
strncpy((char*)ifr.ifr_name, "wlan0", IFNAMSIZ);
if(ioctl(sock, SIOCGIFINDEX, &ifr) == -1)
{
perror("ioctl");
return -1;
}
sll.sll_family = PF_PACKET;
sll.sll_protocol = htons(ETH_P_ARP);
sll.sll_ifindex = ifr.ifr_ifindex;
if(sendto(sock, buff, sizeof(struct ethhdr)+sizeof(struct arphdr_t), 0, (struct sockaddr*)&sll, sizeof(sll)) == -1)
{
perror("sendto");
return -1;
}
close(sock);
return 0;
}