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

📄 dbfunct.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 5 页
字号:
      db_setMem(rec->ref, (BYTE*) "cred", cred); /* Update */
   else {
      /* Host not found - create new list item */
      host = (BYTE*) OSConnectorAlloc(endPtr-url+1);
      #ifndef HAS_SETJMP
      if (host == NULL)
         return FALSE;
      #endif
      for (s=host, t=url; t != endPtr; s++, t++)
         *s = *t;
      *s = '\0';
      
      if (hostAuthSize == maxHostListSize) {
         /* List full; delete the least recently used record.
          * This is done by reusing the record to delete and
          * placing it first in the queue. */
         db_moveToFront(hostListRec, (ElementPtr) rec);
         hostAuthSize--;
         ref = rec->ref;
      } else {
         ref = db_createRecord(hostListRec->ref, NULL, DB_flag_rec, &error);   
         if (error) {
            OSConnectorFree(host);
            OSConnectorFree(cred);
            return FALSE;
         }
      }
      /* Order significant if creating new record; reverse list order */
      error  = db_setMem(ref, (BYTE*) "cred",  cred);
      error |= db_setStr(ref, (BYTE*) "realm", B_STRDUP(realm));
      error |= db_setStr(ref, (BYTE*) "host",  host);
      if (error)
         db_deleteItem_ptr(hostListRec, (ElementPtr) db_findRecord(ref));
      else
         hostAuthSize++;
   }

   return TRUE;
}



/*
 * Search the database and return the credentials connected to
 * the host of 'url' and 'realm'.
 *
 * This function is called in two cases: before a http request
 * and when a request results in a challange for authenification.
 * In the first case, checkHostAuth is called with 'realm' set
 * to NULL and the matching is done based on only the URL. Even
 * if the item exists in the database, a mismatch may occur.
 * This can happen if several items with identical host are in
 * the database. A mismatch will result in a challenge containing
 * the correct realm and the correct credentials can be found.
 *
 * '*credentials' is a pointer to the corresponding credentials.
 * This is a pointer to the database item and not to a copy and
 * and may not be freed. 
 *
 * The host of 'url' and 'realm' are text strings and are matched
 * agaist the database case insensitivily.
 *
 * Return TRUE if found a match.
 */
BOOL checkHostAuth(BYTE* url, BYTE* realm, BYTE* *credentials)
{
   RecordPtr rec;
   void*     bogousPtr;

   if (!url)
      return FALSE;

   if (findHost(&url, realm, (BYTE**)&bogousPtr, &rec)) {
      *credentials = rec->data.f->next->next->data.s;
      return TRUE;
   } else {
      *credentials = NULL;
      return FALSE;
   }
}



/*
 * Remove all host authentication data.
 */
void deleteHostAuth(void)
{
   db_clearRecord(hostListRec->ref);
   hostAuthSize = 0;
}



/* ======== proxy ======== */

/*
 * Find proxy in database. Functionality common to storeProxyAuth
 * and checkProxyAuth is located in this function.
 *
 * Matches 'address' and port number.
 *
 * Return TRUE if match found.
 */
BOOL findProxy(BYTE* address, UINT8 addressLength, UINT16 portNumber, RecordPtr *currentRec)
{
   RecordPtr rec;
   RecordPtr tempRec;
   BYTE*     s;
   BYTE*     t;
   UINT8     sLen;
   UINT8     len;

   /* Search for matching address and port */
   rec = NULL;
   for (tempRec=proxyListRec->data.r; tempRec != proxyListRec;
        tempRec=(RecordPtr) rec->next) {
      rec = tempRec;

      s=rec->data.f->data.s;
      sLen = (UINT8)*(UINT16*) s;
      for (s += 2, t=address, len=0; len < addressLength && len < sLen; s++, t++, len++) {
         if (*s != *t)
            goto Next_loop;
      }
      /* Address found, check realm */
      if (portNumber == rec->data.f->next->data.i)
         goto Match_found;   

Next_loop:;
   }

   /* --- No match found ---*/
   *currentRec  = rec;
   return FALSE;
   

   /* --- Matching address and port found --- */
Match_found:

   /* Move current record first in queue. When list is full, 
    * the least recently used item is deleted. */
   if (proxyListRec->data.r != rec)
      db_moveToFront(proxyListRec, (ElementPtr) rec);
  
   *currentRec  = rec;
   return TRUE;
}


