| 1 |
// vim:set cindent: |
| 2 |
#include <openssl/ssl.h> |
| 3 |
#include <string.h> |
| 4 |
#define PERL_NO_GET_CONTEXT |
| 5 |
#include "EXTERN.h" |
| 6 |
#include "perl.h" |
| 7 |
#include "XSUB.h" |
| 8 |
|
| 9 |
#include "ppport.h" |
| 10 |
|
| 11 |
#include "client.h" |
| 12 |
|
| 13 |
#define SV_OR_DIE(var) { if(!SvOK(var)) croak("%s must not be undef", #var); } |
| 14 |
#define SV_OR_NULL(var) (SvOK(var) ? SvPV_nolen(var) : NULL) |
| 15 |
#define SVdup_OR_NULL(var) (SvOK(var) ? strdup(SvPV_nolen(var)) : NULL) |
| 16 |
#define FREE_IF_SET(ptr) { if(ptr) free(ptr); } |
| 17 |
|
| 18 |
struct nscang_object { |
| 19 |
nscang_client_t client; |
| 20 |
int client_initialized; |
| 21 |
char *host; |
| 22 |
char *identity; |
| 23 |
char *psk; |
| 24 |
char *ciphers; |
| 25 |
char *node_name; |
| 26 |
char *svc_description; |
| 27 |
int port; |
| 28 |
int timeout; |
| 29 |
}; |
| 30 |
typedef struct nscang_object nscang_object_t; |
| 31 |
|
| 32 |
typedef nscang_object_t * Net__NSCAng__Client; |
| 33 |
|
| 34 |
static char *strdupnull(const char *s) { |
| 35 |
return NULL == s ? NULL : strdup(s); |
| 36 |
} |
| 37 |
|
| 38 |
#define DUP_OR_RETURN(attr) { \ |
| 39 |
if(attr) { \ |
| 40 |
if(!(o->attr = strdupnull(attr))) return 1; \ |
| 41 |
} else { \ |
| 42 |
o->attr = NULL; \ |
| 43 |
}\ |
| 44 |
} |
| 45 |
static int init_object( |
| 46 |
nscang_object_t *o, |
| 47 |
char *host, int port, char *identity, char *psk, char *ciphers, |
| 48 |
char *node_name, char * svc_description, int timeout) { |
| 49 |
o->timeout = timeout; |
| 50 |
o->port = port; |
| 51 |
DUP_OR_RETURN(host); |
| 52 |
DUP_OR_RETURN(identity); |
| 53 |
DUP_OR_RETURN(psk); |
| 54 |
DUP_OR_RETURN(ciphers); |
| 55 |
DUP_OR_RETURN(node_name); |
| 56 |
DUP_OR_RETURN(svc_description); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
static void init_client(nscang_object_t *o) |
| 61 |
{ |
| 62 |
|
| 63 |
// Initialize the client library |
| 64 |
if(!nscang_client_init(&o->client, o->host, o->port, o->ciphers, o->identity, o->psk)) { |
| 65 |
char errstr[1024]; |
| 66 |
|
| 67 |
nscang_client_free(&o->client); |
| 68 |
croak("nscang_client_init: %s", |
| 69 |
nscang_client_errstr(&o->client, errstr, sizeof(errstr)) |
| 70 |
); |
| 71 |
} |
| 72 |
o->client_initialized = 1; |
| 73 |
} |
| 74 |
|
| 75 |
static void free_object(nscang_object_t *o) { |
| 76 |
FREE_IF_SET(o->svc_description); |
| 77 |
FREE_IF_SET(o->node_name); |
| 78 |
FREE_IF_SET(o->ciphers); |
| 79 |
FREE_IF_SET(o->psk); |
| 80 |
FREE_IF_SET(o->identity); |
| 81 |
FREE_IF_SET(o->host); |
| 82 |
} |
| 83 |
|
| 84 |
MODULE = Net::NSCAng::Client PACKAGE = Net::NSCAng::Client |
| 85 |
|
| 86 |
PROTOTYPES: DISABLE |
| 87 |
|
| 88 |
BOOT: |
| 89 |
SSL_library_init(); |
| 90 |
SSL_load_error_strings(); |
| 91 |
|
| 92 |
Net::NSCAng::Client |
| 93 |
_new(class, host, port, identity, psk, ciphers, node_name, svc_description, timeout) |
| 94 |
char * class |
| 95 |
char * host |
| 96 |
int port |
| 97 |
char * identity |
| 98 |
char * psk |
| 99 |
SV * ciphers |
| 100 |
SV * node_name |
| 101 |
SV * svc_description |
| 102 |
int timeout |
| 103 |
|
| 104 |
CODE: |
| 105 |
// Alloc new object |
| 106 |
Newxz(RETVAL, 1, nscang_object_t); |
| 107 |
if(!RETVAL) |
| 108 |
croak("no memory for %s", class); |
| 109 |
|
| 110 |
// Copy/duplicate values to object attributes |
| 111 |
if(init_object(RETVAL, |
| 112 |
host, port, identity, psk, |
| 113 |
SV_OR_NULL(ciphers), SV_OR_NULL(node_name), |
| 114 |
SV_OR_NULL(svc_description), timeout) |
| 115 |
) { |
| 116 |
free_object(RETVAL); |
| 117 |
croak("no memory for object"); |
| 118 |
} |
| 119 |
|
| 120 |
init_client(RETVAL); |
| 121 |
OUTPUT: |
| 122 |
RETVAL |
| 123 |
|
| 124 |
void |
| 125 |
DESTROY(self) |
| 126 |
Net::NSCAng::Client self |
| 127 |
CODE: |
| 128 |
if(self) { |
| 129 |
nscang_client_free(&(self->client)); |
| 130 |
free_object(self); |
| 131 |
Safefree(self); |
| 132 |
} |
| 133 |
|
| 134 |
SV * |
| 135 |
_result(self, is_host_result, node_name, svc_description, return_code, plugin_output) |
| 136 |
Net::NSCAng::Client self |
| 137 |
int is_host_result |
| 138 |
SV * node_name |
| 139 |
SV * svc_description |
| 140 |
int return_code |
| 141 |
char * plugin_output |
| 142 |
|
| 143 |
CODE: |
| 144 |
char *cnode_name = self->node_name; |
| 145 |
char *csvc_description = self->svc_description; |
| 146 |
char errstr[1024]; |
| 147 |
nscang_client_t *client = &self->client; |
| 148 |
|
| 149 |
if(!self->client_initialized) |
| 150 |
init_client(self); |
| 151 |
|
| 152 |
if(SvOK(node_name)) |
| 153 |
cnode_name = SvPV_nolen(node_name); |
| 154 |
|
| 155 |
if(is_host_result) { |
| 156 |
csvc_description = NULL; |
| 157 |
} else { |
| 158 |
if(SvOK(svc_description)) |
| 159 |
csvc_description = SvPV_nolen(svc_description); |
| 160 |
} |
| 161 |
|
| 162 |
if(!(is_host_result || csvc_description)) |
| 163 |
croak("svc_description missing"); |
| 164 |
|
| 165 |
if(!cnode_name) |
| 166 |
croak("node_name missing"); |
| 167 |
|
| 168 |
if(nscang_client_send_push(client, |
| 169 |
cnode_name, csvc_description, return_code, plugin_output, self->timeout)) |
| 170 |
XSRETURN_UNDEF; |
| 171 |
|
| 172 |
if(client->_errno == NSCANG_ERROR_TIMEOUT) { |
| 173 |
nscang_client_disconnect(client); |
| 174 |
|
| 175 |
if(nscang_client_send_push(client, |
| 176 |
cnode_name, csvc_description, return_code, plugin_output, self->timeout)) |
| 177 |
XSRETURN_UNDEF; |
| 178 |
} |
| 179 |
|
| 180 |
// Retrieve the error description |
| 181 |
nscang_client_errstr(client, errstr, sizeof(errstr)); |
| 182 |
// Remember to reinitialize the client text time through |
| 183 |
self->client_initialized = 0; |
| 184 |
nscang_client_free(&(self->client)); |
| 185 |
|
| 186 |
RETVAL = newSVpv(errstr, 0); |
| 187 |
OUTPUT: |
| 188 |
RETVAL |
| 189 |
|
| 190 |
SV * |
| 191 |
command(self, command) |
| 192 |
Net::NSCAng::Client self |
| 193 |
SV * command |
| 194 |
|
| 195 |
CODE: |
| 196 |
char errstr[1024]; |
| 197 |
char *ccommand; |
| 198 |
nscang_client_t *client = &self->client; |
| 199 |
|
| 200 |
if(!self->client_initialized) |
| 201 |
init_client(self); |
| 202 |
|
| 203 |
if(SvOK(command)) { |
| 204 |
ccommand = SvPV_nolen(command); |
| 205 |
} else { |
| 206 |
croak("command missing"); |
| 207 |
} |
| 208 |
|
| 209 |
if(nscang_client_send_command(client, ccommand, self->timeout)) |
| 210 |
XSRETURN_UNDEF; |
| 211 |
|
| 212 |
if(client->_errno == NSCANG_ERROR_TIMEOUT) |
| 213 |
nscang_client_disconnect(client); |
| 214 |
|
| 215 |
// Retrieve the error description |
| 216 |
nscang_client_errstr(client, errstr, sizeof(errstr)); |
| 217 |
// Remember to reinitialize the client text time through |
| 218 |
self->client_initialized = 0; |
| 219 |
nscang_client_free(&(self->client)); |
| 220 |
|
| 221 |
croak(errstr); |