Originally Posted by
matB
What about the ip?
Look at the 5th argument of sendto(), its const struct sockaddr *to.
You pass a struct sockaddr_in to it by typecast. The struct sockaddr_in looks like this:
Code:
struct sockaddr_in {
short sin_family;
unsigned short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct in_addr looks like this:
Code:
struct in_addr {
unsigned long s_addr;
};
So to store the ip 127.0.0.1, the port 1234, and AF_INET on the struct sockaddr_in, you'd do the following:
Code:
struct sockaddr_in self;
self.sin_family = AF_INET;
self.sin_addr.s_addr = inet_addr("127.0.0.1"); // you can also use inet_aton()
self.sin_port = htons(1234);
memset(self.sin_zero, '\0', sizeof(self.sin_zero));
Now, to send data to that ip, do the following:
(I'm assuming sock is already created and everything)
Code:
char data[16] = "Hello, world!";
if(sendto(sock, data, strlen(data), 0, (struct sockaddr*)&self, sizeof(self)) == -1)
fprintf(stderr, "sendto() failed\n");
Edit: Imho raw-sockets aren't very useful because they increase the complexity of your code and make it more prone to vulnerabilities and errors. You should use TCP or UDP sockets, they are more convenient to use and more stable, reliable. The chance that your code is faster than the kernel's ip stack is by the way very low.