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

📄 libslp_network.c

📁 SLP协议在linux下的实现。此版本为1.2.1版。官方网站为www.openslp.org
💻 C
📖 第 1 页 / 共 4 页
字号:
/***************************************************************************//*                                                                         *//* Project:     OpenSLP - OpenSource implementation of Service Location    *//*              Protocol                                                   *//*                                                                         *//* File:        libslp_network.c                                           *//*                                                                         *//* Abstract:    Implementation for functions that are related to INTERNAL  *//*              library network (and ipc) communication.                   *//*                                                                         *//*-------------------------------------------------------------------------*//*                                                                         *//*     Please submit patches to http://www.openslp.org                     *//*                                                                         *//*-------------------------------------------------------------------------*//*                                                                         *//* Copyright (C) 2000 Caldera Systems, Inc                                 *//* All rights reserved.                                                    *//*                                                                         *//* Redistribution and use in source and binary forms, with or without      *//* modification, are permitted provided that the following conditions are  *//* met:                                                                    */ /*                                                                         *//*      Redistributions of source code must retain the above copyright     *//*      notice, this list of conditions and the following disclaimer.      *//*                                                                         *//*      Redistributions in binary form must reproduce the above copyright  *//*      notice, this list of conditions and the following disclaimer in    *//*      the documentation and/or other materials provided with the         *//*      distribution.                                                      *//*                                                                         *//*      Neither the name of Caldera Systems nor the names of its           *//*      contributors may be used to endorse or promote products derived    *//*      from this software without specific prior written permission.      *//*                                                                         *//* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS     *//* `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      *//* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR   *//* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CALDERA      *//* SYSTEMS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *//* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT        *//* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  *//* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON       *//* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *//* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   *//* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    *//*                                                                         *//***************************************************************************/#include "slp.h"#include "libslp.h"/*=========================================================================*/int NetworkConnectToSlpd(struct sockaddr_in* peeraddr)/* Connects to slpd and provides a peeraddr to send to                     *//*                                                                         *//* peeraddr         (OUT) pointer to receive the connected DA's address    *//*                                                                         *//* Returns          Connected socket or -1 if no DA connection can be made *//*=========================================================================*/{#ifdef _WIN32 /* on WIN32 setsockopt takes a const char * argument */    char lowat;#else    int lowat;#endif    int result;    result = socket(AF_INET,SOCK_STREAM,0);    if(result >= 0)    {        peeraddr->sin_family      = AF_INET;        peeraddr->sin_port        = htons(SLP_RESERVED_PORT);        peeraddr->sin_addr.s_addr = htonl(LOOPBACK_ADDRESS);        /* TODO: the following connect() could block for a long time.  */        if(connect(result,                   (struct sockaddr*)peeraddr,                   sizeof(struct sockaddr_in)) == 0)        {            /* set the receive and send buffer low water mark to 18 bytes           (the length of the smallest slpv2 message) */            lowat = 18;            setsockopt(result,SOL_SOCKET,SO_RCVLOWAT,&lowat,sizeof(lowat));            setsockopt(result,SOL_SOCKET,SO_SNDLOWAT,&lowat,sizeof(lowat));        }        else        {            /* Could not connect to the slpd through the loopback */            close(result);            result = -1;        }    }    return result;}/*=========================================================================*/ void NetworkDisconnectDA(PSLPHandleInfo handle)/* Called after DA fails to respond                                        *//*                                                                         *//* handle   (IN) a handle previously passed to NetworkConnectToDA()        *//*=========================================================================*/ {    if(handle->dasock)    {#ifdef _WIN32	    closesocket(handle->dasock);#else	    close(handle->dasock);#endif        handle->dasock = -1;    }    /* Mark this DA as bad */    KnownDABadDA(&(handle->daaddr.sin_addr));}/*=========================================================================*/ void NetworkDisconnectSA(PSLPHandleInfo handle)/* Called after SA fails to respond                                        *//*                                                                         *//* handle   (IN) a handle previously passed to NetworkConnectToSA()        *//*=========================================================================*/ {    if(handle->sasock)    {#ifdef _WIN32	    closesocket(handle->sasock);#else	    close(handle->sasock);#endif        handle->sasock = -1;    }}/*=========================================================================*/ int NetworkConnectToDA(PSLPHandleInfo handle,                       const char* scopelist,                       int scopelistlen,                       struct sockaddr_in* peeraddr)/* Connects to slpd and provides a peeraddr to send to                     *//*                                                                         *//* handle           (IN) SLPHandle info  (caches connection info           *//*                                                                         *//* scopelist        (IN) Scope that must be supported by DA. Pass in NULL  *//*                       for any scope                                     *//*                                                                         *//* scopelistlen     (IN) Length of the scope list in bytes.  Ignored if    *//*                       scopelist is NULL                                 *//*                                                                         *//* peeraddr         (OUT) pointer to receive the connected DA's address    *//*                                                                         *//* Returns          Connected socket or -1 if no DA connection can be made *//*=========================================================================*/{    /*-----------------------------------------------------------------*/    /* attempt to use a cached socket if scope is supported  otherwise */    /* discover a DA that supports the scope                           */    /*-----------------------------------------------------------------*/    if(handle->dasock >= 0 &&       handle->dascope &&       SLPCompareString(handle->dascopelen,                        handle->dascope,                        scopelistlen,                        scopelist) == 0)    {        memcpy(peeraddr,&(handle->daaddr),sizeof(struct sockaddr_in));    }    else    {        /* close handle cause it can't support the scope */        if(handle->dasock >= 0)        {#ifdef _WIN32	        closesocket(handle->dasock);#else	        close(handle->dasock);#endif        }        /* Attempt to connect to DA that does support the scope */        handle->dasock = KnownDAConnect(handle,                                        scopelistlen,                                        scopelist,                                        &(handle->daaddr));        if(handle->dasock >= 0)        {            if(handle->dascope) xfree(handle->dascope);            handle->dascope = memdup(scopelist,scopelistlen);            handle->dascopelen = scopelistlen;             memcpy(peeraddr,&(handle->daaddr),sizeof(struct sockaddr_in));        }    }    return handle->dasock;}/*=========================================================================*/ int NetworkConnectToSA(PSLPHandleInfo handle,                       const char* scopelist,                       int scopelistlen,                       struct sockaddr_in* peeraddr)/* Connects to slpd and provides a peeraddr to send to                     *//*                                                                         *//* handle           (IN) SLPHandle info  (caches connection info)          *//*                                                                         *//* scopelist        (IN) Scope that must be supported by SA. Pass in NULL  *//*                       for any scope                                     *//*                                                                         *//* scopelistlen     (IN) Length of the scope list in bytes.  Ignored if    *//*                       scopelist is NULL                                 *//*                                                                         *//* peeraddr         (OUT) pointer to receive the connected SA's address    *//*                                                                         *//* Returns          Connected socket or -1 if no SA connection can be made */ {    /*-----------------------------------------------------------------*/    /* attempt to use a cached socket if scope is supported  otherwise */    /* look to connect to local slpd or a DA that supports the scope   */    /*-----------------------------------------------------------------*/    if(handle->sasock >= 0 &&       handle->sascope &&        SLPCompareString(handle->sascopelen,                        handle->sascope,                        scopelistlen,                        scopelist) == 0)    {        memcpy(peeraddr,&(handle->saaddr),sizeof(struct sockaddr_in));    }    else    {        /* close handle cause it can't support the scope */        if(handle->sasock >= 0)        {#ifdef _WIN32	        closesocket(handle->sasock);#else	        close(handle->sasock);#endif        }        /*-----------------------------------------*/        /* Attempt to connect to slpd via loopback */        /*-----------------------------------------*/        handle->sasock = NetworkConnectToSlpd(&(handle->saaddr));        /*----------------------------------------------------------*/        /* if we connected to something, cache scope and addr info  */        /*----------------------------------------------------------*/        if(handle->sasock >= 0)        {            if(handle->sascope) xfree(handle->sascope);            handle->sascope = memdup(scopelist,scopelistlen);            handle->sascopelen = scopelistlen;             memcpy(peeraddr,&(handle->saaddr),sizeof(struct sockaddr_in));        }    }    return handle->sasock;}/*=========================================================================*/ SLPError NetworkRqstRply(int sock,                         struct sockaddr_in* destaddr,                         const char* langtag,                         int extoffset,                         char* buf,                         char buftype,                         int bufsize,                         NetworkRplyCallback callback,                         void * cookie)/* Transmits and receives SLP messages via multicast convergence algorithm *//*                                                                         *//* Returns  -    SLP_OK on success                                         *//*=========================================================================*/ {    struct timeval      timeout;    struct sockaddr_in  peeraddr;    SLPBuffer           sendbuf         = 0;    SLPBuffer           recvbuf         = 0;    SLPError            result          = 0;    int                 looprecv        = 0;    int                 langtaglen      = 0;    int                 prlistlen       = 0;    char*               prlist          = 0;    int                 xid             = 0;    int                 mtu             = 0;    int                 size            = 0;    int                 xmitcount       = 0;    int                 rplycount       = 0;    int                 maxwait         = 0;    int                 totaltimeout    = 0;#ifdef _WIN32 /* on WIN32 setsockopt takes a const char * argument */    char                socktype        = 0;#else    int                 socktype        = 0;#endif    int                 timeouts[MAX_RETRANSMITS];    unsigned short      flags;    /*----------------------------------------------------*/    /* Save off a few things we don't want to recalculate */    /*----------------------------------------------------*/    langtaglen = strlen(langtag);    xid = SLPXidGenerate();    mtu = SLPPropertyAsInteger(SLPGetProperty("net.slp.MTU"));    sendbuf = SLPBufferAlloc(mtu);    if(sendbuf == 0)    {        result = SLP_MEMORY_ALLOC_FAILED;        goto CLEANUP;    }    /* Figure unicast/multicast,TCP/UDP, wait and time out stuff */    if(ISMCAST(destaddr->sin_addr))    {        /* Multicast or broadcast */        maxwait = SLPPropertyAsInteger(SLPGetProperty("net.slp.multicastMaximumWait"));        SLPPropertyAsIntegerVector(SLPGetProperty("net.slp.multicastTimeouts"),                                    timeouts,                                    MAX_RETRANSMITS );        xmitcount = 0;        looprecv = 1;

⌨️ 快捷键说明

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