| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
#include "eventer/eventer.h" |
|---|
| 35 |
#include "noit_listener.h" |
|---|
| 36 |
#include "utils/noit_hash.h" |
|---|
| 37 |
#include "utils/noit_log.h" |
|---|
| 38 |
#include "utils/noit_sem.h" |
|---|
| 39 |
#include "noit_capabilities_listener.h" |
|---|
| 40 |
#include "noit_module.h" |
|---|
| 41 |
#include "noit_check.h" |
|---|
| 42 |
#include "noit_xml.h" |
|---|
| 43 |
|
|---|
| 44 |
#include <unistd.h> |
|---|
| 45 |
#include <sys/ioctl.h> |
|---|
| 46 |
#include <errno.h> |
|---|
| 47 |
|
|---|
| 48 |
#include <libxml/xmlsave.h> |
|---|
| 49 |
#include <libxml/tree.h> |
|---|
| 50 |
|
|---|
| 51 |
typedef struct noit_capsvc_closure { |
|---|
| 52 |
char *buff; |
|---|
| 53 |
size_t written; |
|---|
| 54 |
size_t towrite; |
|---|
| 55 |
} noit_capsvc_closure_t; |
|---|
| 56 |
|
|---|
| 57 |
void |
|---|
| 58 |
noit_capabilities_listener_init() { |
|---|
| 59 |
eventer_name_callback("capabilities_transit/1.0", noit_capabilities_handler); |
|---|
| 60 |
noit_control_dispatch_delegate(noit_control_dispatch, |
|---|
| 61 |
NOIT_CAPABILITIES_SERVICE, |
|---|
| 62 |
noit_capabilities_handler); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
int |
|---|
| 66 |
noit_capabilities_handler(eventer_t e, int mask, void *closure, |
|---|
| 67 |
struct timeval *now) { |
|---|
| 68 |
int newmask = EVENTER_WRITE | EVENTER_EXCEPTION; |
|---|
| 69 |
acceptor_closure_t *ac = closure; |
|---|
| 70 |
noit_capsvc_closure_t *cl = ac->service_ctx; |
|---|
| 71 |
|
|---|
| 72 |
if(mask & EVENTER_EXCEPTION) { |
|---|
| 73 |
socket_error: |
|---|
| 74 |
/* Exceptions cause us to simply snip the connection */ |
|---|
| 75 |
cleanup_shutdown: |
|---|
| 76 |
eventer_remove_fd(e->fd); |
|---|
| 77 |
e->opset->close(e->fd, &newmask, e); |
|---|
| 78 |
if(cl) { |
|---|
| 79 |
if(cl->buff) free(cl->buff); |
|---|
| 80 |
free(cl); |
|---|
| 81 |
} |
|---|
| 82 |
if(ac) acceptor_closure_free(ac); |
|---|
| 83 |
return 0; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
if(!ac->service_ctx) { |
|---|
| 87 |
char vbuff[128]; |
|---|
| 88 |
noit_hash_table *lc; |
|---|
| 89 |
noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| 90 |
const char *k; |
|---|
| 91 |
int klen; |
|---|
| 92 |
void *data; |
|---|
| 93 |
|
|---|
| 94 |
xmlDocPtr xmldoc; |
|---|
| 95 |
xmlNodePtr root, cmds; |
|---|
| 96 |
|
|---|
| 97 |
cl = ac->service_ctx = calloc(1, sizeof(*cl)); |
|---|
| 98 |
/* fill out capabilities */ |
|---|
| 99 |
noit_build_version(vbuff, sizeof(vbuff)); |
|---|
| 100 |
|
|---|
| 101 |
/* Create an XML Document */ |
|---|
| 102 |
xmldoc = xmlNewDoc((xmlChar *)"1.0"); |
|---|
| 103 |
root = xmlNewDocNode(xmldoc, NULL, (xmlChar *)"noit_capabilities", NULL); |
|---|
| 104 |
xmlDocSetRootElement(xmldoc, root); |
|---|
| 105 |
|
|---|
| 106 |
/* Fill in the document */ |
|---|
| 107 |
xmlNewTextChild(root, NULL, (xmlChar *)"version", (xmlChar *)vbuff); |
|---|
| 108 |
|
|---|
| 109 |
cmds = xmlNewNode(NULL, (xmlChar *)"services"); |
|---|
| 110 |
xmlAddChild(root, cmds); |
|---|
| 111 |
lc = noit_listener_commands(); |
|---|
| 112 |
while(noit_hash_next(lc, &iter, &k, &klen, &data)) { |
|---|
| 113 |
xmlNodePtr cnode; |
|---|
| 114 |
char hexcode[11]; |
|---|
| 115 |
const char *name; |
|---|
| 116 |
eventer_func_t *f = (eventer_func_t *)k; |
|---|
| 117 |
noit_hash_table *sc = (noit_hash_table *)data; |
|---|
| 118 |
noit_hash_iter sc_iter = NOIT_HASH_ITER_ZERO; |
|---|
| 119 |
const char *sc_k; |
|---|
| 120 |
int sc_klen; |
|---|
| 121 |
void *sc_data; |
|---|
| 122 |
|
|---|
| 123 |
name = eventer_name_for_callback(*f); |
|---|
| 124 |
cnode = xmlNewNode(NULL, (xmlChar *)"service"); |
|---|
| 125 |
xmlSetProp(cnode, (xmlChar *)"name", name ? (xmlChar *)name : NULL); |
|---|
| 126 |
if(*f == ac->dispatch) |
|---|
| 127 |
xmlSetProp(cnode, (xmlChar *)"connected", (xmlChar *)"true"); |
|---|
| 128 |
xmlAddChild(cmds, cnode); |
|---|
| 129 |
while(noit_hash_next(sc, &sc_iter, &sc_k, &sc_klen, &sc_data)) { |
|---|
| 130 |
xmlNodePtr scnode; |
|---|
| 131 |
char *name_copy, *version = NULL; |
|---|
| 132 |
eventer_func_t *f = (eventer_func_t *)sc_data; |
|---|
| 133 |
|
|---|
| 134 |
snprintf(hexcode, sizeof(hexcode), "0x%08x", *((u_int32_t *)sc_k)); |
|---|
| 135 |
name = eventer_name_for_callback(*f); |
|---|
| 136 |
name_copy = strdup(name ? name : "[[unknown]]"); |
|---|
| 137 |
version = strchr(name_copy, '/'); |
|---|
| 138 |
if(version) *version++ = '\0'; |
|---|
| 139 |
|
|---|
| 140 |
scnode = xmlNewNode(NULL, (xmlChar *)"command"); |
|---|
| 141 |
xmlSetProp(scnode, (xmlChar *)"name", (xmlChar *)name_copy); |
|---|
| 142 |
if(version) |
|---|
| 143 |
xmlSetProp(scnode, (xmlChar *)"version", (xmlChar *)version); |
|---|
| 144 |
xmlSetProp(scnode, (xmlChar *)"code", (xmlChar *)hexcode); |
|---|
| 145 |
xmlAddChild(cnode, scnode); |
|---|
| 146 |
free(name_copy); |
|---|
| 147 |
} |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
/* Write it out to a buffer and copy it for writing */ |
|---|
| 151 |
cl->buff = noit_xmlSaveToBuffer(xmldoc); |
|---|
| 152 |
cl->towrite = strlen(cl->buff); |
|---|
| 153 |
|
|---|
| 154 |
/* Clean up after ourselves */ |
|---|
| 155 |
xmlFreeDoc(xmldoc); |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
while(cl->towrite > cl->written) { |
|---|
| 159 |
int len; |
|---|
| 160 |
while((len = e->opset->write(e->fd, cl->buff + cl->written, |
|---|
| 161 |
cl->towrite - cl->written, |
|---|
| 162 |
&newmask, e)) == -1 && errno == EINTR); |
|---|
| 163 |
if(len < 0) { |
|---|
| 164 |
if(errno == EAGAIN) return newmask | EVENTER_EXCEPTION; |
|---|
| 165 |
goto socket_error; |
|---|
| 166 |
} |
|---|
| 167 |
cl->written += len; |
|---|
| 168 |
} |
|---|
| 169 |
goto cleanup_shutdown; |
|---|
| 170 |
|
|---|
| 171 |
return 0; |
|---|
| 172 |
} |
|---|