Convert a C pgm

Benoit BESSE benoit.besse at club-internet.fr
Tue Mar 11 16:38:18 EST 2003


Hello,
who is able to convert this C program into a Python script or mudule
Thanks

--------------------------------------------------------------------------------

file sdplex.h

--------------------------------------------------------------------------------

#ifndef _SDPLEX__H
#define _SDPLEX__H
#include <stdio.h>
#include <string.h>
#include <sys/types.h>  /* pour inet_addr inet_ntoa gethostbyaddr */
#include <sys/socket.h>  /* pour inet_addr inet_ntoa gethostbyaddr */
#include <netinet/in.h>  /* pour inet_addr inet_ntoa gethostbyaddr */
#include <arpa/inet.h>  /* pour inet_addr inet_ntoa gethostbyaddr */
#include <netdb.h>  /* pour gethostbyaddr */
#include <sys/time.h>
#include <time.h>  /* pour la date */
#include <sys/ddi.h>  /* pour min, max */
#include <unistd.h>  /* pour chdir */
#include <synch.h>  /* for mutex library */
#include "config.h"
#include "record-session.h"
#include "update-sessions.h"
#define ANNOUNCE 0
#define DELETION 1
void  analyseSAP     (char *buf, int length, struct in_addr from);
void  analyseSDP     (char *buf, int length, struct in_addr from);
void  deleteSDP      (char *buf, int length, struct in_addr from);
int   checkSignature (char*authHeader, int length);
// int   verifyGrammar  (char buf[2048], int length);
int   verifyField(int *i, int *j, int globalRepeat, char *CurrentField,
    char fields[10], char optional[10], char repeat[10]);
int verifyFieldIgnoreOrder(int *i, int *j, int GlobalRepeat, char *CurrentLine,
      char fields[10], char compulsory[10], char repeat[10]);
char *extractLine    (char *buf, int maxlength, char *field);
char *nextField      (char *buf, int maxlength, char *field, char NameOfField);
void  fnextField     (FILE *file, int maxlength, char *field, char NameOfField);
void  saveSession  (Session *session);
void  addSessionToList(char *buf, int length, char *ID);
void  addSessionToCache(char *buf, int length, char *ID);
/* fin des declarations */
#endif /* _SDPLEX__H */

--------------------------------------------------------------------------------

file sdplex.c

--------------------------------------------------------------------------------

#include "sdplex.h"
void analyseSAP(char *buf, int length, struct in_addr from) {
  unsigned long addr_src;
  int header_length, datagram_type;
  printf("Analyse du paquet recu :\n\n");
  printf("Version de SAP  : %d\n", ((buf[0] >> 5) & 0x7)); /* bits 5 a 7 */
  printf("Type de message : ");
  switch ((buf[0] >> 2) & 0x7) /* bits 2 a 4 */
  {
  case 0 : printf("annonce de session\n");    datagram_type = ANNOUNCE; break;
  case 1 : printf("annulation de session\n"); datagram_type = DELETION; break;
  default : perror("type de paquet SAP inconnu"); exit (0);
  }
  if (buf[0] & 0x2) printf("annonce chiffree\n"); /* bit 1 */
  else printf ("annonce en clair\n");

  if (buf[0] & 0x1) printf("annonce gzipee\n"); /* bit 0 */
  else printf("annonce non gzipee\n");

  printf("version de l'annonce: ");
  if (buf[3]) printf("%d\n", buf[3]); else printf ("ignoree\n");

  addr_src = *((unsigned long*) &buf[4]);

  printf("annonce envoyee depuis l'hote %s d'apres le paquet SAP\n",
  inet_ntoa(*((struct in_addr*) &addr_src)));

  printf("annonce envoyee depuis l'hote %s en realite\n",
  inet_ntoa(from));

  printf ("taille des donnees d'authentification %hu x 32 bits\n", buf[2]);
  printf("\n");

  if (buf[2])
    if (checkSignature(&buf[4], buf[2])==0) {
      perror("authentification failure"); return; }
 
  header_length = ((int) buf[2])*4+8;

  if (datagram_type == ANNOUNCE)
    analyseSDP(&buf[header_length], length-header_length, from);
  else if (datagram_type == DELETION)
    deleteSDP(&buf[header_length], length-header_length, from);
}

int  checkSignature(char*authHeader, int length) {
  /* je ne sais pas encore faire */

return 0;
}

void analyseSDP(char *buf, int length, struct in_addr from) {
//  FILE *f;
//  char FileName[256], SessionOwner[256], CurrentField[256];
  char bufCopy[2049];
  Session *session;
  SessionInfo *info;

  /* trouver un nom au fichier du cache */
  /* ce nom doit etre de preference une clef d'identification de la session */
  /* donc se baser sur le champ SDP o= */

  strncpy(bufCopy, buf, 2048);
  bufCopy[length]=0;

  session = verifyGrammar(bufCopy, length);

  printf("Session address in memory : %d\n", (int) session);
  if (session) {
    if ( (info = whereIs(session))!=NULL) delSessionInfo(info);

    mutex_lock(&lock);
    info = addSessionInfo(session);
    mutex_unlock(&lock);

    if (info) {
      saveSession(session);
      createMediaFile(session);
    }
    else printf("Session obsolete\n");

    dealloc(session);
  }
}

void saveSession(Session *session) {
  FILE *f;
  int i;

  chdir(BASE_DIRECTORY);
  chdir(CACHE_DIRECTORY);

  printf ("Creating cache file %s.\n", session->cachename);
  if ((f=fopen(session->cachename, "w")) == NULL) {
    perror ("fopen (write)");
    return;
  }
  for(i=0; i<session->buflen; i++)
    if (session->buf[i]=='\0') fputc('\n', f);
    else fputc(session->buf[i], f);
  fclose(f);
  printf ("Cache file %s created.\n", session->cachename);
}

char *extractLine(char *buf, int maxlength, char *field) {
  /* extrait une ligne (field) d'une chaine de caractere (buf)
   * qui en contient plusieurs. Une ligne se termine avec un \n
   * field contient la chaine extraite et la fonction retourne
   * le pointeur sur le \n final. Pour eviter le debordement
   * de la chaine field, on ne depasse pas la taille maxlength */

  char* index;
  int sizeOfLine;

  index=strstr(buf,"\n");
  sizeOfLine = min ( (int)(index - buf), maxlength - 1 );

  strncpy(field, buf, sizeOfLine);
  field[sizeOfLine]='\0';
  return index;
}

char *nextField (char *buf, int maxlength, char *field, char NameOfField) {
  char tag[4]="\n =";

  if (NameOfField=='\0') return extractLine(buf, maxlength, field);
  else {
    tag[1]=NameOfField;
    return extractLine(strstr(buf, tag)+3, 256, field);
  }
}

void fnextField (FILE *file, int maxlength, char *field, char NameOfField) {
  char CurrentField[256];
  char *state;

  while ( ((state=fgets(CurrentField, maxlength, file))!=NULL) && (CurrentField[0]!=NameOfField));

  if (state) strncpy(field, CurrentField+3, 256);
  else field[0]='\0';
 }

void  deleteSDP  (char *buf, int length, struct in_addr from) {
  printf("Explicit session deletion requested\n");
  return;
}

int verifyField(int *i, int *j, int GlobalRepeat, char *CurrentLine,
  char fields[10], char compulsory[10], char repeat[10]) {

  if (CurrentLine[1]!='=') return 0;
  if (CurrentLine[0]==fields[*i])
    {
      /* note: only optional fields may be multiple defined */
      if(repeat[*i]=='0')
 {
   (*i)++;
   if (fields[*i]=='\0')
   {
     *i=0;
     /* if several descriptions of that group may exist and
      * a new description of that type is detected, then do not
      * increment j */
     if ((GlobalRepeat==1) && (CurrentLine[0]==fields[0]));
     else (*j)++;
   }
 }
      return 1;
    }

  while (CurrentLine[0]!=fields[*i])
    {
      if (compulsory[*i]=='1') return 0;
      else
 {
   (*i)++;
   if (fields[*i]=='\0')
     {
       *i=0;
       /* if the group of fields may not be repeated, then go
        * to next group of fields */
       if (GlobalRepeat==0)
  {
    (*j)++;
    return -1;
  }
     }
 }
    }

  return 1;
}

int verifyFieldIgnoreOrder(int *i, int *j, int GlobalRepeat, char *CurrentLine,
  char fields[10], char compulsory[10], char repeat[10]) {

  char *matchingChar;

  if ((matchingChar=strchr(fields, CurrentLine[0]))!=NULL)
    {
      /* character marked as already matched, though cannot be defined several times */
      if (*matchingChar=='.') return 0;

      if(repeat[*i]=='0')
   /* mark that an unrepeatable field matched */
   fields[*i]='.';

      return 1;
    }

      else
      {
 (*j)++;
 return -1;
      }
}

void addSessionToCache(char *buf, int  length, char *ID) {
  FILE *cacheFile;

  chdir(BASE_DIRECTORY);
  chdir(CACHE_DIRECTORY);

  printf("Writing cache file %s in %s/%s\n", ID, BASE_DIRECTORY, CACHE_DIRECTORY);
  if ((cacheFile = fopen(ID, "w"))==NULL) { perror("fopen"); return; }
  fwrite(buf, 1, length, cacheFile);
  fclose(cacheFile);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20030311/6621d6fd/attachment.html>


More information about the Python-list mailing list