mirror of
https://github.com/9fans/plan9port.git
synced 2025-01-12 11:10:07 +00:00
try to do better with headers
This commit is contained in:
parent
fc11cb4bbb
commit
857072dc11
1 changed files with 13 additions and 12 deletions
|
@ -6,23 +6,24 @@
|
|||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
/*
|
||||
* prefix of all v4 addresses
|
||||
* copied from libip because libc cannot depend on libip
|
||||
*/
|
||||
static uchar v4prefix[IPaddrlen] = {
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0xff, 0xff,
|
||||
0, 0, 0, 0
|
||||
};
|
||||
|
||||
long
|
||||
udpread(int fd, Udphdr *hdr, void *buf, long n)
|
||||
{
|
||||
struct sockaddr_in sin;
|
||||
socklen_t len;
|
||||
|
||||
len = sizeof sin;
|
||||
if(getsockname(fd, (struct sockaddr*)&sin, &len) < 0)
|
||||
return -1;
|
||||
if(len != sizeof sin){
|
||||
werrstr("getsockname acting weird");
|
||||
return -1;
|
||||
}
|
||||
memset(hdr, 0, sizeof *hdr);
|
||||
memmove(hdr->laddr, v4prefix, IPaddrlen);
|
||||
*(u32int*)(hdr->laddr+12) = *(u32int*)&sin.sin_addr;
|
||||
*(u16int*)hdr->lport = *(u16int*)&sin.sin_port;
|
||||
|
||||
len = sizeof sin;
|
||||
n = recvfrom(fd, buf, n, 0, (struct sockaddr*)&sin, &len);
|
||||
if(n < 0)
|
||||
|
@ -31,10 +32,10 @@ udpread(int fd, Udphdr *hdr, void *buf, long n)
|
|||
werrstr("recvfrom acting weird");
|
||||
return -1;
|
||||
}
|
||||
memset(hdr, 0, sizeof *hdr);
|
||||
memmove(hdr->raddr, v4prefix, IPaddrlen);
|
||||
*(u32int*)(hdr->raddr+12) = *(u32int*)&sin.sin_addr;
|
||||
*(u16int*)hdr->rport = *(u16int*)&sin.sin_port;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue