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

libmac_sender.c File Reference

Code sample that illustrates the use of the send functions and the parameter information functions. More...

#include <libmac/libmac.h>

Defines

#define PRESET_PAYLOAD_SIZE   104
 Fixed, arbitrary size for payload.

Functions

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


Detailed Description


Function Documentation

int main int  argc,
char *  argv[]
 

Example usage, of the send-side functions 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
    • sends a frame to the broadcast MAC address using the mac_send_broadcast 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.
00029 { 00030 // Initializations. 00031 int i, err; 00032 struct mac_ifinfo *ptr = NULL; 00033 struct mac_ifinfo_list *temp; 00034 struct mac_params *params_ptr = NULL; 00035 struct mac_params_flags *flags_ptr = NULL; 00036 char payload[PRESET_PAYLOAD_SIZE]; 00037 unsigned char ucast_mac[MACSIZE]; 00038 00039 if (argc < 2) { 00040 printf("Usage: ./sender name_of_interface.\n"); 00041 return(-1); 00042 } 00043 00044 // Setting the payload as an arbitrary string of characters. 00045 memset(payload, 'a', PRESET_PAYLOAD_SIZE); 00046 00047 // If sending to a unicast MAC address, then store the address in this array. 00048 ucast_mac[0] = 0x00; 00049 ucast_mac[1] = 0x02; 00050 ucast_mac[2] = 0x2D; 00051 ucast_mac[3] = 0x6D; 00052 ucast_mac[4] = 0x16; 00053 ucast_mac[5] = 0xAB; 00054 00055 // START: ALLOCATE MEMORY FOR THE LIBRARY STRUCTS. 00056 // Get interface information. 00057 if ( (err = mac_get_ifinfo(&ptr)) ) 00058 { 00059 #ifdef DEBUG_ERR 00060 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00061 #endif 00062 return err; 00063 } 00064 00065 // Initialize params. struct. and params_flags struct. 00066 if ( (err = mac_init_params(&params_ptr, &flags_ptr)) ) 00067 { 00068 #ifdef DEBUG_ERR 00069 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00070 #endif 00071 return err; 00072 } 00073 // END: ALLOCATE MEMORY FOR THE LIBRARY STRUCTS. 00074 00075 #ifdef DEBUG_BASIC 00076 // Go through the list of interfaces. 00077 temp = ptr->mac_list; 00078 while (temp != NULL) 00079 { 00080 // Call the resp. library functions for the interface eth2. 00081 if ( !(strcmp(temp->if_name, argv[1])) ) 00082 { 00083 #ifdef DEBUG_ADV 00084 fprintf(stdout, "%s: %s (ip_addr), %s (hw_addr)\n", temp->if_name, 00085 temp->ip_addr, pr_ether(temp->hw_addr)); 00086 if(temp->mac_flag) 00087 fprintf(stdout, "%s: Wireless interface\n", temp->if_name); 00088 else 00089 fprintf(stdout, "%s: Not wireless interface\n", temp->if_name); 00090 if( (temp->link_struct == NULL) && (temp->pcap_descr == NULL) ) 00091 fprintf(stdout, "%s: libnet and libpcap pointers are NULL\n", 00092 temp->if_name); 00093 #endif 00094 00095 printf("---------- FOR INTERFACE %s -----------\n", temp->if_name); 00096 /* Get parameter information from this interface and store it in the 00097 mac_params struct. 00098 */ 00099 if ( (err = mac_get_params(params_ptr, temp, 4, mac_rssi, 00100 mac_txpower, mac_noise, 00101 mac_currentXmitRate)) ) 00102 { 00103 #ifdef DEBUG_ERR 00104 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00105 #endif 00106 return err; 00107 } 00108 00109 // Print out parameter information retrieved from interface. 00110 printf("----------- VALUES AFTER CALL TO GET -----------------\n"); 00111 for (i = START_PARAMS_KEY; i < 4; i++) 00112 printf("Key: %d, Value: %d\n", params_ptr->key[i], params_ptr->value[i]); 00113 // Try to set parameters to the values specified. 00114 if ( (err = mac_set_params(params_ptr, temp, 4, mac_noise, 10, 00115 mac_rssi, 88, mac_txpower, 50, 00116 mac_currentXmitRate, 11000000)) ) 00117 { 00118 #ifdef DEBUG_ERR 00119 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00120 #endif 00121 return err; 00122 } 00123 00124 /* Print out the values to which the parameters have been set. 00125 If the value printed here corresponds to the largest integer 00126 value, it means that the parameter is read-only. If it prints 00127 out the largest negative integer value, it means that the 00128 parameter is unsupported. 00129 */ 00130 printf("----------- VALUES AFTER CALL TO SET -----------------\n"); 00131 for (i = START_PARAMS_KEY; i < 4; i++) 00132 { 00133 printf("Key: %d, Value: %d\n", params_ptr->key[i], 00134 params_ptr->value[i]); 00135 } 00136 00137 /* Call function to append parameter information on all outgoing 00138 frames. The parameters to be appended in this case are 00139 mac_txpower and mac_currentXmitRate. 00140 */ 00141 if ( (err = mac_append_params(temp, OUTGOING, 2, mac_txpower, 00142 mac_currentXmitRate)) ) 00143 { 00144 #ifdef DEBUG_ERR 00145 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00146 #endif 00147 return err; 00148 } 00149 00150 for (i=0;i<2;i++) 00151 { 00152 /* Send frame, of type 0x0900, on specified interface, 00153 to a unicast MAC address. 00154 */ 00155 if ( (err = mac_send_broadcast(payload, PRESET_PAYLOAD_SIZE, 00156 temp)) ) 00157 { 00158 #ifdef DEBUG_ERR 00159 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00160 #endif 00161 return err; 00162 } 00163 } 00164 } 00165 temp = temp->next; 00166 } 00167 #endif 00168 00169 // START: FREE MEMORY ALLOCATED FOR THE LIBRARY STRUCTS. 00170 00171 // Free parameter data structures. 00172 mac_free_params(&params_ptr, &flags_ptr); 00173 // Free interface information collected and return. 00174 mac_free_ifinfo(&ptr); 00175 00176 // END: FREE MEMORY ALLOCATED FOR THE LIBRARY STRUCTS. 00177 00178 return 0; 00179 }


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