2012-07-10 07:03:39 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2012 Serval Project Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2012-08-23 03:01:07 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2013-10-11 17:24:18 +00:00
|
|
|
#include <sys/uio.h>
|
2012-09-14 02:17:48 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2012-09-18 02:56:30 +00:00
|
|
|
#include <time.h>
|
2012-12-11 05:29:46 +00:00
|
|
|
|
|
|
|
#include "serval.h"
|
|
|
|
#include "conf.h"
|
2012-08-23 03:01:07 +00:00
|
|
|
#include "net.h"
|
2012-11-20 03:13:51 +00:00
|
|
|
#include "str.h"
|
2013-10-11 17:24:18 +00:00
|
|
|
#include "strbuf_helpers.h"
|
2012-07-10 07:03:39 +00:00
|
|
|
|
2013-02-05 06:26:37 +00:00
|
|
|
struct in_addr hton_in_addr(in_addr_t addr)
|
|
|
|
{
|
|
|
|
struct in_addr a;
|
|
|
|
a.s_addr = htonl(addr);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
int _set_nonblock(int fd, struct __sourceloc __whence)
|
2012-07-10 07:03:39 +00:00
|
|
|
{
|
|
|
|
int flags;
|
2012-10-16 06:16:52 +00:00
|
|
|
if ((flags = fcntl(fd, F_GETFL, NULL)) == -1)
|
|
|
|
return WHYF_perror("set_nonblock: fcntl(%d,F_GETFL,NULL)", fd);
|
|
|
|
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
|
|
|
|
return WHYF_perror("set_nonblock: fcntl(%d,F_SETFL,0x%x|O_NONBLOCK)", fd, flags);
|
2012-07-10 07:03:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
int _set_block(int fd, struct __sourceloc __whence)
|
2012-07-10 07:03:39 +00:00
|
|
|
{
|
|
|
|
int flags;
|
2012-10-16 06:16:52 +00:00
|
|
|
if ((flags = fcntl(fd, F_GETFL, NULL)) == -1)
|
|
|
|
return WHYF_perror("set_block: fcntl(%d,F_GETFL,NULL)", fd);
|
|
|
|
if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1)
|
|
|
|
return WHYF_perror("set_block: fcntl(%d,F_SETFL,0x%x&~O_NONBLOCK)", fd, flags);
|
2012-07-10 07:03:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
ssize_t _read_nonblock(int fd, void *buf, size_t len, struct __sourceloc __whence)
|
2012-07-13 01:34:50 +00:00
|
|
|
{
|
|
|
|
ssize_t nread = read(fd, buf, len);
|
|
|
|
if (nread == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
case EAGAIN:
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-16 06:16:52 +00:00
|
|
|
return WHYF_perror("read_nonblock: read(%d,%p,%lu)", fd, buf, (unsigned long)len);
|
2012-07-13 01:34:50 +00:00
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
ssize_t _write_all(int fd, const void *buf, size_t len, struct __sourceloc __whence)
|
2012-07-10 07:03:39 +00:00
|
|
|
{
|
|
|
|
ssize_t written = write(fd, buf, len);
|
2012-10-16 06:16:52 +00:00
|
|
|
if (written == -1)
|
2013-10-11 05:36:12 +00:00
|
|
|
return WHYF_perror("write_all: write(%d,%p %s,%zu)",
|
|
|
|
fd, buf, alloca_toprint(30, buf, len), len);
|
2012-10-16 06:16:52 +00:00
|
|
|
if (written != len)
|
2013-10-11 05:36:12 +00:00
|
|
|
return WHYF_perror("write_all: write(%d,%p %s,%zu) returned %zd",
|
|
|
|
fd, buf, alloca_toprint(30, buf, len), len, (size_t)written);
|
2012-07-10 07:03:39 +00:00
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2013-10-11 17:24:18 +00:00
|
|
|
ssize_t _writev_all(int fd, const struct iovec *iov, int iovcnt, struct __sourceloc __whence)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < iovcnt; ++i)
|
|
|
|
len += iov[i].iov_len;
|
|
|
|
ssize_t written = writev(fd, iov, iovcnt);
|
|
|
|
if (written == -1)
|
|
|
|
return WHYF_perror("writev_all: writev(%d,%s len=%zu)", fd, alloca_iovec(iov, iovcnt), len);
|
|
|
|
if (written != len)
|
|
|
|
return WHYF_perror("writev_all: writev(%d,%s len=%zu) returned %zd", fd, alloca_iovec(iov, iovcnt), len, (size_t)written);
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
ssize_t _write_nonblock(int fd, const void *buf, size_t len, struct __sourceloc __whence)
|
2012-07-10 07:03:39 +00:00
|
|
|
{
|
|
|
|
ssize_t written = write(fd, buf, len);
|
|
|
|
if (written == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
2012-07-13 01:34:50 +00:00
|
|
|
case EAGAIN:
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
2012-07-10 07:03:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-10-16 06:16:52 +00:00
|
|
|
return WHYF_perror("write_nonblock: write(%d,%p %s,%lu)",
|
2012-07-11 04:47:53 +00:00
|
|
|
fd, buf, alloca_toprint(30, buf, len), (unsigned long)len);
|
2012-07-10 09:21:41 +00:00
|
|
|
return -1;
|
2012-07-10 07:03:39 +00:00
|
|
|
}
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
ssize_t _write_all_nonblock(int fd, const void *buf, size_t len, struct __sourceloc __whence)
|
2012-07-10 07:03:39 +00:00
|
|
|
{
|
2012-10-16 06:16:52 +00:00
|
|
|
ssize_t written = _write_nonblock(fd, buf, len, __whence);
|
|
|
|
if (written != -1 && written != len)
|
|
|
|
return WHYF("write_all_nonblock: write(%d,%p %s,%lu) returned %ld",
|
2012-07-11 04:47:53 +00:00
|
|
|
fd, buf, alloca_toprint(30, buf, len), (unsigned long)len, (long)written);
|
2012-07-10 07:03:39 +00:00
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
ssize_t _write_str(int fd, const char *str, struct __sourceloc __whence)
|
2012-07-10 07:03:39 +00:00
|
|
|
{
|
2012-10-16 06:16:52 +00:00
|
|
|
return _write_all(fd, str, strlen(str), __whence);
|
2012-07-10 07:03:39 +00:00
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
ssize_t _write_str_nonblock(int fd, const char *str, struct __sourceloc __whence)
|
2012-07-10 07:03:39 +00:00
|
|
|
{
|
2012-10-16 06:16:52 +00:00
|
|
|
return _write_all_nonblock(fd, str, strlen(str), __whence);
|
2012-07-10 07:03:39 +00:00
|
|
|
}
|
2012-09-14 02:17:48 +00:00
|
|
|
|
|
|
|
ssize_t recvwithttl(int sock,unsigned char *buffer, size_t bufferlen,int *ttl,
|
|
|
|
struct sockaddr *recvaddr, socklen_t *recvaddrlen)
|
|
|
|
{
|
|
|
|
struct msghdr msg;
|
|
|
|
struct iovec iov[1];
|
|
|
|
|
|
|
|
iov[0].iov_base=buffer;
|
|
|
|
iov[0].iov_len=bufferlen;
|
|
|
|
bzero(&msg,sizeof(msg));
|
|
|
|
msg.msg_name = recvaddr;
|
|
|
|
msg.msg_namelen = *recvaddrlen;
|
|
|
|
msg.msg_iov = &iov[0];
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
// setting the following makes the data end up in the wrong place
|
|
|
|
// msg.msg_iov->iov_base=iov_buffer;
|
|
|
|
// msg.msg_iov->iov_len=sizeof(iov_buffer);
|
|
|
|
|
|
|
|
struct cmsghdr cmsgcmsg[16];
|
|
|
|
msg.msg_control = &cmsgcmsg[0];
|
|
|
|
msg.msg_controllen = sizeof(struct cmsghdr)*16;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
|
|
|
|
ssize_t len = recvmsg(sock,&msg,0);
|
|
|
|
if (len == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
|
|
|
|
return WHY_perror("recvmsg");
|
|
|
|
|
2013-09-19 07:56:06 +00:00
|
|
|
#if 0
|
|
|
|
if (config.debug.packetrx) {
|
2013-07-15 00:29:24 +00:00
|
|
|
DEBUGF("recvmsg returned %d (flags=%d, msg_controllen=%d)", (int) len, msg.msg_flags, (int)msg.msg_controllen);
|
2012-09-14 02:17:48 +00:00
|
|
|
dump("received data", buffer, len);
|
|
|
|
}
|
2013-09-19 07:56:06 +00:00
|
|
|
#endif
|
2012-09-14 02:17:48 +00:00
|
|
|
|
2013-09-19 07:56:06 +00:00
|
|
|
if (len > 0) {
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
|
|
|
if ( cmsg->cmsg_level == IPPROTO_IP
|
|
|
|
&& ((cmsg->cmsg_type == IP_RECVTTL) || (cmsg->cmsg_type == IP_TTL))
|
|
|
|
&& cmsg->cmsg_len
|
|
|
|
) {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.packetrx)
|
2012-09-14 02:17:48 +00:00
|
|
|
DEBUGF(" TTL (%p) data location resolves to %p", ttl,CMSG_DATA(cmsg));
|
|
|
|
if (CMSG_DATA(cmsg)) {
|
|
|
|
*ttl = *(unsigned char *) CMSG_DATA(cmsg);
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.packetrx)
|
2012-09-14 02:17:48 +00:00
|
|
|
DEBUGF(" TTL of packet is %d", *ttl);
|
|
|
|
}
|
|
|
|
} else {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.packetrx)
|
2012-09-14 02:17:48 +00:00
|
|
|
DEBUGF("I didn't expect to see level=%02x, type=%02x",
|
|
|
|
cmsg->cmsg_level,cmsg->cmsg_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*recvaddrlen=msg.msg_namelen;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|