#include <sys/errno.h>
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet6/natpt_defs.h>
#include <netinet6/natpt_log.h>
#include <netinet6/natpt_var.h>
static struct sockaddr _natpt_dst = {2, PF_INET};
static struct sockaddr _natpt_src = {2, PF_INET};
struct mbuf *natpt_lbuf __P((int type, int priorities, size_t size));
void
natpt_logMsg(int priorities, void *item, size_t size)
{
natpt_log(LOG_MSG, priorities, item, size);
}
void
natpt_logMBuf(int priorities, struct mbuf *m, char *msg)
{
if (msg)
natpt_log(LOG_MSG, priorities, (void *)msg, strlen(msg)+1);
natpt_log(LOG_MBUF, priorities, (void *)m->m_data, min(m->m_len, LBFSZ));
}
void
natpt_logIp4(int priorities, struct ip *ip4)
{
natpt_log(LOG_IP4, priorities, (void *)ip4, sizeof(struct ip)+8);
}
void
natpt_logIp6(int priorities, struct ip6_hdr *ip6)
{
natpt_log(LOG_IP6, priorities, (void *)ip6, sizeof(struct ip6_hdr)+8);
}
int
natpt_log(int type, int priorities, void *item, size_t size)
{
struct sockproto proto;
struct mbuf *m;
struct lbuf *p;
if ((m = natpt_lbuf(type, priorities, size)) == NULL)
return (ENOBUFS);
p = (struct lbuf *)m->m_data;
m_copyback(m, sizeof(struct l_hdr), p->l_hdr.lh_size, (caddr_t)item);
proto.sp_family = AF_INET;
proto.sp_protocol = IPPROTO_AHIP;
natpt_input(m, &proto, &_natpt_src, &_natpt_dst);
return (0);
}
int
natpt_logIN6addr(int priorities, char *msg, struct in6_addr *sin6addr)
{
int size, msgsz;
struct mbuf *m;
struct lbuf *p;
msgsz = strlen(msg)+1;
size = sizeof(struct l_hdr) + IN6ADDRSZ + msgsz;
m = natpt_lbuf(LOG_IN6ADDR, priorities, size);
if (m == NULL)
return (ENOBUFS);
{
struct sockproto proto;
p = (struct lbuf *)m->m_pktdat;
bcopy(sin6addr, p->l_addr.in6addr, sizeof(struct in6_addr));
strncpy(p->l_msg, msg, min(msgsz, MSGSZ-1));
p->l_msg[MSGSZ-1] = '\0';
proto.sp_family = AF_INET;
proto.sp_protocol = IPPROTO_AHIP;
natpt_input(m, &proto, &_natpt_src, &_natpt_dst);
}
return (0);
}
struct mbuf *
natpt_lbuf(int type, int priorities, size_t size)
{
struct mbuf *m;
struct lbuf *p;
MGETHDR(m, M_NOWAIT, MT_DATA);
if (m == NULL)
return (NULL);
m->m_pkthdr.len = m->m_len = MHLEN;
m->m_pkthdr.rcvif = NULL;
p = (struct lbuf *)m->m_data;
p->l_hdr.lh_type = type;
p->l_hdr.lh_pri = priorities;
p->l_hdr.lh_size = size;
microtime((struct timeval *)&p->l_hdr.lh_sec);
return (m);
}