protocols.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <strings.h>
  5. #include <ctype.h>
  6. #include <errno.h>
  7. #include "multiaddr/protocols.h"
  8. #include "multiaddr/varhexutils.h"
  9. /*
  10. int protocol_REMOVE_id(struct ProtocolListItem* protocol_P, int remid)//Function to remove & shift back all data, sort of like c++ vectors.
  11. {
  12. if(remid < CNT_PROTOCOLNUM && remid >= 0&&CNT_PROTOCOLNUM!=0) //Checking to see if remid actually exists.
  13. {
  14. for(int i=remid; i<CNT_PROTOCOLNUM-1; ++i) //While i < num of registered protocols //Needs to be tested that -1 is for valgrind debugging
  15. {
  16. //strcpy((protocol_P+i)->hexcode, (protocol_P+i+1)->hexcode); //shift memory to the user we want to remove.
  17. (protocol_P+i)->deccode = (protocol_P+i+1)->deccode; //Same as above
  18. (protocol_P+i)->size = (protocol_P+i+1)->size; //Same as above
  19. strcpy((protocol_P+i)->name, (protocol_P+i+1)->name); //Same as above
  20. }//Overwriting user done. Time to get rid of that extra memory.
  21. protocol_P = (struct Protocol*) realloc(protocol_P, (CNT_PROTOCOLNUM-1) * sizeof(struct Protocol));
  22. //Memory erased,
  23. CNT_PROTOCOLNUM--; //Since the record no longer exists, we should decrease the ammount of users.
  24. return 1; //Purely for error checking, in case someone ever wants it/
  25. } //1 = Success
  26. else
  27. {
  28. if(CNT_PROTOCOLNUM == 0)
  29. {
  30. perror("ERROR: 0 PROTOCOLS... Did you load protocols?");
  31. }
  32. else
  33. {
  34. perror("ERROR: No such protocol!");
  35. }
  36. return 0;
  37. }
  38. }
  39. */
  40. void unload_protocols(struct ProtocolListItem* head)
  41. {
  42. struct ProtocolListItem* current = head;
  43. while (current != NULL) {
  44. struct ProtocolListItem* next = current->next;
  45. free(current->current);
  46. free(current);
  47. current = next;
  48. }
  49. }
  50. /**
  51. * load the available protocols into the global protocol_P
  52. * @returns True(1) on success, otherwise 0
  53. */
  54. int load_protocols(struct ProtocolListItem** head)
  55. {
  56. unload_protocols(*head);
  57. int num_protocols = 14;
  58. int dec_code[] = {4, 41, 6, 17, 33, 132, 301, 302, 42, 480, 443, 477, 444, 275};
  59. int size[] = {32, 128, 16, 16, 16, 16, 0, 0, -1, 0, 0, 0, 10, 0 };
  60. char* name[] = { "ip4", "ip6", "tcp", "udp", "dccp", "sctp", "udt", "utp", "ipfs", "http", "https", "ws", "onion", "libp2p-webrtc-star" };
  61. struct ProtocolListItem* last = NULL;
  62. for(int i = 0; i < num_protocols; i++) {
  63. struct ProtocolListItem* current_item = (struct ProtocolListItem*)malloc(sizeof(struct ProtocolListItem));
  64. current_item->current = (struct Protocol*)malloc(sizeof(struct Protocol));
  65. current_item->next = NULL;
  66. current_item->current->deccode = dec_code[i];
  67. strcpy(current_item->current->name, name[i]);
  68. current_item->current->size = size[i];
  69. if (*head == NULL) {
  70. *head = current_item;
  71. } else {
  72. last->next = current_item;
  73. }
  74. last = current_item;
  75. }
  76. return 1;
  77. }
  78. /*
  79. void load_protocols_from_file(struct Protocol** in)
  80. {
  81. struct Protocol* protocol_P = *in;
  82. FILE *FPROC_POINT; //File pointer.
  83. FPROC_POINT = fopen("proto-dat", "r");//Opening proto-dat Or protocols.csv, I just formatted it to my liking.
  84. if(FPROC_POINT != NULL) //While pointer is not null.
  85. {
  86. char W_BUFF[20] = "\0";//Char array to parse file.
  87. for(int i=0; fscanf(FPROC_POINT, "%s", W_BUFF) != EOF; i++) // Scanning file and incrementing for processing.
  88. {
  89. switch(i)
  90. {
  91. case 0: //First word - HEXCODE
  92. {
  93. //ADD MEMORY FOR NEW PROTOCOL
  94. if(CNT_PROTOCOLNUM==0) //If there are no registered protocols yet, allocate memory to pointer.
  95. {
  96. protocol_P = (struct Protocol*) malloc (sizeof(struct Protocol));
  97. }
  98. else //Reallocate memory to fit one more Protocol
  99. {
  100. protocol_P = (struct Protocol*) realloc(protocol_P, (CNT_PROTOCOLNUM+1) * sizeof(struct protocol));
  101. }
  102. //strcpy((protocol_P+CNT_PROTOCOLNUM)->hexcode, W_BUFF); //Copy word to structure at hexcode A hexcode is a string so we keep it as such
  103. break;
  104. }
  105. case 1://Second word - DECCODE
  106. {
  107. (protocol_P+CNT_PROTOCOLNUM)->deccode= atoi(W_BUFF); //Copy word to structure at deccode after converting it to int.
  108. break;
  109. }
  110. case 2://Third word - SIZE
  111. {
  112. (protocol_P+CNT_PROTOCOLNUM)->size= atoi(W_BUFF); //Copy word to structure at size after converting it to int.
  113. break;
  114. }
  115. case 3://Fourth word - NAME
  116. {
  117. strcpy((protocol_P+CNT_PROTOCOLNUM)->name, W_BUFF); //Copy word to structure at name // String
  118. i=-1;
  119. CNT_PROTOCOLNUM++;
  120. break;
  121. }
  122. default:
  123. {
  124. printf("HOUSTON WE HAVE A BIG PROBLEM!!!!\nPROTOCOLS.H-REACHED DEFAULT CASE IN READING FILE!\nREPORT TO SYSTEMS ADMIN!\n");
  125. break;
  126. }
  127. }
  128. }
  129. fclose(FPROC_POINT);
  130. protocol_REMOVE_id(protocol_P, 0);
  131. }
  132. else
  133. {
  134. perror("Fatal Error:");
  135. }
  136. }
  137. */
  138. struct Protocol* proto_with_name(const struct ProtocolListItem* head, const char* proto_w_name) //Search for Protocol with inputted name
  139. {
  140. const struct ProtocolListItem* current = head;
  141. while(current != NULL)
  142. {
  143. if (strcmp(proto_w_name, current->current->name) == 0) {
  144. return current->current;
  145. }
  146. current = current->next;
  147. }
  148. return NULL;
  149. }
  150. struct Protocol* proto_with_deccode(const struct ProtocolListItem* head, int proto_w_deccode) //Search for Protocol with inputted deccode
  151. {
  152. const struct ProtocolListItem* current = head;
  153. while(current != NULL)
  154. {
  155. if (current->current->deccode == proto_w_deccode) {
  156. return current->current;
  157. }
  158. current = current->next;
  159. }
  160. return NULL;
  161. }
  162. void protocols_with_string(const struct ProtocolListItem* head, char* meee, int sizi) // NOT FINISHED, DO NOT USE!
  163. {
  164. int finalsize = 0;
  165. if(!isalnum(meee[sizi-1]) && !isalnum(meee[sizi-1]))
  166. {
  167. //Everything is alright, it's nul terminated!;
  168. finalsize = sizi;
  169. }
  170. else
  171. {
  172. //Well houston we have a problem.
  173. finalsize = sizi+2;
  174. }
  175. char mestring[finalsize];
  176. strcpy(mestring, meee);
  177. if(sizi!=finalsize)
  178. {
  179. strcpy(mestring,"\0");
  180. }
  181. char * words[50] = { NULL };
  182. int atword = 0;
  183. int mem = 0;
  184. for(int i=0; i<sizeof(mestring)-2; i++)
  185. {
  186. if(mestring[i] == '/')
  187. {
  188. printf("NEW WORD!\n");
  189. atword++;
  190. int currentsize = 0;
  191. for(int j = i+1; mestring[j] != '/' && j < sizeof(mestring)-2; j++)
  192. {
  193. currentsize++;
  194. }
  195. char haay[20];
  196. int lesbo = 0;
  197. for(int x = i+1; x<sizeof(mestring)-2; x++)
  198. {
  199. if(mestring[x] == '/')
  200. {
  201. break;
  202. }
  203. haay[lesbo] = mestring[x];
  204. lesbo++;
  205. }
  206. words[atword-1] = (char *) malloc(currentsize+2);
  207. strcpy(words[atword-1], haay);
  208. bzero(haay,20);
  209. }
  210. }
  211. printf("Result:%s\n", words[0]);
  212. for(int mm=0; mm < 50; mm++)
  213. {
  214. if(words[mm])
  215. {
  216. free(words[mm]);
  217. }
  218. }
  219. }