multiaddr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #include <string.h>
  2. #include <sys/socket.h>
  3. #include "multiaddr/varhexutils.h"
  4. #include "multiaddr/varint.h"
  5. #include "multiaddr/protocols.h"
  6. #include "multiaddr/protoutils.h"
  7. #include "multiaddr/multiaddr.h"
  8. int strpos(char *haystack, char *needle)
  9. {
  10. char *p = strstr(haystack, needle);
  11. if (p)
  12. {
  13. return p - haystack;
  14. }
  15. else
  16. {
  17. return -1; // Not found = -1.
  18. }
  19. }
  20. /**
  21. * Construct a new MultiAddress struct
  22. * @returns an empty MultiAddress struct
  23. */
  24. struct MultiAddress* multiaddress_new() {
  25. struct MultiAddress* out = (struct MultiAddress*)malloc(sizeof(struct MultiAddress));
  26. if (out != NULL) {
  27. out->bsize = 0;
  28. out->bytes = NULL;
  29. out->string = NULL;
  30. }
  31. return out;
  32. }
  33. /**
  34. * construct a new MultiAddress from bytes
  35. * @param byteaddress the byte array
  36. * @param size the size of the byte array
  37. * @returns a new MultiAddress struct filled in, or NULL on error
  38. */
  39. struct MultiAddress* multiaddress_new_from_bytes(const uint8_t* byteaddress, int size)//Construct new address from bytes
  40. {
  41. struct MultiAddress* out = multiaddress_new();
  42. if (out != NULL) {
  43. if(byteaddress!=NULL)
  44. {
  45. out->bytes = malloc(size);
  46. if (out->bytes == NULL) {
  47. multiaddress_free(out);
  48. return NULL;
  49. }
  50. out->bsize = size;
  51. memcpy(out->bytes, byteaddress, size);
  52. if(!bytes_to_string(&out->string,byteaddress,size))
  53. {
  54. multiaddress_free(out);
  55. return NULL;
  56. }
  57. } else {
  58. multiaddress_free(out);
  59. return NULL;
  60. }
  61. }
  62. return out;
  63. }
  64. struct MultiAddress* multiaddress_new_from_string(const char* straddress)//Construct new address from string
  65. {
  66. struct MultiAddress* out = multiaddress_new();
  67. if (out != NULL) {
  68. out->string = malloc(strlen(straddress) + 1);
  69. if (out->string == NULL) {
  70. multiaddress_free(out);
  71. return NULL;
  72. }
  73. strcpy(out->string, straddress);
  74. if (string_to_bytes(&(out->bytes), &out->bsize, out->string, strlen(out->string)) == 0 )
  75. {
  76. multiaddress_free(out);
  77. return NULL;
  78. }
  79. }
  80. return out;
  81. }
  82. int multiaddress_is_ip(const struct MultiAddress* in) {
  83. if (in->bytes > 0) {
  84. int byte = in->bytes[0];
  85. if (byte == 4 || byte == 41)
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. int multiaddress_is_ip4(const struct MultiAddress* in) {
  91. return in->bytes[0] == 4;
  92. }
  93. int multiaddress_is_ip6(const struct MultiAddress* in) {
  94. return in->bytes[0] == 41;
  95. }
  96. int multiaddress_get_ip_family(const struct MultiAddress* in) {
  97. if (in->bytes[0] == 4)
  98. return AF_INET;
  99. if (in->bytes[0] == 41)
  100. return AF_INET6;
  101. return 0;
  102. }
  103. /***
  104. * Pulls the textual representation of the IP address from a multihash
  105. * @param in the multihash to parse
  106. * @param ip where to put the ip address
  107. * @returns true(1) on success, otherwise 0
  108. */
  109. int multiaddress_get_ip_address(const struct MultiAddress* in, char** ip) {
  110. // the incoming address is not what was expected
  111. if (strncmp(in->string, "/ip4/", 5) != 0 && strncmp(in->string, "/ip6/", 5) != 0)
  112. return 0;
  113. if (strstr(in->string, "/tcp/") == NULL && strstr(in->string, "/udp/") == NULL)
  114. return 0;
  115. // ip
  116. char* str = malloc(strlen(in->string));
  117. if (str == NULL)
  118. return 0;
  119. strcpy(str, &in->string[5]); // gets rid of /ip4/
  120. char* pos = strchr(str, '/');
  121. pos[0] = 0;
  122. *ip = malloc(strlen(str) + 1);
  123. strcpy(*ip, str);
  124. free(str);
  125. return 1;
  126. }
  127. /***
  128. * Pulls the IP port from a multiaddress
  129. * @param in the multiaddress
  130. * @param port where to put the port
  131. * @returns the port, or a negative number for an error
  132. */
  133. int multiaddress_get_ip_port(const struct MultiAddress* in) {
  134. char* ptr = strstr(in->string, "/tcp/");
  135. if (ptr == NULL)
  136. ptr = strstr(in->string, "/udp/");
  137. if (ptr == NULL)
  138. return -1;
  139. ptr += 5;
  140. char* end_ptr = strstr(ptr, "/");
  141. if (end_ptr == NULL) {
  142. return atoi(ptr);
  143. }
  144. char str[end_ptr - ptr + 1];
  145. memcpy(str, ptr, end_ptr - ptr);
  146. str[end_ptr-ptr] = '\0';
  147. return atoi(str);
  148. }
  149. char* multiaddress_get_peer_id(const struct MultiAddress* in) {
  150. char* result = NULL;
  151. int str_len = 0;
  152. char* slash = NULL;
  153. char* ptr = NULL;
  154. ptr = strstr(in->string, "/ipfs/");
  155. if (ptr != NULL && ptr[6] != 0) {
  156. ptr += 6;
  157. str_len = strlen(ptr);
  158. slash = strchr(ptr, '/');
  159. if (slash != NULL) {
  160. str_len = slash - ptr;
  161. }
  162. if (str_len > 0) {
  163. result = malloc(str_len + 1);
  164. if (result != NULL) {
  165. memset(result, 0, str_len + 1);
  166. memcpy(result, ptr, str_len);
  167. }
  168. }
  169. }
  170. return result;
  171. }
  172. void multiaddress_free(struct MultiAddress* in) {
  173. if (in != NULL) {
  174. if (in->bytes != NULL)
  175. free(in->bytes);
  176. if (in->string != NULL)
  177. free(in->string);
  178. free(in);
  179. in = NULL;
  180. }
  181. }
  182. /**
  183. * Copy a multiaddress from one memory location to another
  184. * @param in the source
  185. * @returns the new struct MultiAddress or NULL if there was a problem (i.e. out of memory)
  186. */
  187. struct MultiAddress* multiaddress_copy(const struct MultiAddress* in) {
  188. struct MultiAddress* out = NULL;
  189. if (in != NULL) {
  190. out = (struct MultiAddress*)malloc(sizeof(struct MultiAddress));
  191. if (out != NULL) {
  192. if (in->bsize > 0) {
  193. out->bytes = malloc(in->bsize);
  194. if (out->bytes == NULL) {
  195. free(out);
  196. return NULL;
  197. }
  198. out->bsize = in->bsize;
  199. memcpy(out->bytes, in->bytes, out->bsize);
  200. } // bytes need to be copied
  201. if (in->string != NULL) {
  202. out->string = malloc(strlen(in->string) + 1);
  203. if (out->string == NULL) {
  204. if (out->bsize > 0)
  205. free(out->bytes);
  206. free(out);
  207. return NULL;
  208. }
  209. strcpy(out->string, in->string);
  210. } // string needs to be copied
  211. } // structure allocated
  212. } // good parameters
  213. return out;
  214. }
  215. /**
  216. * Put a string into the MultiAddress and recalculate the bytes
  217. * @param result the struct
  218. * @param string the new string
  219. */
  220. int multiaddress_encapsulate(struct MultiAddress* result, char* string)
  221. {
  222. if(result != NULL && string != NULL)
  223. {
  224. // remove the old values
  225. if (result->bytes != NULL)
  226. free(result->bytes);
  227. result->bytes = NULL;
  228. result->bsize = 0;
  229. char * exstr;
  230. if(string[0] == '/')
  231. {
  232. exstr = (char *) malloc(strlen(result->string)+1);
  233. }
  234. else
  235. {
  236. exstr = (char *) malloc(strlen(result->string));
  237. }
  238. strcpy(exstr, result->string);
  239. free(result->string);
  240. // insert the new values
  241. result->string = malloc(strlen(string) + strlen(exstr) + 1);
  242. if (result->string == NULL) {
  243. multiaddress_free(result);
  244. return 0;
  245. }
  246. strcpy(result->string, exstr);
  247. free(exstr);
  248. if(string[0] == '/')
  249. {
  250. strcat(result->string, string+1);
  251. }
  252. else
  253. {
  254. strcat(result->string, string);
  255. }
  256. if(string_to_bytes(&result->bytes, &result->bsize, result->string, strlen(result->string)+1) == 0)
  257. {
  258. multiaddress_free(result);
  259. return 0;
  260. }
  261. } else {
  262. return 0;
  263. }
  264. return 1;
  265. }
  266. /**
  267. * Find scri and remove it from the resultant value
  268. * (ie /ip4/127.0.0.1/tcp/4001 becomes ip4/127.0.0.1 when you call decapsulate(addr, "/tcp")
  269. * @param result the address to work with
  270. * @param srci the string to look for
  271. */
  272. int multiaddress_decapsulate(struct MultiAddress * result, char * srci)
  273. {
  274. if(result!=NULL && srci!=NULL)
  275. {
  276. char * procstr = NULL;
  277. procstr = result->string;
  278. int i=0;
  279. int sz=strlen(procstr);
  280. char * src = NULL;
  281. src=srci;
  282. // change slash to space
  283. for(i=0;i<sz;i++)
  284. {
  285. if(procstr[i] == '/')
  286. {
  287. procstr[i]=' ';
  288. }
  289. }
  290. int pos=-1;
  291. pos=strpos(procstr,src);
  292. if(pos!=-1)
  293. {
  294. // fill rest with 0s
  295. for(i=pos;i<sz;i++)
  296. {
  297. procstr[i] = '\0';
  298. }
  299. // replace space with slash
  300. for(i=0;i<sz;i++)
  301. {
  302. if(procstr[i] == ' ')
  303. {
  304. procstr[i] = '/';
  305. }
  306. }
  307. //Bytes update
  308. if (result->bytes != NULL)
  309. free(result->bytes);
  310. result->bytes = NULL;
  311. result->bsize = 0;
  312. if(string_to_bytes(&result->bytes, &result->bsize, result->string, strlen(result->string)+1) == 0)
  313. {
  314. multiaddress_free(result);
  315. return 0;
  316. }
  317. return 1;
  318. }
  319. else
  320. {
  321. return 0;
  322. }
  323. return 0;
  324. }
  325. else
  326. {
  327. return 0;
  328. }
  329. }
  330. /**
  331. * Check to see how these two addresses compare
  332. * @param a side A
  333. * @param b side B
  334. * @returns <0 if B > A; >0 if A > B; 0 if A == B
  335. */
  336. int multiaddress_compare(const struct MultiAddress* a, const struct MultiAddress* b) {
  337. if (a == NULL && b == NULL)
  338. return 0;
  339. if (a == NULL && b != NULL)
  340. return -1;
  341. if (a != NULL && b == NULL)
  342. return 1;
  343. int total = b->bsize - a->bsize;
  344. if (total != 0)
  345. return total;
  346. for(size_t i = 0; i < b->bsize; i++) {
  347. total = b->bytes[i] - a->bytes[i];
  348. if (total != 0)
  349. return total;
  350. }
  351. return 0;
  352. }
  353. /**
  354. * Check to see how these two addresses compare, ignoring IP address,
  355. * only looking at the first /ipfs/ID hash
  356. * @param a side A
  357. * @param b side B
  358. * @returns <0 if B > A; >0 if A > B; 0 if A == B
  359. */
  360. int multiaddress_compare_id(const struct MultiAddress* a, const struct MultiAddress* b) {
  361. char* a_id = multiaddress_get_peer_id(a);
  362. char* b_id = multiaddress_get_peer_id(b);
  363. if (a_id == NULL && b_id == NULL)
  364. return 0;
  365. if (a_id == NULL && b_id != NULL)
  366. return -1;
  367. if (a_id != NULL && b_id == NULL)
  368. return 1;
  369. int retVal = strcmp(a_id, b_id);
  370. if (a_id != NULL)
  371. free(a_id);
  372. if (b_id != NULL)
  373. free(b_id);
  374. return retVal;
  375. }