diff options
author | rsc <devnull@localhost> | 2004-04-21 23:43:46 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2004-04-21 23:43:46 +0000 |
commit | 7e42d210121c9c2898caddcfc09d7e8d836c252e (patch) | |
tree | f83363d352e4ecfd3ae4c2d0c166cfa955c41c5d /src/libip | |
parent | e12fa2a49ba303cee8d302f1911bd55545ada98e (diff) | |
download | plan9port-7e42d210121c9c2898caddcfc09d7e8d836c252e.tar.gz plan9port-7e42d210121c9c2898caddcfc09d7e8d836c252e.tar.bz2 plan9port-7e42d210121c9c2898caddcfc09d7e8d836c252e.zip |
odds and ends
Diffstat (limited to 'src/libip')
-rw-r--r-- | src/libip/udp.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/libip/udp.c b/src/libip/udp.c new file mode 100644 index 00000000..8df03ba0 --- /dev/null +++ b/src/libip/udp.c @@ -0,0 +1,52 @@ +#include <u.h> +#define NOPLAN9DEFINES +#include <libc.h> +#include <ip.h> + +#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; + 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; + } + 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; +} + +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); +} + |