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

📄 sock_bsd.c

📁 基于sip协议的网络电话源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: sock_bsd.c 974 2007-02-19 01:13:53Z bennylp $ *//*  * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  */#include <pj/sock.h>#include <pj/os.h>#include <pj/assert.h>#include <pj/string.h>#include <pj/compat/socket.h>#include <pj/addr_resolv.h>#include <pj/errno.h>/* * Address families conversion. * The values here are indexed based on pj_addr_family. */const pj_uint16_t PJ_AF_UNIX	= AF_UNIX;const pj_uint16_t PJ_AF_INET	= AF_INET;const pj_uint16_t PJ_AF_INET6	= AF_INET6;#ifdef AF_PACKETconst pj_uint16_t PJ_AF_PACKET	= AF_PACKET;#elseconst pj_uint16_t PJ_AF_PACKET	= 0xFFFF;#endif#ifdef AF_IRDAconst pj_uint16_t PJ_AF_IRDA	= AF_IRDA;#elseconst pj_uint16_t PJ_AF_IRDA	= 0xFFFF;#endif/* * Socket types conversion. * The values here are indexed based on pj_sock_type */const pj_uint16_t PJ_SOCK_STREAM= SOCK_STREAM;const pj_uint16_t PJ_SOCK_DGRAM	= SOCK_DGRAM;const pj_uint16_t PJ_SOCK_RAW	= SOCK_RAW;const pj_uint16_t PJ_SOCK_RDM	= SOCK_RDM;/* * Socket level values. */const pj_uint16_t PJ_SOL_SOCKET	= SOL_SOCKET;#ifdef SOL_IPconst pj_uint16_t PJ_SOL_IP	= SOL_IP;#elseconst pj_uint16_t PJ_SOL_IP	= 0xFFFF;#endif /* SOL_IP */#if defined(SOL_TCP)const pj_uint16_t PJ_SOL_TCP	= SOL_TCP;#elif defined(IPPROTO_TCP)const pj_uint16_t PJ_SOL_TCP	= IPPROTO_TCP;#endif /* SOL_TCP */#ifdef SOL_UDPconst pj_uint16_t PJ_SOL_UDP	= SOL_UDP;#elseconst pj_uint16_t PJ_SOL_UDP	= 0xFFFF;#endif#ifdef SOL_IPV6const pj_uint16_t PJ_SOL_IPV6	= SOL_IPV6;#elseconst pj_uint16_t PJ_SOL_IPV6	= 0xFFFF;#endif/* IP_TOS */#ifdef IP_TOSconst pj_uint16_t PJ_IP_TOS	= IP_TOS;#elseconst pj_uint16_t PJ_IP_TOS	= 1;#endif/* TOS settings (declared in netinet/ip.h) */#ifdef IPTOS_LOWDELAYconst pj_uint16_t PJ_IPTOS_LOWDELAY	= IPTOS_LOWDELAY;#elseconst pj_uint16_t PJ_IPTOS_LOWDELAY	= 0x10;#endif#ifdef IPTOS_THROUGHPUTconst pj_uint16_t PJ_IPTOS_THROUGHPUT	= IPTOS_THROUGHPUT;#elseconst pj_uint16_t PJ_IPTOS_THROUGHPUT	= 0x08;#endif#ifdef IPTOS_RELIABILITYconst pj_uint16_t PJ_IPTOS_RELIABILITY	= IPTOS_RELIABILITY;#elseconst pj_uint16_t PJ_IPTOS_RELIABILITY	= 0x04;#endif#ifdef IPTOS_MINCOSTconst pj_uint16_t PJ_IPTOS_MINCOST	= IPTOS_MINCOST;#elseconst pj_uint16_t PJ_IPTOS_MINCOST	= 0x02;#endif/* optname values. */const pj_uint16_t PJ_SO_TYPE    = SO_TYPE;const pj_uint16_t PJ_SO_RCVBUF  = SO_RCVBUF;const pj_uint16_t PJ_SO_SNDBUF  = SO_SNDBUF;/* recv() and send() flags */const int PJ_MSG_OOB		= MSG_OOB;const int PJ_MSG_PEEK		= MSG_PEEK;const int PJ_MSG_DONTROUTE	= MSG_DONTROUTE;#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0#   define SET_LEN(addr,len) (((pj_sockaddr*)(addr))->sa_zero_len=(len))#   define RESET_LEN(addr)   (((pj_sockaddr*)(addr))->sa_zero_len=0)#else#   define SET_LEN(addr,len) #   define RESET_LEN(addr)#endif/* * Convert 16-bit value from network byte order to host byte order. */PJ_DEF(pj_uint16_t) pj_ntohs(pj_uint16_t netshort){    return ntohs(netshort);}/* * Convert 16-bit value from host byte order to network byte order. */PJ_DEF(pj_uint16_t) pj_htons(pj_uint16_t hostshort){    return htons(hostshort);}/* * Convert 32-bit value from network byte order to host byte order. */PJ_DEF(pj_uint32_t) pj_ntohl(pj_uint32_t netlong){    return ntohl(netlong);}/* * Convert 32-bit value from host byte order to network byte order. */PJ_DEF(pj_uint32_t) pj_htonl(pj_uint32_t hostlong){    return htonl(hostlong);}/* * Convert an Internet host address given in network byte order * to string in standard numbers and dots notation. */PJ_DEF(char*) pj_inet_ntoa(pj_in_addr inaddr){#if !defined(PJ_LINUX) && !defined(PJ_LINUX_KERNEL)    return inet_ntoa(*(struct in_addr*)&inaddr);#else    struct in_addr addr;    addr.s_addr = inaddr.s_addr;    return inet_ntoa(addr);#endif}/* * This function converts the Internet host address cp from the standard * numbers-and-dots notation into binary data and stores it in the structure * that inp points to.  */PJ_DEF(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp){    char tempaddr[16];    /* Initialize output with PJ_INADDR_NONE.     * Some apps relies on this instead of the return value     * (and anyway the return value is quite confusing!)     */    inp->s_addr = PJ_INADDR_NONE;    /* Caution:     *	this function might be called with cp->slen >= 16     *  (i.e. when called with hostname to check if it's an IP addr).     */    PJ_ASSERT_RETURN(cp && cp->slen && inp, 0);    if (cp->slen >= 16) {	return 0;    }    pj_memcpy(tempaddr, cp->ptr, cp->slen);    tempaddr[cp->slen] = '\0';#if defined(PJ_SOCK_HAS_INET_ATON) && PJ_SOCK_HAS_INET_ATON != 0    return inet_aton(tempaddr, (struct in_addr*)inp);#else    inp->s_addr = inet_addr(tempaddr);    return inp->s_addr == PJ_INADDR_NONE ? 0 : 1;#endif}/* * Convert address string with numbers and dots to binary IP address. */ PJ_DEF(pj_in_addr) pj_inet_addr(const pj_str_t *cp){    pj_in_addr addr;    pj_inet_aton(cp, &addr);    return addr;}/* * Convert address string with numbers and dots to binary IP address. */ PJ_DEF(pj_in_addr) pj_inet_addr2(const char *cp){    pj_str_t str = pj_str((char*)cp);    return pj_inet_addr(&str);}/* * Set the IP address of an IP socket address from string address,  * with resolving the host if necessary. The string address may be in a * standard numbers and dots notation or may be a hostname. If hostname * is specified, then the function will resolve the host into the IP * address. */PJ_DEF(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,					         const pj_str_t *str_addr){    PJ_CHECK_STACK();    PJ_ASSERT_RETURN(!str_addr || str_addr->slen < PJ_MAX_HOSTNAME,                      (addr->sin_addr.s_addr=PJ_INADDR_NONE, PJ_EINVAL));    RESET_LEN(addr);    addr->sin_family = AF_INET;    pj_bzero(addr->sin_zero, sizeof(addr->sin_zero));    if (str_addr && str_addr->slen) {	addr->sin_addr = pj_inet_addr(str_addr);	if (addr->sin_addr.s_addr == PJ_INADDR_NONE) {    	    pj_hostent he;	    pj_status_t rc;	    rc = pj_gethostbyname(str_addr, &he);	    if (rc == 0) {		addr->sin_addr.s_addr = *(pj_uint32_t*)he.h_addr;	    } else {		addr->sin_addr.s_addr = PJ_INADDR_NONE;		return rc;	    }	}    } else {	addr->sin_addr.s_addr = 0;    }    return PJ_SUCCESS;}/* * Set the IP address and port of an IP socket address. * The string address may be in a standard numbers and dots notation or  * may be a hostname. If hostname is specified, then the function will  * resolve the host into the IP address. */PJ_DEF(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,				         const pj_str_t *str_addr,					 pj_uint16_t port){    PJ_ASSERT_RETURN(addr, (addr->sin_addr.s_addr=PJ_INADDR_NONE, PJ_EINVAL));    RESET_LEN(addr);    addr->sin_family = PJ_AF_INET;    pj_bzero(addr->sin_zero, sizeof(addr->sin_zero));    pj_sockaddr_in_set_port(addr, port);    return pj_sockaddr_in_set_str_addr(addr, str_addr);}/* * Get hostname. */PJ_DEF(const pj_str_t*) pj_gethostname(void){    static char buf[PJ_MAX_HOSTNAME];    static pj_str_t hostname;    PJ_CHECK_STACK();    if (hostname.ptr == NULL) {	hostname.ptr = buf;	if (gethostname(buf, sizeof(buf)) != 0) {	    hostname.ptr[0] = '\0';	    hostname.slen = 0;	} else {	   hostname.slen = strlen(buf);	}    }    return &hostname;}/* * Get first IP address associated with the hostname. */PJ_DEF(pj_in_addr) pj_gethostaddr(void){    pj_sockaddr_in addr;    const pj_str_t *hostname = pj_gethostname();    pj_sockaddr_in_set_str_addr(&addr, hostname);    return addr.sin_addr;}#if defined(PJ_WIN32)/* * Create new socket/endpoint for communication and returns a descriptor. */PJ_DEF(pj_status_t) pj_sock_socket(int af, 				   int type, 

⌨️ 快捷键说明

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