monitor_api.c

Go to the documentation of this file.
00001 /* $Id: monitor_api.c,v 1.16 2001/09/29 16:06:39 jm Exp $
00002  * API calls for Monitor module
00003  *
00004  * Dynamic hierarchial IP tunnel
00005  * Copyright (C) 1998-2000, Dynamics group
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License version 2 as
00009  * published by the Free Software Foundation. See README and COPYING for
00010  * more details.
00011  */
00012 
00013 #include <string.h>
00014 #include "monitor.h"
00015 #include "agentapi.h"
00016 #include "dyn_api.h"
00017 #include "dyn_wireless.h"
00018 #include "debug.h"
00019 #include "util.h"
00020 #include "mn.h" /* MAX_INTERFACES */
00021 
00022 #include "owntypes.h"
00023 
00024 #define DEBUG_FLAG 'm' /* same as in monitor.c */
00025 
00026 extern struct monitor_global_data mon_conf;
00027 extern struct monitor_interface mon_devs[MAX_INTERFACES];
00028 extern struct monitor_conf_vars monitor_conf[];
00029 
00030 
00031 int monitor_api_get_ch(unsigned char *buffer, __u16 *length)
00032 {
00033         int ch, len, count = 0, i;
00034         struct dynamics_mn_iw_ch_info *info 
00035                 = (struct dynamics_mn_iw_ch_info *)buffer;
00036 
00037         len = sizeof(struct dynamics_mn_iw_ch_info);
00038         for (i = 0; i < MAX_INTERFACES; i++) {
00039                 if (!mon_devs[i].in_use)
00040                         continue;
00041 
00042                 /* get channel */
00043                 ch = dyn_wireless_get_channel(mon_devs[i].sock, 
00044                                               mon_devs[i].ifname);
00045                 if (ch < 0) {
00046                         DEBUG(DEBUG_FLAG, "monitor_api_get_ch: channel "
00047                               "< 0 (%s)\n", mon_devs[i].ifname);
00048                         continue;
00049                 }
00050                 mon_devs[i].channel = ch; /* if changed */
00051 
00052                 /* copy to caller */
00053                 if (*length+len > API_DATA_SIZE) {
00054                         DEBUG(DEBUG_FLAG, "monitor_api_get_ch: Not enough "
00055                               "room for interface name (%d, %d)\n", *length,
00056                               *length+len);
00057                         return 1;
00058                 }
00059 
00060                 memcpy(info[count].ifname, mon_devs[i].ifname, IFNAMSIZ);
00061                 info[count].channel = mon_devs[i].channel;
00062 
00063                 /* get AP hw addr */
00064                 if (dyn_wireless_get_ap_address(mon_devs[i].sock,
00065                                                 mon_devs[i].ifname,
00066                                                 info[count].ap_hwaddr) < 0) {
00067                         DEBUG(DEBUG_FLAG, "monitor_api_get_ch: get AP hw "
00068                               "addr failed\n");
00069                         continue;
00070                 }
00071 
00072                 *length += len;
00073                 DEBUG(DEBUG_FLAG, "monitor_api_get_ch: %s: %d - %s\n",
00074                       info[count].ifname, info[count].channel,
00075                       ether_hwtoa(info[count].ap_hwaddr));
00076         }
00077 
00078         return 0;
00079 }
00080 
00081 
00082 int monitor_api_set_ch(char *ifname, int channel)
00083 {
00084         int r, i;
00085 
00086         for (i = 0; i < MAX_INTERFACES; i++) {
00087                 if (!mon_devs[i].in_use || 
00088                     memcmp(ifname, mon_devs[i].ifname, strlen(ifname)))
00089                         continue;
00090                 /* set channel */
00091                 r = dyn_wireless_set_channel(mon_devs[i].sock, 
00092                                              mon_devs[i].ifname, 
00093                                              channel);
00094                 return r;
00095         }
00096 
00097         DEBUG(DEBUG_FLAG, "monitor_set_ch: not wireless interface (%s)\n",
00098               ifname);
00099         return -1;
00100 }
00101 
00102 
00103 static char *itoa(int i)
00104 {
00105         static char str[20];
00106         snprintf(str, 19, "%d", i);
00107         return str;
00108 }
00109 
00110 
00111 static int get_mon_conf(char *buffer, int len)
00112 {
00113         int curr = 0, r = 0, i;
00114         struct monitor_conf_vars *t = monitor_conf;
00115         
00116         for (i = 0, r = 0; t[i].value != NULL && r == 0; i++)
00117                 r = copy_str(buffer, len, &curr, 
00118                              t[i].name, itoa(*(t[i].value)));
00119 
00120         if (r) {
00121                 DEBUG(DEBUG_FLAG, "get_mon_conf: insufficient space\n");
00122                 return API_INSUFFICIENT_SPACE;
00123         }
00124 
00125         DEBUG(DEBUG_FLAG, buffer);
00126         return API_SUCCESS;
00127 }
00128 
00129 
00130 static int get_mon_conf_var(char *buffer, int length)
00131 {
00132         int curr = 0, r, i = 0, len;
00133         struct dynamics_mn_mon_conf_var *reply = 
00134                 (struct dynamics_mn_mon_conf_var *)buffer;
00135         struct monitor_conf_vars *t = monitor_conf;
00136 
00137 
00138         len = strlen(reply->name) < MAX_MON_CONF_VAR_LEN ? 
00139                 strlen(reply->name) : MAX_MON_CONF_VAR_LEN;
00140         while (t[i].value != NULL) {
00141                 r = strncasecmp(reply->name, t[i].name, len);
00142                 if (t[i].min_chars > len && r == 0)
00143                         return API_ILLEGAL_PARAMETERS;
00144                 if (r == 0)
00145                         break; /* found variable */
00146                 i++;
00147         }
00148 
00149         if (t[i].value == NULL) {
00150                 DEBUG(DEBUG_FLAG, "mon_conf_get_var: unknown variable\n");
00151                 return API_NOT_AVAILABLE;
00152         }
00153 
00154         r = copy_str(buffer, length, &curr, t[i].name, itoa(*(t[i].value)));
00155         if (r) {
00156                 DEBUG(DEBUG_FLAG, "mon_conf_get_var: insufficient space\n");
00157                 return API_INSUFFICIENT_SPACE;
00158         }
00159 
00160         DEBUG(DEBUG_FLAG, buffer);
00161         return API_SUCCESS;
00162 }
00163 
00164 
00165 static int set_mon_conf_var(char *buffer, int length)
00166 {
00167         int i = 0, len = 0, r;
00168         struct dynamics_mn_mon_conf_var *reply = 
00169                 (struct dynamics_mn_mon_conf_var *)buffer;
00170         struct monitor_conf_vars *t = monitor_conf;
00171 
00172         len = strlen(reply->name) < MAX_MON_CONF_VAR_LEN ? 
00173                 strlen(reply->name) : MAX_MON_CONF_VAR_LEN;
00174         DEBUG(DEBUG_FLAG, "set_mon_conf_var: '%s', len %d\n",
00175               reply->name, len);
00176         while (t[i].value != NULL) {
00177                 r = strncasecmp(reply->name, t[i].name, len);
00178                 if (t[i].min_chars > len && r == 0)
00179                         return API_ILLEGAL_PARAMETERS;
00180                 if (r == 0) 
00181                         break; /* found variable */
00182                 i++;
00183         }
00184 
00185         if (t[i].value == NULL) {
00186                 DEBUG(DEBUG_FLAG, "set_mon_conf_var: unknown variable\n");
00187                 return API_NOT_AVAILABLE;
00188         }
00189 
00190         if ((reply->value >= t[i].min || t[i].min == -1) && 
00191             (reply->value <= t[i].max || t[i].max == -1)) {
00192                 *(t[i].value) = reply->value;
00193                 return API_SUCCESS;
00194         }
00195         
00196         DEBUG(DEBUG_FLAG, "set_mon_conf_var: illegal value for "
00197               "var %s\n (%d - %d)", reply->name, t[i].min, t[i].max);
00198         return API_ILLEGAL_PARAMETERS;
00199 }
00200 
00201 
00202 int monitor_api_mon_conf(char *buffer, int len, int type)
00203 {
00204         switch (type) {
00205         case GET_MONCONF:
00206                 DEBUG(DEBUG_FLAG, "monitor_api_mon_conf: get_mon_conf\n");
00207                 return get_mon_conf(buffer, len);
00208         case GET_MONCONF_VAR:
00209                 DEBUG(DEBUG_FLAG, "monitor_api_mon_conf: get_mon_conf_var\n");
00210                 return get_mon_conf_var(buffer, len);
00211         case SET_MONCONF_VAR:
00212                 DEBUG(DEBUG_FLAG, "monitor_api_mon_conf: set_mon_conf_var\n");
00213                 return set_mon_conf_var(buffer, len);
00214         default:
00215                 DEBUG(DEBUG_FLAG, "monitor_api_mon_conf: unknown type\n");
00216                 return API_UNDEFINED;
00217         }
00218 
00219         /* never reached */
00220         return 0;
00221 }

Generated on Tue Jan 15 08:50:43 2008 for Virtual foreign agent generator version 0.1 by  doxygen 1.5.1