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

📄 rvsocketaddr.c

📁 h.248协议源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
Filename   : rvsocketaddr.c
Description: Socket address interface definition.
******************************************************************************
                Copyright (c) 1999-2000 RADVision Inc.
******************************************************************************
NOTICE:
This document contains information that is proprietary to RADVision Inc.
No part of this publication may be reproduced in any form whatsoever 
without written prior approval by RADVision Inc.

RADVision Inc. reserves the right to revise this publication and make 
changes without obligation to notify any person of such revisions or 
changes.
******************************************************************************
$Revision:$
$Date:$
$Author: S. Cipolli$
*****************************************************************************/

#include <stddef.h>
#include <stdio.h>
#include <limits.h>
#include "rvplatform.h"
#include "rvlog.h"
#include "rvsocketaddr.h"
#include "rvsocket_.h"

#if defined(RV_SOCKETS_NUCLEUS)
/* redefine structure names */
#define sockaddr_in addr_struct
/* #define sin_len length */
#define sin_family family
#define sin_port port
#define sin_addr id.is_ip_addrs
#endif

RvSocketAddr* rvSocketAddrConstruct(RvSocketAddr *thisPtr) 
{
    memset(thisPtr->address, 0, RV_SOCKADDR_MAXSIZE);
    thisPtr->length   = 0;
    thisPtr->protocol = RV_SOCKETADDR_PROTOCOL_UNDEFINED;
    return thisPtr;
}

RvSocketAddr* rvSocketAddrConstructCopy(RvSocketAddr *thisPtr, 
                                        const RvSocketAddr *srcPtr, 
                                        RvAlloc *allocatorPtr)
{
    rvLogEnter(&rvLog, "rvSocketAddrConstructCopy");
    
    thisPtr->protocol = srcPtr->protocol;
    thisPtr->length   = srcPtr->length;
    memcpy(&thisPtr->address, &srcPtr->address, srcPtr->length);

    rvLogLeave(&rvLog, "rvSocketAddrConstructCopy");

    return thisPtr;
}

RvSocketAddr* rvSocketAddrConstructInetAnyAddr(RvSocketAddr* thisPtr, RvInetPort port)
{
    /* This is currently static, it should be converted
       to be dynamic by using the type RV_SOCKETADDR_PROTOCOL_IPANYADDR
       and chaecking on Bind to create the appropriate address for
       the socket. 
    
    thisPtr->protocol = RV_SOCKETADDR_PROTOCOL_IPANYADDR;
    memset(thisPtr->address, 0, RV_SOCKADDR_MAXSIZE);
    thisPtr->length   = sizeof(RvInetPort);
    *((RvInetPort*)thisPtr->address) = port;
    
    */
#if defined(RV_INET_IPV6)
    RvIpv6Addr address;
    rvIpv6AddrConstruct(&address,&RV_INET_ADDRANY);
    rvSocketAddrConstructIpv6(thisPtr,&address, port);
    rvIpv6AddrDestruct(&address);
#else
    RvIpv4Addr address;
    rvIpv4AddrConstruct(&address,htonl(RV_INET_ADDRANY));
    rvSocketAddrConstructIpv4(thisPtr,&address, port);
    rvIpv4AddrDestruct(&address);
#endif
    return thisPtr;
}


/* Construct a socket address from IPv4 address */
RvSocketAddr* rvSocketAddrConstructIpv4(RvSocketAddr* thisPtr, RvIpv4Addr* ipv4, 
  RvInetPort port) 
{
    struct sockaddr_in* protocolAddr = (struct sockaddr_in*)&thisPtr->address;
    
    rvLogEnter(&rvLog, "rvSocketAddrConstructIpv4");

    memset(thisPtr->address, 0, RV_SOCKADDR_MAXSIZE);
#if defined(RV_SOCKETADDR_BSD44)
    protocolAddr->sin_len = 16;
#endif
    protocolAddr->sin_family = AF_INET;
    protocolAddr->sin_port = htons(port);
#if defined(RV_SOCKETS_NUCLEUS)
    memcpy(&protocolAddr->id.is_ip_addrs, &ipv4->sck_ip_addr, 4);
#else
    protocolAddr->sin_addr = *ipv4;
#endif  
    thisPtr->length = RV_SOCKADDR_MAXSIZE;
    thisPtr->protocol = RV_SOCKETADDR_PROTOCOL_IPV4;

    rvLogLeave(&rvLog, "rvSocketAddrConstructIpv4");

    return thisPtr;
}

#if defined(RV_INET_IPV6)
/* Construct a socket address from IPv6 address */
RvSocketAddr* rvSocketAddrConstructIpv6(RvSocketAddr* thisPtr, RvIpv6Addr* ipv6, 
  RvInetPort port) {
    struct sockaddr_in6* protocolAddr = (struct sockaddr_in6*)&thisPtr->address;
    
    rvLogEnter(&rvLog, "rvSocketAddrConstructIpv6");

    memset(thisPtr->address, 0, RV_SOCKADDR_MAXSIZE);
    protocolAddr->sin6_family = AF_INET6;
    protocolAddr->sin6_port = htons(port);
    protocolAddr->sin6_addr = *ipv6;
    thisPtr->length = sizeof(struct sockaddr_in6);
    thisPtr->protocol = RV_SOCKETADDR_PROTOCOL_IPV6;

    rvLogLeave(&rvLog, "rvSocketAddrConstructIpv6");

    return thisPtr;
}
#endif

/* Construct a socket address of type INET (IPv4, or IPv6) */
RvSocketAddr* rvSocketAddrConstructInet(RvSocketAddr* thisPtr, RvHost* h,  
                                        size_t n, RvInetPort port) 
{
    int addressFamily = rvHostGetAddrType(h);

    rvLogEnter(&rvLog, "rvSocketAddrConstructInet");

    if(addressFamily == AF_INET)
        rvSocketAddrConstructIpv4(thisPtr, rvHostGetIpv4Addr(h, n), port);
#if defined(RV_INET_IPV6)
    else if (addressFamily == AF_INET6)
        rvSocketAddrConstructIpv6(thisPtr, rvHostGetIpv6Addr(h, n), port);
#endif

    rvLogLeave(&rvLog, "rvSocketAddrConstructInet");

    return thisPtr;
}

RvSocketAddr* rvSocketAddrConstructInetByName(RvSocketAddr* thisPtr, const char *dnsOrIp, RvInetPort port)
{
    RvHost host;
    int addressFamily;

    rvLogEnter(&rvLog, "rvSocketAddrConstructInetName");

    if(rvHostConstruct(&host, dnsOrIp) == NULL)
    {
        rvLogLeave(&rvLog, "rvSocketAddrConstructInetByName");
        return NULL;
    }

    addressFamily = rvHostGetAddrType(&host);
 
    if (addressFamily == AF_INET)
        rvSocketAddrConstructIpv4(thisPtr, rvHostGetIpv4Addr(&host, 0), port);
#if defined(RV_INET_IPV6)
    else if (addressFamily == AF_INET6)
        rvSocketAddrConstructIpv6(thisPtr, rvHostGetIpv6Addr(&host, 0), port);
#endif

    rvLogLeave(&rvLog, "rvSocketAddrConstructInetByName");

    return thisPtr;
}

/* str must be large enough to hold converted address */
char* rvSocketAddrToString(const RvSocketAddr* thisPtr, char* str) 
{
    char addrbuf[16];

    rvLogEnter(&rvLog, "rvSocketAddrToString");

    /* If IPv4 ... */
    if (thisPtr->protocol == RV_SOCKETADDR_PROTOCOL_IPV4) 
    {
        struct sockaddr_in* protocolAddr = (struct sockaddr_in*)&thisPtr->address;
#if defined(RV_SOCKETS_NUCLEUS)
        sprintf(str, "%s", rvInetNtoa(*((UINT32 *)&protocolAddr->sin_addr), addrbuf));
#else
        sprintf(str, "%s", rvInetNtoa(protocolAddr->sin_addr, addrbuf));
#endif
    } 
#if defined(RV_INET_IPV6)
    else if (thisPtr->protocol == RV_SOCKETADDR_PROTOCOL_IPV6) 
    {
        struct sockaddr_in6* protocolAddr = (struct sockaddr_in6*)&thisPtr->address;
        char addrStr[INET6_ADDRSTRLEN];
        sprintf(str, "%s", inet_ntop(AF_INET6, 
          &protocolAddr->sin6_addr, addrStr, INET6_ADDRSTRLEN));
    }
#endif
    else if (thisPtr->protocol == RV_SOCKETADDR_PROTOCOL_IPANYADDR)
    {
        sprintf(str, "INADDR_ANY");
    }
    else if (thisPtr->protocol == RV_SOCKETADDR_PROTOCOL_UNKNOWN)
    {
        sprintf(str, "Unknown Address Type");
    }
    else
    {
        sprintf(str, "Undefined Address Type");
    }
   
    rvLogLeave(&rvLog, "rvSocketAddrToString");

    return str;
}

int rvSocketAddrToFullString(const RvSocketAddr* thisPtr, char* str)
{
    int offset = 0;

    if (thisPtr->protocol == RV_SOCKETADDR_PROTOCOL_IPV4) 
    {
        /* If IPv4 */
        char addrbuf[16];
        struct sockaddr_in* protocolAddr = (struct sockaddr_in*)&thisPtr->address;
#if defined(RV_SOCKETS_NUCLEUS)
        offset += sprintf(str, "%s:%i", rvInetNtoa(*((UINT32 *)&protocolAddr->sin_addr), addrbuf),
                          rvSocketAddrGetInetPort(thisPtr));
#else
        offset += sprintf(str, "%s:%i", rvInetNtoa(protocolAddr->sin_addr, addrbuf),
                          rvSocketAddrGetInetPort(thisPtr));
#endif
    } 
#if defined(RV_INET_IPV6)
    else if (thisPtr->protocol == RV_SOCKETADDR_PROTOCOL_IPV6) 
    {
        /* If IPv6 */
        struct sockaddr_in6* protocolAddr = (struct sockaddr_in6*)&thisPtr->address;
        char addrStr[INET6_ADDRSTRLEN];
        offset += sprintf(str, "%s:%i", inet_ntop(AF_INET6, 
          &protocolAddr->sin6_addr, addrStr, INET6_ADDRSTRLEN),
          rvSocketAddrGetInetPort(thisPtr));
    }
#endif
    else if (thisPtr->protocol == RV_SOCKETADDR_PROTOCOL_IPANYADDR)
    {
        offset += sprintf(str, "INADDR_ANY:%i", rvSocketAddrGetInetPort(thisPtr));
    }
    else if (thisPtr->protocol == RV_SOCKETADDR_PROTOCOL_UNKNOWN)
    {
        offset += sprintf(str, "Unknown Address Type");
    }
    else
    {

⌨️ 快捷键说明

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