util.h File Reference

utility functions for the Commotion daemon More...

#include <stdlib.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define NEW(O, T)   O##_create(sizeof(T##_t), T##_proto)
 
#define _(N)   proto.N
 
#define LOWERCASE(N)   for ( ; *N; ++N) *N = tolower(*N);
 
#define UPPERCASE(N)   for ( ; *N; ++N) *N = toupper(*N);
 
#define MAX_ARGS   16
 
#define BSSID_SIZE   6
 
#define BSSID_STR_SIZE   18
 
#define ESSID_SIZE   32
 
#define FREQ_SIZE   4
 
#define CHAN_SIZE   2
 

Typedefs

typedef int(* file_iter )(const char *path, const char *filename)
 

Functions

size_t strlcat (char *dst, const char *src, const size_t size)
 concatenates two strings More...
 
size_t strlcpy (char *dest, const char *src, const size_t size)
 copies a string More...
 
size_t snprintfcat (char *str, size_t size, const char *format,...)
 prints output from string "str" in a specified format More...
 
size_t strstrip (const char *s, char *out, const size_t outlen)
 removes white space from a given string (to parse for arguments) More...
 
int compare_version (const char *aver, const char *bver)
 compares version numbers of two software releases More...
 
int process_files (const char *dir_path, file_iter loader)
 processes file paths More...
 
int string_to_argv (const char *input, char **argv, int *argc, const size_t max)
 parses a string and converts into a set of arguments More...
 
int argv_to_string (char **argv, const int argc, char *output, const size_t max)
 converts argument vectors to a single string More...
 
void mac_string_to_bytes (char *macstr, unsigned char mac[6])
 converts a MAC address from a string to individual bytes More...
 
void print_mac (unsigned char mac[6])
 prints MAC address from MAC array More...
 
int wifi_freq (const int channel)
 sets Wi-Fi frequency corresponding to specified channel More...
 
int wifi_chan (const int frequency)
 sets Wi-Fi channel corresponding to specified freuency More...
 
void get_bssid (const char *essid, const unsigned int channel, char *bssid)
 generates a BSSID from hash of ESSID and channel More...
 
void hexdump (void *mem, unsigned int len)
 prints a raw byte array in hex and ascii output More...
 

Detailed Description

utility functions for the Commotion daemon

Author
Josh King (jheretic), jking.nosp@m.@cha.nosp@m.mbana.nosp@m..net

Function Documentation

int argv_to_string ( char **  argv,
const int  argc,
char *  output,
const size_t  max 
)

converts argument vectors to a single string

Parameters
argvpointer to argument list
argcnumber of arguments
outputconcatenated string of arguments
maxmaximum length of the string

References strlcat().

184  {
185  int i;
186  for(i = 0; i < argc; i++) {
187  strlcat(output, argv[i], max);
188  strlcat(output, " ", max);
189  }
190  if(i < argc) {
191  ERROR("Failed to concatenate all of argv.");
192  return 0;
193  }
194  return 1;
195 }
size_t strlcat(char *dst, const char *src, const size_t size)
concatenates two strings
Definition: util.c:60
int compare_version ( const char *  aver,
const char *  bver 
)

compares version numbers of two software releases

Parameters
averversion number for software 'a'
bverversion number for software 'b'
117  {
118  char *a, *b, *anext, *bnext;
119  long int avalue, bvalue;
120 
121  // Grab pointer to first number in each version string.
122  a = strpbrk(aver, "0123456789");
123  b = strpbrk(bver, "0123456789");
124 
125  while(a && b) {
126  // Convert characters to number.
127  avalue = strtol(a, &anext, 10);
128  bvalue = strtol(b, &bnext, 10);
129 
130  if(avalue < bvalue) return -1;
131  if(avalue > bvalue) return 1;
132 
133  a = strpbrk(anext, "0123456789");
134  b = strpbrk(bnext, "0123456789");
135  }
136 
137  // If valid characters remain in 1 string, assume a point release.
138  if(b) return -1;
139  if(a) return 1;
140 
141  // Versions are identical.
142  return 0;
143 }
void get_bssid ( const char *  essid,
const unsigned int  channel,
char *  bssid 
)

generates a BSSID from hash of ESSID and channel

