Azinix

rlpAct.c

Go to the documentation of this file.
00001 /** \file rlpAct.c
00002 
00003   \brief Routines for manipulating action structures.
00004 
00005 ******************************************************************************/
00006 
00007 #include "rlp.h"
00008 
00009 /**AutomaticStart*************************************************************/
00010 
00011 /**AutomaticEnd***************************************************************/
00012 
00013 
00014 /** \brief  Allocate an acction struct */
00015 
00016 Rlp_Action_t *
00017 Rlp_AllocActionStruct ()
00018 {
00019   Rlp_Action_t *result = (Rlp_Action_t *) malloc (sizeof (Rlp_Action_t));
00020   result->type = Rlp_ActionUnassigned_c;
00021   result->dest = NIL (char);
00022   result->queue = NIL (char);
00023   result->classIndex = 0;
00024 
00025   result->uscript = NIL (char);
00026 
00027   result->ucodeName = NIL (char);
00028   result->ucodeFunction = NULL;
00029   result->ucodeFunctionState = NULL;
00030 
00031   result->argument = NIL (char);
00032 
00033   return result;
00034 }
00035 
00036 
00037 /** \brief  Create an action struct from raw text
00038   
00039   Raw text could be  
00040   <pre>
00041   drop;
00042   queue:foo;  class:1;
00043   dest:0;
00044   tcl-ext:puts "ouch";
00045   </pre>
00046   
00047   */
00048 
00049 Rlp_Action_t *
00050 Rlp_CreateActionFromRawText (char *rawFormula)
00051 {
00052   Rlp_Action_t *result = Rlp_AllocActionStruct ();
00053   char *attribute;
00054   char *value;
00055 
00056   while (*rawFormula == ' ')
00057     {
00058       rawFormula++;
00059     }
00060 
00061   // Special case the case for tcl code so we don't have to worry about
00062   // ;'s in the tcl action string. this precludes us from using tcl
00063   // and non-tcl actions, but that seems like a reasonable restriction
00064 
00065   if (util_strequalN (rawFormula, "tcl-ext:", strlen ("tcl-ext:")) ||
00066       util_strequalN (rawFormula, "c-ext:", strlen ("c-ext:")))
00067     {
00068       char *dupString;
00069       if (util_strequalN (rawFormula, "tcl-ext:", strlen ("tcl-ext:")))
00070     {
00071       result->type = Rlp_ActionUscript_c;
00072       dupString = result->uscript =
00073         strdup (rawFormula + strlen ("tcl-ext:"));
00074     }
00075       else
00076     {
00077       result->type = Rlp_ActionUcode_c;
00078       dupString = result->ucodeName =
00079         strdup (rawFormula + strlen ("c-ext:"));
00080     }
00081 
00082       char *semicolonPtr;
00083       char *colonPtr;
00084       if (!(colonPtr = strchr (dupString, ':')))
00085     {
00086       // there's no argument
00087       // if there is we null terminate
00088       assert (NIL (char) != (semicolonPtr = strchr (dupString, ';')));
00089       *semicolonPtr = '\0';
00090     }
00091       else
00092     {
00093       // there is an argument
00094       // first null terminate the function name
00095       *colonPtr = '\0';
00096       // now get the argument
00097       char *argument = strdup (colonPtr + 1);
00098       // assume ends in a ;
00099       assert (argument[strlen (argument) - 2] == ';');
00100       argument[strlen (argument) - 1] = '\0';
00101       result->argument = argument;
00102     }
00103       return result;
00104     }
00105 
00106 
00107   // the call to Rlp_L7StringParse is overkill
00108   // since we don't have escaped stuff in the
00109   // text, but this should do the job of
00110   // creating the (char * attrib, char *val) pairs
00111   bool haveQueue = false;
00112   bool haveClass = false;
00113   array_t *attributesArray = Rlp_L7StringParse (rawFormula);
00114   int i;
00115   for (i = 0; i < array_n (attributesArray); i++)
00116     {
00117       Rlp_RuleComponent_t *aComponent = array_fetch (Rlp_RuleComponent_t *,
00118                              attributesArray, i);
00119       attribute = aComponent->rawAttribute;
00120       value = aComponent->rawValue;
00121 
00122       if (util_strequal (attribute, "drop"))
00123     {
00124       result->type = Rlp_ActionDrop_c;
00125     }
00126       else if (util_strequal (attribute, "queue"))
00127     {
00128       result->type = Rlp_ActionQueue_c;
00129       result->queue = strdup (value);
00130       haveQueue = true;
00131     }
00132       else if (util_strequal (attribute, "class"))
00133     {
00134       sscanf (value, "%d", &result->classIndex);
00135       haveClass = true;
00136     }
00137       else if (util_strequal (attribute, "route"))
00138     {
00139       result->type = Rlp_ActionRoute_c;
00140       char tmp[100];
00141       sscanf (value, "%s", tmp);
00142       result->dest = strdup (tmp);
00143     }
00144       else if (util_strequal (attribute, "tcl"))
00145     {
00146       printf ("PANIC: shold have picked up tcl earlier in code\n");
00147       assert (0);
00148     }
00149       else
00150     {
00151       printf ("PANIC: Don't support construct %s in action\n", attribute);
00152       assert (0);
00153     }
00154     }
00155   return result;
00156 }
00157 
00158 
00159 /** \brief  Print an action structure */
00160 
00161 void
00162 Rlp_ActionPrint (Rlp_Action_t * action)
00163 {
00164   printf ("drop = %s; dest = %s ; queue = %s; class = %d\n",
00165       (action->type == Rlp_ActionDrop_c) ? "true" : "false", action->dest,
00166       action->queue, action->classIndex);
00167 }