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

📄 slpd_database.c

📁 SLP协议在linux下的实现。此版本为1.2.1版。官方网站为www.openslp.org
💻 C
📖 第 1 页 / 共 3 页
字号:
/***************************************************************************//*                                                                         *//* Project:     OpenSLP - OpenSource implementation of Service Location    *//*              Protocol Version 2                                         *//*                                                                         *//* File:        slpd_database.c                                            *//*                                                                         *//* Abstract:    Implements database abstraction.  Currently a simple       *//*              double linked list (common/slp_database.c) is used for the *//*              underlying storage.                                        *//*                                                                         *//*-------------------------------------------------------------------------*//*                                                                         *//*     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.    *//*                                                                         *//***************************************************************************//*=========================================================================*//* slpd includes                                                           *//*=========================================================================*/#include "slpd_database.h"#include "slpd_regfile.h"#include "slpd_property.h"#include "slpd_log.h"#include "slpd_knownda.h"#ifdef ENABLE_PREDICATES    #include "slpd_predicate.h"#endif/*=========================================================================*//* common code includes                                                    *//*=========================================================================*/#include "slp_compare.h"#include "slp_xmalloc.h"#include "slp_pid.h"/*=========================================================================*/SLPDDatabase G_SlpdDatabase;/* slpd database global                                                    *//*=========================================================================*//*=========================================================================*/void SLPDDatabaseAge(int seconds, int ageall)/* Ages the database entries and clears new and deleted entry lists        *//*                                                                         *//* seconds  (IN) the number of seconds to age each entry by                *//*																		   *//* ageall   (IN) age even entries with SLP_LIFETIME_MAXIMUM                *//*                                                                         *//* Returns  - None                                                         *//*=========================================================================*/{    SLPDatabaseHandle   dh;    SLPDatabaseEntry*   entry;    SLPSrvReg*          srvreg;    dh = SLPDatabaseOpen(&G_SlpdDatabase.database);    if ( dh )    {        while ( 1 )        {            entry = SLPDatabaseEnum(dh);            if ( entry == NULL ) break;            /* srvreg is the SrvReg message from the database */            srvreg = &(entry->msg->body.srvreg);            if ( srvreg->urlentry.lifetime == SLP_LIFETIME_MAXIMUM )            {                if ( srvreg->source == SLP_REG_SOURCE_LOCAL ||                     srvreg->source == SLP_REG_SOURCE_STATIC )                {                    /* entries that were made from local registrations    */                    /* and entries made from static registration file     */                    /* that have a lifetime of SLP_LIFETIME_MAXIMUM must  */                    /* NEVER be aged                                      */                    continue;                }                if ( ageall == 0 )                {                    /* Don't age any services that have a lifetime of     */                    /* SLP_LIFETIME_MAXIMUM unless explicitly told to     */                    continue;                }            }            /* Age entry */            srvreg->urlentry.lifetime -= seconds;            /* Remove entries that have timed out */            if ( srvreg->urlentry.lifetime <= 0 )            {                SLPDLogRegistration("Timeout",entry);		SLPDatabaseRemove(dh,entry);            }        }        SLPDatabaseClose(dh);    }}/*=========================================================================*/int SLPDDatabaseReg(SLPMessage msg, SLPBuffer buf)/* Add a service registration to the database                              *//*                                                                         *//* msg          (IN) SLPMessage of a SrvReg message as returned by         *//*                   SLPMessageParse()                                     *//*                                                                         *//* buf          (IN) Otherwise unreferenced buffer interpreted by the msg  *//*                   structure                                             *//*                                                                         *//* Returns  -   Zero on success.  Nonzero on error                         *//*                                                                         *//* NOTE:        All registrations are treated as fresh                     *//*=========================================================================*/{    SLPDatabaseHandle   dh;    SLPDatabaseEntry*   entry;    SLPSrvReg*          entryreg;    SLPSrvReg*          reg;    int                 result;    /* reg is the SrvReg message being registered */    reg = &(msg->body.srvreg);    /* check service-url syntax */    if ( SLPCheckServiceUrlSyntax(reg->urlentry.url, reg->urlentry.urllen) )    {        return SLP_ERROR_INVALID_REGISTRATION;    }    /* check attr-list syntax */    if ( reg->attrlistlen &&         SLPCheckAttributeListSyntax(reg->attrlist,reg->attrlistlen) )    {        return SLP_ERROR_INVALID_REGISTRATION;    }    dh = SLPDatabaseOpen(&G_SlpdDatabase.database);    if ( dh )    {        /*-----------------------------------------------------*/        /* Check to see if there is already an identical entry */        /*-----------------------------------------------------*/        while ( 1 )        {            entry = SLPDatabaseEnum(dh);            if ( entry == NULL ) break;            /* entry reg is the SrvReg message from the database */            entryreg = &(entry->msg->body.srvreg);            if ( SLPCompareString(entryreg->urlentry.urllen,                                  entryreg->urlentry.url,                                  reg->urlentry.urllen,                                  reg->urlentry.url) == 0 )            {                if ( SLPIntersectStringList(entryreg->scopelistlen,                                            entryreg->scopelist,                                            reg->scopelistlen,                                            reg->scopelist) > 0 )                {                    /* Check to ensure the source addr is the same */                    /* as the original */                    if ( G_SlpdProperty.checkSourceAddr &&                         memcmp(&(entry->msg->peer.sin_addr),                                &(msg->peer.sin_addr),                                sizeof(struct in_addr)) )                    {                        SLPDatabaseClose(dh);                        return SLP_ERROR_AUTHENTICATION_FAILED;                    }#ifdef ENABLE_SLPv2_SECURITY                    if ( entryreg->urlentry.authcount &&                         entryreg->urlentry.authcount != reg->urlentry.authcount )                    {                        SLPDatabaseClose(dh);                        return SLP_ERROR_AUTHENTICATION_FAILED;                    }#endif                      /* Remove the identical entry */                    SLPDatabaseRemove(dh,entry);                    break;                }            }        }        /*------------------------------------*/        /* Add the new srvreg to the database */        /*------------------------------------*/        entry = SLPDatabaseEntryCreate(msg,buf);        if ( entry )        {            /* set the source (allows for quicker aging ) */            if ( msg->body.srvreg.source == SLP_REG_SOURCE_UNKNOWN )            {                if ( ISLOCAL(msg->peer.sin_addr) )                {                    msg->body.srvreg.source = SLP_REG_SOURCE_LOCAL;                 }                else                {                    msg->body.srvreg.source = SLP_REG_SOURCE_REMOTE;                     }            }            /* add to database */            SLPDatabaseAdd(dh, entry);            SLPDLogRegistration("Registration",entry);            /* SUCCESS! */            result = 0;        }        else        {            result = SLP_ERROR_INTERNAL_ERROR;        }        SLPDatabaseClose(dh);    }    else    {        result = SLP_ERROR_INTERNAL_ERROR;    }    return result;}/*=========================================================================*/int SLPDDatabaseDeReg(SLPMessage msg)/* Remove a service registration from the database                         *//*                                                                         *//* msg  - (IN) message interpreting an SrvDereg message                    *//*                                                                         *//* Returns  -   Zero on success.  Non-zero on failure                      *//*=========================================================================*/{    SLPDatabaseHandle   dh;    SLPDatabaseEntry*   entry;    SLPSrvReg*          entryreg;    SLPSrvDeReg*        dereg;    dh = SLPDatabaseOpen(&G_SlpdDatabase.database);    if ( dh )    {        /* dereg is the SrvDereg being deregistered */        dereg = &(msg->body.srvdereg);        /*---------------------------------------------*/        /* Check to see if there is an identical entry */        /*---------------------------------------------*/        while ( 1 )        {            entry = SLPDatabaseEnum(dh);            if ( entry == NULL ) break;            /* entry reg is the SrvReg message from the database */            entryreg = &(entry->msg->body.srvreg);            if ( SLPCompareString(entryreg->urlentry.urllen,                                  entryreg->urlentry.url,                                  dereg->urlentry.urllen,                                  dereg->urlentry.url) == 0 )            {                if ( SLPIntersectStringList(entryreg->scopelistlen,                                            entryreg->scopelist,                                            dereg->scopelistlen,                                            dereg->scopelist) > 0 )                {                    /* Check to ensure the source addr is the same as */                    /* the original */

⌨️ 快捷键说明

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