da_conf.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <sys/socket.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <inttypes.h>
  20. #include <sys/types.h>
  21. #include <netinet/in.h>
  22. #include <arpa/inet.h>
  23. #include "da_log.h"
  24. #include "da_conf.h"
  25. #include "da_util.h"
  26. #include "da_top_percentile.h"
  27. #define DEFINE_ACTION(_hash, _name) string(#_name),
  28. static struct string hash_strings[] = {
  29. HASH_CODEC( DEFINE_ACTION )
  30. null_string };
  31. #undef DEFINE_ACTION
  32. #define DEFINE_ACTION(_hash, _name) hash_##_name,
  33. static hash_t hash_algos[] = { HASH_CODEC(DEFINE_ACTION)
  34. NULL };
  35. #undef DEFINE_ACTION
  36. static struct command conf_commands[] = {
  37. { string("Mid"), conf_set_num, offsetof(struct conf_pool, mid) },
  38. { string("AccessToken"), conf_set_string, offsetof(struct conf_pool,accesskey) },
  39. { string("ListenOn"), conf_set_listen, offsetof(struct conf_pool, listen) },
  40. { string("Backlog"), conf_set_num, offsetof(struct conf_pool, backlog) },
  41. { string("Client_Connections"), conf_set_num, offsetof(struct conf_pool,client_connections) },
  42. { string("Preconnect"), conf_set_bool, offsetof(struct conf_pool, preconnect) },
  43. { string("Server_Connections"), conf_set_num, offsetof(struct conf_pool,server_connections) },
  44. { string("Hash"), conf_set_hash, offsetof(struct conf_pool, hash) },
  45. { string("Timeout"), conf_set_num, offsetof(struct conf_pool, timeout) },
  46. { string("ReplicaEnable"), conf_set_bool, offsetof(struct conf_pool, replica_enable) },
  47. { string("ModuleIDC"), conf_set_string, offsetof(struct conf_pool, idc) },
  48. { string("MainReport"), conf_set_bool, offsetof(struct conf_pool, main_report) },
  49. { string("InstanceReport"), conf_set_bool, offsetof(struct conf_pool, instance_report) },
  50. { string("AutoRemoveReplica"), conf_set_bool, offsetof(struct conf_pool, auto_remove_replica) },
  51. { string("TopPercentileEnable"), conf_set_bool, offsetof(struct conf_pool, top_percentile_enable) },
  52. { string("TopPercentileDomain"), conf_set_string, offsetof(struct conf_pool, top_percentile_domain) },
  53. { string("TopPercentilePort"), conf_set_num, offsetof(struct conf_pool, top_percentile_port) },
  54. //For Sharding
  55. { string("ShardingReplicaEnable"), conf_set_bool, offsetof(struct conf_server, replica_enable) },
  56. { string("ShardingName"), conf_set_string, offsetof(struct conf_server, name) },
  57. //instance parameter
  58. { string("Role"), conf_set_string, offsetof(struct conf_instance, role) },
  59. { string("Enable"), conf_set_bool, offsetof(struct conf_instance, enable) },
  60. { string("Addr"), conf_set_addr, offsetof(struct conf_instance, addr) },
  61. { string("idc"), conf_set_string, offsetof(struct conf_instance, idc) },
  62. //for log
  63. { string("LogSwitch"), conf_set_num, offsetof(struct conf_log, log_switch) },
  64. { string("RemoteLogSwitch"), conf_set_num, offsetof(struct conf_log, remote_log_switch) },
  65. { string("RemoteLogIP"), conf_set_string, offsetof(struct conf_log, remote_log_ip) },
  66. { string("RemoteLogPort"), conf_set_num, offsetof(struct conf_log, remote_log_port) },
  67. null_command };
  68. static char *svrpool_elem[] = {
  69. "Name",//name
  70. "Mid",
  71. "AccessToken", //accesstoken
  72. "ListenOn", //listen address
  73. "Backlog",
  74. "Client_Connections",
  75. "Preconnect",
  76. "Server_Connections",
  77. "Hash",
  78. "Timeout",
  79. "ReplicaEnable",
  80. "ModuleIDC",
  81. "MainReport",
  82. "InstanceReport",
  83. "AutoRemoveReplica",
  84. "TopPercentileEnable",
  85. "TopPercentileDomain",
  86. "TopPercentilePort",
  87. };
  88. static char *server_elem[] = {
  89. "ShardingReplicaEnable",
  90. "ShardingName", //Server name
  91. };
  92. static char *instance_elem[] = {
  93. "Addr",
  94. "idc",
  95. "Role",
  96. "Enable",
  97. };
  98. static char *log_elem[] = {
  99. "LogSwitch",
  100. "RemoteLogSwitch",
  101. "RemoteLogIP",
  102. "RemoteLogPort",
  103. };
  104. static void conf_instance_init(struct conf_instance *ci) {
  105. string_init(&ci->addr);
  106. string_init(&ci->idc);
  107. ci->port = CONF_UNSET_NUM;
  108. string_init(&ci->role);
  109. ci->weight = CONF_UNSET_NUM;
  110. ci->enable = CONF_UNSET_NUM;
  111. ci->valid = 1;
  112. log_debug("init conf instance %p", ci);
  113. }
  114. static void conf_instance_deinit(struct conf_instance *ci) {
  115. string_deinit(&ci->addr);
  116. string_deinit(&ci->idc);
  117. string_deinit(&ci->role);
  118. ci->valid = 0;
  119. log_debug("deinit conf instance %p", ci);
  120. }
  121. static int conf_server_init(struct conf_server *cs) {
  122. string_init(&cs->name);
  123. string_init(&cs->idc);
  124. int status;
  125. cs->replica_enable = CONF_UNSET_NUM;
  126. status = array_init(&cs->instance, CONF_DEFAULT_INSTANCE, sizeof(struct conf_instance));
  127. if (status != 0) {
  128. string_deinit(&cs->name);
  129. return -1;
  130. }
  131. cs->valid = 0;
  132. log_debug("init conf server %p", cs);
  133. return 0;
  134. }
  135. static void conf_server_deinit(struct conf_server *cs) {
  136. string_deinit(&cs->name);
  137. string_deinit(&cs->idc);
  138. while (array_n(&cs->instance) != 0) {
  139. conf_instance_deinit(array_pop(&cs->instance));
  140. }
  141. array_deinit(&cs->instance);
  142. cs->valid = 0;
  143. log_debug("deinit conf server %p", cs);
  144. }
  145. int conf_instance_transform(void *elem,void *data) {
  146. struct conf_instance *ci = elem;
  147. struct cache_instance *i = data;
  148. i->addrlen = ci->info.addrlen;
  149. i->family = ci->info.family;
  150. i->port = ci->port;
  151. i->weight = ci->weight;
  152. i->addr = (struct sockaddr *) &ci->info.addr.in;
  153. string_copy(&i->pname, ci->addr.data, ci->addr.len);
  154. return 0;
  155. }
  156. int conf_server_each_transform(void *elem, void *data) {
  157. struct conf_server *cs = elem;
  158. struct conf_instance *ci;
  159. struct array *server = data;
  160. struct server *s;
  161. struct cache_instance *cachei = NULL;
  162. int ninstance, status, i, cnt;
  163. const char *master_char = "master";
  164. struct string master_str;
  165. string_copy(&master_str,(uint8_t *) master_char, strlen(master_char));
  166. ASSERT(cs->valid);
  167. s = array_push(server);
  168. ASSERT(s != NULL);
  169. ninstance = array_n(&cs->instance);
  170. s->idx = array_idx(server, s);
  171. s->owner = NULL;
  172. string_copy(&s->name, cs->name.data, cs->name.len);
  173. s->weight = 1;
  174. s->replica_enable = cs->replica_enable;
  175. status = array_init(&s->high_ptry_ins, ninstance, sizeof(struct cache_instance));
  176. if (status != 0)
  177. {
  178. return -1;
  179. log_error("transform from conf to server error : can not calloc mem for instance");
  180. }
  181. status = array_init(&s->low_prty_ins, ninstance, sizeof(struct cache_instance));
  182. if (status != 0)
  183. {
  184. array_deinit(&s->high_ptry_ins);
  185. log_error("transform from conf to server error : can not calloc mem for instance");
  186. return -1;
  187. }
  188. //for every conf instance, transform to cache_instance
  189. cnt = 1;
  190. for (i = 0; i < ninstance; i++)
  191. {
  192. ci = array_get(&cs->instance, i);
  193. ASSERT(ci != NULL);
  194. if (string_compare(&ci->role, &master_str) == 0 && ci->enable != 0) {
  195. if (string_compare(&ci->idc, &cs->idc) == 0) {
  196. cachei = array_push(&s->high_ptry_ins);
  197. }
  198. else {
  199. cachei = array_push(&s->low_prty_ins);
  200. }
  201. cachei->idx = 0;
  202. s->master = cachei;
  203. }
  204. else if (string_compare(&ci->role, &master_str) == 0 && ci->enable == 0){
  205. log_error("master instance should be enable");
  206. return -1;
  207. }
  208. else if (cs->replica_enable != 0 && ci->enable != 0 && string_compare(&ci->idc, &cs->idc) == 0) {
  209. cachei = array_push(&s->high_ptry_ins);
  210. cachei->idx = cnt++;
  211. }
  212. else if (cs->replica_enable != 0 && ci->enable != 0 && string_compare(&ci->idc, &cs->idc) != 0 ) {
  213. cachei = array_push(&s->low_prty_ins);
  214. cachei->idx = cnt++;
  215. }
  216. else{
  217. continue;
  218. }
  219. conf_instance_transform(ci, cachei);
  220. cachei->owner = s;
  221. cachei->ns_conn_q = 0;
  222. cachei->nerr = 0;
  223. cachei->failure_num = 0;
  224. cachei->num = 0;
  225. TAILQ_INIT(&cachei->s_conn_q);
  226. }
  227. s->high_prty_idx = 0;
  228. s->high_prty_cnt = 0;
  229. s->low_prty_idx = 0;
  230. s->low_prty_cnt = 0;
  231. log_debug("transform to server %"PRIu32" '%.*s'", s->idx, s->name.len,
  232. s->name.data);
  233. return 0;
  234. }
  235. static int conf_pool_init(struct conf_pool *cp, struct string *name) {
  236. int status;
  237. string_init(&cp->name);
  238. string_init(&cp->accesskey);
  239. string_init(&cp->top_percentile_domain);
  240. string_init(&cp->listen.pname);
  241. string_init(&cp->listen.name);
  242. cp->listen.port = 0;
  243. memset(&cp->listen.info, 0, sizeof(cp->listen.info));
  244. cp->listen.valid = 0;
  245. cp->hash = CONF_UNSET_HASH;
  246. cp->mid = CONF_UNSET_NUM;
  247. cp->timeout = CONF_UNSET_NUM;
  248. cp->backlog = CONF_UNSET_NUM;
  249. cp->client_connections = CONF_UNSET_NUM;
  250. cp->preconnect = CONF_UNSET_NUM;
  251. cp->server_connections = CONF_UNSET_NUM;
  252. array_null(&cp->server);
  253. cp->replica_enable = CONF_UNSET_NUM;
  254. cp->main_report = CONF_UNSET_NUM;
  255. cp->instance_report = CONF_UNSET_NUM;
  256. cp->auto_remove_replica = CONF_UNSET_NUM;
  257. cp->top_percentile_enable = CONF_UNSET_NUM;
  258. cp->top_percentile_port = CONF_UNSET_NUM;
  259. string_init(&cp->idc);
  260. cp->valid = 0;
  261. status = string_duplicate(&cp->name, name);
  262. if (status != 0) {
  263. return status;
  264. }
  265. status = array_init(&cp->server, CONF_DEFAULT_SERVERS,
  266. sizeof(struct conf_server));
  267. if (status != 0) {
  268. string_deinit(&cp->name);
  269. return status;
  270. }
  271. log_debug("init conf pool %p, '%.*s'", cp, name->len, name->data);
  272. return 0;
  273. }
  274. static void conf_pool_deinit(struct conf_pool *cp) {
  275. string_deinit(&cp->name);
  276. string_deinit(&cp->listen.pname);
  277. string_deinit(&cp->listen.name);
  278. string_deinit(&cp->idc);
  279. string_deinit(&cp->top_percentile_domain);
  280. while (array_n(&cp->server) != 0) {
  281. conf_server_deinit(array_pop(&cp->server));
  282. }
  283. array_deinit(&cp->server);
  284. log_debug("deinit conf pool %p", cp);
  285. }
  286. static void conf_log_init(struct conf_log *cl){
  287. cl->log_switch = CONF_UNSET_NUM;
  288. cl->remote_log_switch = CONF_UNSET_NUM;
  289. cl->remote_log_port = CONF_UNSET_NUM;
  290. string_init(&cl->remote_log_ip);
  291. }
  292. static void conf_log_deinit(struct conf_log *cl){
  293. string_deinit(&cl->remote_log_ip);
  294. log_debug("deinit conf log %p", cl);
  295. }
  296. int conf_pool_each_transform(void *elem, void *data) {
  297. int status;
  298. struct conf_pool *cp = elem;
  299. struct array *server_pool = data;
  300. struct server_pool *sp;
  301. ASSERT(cp->valid);
  302. sp = array_push(server_pool);
  303. ASSERT(sp != NULL);
  304. sp->idx = array_idx(server_pool, sp);
  305. sp->mid = cp->mid;
  306. sp->ctx = NULL;
  307. sp->listener = NULL;
  308. sp->c_conn_count = 0;
  309. TAILQ_INIT(&sp->c_conn_q);
  310. array_null(&sp->server);
  311. sp->ncontinuum = 0;
  312. sp->nserver_continuum = 0;
  313. sp->continuum = NULL;
  314. string_copy(&sp->name, cp->name.data, cp->name.len);
  315. string_copy(&sp->accesskey, cp->accesskey.data, cp->accesskey.len);
  316. string_copy(&sp->addrstr, cp->listen.pname.data, cp->listen.pname.len);
  317. sp->port = (uint16_t) cp->listen.port;
  318. sp->family = cp->listen.info.family;
  319. sp->addrlen = cp->listen.info.addrlen;
  320. sp->addr = (struct sockaddr *) &cp->listen.info.addr;
  321. sp->key_hash_type = cp->hash;
  322. sp->key_hash = hash_algos[cp->hash];
  323. sp->backlog = cp->backlog;
  324. sp->timeout = cp->timeout;
  325. sp->client_connections = (uint32_t) cp->client_connections;
  326. sp->server_connections = (uint32_t) cp->server_connections;
  327. sp->preconnect = cp->preconnect ? 1 : 0;
  328. sp->replica_enable = cp->replica_enable ? 1 : 0;
  329. string_copy(&sp->module_idc, cp->idc.data, cp->idc.len);
  330. sp->main_report = cp->main_report;
  331. sp->instance_report = cp->instance_report;
  332. sp->auto_remove_replica = cp->auto_remove_replica;
  333. sp->top_percentile_enable = cp->top_percentile_enable;
  334. sp->top_percentile_fd = set_remote_fd();
  335. sp->top_percentile_addr_len = set_remote_config((char *)cp->top_percentile_domain.data, (uint16_t)cp->top_percentile_port, &sp->top_percentile_addr);
  336. if(0 == sp->top_percentile_addr_len)
  337. sp->top_percentile_addr_len = sizeof(sp->top_percentile_addr);
  338. else
  339. sp->top_percentile_addr_len = 0;
  340. sp->top_percentile_param = malloc(sizeof(struct remote_param) * ((int8_t)RT_MAX - 1));
  341. if(sp->top_percentile_param)
  342. {
  343. char szBid[16];
  344. snprintf(szBid, 9, "%s", sp->accesskey.data);
  345. uint64_t uBid = (uint64_t)(atoi(szBid));
  346. uint64_t uIP = (uint64_t)(ntohl(inet_addr(cp->localip)));
  347. uint64_t uConstValue = 1;
  348. set_remote_param((uBid << 32) | uIP, (uConstValue << 32) | sp->port, RT_SHARDING, sp->top_percentile_param);
  349. set_remote_param(uBid, 1, RT_ALL, sp->top_percentile_param);
  350. }
  351. status = server_init(&sp->server, &cp->server, sp);
  352. if (status != 0) {
  353. return status;
  354. }
  355. log_debug("transform to pool %"PRIu32" '%.*s'", sp->idx, sp->name.len,
  356. sp->name.data);
  357. return 0;
  358. }
  359. static struct conf *conf_open(char *filename) {
  360. int status;
  361. struct conf *cf;
  362. FILE *fh;
  363. fh = fopen(filename, "r");
  364. if (fh == NULL) {
  365. log_error("conf: failed to open configuration '%s': %s", filename,
  366. strerror(errno));
  367. return NULL;
  368. }
  369. cf = malloc(sizeof(*cf));
  370. if (cf == NULL) {
  371. fclose(fh);
  372. return NULL;
  373. }
  374. string_init(&cf->arg);
  375. status = array_init(&cf->pool, CONF_DEFAULT_POOL, sizeof(struct conf_pool));
  376. if (status != 0) {
  377. string_deinit(&cf->arg);
  378. free(cf);
  379. fclose(fh);
  380. return NULL;
  381. }
  382. cf->fname = filename;
  383. cf->fh = fh;
  384. cf->tree = NULL;
  385. /* parser, event, and token are initialized later */
  386. cf->parsed = 0;
  387. cf->valid = 0;
  388. log_debug("opened conf '%s'", filename);
  389. return cf;
  390. }
  391. static int conf_begin_parse(struct conf *cf) {
  392. ASSERT(!cf->parsed);
  393. cf->tree = mxmlLoadFile(NULL, cf->fh, MXML_TEXT_CALLBACK);
  394. if (cf->tree == NULL) {
  395. return -1;
  396. }
  397. return 0;
  398. }
  399. static int conf_handler(char *key, struct conf *cf, void *data) {
  400. int status;
  401. struct command *cmd;
  402. if (da_strncmp(key,"Name",da_strlen(key)) == 0) {
  403. return conf_pool_init(data, &cf->arg);
  404. }
  405. struct string key_str;
  406. status = string_copy(&key_str, (uint8_t *) key, da_strlen(key));
  407. {
  408. if (status < 0) {
  409. log_debug("no memory when parse xml");
  410. return -1;
  411. }
  412. }
  413. for (cmd = conf_commands; cmd->name.len != 0; cmd++) {
  414. char *rv;
  415. if (string_compare(&key_str, &cmd->name) != 0) {
  416. continue;
  417. }
  418. rv = cmd->set(cf, cmd, data);
  419. if (rv != CONF_OK) {
  420. log_error("conf: directive %s %s", key, rv);
  421. return -1;
  422. }
  423. return 0;
  424. }
  425. log_error("conf: directive %s is unknown", key);
  426. return -1;
  427. }
  428. static int conf_parse_core(struct conf *cf) {
  429. ASSERT(!cf->parsed);ASSERT(cf->tree !=NULL);
  430. int status, i;
  431. void *void_cp;
  432. void *void_cs;
  433. void *void_ci;
  434. void *void_cl;
  435. mxml_node_t *poolnode, *servernode, *instancenode, *lognode;
  436. for (poolnode = mxmlFindElement(cf->tree, cf->tree, "MODULE",
  437. NULL, NULL, MXML_DESCEND); poolnode != NULL;
  438. poolnode = mxmlFindElement(poolnode, cf->tree, "MODULE",
  439. NULL, NULL, MXML_DESCEND)) {
  440. // get a conf_pool
  441. void_cp = array_push(&cf->pool);
  442. if (void_cp == NULL) {
  443. return -1;
  444. }
  445. //转存一下localip到conf_pool中,后续server_pool需要使用
  446. struct conf_pool *tmpCP = (struct conf_pool *)void_cp;
  447. strncpy(tmpCP->localip, cf->localip, sizeof(tmpCP->localip));
  448. int sz = sizeof(svrpool_elem) / sizeof(svrpool_elem[0]);
  449. for (i = 0; i < sz; i++) {
  450. char *argment = (char *) mxmlElementGetAttr(poolnode,
  451. svrpool_elem[i]);
  452. if (argment == NULL) {
  453. log_error("get %s from conf '%s' error", svrpool_elem[i],
  454. cf->fname);
  455. return -1;
  456. }
  457. status = string_copy(&cf->arg, (uint8_t *) argment,
  458. da_strlen(argment));
  459. if (status < 0) {
  460. return status;
  461. }
  462. status = conf_handler(svrpool_elem[i], cf, void_cp);
  463. if (status < 0) {
  464. return status;
  465. }
  466. string_deinit(&cf->arg);
  467. }
  468. for (servernode = mxmlFindElement(poolnode, poolnode, "CACHESHARDING",
  469. NULL, NULL, MXML_DESCEND); servernode != NULL; servernode =
  470. mxmlFindElement(servernode, poolnode, "CACHESHARDING",
  471. NULL, NULL, MXML_DESCEND)) {
  472. struct conf_pool *cp = (struct conf_pool*) void_cp;
  473. void_cs = array_push(&cp->server);
  474. if (void_cs == NULL)
  475. {
  476. return -1;
  477. }
  478. status = conf_server_init(void_cs);
  479. if (status != 0)
  480. {
  481. array_pop(&cp->server);
  482. return -1;
  483. }
  484. int sz = sizeof(server_elem) / sizeof(server_elem[0]);
  485. for (i = 0; i < sz; i++) {
  486. char *argment = (char *) mxmlElementGetAttr(servernode,
  487. server_elem[i]);
  488. if (argment == NULL) {
  489. log_error("get %s from conf '%s' error", server_elem[i],
  490. cf->fname);
  491. array_pop(&cp->server);
  492. conf_server_deinit(void_cs);
  493. return -1;
  494. }
  495. status = string_copy(&cf->arg, (uint8_t *) argment,
  496. da_strlen(argment));
  497. if (status < 0) {
  498. array_pop(&cp->server);
  499. conf_server_deinit(void_cs);
  500. return status;
  501. }
  502. status = conf_handler(server_elem[i], cf, void_cs);
  503. if (status < 0) {
  504. array_pop(&cp->server);
  505. conf_server_deinit(void_cs);
  506. return status;
  507. }
  508. string_deinit(&cf->arg);
  509. }
  510. for (instancenode = mxmlFindElement(servernode, servernode, "INSTANCE",
  511. NULL, NULL, MXML_DESCEND); instancenode != NULL; instancenode =
  512. mxmlFindElement(instancenode, servernode, "INSTANCE",
  513. NULL, NULL, MXML_DESCEND)) {
  514. struct conf_server *cs = (struct conf_server *)void_cs;
  515. string_copy(&cs->idc, cp->idc.data, cp->idc.len);
  516. void_ci = array_push(&cs->instance);
  517. if (void_ci == NULL) {
  518. return -1;
  519. }
  520. conf_instance_init(void_ci);
  521. int sz = sizeof(instance_elem) / sizeof(instance_elem[0]);
  522. for (i = 0; i < sz; i++) {
  523. char *argment = (char *)mxmlElementGetAttr(instancenode,
  524. instance_elem[i]);
  525. if (argment == NULL) {
  526. array_pop(&cs->instance);
  527. conf_instance_deinit(void_ci);
  528. log_error("get %s from conf '%s' error", server_elem[i],
  529. cf->fname);
  530. return -1;
  531. }
  532. status = string_copy(&cf->arg, (uint8_t *)argment,
  533. da_strlen(argment));
  534. if (status < 0) {
  535. array_pop(&cs->instance);
  536. conf_instance_deinit(void_ci);
  537. return status;
  538. }
  539. status = conf_handler(instance_elem[i], cf, void_ci);
  540. if (status < 0) {
  541. array_pop(&cs->instance);
  542. conf_instance_deinit(void_ci);
  543. return status;
  544. }
  545. string_deinit(&cf->arg);
  546. }
  547. }
  548. }
  549. }
  550. lognode = mxmlFindElement(cf->tree, cf->tree, "LOG_MODULE", NULL, NULL, MXML_DESCEND);
  551. void_cl = (void *)&cf->stCL;
  552. conf_log_init(void_cl);
  553. int lnSize = sizeof(log_elem) / sizeof(log_elem[0]);
  554. for(i = 0; i < lnSize; ++ i)
  555. {
  556. char *argment = (char *)mxmlElementGetAttr(lognode, log_elem[i]);
  557. if(NULL == argment)
  558. {
  559. log_error("get %s from conf '%s' error", log_elem[i], cf->fname);
  560. continue;
  561. }
  562. status = string_copy(&cf->arg, (uint8_t *)argment, da_strlen(argment));
  563. if(status < 0)
  564. return status;
  565. status = conf_handler(log_elem[i], cf, void_cl);
  566. if(status < 0)
  567. return status;
  568. string_deinit(&cf->arg);
  569. }
  570. return 0;
  571. }
  572. char* arg_module[] = {"shardingsphere", "999", "0000", "127.0.0.1:12999", "500", "900", "true", "1", "chash", "3000", "true",
  573. "LF", "false", "false", "true", "false", "127.0.0.1", "20020"};
  574. char* cachesharding_module[] = {"false", "ss"};
  575. char* instance_module[] = {"", "LF", "master", "true"};
  576. static int conf_add_ss(struct conf *cf) {
  577. int status, i;
  578. void *void_cp;
  579. void *void_cs;
  580. void *void_ci;
  581. void *void_cl;
  582. // get a conf_pool
  583. void_cp = array_push(&cf->pool);
  584. if (void_cp == NULL) {
  585. return -1;
  586. }
  587. //转存一下localip到conf_pool中,后续server_pool需要使用
  588. struct conf_pool *tmpCP = (struct conf_pool *)void_cp;
  589. strncpy(tmpCP->localip, cf->localip, sizeof(tmpCP->localip));
  590. // MODULE
  591. int sz = sizeof(svrpool_elem) / sizeof(svrpool_elem[0]);
  592. for (i = 0; i < sz; i++) {
  593. char *argment = arg_module[i];
  594. if (argment == NULL) {
  595. log_error("get %s from conf '%s' error", svrpool_elem[i],
  596. cf->fname);
  597. return -1;
  598. }
  599. status = string_copy(&cf->arg, (uint8_t *) argment,
  600. da_strlen(argment));
  601. if (status < 0) {
  602. return status;
  603. }
  604. status = conf_handler(svrpool_elem[i], cf, void_cp);
  605. if (status < 0) {
  606. return status;
  607. }
  608. string_deinit(&cf->arg);
  609. }
  610. // CACHESHARDING
  611. struct conf_pool *cp = (struct conf_pool*) void_cp;
  612. void_cs = array_push(&cp->server);
  613. if (void_cs == NULL)
  614. {
  615. return -1;
  616. }
  617. status = conf_server_init(void_cs);
  618. if (status != 0)
  619. {
  620. array_pop(&cp->server);
  621. return -1;
  622. }
  623. sz = sizeof(server_elem) / sizeof(server_elem[0]);
  624. for (i = 0; i < sz; i++) {
  625. char *argment = cachesharding_module[i];
  626. if (argment == NULL) {
  627. log_error("get %s from conf '%s' error", server_elem[i],
  628. cf->fname);
  629. array_pop(&cp->server);
  630. conf_server_deinit(void_cs);
  631. return -1;
  632. }
  633. status = string_copy(&cf->arg, (uint8_t *) argment,
  634. da_strlen(argment));
  635. if (status < 0) {
  636. array_pop(&cp->server);
  637. conf_server_deinit(void_cs);
  638. return status;
  639. }
  640. status = conf_handler(server_elem[i], cf, void_cs);
  641. if (status < 0) {
  642. array_pop(&cp->server);
  643. conf_server_deinit(void_cs);
  644. return status;
  645. }
  646. string_deinit(&cf->arg);
  647. }
  648. // INSTANCE
  649. struct conf_server *cs = (struct conf_server *)void_cs;
  650. char ssaddr[250] = {0};
  651. string_copy(&cs->idc, cp->idc.data, cp->idc.len);
  652. void_ci = array_push(&cs->instance);
  653. if (void_ci == NULL) {
  654. return -1;
  655. }
  656. conf_instance_init(void_ci);
  657. sz = sizeof(instance_elem) / sizeof(instance_elem[0]);
  658. for (i = 0; i < sz; i++) {
  659. char *argment = NULL;
  660. if(i == 0)
  661. {
  662. mxml_node_t* ssnode = mxmlFindElement(cf->tree, cf->tree, "SS_MODULE", NULL, NULL, MXML_DESCEND);
  663. char *ssport = (char *)mxmlElementGetAttr(ssnode, "port");
  664. if(NULL == ssport)
  665. {
  666. log_error("get ssport from conf SS_MODULE error");
  667. continue;
  668. }
  669. sprintf(ssaddr, "127.0.0.1:%s:1", ssport);
  670. argment = ssaddr;
  671. }
  672. else
  673. {
  674. argment = (char *)instance_module[i];
  675. }
  676. if (argment == NULL) {
  677. array_pop(&cs->instance);
  678. conf_instance_deinit(void_ci);
  679. log_error("get %s from conf '%s' error", server_elem[i],
  680. cf->fname);
  681. return -1;
  682. }
  683. status = string_copy(&cf->arg, (uint8_t *)argment,
  684. da_strlen(argment));
  685. if (status < 0) {
  686. array_pop(&cs->instance);
  687. conf_instance_deinit(void_ci);
  688. return status;
  689. }
  690. status = conf_handler(instance_elem[i], cf, void_ci);
  691. if (status < 0) {
  692. array_pop(&cs->instance);
  693. conf_instance_deinit(void_ci);
  694. return status;
  695. }
  696. string_deinit(&cf->arg);
  697. }
  698. return 0;
  699. }
  700. static int conf_end_parse(struct conf *cf) {
  701. mxmlDelete(cf->tree);
  702. return 0;
  703. }
  704. static int conf_parse(struct conf *cf) {
  705. int status;
  706. ASSERT(!cf->parsed);ASSERT(array_n(&cf->arg) == 0);
  707. status = conf_begin_parse(cf);
  708. if (status != 0) {
  709. return status;
  710. }
  711. status = conf_parse_core(cf);
  712. if (status != 0) {
  713. return status;
  714. }
  715. status = conf_add_ss(cf);
  716. if (status != 0) {
  717. return status;
  718. }
  719. status = conf_end_parse(cf);
  720. {
  721. if (status != 0) {
  722. return status;
  723. }
  724. }
  725. cf->parsed = 1;
  726. return 0;
  727. }
  728. void conf_destroy(struct conf *cf) {
  729. string_deinit(&cf->arg);
  730. conf_log_deinit(&cf->stCL);
  731. while (array_n(&cf->pool) != 0) {
  732. conf_pool_deinit(array_pop(&cf->pool));
  733. }
  734. array_deinit(&cf->pool);
  735. free(cf);
  736. }
  737. static int conf_server_name_cmp(const void *t1, const void *t2) {
  738. const struct conf_server *s1 = t1, *s2 = t2;
  739. return string_compare(&s1->name, &s2->name);
  740. }
  741. static int conf_pool_listen_cmp(const void *t1, const void *t2) {
  742. const struct conf_pool *p1 = t1, *p2 = t2;
  743. return string_compare(&p1->listen.pname, &p2->listen.pname);
  744. }
  745. static int conf_pool_name_cmp(const void *t1, const void *t2) {
  746. const struct conf_pool *p1 = t1, *p2 = t2;
  747. return string_compare(&p1->name, &p2->name);
  748. }
  749. static int conf_pool_mid_cmp(const void *t1, const void *t2) {
  750. const struct conf_pool *p1 = t1, *p2 = t2;
  751. return p1->mid >= p2->mid;
  752. }
  753. static int conf_validate_server(struct conf *cf, struct conf_pool *cp) {
  754. uint32_t i, nserver;
  755. bool valid;
  756. nserver = array_n(&cp->server);
  757. if (nserver == 0) {
  758. log_alert("conf: pool '%.*s' has no servers", cp->name.len,
  759. cp->name.data);
  760. return 0;
  761. }
  762. array_sort(&cp->server, conf_server_name_cmp);
  763. for (valid = true, i = 0; i < nserver - 1; i++) {
  764. struct conf_server *cs1, *cs2;
  765. cs1 = array_get(&cp->server, i);
  766. cs2 = array_get(&cp->server, i + 1);
  767. if (string_compare(&cs1->name, &cs2->name) == 0) {
  768. log_error("conf: pool '%.*s' has servers with same name '%.*s'",
  769. cp->name.len, cp->name.data, cs1->name.len, cs1->name.data);
  770. valid = false;
  771. break;
  772. }
  773. }
  774. if (!valid) {
  775. return -1;
  776. }
  777. return 0;
  778. }
  779. static int conf_validate_pool(struct conf *cf, struct conf_pool *cp) {
  780. int status;
  781. ASSERT(!cp->valid);
  782. ASSERT(!string_empty(&cp->name));
  783. if (!cp->listen.valid) {
  784. log_error("conf: directive \"listen:\" is missing");
  785. return -1;
  786. }
  787. if (cp->hash == CONF_UNSET_HASH) {
  788. cp->hash = CONF_DEFAULT_HASH;
  789. }
  790. if (cp->timeout == CONF_UNSET_NUM) {
  791. cp->timeout = CONF_DEFAULT_TIMEOUT;
  792. }
  793. if (cp->backlog == CONF_UNSET_NUM) {
  794. cp->backlog = CONF_DEFAULT_LISTEN_BACKLOG;
  795. }
  796. if (cp->client_connections == CONF_UNSET_NUM) {
  797. cp->client_connections = CONF_DEFAULT_CLIENT_CONNECTIONS;
  798. }
  799. if (cp->preconnect == CONF_UNSET_NUM) {
  800. cp->preconnect = CONF_DEFAULT_PRECONNECT;
  801. }
  802. if (cp->top_percentile_enable == CONF_UNSET_NUM){
  803. cp->top_percentile_enable = CONF_DEFAULT_TOP_PERCENTILE_ENABLE;
  804. }
  805. if (string_empty(&cp->top_percentile_domain)){
  806. string_copy(&cp->top_percentile_domain, (const uint8_t *)(CONF_DEFAULT_TOP_PERCENTILE_DOMAIN), sizeof(CONF_DEFAULT_TOP_PERCENTILE_DOMAIN));
  807. }
  808. if (cp->top_percentile_port == CONF_UNSET_NUM){
  809. cp->top_percentile_port = CONF_DEFAULT_TOP_PERCENTILE_PORT;
  810. }
  811. if (cp->server_connections == CONF_UNSET_NUM) {
  812. cp->server_connections = CONF_DEFAULT_SERVER_CONNECTIONS;
  813. } else if (cp->server_connections == 0) {
  814. log_error("conf: directive \"server_connections:\" cannot be 0");
  815. return -1;
  816. }
  817. status = conf_validate_server(cf, cp);
  818. if (status != 0) {
  819. return status;
  820. }
  821. cp->valid = 1;
  822. return 0;
  823. }
  824. static int conf_validate_log(struct conf_log *cl)
  825. {
  826. if(cl->log_switch == CONF_UNSET_NUM)
  827. cl->log_switch = CONF_DEFAULT_LOG_SWITCH;
  828. if(cl->remote_log_switch == CONF_UNSET_NUM)
  829. cl->remote_log_switch = CONF_DEFAULT_REMOTE_LOG_SWITCH;
  830. if(cl->remote_log_port == CONF_UNSET_NUM)
  831. cl->remote_log_port = CONF_DEFAULT_REMOTE_LOG_PORT;
  832. if(string_empty(&cl->remote_log_ip))
  833. {
  834. int status = string_copy(&cl->remote_log_ip, (const uint8_t *)(CONF_DEFAULT_REMOTE_LOG_IP), sizeof(CONF_DEFAULT_REMOTE_LOG_IP));
  835. if(status != 0)
  836. return -1;
  837. }
  838. return 0;
  839. }
  840. static int conf_validate(struct conf *cf) {
  841. int status;
  842. uint32_t i, npool;
  843. bool valid;
  844. ASSERT(cf->parsed);
  845. ASSERT(!cf->valid);
  846. npool = array_n(&cf->pool);
  847. //has no pool in conf
  848. if (npool == 0) {
  849. log_alert("conf: %s has no pools", cf->fname);
  850. return 0;
  851. }
  852. /* validate pool */
  853. for (i = 0; i < npool; i++) {
  854. struct conf_pool *cp = array_get(&cf->pool, i);
  855. status = conf_validate_pool(cf, cp);
  856. if (status != 0) {
  857. return status;
  858. }
  859. }
  860. /* validate log*/
  861. status = conf_validate_log(&cf->stCL);
  862. if(status != 0)
  863. return status;
  864. array_sort(&cf->pool, conf_pool_mid_cmp);
  865. for (valid = true, i = 0; i < npool - 1; i++) {
  866. struct conf_pool *p1, *p2;
  867. p1 = array_get(&cf->pool, i);
  868. p2 = array_get(&cf->pool, i + 1);
  869. if (p1->mid == p2->mid) {
  870. log_error("conf: pools '%.*s' and '%.*s' have the same mid:%d "
  871. , p1->name.len, p1->name.data, p2->name.len,
  872. p2->name.data, p1->mid);
  873. valid = false;
  874. break;
  875. }
  876. }
  877. array_sort(&cf->pool, conf_pool_listen_cmp);
  878. for (valid = true, i = 0; i < npool - 1; i++) {
  879. struct conf_pool *p1, *p2;
  880. p1 = array_get(&cf->pool, i);
  881. p2 = array_get(&cf->pool, i + 1);
  882. if (string_compare(&p1->listen.pname, &p2->listen.pname) == 0) {
  883. log_error("conf: pools '%.*s' and '%.*s' have the same listen "
  884. "address '%.*s'", p1->name.len, p1->name.data, p2->name.len,
  885. p2->name.data, p1->listen.pname.len, p1->listen.pname.data);
  886. valid = false;
  887. break;
  888. }
  889. }
  890. if (!valid) {
  891. return -1;
  892. }
  893. /* disallow pools with duplicate names */
  894. array_sort(&cf->pool, conf_pool_name_cmp);
  895. for (valid = true, i = 0; i < npool - 1; i++) {
  896. struct conf_pool *p1, *p2;
  897. p1 = array_get(&cf->pool, i);
  898. p2 = array_get(&cf->pool, i + 1);
  899. if (string_compare(&p1->name, &p2->name) == 0) {
  900. log_error("conf: '%s' has pools with same name %.*s'", cf->fname,
  901. p1->name.len, p1->name.data);
  902. valid = false;
  903. break;
  904. }
  905. }
  906. if (!valid) {
  907. return -1;
  908. }
  909. cf->valid = 1;
  910. return 0;
  911. }
  912. static void conf_dump(struct conf *cf) {
  913. uint32_t i, j, k, npool, nserver, ninstance;
  914. struct conf_pool *cp;
  915. struct conf_server *s;
  916. struct conf_instance *ci;
  917. npool = array_n(&cf->pool);
  918. if (npool == 0) {
  919. return;
  920. }
  921. printf("%"PRIu32" pools in configuration file '%s'", npool, cf->fname);
  922. for (i = 0; i < npool; i++) {
  923. cp = array_get(&cf->pool, i);
  924. log_debug("pool mid:%d", cp->mid);
  925. log_debug("%.*s", cp->name.len, cp->name.data);
  926. log_debug("listen: %.*s", cp->listen.pname.len, cp->listen.pname.data);
  927. log_debug("timeout: %d", cp->timeout);
  928. log_debug("backlog: %d", cp->backlog);
  929. log_debug("hash: %d", cp->hash);
  930. log_debug("client_connections: %d", cp->client_connections);
  931. log_debug("preconnect: %d", cp->preconnect);
  932. log_debug("server_connections: %d", cp->server_connections);
  933. log_debug("idc : %.*s", cp->idc.len , cp->idc.data);
  934. log_debug("ReplicaEnable : %d", cp->replica_enable);
  935. nserver = array_n(&cp->server);
  936. log_debug(" servers: %"PRIu32"", nserver);
  937. for (j = 0; j < nserver; j++) {
  938. s = array_get(&cp->server, j);
  939. log_debug(" %.*s", s->name.len, s->name.data);
  940. ninstance = array_n(&s->instance);
  941. log_debug(" instances: %"PRIu32"", ninstance);
  942. for (k = 0; k < ninstance; k++)
  943. {
  944. ci = array_get(&s->instance, k);
  945. log_debug(" %.*s", ci->addr.len, ci->addr.data);
  946. log_debug(" Enable : %d", ci->enable);
  947. log_debug(" idc : %.*s", ci->idc.len, ci->idc.data);
  948. log_debug(" port : %d", ci->port);
  949. log_debug(" role : %.*s", ci->role.len, ci->role.data);
  950. log_debug(" weight : %d", ci->weight);
  951. }
  952. }
  953. }
  954. }
  955. static void get_local_ip(char * out, size_t len)
  956. {
  957. strncpy(out, "0.0.0.0", len);
  958. int iClientSockfd = socket(AF_INET, SOCK_DGRAM, 0);
  959. if(iClientSockfd < 0 )
  960. return ;
  961. struct sockaddr_in stINETAddr;
  962. stINETAddr.sin_addr.s_addr = inet_addr("192.168.0.1");
  963. stINETAddr.sin_family = AF_INET;
  964. stINETAddr.sin_port = htons(8888);
  965. int iCurrentFlag = fcntl(iClientSockfd, F_GETFL, 0);
  966. fcntl(iClientSockfd, F_SETFL, iCurrentFlag | FNDELAY);
  967. if(connect(iClientSockfd, (struct sockaddr *)&stINETAddr, sizeof(struct sockaddr)) != 0) {
  968. close(iClientSockfd);
  969. return ;
  970. }
  971. struct sockaddr_in stINETAddrLocal;
  972. socklen_t iAddrLenLocal = sizeof(stINETAddrLocal);
  973. getsockname(iClientSockfd, (struct sockaddr *)&stINETAddrLocal, &iAddrLenLocal);
  974. close(iClientSockfd);
  975. strncpy(out, inet_ntoa(stINETAddrLocal.sin_addr), len);
  976. }
  977. struct conf *conf_create(char *filename) {
  978. int status;
  979. struct conf *cf;
  980. cf = conf_open(filename);
  981. if (cf == NULL) {
  982. return NULL;
  983. }
  984. //后续需要用到本地IP,所以放在前面获取
  985. get_local_ip(cf->localip, sizeof(cf->localip));
  986. status = conf_parse(cf);
  987. if (status < 0) {
  988. goto error;
  989. }
  990. status = conf_validate(cf);
  991. if (status != 0) {
  992. goto error;
  993. }
  994. conf_dump(cf);
  995. fclose(cf->fh);
  996. cf->fh = NULL;
  997. return cf;
  998. error:
  999. fclose(cf->fh);
  1000. cf->fh = NULL;
  1001. conf_destroy(cf);
  1002. return NULL;
  1003. }
  1004. char *conf_set_listen(struct conf *cf, struct command *cmd, void *conf) {
  1005. int status;
  1006. struct string *value;
  1007. struct conf_listen *field;
  1008. uint8_t *p, *name;
  1009. uint32_t namelen;
  1010. p = conf;
  1011. field = (struct conf_listen *) (p + cmd->offset);
  1012. if (field->valid == 1) {
  1013. return "is a duplicate";
  1014. }
  1015. value = &cf->arg;
  1016. status = string_duplicate(&field->pname, value);
  1017. if (status != 0) {
  1018. return CONF_ERROR ;
  1019. }
  1020. if (value->data[0] == '/') {
  1021. name = value->data;
  1022. namelen = value->len;
  1023. } else {
  1024. uint8_t *q, *start, *port;
  1025. uint32_t portlen;
  1026. /* parse "hostname:port" from the end */
  1027. p = value->data + value->len - 1;
  1028. start = value->data;
  1029. q = da_strrchr(p, start, ':');
  1030. if (q == NULL) {
  1031. return "has an invalid \"hostname:port\" format string";
  1032. }
  1033. port = q + 1;
  1034. portlen = (uint32_t) (p - port + 1);
  1035. p = q - 1;
  1036. name = start;
  1037. namelen = (uint32_t) (p - start + 1);
  1038. field->port = da_atoi(port, portlen);
  1039. if (field->port < 0 || !da_valid_port(field->port)) {
  1040. return "has an invalid port in \"hostname:port\" format string";
  1041. }
  1042. }
  1043. status = string_copy(&field->name, name, namelen);
  1044. if (status != 0) {
  1045. return CONF_ERROR ;
  1046. }
  1047. status = da_resolve(&field->name, field->port, &field->info);
  1048. if (status != 0) {
  1049. return CONF_ERROR ;
  1050. }
  1051. field->valid = 1;
  1052. return CONF_OK ;
  1053. }
  1054. char *conf_set_hash(struct conf *cf, struct command *cmd, void *conf) {
  1055. uint8_t *p;
  1056. hash_type_t *hp;
  1057. struct string *value, *hash;
  1058. p = conf;
  1059. hp = (hash_type_t *) (p + cmd->offset);
  1060. if (*hp != CONF_UNSET_HASH) {
  1061. return "is a duplicate";
  1062. }
  1063. value = &cf->arg;
  1064. for (hash = hash_strings; hash->len != 0; hash++) {
  1065. if (string_compare(value, hash) != 0) {
  1066. continue;
  1067. }
  1068. *hp = hash - hash_strings;
  1069. return CONF_OK ;
  1070. }
  1071. return "is not a valid hash";
  1072. }
  1073. char *conf_set_num(struct conf *cf, struct command *cmd, void *conf) {
  1074. uint8_t *p;
  1075. int num, *np;
  1076. struct string *value;
  1077. p = conf;
  1078. np = (int *) (p + cmd->offset);
  1079. if (*np != CONF_UNSET_NUM) {
  1080. return "is a duplicate";
  1081. }
  1082. value = &cf->arg;
  1083. num = da_atoi(value->data, value->len);
  1084. if (num < 0) {
  1085. return "is not a number";
  1086. }
  1087. *np = num;
  1088. return CONF_OK ;
  1089. }
  1090. char *conf_set_bool(struct conf *cf, struct command *cmd, void *conf) {
  1091. uint8_t *p;
  1092. int *bp;
  1093. struct string *value, true_str, false_str;
  1094. p = conf;
  1095. bp = (int *) (p + cmd->offset);
  1096. if (*bp != CONF_UNSET_NUM) {
  1097. return "is a duplicate";
  1098. }
  1099. value = &cf->arg;
  1100. string_set_text(&true_str, "true");
  1101. string_set_text(&false_str, "false");
  1102. if (string_compare(value, &true_str) == 0) {
  1103. *bp = 1;
  1104. } else if (string_compare(value, &false_str) == 0) {
  1105. *bp = 0;
  1106. } else {
  1107. return "is not \"true\" or \"false\"";
  1108. }
  1109. return CONF_OK ;
  1110. }
  1111. char *conf_set_string(struct conf *cf, struct command *cmd, void *conf) {
  1112. int status;
  1113. uint8_t *p;
  1114. struct string *field, *value;
  1115. p = conf;
  1116. field = (struct string *) (p + cmd->offset);
  1117. if (field->data != CONF_UNSET_PTR) {
  1118. return "is a duplicate";
  1119. }
  1120. value = &cf->arg;
  1121. status = string_duplicate(field, value);
  1122. if (status != 0) {
  1123. return CONF_ERROR ;
  1124. }
  1125. return CONF_OK ;
  1126. }
  1127. char *conf_set_addr(struct conf *cf, struct command *cmd, void *conf) {
  1128. int status;
  1129. struct string *value;
  1130. struct conf_instance *field;
  1131. struct string address;
  1132. char delim[] = "::";
  1133. uint8_t *p, *q, *start;
  1134. uint8_t *pname, *addr, *port, *weight;
  1135. uint32_t k, delimlen, pnamelen, addrlen, portlen, weightlen;
  1136. string_init(&address);
  1137. field = conf;
  1138. value = &cf->arg;
  1139. delimlen = value->data[0] == '/' ? 1 : 2;
  1140. p = value->data + value->len - 1;
  1141. start = value->data;
  1142. port = NULL;
  1143. portlen = 0;
  1144. weight = NULL;
  1145. weightlen = 0;
  1146. /* resolve the addr info for ip:port:weight*/
  1147. for (k = 0; k < sizeof(delim); k++) {
  1148. q = da_strrchr(p, start, delim[k]);
  1149. if (q == NULL) {
  1150. if (k == 0) {
  1151. /*
  1152. * name in "hostname:port:weight [name]" format string is
  1153. * optional
  1154. */
  1155. continue;
  1156. }
  1157. break;
  1158. }
  1159. switch (k) {
  1160. case 0:
  1161. weight = q + 1;
  1162. weightlen = (uint32_t)(p - weight + 1);
  1163. break;
  1164. case 1:
  1165. port = q + 1;
  1166. portlen = (uint32_t)(p - port + 1);
  1167. break;
  1168. default:
  1169. return CONF_ERROR;
  1170. }
  1171. p = q - 1;
  1172. }
  1173. if (k != delimlen) {
  1174. return "has an invalid \"hostname:port:weight [name]\"or \"/path/unix_socket:weight [name]\" format string";
  1175. }
  1176. pname = value->data;
  1177. pnamelen = value->len;
  1178. status = string_copy(&field->addr, pname, pnamelen);
  1179. if (status != 0) {
  1180. return CONF_ERROR;
  1181. }
  1182. field->weight = da_atoi(weight, weightlen);
  1183. if (field->weight < 0) {
  1184. return "has an invalid weight in \"hostname:port:weight [name]\" format string";
  1185. }
  1186. else if (field->weight == 0) {
  1187. return "has a zero weight in \"hostname:port:weight [name]\" format string";
  1188. }
  1189. if (value->data[0] != '/') {
  1190. field->port = da_atoi(port, portlen);
  1191. if (field->port < 0 || !nc_valid_port(field->port)) {
  1192. return "has an invalid port in \"hostname:port:weight [name]\" format string";
  1193. }
  1194. }
  1195. addr = start;
  1196. addrlen = (uint32_t)(p - start + 1);
  1197. status = string_copy(&address, addr, addrlen);
  1198. if (status != 0) {
  1199. return CONF_ERROR;
  1200. }
  1201. status = da_resolve(&address, field->port, &field->info);
  1202. if (status != 0) {
  1203. string_deinit(&address);
  1204. return CONF_ERROR;
  1205. }
  1206. string_deinit(&address);
  1207. return CONF_OK;
  1208. }