📄 inet_base.h
字号:
#include <stdio.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <stdlib.h>#ifdef HAVE_GETOPT_H#include <getopt.h>#endif#include <locale.h>#include <fcntl.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/time.h>#include <sys/resource.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include "log.h"#ifndef INET_BASE_H#define INET_BASE_H#ifdef __cplusplusextern "C" {#endif#ifndef _ERROR_LOG#define _ERROR_LOG(format...){\ fprintf(stderr, "[%s:%d] ", __FILE__, __LINE__);\ fprintf(stderr, "\"");\ fprintf(stderr, format);\ fprintf(stderr, "\"");\ fprintf(stderr, "\n");\}#endif#ifndef _DEBUG_LOG#ifdef _DEBUG#define _DEBUG_LOG(format...){\ fprintf(stdout, "[%s:%d] ", __FILE__, __LINE__);\ fprintf(stdout, "\"");\ fprintf(stdout, format);\ fprintf(stdout, "\"");\ fprintf(stdout, "\n");\}#else #define _DEBUG_LOG(format...)#endif#endif#define S_SOCK_BIND 0x01#define S_SOCK_CONN 0x04#define S_SOCK_NONBLOCK 0x08#define S_SOCK_LISTEN 0x10#ifndef TYPEDEF_SOCKPF#define TYPEDEF_SOCKPFtypedef struct _SOCKPF{ /* The domain parameter specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>. The currently understood formats include: Name Purpose Man page PF_UNIX, PF_LOCAL Local communication unix(7) PF_INET IPv4 Internet protocols ip(7) PF_INET6 IPv6 Internet protocols PF_IPX IPX - Novell protocols PF_NETLINK Kernel user interface device netlink(7) PF_X25 ITU-T X.25 / ISO-8208 protocol x25(7) PF_AX25 Amateur radio AX.25 protocol PF_ATMPVC Access to raw ATM PVCs PF_APPLETALK Appletalk ddp(7) PF_PACKET Low level packet interface packet(7) */ int domain; /* The socket has the indicated type, which specifies the communication semantics. Cur- rently defined types are: SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams. An out-of- band data transmission mechanism may be supported. SOCK_DGRAM Supports datagrams (connectionless, unreliable messages of a fixed maximum length). SOCK_SEQPACKET Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each read system call. SOCK_RAW Provides raw network protocol access. SOCK_RDM Provides a reliable datagram layer that does not guarantee ordering. SOCK_PACKET Obsolete and should not be used in new programs; see packet(7). Some socket types may not be implemented by all protocol families; for example, SOCK_SEQPACKET is not implemented for AF_INET. */ int type; /* The protocol specifies a particular protocol to be used with the socket. Normally only a single protocol exists to support a particular socket type within a given protocol family, in which case protocol can be specified as 0. However, it is possible that many protocols may exist, in which case a particular protocol must be specified in this man- ner. The protocol number to use is specific to the “communication domain” in which com- munication is to take place; see protocols(5). See getprotoent(3) on how to map proto- col name strings to protocol numbers. */ int family;}SOCKPF;/** * initialize SOCKPF * @pf: struct SOCKPF * @domain: see above for detail * @type: see above for detail * @family: see above for detail */#define SOCKPF_SET(pf, domain, type, family){\ pf.domain = domain;\ pf.type = type;\ pf.family = family;\}#endif#ifndef TYPEDEF_SOCKNODE #define TYPEDEF_SOCKNODEtypedef struct _SOCKNODE{ char ip[64]; int port; }SOCKNODE;#endif#ifndef INT2IP/** * Convert 32 bits number to ip string * @_num: 32 bits number * @_ip: ip string delimited with dot */#define INT2IP(_num, _ip){\ uint32_t _p1, _p2, _p3, _p4 , _n;\ _p4 = _num & 0xFF;\ _p3 = (_num >> 8) & 0xFF;\ _p2 = (_num >> 16) & 0xFF;\ _p1 = (_num >> 24) & 0xFF;\ _n = sprintf(_ip, "%d.%d.%d.%d", _p1, _p2, _p3, _p4);\ _ip[_n] = '\0';\}#endif#ifndef IP2INT/** * Convert ip string to 32 bits number * @_ip: ip string * @_num: 32 bits number */#define IP2INT(_ip, _num){\ uint32_t _p1, _p2, _p3, _p4;\ int _n;\ _num = 0;\ _n = sscanf(_ip, "%d.%d.%d.%d", &_p1, &_p2, &_p3, &_p4);\ if (_n == 4)\ _num = (_p1 << 24) + (_p2 << 16) + (_p3 << 8) + _p4;\}#endif#ifndef HOST2IP/** * get ip as given _host * @_host: hostname * @_ip: ip string */#define HOST2IP(_host, _ip){\ struct hostent *_hostent = gethostbyname(_host);\ if(_hostent != NULL){\ int _sip = *((int *)_hostent->h_addr_list[0]);\ INT2IP(_sip, _ip);\ }\}#endif#ifndef SN_SET/** * initialize SOCKNODE * @sn: struct SOCKNODE * @ip: host ip as string, max length is 64 * @port: host port with type int */#define SN_SET(_sn, _ip, _port){\ _sn.port = port;\ char *_s = _ip;\ while(*_s != 0)\ *(_sn.ip++) = *_s++;\ *(_sn.ip) = 0\}#endif#ifndef SA_SET/** * initialize sockaddr_in * @sa: struct sockaddr_in * @ip: host ip as string, max length is 64 * @port: host port with type int */#define SA_SET(_sa, _family, _ip, _port){\ memset(&_sa, 0, sizeof(_sa));\ _sa.sin_family = _family;\ if(_ip == NULL){\ _sa.sin_addr.s_addr = INADDR_ANY;\ }else{\ _sa.sin_addr.s_addr = inet_addr(_ip);\ }\ _sa.sin_port = htons(_port);\}#endif#ifndef SA2SA/** * convert struct SOCKNODE to sockaddr_in * @sn: struct SOCKNODE * @sa: struct sockaddr_in */#define SN2SA(_sn, _sa){\ _sa.sin_addr.s_addr = inet_addr(_sn.ip);\ _sa.sin_port = htons(_sa.port);\}#endif#ifndef SA2SN/** * convert struct sockaddr_in to struct sockaddr_in * @sa: struct sockaddr_in * @sn: struct SOCKNODE */#define SA2SN(_sa, _sn){\ SN_SET(_sn, inet_ntoa(_sa.sin_addr), ntohs(_sa.sin_port));\}#endif#ifndef SOCK_NEW/** * Create new socket as given @_domain @_type @_family * @_fd: new socket fd * @_domain: Same with @domain in socket(@domain, @type, @family); * @_type: Same with @type in socket(@domain, @type, @family); * @_family: Same with @family in socket(@domain, @type, @family); * */#define SOCK_NEW(_fd, _domain, _type, _family){\ _fd = socket(_domain, _type, _family);\ if(_fd < 0 ){\ _ERROR_LOG("ERROR:socket initialized failed, %s", strerror(errno));\ }else{\ _DEBUG_LOG("Initialized socket[%d]", _fd);\ }\}#endif#ifndef SOCK_BIND/** * Bind @_fd and @_sa * @_fd: socket fd * @_sa: pointer of struct sockaddr_in */#define SOCK_BIND(_fd, _sa, _ret){\ int _sa_len = sizeof(_sa);\ int _opt = 1;\ setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR,\ (char *)&_opt, (socklen_t) sizeof(_opt) );\ if((_ret = bind(_fd, (struct sockaddr *)&_sa, _sa_len )) != 0 ){\ _ERROR_LOG("ERROR:socket bind failed, %s", strerror(errno));\ }else{\ _DEBUG_LOG("Binded socket[%d]", _fd);\ }\}#endif#ifndef SOCK_CONN/** * Connect remote socket @_sa via @_fd * @_fd: socket fd * @_sa: description of remote socket */#define SOCK_CONN(_fd, _sa, _ret){\ int _sa_len = sizeof(_sa);\ if((_ret = connect(_fd, (struct sockaddr *)&_sa, _sa_len )) != 0 ){\ _ERROR_LOG("ERROR:socket connect failed, %s", strerror(errno));\ }else{\ _DEBUG_LOG("socket[%d] Connected %s:%d",\ _fd, inet_ntoa(_sa.sin_addr), ntohs(_sa.sin_port));\ }\}#endif#ifndef SOCK_NONBLOCK/** * Set @_fd as NONBLOCK * @_fd: socket fd */#define SOCK_NONBLOCK(_fd, _ret){\ if((_ret = fcntl(_fd, F_SETFL, O_NONBLOCK)) != 0 ){\ _ERROR_LOG("ERROR:set fd NONBLOCK failed, %s", strerror(errno));\ }else{\ _DEBUG_LOG("set socket[%d] NONBLOCK ", _fd);\ }\}#endif #ifndef SOCK_LISTEN/** * Listen socket on @_fd * @_fd: socket fd * @_backlog: same as @backlog in listen(@fd, @backlog) */#define SOCK_LISTEN(_fd, _backlog, _ret){\ if((_ret = listen(_fd, _backlog)) != 0){\ _ERROR_LOG("ERROR:socket listenning failed, %s", strerror(errno));\ }else{\ _DEBUG_LOG("Listen socket[%d]", _fd);\ }\}#endif#ifndef SETRLIMIT#define SETRLIMIT(NAME, RLIM, rlim_set)\{\ struct rlimit rlim;\ rlim.rlim_cur = rlim_set;\ rlim.rlim_max = rlim_set;\ if(setrlimit(RLIM, (&rlim)) != 0) {\ _ERROR_LOG("setrlimit RLIM[%s] cur[%ld] max[%ld] failed, %s",\ NAME, rlim.rlim_cur, rlim.rlim_max, strerror(errno));\ _exit(-1);\ } else {\ _DEBUG_LOG("setrlimit RLIM[%s] cur[%ld] max[%ld]",\ NAME, rlim.rlim_cur, rlim.rlim_max);\ }\}#define GETRLIMIT(NAME, RLIM)\{\ struct rlimit rlim;\ if(getrlimit(RLIM, &rlim) != 0 ) {\ _ERROR_LOG("regetrlimit RLIM[%s] failed, %s",\ NAME, strerror(errno));\ } else {\ _DEBUG_LOG("getrlimit RLIM[%s] cur[%ld] max[%ld]", \ NAME, rlim.rlim_cur, rlim.rlim_max);\ }\}#endif/** * Initialize the inet socket * @fd: socket filedescr * @sa: struct sockaddr_in pointer * @domain: The same to socket(int domain, int type, int family)'s domain * @type: The same to socket(int domain, int type, int family)'s type * @family: The same to socket(int domain, int type, int family)'s family, default set it 0 please * @ip: Host ip string with dot as delimiter, if use all the network interface set is NULL * @backlog: The same to backlog of function listen(int, sockaddr*, int backlog) * @flag: Whether set socket as listen socket, if 1 for listening, 0 not, default 0 */int inet_init(int fd, struct sockaddr_in *sa, int backlog, int flag);/** * read data from socket fd * */int inet_read(int fd, char *buf, int len, suseconds_t timeout);/** * write data TO socket fd * */int inet_write(int fd, char *buf, int len, suseconds_t timeout );#ifdef __cplusplus }#endif#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -