Skip to content

Instantly share code, notes, and snippets.

@kings-way
Last active October 14, 2019 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kings-way/1f20ca2cbc915a4cee6a844df4aada81 to your computer and use it in GitHub Desktop.
Save kings-way/1f20ca2cbc915a4cee6a844df4aada81 to your computer and use it in GitHub Desktop.
send raw packet using 'AF_PACKET' and 'send()', instead of 'sendto'
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
int main()
{
unsigned char data[] = {
1, 2, 3, 4, 5, 6, 7, 8,
9, 0, 1, 2, 3, 4, 5, 6
};
char *interface_name = (char *) "eth0";
int fd = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
if (fd < 0) {
perror("socket");
}
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
addr.sll_family = AF_PACKET;
addr.sll_ifindex = if_nametoindex(interface_name);
addr.sll_protocol = htons(ETH_P_ALL);
if (bind(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
perror("Bind");
close(fd);
}
if (send(fd, data, sizeof(data), 0) <0 ){
perror("Send");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment