Main Page | Data Structures | File List | Data Fields | Globals

libmac_monitor.c File Reference

Code sample that illustrates the use of the function that receives 802.11 frames in RFMON (monitoring) mode. More...

#include <libmac/libmac.h>

Functions

int main (int argc, char *argv[])


Detailed Description


Function Documentation

int main int  argc,
char *  argv[]
 

Example usage of the 802.11 recv-side function, alongwith the parameter information functions and the interface information functions, follows. This piece of code obtains the list of interfaces using the mac_get_ifinfo function and

  • initializes the parameter structs using the mac_init_params function,
  • requests driver to return parameters using the mac_get_params function,
  • requests driver to set parameters using the mac_set_params function,
  • for the wireless interface passed as a command-line argument,
    • requests driver to append parameters using the mac_append_params function and
    • receives 802.11 frames using the mac_promisc_recv function.
  • frees the parameter and interface information structures using the mac_free_params and the mac_free_ifinfo functions respectively.
  • It returns errno and prints the error information on the screen in case of failure of any of the calls.
00025 { 00026 // Initializations. 00027 int i, j, l, err; 00028 struct mac_ifinfo *ifinfo_ptr = NULL; 00029 struct mac_ifinfo_list *temp; 00030 struct mac_params *params_ptr = NULL; 00031 struct mac_params_flags *flags_ptr = NULL; 00032 unsigned char *frame_ptr = NULL; 00033 unsigned short len = 0; 00034 unsigned char *send_parameter_info = NULL, *recv_parameter_info = NULL; 00035 00036 unsigned short hdrlen = 0, type_msb, type_lsb; 00037 00038 if (argc < 2) { 00039 printf("Usage: ./monitor name_of_interface.\n"); 00040 return(-1); 00041 } 00042 00043 // START: ALLOCATE MEMORY FOR THE LIBRARY STRUCTS. ON RECV SIDE. 00044 00045 // Get interface information. 00046 if ( (err = mac_get_ifinfo(&ifinfo_ptr)) ) 00047 { 00048 #ifdef DEBUG_ERR 00049 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00050 #endif 00051 return err; 00052 } 00053 00054 // Initialize params. struct. and params_flags struct. 00055 if ( (err = mac_init_params(&params_ptr, &flags_ptr)) ) 00056 { 00057 #ifdef DEBUG_ERR 00058 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00059 #endif 00060 return err; 00061 } 00062 // END: ALLOCATE MEMORY FOR THE LIBRARY STRUCTS. 00063 00064 #ifdef DEBUG_BASIC 00065 // Go through the list of interfaces. 00066 temp = ifinfo_ptr->mac_list; 00067 while (temp != NULL) 00068 { 00069 // Call the resp. library functions for the interface eth2/wlan0. 00070 if ( !(strcmp(temp->if_name, argv[1])) ) 00071 { 00072 printf("----------- FOR INTERFACE %s -----------\n", temp->if_name); 00073 /* Get parameter information from this interface and store it in 00074 the mac_params struct. 00075 */ 00076 if ( (err = mac_get_params(params_ptr, temp, 4, mac_rssi, 00077 mac_txpower, mac_noise, 00078 mac_currentXmitRate)) ) 00079 { 00080 #ifdef DEBUG_ERR 00081 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00082 #endif 00083 return err; 00084 } 00085 00086 // Print out parameter information retrieved from interface. 00087 printf("----------- VALUES AFTER CALL TO GET ------------------\n"); 00088 for (i = START_PARAMS_KEY; i < 4; i++) 00089 printf("Key: %d, Value: %d\n", params_ptr->key[i], 00090 params_ptr->value[i]); 00091 00092 // Try to set parameters to the values specified. 00093 if ( (err = mac_set_params(params_ptr, temp, 4, mac_noise, 10, 00094 mac_rssi, 88, 00095 mac_currentXmitRate, 2000000)) ) 00096 { 00097 #ifdef DEBUG_ERR 00098 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00099 #endif 00100 return err; 00101 } 00102 00103 /* Print out the values to which the parameters have been set. 00104 If the value printed here corresponds to the largest integer 00105 value, it means that the parameter is read-only. If it prints 00106 out the largest negative integer value, it means that the 00107 parameter is unsupported. 00108 */ 00109 printf("------------ VALUES AFTER CALL TO SET -----------------\n"); 00110 for (i = START_PARAMS_KEY; i < 4; i++) 00111 printf("Key: %d, Value: %d\n", params_ptr->key[i], params_ptr->value[i]); 00112 /* Call function to append parameter information on all incoming 00113 frames. The parameters to be appended in this case are mac_noise 00114 and mac_rssi. 00115 */ 00116 if ( (err = mac_append_params(temp, INCOMING, 2, mac_noise, 00117 mac_rssi)) ) 00118 { 00119 #ifdef DEBUG_ERR 00120 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00121 #endif 00122 return err; 00123 } 00124 00125 00126 while(1) 00127 { 00128 frame_ptr = NULL; 00129 len = 0; 00130 send_parameter_info = NULL; 00131 recv_parameter_info = NULL; 00132 hdrlen = 0; 00133 type_msb = 4; 00134 type_lsb = 4; 00135 // Receive 802.11 frames, on specified interface. 00136 if ( (err = mac_promisc_recv(&frame_ptr, &len, 00137 &send_parameter_info, 00138 &recv_parameter_info, temp)) ) 00139 { 00140 #ifdef DEBUG_ERR 00141 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00142 #endif 00143 return err; 00144 } 00145 00146 if (frame_ptr != NULL) 00147 { 00148 type_msb = frame_ptr[0] & 16; 00149 type_lsb = frame_ptr[0] & 32; 00150 00151 // MSB is 0 and LSB is 0. 00152 if ( (type_msb == 0) && (type_lsb == 0) ) 00153 { 00154 #ifdef DEBUG_ERR 00155 // fprintf(stdout, "Management frame.\n"); 00156 #endif 00157 hdrlen = 24; 00158 } 00159 else if ( (type_msb == 0) && (type_lsb == 32) ) 00160 { // MSB is 0 and LSB is 1. 00161 #ifdef DEBUG_ERR 00162 // fprintf(stdout, "Control frame.\n"); 00163 #endif 00164 hdrlen = 10; 00165 } 00166 00167 else if ( (type_msb == 16) && (type_lsb == 0) ) 00168 { // MSB is 1 and LSB is 0. 00169 #ifdef DEBUG_ADV 00170 fprintf(stdout, "Data frame.\n"); 00171 #endif 00172 hdrlen = 24; 00173 00174 fprintf(stdout, "=========== 80211 ==============\n"); 00175 fprintf(stdout, "Frame control: %hd\n", 00176 ((struct ieee_802_11_header *) 00177 frame_ptr)->frame_control); 00178 fprintf(stdout, "Duration: %hd\n", 00179 ((struct ieee_802_11_header *) 00180 frame_ptr)->duration); 00181 fprintf(stdout, "MAC addr 1: %s\n", 00182 pr_ether((unsigned char *) 00183 (((struct ieee_802_11_header *) 00184 frame_ptr)->mac1))); 00185 fprintf(stdout, "MAC addr 2: %s\n", 00186 pr_ether((unsigned char *) 00187 (((struct ieee_802_11_header *) 00188 frame_ptr)->mac2))); 00189 fprintf(stdout, "MAC addr 3: %s\n", 00190 pr_ether((unsigned char *) 00191 (((struct ieee_802_11_header *) 00192 frame_ptr)->mac3))); 00193 fprintf(stdout, "SeqCtl: %hd\n", 00194 ((struct ieee_802_11_header *) 00195 frame_ptr)->SeqCtl); 00196 fprintf(stdout, "MAC addr 4: %s\n", 00197 pr_ether((unsigned char *) 00198 (((struct ieee_802_11_header *) 00199 frame_ptr)->mac4))); 00200 fprintf(stdout, "gapLen: %hd\n", 00201 ((struct ieee_802_11_header *) 00202 frame_ptr)->gapLen); 00203 00204 printf("Length of entire frame: %d\n", len); 00205 00206 // Print out length of the received payload in bytes. 00207 printf("Length of just payload: %d\n", len - hdrlen); 00208 00209 // Check for send-side parameter information. 00210 if ( (send_parameter_info) != NULL ) 00211 { 00212 // Calculate the number of appended parameters. 00213 params_ptr->number_of_params = *send_parameter_info; 00214 /* Calculate the number of bytes occupied by the 00215 appended parameter field. 00216 */ 00217 l = SIZEOF_PARAMS_LIST_LEN + 00218 ( (params_ptr->number_of_params) * 00219 (SIZEOF_PARAMS_LIST_KEY + 00220 SIZEOF_PARAMS_LIST_VALUE) ); 00221 for(j=SIZEOF_PARAMS_LIST_LEN;j < l; 00222 j=j+SIZEOF_PARAMS_LIST_VALUE) 00223 { 00224 printf("Param key: %d\n", 00225 send_parameter_info[j]); 00226 j = j+SIZEOF_PARAMS_LIST_KEY; 00227 printf("Param value: %hd", 00228 *((unsigned short *) 00229 (send_parameter_info + j))); 00230 printf("\n"); 00231 } 00232 00233 // Check for recv-side parameter information. 00234 if ( (recv_parameter_info) != NULL ) 00235 { 00236 // Calculate the number of appended parameters. 00237 params_ptr->number_of_params = 00238 *recv_parameter_info; 00239 /* Calculate the number of bytes occupied by 00240 the appended parameter field. 00241 */ 00242 l = SIZEOF_PARAMS_LIST_LEN + 00243 ( (params_ptr->number_of_params) * 00244 (SIZEOF_PARAMS_LIST_KEY + 00245 SIZEOF_PARAMS_LIST_VALUE) ); 00246 for(j=SIZEOF_PARAMS_LIST_LEN;j < l; 00247 j=j+SIZEOF_PARAMS_LIST_VALUE) 00248 { 00249 printf("Param key: %d\n", 00250 recv_parameter_info[j]); 00251 j = j+SIZEOF_PARAMS_LIST_KEY; 00252 printf("Param value: %hd", 00253 *((unsigned short *) 00254 (recv_parameter_info + j))); 00255 printf("\n"); 00256 } 00257 } 00258 } 00259 else 00260 { // Check for recv-side parameter information. 00261 if ( (recv_parameter_info) != NULL ) 00262 { 00263 // Calculate the number of appended parameters. 00264 params_ptr->number_of_params = 00265 *recv_parameter_info; 00266 /* Calculate the number of bytes occupied by 00267 the appended parameter field. 00268 */ 00269 l = SIZEOF_PARAMS_LIST_LEN + 00270 ( (params_ptr->number_of_params) * 00271 (SIZEOF_PARAMS_LIST_KEY + 00272 SIZEOF_PARAMS_LIST_VALUE) ); 00273 for(j=SIZEOF_PARAMS_LIST_LEN;j < l; 00274 j=j+SIZEOF_PARAMS_LIST_VALUE) 00275 { 00276 printf("Param key: %d\n", 00277 recv_parameter_info[j]); 00278 j = j+SIZEOF_PARAMS_LIST_KEY; 00279 printf("Param value: %hd", 00280 *((unsigned short *) 00281 (recv_parameter_info + j))); 00282 printf("\n"); 00283 } 00284 } 00285 } 00286 } 00287 else if ( (type_msb == 16) && (type_lsb == 32) ) 00288 { // MSB is 1 and LSB is 1. 00289 #ifdef DEBUG_ADV 00290 fprintf(stdout, "Unrecognized frame type.\n"); 00291 #endif 00292 hdrlen = 4; 00293 } 00294 } 00295 else 00296 fprintf(stdout, "Only ethernet header received.\n"); 00297 } 00298 } 00299 temp = temp->next; 00300 } 00301 #endif 00302 00303 // START: FREE MEMORY ALLOCATED FOR THE LIBRARY STRUCTS. 00304 00305 // Free parameter data structures. 00306 mac_free_params(&params_ptr, &flags_ptr); 00307 // Free interface information collected and return. 00308 mac_free_ifinfo(&ifinfo_ptr); 00309 00310 // END: FREE MEMORY ALLOCATED FOR THE LIBRARY STRUCTS. 00311 00312 return 0; 00313 }


Generated on Tue Aug 24 17:01:41 2004 for libmac by doxygen 1.3.8