⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sock.h

📁 linux下的tcpreplay源代码
💻 H
📖 第 1 页 / 共 3 页
字号:
#ifndef _SOCK_H/* include unph *//* Our own header.  Tabs are set for 4 spaces, not 8 *///#ifndef	__unp_h//#define	__unp_h/* If anything changes in the following list of #includes, must change   acsite.m4 also, for configure's tests. */#include	<sys/types.h>	/* basic system data types */#include	<sys/socket.h>	/* basic socket definitions */#include	<sys/time.h>	/* timeval{} for select() */#include	<time.h>		/* timespec{} for pselect() */#include	<netinet/in.h>	/* sockaddr_in{} and other Internet defns */#include	<arpa/inet.h>	/* inet(3) functions */#include	<errno.h>#include	<fcntl.h>		/* for nonblocking */#include	<netdb.h>#include	<signal.h>#include	<stdio.h>#include	<stdlib.h>#include	<string.h>#include	<sys/stat.h>	/* for S_xxx file mode constants */#include	<sys/uio.h>		/* for iovec{} and readv/writev */#include	<unistd.h>#include	<sys/wait.h>#include	<sys/un.h>		/* for Unix domain sockets */#ifdef	HAVE_SYS_SELECT_H# include	<sys/select.h>	/* for convenience */#endif#ifdef	HAVE_POLL_H# include	<poll.h>		/* for convenience */#endif#ifdef	HAVE_STRINGS_H# include	<strings.h>		/* for convenience */#endif/* Three headers are normally needed for socket/file ioctl's: * <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>. */#include	<sys/ioctl.h>#ifdef	HAVE_SYS_FILIO_H# include	<sys/filio.h>#endif#ifdef	HAVE_SYS_SOCKIO_H# include	<sys/sockio.h>#endif#ifdef	HAVE_PTHREAD_H# include	<pthread.h>#endif/* OSF/1 actually disables recv() and send() in <sys/socket.h> */#ifdef	__osf__#undef	recv#undef	send#define	recv(a,b,c,d)	recvfrom(a,b,c,d,0,0)#define	send(a,b,c,d)	sendto(a,b,c,d,0,0)#endif#ifndef	INADDR_NONE/* $$.Ic INADDR_NONE$$ */#define	INADDR_NONE	0xffffffff	/* should have been in <netinet/in.h> */#endif#ifndef	SHUT_RD				/* these three Posix.1g names are quite new */#define	SHUT_RD		0	/* shutdown for reading */#define	SHUT_WR		1	/* shutdown for writing */#define	SHUT_RDWR	2	/* shutdown for reading and writing *//* $$.Ic SHUT_RD$$ *//* $$.Ic SHUT_WR$$ *//* $$.Ic SHUT_RDWR$$ */#endif/* *INDENT-OFF* */#ifndef INET_ADDRSTRLEN/* $$.Ic INET_ADDRSTRLEN$$ */#define	INET_ADDRSTRLEN		16	/* "ddd.ddd.ddd.ddd\0"								    1234567890123456 */#endif/* Define following even if IPv6 not supported, so we can always allocate   an adequately-sized buffer, without #ifdefs in the code. */#ifndef INET6_ADDRSTRLEN/* $$.Ic INET6_ADDRSTRLEN$$ */#define	INET6_ADDRSTRLEN	46	/* max size of IPv6 address string:				   "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" or				   "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd\0"				    1234567890123456789012345678901234567890123456 */#endif/* *INDENT-ON* *//* Define bzero() as a macro if it's not in standard C library. */#ifndef	HAVE_BZERO#define	bzero(ptr,n)		memset(ptr, 0, n)/* $$.If bzero$$ *//* $$.If memset$$ */#endif/* Older resolvers do not have gethostbyname2() */#ifndef	HAVE_GETHOSTBYNAME2#define	gethostbyname2(host,family)		gethostbyname((host))#endif/* We need the newer CMSG_LEN() and CMSG_SPACE() macros, but few   implementations support them today.  These two macros really need    an ALIGN() macro, but each implementation does this differently. */#ifndef	CMSG_LEN/* $$.Ic CMSG_LEN$$ */#define	CMSG_LEN(size)		(sizeof(struct cmsghdr) + (size))#endif#ifndef	CMSG_SPACE/* $$.Ic CMSG_SPACE$$ */#define	CMSG_SPACE(size)	(sizeof(struct cmsghdr) + (size))#endif/* Posix.1g renames "Unix domain" as "local IPC".   But not all systems DefinE AF_LOCAL and AF_LOCAL (yet). */#ifndef	AF_LOCAL#define AF_LOCAL	AF_UNIX#endif#ifndef	PF_LOCAL#define PF_LOCAL	PF_UNIX#endif/* Posix.1g requires that an #include of <poll.h> DefinE INFTIM, but many   systems still DefinE it in <sys/stropts.h>.  We don't want to include   all the streams stuff if it's not needed, so we just DefinE INFTIM here.   This is the standard value, but there's no guarantee it is -1. */#ifndef INFTIM#define INFTIM          (-1)    /* infinite poll timeout *//* $$.Ic INFTIM$$ */#ifdef	HAVE_POLL_H#define	INFTIM_UNPH				/* tell unpxti.h we defined it */#endif#endif/* Following could be derived from SOMAXCONN in <sys/socket.h>, but many   kernels still #define it as 5, while actually supporting many more */#define	LISTENQ		1024	/* 2nd argument to listen() *//* Miscellaneous constants */#define	MAXLINE		4096	/* max text line length */#define	MAXSOCKADDR  128	/* max socket address structure size */#define	BUFFSIZE	8192	/* buffer size for reads and writes *//* Following shortens all the type casts of pointer arguments */#define	SA	struct sockaddr#define	FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)					/* default file access permissions for new files */#define	DIR_MODE	(FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)					/* default permissions for new directories */typedef	void	Sigfunc(int);	/* for signal handlers */#define	min(a,b)	((a) < (b) ? (a) : (b))#define	max(a,b)	((a) > (b) ? (a) : (b))			/* prototypes for our own library functions */void	 daemon_init(const char *, int);Sigfunc *signal_intr(int, Sigfunc *);int		 tcp_connect(const char *, const char *);int		 tcp_listen(const char *, const char *, socklen_t *);int		 udp_client(const char *, const char *, void **, socklen_t *);int		 udp_connect(const char *, const char *);int		 udp_server(const char *, const char *, socklen_t *);#ifndef	HAVE_INET_PTON_PROTOint			 inet_pton(int, const char *, void *);const char	*inet_ntop(int, const void *, char *, size_t);#endif#ifndef	HAVE_INET_ATON_PROTO// int		 inet_aton(const char *, struct in_addr *);#endif#ifndef	HAVE_ISFDTYPE_PROTOint		 isfdtype(int, int);#endif#ifndef	HAVE_SNPRINTF_PROTOint		 snprintf(char *, size_t, const char *, ...);#endif			/* prototypes for our own library wrapper functions */const char		*Inet_ntop(int, const void *, char *, size_t);void			 Inet_pton(int, const char *, void *);char			*If_indextoname(unsigned int, char *);Sigfunc *Signal(int, Sigfunc *);Sigfunc *Signal_intr(int, Sigfunc *);int		 Sock_bind_wild(int, int);int		 Tcp_connect(const char *, const char *);int		 Tcp_listen(const char *, const char *, socklen_t *);int		 Udp_client(const char *, const char *, void **, socklen_t *);int		 Udp_connect(const char *, const char *);int		 Udp_server(const char *, const char *, socklen_t *);			/* prototypes for our Unix wrapper functions: see {Sec errors} */void	*Calloc(size_t, size_t);void	 Close(int);void	 Dup2(int, int);int		 Fcntl(int, int, int);void	 Gettimeofday(struct timeval *, void *);int		 Ioctl(int, int, void *);pid_t	 Fork(void);void	*Malloc(size_t);void	 Mktemp(char *);void	*Mmap(void *, size_t, int, int, int, off_t);int		 Open(const char *, int, mode_t);void	 Pipe(int *fds);ssize_t	 Read(int, void *, size_t);void	 Sigaddset(sigset_t *, int);void	 Sigdelset(sigset_t *, int);void	 Sigemptyset(sigset_t *);void	 Sigfillset(sigset_t *);int		 Sigismember(const sigset_t *, int);void	 Sigpending(sigset_t *);void	 Sigprocmask(int, const sigset_t *, sigset_t *);char	*Strdup(const char *);long	 Sysconf(int);void	 Sysctl(int *, u_int, void *, size_t *, void *, size_t);void	 Unlink(const char *);pid_t	 Wait(int *);pid_t	 Waitpid(pid_t, int *, int);void	 Write(int, void *, size_t);			/* prototypes for our stdio wrapper functions: see {Sec errors} */void	 Fclose(FILE *);FILE	*Fdopen(int, const char *);char	*Fgets(char *, int, FILE *);FILE	*Fopen(const char *, const char *);void	 Fputs(const char *, FILE *);			/* prototypes for our socket wrapper functions: see {Sec errors} */int		 Accept(int, SA *, socklen_t *);void	 Bind(int, const SA *, socklen_t);void	 Connect(int, const SA *, socklen_t);void	 Getpeername(int, SA *, socklen_t *);void	 Getsockname(int, SA *, socklen_t *);void	 Getsockopt(int, int, int, void *, socklen_t *);int		 Isfdtype(int, int);void	 Listen(int, int);#ifdef	HAVE_POLLint		 Poll(struct pollfd *, unsigned long, int);#endifssize_t	 Readline(int, void *, size_t);ssize_t	 Readn(int, void *, size_t);ssize_t	 Recv(int, void *, size_t, int);ssize_t	 Recvfrom(int, void *, size_t, int, SA *, socklen_t *);ssize_t	 Recvmsg(int, struct msghdr *, int);int		 Select(int, fd_set *, fd_set *, fd_set *, struct timeval *);void	 Send(int, const void *, size_t, int);void	 Sendto(int, const void *, size_t, int, const SA *, socklen_t);void	 Sendmsg(int, const struct msghdr *, int);void	 Setsockopt(int, int, int, const void *, socklen_t);void	 Shutdown(int, int);int		 Socket(int, int, int);void	 Socketpair(int, int, int, int *);void	 Writen(int, void *, size_t);unsigned long Getaddr(char *);void	 err_dump(const char *, ...);void	 err_msg(const char *, ...);void	 err_quit(const char *, ...);void	 err_ret(const char *, ...);void	 err_sys(const char *, ...);/* * Socket wrapper functions. * These could all go into separate files, so only the ones needed cause * the corresponding function to be added to the executable.  If sockets * are a library (SVR4) this might make a difference (?), but if sockets * are in the kernel (BSD) it doesn't matter. * * These wrapper functions also use the same prototypes as POSIX.1g, * which might differ from many implementations (i.e., POSIX.1g specifies * the fourth argument to getsockopt() as "void *", not "char *"). * * If your system's headers are not correct [i.e., the Solaris 2.5 * <sys/socket.h> omits the "const" from the second argument to both * bind() and connect()], you'll get warnings of the form: *warning: passing arg 2 of `bind' discards `const' from pointer target type *warning: passing arg 2 of `connect' discards `const' from pointer target type */	intAccept(int fd, struct sockaddr *sa, socklen_t *salenptr){	int		n;again:	if ( (n = accept(fd, sa, salenptr)) < 0) {#ifdef	EPROTO		if (errno == EPROTO || errno == ECONNABORTED)#else		if (errno == ECONNABORTED)#endif			goto again;		else			err_sys("accept error");	}	return(n);}voidBind(int fd, const struct sockaddr *sa, socklen_t salen){	if (bind(fd, sa, salen) < 0)		err_sys("bind error");}voidConnect(int fd, const struct sockaddr *sa, socklen_t salen){	if (connect(fd, sa, salen) < 0)		err_sys("connect error");}voidGetpeername(int fd, struct sockaddr *sa, socklen_t *salenptr){	if (getpeername(fd, sa, salenptr) < 0)		err_sys("getpeername error");}voidGetsockname(int fd, struct sockaddr *sa, socklen_t *salenptr){	if (getsockname(fd, sa, salenptr) < 0)		err_sys("getsockname error");}voidGetsockopt(int fd, int level, int optname, void *optval, socklen_t *optlenptr){	if (getsockopt(fd, level, optname, optval, optlenptr) < 0)		err_sys("getsockopt error");}intIsfdtype(int fd, int fdtype){	int		n;	if ( (n = isfdtype(fd, fdtype)) < 0)		err_sys("isfdtype error");	return(n);}/* include Listen */voidListen(int fd, int backlog){	char	*ptr;		/*4can override 2nd argument with environment variable */	if ( (ptr = getenv("LISTENQ")) != NULL)		backlog = atoi(ptr);	if (listen(fd, backlog) < 0)		err_sys("listen error");}/* end Listen */#ifdef	HAVE_POLLintPoll(struct pollfd *fdarray, unsigned long nfds, int timeout){	int		n;	if ( (n = poll(fdarray, nfds, timeout)) < 0)		err_sys("poll error");	return(n);}#endifssize_tRecv(int fd, void *ptr, size_t nbytes, int flags){	ssize_t		n;	if ( (n = recv(fd, ptr, nbytes, flags)) < 0)		err_sys("recv error");	return(n);}ssize_tRecvfrom(int fd, void *ptr, size_t nbytes, int flags,		 struct sockaddr *sa, socklen_t *salenptr){	ssize_t		n;	if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0)		err_sys("recvfrom error");	return(n);}ssize_tRecvmsg(int fd, struct msghdr *msg, int flags){	ssize_t		n;	if ( (n = recvmsg(fd, msg, flags)) < 0)		err_sys("recvmsg error");	return(n);}intSelect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,       struct timeval *timeout){	int		n;	if ( (n = select(nfds, readfds, writefds, exceptfds, timeout)) < 0)		err_sys("select error");	return(n);		/* can return 0 on timeout */}voidSend(int fd, const void *ptr, size_t nbytes, int flags){	if (send(fd, ptr, nbytes, flags) != nbytes)		err_sys("send error");}voidSendto(int fd, const void *ptr, size_t nbytes, int flags,	   const struct sockaddr *sa, socklen_t salen){	if (sendto(fd, ptr, nbytes, flags, sa, salen) != nbytes)		err_sys("sendto error");}voidSendmsg(int fd, const struct msghdr *msg, int flags){	int			i;	ssize_t		nbytes;	nbytes = 0;	/* must first figure out what return value should be */	for (i = 0; i < msg->msg_iovlen; i++)		nbytes += msg->msg_iov[i].iov_len;	if (sendmsg(fd, msg, flags) != nbytes)		err_sys("sendmsg error");}voidSetsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen){	if (setsockopt(fd, level, optname, optval, optlen) < 0)		err_sys("setsockopt error");}voidShutdown(int fd, int how){	if (shutdown(fd, how) < 0)		err_sys("shutdown error");}/* include Socket */intSocket(int family, int type, int protocol){	int		n;	if ( (n = socket(family, type, protocol)) < 0)		err_sys("socket error");	return(n);}/* end Socket */voidSocketpair(int family, int type, int protocol, int *fd){	int		n;	if ( (n = socketpair(family, type, protocol, fd)) < 0)		err_sys("socketpair error");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -