mn_api.c File Reference

#include "config.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include "agentapi.h"
#include "debug.h"
#include "dyn_ip.h"
#include "list.h"
#include "util.h"
#include "mn.h"

Include dependency graph for mn_api.c:

Go to the source code of this file.

Functions

void handle_api (int sock, int admin)
void reply_waiting_api (int code, unsigned char *data, int datalen)
else DEBUG (DEBUG_API,"reply_waiting_api: no waiting call\n")

Variables

mn_data mn
mn_config config
timeval timers [TIMER_COUNT]
char * state_strings [MN_STATE_COUNT]


Function Documentation

else DEBUG ( DEBUG_API  ,
"reply_waiting_api: no waiting call\n"   
)

void handle_api ( int  sock,
int  admin 
)

Definition at line 523 of file mn_api.c.

References check_interfaces(), mn_data::co_addr, DEBUG(), DEBUG_API, DEBUG_INFO, disconnect(), find_agent(), GET_MONCONF, GET_MONCONF_VAR, mn_data::ha_error_count, mn_data::iface, IFNAMSIZ, mn_data::last_reg_send_time, mn_data::local_addr, LOG2, mn, MN_CONNECTED, MN_DISCONNECTED, MN_REQUEST_TUNNEL, request_tunnel(), SET_MONCONF_VAR, mn_data::state, STATE_INIT, and mn_data::tunnel_mode.

Referenced by main_vanha().

00524 {
00525         dyn_api_sockaddr addr;
00526         unsigned int addrlen;
00527         struct api_msg msg;
00528 #ifdef MN_LOCUPD_PROFILER
00529         char *name;
00530         char tmp[30];
00531         struct in_addr faaddr;
00532 #endif
00533 
00534         DEBUG(DEBUG_API, "receive from api\n");
00535         addrlen = sizeof(addr);
00536         memset(&addr, 0, addrlen);
00537         if (api_receive(sock, &addr, &addrlen, &msg) < 0) {
00538                 LOG2(LOG_ERR, "api receive: %s\n", strerror(errno));
00539                 return;
00540         }
00541         if (msg.type != API_CALL_MSG) return;
00542 #ifdef MN_LOCUPD_PROFILER
00543         switch (msg.code) {
00544         case API_FORCE_FA:
00545                 memcpy(&faaddr, msg.params, 4);
00546                 snprintf(tmp, sizeof(tmp), "FORCE %s", inet_ntoa(faaddr));
00547                 name = tmp;
00548                 break;
00549         case API_UPDATE_LOCATION: name = "UPDATE"; break;
00550         case API_UPDATE_LOCATION_BLOCK: name = "UPDATEB"; break;
00551         case API_DISCONNECT: name = "DISCONNECT"; break;
00552         case API_CONNECT: name = "CONNECT"; break;
00553         default:
00554                 snprintf(tmp, sizeof(tmp), "%i", msg.code);
00555                 name = tmp;
00556                 break;
00557         }
00558         gettimeofday(&mn.last_api, NULL);
00559         fprintf(mn.profile, "API %s %u.%06u\n", name,
00560                 (unsigned int) mn.last_api.tv_sec,
00561                 (unsigned int) mn.last_api.tv_usec);
00562 #endif
00563         switch (msg.code) {
00564         case API_CONNECT:
00565                 DEBUG(DEBUG_API, "api connect\n");
00566                 if (!admin) break;
00567                 mn.ha_error_count = 0;
00568                 if (mn.state == MN_DISCONNECTED) {
00569                         /* allow quick locupd */
00570                         timerclear(&mn.last_reg_send_time);
00571                         check_interfaces(mn.iface, MAX_INTERFACES);
00572                         memcpy(&mn.tunnel_mode, msg.params,
00573                                sizeof(mn.tunnel_mode));
00574                         DEBUG(DEBUG_API, "tunnel_mode=%i\n", mn.tunnel_mode);
00575                         if (mn.tunnel_mode == API_TUNNEL_FULL_HA &&
00576                             mn.local_addr.s_addr ==
00577                             config.mn_home_ip_addr.s_addr) {
00578                                 struct in_addr iaddr;
00579                                 char device[IFNAMSIZ + 1];
00580                                 if (dyn_ip_route_get(config.ha_ip_addr, device,
00581                                                      IFNAMSIZ) == 0 &&
00582                                     dyn_ip_get_ifaddr(device, &iaddr) == 0 &&
00583                                     mn.local_addr.s_addr != iaddr.s_addr) {
00584                                         DEBUG(DEBUG_INFO,
00585                                               "Address changed to %s\n",
00586                                               inet_ntoa(iaddr));
00587                                         mn.local_addr.s_addr = iaddr.s_addr;
00588                                 }
00589                         }
00590                         find_agent(STATE_INIT);
00591                         memcpy(&api_addr, &addr, sizeof(addr));
00592                         api_addr_len = addrlen;
00593                         /* this API call is replied with reply_waiting_api
00594                          * after the status of the connection attempt is known
00595                          */
00596                 } else {
00597                         api_send_reply(sock, &addr, addrlen, API_NOT_PERMITTED,
00598                                        NULL, 0);
00599                 }
00600                 break;
00601         case API_UPDATE_LOCATION:
00602                 DEBUG(DEBUG_API, "api update location\n");
00603                 if (!admin) break;
00604                 check_interfaces(mn.iface, MAX_INTERFACES);
00605                 handle_api_locupd(sock, &addr, addrlen, &msg, 0);
00606                 break;
00607         case API_UPDATE_LOCATION_BLOCK:
00608                 DEBUG(DEBUG_API, "api update location (block)\n");
00609                 if (!admin) break;
00610                 check_interfaces(mn.iface, MAX_INTERFACES);
00611                 handle_api_locupd(sock, &addr, addrlen, &msg, 1);
00612                 memcpy(&api_addr, &addr, sizeof(addr));
00613                 api_addr_len = addrlen;
00614                 /* this API call is replied with reply_waiting_api
00615                  * after the status of the connection attempt is known */
00616                 break;
00617         case API_CANCEL:
00618                 DEBUG(DEBUG_API, "Cancel received.\n");
00619                 if (!admin) break;
00620 
00621                 api_send_reply(sock, &addr, addrlen, API_SUCCESS, NULL, 0);
00622                 break;
00623         case API_DISCONNECT:
00624                 DEBUG(DEBUG_API, "api disconnect\n");
00625                 if (!admin) break;
00626                 check_interfaces(mn.iface, MAX_INTERFACES);
00627                 disconnect();
00628                 api_send_reply(sock, &addr, addrlen, API_SUCCESS, NULL, 0);
00629                 break;
00630         case API_GET_CAREOF_ADDR:
00631                 api_send_reply(sock, &addr, addrlen, API_SUCCESS,
00632                                (unsigned char *) &mn.co_addr,
00633                                sizeof(mn.co_addr));
00634                 break;
00635         case API_GET_TUNNELING_MODE:
00636                 api_send_reply(sock, &addr, addrlen, API_SUCCESS,
00637                                (unsigned char *) &mn.tunnel_mode, sizeof(int));
00638                 break;
00639         case API_GET_STATUS:
00640                 api_send_reply(sock, &addr, addrlen, API_SUCCESS,
00641                                (unsigned char *) get_mobile_status(),
00642                                sizeof(struct dynamics_mobile_status));
00643                 break;
00644         case API_CONFIRM:
00645                 DEBUG(DEBUG_API, "api confirm\n");
00646                 if (!admin) break;
00647                 if (mn.state != MN_CONNECTED &&
00648                     mn.state != MN_REQUEST_TUNNEL)  {
00649                         api_send_reply(sock, &addr, addrlen,
00650                                        API_NOT_PERMITTED, NULL, 0);
00651                         break;
00652 
00653                 }
00654                 memcpy(&api_addr, &addr, sizeof(addr));
00655                 api_addr_len = addrlen;
00656                 request_tunnel(STATE_INIT, 1, 0);
00657                 break;
00658         case API_FORCE_FA:
00659                 DEBUG(DEBUG_API, "api force fa\n");
00660                 if (!admin) break;
00661                 handle_api_force_fa(sock, &addr, addrlen, &msg);
00662                 break;
00663         case API_GET_FA_LIST:
00664                 DEBUG(DEBUG_API, "api get fa list\n");
00665                 handle_api_get_fa_list(sock, &addr, addrlen, &msg);
00666                 break;
00667         case API_GET_FA_INFO:
00668                 DEBUG(DEBUG_API, "api get fa info\n");
00669                 handle_api_get_fa_info(sock, &addr, addrlen, &msg);
00670                 break;
00671 #ifdef WITH_WIRELESS
00672         case API_IW_GET_CH:
00673                 DEBUG(DEBUG_API, "api get wireless channel info\n");
00674                 handle_api_get_iw_ch(sock, &addr, addrlen, &msg);
00675                 break;
00676         case API_IW_SET_CH:
00677                 DEBUG(DEBUG_API, "api set wireless channel info\n");
00678                 if (!admin) break;
00679                 handle_api_set_iw_ch(sock, &addr, addrlen, &msg);
00680                 break;
00681 #endif
00682         case API_RESCAN:
00683                 DEBUG(DEBUG_API, "api rescan interfaces\n");
00684                 handle_api_rescan(sock, &addr, addrlen, &msg);
00685                 break;
00686         case API_REGISTER_DEV_INFO_SOCKET:
00687                 DEBUG(DEBUG_API, "api register dev info socket\n");
00688                 if (!admin) break;
00689                 handle_register_dev_info_socket(sock, &addr, addrlen, &msg);
00690                 break;
00691         case API_POLICY_OFF:
00692                 DEBUG(DEBUG_API, "api policy off\n");
00693                 if (!admin) break;
00694                 handle_policy(sock, &addr, addrlen, &msg, 0);
00695                 break;
00696         case API_POLICY_ON:
00697                 DEBUG(DEBUG_API, "api policy on\n");
00698                 if (!admin) break;
00699                 handle_policy(sock, &addr, addrlen, &msg, 1);
00700                 break;
00701         case API_GET_POLICY:
00702                 DEBUG(DEBUG_API, "api get policy\n");
00703                 handle_get_policy(sock, &addr, addrlen, &msg);
00704                 break;
00705 #ifdef WITH_WIRELESS
00706         case API_GET_MON_CONF:
00707                 DEBUG(DEBUG_API, "api get mon conf\n");
00708                 handle_mon_conf(sock, &addr, addrlen, &msg, GET_MONCONF);
00709                 break;
00710         case API_GET_MON_CONF_VAR:
00711                 DEBUG(DEBUG_API, "api get mon conf var\n");
00712                 handle_mon_conf(sock, &addr, addrlen, &msg, GET_MONCONF_VAR);
00713                 break;
00714         case API_SET_MON_CONF_VAR:
00715                 DEBUG(DEBUG_API, "api set mon conf\n");
00716                 if (!admin) break;
00717                 handle_mon_conf(sock, &addr, addrlen, &msg, SET_MONCONF_VAR);
00718                 break;
00719 #endif
00720         default:
00721                 /* Just send a message indicating that the function is not
00722                  * supported. No need to confirm that message gets through. */
00723                 DEBUG(DEBUG_API, "api call not supported\n");
00724                 api_send_reply(sock, &addr, addrlen, API_NOT_SUPPORTED, NULL,
00725                                0);
00726                 break;
00727         }
00728 }

