plan9port/src/libip/udp.c

54 lines
1.2 KiB
C
Raw Normal View History

2004-04-21 23:43:46 +00:00
#include <u.h>
#define NOPLAN9DEFINES
#include <libc.h>
#include <ip.h>
#include <sys/socket.h>
#include <netinet/in.h>
long
udpread(int fd, Udphdr *hdr, void *buf, long n)
{
struct sockaddr_in sin;
socklen_t len;
2005-05-07 22:37:26 +00:00
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;
2004-04-21 23:43:46 +00:00
len = sizeof sin;
n = recvfrom(fd, buf, n, 0, (struct sockaddr*)&sin, &len);
if(n < 0)
return -1;
if(len != sizeof sin){
werrstr("recvfrom acting weird");
return -1;
}
memmove(hdr->raddr, v4prefix, IPaddrlen);
*(u32int*)(hdr->raddr+12) = *(u32int*)&sin.sin_addr;
*(u16int*)hdr->rport = *(u16int*)&sin.sin_port;
2005-05-07 22:37:26 +00:00
2004-04-21 23:43:46 +00:00
return n;
}
long
udpwrite(int fd, Udphdr *hdr, void *buf, long n)
{
struct sockaddr_in sin;
memset(&sin, 0, sizeof sin);
sin.sin_family = AF_INET;
*(u32int*)&sin.sin_addr = *(u32int*)(hdr->raddr+12);
*(u16int*)&sin.sin_port = *(u16int*)hdr->rport;
return sendto(fd, buf, n, 0, (struct sockaddr*)&sin, sizeof sin);
}