/*
 * Store 'address' and 'portNumber' together with 'credentials'.
 * If already cfg_wae_wspif_AuthenticationItems are present, the
 * least recently used element is replaced by the new one.
 * 
 * If 'adress' and 'portNumber' matches an item, then the
 * credentials are uppdated to 'credentials'. 'address' can
 * contain any data and is only an exact match counts when
 * comparing addresses.
 *
 * 'address' can contain any data and is only an exact match counts
 * when comparing addresses.
 *
 * Return TRUE if successfull.
 */
BOOL storeProxyAuth(BYTE* address, UINT8 addressLength, UINT16 portNumber,
                    BYTE* credentials, UINT8 credLength)
{
   DB_ref    ref;
   RecordPtr rec;
   BYTE*     addr;
   BYTE*     cred;
   UINT8     error;

   if (!address || !credentials)
      return FALSE;

   cred = (BYTE*) OSConnectorAlloc(credLength+2);
   #ifndef HAS_SETJMP
   if (cred == NULL)
      return FALSE;
   #endif
   *(UINT16*) cred = credLength + 2;
   memcpy(cred+2, credentials, credLength);

   if (findProxy(address, addressLength, portNumber, &rec))
      db_setMem(rec->ref, (BYTE*) "cred", cred);
   else {
      /* Address not found - create new list item */
      addr = (BYTE*) OSConnectorAlloc(addressLength + 2);
      #ifndef HAS_SETJMP
      if (addr == NULL)
         return FALSE;
      #endif
      *(UINT16*) addr = addressLength + 2;
      memcpy(addr + 2, address, addressLength);
      
      if (proxyAuthSize == maxProxyListSize) {
         /* List full; delete the least recently used record.
          * This is done by reusing the record to delete and
          * placing it first in the queue. */
         db_moveToFront(proxyListRec, (ElementPtr) rec);
         proxyAuthSize--;
         ref = rec->ref;
      } else {
         ref = db_createRecord(proxyListRec->ref, NULL, DB_flag_rec, &error);  
         if (error) {
            OSConnectorFree(cred);
            OSConnectorFree(addr);
            return FALSE;
         }
      }
      /* Order significant if creating new record; reverse list order */
      error  = db_setMem(ref, (BYTE*) "cred",  cred); 
      error |= db_setInt(ref, (BYTE*) "port", portNumber);
      error |= db_setMem(ref, (BYTE*) "addr", addr);
      if (error)
         db_deleteItem_ptr(proxyListRec, (ElementPtr) db_findRecord(ref));
      else
         proxyAuthSize++;
   }

   return TRUE;
}



/*
 * Search the database and return the credentials connected to
 * the host of 'address' and 'portNumber'.
 *
 * '*credentials' is a pointer to the corresponding credentials.
 * This is a pointer to the database item and not to a copy and
 * and may not be freed. 
 *
 * 'address' can contain any data and is only an exact match counts
 * when comparing addresses.
 *
 * Return TRUE if found a match.
 */
BOOL checkProxyAuth(BYTE* address, UINT8 addressLength, UINT16 portNumber, BYTE* *credentials)
{
   RecordPtr rec;

   if (!address || addressLength == 0)
      return FALSE;

   if (findProxy(address, addressLength, portNumber, &rec)) {
      *credentials = rec->data.f->next->next->data.s;
      return TRUE;
   } else {
      *credentials = NULL;
      return FALSE;
   }
}



/*
 * Remove all proxy authentication data.
 */
void deleteProxyAuth(void)
{
   db_clearRecord(proxyListRec->ref);
   proxyAuthSize = 0;
}



/* ======== admin ======== */


/*
 * Count number of elements in host or proxy list. Delete items
 * that are not compete, i.e. some data is missing.
 *
 * Return size of list.
 */