Here is the call graph for this function:

void reply_waiting_api ( int  code,
unsigned char *  data,
int  datalen 
)

Definition at line 736 of file mn_api.c.

References mn_data::api_rw_socket, DEBUG(), DEBUG_API, and mn.

Referenced by clean_up(), close_tunneling(), and request_tunnel().

00737 {
00738 #ifdef DYN_TARGET_LINUX
00739         if (api_addr_len > 0) {
00740 #endif
00741 #ifdef DYN_TARGET_WINDOWS
00742         if (api_addr.sin_port != 0) {
00743 #endif
00744                 DEBUG(DEBUG_API, "Replying to waiting API call\n");
00745                 api_send_reply(mn.api_rw_socket, &api_addr, api_addr_len, code,
00746                                data, datalen);
00747                 memset(&api_addr, 0, sizeof(api_addr));
00748                 api_addr_len = 0;
00749         } else DEBUG(DEBUG_API, "reply_waiting_api: no waiting call\n");

Here is the call graph for this function:


Variable Documentation

struct mn_config config

Definition at line 45 of file mn.c.

struct mn_data mn

Definition at line 44 of file mn.c.

char* state_strings[MN_STATE_COUNT]

Initial value:

 {
        "Startup", "Disconnected", "Find Agent", "Passive Find",
        "Request Tunnel", "Connected",
        "Close For Home", "At Home", "Error", "Stop" }

Definition at line 50 of file mn_api.c.

struct timeval timers[TIMER_COUNT]

Definition at line 47 of file mn.c.


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