Using the serviceentry class


Introduction

The serviceentry class provides methods for retrieving network service definitions. These are usually defined in a file like /etc/services on unix-like systems or C:\Windows\System32\drivers\etc\services on Windows, or are served from another source via the name-service switch.

Services can be looked up by name or number.


Lookup By Name

In this example, information for the "smtp" service, "tcp" protocol is retrieved.

// Copyright (c) 2003  David Muse
// See the file COPYING for more information

#include <rudiments/serviceentry.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

        // get the service information for "smtp","tcp" protocol
        serviceentry    se;
        se.initialize("smtp","tcp");

        // print out details
        stdoutput.printf("      Name:           %s\n",se.getName());
        stdoutput.printf("      Port:           %d\n",se.getPort());
        stdoutput.printf("      Protocol:       %s\n",se.getProtocol());
        stdoutput.printf("      Alias list:\n");
        for (uint32_t i=0; se.getAliasList() && se.getAliasList()[i]; i++) {
                stdoutput.printf("              %s\n",se.getAliasList()[i]);
        }
}

Lookup By Number

In this example, information for "service number 25", "tcp" protocol is retrieved.

// Copyright (c) 2003  David Muse
// See the file COPYING for more information

#include <rudiments/serviceentry.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

        // get the service information for the service
        // that would run on port 25, "tcp" protocol
        serviceentry    se;
        se.initialize(25,"tcp");

        // print out details
        stdoutput.printf("      Name:           %s\n",se.getName());
        stdoutput.printf("      Port:           %d\n",se.getPort());
        stdoutput.printf("      Protocol:       %s\n",se.getProtocol());
        stdoutput.printf("      Alias list:\n");
        for (uint32_t i=0; se.getAliasList() && se.getAliasList()[i]; i++) {
                stdoutput.printf("              %s\n",se.getAliasList()[i]);
        }
}