cpd_kcom.c

Go to the documentation of this file.
00001 /*
00002  * $Id: cpd_kcom.c 346460 2009-11-14 05:06:47Z ssiano $
00003  *
00004  * This code is provided as is by Juniper Networks SDK Developer Support.
00005  * It is provided with no warranties or guarantees, and Juniper Networks
00006  * will not provide support or maintenance of this code in any fashion.
00007  * The code is provided only to help a developer better understand how
00008  * the SDK can be used.
00009  *
00010  * Copyright (c) 2009, Juniper Networks, Inc.
00011  * All rights reserved.
00012  */
00013 
00022 #include <jnx/aux_types.h>
00023 #include <netinet/in.h>
00024 #include <arpa/inet.h>
00025 #include <isc/eventlib.h>
00026 #include <jnx/junos_kcom.h>
00027 #include "cpd_kcom.h"
00028 #include "cpd_config.h"
00029 #include "cpd_http.h"
00030 #include "cpd_logging.h"
00031 
00032 /*** Constants ***/
00033 
00034 #define KCOM_ID_CPD             77  
00035 
00036 /*** Data Structures ***/
00037 
00038 
00039 /*** STATIC/INTERNAL Functions ***/
00040 
00041 
00042 
00054 static int
00055 ifa_async_handler(kcom_ifa_t * msg, void * user_info UNUSED)
00056 {    
00057     static in_addr_t last_address = 0; 
00058     
00059     int i;
00060     in_addr_t address;
00061     u_int8_t * cp, * prefix;
00062     
00063     if(get_cpd_address() == 0) {
00064         return 0; // We haven't yet received the server address
00065     }
00066 
00067     // convert address to in_addr_t
00068     if(msg->ifa_lplen > IN_HOST_PLEN) {
00069         LOG(LOG_ERR, "%s: Incorrect inet prefix length %d", __func__,
00070                 msg->ifa_lplen);
00071     }
00072     
00073     address = 0;
00074     cp = (u_int8_t *) &address;
00075     prefix = (u_int8_t *)msg->ifa_lprefix;
00076     for (i = 0;  i < (int)sizeof(in_addr_t); ++i)
00077         *cp++ = *prefix++;
00078     
00079     if(address == get_cpd_address()) {
00080         
00081         if((msg->ifa_op == KCOM_ADD || msg->ifa_op == KCOM_GET) &&
00082                 address != last_address) { // check for an address change
00083             
00084             last_address = address;
00085             
00086             // it is a valid address
00087             LOG(LOG_INFO, "%s: Starting server because of address addition",
00088                     __func__);
00089             init_http_server(); // The CPD's HTTP server address has been added
00090             
00091             if(msg->ifa_op == KCOM_GET) {
00092                 junos_kcom_msg_free(msg);
00093                 return KCOM_ITER_END;
00094             } else {
00095                 junos_kcom_msg_free(msg);
00096                 return 0;
00097             }
00098             
00099         } else if(msg->ifa_op == KCOM_DELETE) {
00100             // it is now an invalid address
00101             LOG(LOG_INFO, "%s: Stopping server because of server address delete",
00102                     __func__);
00103             shutdown_http_server();
00104             junos_kcom_msg_free(msg);
00105             return 0;
00106         }
00107     } else {
00108         struct in_addr in;
00109         in.s_addr = address;
00110         LOG(LOG_DEBUG, "%s: Notified of PIC address %s with op %d",
00111                 __func__, inet_ntoa(in), msg->ifa_op);
00112     }
00113             
00114     junos_kcom_msg_free(msg);
00115 
00116     return 0;
00117 }
00118 
00119 
00131 static int
00132 ifl_iterator(kcom_ifl_t * msg, void * user_info UNUSED)
00133 {
00134     int status;
00135 
00136     status = junos_kcom_ifa_get_all(ifa_async_handler, msg->ifl_index,
00137                 AF_INET, NULL);
00138 
00139     if(!status) {
00140         LOG(LOG_INFO, "%s: Could not find a matching address configured for"
00141                " IFL %s", __func__, msg->ifl_name); // may not yet be configured
00142     } else if (status && status != KCOM_ITER_END) {
00143         // Something went wrong
00144         LOG(LOG_ERR, "%s: Error iterating over the IFAs (%d) for IFL %s",
00145                 __func__, status, msg->ifl_name);
00146     } // else status == KCOM_ITER_END so it was found
00147             
00148     junos_kcom_msg_free(msg);
00149 
00150     return 0;
00151 }
00152 
00153 
00154 
00155 /*** GLOBAL/EXTERNAL Functions ***/
00156 
00157 
00161 void
00162 check_address_status(void)
00163 {
00164     junos_kcom_ifl_get_all(ifl_iterator, NULL, NULL);
00165 }
00166 
00167 
00175 int
00176 cpd_kcom_init(evContext ctx)
00177 {
00178     int status;
00179 
00180     // We cannot get the origin ID on the PIC to feed to kcom init
00181     status = junos_kcom_init(KCOM_ID_CPD, ctx); // any number will do
00182     INSIST(status == KCOM_OK);
00183 
00184     if((status = junos_kcom_register_ifa_handler(NULL, ifa_async_handler))) {
00185         LOG(LOG_ERR, "%s: Register ifa handler FAILED!", __func__);
00186     }
00187     
00188     return status;
00189 }

2007-2009 Juniper Networks, Inc. All rights reserved. The information contained herein is confidential information of Juniper Networks, Inc., and may not be used, disclosed, distributed, modified, or copied without the prior written consent of Juniper Networks, Inc. in an express license. This information is subject to change by Juniper Networks, Inc. Juniper Networks, the Juniper Networks logo, and JUNOS are registered trademarks of Juniper Networks, Inc. in the United States and other countries. All other trademarks, service marks, registered trademarks, or registered service marks are the property of their respective owners.
Generated on Sun May 30 20:26:53 2010 for SDK Your Net Corporation Policy Manager Example: Captive Portal Daemon (cpd) 1.0 by Doxygen 1.5.1