Skip to content

Instantly share code, notes, and snippets.

@rkday
Created November 2, 2019 22:39
Show Gist options
  • Save rkday/62f780bd803aede124cddae82108df9a to your computer and use it in GitHub Desktop.
Save rkday/62f780bd803aede124cddae82108df9a to your computer and use it in GitHub Desktop.
Bind raw socket
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
int main() {
int s = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
u_int8_t ip[4] = {127, 0, 0, 1};
struct sockaddr_storage addr;
((struct sockaddr_in*)&addr)->sin_family = AF_INET;
((struct sockaddr_in*)&addr)->sin_port = 1500;
memcpy(&(((struct sockaddr_in*)&addr)->sin_addr), &ip, 4);
int rc = bind(s, (struct sockaddr*)&addr, sizeof(addr));
printf("Bind result: %d / %d / %s\n", rc, errno, strerror(errno));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment