/* Derived from lib/pname.c by fastpname.sed */
/*-
 * Copyright (c) 1993, 1996, Trusted Information Systems, Incorporated
 * All rights reserved.
 *
 * Redistribution and use are governed by the terms detailed in the
 * license document ("LICENSE") included with the toolkit.
 */

/*
 *	Author: Marcus J. Ranum, Trusted Information Systems, Inc.
 */


#include	<sys/types.h>
#include	<sys/socket.h>
#include	<netinet/in.h>
#include	<netdb.h>
#include	<syslog.h>
#include	<ctype.h>
#include	<string.h>
#include	<stdlib.h>
#include	<stdio.h>
#include	<errno.h>
#include	<arpa/inet.h>

#include	"firewall.h"
#include	"libemfw.h"

int fastpeername(fd,sname,z)
int	fd;
char	*sname;
int	z;
{
	struct	sockaddr_in	a;
	socklen_t		y;

	y = sizeof(a);
	if(getpeername(fd,(struct sockaddr *)&a,&y) < 0) {
		syslog(LLEV,"getpeername failed: %s", strerror(errno));
		return(1);
	}
	strncpy(sname,inet_ntoa(a.sin_addr),z);
	sname[z - 1] = '\0';

#ifdef IP_OPTIONS
	{
		unsigned char	optbuf[1024 / 3];
		unsigned char	*cp;
		char		lbuf[1024];
		char		*lp;
		socklen_t	optsize;

		optsize = sizeof(optbuf);
		if(getsockopt(fd,IPPROTO_IP,IP_OPTIONS,(char *)optbuf,&optsize) == 0 && optsize != 0) {
			lp = lbuf;
			for(cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
				snprintf(lp, sizeof(lbuf) - (lbuf-lp), " %2.2x",*cp);
			syslog(LLEV,"securityalert: Connection received using IP options (ignored):%.100s",lbuf);
			if(setsockopt(fd,IPPROTO_IP,IP_OPTIONS,(char *)0,optsize) != 0) {
				syslog(LLEV,"setsockopt IP_OPTIONS NULL: %s", strerror(errno));
				exit(1);
			}
		}
	}
#endif
	return(0);
}
