summaryrefslogtreecommitdiff
path: root/toxcore/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/util.c')
-rw-r--r--toxcore/util.c69
1 files changed, 62 insertions, 7 deletions
diff --git a/toxcore/util.c b/toxcore/util.c
index 1728ea21..7b5ddc49 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -10,11 +10,12 @@
10#endif 10#endif
11 11
12#include <time.h> 12#include <time.h>
13#include <stdint.h>
14#include <stdbool.h>
15 13
14/* for CLIENT_ID_SIZE */
16#include "DHT.h" 15#include "DHT.h"
17 16
17#include "util.h"
18
18uint64_t now() 19uint64_t now()
19{ 20{
20 return time(NULL); 21 return time(NULL);
@@ -32,11 +33,6 @@ uint64_t random_64b()
32 return r; 33 return r;
33} 34}
34 35
35bool ipp_eq(IP_Port a, IP_Port b)
36{
37 return (a.ip.uint32 == b.ip.uint32) && (a.port == b.port);
38}
39
40bool id_eq(uint8_t *dest, uint8_t *src) 36bool id_eq(uint8_t *dest, uint8_t *src)
41{ 37{
42 return memcmp(dest, src, CLIENT_ID_SIZE) == 0; 38 return memcmp(dest, src, CLIENT_ID_SIZE) == 0;
@@ -46,3 +42,62 @@ void id_cpy(uint8_t *dest, uint8_t *src)
46{ 42{
47 memcpy(dest, src, CLIENT_ID_SIZE); 43 memcpy(dest, src, CLIENT_ID_SIZE);
48} 44}
45
46int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled)
47{
48 int argvoffset = 0, argi;
49 for(argi = 1; argi < argc; argi++)
50 if (!strncasecmp(argv[argi], "--ipv", 5)) {
51 if (argv[argi][5] && !argv[argi][6]) {
52 char c = argv[argi][5];
53 if (c == '4')
54 *ipv6enabled = 0;
55 else if (c == '6')
56 *ipv6enabled = 1;
57 else {
58 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
59 return -1;
60 }
61 }
62 else {
63 printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
64 return -1;
65 }
66
67 if (argvoffset != argi - 1) {
68 printf("Argument must come first: %s.\n", argv[argi]);
69 return -1;
70 }
71
72 argvoffset++;
73 }
74
75 return argvoffset;
76};
77
78#ifdef LOGGING
79char logbuffer[512];
80static FILE *logfile = NULL;
81void loginit(uint16_t port)
82{
83 if (logfile)
84 fclose(logfile);
85
86 sprintf(logbuffer, "%u-%u.log", ntohs(port), now);
87 logfile = fopen(logbuffer, "w");
88};
89void loglog(char *text)
90{
91 if (logfile) {
92 fprintf(logfile, text);
93 fflush(logfile);
94 }
95};
96void logexit()
97{
98 if (logfile) {
99 fclose(logfile);
100 logfile = NULL;
101 }
102};
103#endif