📄 slpd_knownda.c
字号:
SLPDatabaseHandle dh = NULL; dh = SLPDatabaseOpen(&G_SlpdKnownDAs); if ( dh == NULL ) { result = SLP_ERROR_INTERNAL_ERROR; goto CLEANUP; } /* daadvert is the DAAdvert message being added */ daadvert = &(msg->body.daadvert); /* -------------------------------------------------------- * Make sure that the peer address in the DAAdvert matches * the host in the DA service URL. *--------------------------------------------------------- */ if (SLPParseSrvUrl(daadvert->urllen, daadvert->url, &parsedurl)) { /* could not parse the DA service url */ result = SLP_ERROR_PARSE_ERROR; goto CLEANUP; } if (SLPNetResolveHostToAddr(parsedurl->host,&daaddr)) { /* Unable to resolve the host in the DA advert to an address */ xfree(parsedurl); result = SLP_ERROR_PARSE_ERROR; goto CLEANUP; } /* free the parsed url created in call to SLPParseSrvUrl() */ xfree(parsedurl); /* set the peer address in the DAAdvert message so that it matches * the address the DA service URL resolves to */ msg->peer.sin_addr = daaddr; /*-----------------------------------------------------*/ /* Check to see if there is already an identical entry */ /*-----------------------------------------------------*/ while ( 1 ) { entry = SLPDatabaseEnum(dh); if ( entry == NULL ) break; /* entrydaadvert is the DAAdvert message from the database */ entrydaadvert = &(entry->msg->body.daadvert); /* Assume DAs are identical if their URLs match */ if ( SLPCompareString(entrydaadvert->urllen, entrydaadvert->url, daadvert->urllen, daadvert->url) == 0 ) {#ifdef ENABLE_SLPv2_SECURITY if ( G_SlpdProperty.checkSourceAddr && memcmp(&(entry->msg->peer.sin_addr), &(msg->peer.sin_addr), sizeof(struct in_addr)) ) { SLPDatabaseClose(dh); result = SLP_ERROR_AUTHENTICATION_FAILED; goto CLEANUP; } /* make sure an unauthenticated DAAdvert can't replace */ /* an authenticated one */ if ( entrydaadvert->authcount && entrydaadvert->authcount != daadvert->authcount ) { SLPDatabaseClose(dh); result = SLP_ERROR_AUTHENTICATION_FAILED; goto CLEANUP; } #endif if ( daadvert->bootstamp != 0 && daadvert->bootstamp <= entrydaadvert->bootstamp ) { /* Advertising DA must have went down then came back up */ SLPDKnownDARegisterAll(msg,0); } /* Remove the entry that is the same as the advertised entry */ /* so that we can put the new advertised entry back in */ SLPDatabaseRemove(dh,entry); break; } } /* Make sure the DA is not dying */ if (daadvert->bootstamp != 0) { if ( entry == 0 ) { /* create a new database entry using the DAAdvert message */ entry = SLPDatabaseEntryCreate(msg,buf); if (entry) { SLPDatabaseAdd(dh, entry); /* register all the services we know about with this new DA */ SLPDKnownDARegisterAll(msg,0); /* log the addition of a new DA */ SLPDLogDAAdvertisement("Addition",entry); } else { /* Could not create a new entry */ result = SLP_ERROR_INTERNAL_ERROR; goto CLEANUP; } } else { /* The advertising DA is not new to us, but the old entry */ /* has been deleted from our database so that the new entry */ /* with its up to date time stamp can be put back in. */ /* create a new database entry using the DAAdvert message */ entry = SLPDatabaseEntryCreate(msg,buf); if (entry) { SLPDatabaseAdd(dh, entry); } else { /* Could not create a new entry */ result = SLP_ERROR_INTERNAL_ERROR; goto CLEANUP; } } SLPDatabaseClose(dh); return result; } else { /* DA is dying */ if (entry) { /* Dying DA was found in our KnownDA database. Log that it * was removed. */ SLPDLogDAAdvertisement("Removed",entry); } } CLEANUP: /* If we are here, we need to cleanup the message descriptor and the */ /* message buffer because they were not added to the database and not */ /* cleaning them up would result in a memory leak */ /* We also need to make sure the Database handle is closed. */ SLPMessageFree(msg); SLPBufferFree(buf); if (dh) SLPDatabaseClose(dh); return result;}/*=========================================================================*/void SLPDKnownDARemove(struct in_addr* addr)/* Removes known DAs that sent DAAdverts from the specified in_addr *//*=========================================================================*/{ SLPDatabaseHandle dh; SLPDatabaseEntry* entry; dh = SLPDatabaseOpen(&G_SlpdKnownDAs); if ( dh ) { /*-----------------------------------------------------*/ /* Check to see if there is already an identical entry */ /*-----------------------------------------------------*/ while ( 1 ) { entry = SLPDatabaseEnum(dh); if ( entry == NULL ) break; /* Assume DAs are identical if their peer match */ if ( memcmp(addr,&(entry->msg->peer.sin_addr),sizeof(*addr)) == 0 ) { SLPDatabaseRemove(dh,entry); SLPDLogDAAdvertisement("Removal",entry); break; } } SLPDatabaseClose(dh); }}/*=========================================================================*/void* SLPDKnownDAEnumStart()/* Start an enumeration of all Known DAs *//* *//* Returns: An enumeration handle that is passed to subsequent calls to *//* SLPDKnownDAEnum(). Returns NULL on failure. Returned *//* enumeration handle (if not NULL) must be passed to *//* SLPDKnownDAEnumEnd() when you are done with it. *//*=========================================================================*/{ return SLPDatabaseOpen(&G_SlpdKnownDAs); }/*=========================================================================*/SLPMessage SLPDKnownDAEnum(void* eh, SLPMessage* msg, SLPBuffer* buf)/* Enumerate through all Known DAs *//* *//* eh (IN) pointer to opaque data that is used to maintain *//* enumerate entries. Pass in a pointer to NULL to start *//* enumeration. *//* *//* msg (OUT) pointer to the DAAdvert message descriptor *//* *//* buf (OUT) pointer to the DAAdvert message buffer *//* *//* returns: Pointer to enumerated entry or NULL if end of enumeration *//*=========================================================================*/{ SLPDatabaseEntry* entry; entry = SLPDatabaseEnum((SLPDatabaseHandle) eh); if ( entry ) { *msg = entry->msg; *buf = entry->buf; } else { *msg = 0; *buf = 0; } return *msg;}/*=========================================================================*/void SLPDKnownDAEnumEnd(void* eh)/* End an enumeration started by SLPDKnownDAEnumStart() *//* *//* Parameters: eh (IN) The enumeration handle returned by *//* SLPDKnownDAEnumStart() *//*=========================================================================*/{ if ( eh ) { SLPDatabaseClose((SLPDatabaseHandle)eh); }}/*=========================================================================*/int SLPDKnownDAGenerateMyDAAdvert(int errorcode, int deadda, int xid, SLPBuffer* sendbuf) /* Pack a buffer with a DAAdvert using information from a SLPDAentry *//* *//* errorcode (IN) the errorcode for the DAAdvert *//* *//* xid (IN) the xid to for the DAAdvert *//* *//* daentry (IN) pointer to the daentry that contains the rest of the info *//* to make the DAAdvert *//* *//* sendbuf (OUT) pointer to the SLPBuffer that will be packed with a *//* DAAdvert *//* *//* returns: zero on success, non-zero on error *//*=========================================================================*/{ int size; SLPBuffer result = *sendbuf;#ifdef ENABLE_SLPv2_SECURITY int daadvertauthlen = 0; unsigned char* daadvertauth = 0; int spistrlen = 0; char* spistr = 0; G_SlpdProperty.DATimestamp += 1; if ( G_SlpdProperty.securityEnabled ) { SLPSpiGetDefaultSPI(G_SlpdSpiHandle, SLPSPI_KEY_TYPE_PRIVATE, &spistrlen, &spistr); SLPAuthSignDAAdvert(G_SlpdSpiHandle, spistrlen, spistr, G_SlpdProperty.DATimestamp, G_SlpdProperty.myUrlLen, G_SlpdProperty.myUrl, 0, 0, G_SlpdProperty.useScopesLen, G_SlpdProperty.useScopes, spistrlen, spistr, &daadvertauthlen, &daadvertauth); }#else G_SlpdProperty.DATimestamp += 1;#endif /*-------------------------------------------------------------*/ /* ensure the buffer is big enough to handle the whole srvrply */ /*-------------------------------------------------------------*/ size = G_SlpdProperty.localeLen + 29; /* 14 bytes for header */ /* 2 errorcode */ /* 4 bytes for timestamp */ /* 2 bytes for url len */ /* 2 bytes for scope list len */ /* 2 bytes for attr list len */ /* 2 bytes for spi str len */ /* 1 byte for authblock count */ size += G_SlpdProperty.myUrlLen; size += G_SlpdProperty.useScopesLen;#ifdef ENABLE_SLPv2_SECURITY
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -