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

📄 slp_spi.c

📁 SLP协议在linux下的实现。此版本为1.2.1版。官方网站为www.openslp.org
💻 C
📖 第 1 页 / 共 2 页
字号:
            goto SUCCESS;        }        if(result->keyfilename) xfree(result->keyfilename);        if(result->spistr) xfree(result->spistr);    }    if (result)    {         xfree(result);        result = 0;    }    SUCCESS:    if (line) xfree(line);    return result;}/*=========================================================================*/SLPSpiHandle SLPSpiOpen(const char* spifile, int cacheprivate)/* Initializes SLP SPI data storage.                                       *//*                                                                         *//* Parameters: spifile      (IN) path of slp.spi file                      *//*             cacheprivate (IN) should private keys be cached in handle   *//*                                                                         *//* Returns:  valid pointer.  NULL on failure                               *//*=========================================================================*/{    FILE*           fp;    SLPSpiHandle    result = 0;    SLPSpiEntry*    spientry;          fp = fopen(spifile,"r");    if(fp)    {        result = xmalloc(sizeof(struct _SLPSpiHandle));        if(result == 0) return 0;        memset(result, 0, sizeof(struct _SLPSpiHandle));                result->spifile = xstrdup(spifile);        result->cacheprivate = cacheprivate;        while(1)        {            spientry = SLPSpiReadSpiFile(fp, SLPSPI_KEY_TYPE_ANY);            if(spientry == 0) break;            if(spientry->keytype == SLPSPI_KEY_TYPE_PRIVATE &&               cacheprivate == 0)            {                /* destroy the key cause we're not suppose to cache it */                SLPCryptoDSAKeyDestroy(spientry->key);            }                        SLPListLinkHead(&(result->cache),(SLPListItem*)spientry);        }         fclose(fp);     }    return result;}/*=========================================================================*/void SLPSpiClose(SLPSpiHandle hspi)/* Release SLP SPI data storage associated with the specified SLPSpiHandle *//*                                                                         *//* Parameters: hspi (IN) SLPSpiHandle to deinitialize                      *//*=========================================================================*/{    if(hspi)    {        if(hspi->spifile) xfree(hspi->spifile);        while(hspi->cache.count)        {            SLPSpiEntryFree((SLPSpiEntry*)SLPListUnlink(&(hspi->cache),hspi->cache.head));        }                xfree(hspi);    }}/*=========================================================================*/char* SLPSpiGetDefaultSPI(SLPSpiHandle hspi,                           int keytype,                          int* spistrlen,                          char** spistr)/* Gets a reference to the default SPI string for the specified keytype    *//*                                                                         *//* Parameters: hspi      (IN) handle obtained from call to SLPSpiOpen()    *//*             keytype   (IN) type of key                                  *//*             spistrlen (OUT) length or the returned spistr               *//*             spistr    (OUT) pointer to spistr.  MUST be freed by        *//*                             caller!!                                    *//*                                                                         *//* Returns: Pointer to the default SPI string.  Pointer may *not* be NULL  *//*          terminated                                                     *//*=========================================================================*/{    SLPSpiEntry* entry;        *spistr = 0;    *spistrlen = 0;    if(hspi)    {                    entry = SLPSpiEntryFind(&(hspi->cache),keytype,0,0);        if(entry)        {            *spistr = xmalloc(entry->spistrlen);            if(*spistr)            {                memcpy(*spistr, entry->spistr, entry->spistrlen);                *spistrlen = entry->spistrlen;            }        }    }    return *spistr;}    /*=========================================================================*/SLPCryptoDSAKey* SLPSpiGetDSAKey(SLPSpiHandle hspi,                                 int keytype,                                 int spistrlen,                                 const char* spistr,                                 SLPCryptoDSAKey **key)/* Fetches a copy of the private key file used to sign SLP messages.       *//*                                                                         *//* Parameters: hspi      (IN)  handle obtained from call to SLPSpiOpen()   *//*             keytype   (IN)  the type of key desired                     *//*             spistrlen (IN)  the length of the spistr                    *//*             spistr    (IN)  spistr associated with the key              *//*             key       (OUT) the private key.  Caller should use         *//*                             SLPCryptoDSAKeyDestroy() to free key memory *//*                                                                         *//* Returns: A valid pointer. NULL on failure. Caller should use            *//*          SLPCryptoDSAKeyDestroy() to free key memory                    */ /*=========================================================================*/{    SLPSpiEntry*    tmp = 0;            /* For safety NULL out the key from the beginning */    *key = 0;    if(hspi)    {        tmp = SLPSpiEntryFind(&(hspi->cache),                              keytype,                              spistrlen,                              spistr);        if(tmp)        {            if(tmp->key == 0)            {                if(keytype == SLPSPI_KEY_TYPE_PRIVATE && hspi->cacheprivate == 0)                {                    *key = SLPSpiReadKeyFile(tmp->keyfilename,SLPSPI_KEY_TYPE_PRIVATE);                    return *key;                }                                tmp->key = SLPSpiReadKeyFile(tmp->keyfilename,keytype);		if (tmp->key == 0)		    return 0;            }            *key = SLPCryptoDSAKeyDup(tmp->key);        }    }    return *key;}/*=========================================================================*/int SLPSpiCanVerify(SLPSpiHandle hspi,                    int spistrlen,                    const char* spistr)/* Determine if we understand the specified SPI.  No SPI is always         *//* returns true                                                            *//*                                                                         *//* Parameters: hspi      (IN)  handle obtained from call to SLPSpiOpen()   *//*             spistrlen (IN)  the length of the spistr                    *//*             spistr    (IN)  the SPI string                              *//*                                                                         *//* Returns     Non-zero if we verify specified the SPI                     *//*=========================================================================*/{    if (hspi == 0)    {        return 0;    }    if(spistrlen == 0 || spistr == NULL)    {        return 1;    }    return (SLPSpiEntryFind(&(hspi->cache),                             SLPSPI_KEY_TYPE_PUBLIC,                            spistrlen,                             spistr) != 0);}/*=========================================================================*/int SLPSpiCanSign(SLPSpiHandle hspi,                  int spistrlen,                  const char* spistr)/* Determine if we understand the specified SPI.  No SPI is always         *//* return true                                                             *//*                                                                         *//* Parameters: hspi      (IN)  handle obtained from call to SLPSpiOpen()   *//*             spistrlen (IN)  the length of the spistr                    *//*             spistr    (IN)  the SPI string                              *//*                                                                         *//* Returns     Non-zero if we sign using the specified SPI                 *//*=========================================================================*/{    return (SLPSpiEntryFind(&(hspi->cache),                             SLPSPI_KEY_TYPE_PRIVATE,                            spistrlen,                             spistr) != 0);}

⌨️ 快捷键说明

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