Parameters
essidThe ESSID to hash
channelan integer of the channel
bbsidThe returned 6-byte BSSID
315  {
316  DEBUG("ESSID: %s, Channel: %d", essid, chan);
317  unsigned char hash[BSSID_SIZE];
318  memset(hash, '\0', sizeof(hash));
319  char channel[CHAN_SIZE];
320  memset(channel, '\0', sizeof(channel));
321  int i;
322  MD5_CTX ctx;
323  MD5_Init(&ctx);
324 
325  MD5_Update(&ctx, essid, strlen(essid));
326  MD5_Final(hash, &ctx);
327  DEBUG("Hash: %s", (char *)hash);
328 
329  for(i = 0; i < BSSID_SIZE - CHAN_SIZE; i++)
330  {
331  bssid[i] = hash[i];
332  DEBUG("BSSID byte %d as char: %c, as int: %d", i, bssid[i], bssid[i]);
333  }
334 
335  /*
336  * Set the first byte to 0x02 (the value of
337  * a link-local ethernet address) which is
338  * required for a valid bssid.
339  */
340  bssid[0] = 0x02;
341 
342  bssid[i] = chan / 10;
343  bssid[i + 1] = chan % 10;
344 
345  DEBUG("BSSID buffer: %s", (char *)bssid);
346 
347  return;
348 }
Definition: md5.h:34
void hexdump ( void *  mem,
unsigned int  len 
)

prints a raw byte array in hex and ascii output

Parameters
memthe byte array to print
lenlength of the byte array

References HEXDUMP_COLS.

372 {
373  unsigned int i, j;
374 
375  for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
376  {
377  /* print offset */
378  if(i % HEXDUMP_COLS == 0)
379  {
380  printf("0x%06x: ", i);
381  }
382 
383  /* print hex data */
384  if(i < len)
385  {
386  printf("%02x ", 0xFF & ((char*)mem)[i]);
387  }
388  else /* end of block, just aligning for ASCII dump */
389  {
390  printf(" ");
391  }
392 
393  /* print ASCII dump */
394  if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
395  {
396  for(j = i - (HEXDUMP_COLS - 1); j <= i; j++)
397  {
398  if(j >= len) /* end of block, not really printing */
399  {
400  putchar(' ');
401  }
402  else if(isprint(((char*)mem)[j])) /* printable char */
403  {
404  putchar(0xFF & ((char*)mem)[j]);
405  }
406  else /* other char */
407  {
408  putchar('.');
409  }
410  }
411  putchar('\n');
412  }
413  }
414 }
#define HEXDUMP_COLS
Definition: util.c:370
void mac_string_to_bytes ( char *  macstr,
unsigned char  mac[6] 
)

converts a MAC address from a string to individual bytes

