util.h
Go to the documentation of this file.
1 /* vim: set ts=2 expandtab: */
2 /**
3  * @file util.h
4  * @brief utility functions for the Commotion daemon
5  *
6  * @author Josh King (jheretic), jking@chambana.net
7  *
8  * @internal
9  * Created 03/07/2013
10  * Revision $Id: doxygen.commotion.templates,v 0.1 2013/01/01 09:00:00 jheretic Exp $
11  * Compiler gcc/g++
12  * Company The Open Technology Institute
13  * Copyright Copyright (c) 2013, Josh King
14  *
15  * This file is part of Commotion, Copyright (c) 2013, Josh King
16  *
17  * Commotion is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Affero General Public License as
19  * published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * Commotion is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Affero General Public License for more details.
26  *
27  * You should have received a copy of the GNU Affero General Public License
28  * along with Commotion. If not, see <http://www.gnu.org/licenses/>.
29  *
30  * =====================================================================================
31  */
32 
33 #ifndef _UTIL_H
34 #define _UTIL_H
35 #include <stdlib.h>
36 #include <stdint.h>
37 
38 #define NEW(O, T) O##_create(sizeof(T##_t), T##_proto)
39 #define _(N) proto.N
40 
41 #define LOWERCASE(N) for ( ; *N; ++N) *N = tolower(*N);
42 #define UPPERCASE(N) for ( ; *N; ++N) *N = toupper(*N);
43 
44 #define MAX_ARGS 16
45 
46 #define BSSID_SIZE 6
47 #define BSSID_STR_SIZE 18
48 #define ESSID_SIZE 32
49 #define FREQ_SIZE 4
50 #define CHAN_SIZE 2
51 
52 /**
53  * @brief concatenates two strings
54  * @param dst destination for new string
55  * @param src string to be added to
56  * @param size size of combined strings
57  */
58 size_t strlcat(char *dst, const char *src, const size_t size);
59 
60 /**
61  * @brief copies a string
62  * @param dst destination for new string
63  * @param src string to be copied
64  * @param size size of string
65  */
66 size_t strlcpy(char *dest, const char *src, const size_t size);
67 
68 /**
69  * @brief prints output from string "str" in a specified format
70  * @param str string to be printed
71  * @param size size of string
72  * @param format output format
73  */
74 size_t snprintfcat(char *str, size_t size, const char *format, ...);
75 
76 /**
77  * @brief removes white space from a given string (to parse for arguments)
78  * @param s string to parse
79  * @param out output from the string (with white space removed)
80  * @param outlen length of the output
81  */
82 size_t strstrip(const char *s, char *out, const size_t outlen);
83 
84 /**
85  * @brief compares version numbers of two software releases
86  * @param aver version number for software 'a'
87  * @param bver version number for software 'b'
88  */
89 int compare_version(const char * aver, const char *bver);
90 
91 typedef int (*file_iter)(const char* path, const char *filename);
92 
93 /**
94  * @brief processes file paths
95  * @param dir_path string of the directory path
96  * @param loader number of directories in the file path
97  */
98 int process_files(const char *dir_path, file_iter loader);
99 
100 /**
101  * @brief parses a string and converts into a set of arguments
102  * @param input the string to be parsed
103  * @param argv pointer to argument list
104  * @param argc number of arguments read from the string
105  * @param max maximum length of string
106  */
107 int string_to_argv(const char *input, char **argv, int *argc, const size_t max);
108 
109 /**
110  * @brief converts argument vectors to a single string
111  * @param argv pointer to argument list
112  * @param argc number of arguments
113  * @param output concatenated string of arguments
114  * @param max maximum length of the string
115  */
116 int argv_to_string(char **argv, const int argc, char *output, const size_t max);
117 
118 /**
119  * @brief converts a MAC address from a string to individual bytes
120  * @param macstr MAC string
121  * @param mac an array for storing the MAC address
122  */
123 void mac_string_to_bytes(char *macstr, unsigned char mac[6]);
124 
125 /**
126  * @brief prints MAC address from MAC array
127  * @param mac array storing MAC address
128  */
129 void print_mac(unsigned char mac[6]);
130 
131 /**
132  * @brief sets Wi-Fi frequency corresponding to specified channel
133  * @param channel specified channel
134  */
135 int wifi_freq(const int channel);
136 
137 /**
138  * @brief sets Wi-Fi channel corresponding to specified freuency
139  * @param frequency specified frequency
140  */
141 int wifi_chan(const int frequency);
142 
143 /**
144 * @brief generates a BSSID from hash of ESSID and channel
145 * @param essid The ESSID to hash
146 * @param channel an integer of the channel
147 * @param bbsid The returned 6-byte BSSID
148 */
149 void get_bssid(const char *essid, const unsigned int channel, char *bssid);
150 
151 /**
152  * @brief prints a raw byte array in hex and ascii output
153  * @param mem the byte array to print
154  * @param len length of the byte array
155  */
156 void hexdump(void *mem, unsigned int len);
157 
158 #endif
size_t strlcpy(char *dest, const char *src, const size_t size)
copies a string
Definition: util.c:50
size_t snprintfcat(char *str, size_t size, const char *format,...)
prints output from string "str" in a specified format
Definition: util.c:74
size_t strlcat(char *dst, const char *src, const size_t size)
concatenates two strings
Definition: util.c:60
void mac_string_to_bytes(char *macstr, unsigned char mac[6])
converts a MAC address from a string to individual bytes
Definition: util.c:197
void print_mac(unsigned char mac[6])
prints MAC address from MAC array
Definition: util.c:203
int compare_version(const char *aver, const char *bver)
compares version numbers of two software releases
Definition: util.c:117
int wifi_freq(const int channel)
sets Wi-Fi frequency corresponding to specified channel
Definition: util.c:208
int wifi_chan(const int frequency)
sets Wi-Fi channel corresponding to specified freuency
Definition: util.c:261
void get_bssid(const char *essid, const unsigned int channel, char *bssid)
generates a BSSID from hash of ESSID and channel
Definition: util.c:315
size_t strstrip(const char *s, char *out, const size_t outlen)
removes white space from a given string (to parse for arguments)
Definition: util.c:101
int process_files(const char *dir_path, file_iter loader)
processes file paths
Definition: util.c:145
int argv_to_string(char **argv, const int argc, char *output, const size_t max)
converts argument vectors to a single string
Definition: util.c:184
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
Definition: util.c:167
void hexdump(void *mem, unsigned int len)
prints a raw byte array in hex and ascii output
Definition: util.c:371