## modules/ak/ack.cc

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

## FUNCTIONS

This source file includes following functions.

1. AK_add_to_ack
2. AK_add_to_ack_string
3. AK_ack_file_name_generate
4. AK_send_ack
5. AK_delete_ack
6. AK_log_ack

       1 /***************************************
       2   $Revision: 1.5 $
       3
       4   AK (Acknowledgement) module
       5
       6   Status: NOT REVIEWED, NOT TESTED
       7
       8   Author(s):       Engin Gunduz
       9
      10   ******************/ /******************
      11   Modification History:
      12         engin (10/06/2000) Created.
      13   ******************/ /******************
      14   Copyright (c) 2000                              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
      35
      36 #include "ack.h"
      37
      38 /*
      39
      40   AK_add_to_ack: writes a message to the acknowledgement file.
      41
      42 */
      43
      44 void AK_add_to_ack(char * filename, char * fmt, ...){
         /* [<][>][^][v][top][bottom][index][help] */
      45
      46   va_list ap;  /* points to each unnamed arg in turn */
      47   char *p, *sval;
      48   int ival;
      49   double dval;
      50   FILE * ack_file;
      51
      52   if(( ack_file = fopen(filename, "a")) == NULL){
      53     fprintf(stderr, "Can't open ack file, %s", filename);
      54   }
      55
      56   va_start(ap, fmt);
      57
      58   for(p = fmt; *p; p++){
      59     if (*p != '%') {
      60       fprintf(ack_file, "%c", *p);
      61       continue;
      62     }
      63     switch(*++p) {
      64     case 'd':
      65       ival = va_arg(ap, int);
      66       fprintf(ack_file, "%d", ival);
      67       break;
      68     case 'f':
      69       dval = va_arg(ap, double);
      70       fprintf(ack_file, "%f", dval);
      71       break;
      72     case 's':
      73       //for(sval = va_arg(ap, char *); *sval; sval++)
      74       //  putchar(*sval);
      75       sval = va_arg(ap, char *);
      76       fprintf(ack_file, "%s", sval);
      77       break;
      78     default:
      79       putchar(*p);
      80       break;
      81     }
      82   }
      83
      84   va_end(ap); /* clean up */
      85   fclose(ack_file);
      86 }
      87
      88 /*  */
      89 void AK_add_to_ack_string(const char * file_name, const string msg){
         /* [<][>][^][v][top][bottom][index][help] */
      90
      91    ofstream ack_file(file_name, ios::app);
      92
      93    if(!ack_file){
      94      cerr << "Couldn't open ack file" << endl;
      95      return;
      96    }
      97    ack_file << msg;
      98    ack_file.close();
      99 }
     100
     101
     102
     103
     104 /*
     105
     106   AK_ack_file_name_generate: Generates a unique name for temporary acknowledgement
     107      files, and also creates it.
     108
     109       tmpdir: temporary directory (without a trailing '/')
     110       prefix: prefix for the temp file
     111
     112       returns: the generated name.
     113
     114
     115 */
     116
     117 char * AK_ack_file_name_generate( const char * tmpdir, const char * prefix){
         /* [<][>][^][v][top][bottom][index][help] */
     118
     119    FILE * ack_file;
     120    char * name;
     121
     122    /* allocate space for name. 32 should be enough for PID */
     123    name = (char*)malloc(strlen(tmpdir) + strlen(prefix) + 32);
     124
     125    sprintf(name, "%s/%s.%i", tmpdir, prefix, getpid());
     126
     127    /* create the file */
     128    if(( ack_file = fopen(name, "w")) == NULL){
     129      fprintf(stderr, "Can't open ack file, %s", name);
     130    }
     131
     132    /* close it */
     133    fclose(ack_file);
     134
     135    return name;
     136
     137 }
     138
     139
     140 /*
     141
     142 AK_send_ack: sends the ack message contained in the temp file.
     143
     144
     145 */
     146
     147 void AK_send_ack( const char * filename, const char * to_address, const char * mailercommand){
         /* [<][>][^][v][top][bottom][index][help] */
     148
     149     char * mail_command_line = NULL;
     150
     151
     152     if(to_address != NULL){
     153       mail_command_line = (char *)malloc(strlen(mailercommand) + strlen(filename) + 128);
     154       sprintf(mail_command_line, "%s %s < %s", mailercommand, to_address, filename);
     155       system(mail_command_line);
     156     }
     157
     158
     159 }
     160
     161
     162 /*
     163
     164   AK_delete_ack: deletes the temporary acknowledgement file.
     165
     166 */
     167
     168 void AK_delete_ack( const char * filename ){
         /* [<][>][^][v][top][bottom][index][help] */
     169
     170    unlink(filename);
     171
     172 }
     173
     174 /*
     175
     176 AK_log_ack: logs the acknowledgements in the log_file.
     177
     178 */
     179
     180 void AK_log_ack(const char * filename, const char * logfilename){
         /* [<][>][^][v][top][bottom][index][help] */
     181
     182   FILE * ack_file, * log_file;
     183   char * buf;
     184
     185   buf = (char *)malloc(1024);
     186   if(( ack_file = fopen(filename, "r")) == NULL){
     187     fprintf(stderr, "Can't open ack file, %s\n", filename);
     188     return;
     189   }
     190
     191   if(( log_file = fopen(logfilename, "a")) == NULL){
     192     fprintf(stderr, "Can't open log file, %s\n", logfilename);
     193     return;
     194   }
     195
     196   /* must put real time here */
     197   fprintf(log_file, ">>> time: ACK <<<\n\n");
     198
     199
     200   while((buf=fgets(buf, 1023, ack_file)) > 0){
     201     fprintf(log_file, "%s", buf);
     202   }
     203
     204
     205   fclose(ack_file);
     206   fclose(log_file);
     207
     208 }
     209
     210
     211
     212
     213
     214
     215
     216

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