UINT8 checkAuthList(RecordPtr parent)
{
   RecordPtr  rec;
   ElementPtr tempElem;
   UINT8      size;

   size = 0;
   rec = parent->data.r;
   while (rec != parent) {
      tempElem = (ElementPtr) rec;
      rec = (RecordPtr) rec->next;
      if (tempElem->data.e == tempElem ||
          tempElem->data.e->next == tempElem ||
          tempElem->data.e->next->next == tempElem)
         db_deleteItem_ptr(parent, tempElem);
      else
         size++;
   }
   return size;
}


/*
 * Turn on or off persistent status for host or proxy 
 * authentication. 'hostNotProxy' is TRUE for host and FALSE
 * for proxy. 'status' is
 * 0 - no persistent save
 * 1 - backup
 * 2 - write-through
 *
 * Return error code.
 */
UINT8 setPersistentAuthStatus (BOOL hostNotProxy, int status)
{
	DB_ref ref;
	UINT16 flags;
	UINT8  error;

	ref = db_getRef (DB_root, hostNotProxy ? (BYTE*) "authH" : (BYTE*) "authP", &error);
	if (error)
		return error;

	switch (status) {
	case 0:
		flags = DB_noPersistentSave;
		break;
	case 1:
		flags = DB_backup;
		break;
	default:
		flags = DB_writeThrough;
		break;
	}

  ref = db_moveRecord (ref, DB_root, DB_root, flags, &error);
	if (error)
		return error;

  if (hostNotProxy)
    hostListRec  = db_findRecord (db_getRef (ref, (BYTE*) "host", &error));
  else
    proxyListRec = db_findRecord (db_getRef (ref, (BYTE*) "proxy", &error));
	return error;
}


/*
 * Initiate authentication lists. If first time, create host and
 * proxy write through list, else verify the correctness of the
 * list loaded from persistent memory.
 *
 * Must be called after db_dbInit.
 */
void authenticateInit(void)
{
	DB_ref authRef;
	DB_ref ref;
	UINT8  error;

	/* Create proxy database structure */
	authRef = db_createRecord(DB_root, (BYTE*) "authP", DB_rec, &error);
	
	ref = db_createRecord(authRef, (BYTE*) "proxy", DB_set, &error);
	proxyAuthSize = 0;
	proxyListRec = db_findRecord(ref);
	/* proxyAuthSize = checkAuthList(proxyListRec); Not needed if no persistent save */


	/* Create or load host database structure */
	authRef = db_getRef(DB_root, (BYTE*) "authH", &error);
	if (error) {
		/* First time, no copy found on persistent memory. */
		authRef = db_createRecord(DB_root, (BYTE*) "authH",
			DB_rec | DB_writeThrough, &error);
		
		hostAuthSize  = 0;
		
		ref = db_createRecord(authRef, (BYTE*) "host",  DB_set, &error);
		hostListRec = db_findRecord(ref);
		
	} else {
		hostListRec  = db_findRecord(db_getRef(authRef, (BYTE*) "host",  &error));
		
		hostAuthSize  = checkAuthList(hostListRec);
	}
}



/*
 * Terminate authentication.
 *
 * Must be called before db_dbTerminate.
 */
void authenticateTerminate(void)
{
}








/*************************************************************/
/**********************     Cookies    ***********************/
/*************************************************************/

/*

STRUCTURE

The rules for database structures are:
 - compound nodes are records (RecordStruct)
 - leaf nodes with names are fields (FieldStruct)
 - leaf nodes without names are elements (ElementStruct)
In addition, a record may be used as a set in which case its members
are namesless. This implies that in a set with leaf members, the 
members are elements. In a set with compund members, the members are
records but the names are not used in them.

All lists in the database are circular.

The database structure used to save cookies is arranged like this:

root
 |
 coo
  |
  <> ---------------------------------- <> ---------------------------------
   |                                     |
   name - val - ver - dom - path - exp   name - val - ver - dom - path - exp


Key/Name      elem-type  value-type    comment

⌨️ 快捷键说明

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