2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2018-04-17 13:18:40 +00:00
|
|
|
* Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2005-04-26 16:02:40 +00:00
|
|
|
*
|
2016-05-17 18:18:30 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2005-04-26 16:02:40 +00:00
|
|
|
*/
|
|
|
|
|
2016-01-22 19:54:01 +00:00
|
|
|
#include "ssl_locl.h"
|
2005-04-27 16:27:14 +00:00
|
|
|
#include <openssl/bn.h>
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2016-01-22 19:54:01 +00:00
|
|
|
struct pqueue_st {
|
2015-01-22 03:40:55 +00:00
|
|
|
pitem *items;
|
|
|
|
int count;
|
2016-01-22 19:54:01 +00:00
|
|
|
};
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
pitem *pitem_new(unsigned char *prio64be, void *data)
|
|
|
|
{
|
2015-05-02 03:10:31 +00:00
|
|
|
pitem *item = OPENSSL_malloc(sizeof(*item));
|
2018-04-03 15:31:16 +00:00
|
|
|
|
|
|
|
if (item == NULL) {
|
|
|
|
SSLerr(SSL_F_PITEM_NEW, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
2018-04-03 15:31:16 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
memcpy(item->priority, prio64be, sizeof(item->priority));
|
|
|
|
item->data = data;
|
|
|
|
item->next = NULL;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pitem_free(pitem *item)
|
|
|
|
{
|
|
|
|
OPENSSL_free(item);
|
|
|
|
}
|
|
|
|
|
2018-05-09 15:09:50 +00:00
|
|
|
pqueue *pqueue_new(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-01-22 19:54:01 +00:00
|
|
|
pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
|
2018-04-03 15:31:16 +00:00
|
|
|
|
|
|
|
if (pq == NULL)
|
|
|
|
SSLerr(SSL_F_PQUEUE_NEW, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
return pq;
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:54:01 +00:00
|
|
|
void pqueue_free(pqueue *pq)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
OPENSSL_free(pq);
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:54:01 +00:00
|
|
|
pitem *pqueue_insert(pqueue *pq, pitem *item)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
pitem *curr, *next;
|
|
|
|
|
|
|
|
if (pq->items == NULL) {
|
|
|
|
pq->items = item;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (curr = NULL, next = pq->items;
|
|
|
|
next != NULL; curr = next, next = next->next) {
|
|
|
|
/*
|
|
|
|
* we can compare 64-bit value in big-endian encoding with memcmp:-)
|
|
|
|
*/
|
|
|
|
int cmp = memcmp(next->priority, item->priority, 8);
|
|
|
|
if (cmp > 0) { /* next > item */
|
|
|
|
item->next = next;
|
|
|
|
|
|
|
|
if (curr == NULL)
|
|
|
|
pq->items = item;
|
|
|
|
else
|
|
|
|
curr->next = item;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (cmp == 0) /* duplicates not allowed */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->next = NULL;
|
|
|
|
curr->next = item;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:54:01 +00:00
|
|
|
pitem *pqueue_peek(pqueue *pq)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return pq->items;
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:54:01 +00:00
|
|
|
pitem *pqueue_pop(pqueue *pq)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
pitem *item = pq->items;
|
|
|
|
|
|
|
|
if (pq->items != NULL)
|
|
|
|
pq->items = pq->items->next;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:54:01 +00:00
|
|
|
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
pitem *next;
|
|
|
|
pitem *found = NULL;
|
|
|
|
|
|
|
|
if (pq->items == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (next = pq->items; next->next != NULL; next = next->next) {
|
|
|
|
if (memcmp(next->priority, prio64be, 8) == 0) {
|
|
|
|
found = next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the one last node */
|
|
|
|
if (memcmp(next->priority, prio64be, 8) == 0)
|
|
|
|
found = next;
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:54:01 +00:00
|
|
|
pitem *pqueue_iterator(pqueue *pq)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return pqueue_peek(pq);
|
|
|
|
}
|
|
|
|
|
2017-07-29 23:20:47 +00:00
|
|
|
pitem *pqueue_next(piterator *item)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
pitem *ret;
|
|
|
|
|
|
|
|
if (item == NULL || *item == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* *item != NULL */
|
|
|
|
ret = *item;
|
|
|
|
*item = (*item)->next;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-06 18:17:54 +00:00
|
|
|
size_t pqueue_size(pqueue *pq)
|
2009-05-16 16:18:19 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
pitem *item = pq->items;
|
2016-11-04 10:26:57 +00:00
|
|
|
size_t count = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
while (item != NULL) {
|
|
|
|
count++;
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
return count;
|
2009-05-16 16:18:19 +00:00
|
|
|
}
|