1 /*	$Id: arms_methods.c 20800 2012-01-19 05:13:45Z m-oki $	*/
2 
3 /*
4  * Copyright (c) 2012, Internet Initiative Japan, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "config.h"
31 
32 #include <stdlib.h>
33 #include <inttypes.h>
34 
35 #include <axp_extern.h>
36 
37 #include <arms_xml_tag.h>
38 #include <libarms/malloc.h>
39 #include <transaction/transaction.h>
40 #include <protocol/arms_methods.h>
41 
42 static arms_method_t *method_tbl = NULL;
43 
44 /*
45  * Generic Praser for start request messages
46  */
47 
48 static int
register_arms_method(arms_method_t * method)49 register_arms_method(arms_method_t *method)
50 {
51 	int count;
52 	int i;
53 	arms_method_t *new_tbl;
54 
55 	if (method == NULL) {
56 		/* error in caller */
57 		return -1;
58 	}
59 
60 	if (method_tbl == NULL) {
61 		/* first entry */
62 		new_tbl = CALLOC(2, sizeof(*new_tbl));
63 		if (new_tbl == NULL) {
64 			return -1;
65 		}
66 		new_tbl[0] = *method;
67 	} else {
68 		count = 0;
69 		while (method_tbl[count].pm_type != 0) {
70 			count++;
71 		}
72 		/* create new buffer */
73 		new_tbl = CALLOC(count + 2, sizeof(*new_tbl));
74 		if (new_tbl == NULL) {
75 			return -1;
76 		}
77 		/* copy buffer */
78 		for (i = 0;  i < count; i++) {
79 			new_tbl[i] = method_tbl[i];
80 		}
81 		new_tbl[count] = *method;
82 		/* replace table */
83 		FREE(method_tbl);
84 	}
85 	method_tbl = new_tbl;
86 
87 	/* update XML parser */
88 	push_add_schema(method->pm_type, method->pm_string,
89 			method->pm_schema);
90 	return 0;
91 }
92 
93 void
free_arms_method_table(void)94 free_arms_method_table(void)
95 {
96 	FREE(method_tbl);
97 }
98 
99 arms_method_t *
type2method(uint32_t type)100 type2method(uint32_t type)
101 {
102 	arms_method_t *method = method_tbl;
103 	int found = 0;
104 
105 	if (method == NULL) {
106 		return NULL;
107 	}
108 
109 	while (method->pm_type != 0) {
110 		if (method->pm_type == type) {
111 			found = 1;
112 			break;
113 		}
114 		method++;
115 	}
116 
117 	if (!found) {
118 		return NULL;
119 	}
120 
121 	return method;
122 }
123 
124 void
arms_method_init(void)125 arms_method_init(void)
126 {
127 	/* generic error methods */
128 	register_arms_method(&generic_error_methods);
129 
130 #if 0 /* pull method is no need to registration.  (e.g. LS pull and RS pull) */
131 	/* pull methods */
132 	register_arms_method(&push_ready_methods);
133 	register_arms_method(&method_query_methods);
134 #endif
135 
136 	/* push methods */
137 	register_arms_method(&confirm_done_methods);
138 	register_arms_method(&check_transaction_methods);
139 	register_arms_method(&clear_status_methods);
140 	register_arms_method(&configure_methods);
141 	register_arms_method(&dump_debug_methods);
142 	register_arms_method(&md_command_methods);
143 	register_arms_method(&read_module_list_methods);
144 	register_arms_method(&read_status_methods);
145 	register_arms_method(&read_storage_methods);
146 	register_arms_method(&reboot_methods);
147 	register_arms_method(&ping_methods);
148 	register_arms_method(&pull_config_methods);
149 	register_arms_method(&traceroute_methods);
150 }
151