1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// A simple standalone XML-RPC server written in C++.
//
// This server returns to the caller his IP address and port number,
// as a demonstration of how to access such information.
//
// This works only on Unix (to wit, something that uses Abyss's
// ChanSwitchUnix channel switch to accept TCP connections from clients).
//
// See xmlrpc_sample_add_server.cpp for a more basic example.
//
// To run this:
//
// $ ./callinfo_abyss_server &
// $ xmlrpc localhost:8080 getCallInfo
#define WIN32_LEAN_AND_MEAN /* required by xmlrpc-c/server_abyss.hpp */
#include <cassert>
#include <stdexcept>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#endif
using namespace std;
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>
#include <xmlrpc-c/abyss.h>
struct tcpPortAddr {
unsigned char ipAddr[4];
unsigned short portNumber;
};
static struct tcpPortAddr
tcpAddrFromSockAddr(struct sockaddr const sockAddr) {
const struct sockaddr_in * const sockAddrInP(
static_cast<struct sockaddr_in *>((void *)&sockAddr));
const unsigned char * const ipAddr(
static_cast<const unsigned char *>(
(const void *)&sockAddrInP->sin_addr.s_addr)
); // 4 byte array
assert(sockAddrInP->sin_family == AF_INET);
struct tcpPortAddr retval;
retval.ipAddr[0] = ipAddr[0];
retval.ipAddr[1] = ipAddr[1];
retval.ipAddr[2] = ipAddr[2];
retval.ipAddr[3] = ipAddr[3];
retval.portNumber = ntohs(sockAddrInP->sin_port);
return retval;
}
/* On Windows, we have struct abyss_win_chaninfo, while on Unix we have
struct abyss_unix_chaninfo, but for what we're doing here, they're
fungible -- we use only members that exist in both. So we refer to the
generically with macro CHANINFO_TYPE.
*/
#ifdef _WIN32
#define CHANINFO_TYPE abyss_win_chaninfo
#else
#define CHANINFO_TYPE abyss_unix_chaninfo
#endif
static string
rpcIpAddrMsg(xmlrpc_c::callInfo_serverAbyss const& callInfo) {
void * chanInfoPtr;
SessionGetChannelInfo(callInfo.abyssSessionP, &chanInfoPtr);
struct CHANINFO_TYPE * const chanInfoP(
static_cast<struct CHANINFO_TYPE *>(chanInfoPtr));
struct tcpPortAddr const tcpAddr(tcpAddrFromSockAddr(chanInfoP->peerAddr));
char msg[128];
sprintf(msg, "RPC is from IP address %u.%u.%u.%u, Port %hu",
tcpAddr.ipAddr[0],
tcpAddr.ipAddr[1],
tcpAddr.ipAddr[2],
tcpAddr.ipAddr[3],
tcpAddr.portNumber);
return string(msg);
}
class getCallInfoMethod : public xmlrpc_c::method2 {
public:
void
execute(xmlrpc_c::paramList const& paramList,
const xmlrpc_c::callInfo * const callInfoPtr,
xmlrpc_c::value * const retvalP) {
const xmlrpc_c::callInfo_serverAbyss * const callInfoP(
dynamic_cast<const xmlrpc_c::callInfo_serverAbyss *>(callInfoPtr));
paramList.verifyEnd(0);
// Because this gets called via a xmlrpc_c::serverAbyss:
assert(callInfoP != NULL);
*retvalP = xmlrpc_c::value_string(rpcIpAddrMsg(*callInfoP));
}
};
int
main(int const,
const char ** const) {
try {
xmlrpc_c::registry myRegistry;
xmlrpc_c::methodPtr const getCallInfoMethodP(new getCallInfoMethod);
myRegistry.addMethod("getCallInfo", getCallInfoMethodP);
xmlrpc_c::serverAbyss myAbyssServer(xmlrpc_c::serverAbyss::constrOpt()
.registryP(&myRegistry)
.portNumber(8080)
);
myAbyssServer.run();
// xmlrpc_c::serverAbyss.run() never returns
assert(false);
} catch (exception const& e) {
cerr << "Something failed. " << e.what() << endl;
}
return 0;
}
|