Created
November 2, 2019 22:39
-
-
Save rkday/62f780bd803aede124cddae82108df9a to your computer and use it in GitHub Desktop.
Bind raw socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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