Below is the file 'fritz.hh' from this revision. You can also download the file.

#ifndef _FRITZ_H
#define _FRITZ_H

/* fritz.hh
 * common data structures
 */

#include <sys/errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <fcntl.h>
#include <unistd.h>

enum fritz_message {
  FRITZ_MESG_SYSCALL_OPEN,
  FRITZ_MESG_SYSCALL_READ,
  FRITZ_MESG_SYSCALL_WRITE,
  FRITZ_MESG_SYSCALL_CLOSE
};

class fritz_exception: public std::exception
{
private:
    const char *err;

public:
    fritz_exception (const char *e)
    {
        err = e;
    }
    virtual const char* what() const throw()
    {
        char *s_err;

        s_err = strerror (errno);
        std::cout << "from errno: " << s_err << std::endl;
        return err;
    }
};

struct fritz_params {
    size_t              buffer_size;      /* total size of the shared memory segment */
    pid_t               pid;              /* process ID sending this message */
    size_t              message_id;       /* monotonically increasing message ID */
    int                 fd;               /* file descriptor from which data was gathered */
    size_t              contents_size;    /* size of contents */
};

class Fritzer {
protected:
    const char *mode; /* used in debugging messages, eg. "client" or "server" */
    static const int shm_size=(1024*16);
    pid_t my_pid;

    /* semaphores */
    const char *server_sem_name, *client_sem_name;
    sem_t *server_sem, *client_sem;
    /* shared memory */
    const char *shm_name;
    int shm_fd;
    void *shm_mem;
    struct fritz_params *fritz_params;
    unsigned char *fritz_contents;

    void initialise_from_environ (void);
    sem_t *open_semaphore (const char *);
    sem_t *create_semaphore (const char *);
    void close_semaphores (void);
    void map_the_shm (void);
    void update_params (void);
public:
    Fritzer (void);
    size_t get_message_id (void);
};

class FritzServer : public Fritzer {
private:
    void wait_for_request (void);
    void create_semaphores ();
    void create_shared_memory (void);
    void wait_for_client (void);
    void signal_client (void);
    void rewrite (void);
    void rewrite_rot13 (void);

public:
    FritzServer (void);
    ~FritzServer (void);
    void run (void);
};

class FritzClient : public Fritzer {
private:
    void open_semaphores (void);
    void open_shared_memory (void);
    void wait_for_server (void);
    void signal_server (void);
    void sync (void);
public:
    FritzClient (void);
    ~FritzClient (void);
    const char *Hello(void);
    void pass_to_server (void);
    void *grab_symbol_from (const char *, const char *);
    void copy_data (size_t, void *);
    size_t copy_back (size_t, void *);
};

#endif