Skip to content

Instantly share code, notes, and snippets.

@djkonro
Last active October 18, 2018 08:59
Show Gist options
  • Save djkonro/658ade8b5f0fcddad6555a22225a1d36 to your computer and use it in GitHub Desktop.
Save djkonro/658ade8b5f0fcddad6555a22225a1d36 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <arpa/inet.h>
static int s = -1;
static int s1 = -1;
void onexit(int signum)
{
(void)signum;
printf("Exiting");
close(s);
close(s1);
}
int main()
{
char buf[1600];
ssize_t recv_size = -1;
ssize_t send_size = 42;
int i = 0;
struct sockaddr_ll socket_address;
s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if ((s == -1))
{
perror("Socket creation failed");
exit (0);
}
signal(SIGINT, onexit);
memset(&socket_address, 0, sizeof (socket_address));
socket_address.sll_family = PF_PACKET;
socket_address.sll_ifindex = if_nametoindex("veth0");
socket_address.sll_protocol = htons(ETH_P_ALL);
i = bind(s, (struct sockaddr*)&socket_address,
sizeof(socket_address));
if (i == -1)
{
perror("Bind");
exit (0);
}
memset(&buf, 0, sizeof(buf));
buf[0] = 1;
buf[1] = 1;
buf[2] = 1;
buf[3] = 1;
buf[4] = 1;
buf[5] = 1;
buf[6] = 4;
buf[7] = 1;
buf[8] = 3;
buf[9] = 2;
buf[10] = 18;
buf[11] = 93;
buf[12] = 8;
buf[13] = 6;
buf[14] = 0;
buf[15] = 1;
buf[16] = 8;
buf[17] = 0;
buf[18] = 6;
buf[19] = 4;
buf[20] = 0;
buf[21] = 1;
buf[22] = 54;
buf[23] = 21;
buf[24] = -3;
buf[25] = 42;
buf[26] = -18;
buf[27] = -93;
buf[28] = -64;
buf[29] = -88;
buf[30] = 8;
buf[31] = 100;
buf[32] = 0;
buf[33] = 0;
buf[34] = 0;
buf[35] = 0;
buf[36] = 0;
buf[37] = 0;
buf[38] = -40;
buf[39] = 58;
buf[40] = -44;
buf[41] = 100;
send_size = send(s, &buf, send_size, 0);
printf("send size = %ld\n", send_size);
if (send_size == -1)
{
perror("Socket send");
exit (0);
}
recv_size = recv(s, &buf, sizeof(buf), 0);
printf("recv size %ld\n", recv_size);
if (recv_size == -1)
{
perror("Socket receive");
exit (0);
}
printf("\n");
for(i=0; i < recv_size; i++)
{
printf("%02x ", buf[i]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment