modules/ud/ud_misc.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. attribute_free
  2. attribute_upd
  3. attribute_new1
  4. attribute_new
  5. object_free
  6. object_new
  7. transaction_free
  8. transaction_new
  9. UD_ack

   1 /***************************************
   2   $Revision: 1.17 $
   3 
   4   Miscellaneous functions to support UD
   5 
   6   Status: NOT REVUED, NOT TESTED
   7 
   8  Author(s):       Chris Ottrey, Andrei Robachevsky
   9 
  10   ******************/ /******************
  11   Modification History:
  12         andrei (17/01/2000) Created.
  13   ******************/ /******************
  14   Copyright (c) 2000,2001                         RIPE NCC
  15  
  16   All Rights Reserved
  17   
  18   Permission to use, copy, modify, and distribute this software and its
  19   documentation for any purpose and without fee is hereby granted,
  20   provided that the above copyright notice appear in all copies and that
  21   both that copyright notice and this permission notice appear in
  22   supporting documentation, and that the name of the author not be
  23   used in advertising or publicity pertaining to distribution of the
  24   software without specific, written prior permission.
  25   
  26   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  27   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  28   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  29   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  30   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  31   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32  ***************************************/
  33 
  34 #include "rip.h"
  35 
  36 /************************************************************
  37 * void attribute_free()                                     *
  38 *                                                           *
  39 * Frees memory allocated for an attribute                   *
  40 *                                                           *
  41 ************************************************************/
  42 void attribute_free(void *data, void *ptr)
     /* [<][>][^][v][top][bottom][index][help] */
  43 {
  44 Attribute_t *attr = data;
  45 
  46         UT_free(attr->value);
  47         UT_free(attr);
  48 }
  49 
  50 /************************************************************
  51 * Attribute_t *attribute_upd()                              *
  52 *                                                           *
  53 * Changes the value of an attribute                         *
  54 *                                                           *
  55 ************************************************************/
  56 Attribute_t *attribute_upd(Attribute_t *attr, int newtype, char *newvalue)
     /* [<][>][^][v][top][bottom][index][help] */
  57 {
  58  attr->type=newtype;
  59  UT_free(attr->value);
  60  attr->value=g_strdup(newvalue);
  61  if(!attr->value)die;
  62  return(attr);
  63 }
  64 
  65 /************************************************************
  66 * Attribute_t *attribute_new1()                             *
  67 *                                                           *
  68 * Creates an attribute and fills the type and value         *
  69 *                                                           *
  70 ************************************************************/
  71 Attribute_t *attribute_new1(int type, const char *value) 
     /* [<][>][^][v][top][bottom][index][help] */
  72 {
  73 char *n;
  74 Attribute_t *attr = NULL;      
  75 
  76       attr = (Attribute_t *)UT_calloc(1, sizeof(Attribute_t)+1);
  77       attr->type = type;
  78 
  79       /* check for end-of-line comments */
  80       n = index(value, '#');
  81       /* if there is no comment check for trailing \n */
  82       if(n == NULL) n = index(value, '\n');
  83       /* now copy the clean value into the attribute */
  84       if(n == NULL) attr->value = g_strdup(value); 
  85       else  attr->value = g_strndup(value, (n - value));
  86       /* Strip the whitespace */
  87       g_strstrip(attr->value);
  88       return(attr);
  89 
  90 }
  91 /************************************************************
  92 * Attribute_t *attribute_new()                              *
  93 *                                                           *
  94 * Determines the type and value of an attribute and         *
  95 * creates it by calling attribute_new1()                    *
  96 *                                                           *
  97 ************************************************************/
  98 Attribute_t *attribute_new(const char *line) {
     /* [<][>][^][v][top][bottom][index][help] */
  99   Attribute_t *attr = NULL;
 100   int type;
 101   char *colon;
 102   gchar *token;
 103 
 104   
 105   colon = index(line, ':'); 
 106   if (colon != NULL) {
 107     if (line[0] =='*') {
 108       token = g_strndup(line+1, 2);
 109       type = DF_attribute_code2type(token);
 110     }
 111     else {
 112       token = g_strndup(line, (colon - line));
 113       type = DF_attribute_name2type(token);
 114     }
 115     if(token)UT_free(token);
 116 
 117     colon+=2;
 118     if (type >= 0) {
 119         attr=attribute_new1(type, colon);
 120     }
 121   }
 122   return attr;
 123 } /* attribute_new() */
 124 
 125 /************************************************************
 126 * void object_free()                                        *
 127 *                                                           *
 128 * Frees memory allocated for an object                      *
 129 *                                                           *
 130 ************************************************************/
 131 void object_free(Object_t *obj) {
     /* [<][>][^][v][top][bottom][index][help] */
 132   if (obj) {
 133    g_slist_foreach(obj->attributes, attribute_free, NULL);
 134    g_slist_free(obj->attributes);
 135    g_string_free(obj->object, TRUE);
 136    UT_free(obj);
 137   }
 138 } /* object_free() */
 139 
 140 /************************************************************
 141 * Object_t *object_new()                                    *
 142 *                                                           *
 143 * Determines the class type and creates an object           *
 144 *                                                           *
 145 ************************************************************/
 146 Object_t *object_new(const char *line) {
     /* [<][>][^][v][top][bottom][index][help] */
 147   Object_t *obj = NULL;
 148 
 149   int type;
 150   char *colon;
 151   gchar *token=NULL;
 152 
 153   colon = index(line, ':'); 
 154   if (colon != NULL) {
 155     if (line[0] =='*') {
 156       token = g_strndup(line+1, 2);
 157       type = DF_class_code2type(token);
 158     }
 159     else {
 160       token = g_strndup(line, (colon - line));
 161       type = DF_class_name2type(token);
 162     }
 163     if(token)UT_free(token);
 164     
 165     if (type >= 0) {
 166       obj = (Object_t *)UT_calloc(1, sizeof(Object_t)+1);
 167       obj->attributes = NULL;
 168       obj->object = g_string_sized_new(STR_XXXL);
 169       obj->type = type;
 170     }
 171   }
 172   return obj;
 173 } /* object_new() */
 174 
 175 
 176 /************************************************************
 177 * void transaction_free()                                   *
 178 *                                                           *
 179 * Frees memory allocated for a transaction                  *
 180 *                                                           *
 181 ************************************************************/
 182 
 183 void transaction_free(Transaction_t *tr) {
     /* [<][>][^][v][top][bottom][index][help] */
 184   if(tr) {
 185     g_string_free(tr->error_script, TRUE);
 186     g_string_free(tr->K, TRUE);
 187     /* free nic_handle_t structure used for NHR stuff */
 188     if(tr->nh)free_nh(tr->nh);
 189     if(tr->save){
 190 /*          fprintf(stderr, "FREED [%s]\n", tr->save); */
 191             UT_free(tr->save);
 192     }
 193     if(tr->packptr)UT_free(tr->packptr);
 194     UT_free(tr);
 195   }  
 196 } /* transaction_free() */
 197 
 198 /************************************************************
 199 * Transaction_t *transaction_new()                          *
 200 *                                                           *
 201 * Creates a transaction                                     *
 202 *                                                           *
 203 ************************************************************/
 204 Transaction_t *transaction_new(SQ_connection_t *sql_connection, C_Type_t class_type) {
     /* [<][>][^][v][top][bottom][index][help] */
 205   Transaction_t *tr = (Transaction_t *)UT_calloc(1, sizeof(Transaction_t));
 206 
 207   tr->sql_connection = sql_connection;
 208   tr->class_type = class_type;
 209   tr->thread_upd=TR_UPDATE;
 210   tr->thread_ins=TR_INSERT;
 211   tr->succeeded = 1;
 212   tr->error_script = g_string_sized_new(STR_XL);
 213   tr->K = g_string_sized_new(STR_L);
 214   tr->sequence_id=1; /* we start from 1*/
 215   tr->packptr=UT_calloc(1, sizeof(rp_upd_pack_t));
 216 
 217   return tr;
 218 } /* transaction_new() */
 219 /************************************************************
 220 * int UD_ack()                                            *
 221 *
 222 * Sends an acknowledgement to DBupdate and receives a reply *                                                           *
 223 *                                                           *
 224 * Return codes:                                             *
 225 *                                                           *
 226 * 0  - OK
 227 * -1 - 
 228 ************************************************************/
 229 int UD_ack(Transaction_t* tr)
     /* [<][>][^][v][top][bottom][index][help] */
 230 {
 231 GString *g_buff;
 232 int res, error;
 233  
 234  /* if we are not in update/server mode - no ack is needed - just return */
 235  if(IS_STANDALONE(tr->mode)) return(0);
 236  if(!IS_UPDATE(tr->mode)) return(0);
 237  
 238  if ((g_buff = g_string_sized_new(STR_L)) == NULL){ 
 239       ER_perror(FAC_UD, UD_MEM, "cannot allocate gstring\n");
 240       die; 
 241  }
 242 
 243  if(tr->succeeded==0) { /* update failed */
 244    error = tr->error;
 245  }
 246  else error = 0;
 247 
 248  g_string_sprintf(g_buff, "%%ERROR %d\n%s\n", error, (tr->error_script)->str);
 249  res = SK_puts(tr->socket, g_buff->str, NULL);
 250  g_string_free(g_buff, TRUE);
 251  /* close the connection */
 252 /* Let DBupdate close the connection */
 253 /* close(tr->socket); */
 254  return(res);           
 255 }
 256 

/* [<][>][^][v][top][bottom][index][help] */