nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #define _GNU_SOURCE //For getline() support |
2 | |||
3 | #include <string.h> |
||
4 | #include <stdlib.h> |
||
5 | #include <stdio.h> |
||
6 | #include <unistd.h> |
||
7 | #include <sys/time.h> |
||
8 | |||
9 | #include "helpers.h" |
||
10 | |||
11 | char generate_channel() |
||
12 | { |
||
13 | // Generate a random channel |
||
14 | |||
15 | char c = 0; |
||
16 | c = (random() % 14) + 1; |
||
17 | return c; |
||
18 | } |
||
19 | |||
20 | |||
21 | char generate_printable_char(unsigned char malformed) |
||
22 | { |
||
23 | // Generate random printable ascii char, or just a random byte |
||
24 | |||
25 | char rnd = 0; |
||
26 | |||
27 | if (malformed) { |
||
28 | rnd = random(); |
||
29 | } else { |
||
30 | rnd = (random() % 94) + ' '; |
||
31 | } |
||
32 | |||
33 | return rnd; |
||
34 | } |
||
35 | |||
36 | |||
37 | char *generate_ssid(unsigned char malformed) |
||
38 | { |
||
39 | char *ssid = (char*) malloc(256); |
||
40 | int len=0; |
||
41 | int t; |
||
42 | |||
43 | if (malformed) { |
||
44 | len = (random() % 256); |
||
45 | } else { |
||
46 | len = (random() % 32); |
||
47 | } |
||
48 | |||
49 | for (t=0; t<len; t++) ssid[t] = generate_printable_char(malformed); |
||
50 | ssid[len]='\x00'; |
||
51 | |||
52 | return ssid; |
||
53 | } |
||
54 | |||
55 | |||
56 | int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y) { |
||
57 | /* Perform the carry for the later subtraction by updating y. */ |
||
58 | if (x->tv_usec < y->tv_usec) { |
||
59 | int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; |
||
60 | y->tv_usec -= 1000000 * nsec; |
||
61 | y->tv_sec += nsec; |
||
62 | } |
||
63 | if (x->tv_usec - y->tv_usec > 1000000) { |
||
64 | int nsec = (x->tv_usec - y->tv_usec) / 1000000; |
||
65 | y->tv_usec += 1000000 * nsec; |
||
66 | y->tv_sec -= nsec; |
||
67 | } |
||
68 | |||
69 | /* Compute the time remaining to wait. |
||
70 | tv_usec is certainly positive. */ |
||
71 | result->tv_sec = x->tv_sec - y->tv_sec; |
||
72 | result->tv_usec = x->tv_usec - y->tv_usec; |
||
73 | |||
74 | /* Return 1 if result is negative. */ |
||
75 | return x->tv_sec < y->tv_sec; |
||
76 | } |
||
77 | |||
78 | void sleep_till_next_packet(unsigned int pps) { |
||
79 | static struct timeval *lastvisit = NULL; |
||
80 | struct timeval now, next, wait; |
||
81 | unsigned int tbp; |
||
82 | |||
83 | if (!pps) return; |
||
84 | |||
85 | if (! lastvisit) { |
||
86 | lastvisit = malloc(sizeof(struct timeval)); |
||
87 | gettimeofday(lastvisit, NULL); |
||
88 | return; |
||
89 | } |
||
90 | |||
91 | next.tv_sec = lastvisit->tv_sec; next.tv_usec = lastvisit->tv_usec; |
||
92 | tbp = 1000000 / pps; |
||
93 | next.tv_usec += tbp; |
||
94 | if (next.tv_usec > 999999) { next.tv_usec -= 1000000; next.tv_sec++; } |
||
95 | |||
96 | gettimeofday(&now, NULL); |
||
97 | if (! timeval_subtract(&wait, &next, &now)) |
||
98 | usleep(wait.tv_usec); |
||
99 | |||
100 | gettimeofday(lastvisit, NULL); |
||
101 | } |
||
102 | |||
103 | char *read_next_line(char *filename, char reset) |
||
104 | { |
||
105 | static int last_pos = 0; |
||
106 | int bytesread; |
||
107 | char *line = NULL; |
||
108 | char **pline = &line; |
||
109 | FILE *file_fp; |
||
110 | size_t initsize = 1; |
||
111 | |||
112 | if (reset) last_pos = 0; |
||
113 | |||
114 | if ((file_fp = fopen(filename, "r")) == NULL) { |
||
115 | printf("Cannot open file: %s\n", filename); |
||
116 | exit(2); |
||
117 | } |
||
118 | |||
119 | fseek(file_fp, last_pos, SEEK_SET); |
||
120 | bytesread = getline(pline, &initsize, file_fp); |
||
121 | line = *pline; |
||
122 | |||
123 | if (bytesread == -1) { |
||
124 | last_pos = 0; |
||
125 | free(line); //Thanks valgrind for getting this BITCH. even if nothing is read from file, memory is still allocated by getline! |
||
126 | fclose(file_fp); |
||
127 | return NULL; |
||
128 | } |
||
129 | |||
130 | last_pos = ftell(file_fp); |
||
131 | fclose(file_fp); |
||
132 | |||
133 | //Remove newline if any |
||
134 | if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = 0x00; |
||
135 | |||
136 | return line; |
||
137 | } |
||
138 | |||
139 | unsigned char *hex2bin(char *in, int *len) { |
||
140 | *len = strlen(in) / 2; |
||
141 | unsigned char *out = malloc(*len); |
||
142 | int i; |
||
143 | |||
144 | for (i=0; i<*len; i++) { |
||
145 | sscanf(in + (i*2), "%2hhx", out+i); |
||
146 | } |
||
147 | |||
148 | return out; |
||
149 | } |