Parameters
macstrMAC string
macan array for storing the MAC address
197  {
198  memset(mac, '\0', 6);
199  sscanf(macstr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
200  return;
201 }
void print_mac ( unsigned char  mac[6])

prints MAC address from MAC array

Parameters
macarray storing MAC address
203  {
204  printf("%02x:%02x:%02x:%02x:%02x:%02x", mac[0] & 0xff, mac[1] & 0xff, mac[2] & 0xff, mac[3] & 0xff, mac[4] & 0xff, mac[5] & 0xff);
205  return;
206 }
int process_files ( const char *  dir_path,
file_iter  loader 
)

processes file paths

Parameters
dir_pathstring of the directory path
loadernumber of directories in the file path

Referenced by co_plugins_load(), and co_profile_import_files().

145  {
146  size_t path_size = strlen(dir_path);
147  CHECK((path_size > 0) && (path_size <= PATH_MAX), "Invalid path length!");
148  DIR *dir_iter = NULL;
149  CHECK((dir_iter = opendir(dir_path)), "Could not read directory!");
150  struct dirent *dir_entry = NULL;
151  DEBUG("Processing files in directory %s", dir_path);
152 
153  while((dir_entry = readdir(dir_iter)) != NULL) {
154  DEBUG("Checking file %s", dir_entry->d_name);
155  if(!strcmp(dir_entry->d_name, ".")) continue;
156  if(!strcmp(dir_entry->d_name, "..")) continue;
157  if(!(*loader)(dir_path, dir_entry->d_name)) break;
158  }
159 
160  if(dir_iter) closedir(dir_iter);
161  return 1;
162 
163 error:
164  return 0;
165 }
size_t snprintfcat ( char *  str,
size_t  size,
const char *  format,
  ... 
)

prints output from string "str" in a specified format

Parameters
strstring to be printed
sizesize of string
formatoutput format
74  {
75  size_t result;
76  va_list args;
77  size_t len = strnlen(str, size);
78 
79  va_start(args, format);
80  result = vsnprintf(str + len, size - len, format, args);
81  va_end(args);
82 
83  return result + len;
84 }
int string_to_argv ( const char *  input,
char **  argv,
int *  argc,
const size_t  max 
)

parses a string and converts into a set of arguments

Parameters
inputthe string to be parsed
argvpointer to argument list
argcnumber of arguments read from the string
maxmaximum length of string
167  {
168  int count = 0;
169  char *saveptr = NULL;
170  char *token = NULL;
171  char *input_tmp = strdup(input);
172  CHECK_MEM(token = strtok_r(input_tmp, " ", &saveptr));
173  while(token && count < max) {
174  argv[count++] = token;
175  token = strtok_r(NULL, " ", &saveptr);
176  }
177  *argc = count;
178  return 1;
179 error:
180  free(input_tmp);
181  return 0;
182 }
size_t strlcat ( char *  dst,
const char *  src,
const size_t  size 
)

concatenates two strings

Parameters
dstdestination for new string
srcstring to be added to
sizesize of combined strings

Referenced by argv_to_string().

61 {
62  size_t used, length, copy;
63 
64  used = strlen(dst);
65  length = strlen(src);
66  if (size > 0 && used < size - 1) {
67  copy = (length >= size - used) ? size - used - 1 : length;
68  memcpy(dst + used, src, copy);
69  dst[used + copy] = '\0';
70  }
71  return used + length;
72 }
size_t strlcpy ( char *  dest,
const char *  src,
const size_t  size 
)

copies a string

Parameters
dstdestination for new string
srcstring to be copied
sizesize of string

Referenced by co_iface_add(), and strstrip().

51 {
52  const size_t length = strlen(src);
53  if (size != 0) {
54  memmove(dest, src, (length > size - 1) ? size - 1 : length);
55  dest[size - 1] = '\0';
56  }
57  return length;
58 }
size_t strstrip ( const char *  s,
char *  out,
const size_t  outlen 
)

removes white space from a given string (to parse for arguments)

Parameters
sstring to parse
outoutput from the string (with white space removed)
outlenlength of the output

References strlcpy().

101  {
102  size_t len = strlen(s);
103  const char *ptr = s;
104  if(len == 0) return 0;
105 
106  while(*ptr && isspace(*ptr)) {
107  ++ptr;
108  --len;
109  }
110 
111  if(len <= outlen) {
112  strlcpy(out, ptr, len + 1);
113  return len;
114  } else return -1;
115 }
size_t strlcpy(char *dest, const char *src, const size_t size)
copies a string
Definition: util.c:50
int wifi_chan ( const int  frequency)

sets Wi-Fi channel corresponding to specified freuency

Parameters
frequencyspecified frequency
261  {
262  if(frequency < 5080) {
263  return (frequency - 5000)/5;
264  }
265  switch(frequency) {
266  case 2412:
267  return 1;
268  break;
269  case 2417:
270  return 2;
271  break;
272  case 2422:
273  return 3;
274  break;
275  case 2427:
276  return 4;
277  break;
278  case 2432:
279  return 5;
280  break;
281  case 2437:
282  return 6;
283  break;
284  case 2442:
285  return 7;
286  break;
287  case 2447:
288  return 8;
289  break;
290  case 2452:
291  return 9;
292  break;
293  case 2457:
294  return 10;
295  break;
296  case 2462:
297  return 11;
298  break;
299  case 2467:
300  return 12;
301  break;
302  case 2472:
303  return 13;
304  break;
305  case 2484:
306  return 14;
307  break;
308  default:
309  ERROR("Not a valid frequency!");
310  return 0;
311  }
312 
313 }
int wifi_freq ( const int  channel)

sets Wi-Fi frequency corresponding to specified channel

Parameters
channelspecified channel
208  {
209  if(channel > 14) {
210  return 5000 + 5*channel;
211  }
212  switch(channel) {
213  case 1:
214  return 2412;
215  break;
216  case 2:
217  return 2417;
218  break;
219  case 3:
220  return 2422;
221  break;
222  case 4:
223  return 2427;
224  break;
225  case 5:
226  return 2432;
227  break;
228  case 6:
229  return 2437;
230  break;
231  case 7:
232  return 2442;
233  break;
234  case 8:
235  return 2447;
236  break;
237  case 9:
238  return 2452;
239  break;
240  case 10:
241  return 2457;
242  break;
243  case 11:
244  return 2462;
245  break;
246  case 12:
247  return 2467;
248  break;
249  case 13:
250  return 2472;
251  break;
252  case 14:
253  return 2484;
254  break;
255  default:
256  ERROR("Not a valid channel!");
257  return 0;
258  }
259 }