9 #include <sys/socket.h>
13 # define socklen_t int
19 localaddr(_local), remaddr(_rem)
22 setfd(socket(PF_INET, SOCK_RAW, ip_protocol));
24 || setsockopt(
getfd(), SOL_SOCKET, SO_REUSEADDR, &x,
sizeof(x)) < 0)
33 struct sockaddr *sa = _local.sockaddr();
34 if (bind(
getfd(), sa, _local.sockaddr_len()))
42 struct sockaddr_in nsa;
43 socklen_t nsalen =
sizeof(nsa);
44 if (getsockname(
getfd(), (sockaddr *)&nsa, &nsalen) < 0)
49 localaddr =
WvIPAddr((sockaddr*)&nsa);
53 struct sockaddr *sa = _rem.sockaddr();
54 if (connect(
getfd(), sa, _rem.sockaddr_len()))
65 WvIPRawStream::~WvIPRawStream()
76 const WvAddr *WvIPRawStream::local()
const
84 if (!
isok() || !buf || !count)
return 0;
86 struct sockaddr_in from;
87 socklen_t fromlen =
sizeof(from);
88 int in = recvfrom(
getfd(), buf, count, 0, (sockaddr *)&from, &fromlen);
91 remaddr =
WvIPAddr((sockaddr *)&from);
94 return in < 0 ? 0 : in;
100 if (!
isok() || !buf || !count)
return 0;
102 struct sockaddr *to = remaddr.sockaddr();
103 size_t tolen = remaddr.sockaddr_len();
106 out = sendto(
getfd(), buf, count, 0, to, tolen);
108 if (out < 0 && errno == EACCES)
117 return out < 0 ? 0 : out;
121 void WvIPRawStream::enable_broadcasts()
127 setsockopt(
getfd(), SOL_SOCKET, SO_BROADCAST, &value,
sizeof(value));
Base class for different address types, each of which will have the ability to convert itself to/from...
void setfd(int fd)
Sets the file descriptor for both reading and writing.
int getfd() const
Returns the Unix file descriptor for reading and writing.
virtual bool isok() const
return true if the stream is actually usable right now
void set_nonblock(bool nonblock)
Make the fds on this stream blocking or non-blocking.
void set_close_on_exec(bool close_on_exec)
Make the fds on this stream close-on-exec or not.
An IP address is made up of a "dotted quad" – four decimal numbers in the form www....
virtual size_t uwrite(const void *buf, size_t count)
unbuffered I/O functions; these ignore the buffer, which is handled by write().
WvIPRawStream(const WvIPAddr &_local, const WvIPAddr &_rem, int ip_protocol=IPPROTO_RAW)
connect a new socket
virtual const WvAddr * src() const
return the remote address (source of incoming packets, target of outgoing packets).
virtual size_t uread(void *buf, size_t count)
unbuffered I/O functions; these ignore the buffer, which is handled by read().
virtual void seterr(int _errnum)
Override seterr() from WvError so that it auto-closes the stream.