Skip to content
sld.hh 1.94 KiB
Newer Older
Seblu's avatar
Seblu committed

#ifndef SLD_HH
# define SLD_HH

# include <cassert>
# include <ios>
# include <iostream>
# include <fstream>
# include <sstream>
# include <map>

# include "error.hh"

typedef std::string string;
typedef std::stringstream sstream;
typedef std::ostream ostream;
typedef std::ifstream ifstream;

enum {
  EXIT_OK = 0,
  EXIT_USAGE = 1,
  EXIT_BADPARAM = 2,
  EXIT_FILE = 3,
  EXIT_NET = 4,
  EXIT_PROTO = 5,
  EXIT_NOMEM = 41,
  EXIT_UNKNOWN = 42
};

// -----------------------------------------------------------------------------
// Gonstant
// -----------------------------------------------------------------------------

const string VERSION = "1";
const int RECV_BUF_SIZE = 512;

// -----------------------------------------------------------------------------
// SLDAEMON CLASS
// -----------------------------------------------------------------------------

class SLDaemon
{
public:

  static const string version;
  
  struct options {
    options();
    string host;
    int port;
    string user;
    string pass;
    int verbose; //0=false, 1=true, 3=undef
    string scriptdir;
    string conffile;
  };

  options *getoptions(int argc, char *argv[]) const;
  options *getoptions(const string file) const;
  void applyoptions(const options *opt);
  void run();
protected:
  // parsed options
  options options_;

  // current socket
  int socket_;

  // receive buffer
  int recv_buf_[RECV_BUF_SIZE];
  char *recv_line_;
  size_t recv_offset_;
  size_t recv_size_;

  // functions
  void usage(const char *argv0) const;
  void check_options() const;
  void connect();
  void send(const char *data, size_t len);
  void send(const string str);
  char *recv();
  void bufferize(char **str, const char *append, size_t n);
  void protocol_violation();
  void auth();

  inline bool verbose() const { return options_.verbose == 1; }

  void cmd_version();
  void cmd_exit();
  void cmd_update();
  void cmd_exec(const char *line);
  void cmd_file(const char *line);
};

#endif