3. Declaring and reading configuration details

It is highly likely that a backend needs configuration details. On launch, these parameters need to be declared with PDNS so it knows it should accept them in the configuration file and on the command line. Furthermore, they will be listed in the output of --help.

Declaring arguments is done by implementing the member function declareArguments() in the factory class of your backend. PDNS will call this method after launching the backend.

In the declareArguments() method, the function declare() is available. The exact definitions:

void declareArguments(const string &suffix="")

This method is called to allow a backend to register configurable parameters. The suffix is the sub-name of this module. There is no need to touch this suffix, just pass it on to the declare method.

void declare(const string &suffix, const string &param, const string &explanation, const string &value)

The suffix is passed to your method, and can be passed on to declare. param is the name of your parameter. explanation is what will appear in the output of --help. Furthermore, a default value can be supplied in the value parameter.

A sample implementation:

	    void declareArguments(const string &suffix)
	    {
 	      declare(suffix,"dbname","Pdns backend database name to connect to","powerdns");
	      declare(suffix,"user","Pdns backend user to connect as","powerdns");
	      declare(suffix,"host","Pdns backend host to connect to","");
	      declare(suffix,"password","Pdns backend password to connect with","");
	    }
	  

After the arguments have been declared, they can be accessed from your backend using the mustDo(), getArg() and getArgAsNum() methods. The are defined as follows in the DNSBackend class:

void setArgPrefix(const string &prefix)

Must be called before any of the other accessing functions are used. Typical usage is 'setArgPrefix("mybackend"+suffix)' in the constructor of a backend.

bool mustDo(const string &key)

Returns true if the variable key is set to anything but 'no'.

const string& getArg(const string &key)

Returns the exact value of a parameter.

int getArgAsNum(const string &key)

Returns the numerical value of a parameter. Uses atoi() internally

Sample usage from the BindBackend, using the bind-example-zones and bind-config parameters.

  if(mustDo("example-zones")) {
    insert(0,"www.example.com","A","1.2.3.4");
    /* ... */
  }
  

  if(!getArg("config").empty()) {
    BindParser BP;
    
    BP.parse(getArg("config"));
  }