util.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Name: util.c
  2. *
  3. * This file contains safe wrappers for common functions and implementations of
  4. * data structures
  5. *
  6. * Slitheen - a decoy routing system for censorship resistance
  7. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, version 3.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Additional permission under GNU GPL version 3 section 7
  22. *
  23. * If you modify this Program, or any covered work, by linking or combining
  24. * it with the OpenSSL library (or a modified version of that library),
  25. * containing parts covered by the terms of the OpenSSL Licence and the
  26. * SSLeay license, the licensors of this Program grant you additional
  27. * permission to convey the resulting work. Corresponding Source for a
  28. * non-source form of such a combination shall include the source code
  29. * for the parts of the OpenSSL library used as well as that of the covered
  30. * work.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "util.h"
  35. //Standard queue data structure
  36. typedef struct element_st {
  37. void *data;
  38. struct element_st *next;
  39. } element;
  40. typedef struct queue_st {
  41. element *first;
  42. element *last;
  43. } queue;
  44. //malloc macro that exits on error
  45. void *smalloc(size_t size){
  46. void *ptr = malloc(size);
  47. if (ptr == NULL){
  48. fprintf(stderr, "Memory failure. Exiting...\n");
  49. exit(1);
  50. }
  51. return ptr;
  52. }
  53. //calloc macro that exits on error
  54. void *scalloc(size_t nmemb, size_t size){
  55. void *ptr = calloc(nmemb, size);
  56. if(ptr == NULL){
  57. fprintf(stderr, "Memory failure. Exiting...\n");
  58. exit(1);
  59. }
  60. return ptr;
  61. }
  62. /**
  63. * Initializes a generic queue structure
  64. */
  65. queue *init_queue(){
  66. queue *new_queue = smalloc(sizeof(queue));
  67. new_queue->first = NULL;
  68. new_queue->last = NULL;
  69. return new_queue;
  70. }
  71. /**
  72. * Function to append a struct to the end of a list
  73. */
  74. void enqueue(queue *list, void *data){
  75. //Do not allow appending NULL data
  76. if(data == NULL){
  77. return;
  78. }
  79. element *new_elem = smalloc(sizeof(element));
  80. new_elem->data = data;
  81. new_elem->next = NULL;
  82. if(list->first == NULL){
  83. list->first = new_elem;
  84. list->last = new_elem;
  85. } else {
  86. list->last->next = new_elem;
  87. list->last = new_elem;
  88. }
  89. }
  90. /**
  91. * Removes and returns the first element from the front of the list. Returns NULL
  92. * if list is empty
  93. */
  94. void *dequeue(queue *list){
  95. if(list->first == NULL){
  96. return NULL;
  97. }
  98. void *data = list->first->data;
  99. element *target =list->first;
  100. list->first = target->next;
  101. free(target);
  102. return data;
  103. }
  104. /**
  105. * Returns the nth element of the queue (as provided)
  106. *
  107. * An input of -1 peeks at last element
  108. *
  109. * Returns data on success, NULL on failure
  110. */
  111. void *peek(queue *list, int32_t n){
  112. int32_t i;
  113. element *target = list->first;
  114. if(n == -1){
  115. target = list->last;
  116. }
  117. for(i=0; (i< n) && (target == NULL); i++){
  118. target = target->next;
  119. }
  120. if(target == NULL){
  121. return NULL;
  122. } else {
  123. return target->data;
  124. }
  125. }
  126. /**
  127. * Removes (frees the data in) all elements from the list and then frees the list itself
  128. */
  129. void remove_queue(queue *list){
  130. void *data = dequeue(list);
  131. while(data != NULL){
  132. free(data);
  133. data = dequeue(list);
  134. }
  135. free(list);
  136. }