amiga-ntimed/udp.c

199 lines
4.4 KiB
C
Raw Normal View History

2014-12-21 22:31:18 +00:00
/*-
2015-03-12 21:48:56 +00:00
* Copyright (c) 2015 Carsten Larsen
2014-12-21 22:31:18 +00:00
* Copyright (c) 2014 Poul-Henning Kamp
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
2015-01-04 12:05:26 +00:00
#include <math.h>
#include <stdlib.h>
2014-12-21 22:31:18 +00:00
#include <string.h>
2015-03-12 21:48:56 +00:00
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/uio.h>
2015-03-07 22:29:12 +00:00
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
2015-04-18 20:08:39 +00:00
#include "ntimed_platform.h"
2015-03-12 21:48:56 +00:00
#include "ntimed.h"
#include "udp.h"
2015-03-07 22:29:12 +00:00
2015-01-04 12:05:26 +00:00
struct udp_socket {
unsigned magic;
#define UDP_SOCKET_MAGIC 0x302a563f
int fd4;
};
static int
udp_sock(int fam)
2014-12-21 22:31:18 +00:00
{
int fd;
2015-07-10 21:00:05 +00:00
int i;
2014-12-21 22:31:18 +00:00
fd = socket(fam, SOCK_DGRAM, 0);
2015-07-10 21:00:05 +00:00
if (fd < 0)
return (fd);
#ifdef SO_TIMESTAMPNS
i = 1;
(void)setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &i, sizeof i);
#elif defined(SO_TIMESTAMP)
i = 1;
(void)setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &i, sizeof i);
#endif
2014-12-21 22:31:18 +00:00
return (fd);
}
2015-01-04 12:05:26 +00:00
struct udp_socket *
UdpTimedSocket(struct ocx *ocx)
{
struct udp_socket *usc;
ALLOC_OBJ(usc, UDP_SOCKET_MAGIC);
AN(usc);
usc->fd4 = udp_sock(AF_INET);
2015-03-07 22:29:12 +00:00
if (usc->fd4 < 0)
2015-03-12 21:48:56 +00:00
Fail(ocx, 1, "socket failed");
2015-01-04 12:05:26 +00:00
return (usc);
}
2015-03-12 21:48:56 +00:00
void
UDP_Socket_Destroy(struct udp_socket *usc)
{
CHECK_OBJ_NOTNULL(usc, UDP_SOCKET_MAGIC);
#ifdef AROS
CloseSocket(usc->fd4);
#endif
FREE_OBJ(usc);
}
2014-12-21 22:31:18 +00:00
ssize_t
2015-01-04 12:05:26 +00:00
UdpTimedRx(struct ocx *ocx, const struct udp_socket *usc,
sa_family_t fam,
2015-01-04 12:05:26 +00:00
struct sockaddr_storage *ss, socklen_t *sl,
struct timestamp *ts, void *buf, ssize_t len, double tmo)
2014-12-21 22:31:18 +00:00
{
struct msghdr msg;
struct iovec iov;
struct cmsghdr *cmsg;
u_char ctrl[1024];
ssize_t rl;
int i;
2015-01-04 12:05:26 +00:00
int tmo_msec;
struct pollfd pfd[1];
2014-12-21 22:31:18 +00:00
2015-01-04 12:05:26 +00:00
CHECK_OBJ_NOTNULL(usc, UDP_SOCKET_MAGIC);
2014-12-21 22:31:18 +00:00
AN(ss);
AN(sl);
AN(ts);
AN(buf);
assert(len > 0);
if (fam == AF_INET)
pfd[0].fd = usc->fd4;
else
WRONG("Wrong family in UdpTimedRx");
2015-01-04 12:05:26 +00:00
pfd[0].events = POLLIN;
pfd[0].revents = 0;
2015-01-04 12:05:26 +00:00
if (tmo == 0.0) {
tmo_msec = -1;
} else {
2015-03-12 21:48:56 +00:00
tmo_msec = round(1e3 * tmo);
2015-01-04 12:05:26 +00:00
if (tmo_msec <= 0)
tmo_msec = 0;
}
i = poll(pfd, 1, tmo_msec);
2015-03-07 22:29:12 +00:00
2015-01-04 12:05:26 +00:00
if (i < 0)
2015-03-12 21:48:56 +00:00
Fail(ocx, 1, "poll failed\n");
2015-01-04 12:05:26 +00:00
if (i == 0)
return (0);
2015-03-07 22:29:12 +00:00
TB_Now(ts);
2014-12-21 22:31:18 +00:00
memset(&msg, 0, sizeof msg);
msg.msg_name = (void*)ss;
msg.msg_namelen = sizeof *ss;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
2015-03-07 22:29:12 +00:00
msg.msg_control = (char*)ctrl;
2014-12-21 22:31:18 +00:00
msg.msg_controllen = sizeof ctrl;
iov.iov_base = buf;
iov.iov_len = (size_t)len;
memset(ctrl, 0, sizeof ctrl);
cmsg = (void*)ctrl;
rl = recvmsg(pfd[0].fd, &msg, 0);
2015-03-07 22:29:12 +00:00
if (rl <= 0) {
2014-12-21 22:31:18 +00:00
return (rl);
2015-03-07 22:29:12 +00:00
}
2014-12-21 22:31:18 +00:00
*sl = msg.msg_namelen;
if (msg.msg_flags != 0) {
Debug(ocx, "msg_flags = 0x%x", msg.msg_flags);
return (-1);
}
2015-03-11 21:10:43 +00:00
if (msg.msg_controllen == 0)
return (rl);
2014-12-21 22:31:18 +00:00
for(;cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
Debug(ocx, "RX-msg: %d %d %u ",
cmsg->cmsg_level, cmsg->cmsg_type, cmsg->cmsg_len);
DebugHex(ocx, CMSG_DATA(cmsg), cmsg->cmsg_len);
Debug(ocx, "\n");
}
return (rl);
}
2015-01-04 12:05:26 +00:00
ssize_t
Udp_Send(struct ocx *ocx, const struct udp_socket *usc,
2015-01-16 21:21:15 +00:00
const void *ss, socklen_t sl, const void *buf, size_t len)
2015-01-04 12:05:26 +00:00
{
const struct sockaddr *sa;
(void)ocx;
CHECK_OBJ_NOTNULL(usc, UDP_SOCKET_MAGIC);
AN(ss);
AN(sl);
AN(buf);
AN(len);
sa = ss;
if (sa->sa_family == AF_INET)
return (sendto(usc->fd4, buf, len, 0, ss, sl));
WRONG("Wrong AF_");
NEEDLESS_RETURN(0);
2015-01-04 12:05:26 +00:00
}
2015-03-11 21:10:43 +00:00