counterd_kcom.c

Go to the documentation of this file.
00001 /*
00002  * $Id: counterd_kcom.c 366303 2010-03-05 13:14:01Z taoliu $
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) 2007-2008, Juniper Networks, Inc.
00011  * All rights reserved.
00012  */
00013 
00022 #include <string.h>
00023 #include <isc/eventlib.h>
00024 #include <jnx/bits.h>
00025 #include <jnx/aux_types.h>
00026 #include <jnx/provider_info.h>
00027 #include <jnx/junos_kcom.h>
00028 #include "counterd_kcom.h"
00029 #include "counterd_logging.h"
00030 
00031 
00032 /*** Constants ***/
00033 
00034 
00035 #define COUNTERD_GENCFG_KEY 0xc01dface   
00036 #define COUNTERD_MINOR_NUM           2   
00037 
00038 
00039 /*** Data Structures ***/
00040 
00041 
00042 /*** STATIC/INTERNAL Functions ***/
00043 
00044 
00055 static int
00056 counterd_gencfg_async(junos_kcom_gencfg_t * jk_gencfg)
00057 {
00058     ERRMSG(COUNTERD, TRACE_LOG_DEBUG,
00059         "%s - Called and ignored.", __func__);
00060 
00061     jk_gencfg->opcode = JUNOS_KCOM_GENCFG_OPCODE_GET_FREE;
00062     jk_gencfg->get_p = NULL;
00063     junos_kcom_gencfg(jk_gencfg);
00064     
00065     junos_kcom_msg_free(jk_gencfg);
00066     
00067     return KCOM_OK;
00068 }
00069 
00070 
00071 /*** GLOBAL/EXTERNAL Functions ***/
00072 
00073 
00083 int
00084 counterd_kcom_init(evContext ctx)
00085 {
00086     int rc;
00087     junos_kcom_gencfg_t jk_gencfg;
00088     provider_origin_id_t origin_id;
00089 
00090     rc = provider_info_get_origin_id(&origin_id);
00091     
00092     if (rc) {
00093         ERRMSG(COUNTERD, TRACE_LOG_ERR,
00094             "%s: Retrieving origin ID failed: %m", __func__);
00095         return rc;
00096     }
00097     
00098     /*
00099      * Some KCOM required initialization
00100      */
00101     rc = junos_kcom_init(origin_id, ctx);
00102     
00103     if(rc) {
00104         ERRMSG(COUNTERD, TRACE_LOG_ERR,
00105             "%s - Failed to initialize KCOM", __func__);
00106         return rc;
00107     }
00108 
00109     /*
00110      * Some GENCFG initialization
00111      */
00112     bzero(&jk_gencfg, sizeof(junos_kcom_gencfg_t));
00113     jk_gencfg.opcode        = JUNOS_KCOM_GENCFG_OPCODE_INIT;
00114     jk_gencfg.user_info_p   = NULL;
00115     jk_gencfg.user_handler  = counterd_gencfg_async;
00116 
00117     rc = junos_kcom_gencfg(&jk_gencfg);
00118     
00119     if(rc) {
00120         ERRMSG(COUNTERD, TRACE_LOG_ERR,
00121             "%s - Failed to initialize KCOM GENCFG", __func__);
00122         return rc;
00123     }
00124 
00125     /*
00126      * Add a filter to only be notified about our blobs we add
00127      * (cut down the notifications)
00128      */
00129 
00130     bzero(&jk_gencfg, sizeof(junos_kcom_gencfg_t));
00131     jk_gencfg.opcode        = JUNOS_KCOM_GENCFG_OPCODE_FILTER_ADD;
00132     jk_gencfg.blob_id       = COUNTERD_MINOR_NUM;
00133     
00134     rc = junos_kcom_gencfg(&jk_gencfg);
00135     
00136     if(rc) {
00137         ERRMSG(COUNTERD, TRACE_LOG_ERR,
00138             "%s - Failed to add KCOM GENCFG filter", __func__);
00139         return rc;    
00140     }
00141 
00142     return KCOM_OK;
00143 }
00144 
00145 
00158 int
00159 counterd_add_data(void * data, uint32_t data_len)
00160 {
00161     junos_kcom_gencfg_t jk_gencfg;
00162     uint32_t user_key = COUNTERD_GENCFG_KEY; // key for data
00163     int rc;
00164 
00165     INSIST(data != NULL);
00166     INSIST(data_len > 0);
00167     
00168     /*
00169      * Get a blob of data
00170      */
00171     bzero(&jk_gencfg, sizeof(junos_kcom_gencfg_t));
00172     jk_gencfg.opcode        = JUNOS_KCOM_GENCFG_OPCODE_BLOB_GET;
00173     jk_gencfg.blob_id       = COUNTERD_MINOR_NUM;
00174     jk_gencfg.key.size      = sizeof(uint32_t);
00175     jk_gencfg.key.data_p    = (void *) &user_key;
00176     
00177     rc = junos_kcom_gencfg(&jk_gencfg);
00178     
00179     if(!rc) {
00180 
00181         jk_gencfg.opcode = JUNOS_KCOM_GENCFG_OPCODE_GET_FREE;
00182         rc = junos_kcom_gencfg(&jk_gencfg);
00183         
00184         if(rc) {
00185             ERRMSG(COUNTERD, TRACE_LOG_ERR,
00186                 "%s: Failed to free data from KCOM GENCFG blob get",
00187                 __func__);
00188             return rc;
00189         }
00190 
00191         /*
00192          * It already exists so delete it first
00193          */
00194         bzero(&jk_gencfg, sizeof(junos_kcom_gencfg_t));
00195         jk_gencfg.opcode        = JUNOS_KCOM_GENCFG_OPCODE_BLOB_DEL;
00196         jk_gencfg.blob_id       = COUNTERD_MINOR_NUM;
00197         jk_gencfg.key.size      = sizeof(uint32_t);
00198         jk_gencfg.key.data_p    = (void *) &user_key;
00199         
00200         rc = junos_kcom_gencfg(&jk_gencfg);
00201         
00202         if(rc) {
00203             ERRMSG(COUNTERD, TRACE_LOG_ERR,
00204                 "%s: Failed to delete existing blob while replacing data",
00205                 __func__);
00206             return rc;
00207         }
00208     }
00209 
00210     /*
00211      * Add a blob with data
00212      */
00213     bzero(&jk_gencfg, sizeof(junos_kcom_gencfg_t));
00214     jk_gencfg.opcode        = JUNOS_KCOM_GENCFG_OPCODE_BLOB_ADD;
00215     jk_gencfg.blob_id       = COUNTERD_MINOR_NUM;
00216     jk_gencfg.key.size      = sizeof(uint32_t);
00217     jk_gencfg.key.data_p    = (void *) &user_key;
00218     jk_gencfg.blob.size     = data_len;
00219     jk_gencfg.blob.data_p   = data;
00220     
00221     rc = junos_kcom_gencfg(&jk_gencfg);
00222     
00223     if(rc) {
00224         ERRMSG(COUNTERD, TRACE_LOG_ERR,
00225             "%s: Failed to add new blob", __func__);
00226     }
00227     
00228     return rc;
00229 }
00230 
00231 
00244 int
00245 counterd_get_data(void * data, uint32_t data_len)
00246 {
00247 
00248     junos_kcom_gencfg_t jk_gencfg;
00249     uint32_t user_key = COUNTERD_GENCFG_KEY;
00250     int rc;
00251 
00252     INSIST(data != NULL);
00253     INSIST(data_len > 0);
00254     
00255     /*
00256      * Get a blob of data
00257      */
00258     bzero(&jk_gencfg, sizeof(junos_kcom_gencfg_t));
00259     jk_gencfg.opcode        = JUNOS_KCOM_GENCFG_OPCODE_BLOB_GET;
00260     jk_gencfg.blob_id       = COUNTERD_MINOR_NUM;
00261     jk_gencfg.key.size      = sizeof(uint32_t);
00262     jk_gencfg.key.data_p    = (void *) &user_key;
00263     
00264     rc = junos_kcom_gencfg(&jk_gencfg);
00265     
00266     if (rc) {
00267         ERRMSG(COUNTERD, TRACE_LOG_DEBUG,
00268             "%s: Call to get data from KCOM returned nothing", __func__);
00269         return -1;
00270     } else {
00271         jk_gencfg.opcode = JUNOS_KCOM_GENCFG_OPCODE_GET_FREE;
00272         rc = junos_kcom_gencfg(&jk_gencfg);
00273         
00274         if(rc) {
00275             ERRMSG(COUNTERD, TRACE_LOG_ERR,
00276                 "%s: Failed to free data from KCOM GENCFG blob get",
00277                 __func__);
00278             return rc;
00279         }
00280     }
00281     
00282     /*
00283      * Copy retrieved data
00284      */
00285     if(jk_gencfg.blob.size > data_len) {
00286         memcpy(data, jk_gencfg.blob.data_p, data_len);
00287         return data_len;
00288     }
00289     
00290     if(jk_gencfg.blob.size > 0) {
00291         memcpy(data, jk_gencfg.blob.data_p, jk_gencfg.blob.size);
00292         return jk_gencfg.blob.size;
00293     }
00294     
00295     return 0;
00296 }
00297 

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 Counter Example 1.0 by Doxygen 1.5.1