BIND 10 jreed-docs, updated. c2a34db25f4b0c903a025514cdff758e7ea55e42 [master] fix RRset cache update and re-enable test
BIND 10 source code commits
bind10-changes at lists.isc.org
Thu Feb 17 08:53:54 UTC 2011
The branch, jreed-docs has been updated
via c2a34db25f4b0c903a025514cdff758e7ea55e42 (commit)
via 98908690535371b9128a1263c7a4b59150609526 (commit)
via a01b61f9915856af9c915beca5061867804e5a86 (commit)
via 5ba0bcb7d12da8e28ca791fef5bdb82ee2619fa8 (commit)
via 4f992e158c48eba97d4cfd497a1abba12336e38d (commit)
via 496a0353cded1f60342e7713c28fecdffcecbbbe (commit)
via 6c11d9c51b88e2a6d238eac951d1d0317978f636 (commit)
via cdd18357620129b8533e9619b30b37ecbc91ede0 (commit)
via 8aa3b2246ae095bbe7f855fd11656ae3bdb98986 (commit)
via adba8b7c9e699cf557646e942012567f8e5156c9 (commit)
via 541478374f183c2742a7f07e6558836a4dd09570 (commit)
via 17f237478961005707d649a661cc72a4a0d612d4 (commit)
via 12c23a667c1e9a4b92d1cd8fc07136faf4c41a8a (commit)
via dd61821a6c910d90c4a46024e303965048dde0aa (commit)
via 0e6a18e12f61cc482e07078776234f32605312e5 (commit)
via 756e56a8c683906b1b50bba5675c996fb1295820 (commit)
via 0cc21b601f0ef1e907deaf3dbf02813ec2e0f493 (commit)
via afc4be09c37272fdadecbe8496b2579ef4cf3e42 (commit)
via 5e357877317a7a70e9a66643a4f846c32f5dc2ae (commit)
via db9ebc20e4f4b401307d6237796e0252bfaa1c59 (commit)
via b9e1f48df9f398d7641f61317a7d969690af1ff2 (commit)
via d8989e0a05ae7a453a6768f2ff4019d41ad84aab (commit)
via a9ac4ee37388d93c0adceffa424599b5affc5c3a (commit)
via 1708a52ea3956e0e7027991f6cfeb6a1b9415b94 (commit)
via 47e53343554aa6b4323ab05799a6315fb1e42538 (commit)
via dfb68c22293f3e84aae8e81865525dd043f26e0d (commit)
via 72267db6056eca0fbc9ea9b656ffb42475e7480b (commit)
via a151c4a62009d73ab3f05607ccd12889de4b9718 (commit)
via 0ae5d2d73aad78ee24e641870cec1ab06f65e3f6 (commit)
via 411b0f674614000d1f4606e0c225d078c1981628 (commit)
via 696cec5b0e99ebf65c3ea634abbc6114f1b92d8c (commit)
via 2c54e9c5f490b876eb59200cfcf9cc4623250bda (commit)
via c9eb48c9a8c49d99a31ce752b11ac507f3346356 (commit)
via 8697c90c350f8ec3c3c3677de0761230fedc8ef4 (commit)
via 9cf25c82920546461f63a87359a9ef1aa00bfc78 (commit)
via e8cbf87a4e734023f9a79d131dbe1fde067fe0c3 (commit)
via 17a76d75e8ee9e88417058b220b01761c1fd370a (commit)
via af0e5cd93bebb27cb5c4457f7759d12c8bf953a6 (commit)
via 2d2093b3eda1d0d09ae852f2dc6feefc4886b71f (commit)
via 49e2c1ae796e46fca4f5c395a92387638bb828c6 (commit)
via a02e4f3441285f10958b72e2337aa1b6e290fb56 (commit)
via 16bbee505681b46ef60febb8067765704afdeb24 (commit)
via 53f4afe16013ef6f97556d3b49d1bc56443dc08c (commit)
via 180015eada7cdc67f8756a7425f9023d884b19df (commit)
via f9a11dcba2da26e3a27a4be00461aea07aa60d85 (commit)
via a43d8d0ef4cd44d5e0ab9fa9fc746e6d1ff353e0 (commit)
via 495087240c7b5736999e3edca697eb8509068ca3 (commit)
via f364f8feb190aa09fd0a65b35bdf05a3c7fe0932 (commit)
via 1b4b8193675f0ebc9b63480900257fa21e5cfa72 (commit)
via 214eb805737eed8035490dc8ada790bca5c14fb6 (commit)
via 073d75e7ab64ff107198181cbc9ef4482e83b681 (commit)
via 4fd542a1c095efee2afb61c440784cd3984803cc (commit)
via 5400e06818b54c363c6006395ea51567caa651b3 (commit)
via 42e9021df5556117697f9228645a4ff3eee246ae (commit)
via 746980b33112b63147744796663389e5e1c3144c (commit)
via 47f69905aac05aa6f619cb143a81f206b396975a (commit)
via ca368d008315b135fa2ad3ff366aa5ae15eb4d12 (commit)
via c5dfa657c2431f3daea2dcc49741f96383f0d18d (commit)
via 1d018cd8a62fdf430f5322db023badc030cee6ee (commit)
via 0b1f8cb6dfa5481cb67e43ee8c2f24fac97ec95d (commit)
via 0c760d178eecf3162e848074e3fe631d690cc77a (commit)
via c393ec92641b6699822ebacd1c3f4c68496f3316 (commit)
via e7251d0c587ee8a794e590fdcdd4fa07f6a19c1c (commit)
via 26ab69e18bd0417086315c20192b8b93093bf6e3 (commit)
via a61a83e551549808ddc80fa99ad453932d001ff0 (commit)
via 81c35ef7e5e01da333654443045de3dd2663482c (commit)
via 7356673c98e3ca56e1ed4adecab4818c018ccf81 (commit)
via 3032690d227ee2fe8f660bdcd6c0782c6a635ef0 (commit)
via f167a263da41987a76b76e8b71acb9620a0c89c5 (commit)
via c334da7773375a87b5e7154daabf132f945f154a (commit)
via 57fdfeac98e3519e64105849495c1557388bc402 (commit)
via c9f6acc81e24c4b8f0eb351123dc7b43f64e0914 (commit)
via 29c6946c6a6a13a833b6037057debfa82ee19c22 (commit)
via 6e31e88f3be72f3e774892b46e58dc00da565dac (commit)
via d2cb311448c6f9010ff2f614ca6ffbc0c0d668a8 (commit)
via 731862b901f5530ac764db9605468d2a60ced17b (commit)
via 65ff9720f00bccc88590d09aa8f6feae5356e081 (commit)
via 4d29fda48f7304d2c5bfbc246326850ca5970b0f (commit)
via 1ed67ffd6fc90c04a79056766b2658c01d30c548 (commit)
via cd6beb6ccd33f33efbb6ac078a53fa9e0de7f3d0 (commit)
via 0c303cb20840f80038f98f023ebd6903a452efdf (commit)
via f436c5c20d296c17f8afcea1e6ba6242a9d09929 (commit)
via dcfa4914665900dc475ac1dbe98336d8daab1c61 (commit)
via 389cf02a183ea2223d1c44f68fc2f764ca5d69ea (commit)
via b832c3c975bcae66717ce28e392a189887b6f08f (commit)
via f1885e6e7b4d2688be602b189eece51e4e649f42 (commit)
via 9bfbbabf77cb41babe425c9006c050edcd3cda7b (commit)
via 32ca390dad48d4d5c8bea98248bdfc62894f87ed (commit)
via 3b18410e04cda0411a9e5699005a452d144793d7 (commit)
via 81d20bfeeab719136909382cc2d9bc1aba5f8070 (commit)
via b6d39e3da5f916c5c79ea49d7aebeca64a2c03b6 (commit)
via 0c97558ecf0c9c35d9ac86e214a0cbd1c10aaec7 (commit)
via 8cce1250c4b1a5303b515a75278103118cee83e0 (commit)
via 1e5b246a41dbfee1fdeb672b07b4e73ee1b94353 (commit)
via 24bb934f0ad2732c50730187376d96ddadefecbc (commit)
via f0b6f321beecb5145776a730461b61426a52a4f1 (commit)
via 3715569dde8e4770696ba2552fb545d2cff81b87 (commit)
via c336287b3e5f3262b4f85ec45e7b38b893c0b2b8 (commit)
via bc1b548a3828f5cd722496dd8eb5c09fc1f95187 (commit)
via 66f1989566dd871d010b08506c2b3a130ad3ee13 (commit)
via c936d4d7d78f23bd06e5db397ac350d9e3deb1f3 (commit)
via 89dd63f0d2b08ed9bb4e872ecd6b5987fa60523a (commit)
via 5ff48bacf28bfc49dc6dc6351eb72f84a207f3ee (commit)
via 32fcf336abbd3e989ab37108cc896c7f5d6984b2 (commit)
via 94cb95b1d508541201fc064302ba836164d3cbe6 (commit)
via 4d62e02fd9d410441b2f94f982e4a3b4b6e85ab4 (commit)
via f998d613872373c2fbf7088088d7464e79cc10c3 (commit)
via c5f4af2f749808f7e8b96dca09819f69bb094031 (commit)
via 7b1606cea7af15dc71f5ec1d70d958b00aa98af7 (commit)
via 76fa3d127cf0118000f368195cde54b9b49063f1 (commit)
via b9296ca023cc9e76cda48a7eeebb0119166592c5 (commit)
via 8cb9313e68bb0a7867e9c1dc6e51de1b3763fe95 (commit)
via 75692d6d89837df09f1e2c74d0fd069553fa13d8 (commit)
via cc5136da5b04edb3cdc66e90f2f60f0f0291e8ff (commit)
via 81ab8dbc34ed3b181170ecaf553266dfdfaf123f (commit)
via 18706a6018483a20fff84781c5d2fced811c7a4f (commit)
via ab2c79a8815be54d7f3482ea5608d2611b7de433 (commit)
via ce77ca466280e9c63f5c47c001cd3e98381cd56a (commit)
via 254f34910f282862727eaed7557657726887439d (commit)
via 92d766ce1407da1d1bed05e64c2b62b25af63303 (commit)
via 35d21690b35272d7046b54274ccbe87fd0279b30 (commit)
via 4aa5062b964d03a27adb9eef0d519aa26efa34a0 (commit)
via ab989937aef9282b9417a51ace071cdb199b290e (commit)
via bcac9ef1e7fb84fabbda7f41d360d4a437ae2102 (commit)
via 56baabf66b9a5224d0d2d545f65b82ab50ae781d (commit)
via c73a307cb41eb3e8bb6681dd963f6953973330ca (commit)
via a9c024d3ea91431cb4a5cab0ee53aaf4b1f808c0 (commit)
via da171d46c0a6f14f40c3730fd3d6ba34d4bf9dff (commit)
via dc5d961adfa0c6b05b911532535942c00eccda7d (commit)
via 1ab958cdc6f1ebdbf88f863a4a15d6a5783035bc (commit)
via e29c8e8050f63c1a8ca1bcd0dfa0949c3cc94063 (commit)
via 38bc36c4027fdcfbe74891f11f030014a34b46dc (commit)
via 7a264c3e91a77fed53fd305d799db6b4f671108a (commit)
via 3048f118d4b4c9995d7ee258c9405f9d7abbc576 (commit)
via 2931606d7c3d6709284788890b016ff434d0f5e2 (commit)
via 430e0e89ec5ab05bdbbd43b5fba9c93bdcf8f6c9 (commit)
via 1a52db4373a1c4169b0bbd9bc6a550e5a3e68b39 (commit)
via bbd103fff6a6a6e775a075c5132bc1cdd38a5acf (commit)
via ad83fe6b6c31a54e39116f12eb5c70d9fce8d9ea (commit)
via 12aa5f7fd9f8ce0aa10b66cbd3608c363eb3390f (commit)
via 873ab3f9db15055ec293533202bc73d908ab73c1 (commit)
via a929e898df74d43fc590e8df793edeb5f355e29d (commit)
via c11ea16f0ce2d7eac933dfc9e7b57e0e82972205 (commit)
via 18964f9374531c8f131065dcd53aefd998e080ac (commit)
via 8a4a87feea1dceee3b31a25b678d3e7ce45b2d5f (commit)
via 1c325e8b3512372f5d69b366975621d06b5c217e (commit)
via 58a43899ff533e725bb606a0359862dee416f7c7 (commit)
via f70d352db01e774ad9561499f2205de3e66e8dbb (commit)
via 640640363d2a29cc0f4ab8151ca5c02dc9f7c361 (commit)
via 0f31fd26c5525b7bd63acaea187f790c2fed8dc6 (commit)
via e4dd2ed94539a6e17070494878c9023e7a8472cb (commit)
via 2ec914c9ed52fd0f22501addacb726ad193e1fed (commit)
via bf944144b1283d93a5138145cbdb576cea7a9fa0 (commit)
via d1971c14bddf6bcdbc031d1e3195b48ed4bffd62 (commit)
via cfc8341a5ff3f2cc7873689b2b787e9ed6dd1b02 (commit)
via 08b215209a79db8420d565adaec7309413b01ea7 (commit)
via a0f8c9ee2bf78af6d95293a4d802e0205a4429c3 (commit)
via 3372c331452e94681b82b708d11dd0f93c945153 (commit)
via 311eb1ae0574c5f981937ae41614ff0fad034c9a (commit)
via 7c42c6d38d63dd55462eb01037a55a40185920a3 (commit)
via 6d9d7b76a3e55c6ca2a4c7af09674867a0e54c28 (commit)
via 950ffa229928d24ba8ebd74c5945e4cf0facf539 (commit)
via 06756779103175d716dbeee36aff59a116eef39d (commit)
via b7c5f0703226ea1f68af4802e4b410cc42e98144 (commit)
via cf6de64ab83a1db3a71aa065c1d247462f236ea8 (commit)
via 5f6cbcc31cc0f16af49dd433ac96da967d200d6c (commit)
via 923977389a20aa0396dccec4867f3721fc95a4bd (commit)
via d8a7320161d314a5863183a063d0e00fdd1017bd (commit)
via b086fbca9ce75d8950cd51d0b9de3d49715565a7 (commit)
via eb4c7905227413fe7d4ee8a42d1e8dfc185d36f1 (commit)
via 47dc887dfdf6d3501a73bad82f30f6148fc2ff8e (commit)
via 2565b9fe9daf6ba63234b06b9f0eb5b8168257b4 (commit)
via c2b374e5e2d608ce0236b7f49b63bf70b57c761f (commit)
via 2e93f9a6c23985a4e9968c13c3068c61f202ca4f (commit)
via 6031b9096d5a9c73468afa386a3e311984125522 (commit)
via 83904ae5b53bf4a5a5436902f4be88f818db984a (commit)
via f0a43d242f48103eec0703d7081522ec0f2225a9 (commit)
via a6f9d5cd07ea1b21641232312cc15adcce91f1ec (commit)
via 8ddc37ac3b6229f03a1fc122ea3c1c8906cbd75b (commit)
via cac61e91646c344359a5b767d2f2042d05cc1d74 (commit)
via fb4ca10d60279cc2d7da663b912dcb189849d0da (commit)
via 59dd3fb61b265455b544407fcd8976a04f152af3 (commit)
via 65ef7a150b4562f2e80222afd7fb010cbd55ec43 (commit)
via 026a4104701791649673a74591007e1d50167cbe (commit)
via 2363b50bc04e0218e185561a4e198f21e8783905 (commit)
via 4265faedd3e14e2f4afccd664df07a4e09e3ae7f (commit)
via 4db21f814bf18d17475dad5e81b8bbb8e0dfa570 (commit)
via b4aed028611c1f229d44cd688053b5635f6c909d (commit)
via f0a09daf74c62309e9de65adf3b99daf967ca0ac (commit)
via 9f172776a92413f7ba09fae87c98522c3d55fe63 (commit)
via a1e26d979de642bc8fc52d613f65c0dcd8fc8ee3 (commit)
via 210a3fa8f933d7141f0ca48e5088a89fcac611ef (commit)
via ed3436f599952a3e070dbf19668e5bcb2e9dacac (commit)
via dd2f336165e9f974aaed88569b5e718493cd5757 (commit)
via 8a175757cd9b46205b7118c3e7fad72883f130e5 (commit)
via c1dd1aeae9aa2cb06b372d2002fa15d57aa7c552 (commit)
via 324b135b99582c63aa49f50d8c80e00d6e43e2f9 (commit)
via 6fb3a7ca7f9f1b7018493a263b3063b0b0962b5e (commit)
via b211d93037f31731426dbf073098ebfa8ae16bc4 (commit)
via 6c8cd2a184118179db9e54eed8967cedbafecbab (commit)
via 89cbb6aafeaf5cbc51b50a0caa4542c7b0f43eb4 (commit)
via 3aa87f6826846585666e907b446a3809ccfce560 (commit)
via ee7acfccb0cd0d68dc4d7e64ae7fa0655314b444 (commit)
via 80dd41d265d1e2c366c8ea2dc1e79ee353f1e8f1 (commit)
via a8746fb17e10db78330cf69b2850515020f5cd56 (commit)
via 89f3a8b45f18f1bf4e1a2e6082a93513582b6d55 (commit)
via 64ebfb19dd4c42b8ba81b258a1a408345c3b1caf (commit)
via 9d0ec38ee3ef9e08778abd66c6946bdec5f2d25b (commit)
via fb981291afb0a4dc0fcbd16b9bb552e5fbb5c20b (commit)
via 2f151e47e2579aed8c124385dacb3c971978ed45 (commit)
via a0d8f67619db8737abda16b3ef9b1b731ab1bfa1 (commit)
via 6b118748704d34fbea4d80cbed3a1094abd22e11 (commit)
via 94270478763c9c4000ca94afb94fb4762f56c3f9 (commit)
via bebfa15d5007c3ba8ce740403c8333ea6480b83e (commit)
via 42dbc3cabef1db3f4c9ce99bc0830ce34794a704 (commit)
via e7c61f1bb5cf375a1f005bb7d77080d2fcd8fb9e (commit)
via ac8359174eaeb1df18bddcb0a57807d49ff8ed53 (commit)
via c94995ad3292958582d1ad491fdfbaa76e43cad8 (commit)
via fca9cd2ad55dfdff80ed3106c4cb40bf4d508d26 (commit)
via 597bb698382ffdd04cdd2869ce5d2570dd5ac875 (commit)
via e01dadd724cfe1274f45c6be4b5ba6f532df8c0e (commit)
via 42beef26ba126bab9740af0a809617fc3c1906c6 (commit)
via 3f791c7c437212e55981f044ef722a759f58a184 (commit)
via e64c9d4107c1d012b68239ecf4d828467f7eb6f7 (commit)
via ba8595ee4dbcc87deb5aca78359dd7a42e4a6f45 (commit)
via 635c6e467ed5d9bfe731a726bec673a70aad0d0a (commit)
via 0162b6441a567a0a687a726f651e23bb754bfd71 (commit)
via 74266da0141650d6ca337a66f266ba675ad2d357 (commit)
via bd58459c94e2074aecb39b02fa47a2b79e48cfb6 (commit)
via b4c068b2189668459f6f945d6cbe51e294af4e8d (commit)
via 3be136324e68d5ebab0b0d3da88550a920662c98 (commit)
via 238f276c9b5887fcf72e86011e610466ce163f5b (commit)
via 87c347e04505f141d624eb7be497181a37e346d0 (commit)
via 758460fa8ad9cd35841f5c44dd700d9f74f3d070 (commit)
via 3666c6d78561034b5235d91547f94c554adc4b11 (commit)
via 3a899ef4c5c11d4bdfd9f50ac4aaa1b276060a65 (commit)
via a99da0d489fbecc59b6d75a85e71e92ec7d173e3 (commit)
via a0e0f22323abff0332ac4dff60300874a3e2f1a0 (commit)
via 4205ee154f209d4493e2b7176b53c79d5dbaf193 (commit)
via 6e5884cfb1610b47339be5f2578e8e77840d3095 (commit)
via 4e8c15eae9ffb0d159f5d9f138455aa0213163fe (commit)
via 83d5d9a0bc2599bd9a643c2c6294f382638486b6 (commit)
via e75ba8ce2f5b2f9814741d0ea633f3542b458a74 (commit)
via f62f4f06cf94b4d979e5b036a41c06b4f53e5759 (commit)
via 39158d307dd4b4a74f89bb755d22d1edd3a04d80 (commit)
via 777f07c955102aaa875ed2567d788b4103f849e1 (commit)
via 2aff13f019888ae254733f2c16d9a8b995a48318 (commit)
via 0050a51bfb904132db32a2d4729064d8a35a5332 (commit)
via ebb344c1e2b8bb7eaa424ae4b03a46ac01787ff9 (commit)
via 77f677784ed005ff2cf6e5387d837897e3e3bdc4 (commit)
via 886b10843e5e8fe2fd7b04684a590affe1b812ab (commit)
via 07e20270989ec37e95feb3a01fefeb5b80465bef (commit)
via a7bf808a220742d18fb1868241f1665f745ebdbc (commit)
via 8eafc24514f6d6cc1a8b71bef7358cb9bdff5a8c (commit)
via 38e8504574d599e1e6d139ebcfe5f905de678e2d (commit)
via 61e391f3bb0af427263c2374d613af3fd10a5fe4 (commit)
via 6c3a274305a7823a4060769ab21d55c5975615d6 (commit)
via 203152faad9ccda93c9f799ef48c47d22da1d3bf (commit)
via b6b9f884f331a4f8726a3cd6a0f3eed7870b7cf8 (commit)
via 5efcee702cfe268615733f30988abca319089531 (commit)
via 5a7d082452848daa9dcb0b023f0829d457d8bcc0 (commit)
via 17684b0c3c8dae44b22c9f69945fbd4b24faa9bf (commit)
from d351fe6ec30972595d70135cee24e6dd6d990d33 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
-----------------------------------------------------------------------
Summary of changes:
ChangeLog | 66 +-
Makefile.am | 1 +
configure.ac | 66 +-
doc/Doxyfile | 2 +-
.../internal => ext/coroutine}/coroutine.h | 0
src/bin/auth/auth_srv.cc | 12 +-
src/bin/auth/auth_srv.h | 3 +-
src/bin/auth/benchmarks/Makefile.am | 1 +
src/bin/auth/config.cc | 7 +-
src/bin/auth/query.cc | 59 +-
src/bin/auth/tests/Makefile.am | 1 +
src/bin/auth/tests/command_unittest.cc | 2 +-
src/bin/auth/tests/config_unittest.cc | 4 +
src/bin/auth/tests/query_unittest.cc | 175 +++-
src/bin/bind10/bind10.py.in | 42 +-
src/bin/msgq/tests/msgq_test.py | 2 +-
src/bin/resolver/Makefile.am | 2 +-
src/bin/resolver/resolver.cc | 22 +-
src/bin/resolver/resolver.h | 19 +-
src/bin/resolver/response_classifier.cc | 259 -----
src/bin/resolver/response_classifier.h | 138 ---
src/bin/resolver/response_scrubber.cc | 189 +++
src/bin/resolver/response_scrubber.h | 422 +++++++
src/bin/resolver/tests/Makefile.am | 18 +-
.../resolver/tests/response_classifier_unittest.cc | 494 --------
.../resolver/tests/response_scrubber_unittest.cc | 542 +++++++++
src/bin/stats/b10-stats.xml | 3 +-
src/bin/usermgr/b10-cmdctl-usermgr.py.in | 2 +-
src/lib/Makefile.am | 4 +-
src/lib/asiolink/Makefile.am | 29 +-
src/lib/asiolink/asiolink.cc | 725 ------------
src/lib/asiolink/asiolink.h | 627 +----------
src/lib/asiolink/dns_answer.h | 73 ++
src/lib/asiolink/dns_lookup.h | 81 ++
src/lib/asiolink/dns_server.h | 152 +++
src/lib/asiolink/dns_service.cc | 192 ++++
src/lib/asiolink/dns_service.h | 106 ++
src/lib/asiolink/internal/Makefile.am | 1 -
src/lib/asiolink/internal/tcpdns.h | 224 ----
src/lib/asiolink/internal/tests/Makefile.am | 37 -
src/lib/asiolink/internal/tests/run_unittests.cc | 21 -
src/lib/asiolink/internal/tests/udpdns_unittest.cc | 145 ---
src/lib/asiolink/internal/udpdns.h | 302 -----
src/lib/asiolink/interval_timer.cc | 136 +++
src/lib/asiolink/interval_timer.h | 133 +++
src/lib/asiolink/{ioaddress.cc => io_address.cc} | 0
src/lib/asiolink/io_address.h | 127 ++
src/lib/asiolink/io_endpoint.cc | 45 +
src/lib/asiolink/io_endpoint.h | 122 ++
src/lib/asiolink/io_message.h | 103 ++
src/lib/asiolink/io_service.cc | 97 ++
src/lib/asiolink/io_service.h | 77 ++
src/lib/asiolink/io_socket.cc | 67 ++
src/lib/asiolink/{iosocket.h => io_socket.h} | 0
src/lib/asiolink/ioaddress.h | 88 --
src/lib/asiolink/ioendpoint.cc | 43 -
src/lib/asiolink/ioendpoint.h | 121 --
src/lib/asiolink/iomessage.h | 103 --
src/lib/asiolink/iosocket.cc | 67 --
src/lib/asiolink/recursive_query.cc | 457 ++++++++
src/lib/asiolink/recursive_query.h | 113 ++
src/lib/asiolink/simple_callback.h | 71 ++
src/lib/asiolink/tcp_endpoint.h | 98 ++
src/lib/asiolink/tcp_server.cc | 194 ++++
src/lib/asiolink/tcp_server.h | 119 ++
src/lib/asiolink/tcp_socket.h | 52 +
src/lib/asiolink/tcpdns.cc | 194 ----
src/lib/asiolink/tests/Makefile.am | 14 +-
src/lib/asiolink/tests/asiolink_unittest.cc | 1200 --------------------
src/lib/asiolink/tests/interval_timer_unittest.cc | 292 +++++
src/lib/asiolink/tests/io_service_unittest.cc | 115 ++
src/lib/asiolink/tests/ioaddress_unittest.cc | 57 +
src/lib/asiolink/tests/ioendpoint_unittest.cc | 67 ++
src/lib/asiolink/tests/iosocket_unittest.cc | 29 +
src/lib/asiolink/tests/recursive_query_unittest.cc | 791 +++++++++++++
src/lib/asiolink/tests/udp_query_unittest.cc | 145 +++
src/lib/asiolink/udp_endpoint.h | 89 ++
src/lib/asiolink/udp_query.cc | 189 +++
src/lib/asiolink/udp_query.h | 88 ++
src/lib/asiolink/udp_server.cc | 284 +++++
src/lib/asiolink/udp_server.h | 102 ++
src/lib/asiolink/udp_socket.h | 48 +
src/lib/asiolink/udpdns.cc | 319 ------
src/lib/cache/Makefile.am | 33 +
src/lib/cache/TODO | 14 +
src/lib/cache/cache_entry_key.cc | 44 +
src/lib/cache/cache_entry_key.h | 56 +
src/lib/cache/local_zone_data.cc | 58 +
src/lib/cache/local_zone_data.h | 66 ++
src/lib/cache/message_cache.cc | 97 ++
src/lib/cache/message_cache.h | 95 ++
src/lib/cache/message_entry.cc | 251 ++++
src/lib/cache/message_entry.h | 180 +++
src/lib/cache/resolver_cache.cc | 246 ++++
src/lib/cache/resolver_cache.h | 331 ++++++
src/lib/cache/rrset_cache.cc | 106 ++
src/lib/cache/rrset_cache.h | 108 ++
src/lib/cache/rrset_copy.cc | 40 +
src/lib/cache/rrset_copy.h | 44 +
src/lib/cache/rrset_entry.cc | 68 ++
src/lib/cache/rrset_entry.h | 137 +++
src/lib/cache/tests/Makefile.am | 66 ++
src/lib/cache/tests/cache_test_messagefromfile.h | 40 +
src/lib/cache/tests/cache_test_sectioncount.h | 45 +
src/lib/cache/tests/local_zone_data_unittest.cc | 65 ++
src/lib/cache/tests/message_cache_unittest.cc | 98 ++
src/lib/cache/tests/message_entry_unittest.cc | 237 ++++
src/lib/cache/tests/resolver_cache_unittest.cc | 138 +++
src/lib/cache/tests/rrset_cache_unittest.cc | 84 ++
src/lib/cache/tests/rrset_entry_unittest.cc | 107 ++
src/lib/cache/tests/run_unittests.cc | 29 +
.../tests/testdata/message_fromWire1 | 0
src/lib/cache/tests/testdata/message_fromWire2 | 22 +
src/lib/cache/tests/testdata/message_fromWire3 | 76 ++
src/lib/cache/tests/testdata/message_fromWire4 | 80 ++
src/lib/cache/tests/testdata/message_fromWire5 | 36 +
src/lib/cache/tests/testdata/message_fromWire6 | 40 +
src/lib/datasrc/memory_datasrc.cc | 314 ++++--
src/lib/datasrc/rbtree.h | 713 +++++++++---
src/lib/datasrc/tests/memory_datasrc_unittest.cc | 322 +++++-
src/lib/datasrc/tests/rbtree_unittest.cc | 398 +++++--
src/lib/dns/message.cc | 59 +
src/lib/dns/message.h | 36 +-
src/lib/dns/rrset.h | 10 +-
src/lib/dns/tests/message_unittest.cc | 171 +++
src/lib/log/Makefile.am | 43 +-
src/lib/log/compiler/Makefile.am | 20 +
src/lib/log/compiler/message.cc | 450 ++++++++
src/lib/log/dbglevels.h | 31 +
src/lib/log/documentation.txt | 371 ++++++
src/lib/log/filename.cc | 140 +++
src/lib/log/filename.h | 163 +++
src/lib/log/logger.cc | 307 +++++
src/lib/log/logger.h | 327 ++++++
src/lib/log/logger_support.cc | 116 ++
src/lib/log/logger_support.h | 43 +
src/lib/log/message_dictionary.cc | 116 ++
src/lib/log/message_dictionary.h | 150 +++
src/lib/log/message_exception.cc | 28 +
src/lib/log/message_exception.h | 90 ++
src/lib/log/message_initializer.cc | 32 +
src/lib/log/message_initializer.h | 63 +
src/lib/log/message_reader.cc | 184 +++
src/lib/log/message_reader.h | 175 +++
src/lib/log/message_types.h | 32 +
src/lib/log/messagedef.cc | 27 +
src/lib/log/messagedef.h | 24 +
src/lib/log/messagedef.mes | 82 ++
src/lib/log/root_logger_name.cc | 26 +
src/lib/log/root_logger_name.h | 66 ++
src/lib/log/strutil.cc | 138 +++
src/lib/log/strutil.h | 147 +++
src/lib/log/tests/Makefile.am | 47 +
src/lib/log/tests/filename_unittest.cc | 181 +++
src/lib/log/tests/localdef.mes | 23 +
src/lib/log/tests/logger_support_test.cc | 109 ++
src/lib/log/tests/logger_unittest.cc | 395 +++++++
src/lib/log/tests/message_dictionary_unittest.cc | 173 +++
src/lib/log/tests/message_initializer_unittest.cc | 72 ++
.../log/tests/message_initializer_unittest_2.cc | 41 +
src/lib/log/tests/message_reader_unittest.cc | 228 ++++
src/lib/log/tests/root_logger_name_unittest.cc | 52 +
src/lib/log/tests/run_time_init_test.sh.in | 81 ++
src/lib/log/tests/run_unittests.cc | 23 +
src/lib/log/tests/strutil_unittest.cc | 216 ++++
src/lib/log/tests/xdebuglevel_unittest.cc | 205 ++++
src/lib/log/xdebuglevel.cc | 148 +++
src/lib/log/xdebuglevel.h | 164 +++
src/lib/nsas/Makefile.am | 1 -
src/lib/nsas/nameserver_address_store.cc | 9 +-
src/lib/nsas/nameserver_address_store.h | 7 +-
src/lib/nsas/nameserver_entry.cc | 26 +-
src/lib/nsas/nameserver_entry.h | 8 +-
src/lib/nsas/resolver_interface.h | 78 --
.../tests/nameserver_address_store_unittest.cc | 2 +-
src/lib/nsas/tests/nameserver_address_unittest.cc | 6 +-
src/lib/nsas/tests/nameserver_entry_unittest.cc | 16 +-
src/lib/nsas/tests/nsas_test.h | 31 +-
src/lib/nsas/tests/zone_entry_unittest.cc | 9 +-
src/lib/nsas/zone_entry.cc | 22 +-
src/lib/nsas/zone_entry.h | 11 +-
src/lib/python/isc/config/ccsession.py | 12 +-
src/lib/resolve/Makefile.am | 18 +
src/lib/resolve/resolve.cc | 58 +
src/lib/resolve/resolve.h | 60 +
src/lib/resolve/resolver_callback.cc | 36 +
src/lib/resolve/resolver_callback.h | 48 +
src/lib/resolve/resolver_interface.h | 98 ++
src/lib/resolve/response_classifier.cc | 274 +++++
src/lib/resolve/response_classifier.h | 156 +++
src/lib/resolve/tests/Makefile.am | 27 +
src/lib/resolve/tests/resolve_unittest.cc | 172 +++
.../resolve/tests/resolver_callback_unittest.cc | 90 ++
.../resolve/tests/response_classifier_unittest.cc | 537 +++++++++
src/lib/resolve/tests/run_unittests.cc | 24 +
src/lib/testutils/srv_test.cc | 1 +
src/lib/testutils/srv_test.h | 2 +-
197 files changed, 18551 insertions(+), 5725 deletions(-)
rename {src/lib/asiolink/internal => ext/coroutine}/coroutine.h (100%)
delete mode 100644 src/bin/resolver/response_classifier.cc
delete mode 100644 src/bin/resolver/response_classifier.h
create mode 100644 src/bin/resolver/response_scrubber.cc
create mode 100644 src/bin/resolver/response_scrubber.h
delete mode 100644 src/bin/resolver/tests/response_classifier_unittest.cc
create mode 100644 src/bin/resolver/tests/response_scrubber_unittest.cc
delete mode 100644 src/lib/asiolink/asiolink.cc
create mode 100644 src/lib/asiolink/dns_answer.h
create mode 100644 src/lib/asiolink/dns_lookup.h
create mode 100644 src/lib/asiolink/dns_server.h
create mode 100644 src/lib/asiolink/dns_service.cc
create mode 100644 src/lib/asiolink/dns_service.h
delete mode 100644 src/lib/asiolink/internal/Makefile.am
delete mode 100644 src/lib/asiolink/internal/tcpdns.h
delete mode 100644 src/lib/asiolink/internal/tests/Makefile.am
delete mode 100644 src/lib/asiolink/internal/tests/run_unittests.cc
delete mode 100644 src/lib/asiolink/internal/tests/udpdns_unittest.cc
delete mode 100644 src/lib/asiolink/internal/udpdns.h
create mode 100644 src/lib/asiolink/interval_timer.cc
create mode 100644 src/lib/asiolink/interval_timer.h
rename src/lib/asiolink/{ioaddress.cc => io_address.cc} (100%)
create mode 100644 src/lib/asiolink/io_address.h
create mode 100644 src/lib/asiolink/io_endpoint.cc
create mode 100644 src/lib/asiolink/io_endpoint.h
create mode 100644 src/lib/asiolink/io_message.h
create mode 100644 src/lib/asiolink/io_service.cc
create mode 100644 src/lib/asiolink/io_service.h
create mode 100644 src/lib/asiolink/io_socket.cc
rename src/lib/asiolink/{iosocket.h => io_socket.h} (100%)
delete mode 100644 src/lib/asiolink/ioaddress.h
delete mode 100644 src/lib/asiolink/ioendpoint.cc
delete mode 100644 src/lib/asiolink/ioendpoint.h
delete mode 100644 src/lib/asiolink/iomessage.h
delete mode 100644 src/lib/asiolink/iosocket.cc
create mode 100644 src/lib/asiolink/recursive_query.cc
create mode 100644 src/lib/asiolink/recursive_query.h
create mode 100644 src/lib/asiolink/simple_callback.h
create mode 100644 src/lib/asiolink/tcp_endpoint.h
create mode 100644 src/lib/asiolink/tcp_server.cc
create mode 100644 src/lib/asiolink/tcp_server.h
create mode 100644 src/lib/asiolink/tcp_socket.h
delete mode 100644 src/lib/asiolink/tcpdns.cc
delete mode 100644 src/lib/asiolink/tests/asiolink_unittest.cc
create mode 100644 src/lib/asiolink/tests/interval_timer_unittest.cc
create mode 100644 src/lib/asiolink/tests/io_service_unittest.cc
create mode 100644 src/lib/asiolink/tests/ioaddress_unittest.cc
create mode 100644 src/lib/asiolink/tests/ioendpoint_unittest.cc
create mode 100644 src/lib/asiolink/tests/iosocket_unittest.cc
create mode 100644 src/lib/asiolink/tests/recursive_query_unittest.cc
create mode 100644 src/lib/asiolink/tests/udp_query_unittest.cc
create mode 100644 src/lib/asiolink/udp_endpoint.h
create mode 100644 src/lib/asiolink/udp_query.cc
create mode 100644 src/lib/asiolink/udp_query.h
create mode 100644 src/lib/asiolink/udp_server.cc
create mode 100644 src/lib/asiolink/udp_server.h
create mode 100644 src/lib/asiolink/udp_socket.h
delete mode 100644 src/lib/asiolink/udpdns.cc
create mode 100644 src/lib/cache/Makefile.am
create mode 100644 src/lib/cache/TODO
create mode 100644 src/lib/cache/cache_entry_key.cc
create mode 100644 src/lib/cache/cache_entry_key.h
create mode 100644 src/lib/cache/local_zone_data.cc
create mode 100644 src/lib/cache/local_zone_data.h
create mode 100644 src/lib/cache/message_cache.cc
create mode 100644 src/lib/cache/message_cache.h
create mode 100644 src/lib/cache/message_entry.cc
create mode 100644 src/lib/cache/message_entry.h
create mode 100644 src/lib/cache/resolver_cache.cc
create mode 100644 src/lib/cache/resolver_cache.h
create mode 100644 src/lib/cache/rrset_cache.cc
create mode 100644 src/lib/cache/rrset_cache.h
create mode 100644 src/lib/cache/rrset_copy.cc
create mode 100644 src/lib/cache/rrset_copy.h
create mode 100644 src/lib/cache/rrset_entry.cc
create mode 100644 src/lib/cache/rrset_entry.h
create mode 100644 src/lib/cache/tests/Makefile.am
create mode 100644 src/lib/cache/tests/cache_test_messagefromfile.h
create mode 100644 src/lib/cache/tests/cache_test_sectioncount.h
create mode 100644 src/lib/cache/tests/local_zone_data_unittest.cc
create mode 100644 src/lib/cache/tests/message_cache_unittest.cc
create mode 100644 src/lib/cache/tests/message_entry_unittest.cc
create mode 100644 src/lib/cache/tests/resolver_cache_unittest.cc
create mode 100644 src/lib/cache/tests/rrset_cache_unittest.cc
create mode 100644 src/lib/cache/tests/rrset_entry_unittest.cc
create mode 100644 src/lib/cache/tests/run_unittests.cc
copy src/lib/{dns => cache}/tests/testdata/message_fromWire1 (100%)
create mode 100644 src/lib/cache/tests/testdata/message_fromWire2
create mode 100644 src/lib/cache/tests/testdata/message_fromWire3
create mode 100644 src/lib/cache/tests/testdata/message_fromWire4
create mode 100644 src/lib/cache/tests/testdata/message_fromWire5
create mode 100644 src/lib/cache/tests/testdata/message_fromWire6
create mode 100644 src/lib/log/compiler/Makefile.am
create mode 100644 src/lib/log/compiler/message.cc
create mode 100644 src/lib/log/dbglevels.h
create mode 100644 src/lib/log/documentation.txt
create mode 100644 src/lib/log/filename.cc
create mode 100644 src/lib/log/filename.h
create mode 100644 src/lib/log/logger.cc
create mode 100644 src/lib/log/logger.h
create mode 100644 src/lib/log/logger_support.cc
create mode 100644 src/lib/log/logger_support.h
create mode 100644 src/lib/log/message_dictionary.cc
create mode 100644 src/lib/log/message_dictionary.h
create mode 100644 src/lib/log/message_exception.cc
create mode 100644 src/lib/log/message_exception.h
create mode 100644 src/lib/log/message_initializer.cc
create mode 100644 src/lib/log/message_initializer.h
create mode 100644 src/lib/log/message_reader.cc
create mode 100644 src/lib/log/message_reader.h
create mode 100644 src/lib/log/message_types.h
create mode 100644 src/lib/log/messagedef.cc
create mode 100644 src/lib/log/messagedef.h
create mode 100644 src/lib/log/messagedef.mes
create mode 100644 src/lib/log/root_logger_name.cc
create mode 100644 src/lib/log/root_logger_name.h
create mode 100644 src/lib/log/strutil.cc
create mode 100644 src/lib/log/strutil.h
create mode 100644 src/lib/log/tests/Makefile.am
create mode 100644 src/lib/log/tests/filename_unittest.cc
create mode 100644 src/lib/log/tests/localdef.mes
create mode 100644 src/lib/log/tests/logger_support_test.cc
create mode 100644 src/lib/log/tests/logger_unittest.cc
create mode 100644 src/lib/log/tests/message_dictionary_unittest.cc
create mode 100644 src/lib/log/tests/message_initializer_unittest.cc
create mode 100644 src/lib/log/tests/message_initializer_unittest_2.cc
create mode 100644 src/lib/log/tests/message_reader_unittest.cc
create mode 100644 src/lib/log/tests/root_logger_name_unittest.cc
create mode 100755 src/lib/log/tests/run_time_init_test.sh.in
create mode 100644 src/lib/log/tests/run_unittests.cc
create mode 100644 src/lib/log/tests/strutil_unittest.cc
create mode 100644 src/lib/log/tests/xdebuglevel_unittest.cc
create mode 100644 src/lib/log/xdebuglevel.cc
create mode 100644 src/lib/log/xdebuglevel.h
delete mode 100644 src/lib/nsas/resolver_interface.h
create mode 100644 src/lib/resolve/Makefile.am
create mode 100644 src/lib/resolve/resolve.cc
create mode 100644 src/lib/resolve/resolve.h
create mode 100644 src/lib/resolve/resolver_callback.cc
create mode 100644 src/lib/resolve/resolver_callback.h
create mode 100644 src/lib/resolve/resolver_interface.h
create mode 100644 src/lib/resolve/response_classifier.cc
create mode 100644 src/lib/resolve/response_classifier.h
create mode 100644 src/lib/resolve/tests/Makefile.am
create mode 100644 src/lib/resolve/tests/resolve_unittest.cc
create mode 100644 src/lib/resolve/tests/resolver_callback_unittest.cc
create mode 100644 src/lib/resolve/tests/response_classifier_unittest.cc
create mode 100644 src/lib/resolve/tests/run_unittests.cc
-----------------------------------------------------------------------
diff --git a/ChangeLog b/ChangeLog
index 53b3cc2..9cf9b3b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,56 @@
+ 169. [func] zhang likun, jelte
+ Added a basic implementation for a resolver cache (though not
+ used yet).
+ (Trac #449, git 8aa3b2246ae095bbe7f855fd11656ae3bdb98986)
+
+ 168. [bug] vorner
+ Boss no longer has the -f argument, which was undocumented and stayed as
+ a relict of previous versions, currently causing only strange behaviour.
+ (Trac #572, git 17f237478961005707d649a661cc72a4a0d612d4)
+
+ 167. [bug] naokikambe
+ Fixed failure of termination of msgq_test.py with python3 coverage(3.3.1)
+ (Trac #573, git 0e6a18e12f61cc482e07078776234f32605312e5)
+
+ 166. [func] jelte
+ The resolver now sends back a SERVFAIL when there is a client
+ timeout (timeout_client config setting), but it will not stop
+ resolving (until there is a lookup timeout or a result).
+ (Trac #497 and #489, git af0e5cd93bebb27cb5c4457f7759d12c8bf953a6)
+
+ 165. [func] jelte
+ The resolver now handles CNAMEs, it will follow them, and include
+ them in the answer. The maximum length of CNAME chains that is
+ supported is 16.
+ (Trac #497, git af0e5cd93bebb27cb5c4457f7759d12c8bf953a6)
+
+ 164. [bug] y-aharen
+ IntervalTimer: Modified the interface to accept interval in
+ milliseconds. It shortens the time of the tests of IntervalTimer.
+ (Trac #452, git c9f6acc81e24c4b8f0eb351123dc7b43f64e0914)
+
+ 163. [func] vorner
+ The pimpl design pattern is used in UDPServer, with a shared pointer. This
+ makes it smaller to copy (which is done a lot as a sideeffect of being
+ coroutine) and speeds applications of this class (notably b10-auth) up by
+ around 10%.
+ (Trac #537, git 94cb95b1d508541201fc064302ba836164d3cbe6)
+
+ 162. [func] stephen
+ Added C++ logging, allowing logging at different severities.
+ Code specifies the message to be logged via a symbol, and the
+ logging code picks up the message from an in-built dictionary.
+ The contents of the dictionary can be replaced at run-time by
+ locale-specific messages. A message compiler program is provided
+ to create message header files and supply the default messages.
+ (Trac #438, git 7b1606cea7af15dc71f5ec1d70d958b00aa98af7)
+
+ 161. [func] stephen
+ Added ResponseScrubber class to examine response from
+ a server and to remove out-of-bailiwick RRsets. Also
+ does cross-section checks to ensure consistency.
+ (Trac #496, git b9296ca023cc9e76cda48a7eeebb0119166592c5)
+
160. [func] jelte
Updated the resolver to take 3 different timeout values;
timeout_query for outstanding queries we sent while resolving
@@ -44,7 +97,8 @@ bind10-devel-20110120 released on January 20, 2011
receive buffer became full, and many other components that rely
on CC channels would stall (as noted in #420 and #513). This is
an urgent care fix due to the severity of the problem; we'll need
- to revisit it for cleaner fix later. (Trac #516, git 62c72fc)
+ to revisit it for cleaner fix later.
+ (Trac #516, git 62c72fcdf4617e4841e901408f1e7961255b8194)
153. [bug] jelte
b10-cfgmgr: Fixed a bug where configuration updates sometimes
@@ -58,7 +112,7 @@ bind10-devel-20110120 released on January 20, 2011
the value to 0. Disabling statistics updates will also work as
a temporary workaround of a known issue that b10-auth can block in
sending statistics and stop responding to queries as a result.
- (Trac #513, git 285c5ee)
+ (Trac #513, git 285c5ee3d5582ed6df02d1aa00387f92a74e3695)
151. [bug] smann
lib/log/dummylog.h:
@@ -90,7 +144,7 @@ bind10-devel-20110120 released on January 20, 2011
147. [bug] jinmei
python/isc/config: Fixed a bug that importing custom configuration
(in b10-config.db) of a remote module didn't work.
- (Trac #478, git ea4a481)
+ (Trac #478, git ea4a481003d80caf2bff8d0187790efd526d72ca)
146. [func] jelte
Command arguments were not validated internally against their
@@ -105,7 +159,8 @@ bind10-devel-20110120 released on January 20, 2011
only feasible for class IN in memory data source. To reload a
zone "example.com" via bindctl, execute the command as follows:
> Auth loadzone origin = example.com
- (Trac #467)
+ (Trac #467 git 4f7e1f46da1046de527ab129a88f6aad3dba7562
+ from 1d7d3918661ba1c6a8b1e40d8fcbc5640a84df12)
144. [build] jinmei
Introduced a workaround for clang++ build on FreeBSD (and probably
@@ -115,7 +170,8 @@ bind10-devel-20110120 released on January 20, 2011
doesn't matter; the important part is the -L flag). This
workaround is not automatically enabled as it's difficult to
detect the need for it dynamically, and must be enabled via the
- variable by hand. (Trac #474, git cfde436)
+ variable by hand.
+ (Trac #474, git cfde436fbd7ddf3f49cbbd153999656e8ca2a298)
143. [build] jinmei
Fixed build problems with clang++ in unit tests due to recent
diff --git a/Makefile.am b/Makefile.am
index 93a7498..68a41d6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -282,3 +282,4 @@ EXTRA_DIST += ext/asio/asio/is_write_buffered.hpp
EXTRA_DIST += ext/asio/asio/buffered_read_stream_fwd.hpp
EXTRA_DIST += ext/asio/asio/socket_acceptor_service.hpp
EXTRA_DIST += ext/asio/asio.hpp
+EXTRA_DIST += ext/coroutine/coroutine.h
diff --git a/configure.ac b/configure.ac
index f08f044..139166b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -363,6 +363,57 @@ if test "$lcov" != "no"; then
fi
AC_SUBST(USE_LCOV)
+# Configure log4cxx header and library path
+#
+# If explicitly specified, use it.
+
+AC_ARG_WITH([log4cxx],
+ AC_HELP_STRING([--with-log4cxx=PATH],
+ [specify directory where log4cxx is installed]),
+ [
+ log4cxx_include_path="${withval}/include";
+ log4cxx_library_path="${withval}/lib"
+ ])
+
+# This is an urgent fix to avoid regression due to log4cxx on some
+# platforms. It should be cleaned up with a better fix.
+if test "X$with_log4cxx" != "Xno"; then
+
+# If not specified, try some common paths. These default to
+# /usr/include and /usr/lib if not found
+
+if test -z "$with_log4cxx"; then
+ log4cxxdirs="/usr/local /usr/pkg /opt /opt/local"
+ for d in $log4cxxdirs
+ do
+ if test -d $d/include/log4cxx; then
+ log4cxx_include_path=$d/include
+ log4cxx_library_path=$d/lib
+ break
+ fi
+ done
+fi
+
+CPPFLAGS_SAVES="$CPPFLAGS"
+if test "${log4cxx_include_path}" ; then
+ LOG4CXX_INCLUDES="-I${log4cxx_include_path}"
+ CPPFLAGS="$CPPFLAGS $LOG4CXX_INCLUDES"
+fi
+AC_CHECK_HEADER([log4cxx/logger.h],, AC_MSG_ERROR([Missing log4cxx header files.]))
+CPPFLAGS="$CPPFLAGS_SAVES"
+AC_SUBST(LOG4CXX_INCLUDES)
+
+LOG4CXX_LDFLAGS="-llog4cxx";
+if test "${log4cxx_library_path}"; then
+ LOG4CXX_LDFLAGS="-L${log4cxx_library_path} -llog4cxx"
+fi
+AC_SUBST(LOG4CXX_LDFLAGS)
+
+# The following two lines are part of the urgent fix, and should be cleaned
+# up with a better fix.
+fi
+AM_CONDITIONAL(USE_LOG4CXX, test "X${with_log4cxx}" != "Xno")
+
#
# Configure Boost header path
#
@@ -522,6 +573,9 @@ AC_SUBST(MULTITHREADING_FLAG)
#
CPPFLAGS="$CPPFLAGS -I\$(top_srcdir)/ext/asio"
#
+# Use our 'coroutine' header from ext
+CPPFLAGS="$CPPFLAGS -I\$(top_srcdir)/ext/coroutine"
+#
# Disable threads: Currently we don't use them.
CPPFLAGS="$CPPFLAGS -DASIO_DISABLE_THREADS=1"
#
@@ -615,8 +669,6 @@ AC_CONFIG_FILES([Makefile
src/lib/Makefile
src/lib/asiolink/Makefile
src/lib/asiolink/tests/Makefile
- src/lib/asiolink/internal/Makefile
- src/lib/asiolink/internal/tests/Makefile
src/lib/bench/Makefile
src/lib/bench/example/Makefile
src/lib/bench/tests/Makefile
@@ -652,10 +704,16 @@ AC_CONFIG_FILES([Makefile
src/lib/datasrc/tests/Makefile
src/lib/xfr/Makefile
src/lib/log/Makefile
+ src/lib/log/compiler/Makefile
+ src/lib/log/tests/Makefile
+ src/lib/resolve/Makefile
+ src/lib/resolve/tests/Makefile
src/lib/testutils/Makefile
src/lib/testutils/testdata/Makefile
src/lib/nsas/Makefile
src/lib/nsas/tests/Makefile
+ src/lib/cache/Makefile
+ src/lib/cache/tests/Makefile
])
AC_OUTPUT([doc/version.ent
src/bin/cfgmgr/b10-cfgmgr.py
@@ -711,6 +769,7 @@ AC_OUTPUT([doc/version.ent
src/lib/dns/tests/testdata/gen-wiredata.py
src/lib/cc/session_config.h.pre
src/lib/cc/tests/session_unittests_config.h
+ src/lib/log/tests/run_time_init_test.sh
], [
chmod +x src/bin/cmdctl/run_b10-cmdctl.sh
chmod +x src/bin/xfrin/run_b10-xfrin.sh
@@ -734,6 +793,7 @@ AC_OUTPUT([doc/version.ent
chmod +x src/bin/msgq/tests/msgq_test
chmod +x src/lib/dns/gen-rdatacode.py
chmod +x src/lib/dns/tests/testdata/gen-wiredata.py
+ chmod +x src/lib/log/tests/run_time_init_test.sh
])
AC_OUTPUT
@@ -761,6 +821,8 @@ dnl includes too
${PYTHON_LDFLAGS}
${PYTHON_LIB}
Boost: ${BOOST_INCLUDES}
+ log4cxx: ${LOG4CXX_INCLUDES}
+ ${LOG4CXX_LDFLAGS}
SQLite: $SQLITE_CFLAGS
$SQLITE_LIBS
diff --git a/doc/Doxyfile b/doc/Doxyfile
index 6ab2001..34ec3d8 100644
--- a/doc/Doxyfile
+++ b/doc/Doxyfile
@@ -568,7 +568,7 @@ WARN_LOGFILE =
# directories like "/usr/src/myproject". Separate the files or directories
# with spaces.
-INPUT = ../src/lib/cc ../src/lib/config ../src/lib/dns ../src/lib/exceptions ../src/lib/datasrc ../src/bin/auth ../src/bin/resolver ../src/lib/bench ../src/lib/log ../src/lib/asiolink/ ../src/lib/nsas ../src/lib/testutils
+INPUT = ../src/lib/cc ../src/lib/config ../src/lib/dns ../src/lib/exceptions ../src/lib/datasrc ../src/bin/auth ../src/bin/resolver ../src/lib/bench ../src/lib/log ../src/lib/asiolink/ ../src/lib/nsas ../src/lib/testutils ../src/lib/cache
# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is
diff --git a/ext/coroutine/coroutine.h b/ext/coroutine/coroutine.h
new file mode 100644
index 0000000..985888b
--- /dev/null
+++ b/ext/coroutine/coroutine.h
@@ -0,0 +1,133 @@
+//
+// coroutine.h
+// ~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#ifndef COROUTINE_HPP
+#define COROUTINE_HPP
+
+
+// \brief Coroutine object
+//
+// A coroutine object maintains the state of a re-enterable routine. It
+// is assignable and copy-constructable, and can be used as a base class
+// for a class that uses it, or as a data member. The copy overhead is
+// a single int.
+//
+// A reenterable function contains a CORO_REENTER (coroutine) { ... }
+// block. Whenever an asychrnonous operation is initiated within the
+// routine, the function is provided as the handler object. (The simplest
+// way to do this is to have the reenterable function be the operator()
+// member for the coroutine object itself.) For example:
+//
+// CORO_YIELD socket->async_read_some(buffer, *this);
+//
+// The CORO_YIELD keyword updates the current status of the coroutine to
+// indicate the line number currently being executed. The
+// async_read_some() call is initiated, with a copy of the updated
+// corotutine as its handler object, and the current coroutine exits. When
+// the async_read_some() call finishes, the copied coroutine will be
+// called, and will resume processing exactly where the original one left
+// off--right after asynchronous call. This allows asynchronous I/O
+// routines to be written with a logical flow, step following step, rather
+// than as a linked chain of separate handler functions.
+//
+// When necessary, a coroutine can fork itself using the CORO_FORK keyword.
+// This updates the status of the coroutine and makes a copy. The copy can
+// then be called directly or posted to the ASIO service queue so that both
+// coroutines will continue forward, one "parent" and one "child". The
+// is_parent() and is_child() methods indicate which is which.
+//
+// The CORO_REENTER, CORO_YIELD and CORO_FORK keywords are implemented
+// via preprocessor macros. The CORO_REENTER block is actually a large,
+// complex switch statement. Because of this, inline variable declaration
+// is impossible within CORO_REENTER unless it is done in a subsidiary
+// scope--and if it is, that scope cannot contain CORO_YIELD or CORO_FORK
+// keywords.
+//
+// Because coroutines are frequently copied, it is best to minimize copy
+// overhead by limiting the size of data members in derived classes.
+//
+// It should be noted that when a coroutine falls out of scope its memory
+// is reclaimed, even though it may be scheduled to resume when an
+// asynchronous operation completes. Any shared_ptr<> objects declared in
+// the coroutine may be destroyed if their reference count drops to zero,
+// in which case the coroutine will have serious problems once it resumes.
+// One solution so this is to have the space that will be used by a
+// coroutine pre-allocated and stored on a free list; a new coroutine can
+// fetch the block of space off a free list, place a shared pointer to it
+// on an "in use" list, and carry on. The reference in the "in use" list
+// would prevent the data from being destroyed.
+class coroutine
+{
+public:
+ coroutine() : value_(0) {}
+ virtual ~coroutine() {}
+ bool is_child() const { return value_ < 0; }
+ bool is_parent() const { return !is_child(); }
+ bool is_complete() const { return value_ == -1; }
+ int get_value() const { return value_; }
+private:
+ friend class coroutine_ref;
+ int value_;
+};
+
+class coroutine_ref
+{
+public:
+ coroutine_ref(coroutine& c) : value_(c.value_), modified_(false) {}
+ coroutine_ref(coroutine* c) : value_(c->value_), modified_(false) {}
+ ~coroutine_ref() { if (!modified_) value_ = -1; }
+ operator int() const { return value_; }
+ int& operator=(int v) { modified_ = true; return value_ = v; }
+private:
+ void operator=(const coroutine_ref&);
+ int& value_;
+ bool modified_;
+};
+
+#define CORO_REENTER(c) \
+ switch (coroutine_ref _coro_value = c) \
+ case -1: if (_coro_value) \
+ { \
+ goto terminate_coroutine; \
+ terminate_coroutine: \
+ _coro_value = -1; \
+ goto bail_out_of_coroutine; \
+ bail_out_of_coroutine: \
+ break; \
+ } \
+ else case 0:
+
+#define CORO_YIELD \
+ for (_coro_value = __LINE__;;) \
+ if (_coro_value == 0) \
+ { \
+ case __LINE__: ; \
+ break; \
+ } \
+ else \
+ switch (_coro_value ? 0 : 1) \
+ for (;;) \
+ case -1: if (_coro_value) \
+ goto terminate_coroutine; \
+ else for (;;) \
+ case 1: if (_coro_value) \
+ goto bail_out_of_coroutine; \
+ else case 0:
+
+#define CORO_FORK \
+ for (_coro_value = -__LINE__;; _coro_value = __LINE__) \
+ if (_coro_value == __LINE__) \
+ { \
+ case -__LINE__: ; \
+ break; \
+ } \
+ else
+#endif // COROUTINE_HPP
+
diff --git a/src/bin/auth/auth_srv.cc b/src/bin/auth/auth_srv.cc
index b8d5730..045fe7f 100644
--- a/src/bin/auth/auth_srv.cc
+++ b/src/bin/auth/auth_srv.cc
@@ -354,7 +354,7 @@ AuthSrv::setMemoryDataSrc(const isc::dns::RRClass& rrclass,
uint32_t
AuthSrv::getStatisticsTimerInterval() const {
- return (impl_->statistics_timer_.getInterval());
+ return (impl_->statistics_timer_.getInterval() / 1000);
}
void
@@ -362,11 +362,17 @@ AuthSrv::setStatisticsTimerInterval(uint32_t interval) {
if (interval == impl_->statistics_timer_.getInterval()) {
return;
}
+ if (interval > 86400) {
+ // It can't occur since the value is checked in
+ // statisticsIntervalConfig::build().
+ isc_throw(InvalidParameter, "Too long interval: " << interval);
+ }
if (interval == 0) {
impl_->statistics_timer_.cancel();
} else {
- impl_->statistics_timer_.setupTimer(
- boost::bind(&AuthSrv::submitStatistics, this), interval);
+ impl_->statistics_timer_.setup(boost::bind(&AuthSrv::submitStatistics,
+ this),
+ interval * 1000);
}
if (impl_->verbose_mode_) {
if (interval == 0) {
diff --git a/src/bin/auth/auth_srv.h b/src/bin/auth/auth_srv.h
index 7806be9..4772a02 100644
--- a/src/bin/auth/auth_srv.h
+++ b/src/bin/auth/auth_srv.h
@@ -318,7 +318,8 @@ public:
/// If the specified value is non 0, the \c AuthSrv object will submit
/// its statistics to the statistics module every \c interval seconds.
/// If it's 0, and \c AuthSrv currently submits statistics, the submission
- /// will be disabled.
+ /// will be disabled. \c interval must be equal to or shorter than 86400
+ /// seconds (1 day).
///
/// This method should normally not throw an exception; however, its
/// underlying library routines may involve resource allocation, and
diff --git a/src/bin/auth/benchmarks/Makefile.am b/src/bin/auth/benchmarks/Makefile.am
index 165bb4c..05ab754 100644
--- a/src/bin/auth/benchmarks/Makefile.am
+++ b/src/bin/auth/benchmarks/Makefile.am
@@ -20,5 +20,6 @@ query_bench_LDADD += $(top_builddir)/src/lib/datasrc/libdatasrc.la
query_bench_LDADD += $(top_builddir)/src/lib/config/libcfgclient.la
query_bench_LDADD += $(top_builddir)/src/lib/cc/libcc.la
query_bench_LDADD += $(top_builddir)/src/lib/xfr/libxfr.la
+query_bench_LDADD += $(top_builddir)/src/lib/log/liblog.la
query_bench_LDADD += $(top_builddir)/src/lib/asiolink/libasiolink.la
query_bench_LDADD += $(SQLITE_LIBS)
diff --git a/src/bin/auth/config.cc b/src/bin/auth/config.cc
index 1f258e3..5befc6e 100644
--- a/src/bin/auth/config.cc
+++ b/src/bin/auth/config.cc
@@ -179,9 +179,14 @@ public:
virtual void build(ConstElementPtr config_value) {
const int32_t config_interval = config_value->intValue();
if (config_interval < 0) {
- isc_throw(AuthConfigError, "negative statistics-interval value: "
+ isc_throw(AuthConfigError, "Negative statistics interval value: "
<< config_interval);
}
+ if (config_interval > 86400) {
+ isc_throw(AuthConfigError, "Statistics interval value "
+ << config_interval
+ << " must be equal to or shorter than 86400");
+ }
interval_ = config_interval;
}
virtual void commit() {
diff --git a/src/bin/auth/query.cc b/src/bin/auth/query.cc
index e85c31a..e936c97 100644
--- a/src/bin/auth/query.cc
+++ b/src/bin/auth/query.cc
@@ -141,13 +141,56 @@ Query::process() const {
// Found a zone which is the nearest ancestor to QNAME, set the AA bit
response_.setHeaderFlag(Message::HEADERFLAG_AA);
+ response_.setRcode(Rcode::NOERROR());
while (keep_doing) {
keep_doing = false;
std::auto_ptr<RRsetList> target(qtype_is_any ? new RRsetList : NULL);
- Zone::FindResult db_result =
- result.zone->find(qname_, qtype_, target.get());
+ const Zone::FindResult db_result(result.zone->find(qname_, qtype_,
+ target.get()));
switch (db_result.code) {
+ case Zone::DNAME: {
+ // First, put the dname into the answer
+ response_.addRRset(Message::SECTION_ANSWER,
+ boost::const_pointer_cast<RRset>(db_result.rrset));
+ /*
+ * Empty DNAME should never get in, as it is impossible to
+ * create one in master file.
+ *
+ * FIXME: Other way to prevent this should be done
+ */
+ assert(db_result.rrset->getRdataCount() > 0);
+ // Get the data of DNAME
+ const rdata::generic::DNAME& dname(
+ dynamic_cast<const rdata::generic::DNAME&>(
+ db_result.rrset->getRdataIterator()->getCurrent()));
+ // The yet unmatched prefix dname
+ const Name prefix(qname_.split(0, qname_.getLabelCount() -
+ db_result.rrset->getName().getLabelCount()));
+ // If we put it together, will it be too long?
+ // (The prefix contains trailing ., which will be removed
+ if (prefix.getLength() - Name::ROOT_NAME().getLength() +
+ dname.getDname().getLength() > Name::MAX_WIRE) {
+ /*
+ * In case the synthesized name is too long, section 4.1
+ * of RFC 2672 mandates we return YXDOMAIN.
+ */
+ response_.setRcode(Rcode::YXDOMAIN());
+ return;
+ }
+ // The new CNAME we are creating (it will be unsigned even
+ // with DNSSEC, the DNAME is signed and it can be validated
+ // by that)
+ RRsetPtr cname(new RRset(qname_, db_result.rrset->getClass(),
+ RRType::CNAME(), db_result.rrset->getTTL()));
+ // Construct the new target by replacing the end
+ cname->addRdata(rdata::generic::CNAME(qname_.split(0,
+ qname_.getLabelCount() -
+ db_result.rrset->getName().getLabelCount()).
+ concatenate(dname.getDname())));
+ response_.addRRset(Message::SECTION_ANSWER, cname);
+ break;
+ }
case Zone::CNAME:
/*
* We don't do chaining yet. Therefore handling a CNAME is
@@ -155,10 +198,13 @@ Query::process() const {
* what we expected. It means no exceptions in ANY or NS
* on the origin (though CNAME in origin is probably
* forbidden anyway).
+ *
+ * So, just put it there.
*/
- // No break; here, fall trough.
+ response_.addRRset(Message::SECTION_ANSWER,
+ boost::const_pointer_cast<RRset>(db_result.rrset));
+ break;
case Zone::SUCCESS:
- response_.setRcode(Rcode::NOERROR());
if (qtype_is_any) {
// If quety type is ANY, insert all RRs under the domain
// into answer section.
@@ -184,7 +230,6 @@ Query::process() const {
break;
case Zone::DELEGATION:
response_.setHeaderFlag(Message::HEADERFLAG_AA, false);
- response_.setRcode(Rcode::NOERROR());
response_.addRRset(Message::SECTION_AUTHORITY,
boost::const_pointer_cast<RRset>(db_result.rrset));
getAdditional(*result.zone, *db_result.rrset);
@@ -196,12 +241,8 @@ Query::process() const {
break;
case Zone::NXRRSET:
// Just empty answer with SOA in authority section
- response_.setRcode(Rcode::NOERROR());
putSOA(*result.zone);
break;
- case Zone::DNAME:
- // TODO : replace qname, continue lookup
- break;
}
}
}
diff --git a/src/bin/auth/tests/Makefile.am b/src/bin/auth/tests/Makefile.am
index 7298498..a1114e4 100644
--- a/src/bin/auth/tests/Makefile.am
+++ b/src/bin/auth/tests/Makefile.am
@@ -44,6 +44,7 @@ run_unittests_LDADD += $(top_builddir)/src/lib/config/libcfgclient.la
run_unittests_LDADD += $(top_builddir)/src/lib/cc/libcc.la
run_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libexceptions.la
run_unittests_LDADD += $(top_builddir)/src/lib/xfr/libxfr.la
+run_unittests_LDADD += $(top_builddir)/src/lib/log/liblog.la
endif
noinst_PROGRAMS = $(TESTS)
diff --git a/src/bin/auth/tests/command_unittest.cc b/src/bin/auth/tests/command_unittest.cc
index 0ba5e86..f788d9e 100644
--- a/src/bin/auth/tests/command_unittest.cc
+++ b/src/bin/auth/tests/command_unittest.cc
@@ -98,7 +98,7 @@ AuthConmmandTest::stopServer() {
TEST_F(AuthConmmandTest, shutdown) {
asiolink::IntervalTimer itimer(server.getIOService());
- itimer.setupTimer(boost::bind(&AuthConmmandTest::stopServer, this), 1);
+ itimer.setup(boost::bind(&AuthConmmandTest::stopServer, this), 1);
server.getIOService().run();
EXPECT_EQ(0, rcode);
}
diff --git a/src/bin/auth/tests/config_unittest.cc b/src/bin/auth/tests/config_unittest.cc
index 0e0aee9..b8b379e 100644
--- a/src/bin/auth/tests/config_unittest.cc
+++ b/src/bin/auth/tests/config_unittest.cc
@@ -365,5 +365,9 @@ TEST_F(StatisticsIntervalConfigTest, badInterval) {
EXPECT_THROW(parser->build(Element::fromJSON("2.5")),
isc::data::TypeError);
EXPECT_THROW(parser->build(Element::fromJSON("-1")), AuthConfigError);
+ // bounds check: interval value must be equal to or shorter than
+ // 86400 seconds (1 day)
+ EXPECT_NO_THROW(parser->build(Element::fromJSON("86400")));
+ EXPECT_THROW(parser->build(Element::fromJSON("86401")), AuthConfigError);
}
}
diff --git a/src/bin/auth/tests/query_unittest.cc b/src/bin/auth/tests/query_unittest.cc
index 3bf4142..2d3cf03 100644
--- a/src/bin/auth/tests/query_unittest.cc
+++ b/src/bin/auth/tests/query_unittest.cc
@@ -75,6 +75,17 @@ const char* const cname_nxdom_txt =
// CNAME Leading out of zone
const char* const cname_out_txt =
"cnameout.example.com. 3600 IN CNAME www.example.org.\n";
+// The DNAME to do tests against
+const char* const dname_txt =
+ "dname.example.com. 3600 IN DNAME "
+ "somethinglong.dnametarget.example.com.\n";
+// Some data at the dname node (allowed by RFC 2672)
+const char* const dname_a_txt =
+ "dname.example.com. 3600 IN A 192.0.2.5\n";
+// This is not inside the zone, this is created at runtime
+const char* const synthetized_cname_txt =
+ "www.dname.example.com. 3600 IN CNAME "
+ "www.somethinglong.dnametarget.example.com.\n";
// The rest of data won't be referenced from the test cases.
const char* const other_zone_rrs =
"cnamemailer.example.com. 3600 IN CNAME www.example.com.\n"
@@ -88,13 +99,16 @@ const char* const other_zone_rrs =
// behavior.
// For simplicity, most names are assumed to be "in zone"; there's only
// one zone cut at the point of name "delegation.example.com".
-// It doesn't handle empty non terminal nodes (if we need to test such cases
-// find() should have specialized code for it).
+// Another special name is "dname.example.com". Query names under this name
+// will result in DNAME.
+// This mock zone doesn't handle empty non terminal nodes (if we need to test
+// such cases find() should have specialized code for it).
class MockZone : public Zone {
public:
MockZone() :
origin_(Name("example.com")),
delegation_name_("delegation.example.com"),
+ dname_name_("dname.example.com"),
has_SOA_(true),
has_apex_NS_(true),
rrclass_(RRClass::IN())
@@ -102,7 +116,8 @@ public:
stringstream zone_stream;
zone_stream << soa_txt << zone_ns_txt << ns_addrs_txt <<
delegation_txt << mx_txt << www_a_txt << cname_txt <<
- cname_nxdom_txt << cname_out_txt << other_zone_rrs;
+ cname_nxdom_txt << cname_out_txt << dname_txt << dname_a_txt <<
+ other_zone_rrs;
masterLoad(zone_stream, origin_, rrclass_,
boost::bind(&MockZone::loadRRset, this, _1));
@@ -131,14 +146,20 @@ private:
if (rrset->getName() == delegation_name_ &&
rrset->getType() == RRType::NS()) {
delegation_rrset_ = rrset;
+ } else if (rrset->getName() == dname_name_ &&
+ rrset->getType() == RRType::DNAME()) {
+ dname_rrset_ = rrset;
}
}
const Name origin_;
+ // Names where we delegate somewhere else
const Name delegation_name_;
+ const Name dname_name_;
bool has_SOA_;
bool has_apex_NS_;
ConstRRsetPtr delegation_rrset_;
+ ConstRRsetPtr dname_rrset_;
const RRClass rrclass_;
};
@@ -160,6 +181,10 @@ MockZone::find(const Name& name, const RRType& type,
name.compare(delegation_name_).getRelation() ==
NameComparisonResult::SUBDOMAIN)) {
return (FindResult(DELEGATION, delegation_rrset_));
+ // And under DNAME
+ } else if (name.compare(dname_name_).getRelation() ==
+ NameComparisonResult::SUBDOMAIN) {
+ return (FindResult(DNAME, dname_rrset_));
}
// normal cases. names are searched for only per exact-match basis
@@ -176,8 +201,7 @@ MockZone::find(const Name& name, const RRType& type,
// If not found but we have a target, fill it with all RRsets here
if (!found_domain->second.empty() && target != NULL) {
for (found_rrset = found_domain->second.begin();
- found_rrset != found_domain->second.end(); found_rrset++)
- {
+ found_rrset != found_domain->second.end(); found_rrset++) {
// Insert RRs under the domain name into target
target->addRRset(
boost::const_pointer_cast<RRset>(found_rrset->second));
@@ -443,8 +467,8 @@ TEST_F(QueryTest, CNAME) {
Query(memory_datasrc, Name("cname.example.com"), RRType::A(),
response).process();
- responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
- cname_txt, zone_ns_txt, ns_addrs_txt);
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
+ cname_txt, NULL, NULL);
}
TEST_F(QueryTest, explicitCNAME) {
@@ -465,8 +489,8 @@ TEST_F(QueryTest, CNAME_NX_RRSET) {
Query(memory_datasrc, Name("cname.example.com"), RRType::TXT(),
response).process();
- responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
- cname_txt, zone_ns_txt, ns_addrs_txt);
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
+ cname_txt, NULL, NULL);
}
TEST_F(QueryTest, explicitCNAME_NX_RRSET) {
@@ -488,8 +512,8 @@ TEST_F(QueryTest, CNAME_NX_DOMAIN) {
Query(memory_datasrc, Name("cnamenxdom.example.com"), RRType::A(),
response).process();
- responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
- cname_nxdom_txt, zone_ns_txt, ns_addrs_txt);
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
+ cname_nxdom_txt, NULL, NULL);
}
TEST_F(QueryTest, explicitCNAME_NX_DOMAIN) {
@@ -513,8 +537,8 @@ TEST_F(QueryTest, CNAME_OUT) {
Query(memory_datasrc, Name("cnameout.example.com"), RRType::A(),
response).process();
- responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
- cname_out_txt, zone_ns_txt, ns_addrs_txt);
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
+ cname_out_txt, NULL, NULL);
}
TEST_F(QueryTest, explicitCNAME_OUT) {
@@ -526,4 +550,129 @@ TEST_F(QueryTest, explicitCNAME_OUT) {
cname_out_txt, zone_ns_txt, ns_addrs_txt);
}
+/*
+ * Test a query under a domain with DNAME. We should get a synthetized CNAME
+ * as well as the DNAME.
+ *
+ * TODO: Once we have CNAME chaining, check it works with synthetized CNAMEs
+ * as well. This includes tests pointing inside the zone, outside the zone,
+ * pointing to NXRRSET and NXDOMAIN cases (similarly as with CNAME).
+ */
+TEST_F(QueryTest, DNAME) {
+ Query(memory_datasrc, Name("www.dname.example.com"), RRType::A(),
+ response).process();
+
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
+ (string(dname_txt) + synthetized_cname_txt).c_str(),
+ NULL, NULL);
+}
+
+/*
+ * Ask an ANY query below a DNAME. Should return the DNAME and synthetized
+ * CNAME.
+ *
+ * ANY is handled specially sometimes. We check it is not the case with
+ * DNAME.
+ */
+TEST_F(QueryTest, DNAME_ANY) {
+ Query(memory_datasrc, Name("www.dname.example.com"), RRType::ANY(),
+ response).process();
+
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
+ (string(dname_txt) + synthetized_cname_txt).c_str(), NULL, NULL);
+}
+
+// Test when we ask for DNAME explicitly, it does no synthetizing.
+TEST_F(QueryTest, explicitDNAME) {
+ Query(memory_datasrc, Name("dname.example.com"), RRType::DNAME(),
+ response).process();
+
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
+ dname_txt, zone_ns_txt, ns_addrs_txt);
+}
+
+/*
+ * Request a RRset at the domain with DNAME. It should not synthetize
+ * the CNAME, it should return the RRset.
+ */
+TEST_F(QueryTest, DNAME_A) {
+ Query(memory_datasrc, Name("dname.example.com"), RRType::A(),
+ response).process();
+
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
+ dname_a_txt, zone_ns_txt, ns_addrs_txt);
+}
+
+/*
+ * Request a RRset at the domain with DNAME that is not there (NXRRSET).
+ * It should not synthetize the CNAME.
+ */
+TEST_F(QueryTest, DNAME_NX_RRSET) {
+ EXPECT_NO_THROW(Query(memory_datasrc, Name("dname.example.com"),
+ RRType::TXT(), response).process());
+
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 1, 0,
+ NULL, soa_txt, NULL, mock_zone->getOrigin());
+}
+
+/*
+ * Constructing the CNAME will result in a name that is too long. This,
+ * however, should not throw (and crash the server), but respond with
+ * YXDOMAIN.
+ */
+TEST_F(QueryTest, LongDNAME) {
+ // A name that is as long as it can be
+ Name longname(
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
+ "dname.example.com.");
+ EXPECT_NO_THROW(Query(memory_datasrc, longname, RRType::A(),
+ response).process());
+
+ responseCheck(response, Rcode::YXDOMAIN(), AA_FLAG, 1, 0, 0,
+ dname_txt, NULL, NULL);
+}
+
+/*
+ * Constructing the CNAME will result in a name of maximal length.
+ * This tests that we don't reject valid one by some kind of off by
+ * one mistake.
+ */
+TEST_F(QueryTest, MaxLenDNAME) {
+ Name longname(
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
+ "dname.example.com.");
+ EXPECT_NO_THROW(Query(memory_datasrc, longname, RRType::A(),
+ response).process());
+
+ // Check the answer is OK
+ responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
+ NULL, NULL, NULL);
+
+ // Check that the CNAME has the maximal length.
+ bool ok(false);
+ for (RRsetIterator i(response.beginSection(Message::SECTION_ANSWER));
+ i != response.endSection(Message::SECTION_ANSWER); ++ i) {
+ if ((*i)->getType() == RRType::CNAME()) {
+ ok = true;
+ RdataIteratorPtr ci((*i)->getRdataIterator());
+ ASSERT_FALSE(ci->isLast()) << "The CNAME is empty";
+ /*
+ * Does anybody have a clue why, if the Name::MAX_WIRE is put
+ * directly inside ASSERT_EQ, it fails to link and complains
+ * it is unresolved external?
+ */
+ const size_t max_len(Name::MAX_WIRE);
+ ASSERT_EQ(max_len, dynamic_cast<const rdata::generic::CNAME&>(
+ ci->getCurrent()).getCname().getLength());
+ }
+ }
+ EXPECT_TRUE(ok) << "The synthetized CNAME not found";
+}
+
}
diff --git a/src/bin/bind10/bind10.py.in b/src/bin/bind10/bind10.py.in
index 7594b77..48d5b38 100644
--- a/src/bin/bind10/bind10.py.in
+++ b/src/bin/bind10/bind10.py.in
@@ -195,8 +195,7 @@ class BoB:
"""Boss of BIND class."""
def __init__(self, msgq_socket_file=None, dns_port=5300, address=None,
- forward=None, nocache=False, verbose=False, setuid=None,
- username=None):
+ nocache=False, verbose=False, setuid=None, username=None):
"""
Initialize the Boss of BIND. This is a singleton (only one can run).
@@ -206,11 +205,6 @@ class BoB:
"""
self.address = address
self.dns_port = dns_port
- self.forward = forward
- if forward:
- self.resolver = True
- else:
- self.resolver = False
self.cc_session = None
self.ccs = None
self.cfg_start_auth = True
@@ -422,26 +416,19 @@ class BoB:
"""
Start the Authoritative server
"""
- # XXX: this must be read from the configuration manager in the future
- if self.resolver:
- dns_prog = 'b10-resolver'
- else:
- dns_prog = 'b10-auth'
- dnsargs = [dns_prog]
- if not self.resolver:
- # The resolver uses configuration manager for these
- dnsargs += ['-p', str(self.dns_port)]
- if self.address:
- dnsargs += ['-a', str(self.address)]
- if self.nocache:
- dnsargs += ['-n']
+ authargs = ['b10-auth']
+ authargs += ['-p', str(self.dns_port)]
+ if self.address:
+ authargs += ['-a', str(self.address)]
+ if self.nocache:
+ authargs += ['-n']
if self.uid:
- dnsargs += ['-u', str(self.uid)]
+ authargs += ['-u', str(self.uid)]
if self.verbose:
- dnsargs += ['-v']
+ authargs += ['-v']
# ... and start
- self.start_process("b10-auth", dnsargs, c_channel_env,
+ self.start_process("b10-auth", authargs, c_channel_env,
self.dns_port, self.address)
def start_resolver(self, c_channel_env):
@@ -739,8 +726,6 @@ def check_addr(option, opt_str, value, parser):
try:
if opt_str in ['-a', '--address']:
parser.values.address = isc.net.parse.addr_parse(value)
- elif opt_str in ['-f', '--forward']:
- parser.values.forward = isc.net.parse.addr_parse(value)
else:
raise OptionValueError("Unknown option " + opt_str)
except ValueError:
@@ -761,9 +746,6 @@ def main():
parser.add_option("-a", "--address", dest="address", type="string",
action="callback", callback=check_addr, default=None,
help="address the DNS server will use (default: listen on all addresses)")
- parser.add_option("-f", "--forward", dest="forward", type="string",
- action="callback", callback=check_addr, default=None,
- help="nameserver to which DNS queries should be forwarded")
parser.add_option("-m", "--msgq-socket-file", dest="msgq_socket_file",
type="string", default=None,
help="UNIX domain socket file the b10-msgq daemon will use")
@@ -833,8 +815,8 @@ def main():
# Go bob!
boss_of_bind = BoB(options.msgq_socket_file, options.dns_port,
- options.address, options.forward, options.nocache,
- options.verbose, setuid, username)
+ options.address, options.nocache, options.verbose,
+ setuid, username)
startup_result = boss_of_bind.startup()
if startup_result:
sys.stderr.write("[bind10] Error on startup: %s\n" % startup_result)
diff --git a/src/bin/msgq/tests/msgq_test.py b/src/bin/msgq/tests/msgq_test.py
index efae151..59fcf41 100644
--- a/src/bin/msgq/tests/msgq_test.py
+++ b/src/bin/msgq/tests/msgq_test.py
@@ -132,7 +132,7 @@ class SendNonblock(unittest.TestCase):
task()
# If we got here, then everything worked well and in time
# In that case, we terminate successfully
- sys.exit()
+ sys.exit(0) # needs exit code
else:
(pid, status) = os.waitpid(task_pid, 0)
self.assertEqual(0, status,
diff --git a/src/bin/resolver/Makefile.am b/src/bin/resolver/Makefile.am
index dc6deed..9e86ddd 100644
--- a/src/bin/resolver/Makefile.am
+++ b/src/bin/resolver/Makefile.am
@@ -37,7 +37,7 @@ spec_config.h: spec_config.h.pre
BUILT_SOURCES = spec_config.h
pkglibexec_PROGRAMS = b10-resolver
b10_resolver_SOURCES = resolver.cc resolver.h
-b10_resolver_SOURCES += response_classifier.cc response_classifier.h
+b10_resolver_SOURCES += response_scrubber.cc response_scrubber.h
b10_resolver_SOURCES += $(top_builddir)/src/bin/auth/change_user.h
b10_resolver_SOURCES += $(top_builddir)/src/bin/auth/common.h
b10_resolver_SOURCES += main.cc
diff --git a/src/bin/resolver/resolver.cc b/src/bin/resolver/resolver.cc
index 4fd887e..dfd84a6 100644
--- a/src/bin/resolver/resolver.cc
+++ b/src/bin/resolver/resolver.cc
@@ -21,7 +21,6 @@
#include <cassert>
#include <asiolink/asiolink.h>
-#include <asiolink/ioaddress.h>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
@@ -131,6 +130,9 @@ public:
}
}
+ void resolve(const isc::dns::QuestionPtr& question,
+ const isc::resolve::ResolverInterface::CallbackPtr& callback);
+
void processNormalQuery(const Question& question,
MessagePtr answer_message,
OutputBufferPtr buffer,
@@ -333,7 +335,6 @@ Resolver::~Resolver() {
delete checkin_;
delete dns_lookup_;
delete dns_answer_;
- dlog("Deleting the Resolver",true);
}
void
@@ -352,6 +353,14 @@ Resolver::getConfigSession() const {
}
void
+Resolver::resolve(const isc::dns::QuestionPtr& question,
+ const isc::resolve::ResolverInterface::CallbackPtr& callback)
+{
+ impl_->resolve(question, callback);
+}
+
+
+void
Resolver::processMessage(const IOMessage& io_message,
MessagePtr query_message,
MessagePtr answer_message,
@@ -435,13 +444,20 @@ Resolver::processMessage(const IOMessage& io_message,
}
void
+ResolverImpl::resolve(const QuestionPtr& question,
+ const isc::resolve::ResolverInterface::CallbackPtr& callback)
+{
+ rec_query_->resolve(question, callback);
+}
+
+void
ResolverImpl::processNormalQuery(const Question& question,
MessagePtr answer_message,
OutputBufferPtr buffer,
DNSServer* server)
{
dlog("Processing normal query");
- rec_query_->sendQuery(question, answer_message, buffer, server);
+ rec_query_->resolve(question, answer_message, buffer, server);
}
namespace {
diff --git a/src/bin/resolver/resolver.h b/src/bin/resolver/resolver.h
index 5bce24d..2ae8079 100644
--- a/src/bin/resolver/resolver.h
+++ b/src/bin/resolver/resolver.h
@@ -24,6 +24,8 @@
#include <asiolink/asiolink.h>
+#include <resolve/resolver_interface.h>
+
class ResolverImpl;
/**
@@ -35,7 +37,7 @@ class ResolverImpl;
* answer. It doesn't really know about chasing referrals and similar, it
* simply plugs the parts that know into the network handling code.
*/
-class Resolver {
+class Resolver : public isc::resolve::ResolverInterface {
///
/// \name Constructors, Assignment Operator and Destructor.
///
@@ -51,6 +53,10 @@ public:
~Resolver();
//@}
+ virtual void resolve(
+ const isc::dns::QuestionPtr& question,
+ const isc::resolve::ResolverInterface::CallbackPtr& callback);
+
/// \brief Process an incoming DNS message, then signal 'server' to resume
///
/// A DNS query (or other message) has been received by a \c DNSServer
@@ -59,7 +65,10 @@ public:
/// send the reply.
///
/// \param io_message The raw message received
- /// \param message Pointer to the \c Message object
+ /// \param query_message Pointer to the query Message object we
+ /// received from the client
+ /// \param answer_message Pointer to the anwer Message object we
+ /// shall return to the client
/// \param buffer Pointer to an \c OutputBuffer for the resposne
/// \param server Pointer to the \c DNSServer
void processMessage(const asiolink::IOMessage& io_message,
@@ -140,7 +149,11 @@ public:
* \short Set options related to timeouts.
*
* This sets the time of timeout and number of retries.
- * \param timeout The time in milliseconds. The value -1 disables timeouts.
+ * \param query_timeout The timeout we use for queries we send
+ * \param client_timeout The timeout at which point we send back a
+ * SERVFAIL (while continuing to resolve the query)
+ * \param lookup_timeout The timeout at which point we give up and
+ * stop.
* \param retries The number of retries (0 means try the first time only,
* do not retry).
*/
diff --git a/src/bin/resolver/response_classifier.cc b/src/bin/resolver/response_classifier.cc
deleted file mode 100644
index 2d21407..0000000
--- a/src/bin/resolver/response_classifier.cc
+++ /dev/null
@@ -1,259 +0,0 @@
-// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-// $Id$
-
-#include <cstddef>
-#include <vector>
-
-#include <resolver/response_classifier.h>
-#include <dns/name.h>
-#include <dns/opcode.h>
-#include <dns/rcode.h>
-#include <dns/rrset.h>
-
-using namespace isc::dns;
-using namespace std;
-
-// Classify the response in the "message" object.
-
-ResponseClassifier::Category ResponseClassifier::classify(
- const Question& question, const MessagePtr& message, bool tcignore)
-{
- // Check header bits
- if (!message->getHeaderFlag(Message::HEADERFLAG_QR)) {
- return (NOTRESPONSE); // Query-response bit not set in the response
- }
-
- // We only recognise responses to queries here
- if (message->getOpcode() != Opcode::QUERY()) {
- return (OPCODE);
- }
-
- // Apparently have a response. There must be a single question in it...
- const vector<QuestionPtr> msgquestion(message->beginQuestion(),
- message->endQuestion());
- if (msgquestion.size() != 1) {
- return (NOTONEQUEST); // Not one question in response question section
- }
-
- // ... and the question should be equal to the question given.
- // XXX: This means that "question" may not be the question sent by the
- // client. In the case of a CNAME response, the qname of subsequent
- // questions needs to be altered.
- if (question != *(msgquestion[0])) {
- return (MISMATQUEST);
- }
-
- // Check for Rcode-related errors.
- const Rcode& rcode = message->getRcode();
- if (rcode != Rcode::NOERROR()) {
- if (rcode == Rcode::NXDOMAIN()) {
-
- // No such domain. According to RFC2308, the domain referred to by
- // the QNAME does not exist, although there may be a CNAME in the
- // answer section and there may be an SOA and/or NS RRs in the
- // authority section (ignoring any DNSSEC RRs for now).
- //
- // Note the "may". There may not be anything. Also, note that if
- // there is a CNAME in the answer section, the authoritative server
- // has verified that the name given in the CNAME's RDATA field does
- // not exist. And that if a CNAME is returned in the answer, then
- // the QNAME of the RRs in the authority section will refer to the
- // authority for the CNAME's RDATA and not to the original question.
- //
- // Without doing further classification, it is sufficient to say
- // that if an NXDOMAIN is received, there was no translation of the
- // QNAME available.
- return (NXDOMAIN); // Received NXDOMAIN from parent.
-
- } else {
-
- // Not NXDOMAIN but not NOERROR either. Must be an RCODE-related
- // error.
- return (RCODE);
- }
- }
-
- // All seems OK and we can start looking at the content. However, one
- // more header check remains - was the response truncated? If so, we'll
- // probably want to re-query over TCP. However, in some circumstances we
- // might want to go with what we have. So give the caller the option of
- // ignoring the TC bit.
- if (message->getHeaderFlag(Message::HEADERFLAG_TC) && (!tcignore)) {
- return (TRUNCATED);
- }
-
- // By the time we get here, we're assured that the packet format is correct.
- // We now need to decide as to whether it is an answer, a CNAME, or a
- // referral. For this, we need to inspect the contents of the answer
- // and authority sections.
- const vector<RRsetPtr> answer(
- message->beginSection(Message::SECTION_ANSWER),
- message->endSection(Message::SECTION_ANSWER)
- );
- const vector<RRsetPtr> authority(
- message->beginSection(Message::SECTION_AUTHORITY),
- message->endSection(Message::SECTION_AUTHORITY)
- );
-
- // If there is nothing in the answer section, it is a referral - unless
- // there is nothing in the authority section
- if (answer.empty()) {
- if (authority.empty()) {
- return (EMPTY);
- } else {
- return (REFERRAL);
- }
- }
-
- // Look at two cases - one RRset in the answer and multiple RRsets in
- // the answer.
- if (answer.size() == 1) {
-
- // Does the name and class of the answer match that of the question?
- if ((answer[0]->getName() == question.getName()) &&
- (answer[0]->getClass() == question.getClass())) {
-
- // It does. How about the type of the response? The response
- // is an answer if the type matches that of the question, or if the
- // question was for type ANY. It is a CNAME reply if the answer
- // type is CNAME. And it is an error for anything else.
- if ((answer[0]->getType() == question.getType()) ||
- (question.getType() == RRType::ANY())) {
- return (ANSWER);
- } else if (answer[0]->getType() == RRType::CNAME()) {
- return (CNAME);
- } else {
- return (INVTYPE);
- }
- }
- else {
-
- // Either the name and/or class of the reply don't match that of
- // the question.
- return (INVNAMCLASS);
- }
- }
-
- // There are multiple RRsets in the answer. They should all have the same
- // QCLASS, else there is some error in the response.
- for (int i = 1; i < answer.size(); ++i) {
- if (answer[0]->getClass() != answer[i]->getClass()) {
- return (MULTICLASS);
- }
- }
-
- // If the request type was ANY and they all have the same QNAME, we have
- // an answer. But if they don't have the same QNAME, we must have an error;
- // the only way we could get different QNAMES in an answer is if one were a
- // CNAME - in which case there should no other record types at that QNAME.
- if (question.getType() == RRType::ANY()) {
- bool all_same = true;
- for (int i = 1; (i < answer.size()) && all_same; ++i) {
- all_same = (answer[0]->getName() == answer[i]->getName());
- }
- if (all_same) {
- return (ANSWER);
- } else {
- return (EXTRADATA);
- }
- }
-
- // Multiple RRs in the answer, and not all the same QNAME. This
- // is either an answer, a CNAME (in either case, there could be multiple
- // CNAMEs in the chain) or an error.
- //
- // So we need to follow the CNAME chain to resolve this. For this to work:
- //
- // a) There must be one RR that matches the name, class and type of
- // the question, and this is a CNAME.
- // b) The CNAME chain is followed until the end of the chain does not
- // exist (answer is a CNAME) or it is not of type CNAME (ANSWER).
- //
- // In the latter case, if there are additional RRs, it must be an error.
-
- vector<RRsetPtr> ansrrset(answer);
- vector<int> present(ansrrset.size(), 1);
- return cnameChase(question.getName(), question.getType(), ansrrset, present,
- ansrrset.size());
-}
-
-// Search the CNAME chain.
-ResponseClassifier::Category ResponseClassifier::cnameChase(
- const Name& qname, const RRType& qtype, vector<RRsetPtr>& ansrrset,
- vector<int>& present, size_t size)
-{
- // Search through the vector of RRset pointers until we find one with the
- // right QNAME.
- for (int i = 0; i < ansrrset.size(); ++i) {
- if (present[i]) {
-
- // This entry has not been logically removed, so look at it.
- if (ansrrset[i]->getName() == qname) {
-
- // QNAME match. If this RRset is a CNAME, remove it from
- // further consideration. If nothing is left, the end of the
- // chain is a CNAME so this is a CNAME. Otherwise replace
- // the name with the RDATA of the CNAME and call ourself
- // recursively.
- if (ansrrset[i]->getType() == RRType::CNAME()) {
-
- // Don't consider it in the next iteration (although we
- // can still access it for now).
- present[i] = 0;
- --size;
- if (size == 0) {
- return (CNAME);
- }
- else {
- if (ansrrset[i]->getRdataCount() != 1) {
-
- // Multiple RDATA for a CNAME? This is invalid.
-
- return (NOTSINGLE);
- }
- RdataIteratorPtr it = ansrrset[i]->getRdataIterator();
- Name newname(it->getCurrent().toText());
-
- return cnameChase(newname, qtype, ansrrset, present,
- size);
- }
-
- } else {
-
- // We've got here because the element is not a CNAME. If
- // this is the last element and the type is the one we are
- // after, we've found the answer, or it is an error. If
- // there is more than one RRset left in the list we are
- // searching, we have extra data in the answer.
- if (ansrrset[i]->getType() == qtype) {
- if (size == 1) {
- return (ANSWERCNAME);
- } else {
- return (EXTRADATA);
- }
- }
- return (INVTYPE);
- }
- }
- }
- }
-
- // We get here if we've dropped off the end of the list without finding the
- // QNAME we are looking for. This means that the CNAME chain has ended
- // but there are additional RRsets in the data.
-
- return (EXTRADATA);
-}
diff --git a/src/bin/resolver/response_classifier.h b/src/bin/resolver/response_classifier.h
deleted file mode 100644
index 394d60f..0000000
--- a/src/bin/resolver/response_classifier.h
+++ /dev/null
@@ -1,138 +0,0 @@
-// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-// $Id$
-
-#ifndef __RESPONSE_CLASSIFIER_H
-#define __RESPONSE_CLASSIFIER_H
-
-#include <cstddef>
-
-#include <dns/question.h>
-#include <dns/message.h>
-#include <dns/question.h>
-
-/// \brief Classify Server Response
-///
-/// This class is used in the recursive server. It is passed an answer received
-/// from an upstream server and categorises it.
-///
-/// TODO: It is unlikely that the code can be used in this form. Some adaption
-/// of it will be required to put it in the server.
-///
-/// TODO: The code here does not take into account any EDNS0 fields.
-
-class ResponseClassifier {
-public:
-
- /// \brief Category of Answer
- ///
- /// In the valid answers, not the distinction between REFERRAL and CNAME.
- /// A REFERRAL answer means that the answer section of the message is
- /// empty, but there is something in the authority section. A CNAME means
- /// that the answer section contains one or more CNAMES in a chain that
- /// do not end with a non-CNAME RRset.
- enum Category {
-
- // Codes indicating that a message is valid.
-
- ANSWER, ///< Response contains the answer
- ANSWERCNAME, ///< Response was a CNAME chain ending in an answer
- CNAME, ///< Response was a CNAME
- NXDOMAIN, ///< Response was an NXDOMAIN
- REFERRAL, ///< Response contains a referral
-
- // Codes indicating that a message is invalid. Note that the error()
- // method relies on these appearing after the "message valid" codes.
-
- EMPTY, ///< No answer or authority sections
- EXTRADATA, ///< Answer section contains more RRsets than needed
- INVNAMCLASS, ///< Invalid name or class in answer
- INVTYPE, ///< Name/class of answer correct, type is wrong
- MISMATQUEST, ///< Response question section != question
- MULTICLASS, ///< Multiple classes in multi-RR answer
- NOTONEQUEST, ///< Not one question in response question section
- NOTRESPONSE, ///< Response has the Query/Response bit clear
- NOTSINGLE, ///< CNAME has multiple RDATA elements.
- OPCODE, ///< Opcode field does not indicate a query
- RCODE, ///< RCODE indicated an error
- TRUNCATED ///< Response was truncated
- };
-
- /// \brief Check Error
- ///
- /// An inline routine to quickly classify whether the return category is
- /// an error or not. This makes use of internal knowledge of the order of
- /// codes in the Category enum.
- ///
- /// \param code Return category from classify()
- ///
- /// \return true if the category is an error, false if not.
- static bool error(Category code) {
- return (code > REFERRAL);
- }
-
- /// \brief Classify
- ///
- /// Classify the response in the "message" object.
- ///
- /// \param question Question that was sent to the server
- /// \param message Pointer to the associated response from the server.
- /// \param tcignore If set, the TC bit in a response packet is
- /// ignored. Otherwise the error code TRUNCATED will be returned. The
- /// only time this is likely to be used is in development where we are not
- /// going to fail over to TCP and will want to use what is returned, even
- /// if some of the response was lost.
- static Category classify(const isc::dns::Question& question,
- const isc::dns::MessagePtr& message, bool tcignore = false);
-
-private:
- /// \brief Follow CNAMEs
- ///
- /// Given a QNAME and an answer section that contains CNAMEs, assume that
- /// they form a CNAME chain and search through them. Possible outcomes
- /// are:
- ///
- /// a) All CNAMES and they form a chain. The result is a referral.
- /// b) All but one are CNAMES and they form a chain. The other is pointed
- /// to by the last element of the chain and is the correct QTYPE. The
- /// result is an answer.
- /// c) Having followed the CNAME chain as far as we can, there is one
- /// remaining RRset that is of the wrong type, or there are multiple
- /// RRsets remaining. return the EXTRADATA code.
- ///
- /// \param qname Question name we are searching for
- /// \param qtype Question type we are search for. (This is assumed not
- /// to be "ANY".)
- /// \param ansrrset Vector of RRsetPtr pointing to the RRsets we are
- /// considering.
- /// \param present Array of "int" the same size of ansrrset, with each
- /// element set to "1" to allow the corresponding element of ansrrset to
- /// be checked, and "0" to skip it. This might be premature optimisation,
- /// but the algorithm would otherwise involve duplicating the RRset
- /// vector then removing elements from random positions one by one. As
- /// each removal involves the destruction of an "xxxPtr" element (which
- /// presently is implemented by boost::shared_ptr), the overhad of memory
- /// management seemed high. This solution imposes some additional loop
- /// cycles, but that should be minimal compared with the overhead of the
- /// memory management.
- /// \param size Number of elements to check. See description of \c present
- /// for details.
- static Category cnameChase(const isc::dns::Name& qname,
- const isc::dns::RRType& qtype,
- std::vector<isc::dns::RRsetPtr>& ansrrset, std::vector<int>& present,
- size_t size);
-};
-
-#endif // __RESPONSE_CLASSIFIER_H
diff --git a/src/bin/resolver/response_scrubber.cc b/src/bin/resolver/response_scrubber.cc
new file mode 100644
index 0000000..060a8b1
--- /dev/null
+++ b/src/bin/resolver/response_scrubber.cc
@@ -0,0 +1,189 @@
+
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <iostream>
+#include <vector>
+#include <dns/message.h>
+#include <dns/rrset.h>
+#include <dns/name.h>
+#include "response_scrubber.h"
+
+using namespace isc::dns;
+using namespace std;
+
+// Compare addresses etc.
+
+ResponseScrubber::Category ResponseScrubber::addressCheck(
+ const asiolink::IOEndpoint& to, const asiolink::IOEndpoint& from)
+{
+ if (from.getProtocol() == to.getProtocol()) {
+ if (from.getAddress() == to.getAddress()) {
+ if (from.getPort() == to.getPort()) {
+ return (ResponseScrubber::SUCCESS);
+ } else {
+ return (ResponseScrubber::PORT);
+ }
+ } else {
+ return (ResponseScrubber::ADDRESS);
+ }
+ }
+ return (ResponseScrubber::PROTOCOL);
+}
+
+// Do a general scrubbing. The QNAMES of RRsets in the specified section are
+// compared against the list of name given and if they are not equal and not in
+// the specified relationship (generally superdomain or subdomain) to at least
+// of of the given names, they are removed.
+
+unsigned int
+ResponseScrubber::scrubSection(Message& message,
+ const vector<const Name*>& names,
+ const NameComparisonResult::NameRelation connection,
+ const Message::Section section)
+{
+ unsigned int count = 0; // Count of RRsets removed
+ unsigned int kept = 0; // Count of RRsets kept
+ bool removed = true; // Set true if RRset removed in a pass
+
+ // Need to go through the section multiple times as when an RRset is
+ // removed, all iterators into the section are invalidated. This condition
+ // is flagged by "remove" being set true when an RRset is removed.
+
+ while (removed) {
+ RRsetIterator i = message.beginSection(section);
+
+ // Skips the ones that have been checked (and retained) in a previous
+ // pass through the "while" loop. (Although RRset removal invalidates
+ // iterators, it does not change the relative order of the retained
+ // RRsets in the section.)
+ for (int j = 0; j < kept; ++j) {
+ ++i;
+ }
+
+ // Start looking at the remaining entries in the section.
+ removed = false;
+ for (; (i != message.endSection(section)) && (!removed); ++i) {
+
+ // Loop through the list of names given and see if any are in the
+ // given relationship with the QNAME of this RRset
+ bool nomatch = true;
+ for (vector<const Name*>::const_iterator n = names.begin();
+ ((n != names.end()) && nomatch); ++n) {
+ NameComparisonResult result = (*i)->getName().compare(**n);
+ NameComparisonResult::NameRelation relationship =
+ result.getRelation();
+ if ((relationship == NameComparisonResult::EQUAL) ||
+ (relationship == connection)) {
+
+ // RRset in the specified relationship, so a match has
+ // been found
+ nomatch = false;
+ }
+ }
+
+ // Remove the RRset if there was no match to one of the given names.
+ if (nomatch) {
+ message.removeRRset(section, i);
+ ++count; // One more RRset removed
+ removed = true; // Something was removed
+ } else {
+
+ // There was a match so this is one more entry we can skip next
+ // time.
+ ++kept;
+ }
+ }
+ }
+
+ return count;
+}
+
+// Perform the scrubbing of all sections of the message.
+
+unsigned int
+ResponseScrubber::scrubAllSections(Message& message, const Name& bailiwick) {
+
+ // Leave the question section alone. Just go through the RRsets in the
+ // answer, authority and additional sections.
+ unsigned int count = 0;
+ const vector<const Name*> bailiwick_names(1, &bailiwick);
+ count += scrubSection(message, bailiwick_names,
+ NameComparisonResult::SUBDOMAIN, Message::SECTION_ANSWER);
+ count += scrubSection(message, bailiwick_names,
+ NameComparisonResult::SUBDOMAIN, Message::SECTION_AUTHORITY);
+ count += scrubSection(message, bailiwick_names,
+ NameComparisonResult::SUBDOMAIN, Message::SECTION_ADDITIONAL);
+
+ return count;
+}
+
+// Scrub across sections.
+
+unsigned int
+ResponseScrubber::scrubCrossSections(isc::dns::Message& message) {
+
+ // Get a list of the names in the answer section or, failing this, the
+ // question section. Note that pointers to the names within "message" are
+ // stored; this is OK as the relevant sections in "message" will not change
+ // during the lifetime of this method (it only affects the authority
+ // section).
+ vector<const Name*> source;
+ if (message.getRRCount(Message::SECTION_ANSWER) != 0) {
+ for (RRsetIterator i = message.beginSection(Message::SECTION_ANSWER);
+ i != message.endSection(Message::SECTION_ANSWER); ++i) {
+ const Name& qname = (*i)->getName();
+ source.push_back(&qname);
+ }
+
+ } else {
+ for (QuestionIterator i = message.beginQuestion();
+ i != message.endQuestion(); ++i) {
+ const Name& qname = (*i)->getName();
+ source.push_back(&qname);
+ }
+ }
+
+ if (source.empty()) {
+ // TODO: Log the fact - should be at least a question present
+ return (0);
+ }
+
+ // Could be duplicates, especially in the answer section, so sort the
+ // names and remove them.
+ sort(source.begin(), source.end(), ResponseScrubber::compareNameLt);
+ vector<const Name*>::iterator endunique =
+ unique(source.begin(), source.end(), ResponseScrubber::compareNameEq);
+ source.erase(endunique, source.end());
+
+ // Now purge the authority section of RRsets that are not equal to or a
+ // superdomain of the names in the question/answer section.
+ return (scrubSection(message, source,
+ NameComparisonResult::SUPERDOMAIN, Message::SECTION_AUTHORITY));
+
+}
+
+// Scrub a message
+
+unsigned int
+ResponseScrubber::scrub(const isc::dns::MessagePtr& message,
+ const isc::dns::Name& bailiwick)
+{
+ unsigned int sections_removed = scrubAllSections(*message, bailiwick);
+ sections_removed += scrubCrossSections(*message);
+
+ return sections_removed;
+}
+
+
diff --git a/src/bin/resolver/response_scrubber.h b/src/bin/resolver/response_scrubber.h
new file mode 100644
index 0000000..c3fce57
--- /dev/null
+++ b/src/bin/resolver/response_scrubber.h
@@ -0,0 +1,422 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __RESPONSE_SCRUBBER_H
+#define __RESPONSE_SCRUBBER_H
+
+/// \page DataScrubbing Data Scrubbing
+/// \section DataScrubbingIntro Introduction
+/// When a response is received from an authoritative server, it should be
+/// checked to ensure that the data contained in it is valid. Signed data is
+/// not a problem - validating the signatures is a sufficient check. But
+/// unsigned data in a response is more of a problem. (Note that even data from
+/// signed zones may be not be signed, e.g. delegations are not signed.) In
+/// particular, how do we know that the server from which the response was
+/// received was authoritive for the data it returned?
+///
+/// The part of the code that checks for this is the "Data Scrubbing" module.
+/// Although it includes the checking of IP addresses and ports, it is called
+/// "Scrubbing" because it "scrubs" the returned message and removes doubtful
+/// information.
+///
+/// \section DataScrubbingBasic Basic Checks
+/// The first part - how do we know that the response comes from the correct
+/// server - is relatively trivial, albeit not foolproof (which is why DNSSEC
+/// was developed). The following are checked:
+///
+/// - The IP address from which the response was received is the same as the
+/// one to which the query was sent.
+/// - The port on which the response was received is the same as the one from
+/// which the query was sent.
+///
+/// (These tests need not not done for a TCP connection - if data is received
+/// over the TCP stream, it is assumed that it comes from the address and port
+/// to which a connection was made.)
+///
+/// - The protocol used to send the question is the same as the protocol on
+/// which an answer was received.
+///
+/// (Strictly speaking, if this check fails it is a programming error - the
+/// code should not mix up UPD and TCP messages.)
+///
+/// - The QID in the response message is the same as the QID in the query
+/// message sent.
+///
+/// If the conditions are met, then the data - in all three response sections -
+/// is scanned and out of bailiwick data is removed ("scrubbed").
+///
+/// \section DataScrubbingBailiwick Bailiwick
+/// Bailiwick means "district or jurisdiction of bailie or bailiff" (Concise
+/// Oxford Dictionary, 7th Edition). It is not a term mentioned in any RFC
+/// (or at least, any RFC up to RFC 5997) but is widely used in DNS literature.
+/// In this context it is taken to mean the data for which a DNS server has
+/// authority. So when we speak of the information being "in bailiwick", we
+/// mean that the the server is the ultimate source of authority for that data.
+///
+/// In practice, determining this from the response alone is difficult. In
+/// particular, as a server may be authoritative for many zones, it could in
+/// theory be authoritative for any combination of RRsets that appear in a
+/// response.
+///
+/// For this reason, bailiwick is dependent on the query. If, for example, a
+/// query for www.example.com is sent to the nameservers for example.com
+/// (because of a referral of from the com. servers), the bailiwick for the
+/// query is example.com. This means that any information returned on domains
+/// other than example.com may not be authoritative. More exactly, it may be
+/// authoritative (because the server is also authoritative for the zone
+/// concerned), but based on the information available (in this example, that
+/// the response originated from a nameserver for the zone example.com) it is
+/// not possible to be certain.
+///
+/// Ideally, out of bailiwick data should be excluded from further processing
+/// as it may be incorrect and corrupt the cache. In practice, there are
+/// two cases to consider:
+///
+/// The first is when the data has a qname that is not example.com or a
+/// subdomain of it (e.g. xyz.com, www.example.net). In this case the data can
+/// be retrieved by an independent query - no path from the root zone to the
+/// data goes through the current bailiwick, so there is no chance of ending up
+/// in a loop. In this case, data that appears to be out of bailiwick can be
+/// dropped from the response.
+///
+/// The second case is when the QNAME of the data is a subdomain of the
+/// bailiwick. Here the server may or may not be authoritative for the data.
+/// For example, if the name queried for were www.sub.example.com and the
+/// example.com nameservers supplied an answer:
+///
+/// - The answer could be authoritative - www.sub.example.com could be
+/// in the example.com zone.
+/// - The answer might not be authoritative - the zone sub.example.com may have
+/// been delegated, so the authoritative answer should come from
+/// sub.example.com's nameservers.
+/// - The answer might be authoritative even though zone sub.example.com has
+/// been delegated, because the nameserver for example.com is the same as
+/// that for sub.example.com.
+///
+/// Unlike the previous case, it is not possible to err on the side of caution
+/// and drop such data. Any independent query for it will pass through the
+/// current bailiwick and the same question will be asked again. For this
+/// reason, any data in the response that has a QNAME equal to a subdomain of
+/// the bailiwick has to be accepted.
+///
+/// In summary then, data in a response that has a QNAME equal to or a subdomain
+/// of the bailiwick is considered in-bailiwick. Anything else is out of of
+/// bailiwick.
+///
+/// \subsection DataScrubbingCrossSection Cross-Section Scrubbing
+/// Even with the bailiwick checks above, there are some additional cleaning
+/// that can be done with the packet. In particular:
+///
+/// - The QNAMEs of the RRsets in the authority section must be equal to or
+/// superdomains of a QNAME of an RRset in the answer. Any that are not
+/// should be removed.
+/// - If there is no answer section, the QNAMES of RRsets in the authority
+/// section must be equal to or superdomains of the QNAME of the RRset in the
+/// question.
+///
+/// Although previous checks should have removed some inconsistencies, it
+/// will not trap obscure cases (e.g. bailiwick: "example.com", answer:
+/// "www.example.com", authority: sub.example.com). These checks do just that.
+///
+/// (Note that not included here is QNAME of question not equal to or a
+/// superdomain of the answer; that check is made in the ResponseClassifier
+/// class.)
+///
+/// \section DataScrubbingExample Examples
+/// Some examples should make this clear: they all use the notation
+/// Qu = Question, Zo = Zone being queried, An = Answer, Au = Authority,
+/// Ad = Additional.
+///
+/// \subsection DataScrubbingEx1 Example 1: Simple Query
+/// Querying a nameserver for the zone "example.com" for www.example.com and
+/// receiving the answer "www.example.com A 1.2.3.4" with two nameservers quoted
+/// as authority and both their addresses in the additional section:
+///
+/// Qu: www.example.com\n
+/// Zo: example.com
+///
+/// An: www.example.com A 192.0.2.1
+///
+/// Au(1): example.com NS ns0.example.com\n
+/// Au(2): example.com NS ns1.example.net
+///
+/// Ad(1): ns0.example.com A 192.0.2.100\n
+/// Ad(2): ns1.example.net A 192.0.2.200
+///
+/// This answer could be returned by a properly configured server. All resource
+/// records in the answer - with the exception of Ad(2) - are in bailiwick
+/// because the QNAME is equal to or a subdomain of the zone being queried.
+///
+/// It is permissible for Ad(2) to be returned by a properly configured server
+/// as a hint to resolvers. However the example.com nameservers are not
+/// authoritative for addresses of domains in example.net; that record could
+/// be out of date or incorrect. Indeed, it might even be a deliberate attempt
+/// at a spoof by getting us to cache an invalid address for ns1.example.net.
+/// The safest thing to do is to drop the A record and to get the address of
+/// ns1.example.net by querying for that name through the .net nameservers.
+///
+/// \subsection DataScrubbingEx2 Example 2: Multiple Zones on Same Nameserver
+/// Assume now that example.com and sub.example.com are hosted on the same
+/// nameserver and that from the .com zone the resolver has received a referral
+/// to example.com. Suppose that the query is for www.sub.example.com and that
+/// the following response is received:
+///
+/// Qu: www.sub.example.com\n
+/// Zo: example.com
+///
+/// An: <nothing>
+///
+/// Au(1): sub.example.com NS ns0.sub.example.com\n
+/// Au(2): sub.example.com NS ns1.example.net
+///
+/// Ad(1): ns0.sub.example.com A 192.0.2.101\n
+/// Ad(2): ns1.example.net A 192.0.2.201
+///
+/// Although we asked the example.com nameservers for information, we got the
+/// nameservers for sub.example.com in the authority section. This is valid
+/// because if BIND-10 hosts multiple zones, it will look up the data in the
+/// zone that most closely matches the query.
+///
+/// Using the criteria above, the data in the additional section can therefore
+/// be regarded as in bailiwick because sub.example.com is a subdomain of
+/// example.com. As before though, the address for ns1.example.net in the
+/// additional section is not in bailiwick because ns1.example.net is now a
+/// subdomain of example.com.
+///
+/// \subsection DataScrubbingEx3 Example 3: Deliberate Spoof Attempt
+/// Qu: www.example.com\n
+/// Zo: example.com
+///
+/// An: www.example.com A 192.0.2.1
+///
+/// Au(1): com NS ns0.example.com\n
+/// Au(2): com NS ns1.example.net
+///
+/// Ad(1): ns0.example.com A 192.0.2.100\n
+/// Ad(2): ns1.example.net A 192.0.2.200
+///
+/// This is a deliberately invalid response. The query is being sent to the
+/// nameservers for example.com (presumably because a referral to example.com
+/// was received from the com nameservers), but the response is an attempt
+/// to get the specified nameservers cached as the nameservers for com - for
+/// which example.com is not authoritative.
+///
+/// Note though that this response is only invalid because, due to the previous
+/// referral, the query was sent to the example.com nameservers. Had the
+/// referral been to the com nameservers, it would be a valid response; the com
+/// zone could well be serving all the data for example.com. Having said that,
+/// the A record for ns1.example.net would still be regarded as being out of
+/// bailiwick becase the nameserver is not authoritative for the .net zone.
+///
+/// \subsection DataScrubbingEx4 Example 4: Inconsistent Answer Section
+/// Qu: www.example.com\n
+/// Zo: example.com
+///
+/// An: www.example.com A 192.0.2.1
+///
+/// Au(1): alpha.example.com NS ns0.example.com\n
+/// Au(2): alpha.example.com NS ns1.example.net
+///
+/// Ad(1): ns0.example.com A 192.0.2.100\n
+/// Ad(2): ns1.example.net A 192.0.2.200
+///
+/// Here, everything in the answer and authority sections is in bailiwick for
+/// the example.com server. And although the zone example.com was queried, it
+/// is permissible for the authority section to contain nameservers with a
+/// qname that is a subdomain of example.com (e.g. see \ref DataScrubbingEx2).
+/// However, only servers with a qname that is equal to or a superdomain of
+/// the answer are authoritative for the answer. So in this case, both
+/// Au(1) and Au(2) (as well as Ad(2), for reasons given earlier) will be
+/// scrubbed.
+
+#include <config.h>
+#include <asiolink/io_endpoint.h>
+#include <dns/message.h>
+#include <dns/name.h>
+
+/// \brief Response Data Scrubbing
+///
+/// This is the class that implements the data scrubbing. Given a response
+/// message and some additional information, it checks the information using
+/// the rules given in \ref DataScrubbing and either rejects the packet or
+/// modifies it to remove non-conforming RRsets.
+///
+/// TODO: Examine the additional records and remove all cases where the
+/// QNAME does not match the RDATA of records in the authority section.
+
+class ResponseScrubber {
+public:
+
+ /// \brief Response Code for Address Check
+ enum Category {
+ SUCCESS = 0, ///< Packet is OK
+
+ // Error categories
+
+ ADDRESS = 1, ///< Mismatching IP address
+ PORT = 2, ///< Mismatching port
+ PROTOCOL = 3 ///< Mismatching protocol
+ };
+
+ /// \brief Check IP Address
+ ///
+ /// Compares the address to which the query was sent, the port it was
+ /// sent from, and the protocol used for communication with the (address,
+ /// port, protocol) from which the response was received.
+ ///
+ /// \param to Endpoint representing the address to which the query was sent.
+ /// \param from Endpoint from which the response was received.
+ ///
+ /// \return SUCCESS if the two endpoints match, otherwise an error status
+ /// indicating what was incorrect.
+ static Category addressCheck(const asiolink::IOEndpoint& to,
+ const asiolink::IOEndpoint& from);
+
+ /// \brief Check QID
+ ///
+ /// Compares the QID in the sent message with the QID in the response.
+ ///
+ /// \param sent Message sent to the authoritative server
+ /// \param received Message received from the authoritative server
+ ///
+ /// \return true if the QIDs match, false otherwise.
+ static bool qidCheck(const isc::dns::Message& sent,
+ const isc::dns::Message& received) {
+ return (sent.getQid() == received.getQid());
+ }
+
+ /// \brief Generalised Scrub Message Section
+ ///
+ /// When scrubbing a message given the bailiwick of the server, RRsets are
+ /// retained in the message section if the QNAME is equal to or a subdomain
+ /// of the bailiwick. However, when checking QNAME of RRsets in the
+ /// authority section against the QNAME of the question or answers, RRsets
+ /// are retained only if their QNAME is equal to or a superdomain of the
+ /// name in question.
+ ///
+ /// This method provides the generalised scrubbing whereby the RRsets in
+ /// a section are tested against a given name, and RRsets kept if their
+ /// QNAME is equal to or in the supplied relationship with the given name.
+ ///
+ /// \param section Section of the message to be scrubbed.
+ /// \param zone Names against which RRsets should be checked. Note that
+ /// this is a vector of pointers to Name objects; they are assumed to
+ /// independently exist, and the caller retains ownership of them and is
+ /// assumed to destroy them when needed.
+ /// \param connection Relationship required for retention, i.e. the QNAME of
+ /// an RRset in the specified section must be equal to or a "connection"
+ /// (SUPERDOMAIN/SUBDOMAIN) of "name" for the RRset to be retained.
+ /// \param message Message to be scrubbed.
+ ///
+ /// \return Count of the number of RRsets removed from the section.
+ static unsigned int scrubSection(isc::dns::Message& message,
+ const std::vector<const isc::dns::Name*>& names,
+ const isc::dns::NameComparisonResult::NameRelation connection,
+ const isc::dns::Message::Section section);
+
+ /// \brief Scrub All Sections of a Message
+ ///
+ /// Scrubs each of the answer, authority and additional sections of the
+ /// message.
+ ///
+ /// No distinction is made between RRsets legitimately in the message (e.g.
+ /// glue for authorities that are not in bailiwick) and ones that could be
+ /// considered as attempts of spoofing (e.g. non-bailiwick RRsets in the
+ /// additional section that are not related to the query).
+ ///
+ /// The resultant packet returned to the caller may be invalid. If so, it
+ /// is up to the caller to detect that.
+ ///
+ /// \param message Message to be scrubbed.
+ /// \param bailiwick Name of the zone whose authoritative servers were
+ /// queried.
+ ///
+ /// \return Count of the number of RRsets removed from the message.
+ static unsigned int scrubAllSections(isc::dns::Message& message,
+ const isc::dns::Name& bailiwick);
+
+ /// \brief Scrub Across Message Sections
+ ///
+ /// Does some cross-section comparisons and removes inconsistent RRs. In
+ /// particular it:
+ ///
+ /// - If an answer is present, checks that the qname of the authority RRs
+ /// are equal to or superdomain of the qname answer RRsets. Any that are
+ /// not are removed.
+ /// - If an answer is not present, checks that the authority RRs are
+ /// equal to or superdomains of the question. If not, the authority RRs
+ /// are removed.
+ ///
+ /// Note that the scrubbing does not check:
+ ///
+ /// - that the question is in the bailiwick of the server; that check is
+ /// assumed to have been done prior to the query being sent (else why
+ /// was the query sent there in the first place?)
+ /// - that the qname of one of the RRsets in the answer (if present) is
+ /// equal to the qname of the question (that check is done in the
+ /// response classification code).
+ ///
+ /// \param message Message to be scrubbed.
+ ///
+ /// \return Count of the number of RRsets removed from the section.
+ static unsigned int scrubCrossSections(isc::dns::Message& message);
+
+ /// \brief Main Scrubbing Entry Point
+ ///
+ /// The single entry point to the module to sanitise the message. All
+ /// it does is call the various other scrubbing methods.
+ ///
+ /// \param message Pointer to the message to be scrubbed. (This is a
+ /// pointer - as opposed to a Message as in other methods in this class -
+ /// as the external code is expected to be mainly using message pointers
+ /// to access messages.)
+ /// \param bailiwick Name of the zone whose authoritative servers were
+ /// queried.
+ ///
+ /// \return Count of the number of RRsets removed from the message.
+ static unsigned int scrub(const isc::dns::MessagePtr& message,
+ const isc::dns::Name& bailiwick);
+
+ /// \brief Comparison Function for Sorting Name Pointers
+ ///
+ /// Utility method called to sorts pointers to names in lexical order.
+ ///
+ /// \param n1 Pointer to first Name object
+ /// \param n2 Pointer to second Name object
+ ///
+ /// \return true if n1 is less than n2, false otherwise.
+ static bool compareNameLt(const isc::dns::Name* n1,
+ const isc::dns::Name* n2)
+ {
+ return (*n1 < *n2);
+ }
+
+ /// \brief Function for Comparing Name Pointers
+ ///
+ /// Utility method called to sorts pointers to names in lexical order.
+ ///
+ /// \param n1 Pointer to first Name object
+ /// \param n2 Pointer to second Name object
+ ///
+ /// \return true if n1 is equal to n2, false otherwise.
+ static bool compareNameEq(const isc::dns::Name* n1,
+ const isc::dns::Name* n2)
+ {
+ return (*n1 == *n2);
+ }
+};
+
+#endif // __RESPONSE_SCRUBBER_H
diff --git a/src/bin/resolver/tests/Makefile.am b/src/bin/resolver/tests/Makefile.am
index 8552439..a03439c 100644
--- a/src/bin/resolver/tests/Makefile.am
+++ b/src/bin/resolver/tests/Makefile.am
@@ -4,7 +4,6 @@ AM_CPPFLAGS += -I$(top_builddir)/src/lib/cc
AM_CPPFLAGS += -DTEST_DATA_DIR=\"$(top_srcdir)/src/lib/testutils/testdata\"
AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/lib/testutils/testdata\"
AM_CPPFLAGS += $(BOOST_INCLUDES)
-AM_CPPFLAGS += $(BOOST_INCLUDES)
AM_CXXFLAGS = $(B10_CXXFLAGS)
@@ -20,24 +19,33 @@ TESTS += run_unittests
run_unittests_SOURCES = $(top_srcdir)/src/lib/dns/tests/unittest_util.h
run_unittests_SOURCES += $(top_srcdir)/src/lib/dns/tests/unittest_util.cc
run_unittests_SOURCES += ../resolver.h ../resolver.cc
-run_unittests_SOURCES += ../response_classifier.h ../response_classifier.cc
+run_unittests_SOURCES += ../response_scrubber.h ../response_scrubber.cc
run_unittests_SOURCES += resolver_unittest.cc
run_unittests_SOURCES += resolver_config_unittest.cc
-run_unittests_SOURCES += response_classifier_unittest.cc
+run_unittests_SOURCES += response_scrubber_unittest.cc
run_unittests_SOURCES += run_unittests.cc
run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
run_unittests_LDADD = $(GTEST_LDADD)
run_unittests_LDADD += $(SQLITE_LIBS)
run_unittests_LDADD += $(top_builddir)/src/lib/testutils/libtestutils.la
-run_unittests_LDADD += $(top_builddir)/src/lib/datasrc/libdatasrc.la
-run_unittests_LDADD += $(top_builddir)/src/lib/dns/libdns++.la
+run_unittests_LDADD += $(top_builddir)/src/lib/datasrc/libdatasrc.la
+run_unittests_LDADD += $(top_builddir)/src/lib/dns/libdns++.la
run_unittests_LDADD += $(top_builddir)/src/lib/asiolink/libasiolink.la
run_unittests_LDADD += $(top_builddir)/src/lib/config/libcfgclient.la
run_unittests_LDADD += $(top_builddir)/src/lib/cc/libcc.la
run_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libexceptions.la
run_unittests_LDADD += $(top_builddir)/src/lib/xfr/libxfr.la
run_unittests_LDADD += $(top_builddir)/src/lib/log/liblog.la
+
+# Note the ordering matters: -Wno-... must follow -Wextra (defined in
+# B10_CXXFLAGS
+run_unittests_CXXFLAGS = $(AM_CXXFLAGS)
+if USE_GXX
+run_unittests_CXXFLAGS += -Wno-unused-parameter
+endif
endif
+
+
noinst_PROGRAMS = $(TESTS)
diff --git a/src/bin/resolver/tests/response_classifier_unittest.cc b/src/bin/resolver/tests/response_classifier_unittest.cc
deleted file mode 100644
index ee439b1..0000000
--- a/src/bin/resolver/tests/response_classifier_unittest.cc
+++ /dev/null
@@ -1,494 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <iostream>
-#include <gtest/gtest.h>
-
-#include <dns/tests/unittest_util.h>
-
-#include <resolver/response_classifier.h>
-
-#include <dns/name.h>
-#include <dns/opcode.h>
-#include <dns/question.h>
-#include <dns/rdata.h>
-#include <dns/rdataclass.h>
-#include <dns/rcode.h>
-#include <dns/rrclass.h>
-#include <dns/rrset.h>
-#include <dns/rrtype.h>
-#include <dns/rrttl.h>
-
-using namespace std;
-using namespace isc::dns;
-using namespace rdata;
-using namespace isc::dns::rdata::generic;
-using namespace isc::dns::rdata::in;
-
-namespace {
-class ResponseClassifierTest : public ::testing::Test {
-public:
- /// \brief Constructor
- ///
- /// The naming convention is:
- ///
- /// <category>_<class>_<type>_<name>
- ///
- /// <category> is "qu" (question), "rrs" (rrset),
- /// <qclass> is self-explanatory
- /// <qtype> is self-explanatory
- /// <name> is the first part of the domain name (all expected to be in
- /// example.com)
- ///
- /// Message variables
- ///
- /// msg_<qtype> Where <qtype> is the type of query. These are only used
- /// in the early tests where simple messages are required.
-
- ResponseClassifierTest() :
- msg_a(new Message(Message::RENDER)),
- msg_any(new Message(Message::RENDER)),
- qu_ch_a_www(Name("www.example.com"), RRClass::CH(), RRType::A()),
- qu_in_any_www(Name("www.example.com"), RRClass::IN(), RRType::ANY()),
- qu_in_a_www2(Name("www2.example.com"), RRClass::IN(), RRType::A()),
- qu_in_a_www(Name("www.example.com"), RRClass::IN(), RRType::A()),
- qu_in_cname_www1(Name("www1.example.com"), RRClass::IN(), RRType::A()),
- qu_in_ns_(Name("example.com"), RRClass::IN(), RRType::NS()),
- qu_in_txt_www(Name("www.example.com"), RRClass::IN(), RRType::TXT()),
- rrs_hs_txt_www(new RRset(Name("www.example.com"), RRClass::HS(),
- RRType::TXT(), RRTTL(300))),
- rrs_in_a_mail(new RRset(Name("mail.example.com"), RRClass::IN(),
- RRType::A(), RRTTL(300))),
- rrs_in_a_www(new RRset(Name("www.example.com"), RRClass::IN(),
- RRType::A(), RRTTL(300))),
- rrs_in_cname_www1(new RRset(Name("www1.example.com"), RRClass::IN(),
- RRType::CNAME(), RRTTL(300))),
- rrs_in_cname_www2(new RRset(Name("www2.example.com"), RRClass::IN(),
- RRType::CNAME(), RRTTL(300))),
- rrs_in_ns_(new RRset(Name("example.com"), RRClass::IN(),
- RRType::NS(), RRTTL(300))),
- rrs_in_txt_www(new RRset(Name("www.example.com"), RRClass::IN(),
- RRType::TXT(), RRTTL(300)))
- {
- // Set up the message to indicate a successful response to the question
- // "www.example.com A", but don't add in any response sections.
- msg_a->setHeaderFlag(Message::HEADERFLAG_QR);
- msg_a->setOpcode(Opcode::QUERY());
- msg_a->setRcode(Rcode::NOERROR());
- msg_a->addQuestion(qu_in_a_www);
-
- // ditto for the query "www.example.com ANY"
- msg_any->setHeaderFlag(Message::HEADERFLAG_QR);
- msg_any->setOpcode(Opcode::QUERY());
- msg_any->setRcode(Rcode::NOERROR());
- msg_any->addQuestion(qu_in_any_www);
-
- // The next set of assignments set up the following zone records
- //
- // example.com NS ns0.isc.org
- // NS ns0.example.org
- //
- // www.example.com A 1.2.3.4
- // TXT "An example text string"
- //
- // mail.example.com A 4.5.6.7
- //
- // www1.example.com CNAME www.example.com
- //
- // www2.example.com CNAME www1.example.com
-
- // Set up an imaginary NS RRset for an authority section
- rrs_in_ns_->addRdata(ConstRdataPtr(new NS(Name("ns0.isc.org"))));
- rrs_in_ns_->addRdata(ConstRdataPtr(new NS(Name("ns0.example.org"))));
-
- // Set up the records for the www host
- rrs_in_a_www->addRdata(ConstRdataPtr(new A("1.2.3.4")));
- rrs_in_txt_www->addRdata(ConstRdataPtr(
- new TXT("An example text string")));
-
- // ... for the mail host
- rrs_in_a_mail->addRdata(ConstRdataPtr(new A("5.6.7.8")));
-
- // ... the CNAME records
- rrs_in_cname_www1->addRdata(ConstRdataPtr(
- new CNAME("www.example.com")));
- rrs_in_cname_www2->addRdata(ConstRdataPtr(
- new CNAME("www1.example.com")));
- }
-
- MessagePtr msg_a; // Pointer to message in RENDER state
- MessagePtr msg_any; // Pointer to message in RENDER state
- Question qu_ch_a_www; // www.example.com CH A
- Question qu_in_any_www; // www.example.com IN ANY
- Question qu_in_a_www2; // www.example.com IN ANY
- Question qu_in_a_www; // www.example.com IN A
- Question qu_in_cname_www1; // www1.example.com IN CNAME
- Question qu_in_ns_; // example.com IN NS
- Question qu_in_txt_www; // www.example.com IN TXT
- RRsetPtr rrs_hs_txt_www; // www.example.com HS TXT
- RRsetPtr rrs_in_a_mail; // mail.example.com IN A
- RRsetPtr rrs_in_a_www; // www.example.com IN A
- RRsetPtr rrs_in_cname_www1; // www1.example.com IN CNAME
- RRsetPtr rrs_in_cname_www2; // www2.example.com IN CNAME
- RRsetPtr rrs_in_ns_; // example.com IN NS
- RRsetPtr rrs_in_txt_www; // www.example.com IN TXT
-};
-
-// Test that the error() function categorises the codes correctly.
-
-TEST_F(ResponseClassifierTest, StatusCodes) {
- EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::ANSWER));
- EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::ANSWERCNAME));
- EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::CNAME));
- EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::NXDOMAIN));
- EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::REFERRAL));
-
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::EMPTY));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::EXTRADATA));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::INVNAMCLASS));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::INVTYPE));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::MISMATQUEST));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::MULTICLASS));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::NOTONEQUEST));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::NOTRESPONSE));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::NOTSINGLE));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::OPCODE));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::RCODE));
- EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::TRUNCATED));
-}
-
-// Test that the system will reject a message which is a query.
-
-TEST_F(ResponseClassifierTest, Query) {
-
- // Set up message to indicate a query (QR flag = 0, one question). By
- // default the opcode will be 0 (query)
- msg_a->setHeaderFlag(Message::HEADERFLAG_QR, false);
-
- // Should be rejected as it is a query, not a response
- EXPECT_EQ(ResponseClassifier::NOTRESPONSE,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
-}
-
-// Check that we get an OPCODE error on all but QUERY opcodes.
-
-TEST_F(ResponseClassifierTest, Opcode) {
-
- uint8_t query = static_cast<uint8_t>(Opcode::QUERY().getCode());
-
- for (uint8_t i = 0; i < (1 << 4); ++i) {
- msg_a->setOpcode(Opcode(i));
- if (i == query) {
- EXPECT_NE(ResponseClassifier::OPCODE,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
- } else {
- EXPECT_EQ(ResponseClassifier::OPCODE,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
- }
- }
-}
-
-// Test that the system will reject a response with anything other than one
-// question.
-
-TEST_F(ResponseClassifierTest, MultipleQuestions) {
-
- // Create a message object for this test that has no question section.
- MessagePtr message(new Message(Message::RENDER));
- message->setHeaderFlag(Message::HEADERFLAG_QR);
- message->setOpcode(Opcode::QUERY());
- message->setRcode(Rcode::NOERROR());
-
- // Zero questions
- EXPECT_EQ(ResponseClassifier::NOTONEQUEST,
- ResponseClassifier::classify(qu_in_a_www, message));
-
- // One question
- message->addQuestion(qu_in_a_www);
- EXPECT_NE(ResponseClassifier::NOTONEQUEST,
- ResponseClassifier::classify(qu_in_a_www, message));
-
- // Two questions
- message->addQuestion(qu_in_ns_);
- EXPECT_EQ(ResponseClassifier::NOTONEQUEST,
- ResponseClassifier::classify(qu_in_a_www, message));
-
- // And finish the check with three questions
- message->addQuestion(qu_in_txt_www);
- EXPECT_EQ(ResponseClassifier::NOTONEQUEST,
- ResponseClassifier::classify(qu_in_a_www, message));
-}
-
-// Test that the question in the question section in the message response
-// is equal to the question supplied.
-
-TEST_F(ResponseClassifierTest, SameQuestion) {
-
- EXPECT_EQ(ResponseClassifier::MISMATQUEST,
- ResponseClassifier::classify(qu_in_ns_, msg_a));
- EXPECT_NE(ResponseClassifier::MISMATQUEST,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
-}
-
-// Should get an NXDOMAIN response only on an NXDOMAIN RCODE.
-
-TEST_F(ResponseClassifierTest, NXDOMAIN) {
-
- uint16_t nxdomain = static_cast<uint16_t>(Rcode::NXDOMAIN().getCode());
-
- for (uint8_t i = 0; i < (1 << 4); ++i) {
- msg_a->setRcode(Rcode(i));
- if (i == nxdomain) {
- EXPECT_EQ(ResponseClassifier::NXDOMAIN,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
- } else {
- EXPECT_NE(ResponseClassifier::NXDOMAIN,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
- }
- }
-}
-
-// Check that we get an RCODE error on all but NXDOMAIN and NOERROR responses.
-
-TEST_F(ResponseClassifierTest, RCODE) {
-
- uint16_t nxdomain = static_cast<uint16_t>(Rcode::NXDOMAIN().getCode());
- uint16_t noerror = static_cast<uint16_t>(Rcode::NOERROR().getCode());
-
- for (uint8_t i = 0; i < (1 << 4); ++i) {
- msg_a->setRcode(Rcode(i));
- if ((i == nxdomain) || (i == noerror)) {
- EXPECT_NE(ResponseClassifier::RCODE,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
- } else {
- EXPECT_EQ(ResponseClassifier::RCODE,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
- }
- }
-}
-
-// Test that the code will detect a truncated message. Even if nothing else
-// is wrong, we'll want to retry the query if we receive a truncated code.
-// However, we give the option to the user of the code aws to whether they
-// want to take into account the truncated bit.
-
-TEST_F(ResponseClassifierTest, Truncated) {
-
- // Don't expect the truncated code whatever option we ask for if the TC
- // bit is not set.
- msg_a->setHeaderFlag(Message::HEADERFLAG_TC, false);
- EXPECT_NE(ResponseClassifier::TRUNCATED,
- ResponseClassifier::classify(qu_in_a_www, msg_a, true));
- EXPECT_NE(ResponseClassifier::TRUNCATED,
- ResponseClassifier::classify(qu_in_a_www, msg_a, false));
-
- // Expect the truncated code if the TC bit is set, only if we don't ignore
- // it.
- msg_a->setHeaderFlag(Message::HEADERFLAG_TC, true);
- EXPECT_NE(ResponseClassifier::TRUNCATED,
- ResponseClassifier::classify(qu_in_a_www, msg_a, true));
- EXPECT_EQ(ResponseClassifier::TRUNCATED,
- ResponseClassifier::classify(qu_in_a_www, msg_a, false));
-}
-
-// Check for an empty packet (i.e. no error, but with the answer and additional
-// sections empty).
-
-TEST_F(ResponseClassifierTest, Empty) {
-
- EXPECT_EQ(ResponseClassifier::EMPTY,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
-}
-
-// Anything where we have an empty answer section but something in the
-// authority section is a referral (if the status is NOERROR).
-
-TEST_F(ResponseClassifierTest, EmptyAnswerReferral) {
-
- msg_a->addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_);
- EXPECT_EQ(ResponseClassifier::REFERRAL,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
-
-}
-
-// Check the case where we have a simple answer in the answer section. This
-// occurs when the QNAME/QTYPE/QCLASS matches one of the RRsets in the
-// answer section - expect when the QTYPE is ANY, in which case the match
-// must be on the QNAME/QCLASS alone.
-
-TEST_F(ResponseClassifierTest, SingleAnswer) {
-
- // Check a question that matches the answer
- msg_a->addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
- EXPECT_EQ(ResponseClassifier::ANSWER,
- ResponseClassifier::classify(qu_in_a_www, msg_a));
-
- // Check an ANY question that matches the answer
- msg_any->addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
- EXPECT_EQ(ResponseClassifier::ANSWER,
- ResponseClassifier::classify(qu_in_any_www, msg_any));
-
- // Check a CNAME response that matches the QNAME.
- MessagePtr message_a(new Message(Message::RENDER));
- message_a->setHeaderFlag(Message::HEADERFLAG_QR);
- message_a->setOpcode(Opcode::QUERY());
- message_a->setRcode(Rcode::NOERROR());
- message_a->addQuestion(qu_in_cname_www1);
- message_a->addRRset(Message::SECTION_ANSWER, rrs_in_cname_www1);
- EXPECT_EQ(ResponseClassifier::CNAME,
- ResponseClassifier::classify(qu_in_cname_www1, message_a));
-
- // Check if the answer QNAME does not match the question
- // Q: www.example.com IN A
- // A: mail.example.com IN A
- MessagePtr message_b(new Message(Message::RENDER));
- message_b->setHeaderFlag(Message::HEADERFLAG_QR);
- message_b->setOpcode(Opcode::QUERY());
- message_b->setRcode(Rcode::NOERROR());
- message_b->addQuestion(qu_in_a_www);
- message_b->addRRset(Message::SECTION_ANSWER, rrs_in_a_mail);
- EXPECT_EQ(ResponseClassifier::INVNAMCLASS,
- ResponseClassifier::classify(qu_in_a_www, message_b));
-
- // Check if the answer class does not match the question
- // Q: www.example.com CH A
- // A: www.example.com IN A
- MessagePtr message_c(new Message(Message::RENDER));
- message_c->setHeaderFlag(Message::HEADERFLAG_QR);
- message_c->setOpcode(Opcode::QUERY());
- message_c->setRcode(Rcode::NOERROR());
- message_c->addQuestion(qu_ch_a_www);
- message_c->addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
- EXPECT_EQ(ResponseClassifier::INVNAMCLASS,
- ResponseClassifier::classify(qu_ch_a_www, message_c));
-
- // Check if the answer type does not match the question
- // Q: www.example.com IN A
- // A: www.example.com IN TXT
- MessagePtr message_d(new Message(Message::RENDER));
- message_d->setHeaderFlag(Message::HEADERFLAG_QR);
- message_d->setOpcode(Opcode::QUERY());
- message_d->setRcode(Rcode::NOERROR());
- message_d->addQuestion(qu_in_a_www);
- message_d->addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
- EXPECT_EQ(ResponseClassifier::INVTYPE,
- ResponseClassifier::classify(qu_in_a_www, message_d));
-}
-
-// Check what happens if we have multiple RRsets in the answer.
-
-TEST_F(ResponseClassifierTest, MultipleAnswerRRsets) {
-
- // All the same QNAME but different types is only valid on an ANY query.
- MessagePtr message_a(new Message(Message::RENDER));
- message_a->setHeaderFlag(Message::HEADERFLAG_QR);
- message_a->setOpcode(Opcode::QUERY());
- message_a->setRcode(Rcode::NOERROR());
- message_a->addQuestion(qu_in_any_www);
- message_a->addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
- message_a->addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
- EXPECT_EQ(ResponseClassifier::ANSWER,
- ResponseClassifier::classify(qu_in_any_www, message_a));
-
- // On another type of query, it results in an EXTRADATA error
- MessagePtr message_b(new Message(Message::RENDER));
- message_b->setHeaderFlag(Message::HEADERFLAG_QR);
- message_b->setOpcode(Opcode::QUERY());
- message_b->setRcode(Rcode::NOERROR());
- message_b->addQuestion(qu_in_a_www);
- message_b->addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
- message_b->addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
- EXPECT_EQ(ResponseClassifier::EXTRADATA,
- ResponseClassifier::classify(qu_in_a_www, message_b));
-
- // Same QNAME on an ANY query is not valid with mixed classes
- MessagePtr message_c(new Message(Message::RENDER));
- message_c->setHeaderFlag(Message::HEADERFLAG_QR);
- message_c->setOpcode(Opcode::QUERY());
- message_c->setRcode(Rcode::NOERROR());
- message_c->addQuestion(qu_in_any_www);
- message_c->addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
- message_c->addRRset(Message::SECTION_ANSWER, rrs_hs_txt_www);
- EXPECT_EQ(ResponseClassifier::MULTICLASS,
- ResponseClassifier::classify(qu_in_any_www, message_c));
-
- // Mixed QNAME is not valid unless QNAME requested is a CNAME.
- MessagePtr message_d(new Message(Message::RENDER));
- message_d->setHeaderFlag(Message::HEADERFLAG_QR);
- message_d->setOpcode(Opcode::QUERY());
- message_d->setRcode(Rcode::NOERROR());
- message_d->addQuestion(qu_in_a_www);
- message_d->addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
- message_d->addRRset(Message::SECTION_ANSWER, rrs_in_a_mail);
- EXPECT_EQ(ResponseClassifier::EXTRADATA,
- ResponseClassifier::classify(qu_in_a_www, message_d));
-
- // Mixed QNAME is not valid when the query is an ANY.
- MessagePtr message_e(new Message(Message::RENDER));
- message_e->setHeaderFlag(Message::HEADERFLAG_QR);
- message_e->setOpcode(Opcode::QUERY());
- message_e->setRcode(Rcode::NOERROR());
- message_e->addQuestion(qu_in_any_www);
- message_e->addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
- message_e->addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
- message_e->addRRset(Message::SECTION_ANSWER, rrs_in_a_mail);
- EXPECT_EQ(ResponseClassifier::EXTRADATA,
- ResponseClassifier::classify(qu_in_any_www, message_e));
-}
-
-// CNAME chain is CNAME if it terminates in a CNAME, answer if it
-// does not, and error if there are RRs left over.
-TEST_F(ResponseClassifierTest, CNAMEChain) {
-
- // Answer contains a single CNAME
- MessagePtr message_a(new Message(Message::RENDER));
- message_a->setHeaderFlag(Message::HEADERFLAG_QR);
- message_a->setOpcode(Opcode::QUERY());
- message_a->setRcode(Rcode::NOERROR());
- message_a->addQuestion(qu_in_a_www2);
- message_a->addRRset(Message::SECTION_ANSWER, rrs_in_cname_www2);
- EXPECT_EQ(ResponseClassifier::CNAME,
- ResponseClassifier::classify(qu_in_a_www2, message_a));
-
- // Add a CNAME for www1, and it should still return a CNAME
- message_a->addRRset(Message::SECTION_ANSWER, rrs_in_cname_www1);
- EXPECT_EQ(ResponseClassifier::CNAME,
- ResponseClassifier::classify(qu_in_a_www2, message_a));
-
- // Add the A record for www and it should be an answer
- message_a->addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
- EXPECT_EQ(ResponseClassifier::ANSWERCNAME,
- ResponseClassifier::classify(qu_in_a_www2, message_a));
-
- // Adding an unrelated TXT record should result in EXTRADATA
- message_a->addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
- EXPECT_EQ(ResponseClassifier::EXTRADATA,
- ResponseClassifier::classify(qu_in_a_www2, message_a));
-
- // Recreate the chain, but this time end with a TXT RR and not the A
- // record. This should return INVTYPE.
- MessagePtr message_b(new Message(Message::RENDER));
- message_b->setHeaderFlag(Message::HEADERFLAG_QR);
- message_b->setOpcode(Opcode::QUERY());
- message_b->setRcode(Rcode::NOERROR());
- message_b->addQuestion(qu_in_a_www2);
- message_b->addRRset(Message::SECTION_ANSWER, rrs_in_cname_www2);
- message_b->addRRset(Message::SECTION_ANSWER, rrs_in_cname_www1);
- message_b->addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
-
- EXPECT_EQ(ResponseClassifier::INVTYPE,
- ResponseClassifier::classify(qu_in_a_www2, message_b));
-}
-
-} // Anonymous namespace
diff --git a/src/bin/resolver/tests/response_scrubber_unittest.cc b/src/bin/resolver/tests/response_scrubber_unittest.cc
new file mode 100644
index 0000000..1dc6639
--- /dev/null
+++ b/src/bin/resolver/tests/response_scrubber_unittest.cc
@@ -0,0 +1,542 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <string>
+#include <iostream>
+
+#include <gtest/gtest.h>
+
+#include <config.h>
+
+#include <asiolink/io_endpoint.h>
+#include <asiolink/io_address.h>
+#include <netinet/in.h>
+
+#include <dns/name.h>
+#include <dns/opcode.h>
+#include <dns/question.h>
+#include <dns/rdata.h>
+#include <dns/rdataclass.h>
+#include <dns/rcode.h>
+#include <dns/rrclass.h>
+#include <dns/rrset.h>
+#include <dns/rrtype.h>
+#include <dns/rrttl.h>
+#include <resolver/response_scrubber.h>
+
+
+// Class for endpoint checks. The family of the endpoint is set in the
+// constructor; the address family by the string provided for the address.
+
+namespace asiolink {
+
+class GenericEndpoint : public IOEndpoint {
+public:
+ GenericEndpoint(const std::string& address, uint16_t port, short protocol) :
+ address_(address), port_(port), protocol_(protocol)
+ {}
+ virtual ~GenericEndpoint()
+ {}
+
+ virtual IOAddress getAddress() const {
+ return address_;
+ }
+
+ virtual uint16_t getPort() const {
+ return port_;
+ }
+
+ virtual short getProtocol() const {
+ return protocol_;
+ }
+
+ virtual short getFamily() const {
+ return address_.getFamily();
+ }
+
+private:
+ IOAddress address_; // Address of endpoint
+ uint16_t port_; // Port number of endpoint
+ short protocol_; // Protocol of the endpoint
+ };
+}
+
+using namespace asio::ip;
+using namespace isc::dns;
+using namespace rdata;
+using namespace isc::dns::rdata::generic;
+using namespace isc::dns::rdata::in;
+using namespace asiolink;
+
+// Test class
+
+namespace {
+class ResponseScrubberTest : public ::testing::Test {
+public:
+ ResponseScrubberTest() :
+ bailiwick("example.com"),
+
+ qu_in_any_www(Name("www.example.com"), RRClass::IN(), RRType::ANY()),
+ qu_in_a_www(Name("www.example.com"), RRClass::IN(), RRType::A()),
+ qu_in_ns(Name("example.com"), RRClass::IN(), RRType::NS()),
+ qu_in_txt_www(Name("www.example.com"), RRClass::IN(), RRType::TXT()),
+ rrs_in_a_org(new RRset(Name("mail.example.org"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+
+ rrs_in_a_net(new RRset(Name("mail.example.net"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+ rrs_in_a_www(new RRset(Name("www.example.com"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+ rrs_in_cname_www(new RRset(Name("www.example.com"), RRClass::IN(),
+ RRType::CNAME(), RRTTL(300))),
+ rrs_in_a_wwwnet(new RRset(Name("www.example.net"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+ rrs_in_ns(new RRset(Name("example.com"), RRClass::IN(),
+ RRType::NS(), RRTTL(300))),
+ rrs_in_ns_com(new RRset(Name("com"), RRClass::IN(),
+ RRType::NS(), RRTTL(300))),
+ rrs_in_ns_net(new RRset(Name("example.net"), RRClass::IN(),
+ RRType::NS(), RRTTL(300))),
+ rrs_in_ns_sub(new RRset(Name("subdomain.example.com"), RRClass::IN(),
+ RRType::NS(), RRTTL(300))),
+ rrs_in_ns_sub2(new RRset(Name("subdomain2.example.com"), RRClass::IN(),
+ RRType::NS(), RRTTL(300))),
+ rrs_in_a_ns0(new RRset(Name("ns0.example.com"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+ rrs_in_a_ns1(new RRset(Name("ns1.com"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+ rrs_in_a_ns2(new RRset(Name("ns2.example.net"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+ rrs_in_a_ns3(new RRset(Name("ns3.subdomain.example.com"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+ rrs_in_txt_www(new RRset(Name("www.example.com"), RRClass::IN(),
+ RRType::TXT(), RRTTL(300)))
+ {}
+ Name bailiwick; // Bailiwick of the server queried
+ Question qu_in_any_www; // www.example.com IN ANY
+ Question qu_in_a_www; // www.example.com IN A
+ Question qu_in_ns; // example.com IN NS
+ Question qu_in_txt_www; // www.example.com IN TXT
+ RRsetPtr rrs_in_a_org; // mail.example.org IN A
+ RRsetPtr rrs_in_a_net; // mail.example.org IN A
+ RRsetPtr rrs_in_a_www; // www.example.com IN A
+ RRsetPtr rrs_in_cname_www; // www.example.com IN CNAME
+ RRsetPtr rrs_in_a_wwwnet; // www.example.net IN A
+ RRsetPtr rrs_in_ns; // example.com IN NS
+ RRsetPtr rrs_in_ns_com; // com IN NS
+ RRsetPtr rrs_in_ns_net; // example.net IN NS
+ RRsetPtr rrs_in_ns_sub; // subdomain.example.com IN NS
+ RRsetPtr rrs_in_ns_sub2; // subdomain2.example.com IN NS
+ RRsetPtr rrs_in_a_ns0; // ns0.example.com IN A
+ RRsetPtr rrs_in_a_ns1; // ns1.com IN A
+ RRsetPtr rrs_in_a_ns2; // ns2.example.net IN A
+ RRsetPtr rrs_in_a_ns3; // ns3.subdomain.example.net IN A
+ RRsetPtr rrs_in_txt_www; // www.example.com IN TXT
+};
+
+
+// Check that the IP addresses/ports/protocol for the packets sent and received
+// both match if both types are IP V4.
+
+TEST_F(ResponseScrubberTest, UDPv4) {
+
+ // Basic UDP Endpoint
+ GenericEndpoint udp_a("192.0.2.1", 12345, IPPROTO_UDP);
+
+ // Same address, port
+ GenericEndpoint udp_b("192.0.2.1", 12345, IPPROTO_UDP);
+ EXPECT_EQ(ResponseScrubber::SUCCESS,
+ ResponseScrubber::addressCheck(udp_a, udp_b));
+
+ // Different address, same port
+ GenericEndpoint udp_c("192.0.2.2", 12345, IPPROTO_UDP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(udp_a, udp_c));
+
+ // Same address, different port
+ GenericEndpoint udp_d("192.0.2.1", 12346, IPPROTO_UDP);
+ EXPECT_EQ(ResponseScrubber::PORT,
+ ResponseScrubber::addressCheck(udp_a, udp_d));
+
+ // Different address, different port
+ GenericEndpoint udp_e("192.0.2.3", 12347, IPPROTO_UDP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(udp_a, udp_e));
+
+}
+
+// Repeat the tests for TCP
+
+TEST_F(ResponseScrubberTest, TCPv4) {
+
+ // Basic TCP Endpoint
+ GenericEndpoint tcp_a("192.0.2.1", 12345, IPPROTO_TCP);
+
+ // Same address, port
+ GenericEndpoint tcp_b("192.0.2.1", 12345, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::SUCCESS,
+ ResponseScrubber::addressCheck(tcp_a, tcp_b));
+
+ // Different address, same port
+ GenericEndpoint tcp_c("192.0.2.2", 12345, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(tcp_a, tcp_c));
+
+ // Same address, different port
+ GenericEndpoint tcp_d("192.0.2.1", 12346, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::PORT,
+ ResponseScrubber::addressCheck(tcp_a, tcp_d));
+
+ // Different address, different port
+ GenericEndpoint tcp_e("192.0.2.3", 12347, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(tcp_a, tcp_e));
+
+}
+
+// Repeat the tests for UDP/IPv6
+
+TEST_F(ResponseScrubberTest, UDPv6) {
+
+ // Basic UDP Endpoint
+ GenericEndpoint udp_a("2001:db8::1", 12345, IPPROTO_UDP);
+
+ // Same address and port
+ GenericEndpoint udp_b("2001:db8::1", 12345, IPPROTO_UDP);
+ EXPECT_EQ(ResponseScrubber::SUCCESS,
+ ResponseScrubber::addressCheck(udp_a, udp_b));
+
+ // Different address, same port
+ GenericEndpoint udp_c("2001:db8::3", 12345, IPPROTO_UDP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(udp_a, udp_c));
+
+ // Same address, different port
+ GenericEndpoint udp_d("2001:db8::1", 12346, IPPROTO_UDP);
+ EXPECT_EQ(ResponseScrubber::PORT,
+ ResponseScrubber::addressCheck(udp_a, udp_d));
+
+ // Different address, different port
+ GenericEndpoint udp_e("2001:db8::3", 12347, IPPROTO_UDP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(udp_a, udp_e));
+
+}
+
+// Same again for TCP/IPv6
+
+TEST_F(ResponseScrubberTest, TCPv6) {
+
+ // Basic TCP Endpoint
+ GenericEndpoint tcp_a("2001:db8::1", 12345, IPPROTO_TCP);
+
+ // Same address and port
+ GenericEndpoint tcp_b("2001:db8::1", 12345, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::SUCCESS,
+ ResponseScrubber::addressCheck(tcp_a, tcp_b));
+
+ // Different address, same port
+ GenericEndpoint tcp_c("2001:db8::3", 12345, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(tcp_a, tcp_c));
+
+ // Same address, different port
+ GenericEndpoint tcp_d("2001:db8::1", 12346, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::PORT,
+ ResponseScrubber::addressCheck(tcp_a, tcp_d));
+
+ // Different address, different port
+ GenericEndpoint tcp_e("2001:db8::3", 12347, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(tcp_a, tcp_e));
+
+}
+
+// Ensure that mixed IPv4/6 addresses don't match.
+
+TEST_F(ResponseScrubberTest, v4v6) {
+
+ // UDP
+ GenericEndpoint udp_a("2001:db8::1", 12345, IPPROTO_UDP);
+ GenericEndpoint udp_b("192.0.2.1", 12345, IPPROTO_UDP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(udp_a, udp_b));
+
+ // TCP
+ GenericEndpoint tcp_a("2001:db8::1", 12345, IPPROTO_TCP);
+ GenericEndpoint tcp_b("192.0.2.1", 12345, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::ADDRESS,
+ ResponseScrubber::addressCheck(udp_a, udp_b));
+}
+
+// Check mixed protocols are detected
+
+TEST_F(ResponseScrubberTest, Protocol) {
+ GenericEndpoint udp_a("2001:db8::1", 12345, IPPROTO_UDP);
+ GenericEndpoint tcp_a("2001:db8::1", 12345, IPPROTO_TCP);
+ EXPECT_EQ(ResponseScrubber::PROTOCOL,
+ ResponseScrubber::addressCheck(udp_a, tcp_a));
+}
+
+// Check that the QIDs check OK
+
+TEST_F(ResponseScrubberTest, Qid) {
+ Message a(Message::RENDER);
+ a.setQid(27);
+
+ Message b(Message::RENDER);
+ b.setQid(27);
+ EXPECT_TRUE(ResponseScrubber::qidCheck(a, b));
+
+ Message c(Message::RENDER);
+ c.setQid(28);
+ EXPECT_FALSE(ResponseScrubber::qidCheck(a, c));
+}
+
+// Check the scrubAllSections() method. As this operates by calling the
+// scrubSection() method (with a SUBDOMAIN argument), this is also a check of
+// the latter.
+
+TEST_F(ResponseScrubberTest, ScrubAllSectionsValid) {
+ Message valid(Message::RENDER);
+
+ // Valid message with nothing out of bailiwick
+ valid.addQuestion(qu_in_a_www);
+ valid.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ valid.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns);
+ valid.addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns0);
+
+ // Scrub the message and expect nothing to have been removed.
+ int removed = ResponseScrubber::scrubAllSections(valid, bailiwick);
+ EXPECT_EQ(0, removed);
+
+ // ... and check that this is the case
+ EXPECT_TRUE(valid.hasRRset(Message::SECTION_ANSWER, rrs_in_a_www));
+ EXPECT_TRUE(valid.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns));
+ EXPECT_TRUE(valid.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns0));
+
+ // Add out-of-bailiwick glue to the additional section (pretend that the
+ // NS RRset contained an out-of-domain server.
+ valid.addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns2);
+ EXPECT_TRUE(valid.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns2));
+
+ // ... and check that it is removed when scrubbed
+ removed = ResponseScrubber::scrubAllSections(valid, bailiwick);
+ EXPECT_EQ(1, removed);
+ EXPECT_TRUE(valid.hasRRset(Message::SECTION_ANSWER, rrs_in_a_www));
+ EXPECT_TRUE(valid.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns));
+ EXPECT_TRUE(valid.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns0));
+ EXPECT_FALSE(valid.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns2));
+ }
+
+TEST_F(ResponseScrubberTest, ScrubAllSectionsInvalid) {
+ Message invalid(Message::RENDER);
+
+ // Invalid message, with various things in and out of bailiwick.
+
+ invalid.addQuestion(qu_in_a_www);
+
+ // Answer section
+ //
+ // rrs_in_a_www - "www.example.com A", in bailiwick
+ // rrs_in_txt_www - "www.example.com TXT", in bailiwick
+ // rrs_in_a_org - "mail.example.org A", out of bailiwick - the qname is
+ // related to the bailiwick name by having a common ancestor at the root
+ // rrs_in_a_net - "mail.example.net A", out of bailiwick - the qname is
+ // related to the bailiwick name by having a common ancestor at the root
+ invalid.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ invalid.addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
+ invalid.addRRset(Message::SECTION_ANSWER, rrs_in_a_org);
+ invalid.addRRset(Message::SECTION_ANSWER, rrs_in_a_net);
+
+ // Authority section
+ //
+ // rrs_in_ns - "example.com NS", in bailiwick (qname is bailiwick name)
+ // rrs_in_ns_com - "com NS", out of bailiwick as the qname is a superdomain
+ // (direct ancestor) of the bailiwick name
+ // rrs_in_ns_net - "example.net NS", out of bailiwick - the qname is related
+ // to the bailiwick name by having a common ancestor at the root
+ // rrs_in_ns_sub - "subdomain.example.com", in bailiwick as the qname is
+ // a subdomain of the bailiwick name
+ invalid.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns);
+ invalid.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_com);
+ invalid.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_net);
+ invalid.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub);
+
+ // Additional section
+ //
+ // rrs_in_a_ns0 - "ns0.example.com", in bailiwick because the qname is
+ // a subdomain of the bailiwick name
+ // rrs_in_a_ns1 - "ns1.com", out of bailiwick because the qname is a
+ // sibling to the bailiwick name
+ // rrs_in_a_ns2 - "ns2.example.net", out of bailiwick because qname is
+ // related by having a common ancestor and the root.
+ // rrs_in_a_ns3 - "ns3.subdomain.example.com", in bailiwick because the
+ // qname is a direct descendent of the bailiwick name.
+ invalid.addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns0);
+ invalid.addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns1);
+ invalid.addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns2);
+ invalid.addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns3);
+
+ // Scrub the message
+ int removed = ResponseScrubber::scrubAllSections(invalid, bailiwick);
+ EXPECT_EQ(6, removed);
+
+ // ... and check the sections. Answer...
+ EXPECT_TRUE(invalid.hasRRset(Message::SECTION_ANSWER, rrs_in_a_www));
+ EXPECT_TRUE(invalid.hasRRset(Message::SECTION_ANSWER, rrs_in_txt_www));
+ EXPECT_FALSE(invalid.hasRRset(Message::SECTION_ANSWER, rrs_in_a_org));
+ EXPECT_FALSE(invalid.hasRRset(Message::SECTION_ANSWER, rrs_in_a_net));
+
+ // ... authority...
+ EXPECT_TRUE(invalid.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns));
+ EXPECT_FALSE(invalid.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns_com));
+ EXPECT_FALSE(invalid.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns_net));
+ EXPECT_TRUE(invalid.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub));
+
+ // ... additional.
+ EXPECT_TRUE(invalid.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns0));
+ EXPECT_FALSE(invalid.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns1));
+ EXPECT_FALSE(invalid.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns2));
+ EXPECT_TRUE(invalid.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns3));
+}
+
+// An empty message
+
+TEST_F(ResponseScrubberTest, ScrubAllSectionsEmpty) {
+ Message empty(Message::RENDER);
+
+ EXPECT_EQ(0, empty.getRRCount(Message::SECTION_QUESTION));
+ EXPECT_EQ(0, empty.getRRCount(Message::SECTION_ANSWER));
+ EXPECT_EQ(0, empty.getRRCount(Message::SECTION_AUTHORITY));
+ EXPECT_EQ(0, empty.getRRCount(Message::SECTION_ADDITIONAL));
+
+ int removed = ResponseScrubber::scrubAllSections(empty, bailiwick);
+ EXPECT_EQ(0, removed);
+
+ EXPECT_EQ(0, empty.getRRCount(Message::SECTION_QUESTION));
+ EXPECT_EQ(0, empty.getRRCount(Message::SECTION_ANSWER));
+ EXPECT_EQ(0, empty.getRRCount(Message::SECTION_AUTHORITY));
+ EXPECT_EQ(0, empty.getRRCount(Message::SECTION_ADDITIONAL));
+
+}
+
+// Check the cross-section scrubbing (checks the general scrubSection()
+// method with a SUPERDOMAIN argument.)
+
+// Empty message (apart from question)
+
+TEST_F(ResponseScrubberTest, CrossSectionEmpty) {
+
+ Message message1(Message::RENDER);
+ message1.addQuestion(qu_in_a_www);
+ int removed = ResponseScrubber::scrubCrossSections(message1);
+ EXPECT_EQ(0, removed);
+}
+
+// Valid answer section
+
+TEST_F(ResponseScrubberTest, CrossSectionAnswer) {
+
+ // Valid message with nothing out of bailiwick, but the authority
+ // (subdomain.example.com) is not authoritative for the answer.
+ //
+ // TODO: Test the case where the additional section does not match
+ // with something in the authority section.
+ Message message1(Message::RENDER);
+ message1.addQuestion(qu_in_a_www);
+ message1.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ message1.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub);
+ message1.addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns3);
+ int removed = ResponseScrubber::scrubCrossSections(message1);
+ EXPECT_EQ(1, removed);
+ EXPECT_TRUE(message1.hasRRset(Message::SECTION_ANSWER, rrs_in_a_www));
+ EXPECT_FALSE(message1.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub));
+ EXPECT_TRUE(message1.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns3));
+
+ // A repeat of the test, this time with a mixture of incorrect and correct
+ // authorities.
+ Message message2(Message::RENDER);
+ message2.addQuestion(qu_in_a_www);
+ message2.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ message2.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub);
+ message2.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns);
+ message2.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub2);
+ message2.addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns3);
+ removed = ResponseScrubber::scrubCrossSections(message2);
+ EXPECT_EQ(2, removed);
+ EXPECT_TRUE(message2.hasRRset(Message::SECTION_ANSWER, rrs_in_a_www));
+ EXPECT_FALSE(message2.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub));
+ EXPECT_TRUE(message2.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns));
+ EXPECT_FALSE(message2.hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub2));
+ EXPECT_TRUE(message2.hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns3));
+}
+
+// Test the main "scrub" method. This is a single to ensure that the
+// combination of methods
+
+TEST_F(ResponseScrubberTest, All) {
+ MessagePtr mptr(new Message(Message::RENDER));
+
+ // Question is "www.example.com IN A" sent to a nameserver with the
+ // bailiwick of "example.com".
+ mptr->addQuestion(qu_in_a_www);
+
+ // Answer section.
+
+ // "www.example.com IN CNAME www.example.net" - should be kept
+ mptr->addRRset(Message::SECTION_ANSWER, rrs_in_cname_www);
+
+ // "www.example.net IN A a.b.c.d" - should be removed, out of bailiwick
+ mptr->addRRset(Message::SECTION_ANSWER, rrs_in_a_wwwnet);
+
+ // Authority section.
+
+ // "example.net IN NS xxxx" - should be removed, out of bailiwick.
+ mptr->addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_net);
+
+ // "example.com IN NS xxx" - kept
+ mptr->addRRset(Message::SECTION_AUTHORITY, rrs_in_ns);
+
+ // "com IN NS xxx" - removed, out of bailiwick
+ mptr->addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_com);
+
+ // "subdomain.example.com IN NS xxx" - removed, not a superdomain of the
+ // answer.
+ mptr->addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub);
+
+ // Additional section
+
+ // "ns2.example.net IN A a.b.c.d" - removed, out of bailiwick
+ mptr->addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns2);
+
+ // "ns3.subdomain.example.com IN A a.b.c.d" - retained.
+ mptr->addRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns3);
+
+ unsigned int removed = ResponseScrubber::scrub(mptr, bailiwick);
+ EXPECT_EQ(5, removed);
+
+ EXPECT_TRUE(mptr->hasRRset(Message::SECTION_ANSWER, rrs_in_cname_www));
+ EXPECT_FALSE(mptr->hasRRset(Message::SECTION_ANSWER, rrs_in_a_wwwnet));
+ EXPECT_FALSE(mptr->hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns_net));
+ EXPECT_TRUE(mptr->hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns));
+ EXPECT_FALSE(mptr->hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns_com));
+ EXPECT_FALSE(mptr->hasRRset(Message::SECTION_AUTHORITY, rrs_in_ns_sub));
+ EXPECT_FALSE(mptr->hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns2));
+ EXPECT_TRUE(mptr->hasRRset(Message::SECTION_ADDITIONAL, rrs_in_a_ns3));
+
+}
+} // Anonymous namespace
diff --git a/src/bin/stats/b10-stats.xml b/src/bin/stats/b10-stats.xml
index 62051df..f622439 100644
--- a/src/bin/stats/b10-stats.xml
+++ b/src/bin/stats/b10-stats.xml
@@ -89,7 +89,8 @@
<para><filename>/usr/local/share/bind10-devel/stats.spec</filename>
— This is a spec file for <command>b10-stats</command>. It
contains definitions of statistics items of BIND 10 and commands
- received vi bindctl.
+ received via
+ <refentrytitle>bindctl</refentrytitle><manvolnum>1</manvolnum>.
</para>
</refsect1>
diff --git a/src/bin/usermgr/b10-cmdctl-usermgr.py.in b/src/bin/usermgr/b10-cmdctl-usermgr.py.in
index 645c053..d62ad72 100644
--- a/src/bin/usermgr/b10-cmdctl-usermgr.py.in
+++ b/src/bin/usermgr/b10-cmdctl-usermgr.py.in
@@ -24,7 +24,7 @@ from hashlib import sha1
import csv
import getpass
import getopt
-import sys
+import sys; sys.path.append ('@@PYTHONPATH@@')
import isc.util.process
isc.util.process.rename()
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 866ca65..8ab93ba 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -1,2 +1,2 @@
-SUBDIRS = exceptions dns cc config datasrc python xfr bench log asiolink \
- testutils nsas
+SUBDIRS = exceptions dns cc config datasrc python xfr bench log \
+ resolve asiolink testutils nsas cache
diff --git a/src/lib/asiolink/Makefile.am b/src/lib/asiolink/Makefile.am
index 26d9a68..868fde5 100644
--- a/src/lib/asiolink/Makefile.am
+++ b/src/lib/asiolink/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = . tests internal
+SUBDIRS = . tests
AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
AM_CPPFLAGS += $(BOOST_INCLUDES)
@@ -12,14 +12,24 @@ CLEANFILES = *.gcno *.gcda
# have some code fragments that would hit gcc's unused-parameter warning,
# which would make the build fail with -Werror (our default setting).
lib_LTLIBRARIES = libasiolink.la
-libasiolink_la_SOURCES = asiolink.cc asiolink.h
-libasiolink_la_SOURCES += iosocket.cc iosocket.h
-libasiolink_la_SOURCES += iomessage.h
-libasiolink_la_SOURCES += ioaddress.cc ioaddress.h
-libasiolink_la_SOURCES += ioendpoint.cc ioendpoint.h
-libasiolink_la_SOURCES += udpdns.cc internal/udpdns.h
-libasiolink_la_SOURCES += tcpdns.cc internal/tcpdns.h
-libasiolink_la_SOURCES += internal/coroutine.h
+libasiolink_la_SOURCES = asiolink.h
+libasiolink_la_SOURCES += io_service.cc io_service.h
+libasiolink_la_SOURCES += dns_service.cc dns_service.h
+libasiolink_la_SOURCES += dns_server.h
+libasiolink_la_SOURCES += dns_lookup.h
+libasiolink_la_SOURCES += dns_answer.h
+libasiolink_la_SOURCES += simple_callback.h
+libasiolink_la_SOURCES += interval_timer.h interval_timer.cc
+libasiolink_la_SOURCES += recursive_query.h recursive_query.cc
+libasiolink_la_SOURCES += io_socket.cc io_socket.h
+libasiolink_la_SOURCES += io_message.h
+libasiolink_la_SOURCES += io_address.cc io_address.h
+libasiolink_la_SOURCES += io_endpoint.cc io_endpoint.h
+libasiolink_la_SOURCES += udp_endpoint.h udp_socket.h
+libasiolink_la_SOURCES += udp_server.h udp_server.cc
+libasiolink_la_SOURCES += udp_query.h udp_query.cc
+libasiolink_la_SOURCES += tcp_endpoint.h tcp_socket.h
+libasiolink_la_SOURCES += tcp_server.h tcp_server.cc
# Note: the ordering matters: -Wno-... must follow -Wextra (defined in
# B10_CXXFLAGS)
libasiolink_la_CXXFLAGS = $(AM_CXXFLAGS)
@@ -32,3 +42,4 @@ libasiolink_la_CXXFLAGS += -Wno-error
endif
libasiolink_la_CPPFLAGS = $(AM_CPPFLAGS)
libasiolink_la_LIBADD = $(top_builddir)/src/lib/log/liblog.la
+libasiolink_la_LIBADD += $(top_builddir)/src/lib/resolve/libresolve.la
diff --git a/src/lib/asiolink/asiolink.cc b/src/lib/asiolink/asiolink.cc
deleted file mode 100644
index 10574a9..0000000
--- a/src/lib/asiolink/asiolink.cc
+++ /dev/null
@@ -1,725 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <config.h>
-
-#include <cstdlib> // For rand(), temporary until better forwarding is done
-
-#include <unistd.h> // for some IPC/network system calls
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-#include <vector>
-#include <asio.hpp>
-#include <boost/lexical_cast.hpp>
-#include <boost/bind.hpp>
-#include <boost/date_time/posix_time/posix_time_types.hpp>
-
-#include <boost/shared_ptr.hpp>
-
-#include <dns/buffer.h>
-#include <dns/message.h>
-#include <dns/rcode.h>
-
-#include <asiolink/asiolink.h>
-#include <asiolink/internal/tcpdns.h>
-#include <asiolink/internal/udpdns.h>
-
-#include <log/dummylog.h>
-
-
-using namespace asio;
-using asio::ip::udp;
-using asio::ip::tcp;
-
-using namespace std;
-using namespace isc::dns;
-using isc::log::dlog;
-using namespace boost;
-
-// Is this something we can use in libdns++?
-namespace {
- class SectionInserter {
- public:
- SectionInserter(MessagePtr message, const Message::Section sect) :
- message_(message), section_(sect)
- {}
- void operator()(const RRsetPtr rrset) {
- message_->addRRset(section_, rrset, true);
- }
- MessagePtr message_;
- const Message::Section section_;
- };
-
-
- /// \brief Copies the parts relevant for a DNS answer to the
- /// target message
- ///
- /// This adds all the RRsets in the answer, authority and
- /// additional sections to the target, as well as the response
- /// code
- void copyAnswerMessage(const Message& source, MessagePtr target) {
- target->setRcode(source.getRcode());
-
- for_each(source.beginSection(Message::SECTION_ANSWER),
- source.endSection(Message::SECTION_ANSWER),
- SectionInserter(target, Message::SECTION_ANSWER));
- for_each(source.beginSection(Message::SECTION_AUTHORITY),
- source.endSection(Message::SECTION_AUTHORITY),
- SectionInserter(target, Message::SECTION_AUTHORITY));
- for_each(source.beginSection(Message::SECTION_ADDITIONAL),
- source.endSection(Message::SECTION_ADDITIONAL),
- SectionInserter(target, Message::SECTION_ADDITIONAL));
- }
-}
-
-namespace asiolink {
-
-typedef pair<string, uint16_t> addr_t;
-
-class IOServiceImpl {
-private:
- IOServiceImpl(const IOService& source);
- IOServiceImpl& operator=(const IOService& source);
-public:
- /// \brief The constructor
- IOServiceImpl() :
- io_service_(),
- work_(io_service_)
- {};
- /// \brief The destructor.
- ~IOServiceImpl() {};
- //@}
-
- /// \brief Start the underlying event loop.
- ///
- /// This method does not return control to the caller until
- /// the \c stop() method is called via some handler.
- void run() { io_service_.run(); };
-
- /// \brief Run the underlying event loop for a single event.
- ///
- /// This method return control to the caller as soon as the
- /// first handler has completed. (If no handlers are ready when
- /// it is run, it will block until one is.)
- void run_one() { io_service_.run_one();} ;
-
- /// \brief Stop the underlying event loop.
- ///
- /// This will return the control to the caller of the \c run() method.
- void stop() { io_service_.stop();} ;
-
- /// \brief Return the native \c io_service object used in this wrapper.
- ///
- /// This is a short term work around to support other BIND 10 modules
- /// that share the same \c io_service with the authoritative server.
- /// It will eventually be removed once the wrapper interface is
- /// generalized.
- asio::io_service& get_io_service() { return io_service_; };
-private:
- asio::io_service io_service_;
- asio::io_service::work work_;
-};
-
-IOService::IOService() {
- io_impl_ = new IOServiceImpl();
-}
-
-IOService::~IOService() {
- delete io_impl_;
-}
-
-void
-IOService::run() {
- io_impl_->run();
-}
-
-void
-IOService::run_one() {
- io_impl_->run_one();
-}
-
-void
-IOService::stop() {
- io_impl_->stop();
-}
-
-asio::io_service&
-IOService::get_io_service() {
- return (io_impl_->get_io_service());
-}
-
-class DNSServiceImpl {
-public:
- DNSServiceImpl(IOService& io_service, const char& port,
- const ip::address* v4addr, const ip::address* v6addr,
- SimpleCallback* checkin, DNSLookup* lookup,
- DNSAnswer* answer);
-
- IOService& io_service_;
-
- typedef boost::shared_ptr<UDPServer> UDPServerPtr;
- typedef boost::shared_ptr<TCPServer> TCPServerPtr;
- typedef boost::shared_ptr<DNSServer> DNSServerPtr;
- vector<DNSServerPtr> servers_;
- SimpleCallback *checkin_;
- DNSLookup *lookup_;
- DNSAnswer *answer_;
-
- void addServer(uint16_t port, const ip::address& address) {
- try {
- dlog(std::string("Initialize TCP server at ") + address.to_string() + ":" + boost::lexical_cast<string>(port));
- TCPServerPtr tcpServer(new TCPServer(io_service_.get_io_service(),
- address, port, checkin_, lookup_, answer_));
- (*tcpServer)();
- servers_.push_back(tcpServer);
- dlog(std::string("Initialize UDP server at ") + address.to_string() + ":" + boost::lexical_cast<string>(port));
- UDPServerPtr udpServer(new UDPServer(io_service_.get_io_service(),
- address, port, checkin_, lookup_, answer_));
- (*udpServer)();
- servers_.push_back(udpServer);
- }
- catch (const asio::system_error& err) {
- // We need to catch and convert any ASIO level exceptions.
- // This can happen for unavailable address, binding a privilege port
- // without the privilege, etc.
- isc_throw(IOError, "Failed to initialize network servers: " <<
- err.what());
- }
- }
- void addServer(const char& port, const ip::address& address) {
- uint16_t portnum;
- try {
- // XXX: SunStudio with stlport4 doesn't reject some invalid
- // representation such as "-1" by lexical_cast<uint16_t>, so
- // we convert it into a signed integer of a larger size and perform
- // range check ourselves.
- const int32_t portnum32 = boost::lexical_cast<int32_t>(&port);
- if (portnum32 < 0 || portnum32 > 65535) {
- isc_throw(IOError, "Invalid port number '" << &port);
- }
- portnum = portnum32;
- } catch (const boost::bad_lexical_cast& ex) {
- isc_throw(IOError, "Invalid port number '" << &port << "': " <<
- ex.what());
- }
- addServer(portnum, address);
- }
-};
-
-DNSServiceImpl::DNSServiceImpl(IOService& io_service,
- const char& port,
- const ip::address* const v4addr,
- const ip::address* const v6addr,
- SimpleCallback* checkin,
- DNSLookup* lookup,
- DNSAnswer* answer) :
- io_service_(io_service),
- checkin_(checkin),
- lookup_(lookup),
- answer_(answer)
-{
-
- if (v4addr) {
- addServer(port, *v4addr);
- }
- if (v6addr) {
- addServer(port, *v6addr);
- }
-}
-
-DNSService::DNSService(IOService& io_service,
- const char& port, const char& address,
- SimpleCallback* checkin,
- DNSLookup* lookup,
- DNSAnswer* answer) :
- impl_(new DNSServiceImpl(io_service, port, NULL, NULL, checkin, lookup,
- answer)), io_service_(io_service)
-{
- addServer(port, &address);
-}
-
-DNSService::DNSService(IOService& io_service,
- const char& port,
- const bool use_ipv4, const bool use_ipv6,
- SimpleCallback* checkin,
- DNSLookup* lookup,
- DNSAnswer* answer) :
- impl_(NULL), io_service_(io_service)
-{
- const ip::address v4addr_any = ip::address(ip::address_v4::any());
- const ip::address* const v4addrp = use_ipv4 ? &v4addr_any : NULL;
- const ip::address v6addr_any = ip::address(ip::address_v6::any());
- const ip::address* const v6addrp = use_ipv6 ? &v6addr_any : NULL;
- impl_ = new DNSServiceImpl(io_service, port, v4addrp, v6addrp, checkin, lookup, answer);
-}
-
-DNSService::DNSService(IOService& io_service, SimpleCallback* checkin,
- DNSLookup* lookup, DNSAnswer *answer) :
- impl_(new DNSServiceImpl(io_service, *"0", NULL, NULL, checkin, lookup,
- answer)), io_service_(io_service)
-{
-}
-
-DNSService::~DNSService() {
- delete impl_;
-}
-
-namespace {
-
-typedef std::vector<std::pair<std::string, uint16_t> > AddressVector;
-
-}
-
-RecursiveQuery::RecursiveQuery(DNSService& dns_service,
- const AddressVector& upstream,
- const AddressVector& upstream_root,
- int query_timeout, int client_timeout, int lookup_timeout,
- unsigned retries) :
- dns_service_(dns_service), upstream_(new AddressVector(upstream)),
- upstream_root_(new AddressVector(upstream_root)),
- query_timeout_(query_timeout), client_timeout_(client_timeout),
- lookup_timeout_(lookup_timeout), retries_(retries)
-{}
-
-namespace {
-
-ip::address
-convertAddr(const string& address) {
- error_code err;
- ip::address addr = ip::address::from_string(address, err);
- if (err) {
- isc_throw(IOError, "Invalid IP address '" << &address << "': "
- << err.message());
- }
- return (addr);
-}
-
-}
-
-void
-DNSService::addServer(const char& port, const string& address) {
- impl_->addServer(port, convertAddr(address));
-}
-
-void
-DNSService::addServer(uint16_t port, const string& address) {
- impl_->addServer(port, convertAddr(address));
-}
-
-void
-DNSService::clearServers() {
- // FIXME: This does not work, it does not close the socket.
- // How is it done?
- impl_->servers_.clear();
-}
-
-namespace {
-
-/*
- * This is a query in progress. When a new query is made, this one holds
- * the context information about it, like how many times we are allowed
- * to retry on failure, what to do when we succeed, etc.
- *
- * Used by RecursiveQuery::sendQuery.
- */
-class RunningQuery : public UDPQuery::Callback {
-private:
- // The io service to handle async calls
- asio::io_service& io_;
-
- // Info for (re)sending the query (the question and destination)
- Question question_;
-
- // This is where we build and store our final answer
- MessagePtr answer_message_;
-
- // currently we use upstream as the current list of NS records
- // we should differentiate between forwarding and resolving
- shared_ptr<AddressVector> upstream_;
-
- // root servers...just copied over to the zone_servers_
- shared_ptr<AddressVector> upstream_root_;
-
- // Buffer to store the result.
- OutputBufferPtr buffer_;
-
- // Server to notify when we succeed or fail
- shared_ptr<DNSServer> server_;
-
- /*
- * TODO Do something more clever with timeouts. In the long term, some
- * computation of average RTT, increase with each retry, etc.
- */
- // Timeout information
- int query_timeout_;
- unsigned retries_;
-
- // normal query state
-
- // Not using NSAS at this moment, so we keep a list
- // of 'current' zone servers
- vector<addr_t> zone_servers_;
-
- // Update the question that will be sent to the server
- void setQuestion(const Question& new_question) {
- question_ = new_question;
- }
-
- deadline_timer client_timer;
- deadline_timer lookup_timer;
-
- size_t queries_out_;
-
- // If we timed out ourselves (lookup timeout), stop issuing queries
- bool done_;
-
- // (re)send the query to the server.
- void send() {
- const int uc = upstream_->size();
- const int zs = zone_servers_.size();
- buffer_->clear();
- if (uc > 0) {
- int serverIndex = rand() % uc;
- dlog("Sending upstream query (" + question_.toText() +
- ") to " + upstream_->at(serverIndex).first);
- UDPQuery query(io_, question_,
- upstream_->at(serverIndex).first,
- upstream_->at(serverIndex).second, buffer_, this,
- query_timeout_);
- ++queries_out_;
- io_.post(query);
- } else if (zs > 0) {
- int serverIndex = rand() % zs;
- dlog("Sending query to zone server (" + question_.toText() +
- ") to " + zone_servers_.at(serverIndex).first);
- UDPQuery query(io_, question_,
- zone_servers_.at(serverIndex).first,
- zone_servers_.at(serverIndex).second, buffer_, this,
- query_timeout_);
- ++queries_out_;
- io_.post(query);
- } else {
- dlog("Error, no upstream servers to send to.");
- }
- }
-
- // This function is called by operator() if there is an actual
- // answer from a server and we are in recursive mode
- // depending on the contents, we go on recursing or return
- //
- // Note that the footprint may change as this function may
- // need to append data to the answer we are building later.
- //
- // returns true if we are done
- // returns false if we are not done
- bool handleRecursiveAnswer(const Message& incoming) {
- if (incoming.getRRCount(Message::SECTION_ANSWER) > 0) {
- dlog("Got final result, copying answer.");
- copyAnswerMessage(incoming, answer_message_);
- return true;
- } else {
- dlog("Got delegation, continuing");
- // ok we need to do some more processing.
- // the ns list should contain all nameservers
- // while the additional may contain addresses for
- // them.
- // this needs to tie into NSAS of course
- // for this very first mockup, hope there is an
- // address in additional and just use that
-
- // send query to the addresses in the delegation
- bool found_ns_address = false;
- zone_servers_.clear();
-
- for (RRsetIterator rrsi = incoming.beginSection(Message::SECTION_ADDITIONAL);
- rrsi != incoming.endSection(Message::SECTION_ADDITIONAL) && !found_ns_address;
- rrsi++) {
- ConstRRsetPtr rrs = *rrsi;
- if (rrs->getType() == RRType::A()) {
- // found address
- RdataIteratorPtr rdi = rrs->getRdataIterator();
- // just use the first for now
- if (!rdi->isLast()) {
- std::string addr_str = rdi->getCurrent().toText();
- dlog("[XX] first address found: " + addr_str);
- // now we have one address, simply
- // resend that exact same query
- // to that address and yield, when it
- // returns, loop again.
-
- // should use NSAS
- zone_servers_.push_back(addr_t(addr_str, 53));
- found_ns_address = true;
- }
- }
- }
- if (found_ns_address) {
- // next resolver round
- send();
- return false;
- } else {
- dlog("[XX] no ready-made addresses in additional. need nsas.");
- // this will result in answering with the delegation. oh well
- copyAnswerMessage(incoming, answer_message_);
- return true;
- }
- }
- }
-
-
-public:
- RunningQuery(asio::io_service& io, const Question &question,
- MessagePtr answer_message, shared_ptr<AddressVector> upstream,
- shared_ptr<AddressVector> upstream_root,
- OutputBufferPtr buffer, DNSServer* server,
- int query_timeout, int client_timeout, int lookup_timeout,
- unsigned retries) :
- io_(io),
- question_(question),
- answer_message_(answer_message),
- upstream_(upstream),
- upstream_root_(upstream_root),
- buffer_(buffer),
- server_(server->clone()),
- query_timeout_(query_timeout),
- retries_(retries),
- client_timer(io),
- lookup_timer(io),
- queries_out_(0),
- done_(false)
- {
- // Setup the timer to stop trying (lookup_timeout)
- if (lookup_timeout >= 0) {
- lookup_timer.expires_from_now(
- boost::posix_time::milliseconds(lookup_timeout));
- lookup_timer.async_wait(boost::bind(&RunningQuery::stop, this, false));
- }
-
- // Setup the timer to send an answer (client_timeout)
- if (client_timeout >= 0) {
- client_timer.expires_from_now(
- boost::posix_time::milliseconds(client_timeout));
- client_timer.async_wait(boost::bind(&RunningQuery::clientTimeout, this));
- }
-
- // should use NSAS for root servers
- // Adding root servers if not a forwarder
- if (upstream_->empty()) {
- if (upstream_root_->empty()) { //if no root ips given, use this
- zone_servers_.push_back(addr_t("192.5.5.241", 53));
- }
- else
- {
- //copy the list
- dlog("Size is " +
- boost::lexical_cast<string>(upstream_root_->size()) +
- "\n");
- //Use BOOST_FOREACH here? Is it faster?
- for(AddressVector::iterator it = upstream_root_->begin();
- it < upstream_root_->end(); it++) {
- zone_servers_.push_back(addr_t(it->first,it->second));
- dlog("Put " + zone_servers_.back().first + "into root list\n");
- }
- }
- }
-
- send();
- }
-
- virtual void clientTimeout() {
- // right now, just stop (should make SERVFAIL and send that
- // back, but not stop)
- stop(false);
- }
-
- virtual void stop(bool resume) {
- // if we cancel our timers, we will still get an event for
- // that, so we cannot delete ourselves just yet (those events
- // would be bound to a deleted object)
- // cancel them one by one, both cancels should get us back
- // here again.
- // same goes if we have an outstanding query (can't delete
- // until that one comes back to us)
- done_ = true;
- server_->resume(resume);
- if (lookup_timer.cancel() != 0) {
- return;
- }
- if (client_timer.cancel() != 0) {
- return;
- }
- if (queries_out_ > 0) {
- return;
- }
- delete this;
- }
-
- // This function is used as callback from DNSQuery.
- virtual void operator()(UDPQuery::Result result) {
- // XXX is this the place for TCP retry?
- --queries_out_;
- if (!done_ && result != UDPQuery::TIME_OUT) {
- // we got an answer
- Message incoming(Message::PARSE);
- InputBuffer ibuf(buffer_->getData(), buffer_->getLength());
- incoming.fromWire(ibuf);
-
- if (upstream_->size() == 0 &&
- incoming.getRcode() == Rcode::NOERROR()) {
- done_ = handleRecursiveAnswer(incoming);
- } else {
- copyAnswerMessage(incoming, answer_message_);
- done_ = true;
- }
-
- if (done_) {
- stop(result == UDPQuery::SUCCESS);
- }
- } else if (!done_ && retries_--) {
- // We timed out, but we have some retries, so send again
- dlog("Timeout, resending query");
- send();
- } else {
- // We are done
- stop(false);
- }
- }
-};
-
-}
-
-void
-RecursiveQuery::sendQuery(const Question& question,
- MessagePtr answer_message,
- OutputBufferPtr buffer,
- DNSServer* server)
-{
- // XXX: eventually we will need to be able to determine whether
- // the message should be sent via TCP or UDP, or sent initially via
- // UDP and then fall back to TCP on failure, but for the moment
- // we're only going to handle UDP.
- asio::io_service& io = dns_service_.get_io_service();
- // It will delete itself when it is done
- new RunningQuery(io, question, answer_message, upstream_, upstream_root_,
- buffer, server, query_timeout_, client_timeout_,
- lookup_timeout_, retries_);
-}
-
-class IntervalTimerImpl {
-private:
- // prohibit copy
- IntervalTimerImpl(const IntervalTimerImpl& source);
- IntervalTimerImpl& operator=(const IntervalTimerImpl& source);
-public:
- IntervalTimerImpl(IOService& io_service);
- ~IntervalTimerImpl();
- void setupTimer(const IntervalTimer::Callback& cbfunc,
- const uint32_t interval);
- void callback(const asio::error_code& error);
- void cancel() {
- timer_.cancel();
- interval_ = 0;
- }
- uint32_t getInterval() const { return (interval_); }
-private:
- // a function to update timer_ when it expires
- void updateTimer();
- // a function to call back when timer_ expires
- IntervalTimer::Callback cbfunc_;
- // interval in seconds
- uint32_t interval_;
- // asio timer
- asio::deadline_timer timer_;
-};
-
-IntervalTimerImpl::IntervalTimerImpl(IOService& io_service) :
- interval_(0), timer_(io_service.get_io_service())
-{}
-
-IntervalTimerImpl::~IntervalTimerImpl()
-{}
-
-void
-IntervalTimerImpl::setupTimer(const IntervalTimer::Callback& cbfunc,
- const uint32_t interval)
-{
- // Interval should not be 0.
- if (interval == 0) {
- isc_throw(isc::BadValue, "Interval should not be 0");
- }
- // Call back function should not be empty.
- if (cbfunc.empty()) {
- isc_throw(isc::InvalidParameter, "Callback function is empty");
- }
- cbfunc_ = cbfunc;
- interval_ = interval;
- // Set initial expire time.
- // At this point the timer is not running yet and will not expire.
- // After calling IOService::run(), the timer will expire.
- updateTimer();
- return;
-}
-
-void
-IntervalTimerImpl::updateTimer() {
- if (interval_ == 0) {
- // timer has been canceled. Do nothing.
- return;
- }
- try {
- // Update expire time to (current time + interval_).
- timer_.expires_from_now(boost::posix_time::seconds(interval_));
- } catch (const asio::system_error& e) {
- isc_throw(isc::Unexpected, "Failed to update timer");
- }
- // Reset timer.
- timer_.async_wait(boost::bind(&IntervalTimerImpl::callback, this, _1));
-}
-
-void
-IntervalTimerImpl::callback(const asio::error_code& cancelled) {
- // Do not call cbfunc_ in case the timer was cancelled.
- // The timer will be canelled in the destructor of asio::deadline_timer.
- if (!cancelled) {
- cbfunc_();
- // Set next expire time.
- updateTimer();
- }
-}
-
-IntervalTimer::IntervalTimer(IOService& io_service) {
- impl_ = new IntervalTimerImpl(io_service);
-}
-
-IntervalTimer::~IntervalTimer() {
- delete impl_;
-}
-
-void
-IntervalTimer::setupTimer(const Callback& cbfunc, const uint32_t interval) {
- return (impl_->setupTimer(cbfunc, interval));
-}
-
-void
-IntervalTimer::cancel() {
- impl_->cancel();
-}
-
-uint32_t
-IntervalTimer::getInterval() const {
- return (impl_->getInterval());
-}
-
-}
diff --git a/src/lib/asiolink/asiolink.h b/src/lib/asiolink/asiolink.h
index 5e46dfc..43a1244 100644
--- a/src/lib/asiolink/asiolink.h
+++ b/src/lib/asiolink/asiolink.h
@@ -18,31 +18,20 @@
// IMPORTANT NOTE: only very few ASIO headers files can be included in
// this file. In particular, asio.hpp should never be included here.
// See the description of the namespace below.
-#include <unistd.h> // for some network system calls
-#include <asio/ip/address.hpp>
-#include <boost/shared_ptr.hpp>
-#include <boost/function.hpp>
-#include <functional>
-#include <string>
-#include <vector>
-#include <utility>
+#include <asiolink/io_service.h>
+#include <asiolink/dns_service.h>
+#include <asiolink/dns_server.h>
+#include <asiolink/dns_lookup.h>
+#include <asiolink/dns_answer.h>
+#include <asiolink/simple_callback.h>
+#include <asiolink/recursive_query.h>
+#include <asiolink/interval_timer.h>
-#include <dns/buffer.h>
-#include <dns/message.h>
-#include <dns/question.h>
-
-#include <exceptions/exceptions.h>
-
-#include <asiolink/ioaddress.h>
-#include <asiolink/ioendpoint.h>
-#include <asiolink/iomessage.h>
-#include <asiolink/iosocket.h>
-
-namespace asio {
-// forward declaration for IOService::get_io_service() below
-class io_service;
-}
+#include <asiolink/io_address.h>
+#include <asiolink/io_endpoint.h>
+#include <asiolink/io_message.h>
+#include <asiolink/io_socket.h>
/// \namespace asiolink
/// \brief A wrapper interface for the ASIO library.
@@ -95,9 +84,7 @@ class io_service;
/// http://think-async.com/Asio/asio-1.3.1/doc/asio/reference/asio_handler_allocate.html
namespace asiolink {
-class DNSServiceImpl;
-struct IOServiceImpl;
-struct IntervalTimerImpl;
+
/// \brief An exception that is thrown if an error occurs within the IO
/// module. This is mainly intended to be a wrapper exception class for
@@ -108,594 +95,6 @@ public:
isc::Exception(file, line, what) {}
};
-/// \brief Forward declarations for classes used below
-class SimpleCallback;
-class DNSLookup;
-class DNSAnswer;
-
-/// \brief The \c IOService class is a wrapper for the ASIO \c io_service
-/// class.
-///
-class IOService {
- ///
- /// \name Constructors and Destructor
- ///
- /// Note: The copy constructor and the assignment operator are
- /// intentionally defined as private, making this class non-copyable.
- //@{
-private:
- IOService(const IOService& source);
- IOService& operator=(const IOService& source);
-public:
- /// \brief The constructor
- IOService();
- /// \brief The destructor.
- ~IOService();
- //@}
-
- /// \brief Start the underlying event loop.
- ///
- /// This method does not return control to the caller until
- /// the \c stop() method is called via some handler.
- void run();
-
- /// \brief Run the underlying event loop for a single event.
- ///
- /// This method return control to the caller as soon as the
- /// first handler has completed. (If no handlers are ready when
- /// it is run, it will block until one is.)
- void run_one();
-
- /// \brief Stop the underlying event loop.
- ///
- /// This will return the control to the caller of the \c run() method.
- void stop();
-
- /// \brief Return the native \c io_service object used in this wrapper.
- ///
- /// This is a short term work around to support other BIND 10 modules
- /// that share the same \c io_service with the authoritative server.
- /// It will eventually be removed once the wrapper interface is
- /// generalized.
- asio::io_service& get_io_service();
-
-private:
- IOServiceImpl* io_impl_;
-};
-
-///
-/// DNSService is the service that handles DNS queries and answers with
-/// a given IOService. This class is mainly intended to hold all the
-/// logic that is shared between the authoritative and the recursive
-/// server implementations. As such, it handles asio, including config
-/// updates (through the 'Checkinprovider'), and listening sockets.
-///
-class DNSService {
- ///
- /// \name Constructors and Destructor
- ///
- /// Note: The copy constructor and the assignment operator are
- /// intentionally defined as private, making this class non-copyable.
- //@{
-private:
- DNSService(const DNSService& source);
- DNSService& operator=(const DNSService& source);
-
-public:
- /// \brief The constructor with a specific IP address and port on which
- /// the services listen on.
- ///
- /// \param io_service The IOService to work with
- /// \param port the port to listen on
- /// \param address the IP address to listen on
- /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
- /// \param lookup The lookup provider (see \c DNSLookup)
- /// \param answer The answer provider (see \c DNSAnswer)
- DNSService(IOService& io_service, const char& port,
- const char& address, SimpleCallback* checkin,
- DNSLookup* lookup, DNSAnswer* answer);
- /// \brief The constructor with a specific port on which the services
- /// listen on.
- ///
- /// It effectively listens on "any" IPv4 and/or IPv6 addresses.
- /// IPv4/IPv6 services will be available if and only if \c use_ipv4
- /// or \c use_ipv6 is \c true, respectively.
- ///
- /// \param io_service The IOService to work with
- /// \param port the port to listen on
- /// \param ipv4 If true, listen on ipv4 'any'
- /// \param ipv6 If true, listen on ipv6 'any'
- /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
- /// \param lookup The lookup provider (see \c DNSLookup)
- /// \param answer The answer provider (see \c DNSAnswer)
- DNSService(IOService& io_service, const char& port,
- const bool use_ipv4, const bool use_ipv6,
- SimpleCallback* checkin, DNSLookup* lookup,
- DNSAnswer* answer);
- /// \brief The constructor without any servers.
- ///
- /// Use addServer() to add some servers.
- DNSService(IOService& io_service, SimpleCallback* checkin,
- DNSLookup* lookup, DNSAnswer* answer);
- /// \brief The destructor.
- ~DNSService();
- //@}
-
- /// \brief Add another server to the service
- void addServer(uint16_t port, const std::string &address);
- void addServer(const char &port, const std::string &address);
- /// \brief Remove all servers from the service
- void clearServers();
-
- /// \brief Return the native \c io_service object used in this wrapper.
- ///
- /// This is a short term work around to support other BIND 10 modules
- /// that share the same \c io_service with the authoritative server.
- /// It will eventually be removed once the wrapper interface is
- /// generalized.
- asio::io_service& get_io_service() { return io_service_.get_io_service(); }
-private:
- DNSServiceImpl* impl_;
- IOService& io_service_;
-};
-
-/// \brief The \c DNSServer class is a wrapper (and base class) for
-/// classes which provide DNS server functionality.
-///
-/// The classes derived from this one, \c TCPServer and \c UDPServer,
-/// act as the interface layer between clients sending queries, and
-/// functions defined elsewhere that provide answers to those queries.
-/// Those functions are described in more detail below under
-/// \c SimpleCallback, \c DNSLookup, and \c DNSAnswer.
-///
-/// Notes to developers:
-/// When constructed, this class (and its derived classes) will have its
-/// "self_" member set to point to "this". Objects of this class (as
-/// instantiated through a base class) are sometimes passed by
-/// reference (as this superclass); calls to methods in the base
-/// class are then rerouted via this pointer to methods in the derived
-/// class. This allows code from outside asiolink, with no specific
-/// knowledge of \c TCPServer or \c UDPServer, to access their methods.
-///
-/// This class is both assignable and copy-constructable. Its subclasses
-/// use the "stackless coroutine" pattern, meaning that it will copy itself
-/// when "forking", and that instances will be posted as ASIO handler
-/// objects, which are always copied.
-///
-/// Because these objects are frequently copied, it is recommended
-/// that derived classes be kept small to reduce copy overhead.
-class DNSServer {
-protected:
- ///
- /// \name Constructors and destructors
- ///
- /// This is intentionally defined as \c protected, as this base class
- /// should never be instantiated except as part of a derived class.
- //@{
- DNSServer() : self_(this) {}
-public:
- /// \brief The destructor
- virtual ~DNSServer() {}
- //@}
-
- ///
- /// \name Class methods
- ///
- /// These methods all make their calls indirectly via the "self_"
- /// pointer, ensuring that the functions ultimately invoked will be
- /// the ones in the derived class. This makes it possible to pass
- /// instances of derived classes as references to this base class
- /// without losing access to derived class data.
- ///
- //@{
- /// \brief The funtion operator
- virtual void operator()(asio::error_code ec = asio::error_code(),
- size_t length = 0)
- {
- (*self_)(ec, length);
- }
-
- /// \brief Resume processing of the server coroutine after an
- /// asynchronous call (e.g., to the DNS Lookup provider) has completed.
- ///
- /// \param done If true, this signals the system there is an answer
- /// to return.
- virtual void resume(const bool done) { self_->resume(done); }
-
- /// \brief Indicate whether the server is able to send an answer
- /// to a query.
- ///
- /// This is presently used only for testing purposes.
- virtual bool hasAnswer() { return (self_->hasAnswer()); }
-
- /// \brief Returns the current value of the 'coroutine' object
- ///
- /// This is a temporary method, intended to be used for debugging
- /// purposes during development and removed later. It allows
- /// callers from outside the coroutine object to retrieve information
- /// about its current state.
- ///
- /// \return The value of the 'coroutine' object
- virtual int value() { return (self_->value()); }
-
- /// \brief Returns a pointer to a clone of this DNSServer object.
- ///
- /// When a \c DNSServer object is copied or assigned, the result will
- /// normally be another \c DNSServer object containing a copy
- /// of the original "self_" pointer. Calling clone() guarantees
- /// that the underlying object is also correctly copied.
- ///
- /// \return A deep copy of this DNSServer object
- virtual DNSServer* clone() { return (self_->clone()); }
- //@}
-
-protected:
- /// \brief Lookup handler object.
- ///
- /// This is a protected class; it can only be instantiated
- /// from within a derived class of \c DNSServer.
- ///
- /// A server object that has received a query creates an instance
- /// of this class and scheudles it on the ASIO service queue
- /// using asio::io_service::post(). When the handler executes, it
- /// calls the asyncLookup() method in the server object to start a
- /// DNS lookup. When the lookup is complete, the server object is
- /// scheduled to resume, again using io_service::post().
- ///
- /// Note that the calling object is copied into the handler object,
- /// not referenced. This is because, once the calling object yields
- /// control to the handler, it falls out of scope and may disappear
- template <typename T>
- class AsyncLookup {
- public:
- AsyncLookup(T& caller) : caller_(caller) {}
- void operator()() { caller_.asyncLookup(); }
- private:
- T caller_;
- };
-
- /// \brief Carries out a DNS lookup.
- ///
- /// This function calls the \c DNSLookup object specified by the
- /// DNS server when the \c IOService was created, passing along
- /// the details of the query and a pointer back to the current
- /// server object. It is called asynchronously via the AsyncLookup
- /// handler class.
- virtual void asyncLookup() { self_->asyncLookup(); }
-
-private:
- DNSServer* self_;
-};
-
-/// \brief The \c DNSLookup class is an abstract base class for a DNS
-/// Lookup provider function.
-///
-/// Specific derived class implementations are hidden within the
-/// implementation. Instances of the derived classes can be called
-/// as functions via the operator() interface. Pointers to these
-/// instances can then be provided to the \c IOService class
-/// via its constructor.
-///
-/// A DNS Lookup provider function obtains the data needed to answer
-/// a DNS query (e.g., from authoritative data source, cache, or upstream
-/// query). After it has run, the OutputBuffer object passed to it
-/// should contain the answer to the query, in an internal representation.
-class DNSLookup {
- ///
- /// \name Constructors and Destructor
- ///
- /// Note: The copy constructor and the assignment operator are
- /// intentionally defined as private, making this class non-copyable.
- //@{
-private:
- DNSLookup(const DNSLookup& source);
- DNSLookup& operator=(const DNSLookup& source);
-protected:
- /// \brief The default constructor.
- ///
- /// This is intentionally defined as \c protected as this base class
- /// should never be instantiated (except as part of a derived class).
- DNSLookup() : self_(this) {}
-public:
- /// \brief The destructor
- virtual ~DNSLookup() {}
- //@}
- /// \brief The function operator
- ///
- /// This makes its call indirectly via the "self" pointer, ensuring
- /// that the function ultimately invoked will be the one in the derived
- /// class.
- ///
- /// \param io_message The event message to handle
- /// \param message The DNS MessagePtr that needs handling
- /// \param buffer The final answer is put here
- /// \param DNSServer DNSServer object to use
- virtual void operator()(const IOMessage& io_message,
- isc::dns::MessagePtr message,
- isc::dns::MessagePtr answer_message,
- isc::dns::OutputBufferPtr buffer,
- DNSServer* server) const
- {
- (*self_)(io_message, message, answer_message, buffer, server);
- }
-private:
- DNSLookup* self_;
-};
-
-/// \brief The \c DNSAnswer class is an abstract base class for a DNS
-/// Answer provider function.
-///
-/// Specific derived class implementations are hidden within the
-/// implementation. Instances of the derived classes can be called
-/// as functions via the operator() interface. Pointers to these
-/// instances can then be provided to the \c IOService class
-/// via its constructor.
-///
-/// A DNS Answer provider function takes answer data that has been obtained
-/// from a DNS Lookup provider functon and readies it to be sent to the
-/// client. After it has run, the OutputBuffer object passed to it should
-/// contain the answer to the query rendered into wire format.
-class DNSAnswer {
- ///
- /// \name Constructors and Destructor
- ///
- /// Note: The copy constructor and the assignment operator are
- /// intentionally defined as private, making this class non-copyable.
- //@{
-private:
- DNSAnswer(const DNSAnswer& source);
- DNSAnswer& operator=(const DNSAnswer& source);
-protected:
- /// \brief The default constructor.
- ///
- /// This is intentionally defined as \c protected as this base class
- /// should never be instantiated (except as part of a derived class).
- DNSAnswer() {}
-public:
- /// \brief The destructor
- virtual ~DNSAnswer() {}
- //@}
- /// \brief The function operator
- ///
- /// This makes its call indirectly via the "self" pointer, ensuring
- /// that the function ultimately invoked will be the one in the derived
- /// class.
- ///
- /// \param io_message The event message to handle
- /// \param message The DNS MessagePtr that needs handling
- /// \param buffer The result is put here
- virtual void operator()(const IOMessage& io_message,
- isc::dns::MessagePtr message,
- isc::dns::MessagePtr answer_message,
- isc::dns::OutputBufferPtr buffer) const = 0;
-};
-
-/// \brief The \c SimpleCallback class is an abstract base class for a
-/// simple callback function with the signature:
-///
-/// void simpleCallback(const IOMessage& io_message) const;
-///
-/// Specific derived class implementations are hidden within the
-/// implementation. Instances of the derived classes can be called
-/// as functions via the operator() interface. Pointers to these
-/// instances can then be provided to the \c IOService class
-/// via its constructor.
-///
-/// The \c SimpleCallback is expected to be used for basic, generic
-/// tasks such as checking for configuration changes. It may also be
-/// used for testing purposes.
-class SimpleCallback {
- ///
- /// \name Constructors and Destructor
- ///
- /// Note: The copy constructor and the assignment operator are
- /// intentionally defined as private, making this class non-copyable.
- //@{
-private:
- SimpleCallback(const SimpleCallback& source);
- SimpleCallback& operator=(const SimpleCallback& source);
-protected:
- /// \brief The default constructor.
- ///
- /// This is intentionally defined as \c protected as this base class
- /// should never be instantiated (except as part of a derived class).
- SimpleCallback() : self_(this) {}
-public:
- /// \brief The destructor
- virtual ~SimpleCallback() {}
- /// \brief The function operator
- //@}
- ///
- /// This makes its call indirectly via the "self" pointer, ensuring
- /// that the function ultimately invoked will be the one in the derived
- /// class.
- ///
- /// \param io_message The event message to handle
- virtual void operator()(const IOMessage& io_message) const {
- (*self_)(io_message);
- }
-private:
- SimpleCallback* self_;
-};
-
-/// \brief The \c RecursiveQuery class provides a layer of abstraction around
-/// the ASIO code that carries out an upstream query.
-///
-/// This design is very preliminary; currently it is only capable of
-/// handling simple forward requests to a single resolver.
-class RecursiveQuery {
- ///
- /// \name Constructors
- ///
- //@{
-public:
- /// \brief Constructor for use when acting as a forwarder
- ///
- /// This is currently the only way to construct \c RecursiveQuery
- /// object. The addresses of the forward nameservers is specified,
- /// and every upstream query will be sent to one random address.
- /// \param dns_service The DNS Service to perform the recursive
- /// query on.
- /// \param upstream Addresses and ports of the upstream servers
- /// to forward queries to.
- /// \param upstream_root Addresses and ports of the root servers
- /// to use when resolving.
- /// \param timeout How long to timeout the query, in ms
- /// -1 means never timeout (but do not use that).
- /// TODO: This should be computed somehow dynamically in future
- /// \param retries how many times we try again (0 means just send and
- /// and return if it returs).
- RecursiveQuery(DNSService& dns_service,
- const std::vector<std::pair<std::string, uint16_t> >&
- upstream,
- const std::vector<std::pair<std::string, uint16_t> >&
- upstream_root,
- int query_timeout = 2000,
- int client_timeout = 4000,
- int lookup_timeout = 30000,
- unsigned retries = 3);
- //@}
-
- /// \brief Initiates an upstream query in the \c RecursiveQuery object.
- ///
- /// When sendQuery() is called, a message is sent asynchronously to
- /// the upstream name server. When a reply arrives, 'server'
- /// is placed on the ASIO service queue via io_service::post(), so
- /// that the original \c DNSServer objct can resume processing.
- ///
- /// \param question The question being answered <qname/qclass/qtype>
- /// \param buffer An output buffer into which the response can be copied
- /// \param server A pointer to the \c DNSServer object handling the client
- void sendQuery(const isc::dns::Question& question,
- isc::dns::MessagePtr answer_message,
- isc::dns::OutputBufferPtr buffer,
- DNSServer* server);
-private:
- DNSService& dns_service_;
- boost::shared_ptr<std::vector<std::pair<std::string, uint16_t> > >
- upstream_;
- boost::shared_ptr<std::vector<std::pair<std::string, uint16_t> > >
- upstream_root_;
- int query_timeout_;
- int client_timeout_;
- int lookup_timeout_;
- unsigned retries_;
-};
-
-/// \brief The \c IntervalTimer class is a wrapper for the ASIO
-/// \c asio::deadline_timer class.
-///
-/// This class is implemented to use \c asio::deadline_timer as
-/// interval timer.
-///
-/// \c setupTimer() sets a timer to expire on (now + interval) and
-/// a call back function.
-///
-/// \c IntervalTimerImpl::callback() is called by the timer when
-/// it expires.
-///
-/// The function calls the call back function set by \c setupTimer()
-/// and updates the timer to expire in (now + interval) seconds.
-/// The type of call back function is \c void(void).
-///
-/// The call back function will not be called if the instance of this
-/// class is destructed before the timer is expired.
-///
-/// Note: Destruction of an instance of this class while call back
-/// is pending causes throwing an exception from \c IOService.
-///
-/// Sample code:
-/// \code
-/// void function_to_call_back() {
-/// // this function will be called periodically
-/// }
-/// int interval_in_seconds = 1;
-/// IOService io_service;
-///
-/// IntervalTimer intervalTimer(io_service);
-/// intervalTimer.setupTimer(function_to_call_back, interval_in_seconds);
-/// io_service.run();
-/// \endcode
-///
-class IntervalTimer {
-public:
- /// \name The type of timer callback function
- typedef boost::function<void()> Callback;
-
- ///
- /// \name Constructors and Destructor
- ///
- /// Note: The copy constructor and the assignment operator are
- /// intentionally defined as private, making this class non-copyable.
- //@{
-private:
- IntervalTimer(const IntervalTimer& source);
- IntervalTimer& operator=(const IntervalTimer& source);
-public:
- /// \brief The constructor with \c IOService.
- ///
- /// This constructor may throw a standard exception if
- /// memory allocation fails inside the method.
- /// This constructor may also throw \c asio::system_error.
- ///
- /// \param io_service A reference to an instance of IOService
- ///
- IntervalTimer(IOService& io_service);
-
- /// \brief The destructor.
- ///
- /// This destructor never throws an exception.
- ///
- /// On the destruction of this class the timer will be canceled
- /// inside \c asio::deadline_timer.
- ///
- ~IntervalTimer();
- //@}
-
- /// \brief Register timer callback function and interval.
- ///
- /// This function sets callback function and interval in seconds.
- /// Timer will actually start after calling \c IOService::run().
- ///
- /// \param cbfunc A reference to a function \c void(void) to call back
- /// when the timer is expired (should not be an empty functor)
- /// \param interval Interval in seconds (greater than 0)
- ///
- /// Note: IntervalTimer will not pass \c asio::error_code to
- /// call back function. In case the timer is cancelled, the function
- /// will not be called.
- ///
- /// \throw isc::InvalidParameter cbfunc is empty
- /// \throw isc::BadValue interval is 0
- /// \throw isc::Unexpected ASIO library error
- ///
- void setupTimer(const Callback& cbfunc, const uint32_t interval);
-
- /// Cancel the timer.
- ///
- /// If the timer has been set up, this method cancels any asynchronous
- /// events waiting on the timer and stops the timer itself.
- /// If the timer has already been canceled, this method effectively does
- /// nothing.
- ///
- /// This method never throws an exception.
- void cancel();
-
- /// Return the timer interval.
- ///
- /// This method returns the timer interval in seconds if it's running;
- /// if the timer has been canceled it returns 0.
- ///
- /// This method never throws an exception.
- ///
- /// Note: We may want to change the granularity of the timer to
- /// milliseconds or even finer. If and when this happens the semantics
- /// of the return value of this method will be changed accordingly.
- uint32_t getInterval() const;
-
-private:
- IntervalTimerImpl* impl_;
-};
} // asiolink
#endif // __ASIOLINK_H
diff --git a/src/lib/asiolink/dns_answer.h b/src/lib/asiolink/dns_answer.h
new file mode 100644
index 0000000..84e1f6f
--- /dev/null
+++ b/src/lib/asiolink/dns_answer.h
@@ -0,0 +1,73 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __ASIOLINK_DNS_ANSWER_H
+#define __ASIOLINK_DNS_ANSWER_H 1
+
+#include <asiolink/io_message.h>
+
+namespace asiolink {
+
+/// \brief The \c DNSAnswer class is an abstract base class for a DNS
+/// Answer provider function.
+///
+/// Specific derived class implementations are hidden within the
+/// implementation. Instances of the derived classes can be called
+/// as functions via the operator() interface. Pointers to these
+/// instances can then be provided to the \c IOService class
+/// via its constructor.
+///
+/// A DNS Answer provider function takes answer data that has been obtained
+/// from a DNS Lookup provider functon and readies it to be sent to the
+/// client. After it has run, the OutputBuffer object passed to it should
+/// contain the answer to the query rendered into wire format.
+class DNSAnswer {
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non-copyable.
+ //@{
+private:
+ DNSAnswer(const DNSAnswer& source);
+ DNSAnswer& operator=(const DNSAnswer& source);
+protected:
+ /// \brief The default constructor.
+ ///
+ /// This is intentionally defined as \c protected as this base class
+ /// should never be instantiated (except as part of a derived class).
+ DNSAnswer() {}
+public:
+ /// \brief The destructor
+ virtual ~DNSAnswer() {}
+ //@}
+ /// \brief The function operator
+ ///
+ /// This makes its call indirectly via the "self" pointer, ensuring
+ /// that the function ultimately invoked will be the one in the derived
+ /// class.
+ ///
+ /// \param io_message The event message to handle
+ /// \param query_message The DNS MessagePtr of the original query
+ /// \param answer_message The DNS MessagePtr of the answer we are
+ /// building
+ /// \param buffer Intermediate data results are put here
+ virtual void operator()(const IOMessage& io_message,
+ isc::dns::MessagePtr query_message,
+ isc::dns::MessagePtr answer_message,
+ isc::dns::OutputBufferPtr buffer) const = 0;
+};
+
+} // namespace asiolink
+#endif // __ASIOLINK_DNS_ANSWER_H
diff --git a/src/lib/asiolink/dns_lookup.h b/src/lib/asiolink/dns_lookup.h
new file mode 100644
index 0000000..0788853
--- /dev/null
+++ b/src/lib/asiolink/dns_lookup.h
@@ -0,0 +1,81 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __ASIOLINK_DNS_LOOKUP_H
+#define __ASIOLINK_DNS_LOOKUP_H 1
+
+#include <asiolink/io_message.h>
+#include <asiolink/dns_server.h>
+#include <dns/buffer.h>
+#include <dns/message.h>
+
+namespace asiolink {
+
+/// \brief The \c DNSLookup class is an abstract base class for a DNS
+/// Lookup provider function.
+///
+/// Specific derived class implementations are hidden within the
+/// implementation. Instances of the derived classes can be called
+/// as functions via the operator() interface. Pointers to these
+/// instances can then be provided to the \c IOService class
+/// via its constructor.
+///
+/// A DNS Lookup provider function obtains the data needed to answer
+/// a DNS query (e.g., from authoritative data source, cache, or upstream
+/// query). After it has run, the OutputBuffer object passed to it
+/// should contain the answer to the query, in an internal representation.
+class DNSLookup {
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non-copyable.
+ //@{
+private:
+ DNSLookup(const DNSLookup& source);
+ DNSLookup& operator=(const DNSLookup& source);
+protected:
+ /// \brief The default constructor.
+ ///
+ /// This is intentionally defined as \c protected as this base class
+ /// should never be instantiated (except as part of a derived class).
+ DNSLookup() : self_(this) {}
+public:
+ /// \brief The destructor
+ virtual ~DNSLookup() {}
+ //@}
+ /// \brief The function operator
+ ///
+ /// This makes its call indirectly via the "self" pointer, ensuring
+ /// that the function ultimately invoked will be the one in the derived
+ /// class.
+ ///
+ /// \param io_message The event message to handle
+ /// \param message The DNS MessagePtr that needs handling
+ /// \param buffer The final answer is put here
+ /// \param DNSServer DNSServer object to use
+ virtual void operator()(const IOMessage& io_message,
+ isc::dns::MessagePtr message,
+ isc::dns::MessagePtr answer_message,
+ isc::dns::OutputBufferPtr buffer,
+ DNSServer* server) const
+ {
+ (*self_)(io_message, message, answer_message, buffer, server);
+ }
+private:
+ DNSLookup* self_;
+};
+
+} // namespace asiolink
+#endif // __ASIOLINK_DNS_LOOKUP_H
diff --git a/src/lib/asiolink/dns_server.h b/src/lib/asiolink/dns_server.h
new file mode 100644
index 0000000..6545275
--- /dev/null
+++ b/src/lib/asiolink/dns_server.h
@@ -0,0 +1,152 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __ASIOLINK_DNS_SERVER_H
+#define __ASIOLINK_DNS_SERVER_H 1
+
+#include <asiolink/io_message.h>
+
+namespace asiolink {
+
+/// \brief The \c DNSServer class is a wrapper (and base class) for
+/// classes which provide DNS server functionality.
+///
+/// The classes derived from this one, \c TCPServer and \c UDPServer,
+/// act as the interface layer between clients sending queries, and
+/// functions defined elsewhere that provide answers to those queries.
+/// Those functions are described in more detail below under
+/// \c SimpleCallback, \c DNSLookup, and \c DNSAnswer.
+///
+/// Notes to developers:
+/// When constructed, this class (and its derived classes) will have its
+/// "self_" member set to point to "this". Objects of this class (as
+/// instantiated through a base class) are sometimes passed by
+/// reference (as this superclass); calls to methods in the base
+/// class are then rerouted via this pointer to methods in the derived
+/// class. This allows code from outside asiolink, with no specific
+/// knowledge of \c TCPServer or \c UDPServer, to access their methods.
+///
+/// This class is both assignable and copy-constructable. Its subclasses
+/// use the "stackless coroutine" pattern, meaning that it will copy itself
+/// when "forking", and that instances will be posted as ASIO handler
+/// objects, which are always copied.
+///
+/// Because these objects are frequently copied, it is recommended
+/// that derived classes be kept small to reduce copy overhead.
+class DNSServer {
+protected:
+ ///
+ /// \name Constructors and destructors
+ ///
+ /// This is intentionally defined as \c protected, as this base class
+ /// should never be instantiated except as part of a derived class.
+ //@{
+ DNSServer() : self_(this) {}
+public:
+ /// \brief The destructor
+ virtual ~DNSServer() {}
+ //@}
+
+ ///
+ /// \name Class methods
+ ///
+ /// These methods all make their calls indirectly via the "self_"
+ /// pointer, ensuring that the functions ultimately invoked will be
+ /// the ones in the derived class. This makes it possible to pass
+ /// instances of derived classes as references to this base class
+ /// without losing access to derived class data.
+ ///
+ //@{
+ /// \brief The funtion operator
+ virtual void operator()(asio::error_code ec = asio::error_code(),
+ size_t length = 0)
+ {
+ (*self_)(ec, length);
+ }
+
+ /// \brief Resume processing of the server coroutine after an
+ /// asynchronous call (e.g., to the DNS Lookup provider) has completed.
+ ///
+ /// \param done If true, this signals the system there is an answer
+ /// to return.
+ virtual void resume(const bool done) { self_->resume(done); }
+
+ /// \brief Indicate whether the server is able to send an answer
+ /// to a query.
+ ///
+ /// This is presently used only for testing purposes.
+ virtual bool hasAnswer() { return (self_->hasAnswer()); }
+
+ /// \brief Returns the current value of the 'coroutine' object
+ ///
+ /// This is a temporary method, intended to be used for debugging
+ /// purposes during development and removed later. It allows
+ /// callers from outside the coroutine object to retrieve information
+ /// about its current state.
+ ///
+ /// \return The value of the 'coroutine' object
+ virtual int value() { return (self_->value()); }
+
+ /// \brief Returns a pointer to a clone of this DNSServer object.
+ ///
+ /// When a \c DNSServer object is copied or assigned, the result will
+ /// normally be another \c DNSServer object containing a copy
+ /// of the original "self_" pointer. Calling clone() guarantees
+ /// that the underlying object is also correctly copied.
+ ///
+ /// \return A deep copy of this DNSServer object
+ virtual DNSServer* clone() { return (self_->clone()); }
+ //@}
+
+protected:
+ /// \brief Lookup handler object.
+ ///
+ /// This is a protected class; it can only be instantiated
+ /// from within a derived class of \c DNSServer.
+ ///
+ /// A server object that has received a query creates an instance
+ /// of this class and scheudles it on the ASIO service queue
+ /// using asio::io_service::post(). When the handler executes, it
+ /// calls the asyncLookup() method in the server object to start a
+ /// DNS lookup. When the lookup is complete, the server object is
+ /// scheduled to resume, again using io_service::post().
+ ///
+ /// Note that the calling object is copied into the handler object,
+ /// not referenced. This is because, once the calling object yields
+ /// control to the handler, it falls out of scope and may disappear
+ template <typename T>
+ class AsyncLookup {
+ public:
+ AsyncLookup(T& caller) : caller_(caller) {}
+ void operator()() { caller_.asyncLookup(); }
+ private:
+ T caller_;
+ };
+
+ /// \brief Carries out a DNS lookup.
+ ///
+ /// This function calls the \c DNSLookup object specified by the
+ /// DNS server when the \c IOService was created, passing along
+ /// the details of the query and a pointer back to the current
+ /// server object. It is called asynchronously via the AsyncLookup
+ /// handler class.
+ virtual void asyncLookup() { self_->asyncLookup(); }
+
+private:
+ DNSServer* self_;
+};
+
+
+} // asiolink
+#endif // __ASIOLINK_DNS_SERVER_H
diff --git a/src/lib/asiolink/dns_service.cc b/src/lib/asiolink/dns_service.cc
new file mode 100644
index 0000000..98ca032
--- /dev/null
+++ b/src/lib/asiolink/dns_service.cc
@@ -0,0 +1,192 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+// unistd is needed for asio.hpp with SunStudio
+#include <unistd.h>
+
+#include <asio.hpp>
+
+#include <asiolink/io_service.h>
+#include <asiolink/tcp_server.h>
+#include <asiolink/udp_server.h>
+
+#include <log/dummylog.h>
+
+#include <boost/lexical_cast.hpp>
+
+using isc::log::dlog;
+
+namespace asiolink {
+
+class SimpleCallback;
+class DNSLookup;
+class DNSAnswer;
+
+namespace {
+
+asio::ip::address
+convertAddr(const std::string& address) {
+ asio::error_code err;
+ asio::ip::address addr = asio::ip::address::from_string(address, err);
+ if (err) {
+ isc_throw(IOError, "Invalid IP address '" << &address << "': "
+ << err.message());
+ }
+ return (addr);
+}
+
+}
+
+
+class DNSServiceImpl {
+public:
+ DNSServiceImpl(IOService& io_service, const char& port,
+ const asio::ip::address* v4addr,
+ const asio::ip::address* v6addr,
+ SimpleCallback* checkin, DNSLookup* lookup,
+ DNSAnswer* answer);
+
+ IOService& io_service_;
+
+ typedef boost::shared_ptr<UDPServer> UDPServerPtr;
+ typedef boost::shared_ptr<TCPServer> TCPServerPtr;
+ typedef boost::shared_ptr<DNSServer> DNSServerPtr;
+ std::vector<DNSServerPtr> servers_;
+ SimpleCallback *checkin_;
+ DNSLookup *lookup_;
+ DNSAnswer *answer_;
+
+ void addServer(uint16_t port, const asio::ip::address& address) {
+ try {
+ dlog(std::string("Initialize TCP server at ") + address.to_string() + ":" + boost::lexical_cast<std::string>(port));
+ TCPServerPtr tcpServer(new TCPServer(io_service_.get_io_service(),
+ address, port, checkin_, lookup_, answer_));
+ (*tcpServer)();
+ servers_.push_back(tcpServer);
+ dlog(std::string("Initialize UDP server at ") + address.to_string() + ":" + boost::lexical_cast<std::string>(port));
+ UDPServerPtr udpServer(new UDPServer(io_service_.get_io_service(),
+ address, port, checkin_, lookup_, answer_));
+ (*udpServer)();
+ servers_.push_back(udpServer);
+ }
+ catch (const asio::system_error& err) {
+ // We need to catch and convert any ASIO level exceptions.
+ // This can happen for unavailable address, binding a privilege port
+ // without the privilege, etc.
+ isc_throw(IOError, "Failed to initialize network servers: " <<
+ err.what());
+ }
+ }
+ void addServer(const char& port, const asio::ip::address& address) {
+ uint16_t portnum;
+ try {
+ // XXX: SunStudio with stlport4 doesn't reject some invalid
+ // representation such as "-1" by lexical_cast<uint16_t>, so
+ // we convert it into a signed integer of a larger size and perform
+ // range check ourselves.
+ const int32_t portnum32 = boost::lexical_cast<int32_t>(&port);
+ if (portnum32 < 0 || portnum32 > 65535) {
+ isc_throw(IOError, "Invalid port number '" << &port);
+ }
+ portnum = portnum32;
+ } catch (const boost::bad_lexical_cast& ex) {
+ isc_throw(IOError, "Invalid port number '" << &port << "': " <<
+ ex.what());
+ }
+ addServer(portnum, address);
+ }
+};
+
+DNSServiceImpl::DNSServiceImpl(IOService& io_service,
+ const char& port,
+ const asio::ip::address* const v4addr,
+ const asio::ip::address* const v6addr,
+ SimpleCallback* checkin,
+ DNSLookup* lookup,
+ DNSAnswer* answer) :
+ io_service_(io_service),
+ checkin_(checkin),
+ lookup_(lookup),
+ answer_(answer)
+{
+
+ if (v4addr) {
+ addServer(port, *v4addr);
+ }
+ if (v6addr) {
+ addServer(port, *v6addr);
+ }
+}
+
+DNSService::DNSService(IOService& io_service,
+ const char& port, const char& address,
+ SimpleCallback* checkin,
+ DNSLookup* lookup,
+ DNSAnswer* answer) :
+ impl_(new DNSServiceImpl(io_service, port, NULL, NULL, checkin, lookup,
+ answer)), io_service_(io_service)
+{
+ addServer(port, &address);
+}
+
+DNSService::DNSService(IOService& io_service,
+ const char& port,
+ const bool use_ipv4, const bool use_ipv6,
+ SimpleCallback* checkin,
+ DNSLookup* lookup,
+ DNSAnswer* answer) :
+ impl_(NULL), io_service_(io_service)
+{
+ const asio::ip::address v4addr_any =
+ asio::ip::address(asio::ip::address_v4::any());
+ const asio::ip::address* const v4addrp = use_ipv4 ? &v4addr_any : NULL;
+ const asio::ip::address v6addr_any =
+ asio::ip::address(asio::ip::address_v6::any());
+ const asio::ip::address* const v6addrp = use_ipv6 ? &v6addr_any : NULL;
+ impl_ = new DNSServiceImpl(io_service, port, v4addrp, v6addrp, checkin, lookup, answer);
+}
+
+DNSService::DNSService(IOService& io_service, SimpleCallback* checkin,
+ DNSLookup* lookup, DNSAnswer *answer) :
+ impl_(new DNSServiceImpl(io_service, *"0", NULL, NULL, checkin, lookup,
+ answer)), io_service_(io_service)
+{
+}
+
+DNSService::~DNSService() {
+ delete impl_;
+}
+
+void
+DNSService::addServer(const char& port, const std::string& address) {
+ impl_->addServer(port, convertAddr(address));
+}
+
+void
+DNSService::addServer(uint16_t port, const std::string& address) {
+ impl_->addServer(port, convertAddr(address));
+}
+
+void
+DNSService::clearServers() {
+ // FIXME: This does not work, it does not close the socket.
+ // How is it done?
+ impl_->servers_.clear();
+}
+
+
+
+} // namespace asiolink
diff --git a/src/lib/asiolink/dns_service.h b/src/lib/asiolink/dns_service.h
new file mode 100644
index 0000000..6b14345
--- /dev/null
+++ b/src/lib/asiolink/dns_service.h
@@ -0,0 +1,106 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __ASIOLINK_DNS_SERVICE_H
+#define __ASIOLINK_DNS_SERVICE_H 1
+
+#include <resolve/resolver_interface.h>
+
+#include <asiolink/io_service.h>
+
+namespace asiolink {
+
+class SimpleCallback;
+class DNSLookup;
+class DNSAnswer;
+class DNSServiceImpl;
+
+///
+/// DNSService is the service that handles DNS queries and answers with
+/// a given IOService. This class is mainly intended to hold all the
+/// logic that is shared between the authoritative and the recursive
+/// server implementations. As such, it handles asio, including config
+/// updates (through the 'Checkinprovider'), and listening sockets.
+///
+class DNSService {
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non-copyable.
+ //@{
+private:
+ DNSService(const DNSService& source);
+ DNSService& operator=(const DNSService& source);
+
+public:
+ /// \brief The constructor with a specific IP address and port on which
+ /// the services listen on.
+ ///
+ /// \param io_service The IOService to work with
+ /// \param port the port to listen on
+ /// \param address the IP address to listen on
+ /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
+ /// \param lookup The lookup provider (see \c DNSLookup)
+ /// \param answer The answer provider (see \c DNSAnswer)
+ DNSService(IOService& io_service, const char& port,
+ const char& address, SimpleCallback* checkin,
+ DNSLookup* lookup, DNSAnswer* answer);
+ /// \brief The constructor with a specific port on which the services
+ /// listen on.
+ ///
+ /// It effectively listens on "any" IPv4 and/or IPv6 addresses.
+ /// IPv4/IPv6 services will be available if and only if \c use_ipv4
+ /// or \c use_ipv6 is \c true, respectively.
+ ///
+ /// \param io_service The IOService to work with
+ /// \param port the port to listen on
+ /// \param ipv4 If true, listen on ipv4 'any'
+ /// \param ipv6 If true, listen on ipv6 'any'
+ /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
+ /// \param lookup The lookup provider (see \c DNSLookup)
+ /// \param answer The answer provider (see \c DNSAnswer)
+ DNSService(IOService& io_service, const char& port,
+ const bool use_ipv4, const bool use_ipv6,
+ SimpleCallback* checkin, DNSLookup* lookup,
+ DNSAnswer* answer);
+ /// \brief The constructor without any servers.
+ ///
+ /// Use addServer() to add some servers.
+ DNSService(IOService& io_service, SimpleCallback* checkin,
+ DNSLookup* lookup, DNSAnswer* answer);
+ /// \brief The destructor.
+ ~DNSService();
+ //@}
+
+ /// \brief Add another server to the service
+ void addServer(uint16_t port, const std::string &address);
+ void addServer(const char &port, const std::string &address);
+ /// \brief Remove all servers from the service
+ void clearServers();
+
+ /// \brief Return the native \c io_service object used in this wrapper.
+ ///
+ /// This is a short term work around to support other BIND 10 modules
+ /// that share the same \c io_service with the authoritative server.
+ /// It will eventually be removed once the wrapper interface is
+ /// generalized.
+ asio::io_service& get_io_service() { return io_service_.get_io_service(); }
+private:
+ DNSServiceImpl* impl_;
+ IOService& io_service_;
+};
+
+} // namespace asiolink
+#endif // __ASIOLINK_DNS_SERVICE_H
diff --git a/src/lib/asiolink/internal/Makefile.am b/src/lib/asiolink/internal/Makefile.am
deleted file mode 100644
index 3c6155b..0000000
--- a/src/lib/asiolink/internal/Makefile.am
+++ /dev/null
@@ -1 +0,0 @@
-SUBDIRS = tests
diff --git a/src/lib/asiolink/internal/coroutine.h b/src/lib/asiolink/internal/coroutine.h
deleted file mode 100644
index 985888b..0000000
--- a/src/lib/asiolink/internal/coroutine.h
+++ /dev/null
@@ -1,133 +0,0 @@
-//
-// coroutine.h
-// ~~~~~~~~~~~
-//
-// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
-//
-// Distributed under the Boost Software License, Version 1.0. (See accompanying
-// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
-//
-
-#ifndef COROUTINE_HPP
-#define COROUTINE_HPP
-
-
-// \brief Coroutine object
-//
-// A coroutine object maintains the state of a re-enterable routine. It
-// is assignable and copy-constructable, and can be used as a base class
-// for a class that uses it, or as a data member. The copy overhead is
-// a single int.
-//
-// A reenterable function contains a CORO_REENTER (coroutine) { ... }
-// block. Whenever an asychrnonous operation is initiated within the
-// routine, the function is provided as the handler object. (The simplest
-// way to do this is to have the reenterable function be the operator()
-// member for the coroutine object itself.) For example:
-//
-// CORO_YIELD socket->async_read_some(buffer, *this);
-//
-// The CORO_YIELD keyword updates the current status of the coroutine to
-// indicate the line number currently being executed. The
-// async_read_some() call is initiated, with a copy of the updated
-// corotutine as its handler object, and the current coroutine exits. When
-// the async_read_some() call finishes, the copied coroutine will be
-// called, and will resume processing exactly where the original one left
-// off--right after asynchronous call. This allows asynchronous I/O
-// routines to be written with a logical flow, step following step, rather
-// than as a linked chain of separate handler functions.
-//
-// When necessary, a coroutine can fork itself using the CORO_FORK keyword.
-// This updates the status of the coroutine and makes a copy. The copy can
-// then be called directly or posted to the ASIO service queue so that both
-// coroutines will continue forward, one "parent" and one "child". The
-// is_parent() and is_child() methods indicate which is which.
-//
-// The CORO_REENTER, CORO_YIELD and CORO_FORK keywords are implemented
-// via preprocessor macros. The CORO_REENTER block is actually a large,
-// complex switch statement. Because of this, inline variable declaration
-// is impossible within CORO_REENTER unless it is done in a subsidiary
-// scope--and if it is, that scope cannot contain CORO_YIELD or CORO_FORK
-// keywords.
-//
-// Because coroutines are frequently copied, it is best to minimize copy
-// overhead by limiting the size of data members in derived classes.
-//
-// It should be noted that when a coroutine falls out of scope its memory
-// is reclaimed, even though it may be scheduled to resume when an
-// asynchronous operation completes. Any shared_ptr<> objects declared in
-// the coroutine may be destroyed if their reference count drops to zero,
-// in which case the coroutine will have serious problems once it resumes.
-// One solution so this is to have the space that will be used by a
-// coroutine pre-allocated and stored on a free list; a new coroutine can
-// fetch the block of space off a free list, place a shared pointer to it
-// on an "in use" list, and carry on. The reference in the "in use" list
-// would prevent the data from being destroyed.
-class coroutine
-{
-public:
- coroutine() : value_(0) {}
- virtual ~coroutine() {}
- bool is_child() const { return value_ < 0; }
- bool is_parent() const { return !is_child(); }
- bool is_complete() const { return value_ == -1; }
- int get_value() const { return value_; }
-private:
- friend class coroutine_ref;
- int value_;
-};
-
-class coroutine_ref
-{
-public:
- coroutine_ref(coroutine& c) : value_(c.value_), modified_(false) {}
- coroutine_ref(coroutine* c) : value_(c->value_), modified_(false) {}
- ~coroutine_ref() { if (!modified_) value_ = -1; }
- operator int() const { return value_; }
- int& operator=(int v) { modified_ = true; return value_ = v; }
-private:
- void operator=(const coroutine_ref&);
- int& value_;
- bool modified_;
-};
-
-#define CORO_REENTER(c) \
- switch (coroutine_ref _coro_value = c) \
- case -1: if (_coro_value) \
- { \
- goto terminate_coroutine; \
- terminate_coroutine: \
- _coro_value = -1; \
- goto bail_out_of_coroutine; \
- bail_out_of_coroutine: \
- break; \
- } \
- else case 0:
-
-#define CORO_YIELD \
- for (_coro_value = __LINE__;;) \
- if (_coro_value == 0) \
- { \
- case __LINE__: ; \
- break; \
- } \
- else \
- switch (_coro_value ? 0 : 1) \
- for (;;) \
- case -1: if (_coro_value) \
- goto terminate_coroutine; \
- else for (;;) \
- case 1: if (_coro_value) \
- goto bail_out_of_coroutine; \
- else case 0:
-
-#define CORO_FORK \
- for (_coro_value = -__LINE__;; _coro_value = __LINE__) \
- if (_coro_value == __LINE__) \
- { \
- case -__LINE__: ; \
- break; \
- } \
- else
-#endif // COROUTINE_HPP
-
diff --git a/src/lib/asiolink/internal/tcpdns.h b/src/lib/asiolink/internal/tcpdns.h
deleted file mode 100644
index a97ed17..0000000
--- a/src/lib/asiolink/internal/tcpdns.h
+++ /dev/null
@@ -1,224 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#ifndef __TCPDNS_H
-#define __TCPDNS_H 1
-
-#include <config.h>
-
-
-#include <asio.hpp>
-#include <boost/shared_array.hpp>
-#include <boost/shared_ptr.hpp>
-
-#include <dns/buffer.h>
-#include <dns/message.h>
-
-#include <asiolink/asiolink.h>
-#include <asiolink/internal/coroutine.h>
-
-// This file contains TCP-specific implementations of generic classes
-// defined in asiolink.h. It is *not* intended to be part of the public
-// API.
-
-namespace asiolink {
-/// \brief The \c TCPEndpoint class is a concrete derived class of
-/// \c IOEndpoint that represents an endpoint of a TCP connection.
-///
-/// In the current implementation, an object of this class is always
-/// instantiated within the wrapper routines. Applications are expected to
-/// get access to the object via the abstract base class, \c IOEndpoint.
-/// This design may be changed when we generalize the wrapper interface.
-///
-/// Note: this implementation is optimized for the case where this object
-/// is created from an ASIO endpoint object in a receiving code path
-/// by avoiding to make a copy of the base endpoint. For TCP it may not be
-/// a big deal, but when we receive UDP packets at a high rate, the copy
-/// overhead might be significant.
-class TCPEndpoint : public IOEndpoint {
-public:
- ///
- /// \name Constructors and Destructor
- ///
- //@{
- /// \brief Constructor from a pair of address and port.
- ///
- /// \param address The IP address of the endpoint.
- /// \param port The TCP port number of the endpoint.
- TCPEndpoint(const IOAddress& address, const unsigned short port) :
- asio_endpoint_placeholder_(
- new asio::ip::tcp::endpoint(
- asio::ip::address::from_string(address.toText()), port)),
- asio_endpoint_(*asio_endpoint_placeholder_)
- {}
-
- /// \brief Constructor from an ASIO TCP endpoint.
- ///
- /// This constructor is designed to be an efficient wrapper for the
- /// corresponding ASIO class, \c tcp::endpoint.
- ///
- /// \param asio_endpoint The ASIO representation of the TCP endpoint.
- TCPEndpoint(const asio::ip::tcp::endpoint& asio_endpoint) :
- asio_endpoint_placeholder_(NULL), asio_endpoint_(asio_endpoint)
- {}
-
- /// \brief The destructor.
- ~TCPEndpoint() { delete asio_endpoint_placeholder_; }
- //@}
-
- IOAddress getAddress() const {
- return (asio_endpoint_.address());
- }
-
- uint16_t getPort() const {
- return (asio_endpoint_.port());
- }
-
- short getProtocol() const {
- return (asio_endpoint_.protocol().protocol());
- }
-
- short getFamily() const {
- return (asio_endpoint_.protocol().family());
- }
-
- // This is not part of the exosed IOEndpoint API but allows
- // direct access to the ASIO implementation of the endpoint
- const asio::ip::tcp::endpoint& getASIOEndpoint() const {
- return (asio_endpoint_);
- }
-
-private:
- const asio::ip::tcp::endpoint* asio_endpoint_placeholder_;
- const asio::ip::tcp::endpoint& asio_endpoint_;
-};
-
-/// \brief The \c TCPSocket class is a concrete derived class of
-/// \c IOSocket that represents a TCP socket.
-///
-/// In the current implementation, an object of this class is always
-/// instantiated within the wrapper routines. Applications are expected to
-/// get access to the object via the abstract base class, \c IOSocket.
-/// This design may be changed when we generalize the wrapper interface.
-class TCPSocket : public IOSocket {
-private:
- TCPSocket(const TCPSocket& source);
- TCPSocket& operator=(const TCPSocket& source);
-public:
- /// \brief Constructor from an ASIO TCP socket.
- ///
- /// \param socket The ASIO representation of the TCP socket.
- TCPSocket(asio::ip::tcp::socket& socket) : socket_(socket) {}
-
- int getNative() const { return (socket_.native()); }
- int getProtocol() const { return (IPPROTO_TCP); }
-
-private:
- asio::ip::tcp::socket& socket_;
-};
-
-/// \brief A TCP-specific \c DNSServer object.
-///
-/// This class inherits from both \c DNSServer and from \c coroutine,
-/// defined in coroutine.h.
-class TCPServer : public virtual DNSServer, public virtual coroutine {
-public:
- explicit TCPServer(asio::io_service& io_service,
- const asio::ip::address& addr, const uint16_t port,
- const SimpleCallback* checkin = NULL,
- const DNSLookup* lookup = NULL,
- const DNSAnswer* answer = NULL);
-
- void operator()(asio::error_code ec = asio::error_code(),
- size_t length = 0);
- void asyncLookup();
- void resume(const bool done);
- bool hasAnswer() { return (done_); }
- int value() { return (get_value()); }
-
- DNSServer* clone() {
- TCPServer* s = new TCPServer(*this);
- return (s);
- }
-
-private:
- enum { MAX_LENGTH = 65535 };
- static const size_t TCP_MESSAGE_LENGTHSIZE = 2;
-
- // The ASIO service object
- asio::io_service& io_;
-
- // Class member variables which are dynamic, and changes to which
- // need to accessible from both sides of a coroutine fork or from
- // outside of the coroutine (i.e., from an asynchronous I/O call),
- // should be declared here as pointers and allocated in the
- // constructor or in the coroutine. This allows state information
- // to persist when an individual copy of the coroutine falls out
- // scope while waiting for an event, *so long as* there is another
- // object that is referencing the same data. As a side-benefit, using
- // pointers also reduces copy overhead for coroutine objects.
- //
- // Note: Currently these objects are allocated by "new" in the
- // constructor, or in the function operator while processing a query.
- // Repeated allocations from the heap for every incoming query is
- // clearly a performance issue; this must be optimized in the future.
- // The plan is to have a structure pre-allocate several "server state"
- // objects which can be pulled off a free list and placed on an in-use
- // list whenever a query comes in. This will serve the dual purpose
- // of improving performance and guaranteeing that state information
- // will *not* be destroyed when any one instance of the coroutine
- // falls out of scope while waiting for an event.
- //
- // An ASIO acceptor object to handle new connections. Created in
- // the constructor.
- boost::shared_ptr<asio::ip::tcp::acceptor> acceptor_;
-
- // Socket used to for listen for queries. Created in the
- // constructor and stored in a shared_ptr because socket objects
- // are not copyable.
- boost::shared_ptr<asio::ip::tcp::socket> socket_;
-
- // The buffer into which the response is written
- boost::shared_ptr<isc::dns::OutputBuffer> respbuf_;
-
- // \c IOMessage and \c Message objects to be passed to the
- // DNS lookup and answer providers
- boost::shared_ptr<asiolink::IOMessage> io_message_;
- isc::dns::MessagePtr query_message_;
- isc::dns::MessagePtr answer_message_;
-
- // The buffer into which the query packet is written
- boost::shared_array<char>data_;
-
- // State information that is entirely internal to a given instance
- // of the coroutine can be declared here.
- size_t bytes_;
- bool done_;
-
- // Callback functions provided by the caller
- const SimpleCallback* checkin_callback_;
- const DNSLookup* lookup_callback_;
- const DNSAnswer* answer_callback_;
-
- boost::shared_ptr<IOEndpoint> peer_;
- boost::shared_ptr<IOSocket> iosock_;
-};
-
-}
-
-#endif // __TCPDNS_H
-
-// Local Variables:
-// mode: c++
-// End:
diff --git a/src/lib/asiolink/internal/tests/Makefile.am b/src/lib/asiolink/internal/tests/Makefile.am
deleted file mode 100644
index 449cab7..0000000
--- a/src/lib/asiolink/internal/tests/Makefile.am
+++ /dev/null
@@ -1,37 +0,0 @@
-AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
-AM_CPPFLAGS += $(BOOST_INCLUDES)
-
-AM_CXXFLAGS = $(B10_CXXFLAGS)
-
-if USE_STATIC_LINK
-AM_LDFLAGS = -static
-endif
-
-CLEANFILES = *.gcno *.gcda
-
-TESTS =
-if HAVE_GTEST
-TESTS += run_unittests
-run_unittests_SOURCES = udpdns_unittest.cc
-run_unittests_SOURCES += run_unittests.cc
-run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
-run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
-run_unittests_LDADD = $(GTEST_LDADD)
-run_unittests_LDADD += $(top_builddir)/src/lib/asiolink/libasiolink.la
-run_unittests_LDADD += $(top_builddir)/src/lib/dns/libdns++.la
-run_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libexceptions.la
-run_unittests_LDADD += $(top_builddir)/src/lib/log/liblog.la
-# Note: the ordering matters: -Wno-... must follow -Wextra (defined in
-# B10_CXXFLAGS)
-run_unittests_CXXFLAGS = $(AM_CXXFLAGS)
-if USE_GXX
-run_unittests_CXXFLAGS += -Wno-unused-parameter
-endif
-if USE_CLANGPP
-# We need to disable -Werror for any test that uses internal definitions of
-# ASIO when using clang++
-run_unittests_CXXFLAGS += -Wno-error
-endif
-endif
-
-noinst_PROGRAMS = $(TESTS)
diff --git a/src/lib/asiolink/internal/tests/run_unittests.cc b/src/lib/asiolink/internal/tests/run_unittests.cc
deleted file mode 100644
index 5f1195d..0000000
--- a/src/lib/asiolink/internal/tests/run_unittests.cc
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <gtest/gtest.h>
-
-int
-main(int argc, char* argv[]) {
- ::testing::InitGoogleTest(&argc, argv);
- return (RUN_ALL_TESTS());
-}
diff --git a/src/lib/asiolink/internal/tests/udpdns_unittest.cc b/src/lib/asiolink/internal/tests/udpdns_unittest.cc
deleted file mode 100644
index 099071c..0000000
--- a/src/lib/asiolink/internal/tests/udpdns_unittest.cc
+++ /dev/null
@@ -1,145 +0,0 @@
-// Copyright (C) 2010 CZ.NIC
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <gtest/gtest.h>
-#include <asio.hpp>
-#include <boost/bind.hpp>
-#include <cstdlib>
-
-#include <dns/question.h>
-
-#include <asiolink/internal/udpdns.h>
-
-using namespace asio;
-using namespace isc::dns;
-using asio::ip::udp;
-
-namespace {
-
-const asio::ip::address TEST_HOST(asio::ip::address::from_string("127.0.0.1"));
-const uint16_t TEST_PORT(5301);
-// FIXME Shouldn't we send something that is real message?
-const char TEST_DATA[] = "TEST DATA";
-
-// Test fixture for the asiolink::UDPQuery.
-class UDPQueryTest : public ::testing::Test,
- public asiolink::UDPQuery::Callback
-{
- public:
- // Expected result of the callback
- asiolink::UDPQuery::Result expected_;
- // Did the callback run already?
- bool run_;
- // We use an io_service to run the query
- io_service service_;
- // Something to ask
- Question question_;
- // Buffer where the UDPQuery will store response
- OutputBufferPtr buffer_;
- // The query we are testing
- asiolink::UDPQuery query_;
-
- UDPQueryTest() :
- run_(false),
- question_(Name("example.net"), RRClass::IN(), RRType::A()),
- buffer_(new OutputBuffer(512)),
- query_(service_, question_, asiolink::IOAddress(TEST_HOST),
- TEST_PORT, buffer_, this, 100)
- { }
-
- // This is the callback's (), so it can be called.
- void operator()(asiolink::UDPQuery::Result result) {
- // We check the query returns the correct result
- EXPECT_EQ(expected_, result);
- // Check it is called only once
- EXPECT_FALSE(run_);
- // And mark the callback was called
- run_ = true;
- }
- // A response handler, pretending to be remote DNS server
- void respond(udp::endpoint* remote, udp::socket* socket) {
- // Some data came, just send something back.
- socket->send_to(asio::buffer(TEST_DATA, sizeof TEST_DATA),
- *remote);
- socket->close();
- }
-};
-
-/*
- * Test that when we run the query and stop it after it was run,
- * it returns "stopped" correctly.
- *
- * That is why stop() is posted to the service_ as well instead
- * of calling it.
- */
-TEST_F(UDPQueryTest, stop) {
- expected_ = asiolink::UDPQuery::STOPPED;
- // Post the query
- service_.post(query_);
- // Post query_.stop() (yes, the boost::bind thing is just
- // query_.stop()).
- service_.post(boost::bind(&asiolink::UDPQuery::stop, query_,
- asiolink::UDPQuery::STOPPED));
- // Run both of them
- service_.run();
- EXPECT_TRUE(run_);
-}
-
-/*
- * Test that when we queue the query to service_ and call stop()
- * before it gets executed, it acts sanely as well (eg. has the
- * same result as running stop() after - calls the callback).
- */
-TEST_F(UDPQueryTest, prematureStop) {
- expected_ = asiolink::UDPQuery::STOPPED;
- // Stop before it is started
- query_.stop();
- service_.post(query_);
- service_.run();
- EXPECT_TRUE(run_);
-}
-
-/*
- * Test that it will timeout when no answer will arrive.
- */
-TEST_F(UDPQueryTest, timeout) {
- expected_ = asiolink::UDPQuery::TIME_OUT;
- service_.post(query_);
- service_.run();
- EXPECT_TRUE(run_);
-}
-
-/*
- * Test that it will succeed when we fake an answer and
- * stores the same data we send.
- *
- * This is done through a real socket on loopback address.
- */
-TEST_F(UDPQueryTest, receive) {
- expected_ = asiolink::UDPQuery::SUCCESS;
- udp::socket socket(service_, udp::v4());
- socket.set_option(socket_base::reuse_address(true));
- socket.bind(udp::endpoint(TEST_HOST, TEST_PORT));
- char inbuff[512];
- udp::endpoint remote;
- socket.async_receive_from(asio::buffer(inbuff, 512), remote, boost::bind(
- &UDPQueryTest::respond, this, &remote, &socket));
- service_.post(query_);
- service_.run();
- EXPECT_TRUE(run_);
- ASSERT_EQ(sizeof TEST_DATA, buffer_->getLength());
- EXPECT_EQ(0, memcmp(TEST_DATA, buffer_->getData(), sizeof TEST_DATA));
-}
-
-}
diff --git a/src/lib/asiolink/internal/udpdns.h b/src/lib/asiolink/internal/udpdns.h
deleted file mode 100644
index f29d5c8..0000000
--- a/src/lib/asiolink/internal/udpdns.h
+++ /dev/null
@@ -1,302 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#ifndef __UDPDNS_H
-#define __UDPDNS_H 1
-
-#include <config.h>
-
-#include <asio.hpp>
-#include <boost/shared_array.hpp>
-#include <boost/shared_ptr.hpp>
-
-#include <dns/buffer.h>
-#include <dns/message.h>
-#include <dns/messagerenderer.h>
-
-#include <asiolink/asiolink.h>
-#include <asiolink/internal/coroutine.h>
-
-// This file contains UDP-specific implementations of generic classes
-// defined in asiolink.h. It is *not* intended to be part of the public
-// API.
-
-namespace asiolink {
-/// \brief The \c UDPEndpoint class is a concrete derived class of
-/// \c IOEndpoint that represents an endpoint of a UDP packet.
-///
-/// Other notes about \c TCPEndpoint applies to this class, too.
-class UDPEndpoint : public IOEndpoint {
-public:
- ///
- /// \name Constructors and Destructor.
- ///
- //@{
- /// \brief Constructor from a pair of address and port.
- ///
- /// \param address The IP address of the endpoint.
- /// \param port The UDP port number of the endpoint.
- UDPEndpoint(const IOAddress& address, const unsigned short port) :
- asio_endpoint_placeholder_(
- new asio::ip::udp::endpoint(asio::ip::address::from_string(address.toText()),
- port)),
- asio_endpoint_(*asio_endpoint_placeholder_)
- {}
-
- /// \brief Constructor from an ASIO UDP endpoint.
- ///
- /// This constructor is designed to be an efficient wrapper for the
- /// corresponding ASIO class, \c udp::endpoint.
- ///
- /// \param asio_endpoint The ASIO representation of the UDP endpoint.
- UDPEndpoint(const asio::ip::udp::endpoint& asio_endpoint) :
- asio_endpoint_placeholder_(NULL), asio_endpoint_(asio_endpoint)
- {}
-
- /// \brief The destructor.
- ~UDPEndpoint() { delete asio_endpoint_placeholder_; }
- //@}
-
- inline IOAddress getAddress() const {
- return (asio_endpoint_.address());
- }
-
- inline uint16_t getPort() const {
- return (asio_endpoint_.port());
- }
-
- inline short getProtocol() const {
- return (asio_endpoint_.protocol().protocol());
- }
-
- inline short getFamily() const {
- return (asio_endpoint_.protocol().family());
- }
-
- // This is not part of the exosed IOEndpoint API but allows
- // direct access to the ASIO implementation of the endpoint
- inline const asio::ip::udp::endpoint& getASIOEndpoint() const {
- return (asio_endpoint_);
- }
-
-private:
- const asio::ip::udp::endpoint* asio_endpoint_placeholder_;
- const asio::ip::udp::endpoint& asio_endpoint_;
-};
-
-/// \brief The \c UDPSocket class is a concrete derived class of
-/// \c IOSocket that represents a UDP socket.
-///
-/// Other notes about \c TCPSocket applies to this class, too.
-class UDPSocket : public IOSocket {
-private:
- UDPSocket(const UDPSocket& source);
- UDPSocket& operator=(const UDPSocket& source);
-public:
- /// \brief Constructor from an ASIO UDP socket.
- ///
- /// \param socket The ASIO representation of the UDP socket.
- UDPSocket(asio::ip::udp::socket& socket) : socket_(socket) {}
-
- virtual int getNative() const { return (socket_.native()); }
- virtual int getProtocol() const { return (IPPROTO_UDP); }
-
-private:
- asio::ip::udp::socket& socket_;
-};
-
-//
-// Asynchronous UDP server coroutine
-//
-///
-/// \brief This class implements the coroutine to handle UDP
-/// DNS query event. As such, it is both a \c DNSServer and
-/// a \c coroutine
-///
-class UDPServer : public virtual DNSServer, public virtual coroutine {
-public:
- /// \brief Constructor
- /// \param io_service the asio::io_service to work with
- /// \param addr the IP address to listen for queries on
- /// \param port the port to listen for queries on
- /// \param checkin the callbackprovider for non-DNS events
- /// \param lookup the callbackprovider for DNS lookup events
- /// \param answer the callbackprovider for DNS answer events
- explicit UDPServer(asio::io_service& io_service,
- const asio::ip::address& addr, const uint16_t port,
- SimpleCallback* checkin = NULL,
- DNSLookup* lookup = NULL,
- DNSAnswer* answer = NULL);
-
- /// \brief The function operator
- void operator()(asio::error_code ec = asio::error_code(),
- size_t length = 0);
-
- /// \brief Calls the lookup callback
- void asyncLookup();
-
- /// \brief Resume operation
- ///
- /// \param done Set this to true if the lookup action is done and
- /// we have an answer
- void resume(const bool done);
-
- /// \brief Check if we have an answer
- ///
- /// \return true if we have an answer
- bool hasAnswer() { return (done_); }
-
- /// \brief Returns the coroutine state value
- ///
- /// \return the coroutine state value
- int value() { return (get_value()); }
-
- /// \brief Clones the object
- ///
- /// \return a newly allocated copy of this object
- DNSServer* clone() {
- UDPServer* s = new UDPServer(*this);
- return (s);
- }
-
-private:
- enum { MAX_LENGTH = 4096 };
-
- // The ASIO service object
- asio::io_service& io_;
-
- // Class member variables which are dynamic, and changes to which
- // need to accessible from both sides of a coroutine fork or from
- // outside of the coroutine (i.e., from an asynchronous I/O call),
- // should be declared here as pointers and allocated in the
- // constructor or in the coroutine. This allows state information
- // to persist when an individual copy of the coroutine falls out
- // scope while waiting for an event, *so long as* there is another
- // object that is referencing the same data. As a side-benefit, using
- // pointers also reduces copy overhead for coroutine objects.
- //
- // Note: Currently these objects are allocated by "new" in the
- // constructor, or in the function operator while processing a query.
- // Repeated allocations from the heap for every incoming query is
- // clearly a performance issue; this must be optimized in the future.
- // The plan is to have a structure pre-allocate several "server state"
- // objects which can be pulled off a free list and placed on an in-use
- // list whenever a query comes in. This will serve the dual purpose
- // of improving performance and guaranteeing that state information
- // will *not* be destroyed when any one instance of the coroutine
- // falls out of scope while waiting for an event.
- //
- // Socket used to for listen for queries. Created in the
- // constructor and stored in a shared_ptr because socket objects
- // are not copyable.
- boost::shared_ptr<asio::ip::udp::socket> socket_;
-
- // The ASIO-enternal endpoint object representing the client
- boost::shared_ptr<asio::ip::udp::endpoint> sender_;
-
- // \c IOMessage and \c Message objects to be passed to the
- // DNS lookup and answer providers
- boost::shared_ptr<asiolink::IOMessage> io_message_;
-
- // The original query as sent by the client
- isc::dns::MessagePtr query_message_;
-
- // The response message we are building
- isc::dns::MessagePtr answer_message_;
-
- // The buffer into which the response is written
- isc::dns::OutputBufferPtr respbuf_;
-
- // The buffer into which the query packet is written
- boost::shared_array<char> data_;
-
- // State information that is entirely internal to a given instance
- // of the coroutine can be declared here.
- size_t bytes_;
- bool done_;
-
- // Callback functions provided by the caller
- const SimpleCallback* checkin_callback_;
- const DNSLookup* lookup_callback_;
- const DNSAnswer* answer_callback_;
-
- boost::shared_ptr<IOEndpoint> peer_;
- boost::shared_ptr<IOSocket> iosock_;
-};
-
-//
-// Asynchronous UDP coroutine for upstream queries
-//
-class UDPQuery : public coroutine {
-public:
- // TODO Maybe this should be more generic than just for UDPQuery?
- ///
- /// \brief Result of the query
- ///
- /// This is related only to contacting the remote server. If the answer
- ///indicates error, it is still counted as SUCCESS here, if it comes back.
- ///
- enum Result {
- SUCCESS,
- TIME_OUT,
- STOPPED
- };
- /// Abstract callback for the UDPQuery.
- class Callback {
- public:
- virtual ~Callback() {}
-
- /// This will be called when the UDPQuery is completed
- virtual void operator()(Result result) = 0;
- };
- ///
- /// \brief Constructor.
- ///
- /// It creates the query.
- /// @param callback will be called when we terminate. It is your task to
- /// delete it if allocated on heap.
- ///@param timeout in ms.
- ///
- explicit UDPQuery(asio::io_service& io_service,
- const isc::dns::Question& q,
- const IOAddress& addr, uint16_t port,
- isc::dns::OutputBufferPtr buffer,
- Callback* callback, int timeout = -1);
- void operator()(asio::error_code ec = asio::error_code(),
- size_t length = 0);
- /// Terminate the query.
- void stop(Result reason = STOPPED);
-private:
- enum { MAX_LENGTH = 4096 };
-
- ///
- /// \short Private data
- ///
- /// They are not private because of stability of the
- /// interface (this is private class anyway), but because this class
- /// will be copyed often (it is used as a coroutine and passed as callback
- /// to many async_*() functions) and we want keep the same data. Some of
- /// the data is not copyable too.
- ///
- struct PrivateData;
- boost::shared_ptr<PrivateData> data_;
-};
-}
-
-
-#endif // __UDPDNS_H
-
-// Local Variables:
-// mode: c++
-// End:
diff --git a/src/lib/asiolink/interval_timer.cc b/src/lib/asiolink/interval_timer.cc
new file mode 100644
index 0000000..6b0cd09
--- /dev/null
+++ b/src/lib/asiolink/interval_timer.cc
@@ -0,0 +1,136 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+// unistd is needed for asio.hpp with SunStudio
+#include <unistd.h>
+
+#include <asio.hpp>
+
+#include <exceptions/exceptions.h>
+
+#include <asiolink/interval_timer.h>
+#include <asiolink/io_service.h>
+
+#include <boost/bind.hpp>
+
+namespace asiolink {
+
+class IntervalTimerImpl {
+private:
+ // prohibit copy
+ IntervalTimerImpl(const IntervalTimerImpl& source);
+ IntervalTimerImpl& operator=(const IntervalTimerImpl& source);
+public:
+ IntervalTimerImpl(IOService& io_service);
+ ~IntervalTimerImpl();
+ void setup(const IntervalTimer::Callback& cbfunc, const long interval);
+ void callback(const asio::error_code& error);
+ void cancel() {
+ timer_.cancel();
+ interval_ = 0;
+ }
+ long getInterval() const { return (interval_); }
+private:
+ // a function to update timer_ when it expires
+ void update();
+ // a function to call back when timer_ expires
+ IntervalTimer::Callback cbfunc_;
+ // interval in milliseconds
+ long interval_;
+ // asio timer
+ asio::deadline_timer timer_;
+};
+
+IntervalTimerImpl::IntervalTimerImpl(IOService& io_service) :
+ interval_(0), timer_(io_service.get_io_service())
+{}
+
+IntervalTimerImpl::~IntervalTimerImpl()
+{}
+
+void
+IntervalTimerImpl::setup(const IntervalTimer::Callback& cbfunc,
+ const long interval)
+{
+ // Interval should not be less than or equal to 0.
+ if (interval <= 0) {
+ isc_throw(isc::BadValue, "Interval should not be less than or "
+ "equal to 0");
+ }
+ // Call back function should not be empty.
+ if (cbfunc.empty()) {
+ isc_throw(isc::InvalidParameter, "Callback function is empty");
+ }
+ cbfunc_ = cbfunc;
+ interval_ = interval;
+ // Set initial expire time.
+ // At this point the timer is not running yet and will not expire.
+ // After calling IOService::run(), the timer will expire.
+ update();
+ return;
+}
+
+void
+IntervalTimerImpl::update() {
+ if (interval_ == 0) {
+ // timer has been canceled. Do nothing.
+ return;
+ }
+ try {
+ // Update expire time to (current time + interval_).
+ timer_.expires_from_now(boost::posix_time::millisec(interval_));
+ } catch (const asio::system_error& e) {
+ isc_throw(isc::Unexpected, "Failed to update timer");
+ }
+ // Reset timer.
+ timer_.async_wait(boost::bind(&IntervalTimerImpl::callback, this, _1));
+}
+
+void
+IntervalTimerImpl::callback(const asio::error_code& cancelled) {
+ // Do not call cbfunc_ in case the timer was cancelled.
+ // The timer will be canelled in the destructor of asio::deadline_timer.
+ if (!cancelled) {
+ cbfunc_();
+ // Set next expire time.
+ update();
+ }
+}
+
+IntervalTimer::IntervalTimer(IOService& io_service) {
+ impl_ = new IntervalTimerImpl(io_service);
+}
+
+IntervalTimer::~IntervalTimer() {
+ delete impl_;
+}
+
+void
+IntervalTimer::setup(const Callback& cbfunc, const long interval) {
+ return (impl_->setup(cbfunc, interval));
+}
+
+void
+IntervalTimer::cancel() {
+ impl_->cancel();
+}
+
+long
+IntervalTimer::getInterval() const {
+ return (impl_->getInterval());
+}
+
+}
diff --git a/src/lib/asiolink/interval_timer.h b/src/lib/asiolink/interval_timer.h
new file mode 100644
index 0000000..d805cd7
--- /dev/null
+++ b/src/lib/asiolink/interval_timer.h
@@ -0,0 +1,133 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __ASIOLINK_INTERVAL_TIMER_H
+#define __ASIOLINK_INTERVAL_TIMER_H 1
+
+#include <boost/function.hpp>
+
+#include <asiolink/io_service.h>
+
+namespace asiolink {
+
+struct IntervalTimerImpl;
+
+/// \brief The \c IntervalTimer class is a wrapper for the ASIO
+/// \c asio::deadline_timer class.
+///
+/// This class is implemented to use \c asio::deadline_timer as interval
+/// timer.
+///
+/// \c setup() sets a timer to expire on (now + interval) and a call back
+/// function.
+///
+/// \c IntervalTimerImpl::callback() is called by the timer when it expires.
+///
+/// The function calls the call back function set by \c setup() and updates
+/// the timer to expire in (now + interval) milliseconds.
+/// The type of call back function is \c void(void).
+///
+/// The call back function will not be called if the instance of this class is
+/// destroyed before the timer is expired.
+///
+/// Note: Destruction of an instance of this class while call back is pending
+/// causes throwing an exception from \c IOService.
+///
+/// Sample code:
+/// \code
+/// void function_to_call_back() {
+/// // this function will be called periodically
+/// }
+/// int interval_in_milliseconds = 1000;
+/// IOService io_service;
+///
+/// IntervalTimer intervalTimer(io_service);
+/// intervalTimer.setup(function_to_call_back, interval_in_milliseconds);
+/// io_service.run();
+/// \endcode
+class IntervalTimer {
+public:
+ /// \name The type of timer callback function
+ typedef boost::function<void()> Callback;
+
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non-copyable.
+ //@{
+private:
+ IntervalTimer(const IntervalTimer& source);
+ IntervalTimer& operator=(const IntervalTimer& source);
+public:
+ /// \brief The constructor with \c IOService.
+ ///
+ /// This constructor may throw a standard exception if
+ /// memory allocation fails inside the method.
+ /// This constructor may also throw \c asio::system_error.
+ ///
+ /// \param io_service A reference to an instance of IOService
+ IntervalTimer(IOService& io_service);
+
+ /// \brief The destructor.
+ ///
+ /// This destructor never throws an exception.
+ ///
+ /// On the destruction of this class the timer will be canceled
+ /// inside \c asio::deadline_timer.
+ ~IntervalTimer();
+ //@}
+
+ /// \brief Register timer callback function and interval.
+ ///
+ /// This function sets callback function and interval in milliseconds.
+ /// Timer will actually start after calling \c IOService::run().
+ ///
+ /// \param cbfunc A reference to a function \c void(void) to call back
+ /// when the timer is expired (should not be an empty functor)
+ /// \param interval Interval in milliseconds (greater than 0)
+ ///
+ /// Note: IntervalTimer will not pass \c asio::error_code to
+ /// call back function. In case the timer is cancelled, the function
+ /// will not be called.
+ ///
+ /// \throw isc::InvalidParameter cbfunc is empty
+ /// \throw isc::BadValue interval is less than or equal to 0
+ /// \throw isc::Unexpected ASIO library error
+ void setup(const Callback& cbfunc, const long interval);
+
+ /// Cancel the timer.
+ ///
+ /// If the timer has been set up, this method cancels any asynchronous
+ /// events waiting on the timer and stops the timer itself.
+ /// If the timer has already been canceled, this method effectively does
+ /// nothing.
+ ///
+ /// This method never throws an exception.
+ void cancel();
+
+ /// Return the timer interval.
+ ///
+ /// This method returns the timer interval in milliseconds if it's running;
+ /// if the timer has been canceled it returns 0.
+ ///
+ /// This method never throws an exception.
+ long getInterval() const;
+
+private:
+ IntervalTimerImpl* impl_;
+};
+
+} // namespace asiolink
+#endif // __ASIOLINK_INTERVAL_TIMER_H
diff --git a/src/lib/asiolink/io_address.cc b/src/lib/asiolink/io_address.cc
new file mode 100644
index 0000000..990524a
--- /dev/null
+++ b/src/lib/asiolink/io_address.cc
@@ -0,0 +1,62 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+#include <unistd.h> // for some IPC/network system calls
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <asio.hpp>
+
+#include <asiolink/asiolink.h>
+
+using namespace asio;
+using asio::ip::udp;
+using asio::ip::tcp;
+
+using namespace std;
+
+namespace asiolink {
+
+// XXX: we cannot simply construct the address in the initialization list,
+// because we'd like to throw our own exception on failure.
+IOAddress::IOAddress(const string& address_str) {
+ error_code err;
+ asio_address_ = ip::address::from_string(address_str, err);
+ if (err) {
+ isc_throw(IOError, "Failed to convert string to address '"
+ << address_str << "': " << err.message());
+ }
+}
+
+IOAddress::IOAddress(const ip::address& asio_address) :
+ asio_address_(asio_address)
+{}
+
+string
+IOAddress::toText() const {
+ return (asio_address_.to_string());
+}
+
+short
+IOAddress::getFamily() const {
+ if (asio_address_.is_v4()) {
+ return (AF_INET);
+ } else {
+ return (AF_INET6);
+ }
+}
+
+}
diff --git a/src/lib/asiolink/io_address.h b/src/lib/asiolink/io_address.h
new file mode 100644
index 0000000..98e6fe8
--- /dev/null
+++ b/src/lib/asiolink/io_address.h
@@ -0,0 +1,127 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __IOADDRESS_H
+#define __IOADDRESS_H 1
+
+// IMPORTANT NOTE: only very few ASIO headers files can be included in
+// this file. In particular, asio.hpp should never be included here.
+// See the description of the namespace below.
+#include <unistd.h> // for some network system calls
+#include <asio/ip/address.hpp>
+
+#include <functional>
+#include <string>
+
+#include <exceptions/exceptions.h>
+
+namespace asiolink {
+
+/// \brief The \c IOAddress class represents an IP addresses (version
+/// agnostic)
+///
+/// This class is a wrapper for the ASIO \c ip::address class.
+class IOAddress {
+public:
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// This class is copyable. We use default versions of copy constructor
+ /// and the assignment operator.
+ /// We use the default destructor.
+ //@{
+ /// \brief Constructor from string.
+ ///
+ /// This constructor converts a textual representation of IPv4 and IPv6
+ /// addresses into an IOAddress object.
+ /// If \c address_str is not a valid representation of any type of
+ /// address, an exception of class \c IOError will be thrown.
+ /// This constructor allocates memory for the object, and if that fails
+ /// a corresponding standard exception will be thrown.
+ ///
+ /// \param address_str Textual representation of address.
+ IOAddress(const std::string& address_str);
+
+ /// \brief Constructor from an ASIO \c ip::address object.
+ ///
+ /// This constructor is intended to be used within the wrapper
+ /// implementation; user applications of the wrapper API won't use it.
+ ///
+ /// This constructor never throws an exception.
+ ///
+ /// \param asio_address The ASIO \c ip::address to be converted.
+ IOAddress(const asio::ip::address& asio_adress);
+ //@}
+
+ /// \brief Convert the address to a string.
+ ///
+ /// This method is basically expected to be exception free, but
+ /// generating the string will involve resource allocation,
+ /// and if it fails the corresponding standard exception will be thrown.
+ ///
+ /// \return A string representation of the address.
+ std::string toText() const;
+
+ /// \brief Returns the address family
+ ///
+ /// \return AF_INET for IPv4 or AF_INET6 for IPv6.
+ short getFamily() const;
+
+ /// \brief Compare addresses for equality
+ ///
+ /// \param other Address to compare against.
+ ///
+ /// \return true if addresses are equal, false if not.
+ bool equals(const IOAddress& other) const {
+ return (asio_address_ == other.asio_address_);
+ }
+
+ /// \brief Compare addresses for equality
+ ///
+ /// \param other Address to compare against.
+ ///
+ /// \return true if addresses are equal, false if not.
+ bool operator==(const IOAddress& other) const {
+ return equals(other);
+ }
+
+ // \brief Compare addresses for inequality
+ ///
+ /// \param other Address to compare against.
+ ///
+ /// \return false if addresses are equal, true if not.
+ bool nequals(const IOAddress& other) const {
+ return (!equals(other));
+ }
+
+ // \brief Compare addresses for inequality
+ ///
+ /// \param other Address to compare against.
+ ///
+ /// \return false if addresses are equal, true if not.
+ bool operator!=(const IOAddress& other) const {
+ return (nequals(other));
+ }
+
+
+private:
+ asio::ip::address asio_address_;
+};
+
+} // asiolink
+#endif // __IOADDRESS_H
+
+// Local Variables:
+// mode: c++
+// End:
diff --git a/src/lib/asiolink/io_endpoint.cc b/src/lib/asiolink/io_endpoint.cc
new file mode 100644
index 0000000..86e0607
--- /dev/null
+++ b/src/lib/asiolink/io_endpoint.cc
@@ -0,0 +1,45 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+#include <unistd.h> // for some IPC/network system calls
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <asio.hpp>
+
+#include <asiolink/asiolink.h>
+#include <asiolink/tcp_endpoint.h>
+#include <asiolink/udp_endpoint.h>
+
+using namespace std;
+
+namespace asiolink {
+
+const IOEndpoint*
+IOEndpoint::create(const int protocol, const IOAddress& address,
+ const unsigned short port)
+{
+ if (protocol == IPPROTO_UDP) {
+ return (new UDPEndpoint(address, port));
+ } else if (protocol == IPPROTO_TCP) {
+ return (new TCPEndpoint(address, port));
+ }
+ isc_throw(IOError,
+ "IOEndpoint creation attempt for unsupported protocol: " <<
+ protocol);
+}
+
+}
diff --git a/src/lib/asiolink/io_endpoint.h b/src/lib/asiolink/io_endpoint.h
new file mode 100644
index 0000000..37f9087
--- /dev/null
+++ b/src/lib/asiolink/io_endpoint.h
@@ -0,0 +1,122 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __IOENDPOINT_H
+#define __IOENDPOINT_H 1
+
+// IMPORTANT NOTE: only very few ASIO headers files can be included in
+// this file. In particular, asio.hpp should never be included here.
+// See the description of the namespace below.
+#include <unistd.h> // for some network system calls
+
+#include <functional>
+#include <string>
+
+#include <exceptions/exceptions.h>
+#include <asiolink/io_address.h>
+
+namespace asiolink {
+
+/// \brief The \c IOEndpoint class is an abstract base class to represent
+/// a communication endpoint.
+///
+/// This class is a wrapper for the ASIO endpoint classes such as
+/// \c ip::tcp::endpoint and \c ip::udp::endpoint.
+///
+/// Derived class implementations are completely hidden within the
+/// implementation. User applications only get access to concrete
+/// \c IOEndpoint objects via the abstract interfaces.
+class IOEndpoint {
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non-copyable.
+ //@{
+private:
+ IOEndpoint(const IOEndpoint& source);
+ IOEndpoint& operator=(const IOEndpoint& source);
+protected:
+ /// \brief The default constructor.
+ ///
+ /// This is intentionally defined as \c protected as this base class
+ /// should never be instantiated (except as part of a derived class).
+ IOEndpoint() {}
+public:
+ /// The destructor.
+ virtual ~IOEndpoint() {}
+ //@}
+
+ /// \brief Returns the address of the endpoint.
+ ///
+ /// This method returns an IOAddress object corresponding to \c this
+ /// endpoint.
+ ///
+ /// Note that the return value is a real object, not a reference or
+ /// a pointer.
+ ///
+ /// This is aligned with the interface of the ASIO counterpart:
+ /// the \c address() method of \c ip::xxx::endpoint classes returns
+ /// an \c ip::address object.
+ ///
+ /// This also means handling the address of an endpoint using this method
+ /// can be expensive. If the address information is necessary in a
+ /// performance sensitive context and there's a more efficient interface
+ /// for that purpose, it's probably better to avoid using this method.
+ ///
+ /// This method never throws an exception.
+ ///
+ /// \return A copy of \c IOAddress object corresponding to the endpoint.
+ virtual IOAddress getAddress() const = 0;
+
+ /// \brief Returns the port of the endpoint.
+ virtual uint16_t getPort() const = 0;
+
+ /// \brief Returns the protocol number of the endpoint (TCP, UDP...)
+ virtual short getProtocol() const = 0;
+
+ /// \brief Returns the address family of the endpoint.
+ virtual short getFamily() const = 0;
+
+ /// \brief A polymorphic factory of endpoint from address and port.
+ ///
+ /// This method creates a new instance of (a derived class of)
+ /// \c IOEndpoint object that identifies the pair of given address
+ /// and port.
+ /// The appropriate derived class is chosen based on the specified
+ /// transport protocol. If the \c protocol doesn't specify a protocol
+ /// supported in this implementation, an exception of class \c IOError
+ /// will be thrown.
+ ///
+ /// Memory for the created object will be dynamically allocated. It's
+ /// the caller's responsibility to \c delete it later.
+ /// If resource allocation for the new object fails, a corresponding
+ /// standard exception will be thrown.
+ ///
+ /// \param protocol The transport protocol used for the endpoint.
+ /// Currently, only \c IPPROTO_UDP and \c IPPROTO_TCP can be specified.
+ /// \param address The (IP) address of the endpoint.
+ /// \param port The transport port number of the endpoint
+ /// \return A pointer to a newly created \c IOEndpoint object.
+ static const IOEndpoint* create(const int protocol,
+ const IOAddress& address,
+ const unsigned short port);
+};
+
+} // asiolink
+#endif // __IOENDPOINT_H
+
+// Local Variables:
+// mode: c++
+// End:
diff --git a/src/lib/asiolink/io_message.h b/src/lib/asiolink/io_message.h
new file mode 100644
index 0000000..7d7d56c
--- /dev/null
+++ b/src/lib/asiolink/io_message.h
@@ -0,0 +1,103 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __IOMESSAGE_H
+#define __IOMESSAGE_H 1
+
+// IMPORTANT NOTE: only very few ASIO headers files can be included in
+// this file. In particular, asio.hpp should never be included here.
+// See the description of the namespace below.
+#include <unistd.h> // for some network system calls
+
+#include <functional>
+#include <string>
+
+#include <exceptions/exceptions.h>
+
+#include <asiolink/io_endpoint.h>
+#include <asiolink/io_socket.h>
+
+namespace asiolink {
+
+/// \brief The \c IOMessage class encapsulates an incoming message received
+/// on a socket.
+///
+/// An \c IOMessage object represents a tuple of a chunk of data
+/// (a UDP packet or some segment of TCP stream), the socket over which the
+/// data is passed, the information about the other end point of the
+/// communication, and perhaps more.
+///
+/// The current design and interfaces of this class is tentative.
+/// It only provides a minimal level of support that is necessary for
+/// the current implementation of the authoritative server.
+/// A future version of this class will definitely support more.
+class IOMessage {
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non-copyable.
+ //@{
+private:
+ IOMessage(const IOMessage& source);
+ IOMessage& operator=(const IOMessage& source);
+public:
+ /// \brief Constructor from message data
+ ///
+ /// This constructor needs to handle the ASIO \c ip::address class,
+ /// and is intended to be used within this wrapper implementation.
+ /// Once the \c IOMessage object is created, the application can
+ /// get access to the information via the wrapper interface such as
+ /// \c getRemoteAddress().
+ ///
+ /// This constructor never throws an exception.
+ ///
+ /// \param data A pointer to the message data.
+ /// \param data_size The size of the message data in bytes.
+ /// \param io_socket The socket over which the data is given.
+ /// \param remote_endpoint The other endpoint of the socket, that is,
+ /// the sender of the message.
+ IOMessage(const void* data, const size_t data_size,
+ const IOSocket& io_socket, const IOEndpoint& remote_endpoint) :
+ data_(data), data_size_(data_size), io_socket_(io_socket),
+ remote_endpoint_(remote_endpoint)
+ {}
+ //@}
+
+ /// \brief Returns a pointer to the received data.
+ const void* getData() const { return (data_); }
+
+ /// \brief Returns the size of the received data in bytes.
+ size_t getDataSize() const { return (data_size_); }
+
+ /// \brief Returns the socket on which the message arrives.
+ const IOSocket& getSocket() const { return (io_socket_); }
+
+ /// \brief Returns the endpoint that sends the message.
+ const IOEndpoint& getRemoteEndpoint() const { return (remote_endpoint_); }
+
+private:
+ const void* data_;
+ const size_t data_size_;
+ const IOSocket& io_socket_;
+ const IOEndpoint& remote_endpoint_;
+};
+
+
+} // asiolink
+#endif // __IOMESSAGE_H
+
+// Local Variables:
+// mode: c++
+// End:
diff --git a/src/lib/asiolink/io_service.cc b/src/lib/asiolink/io_service.cc
new file mode 100644
index 0000000..8d96da9
--- /dev/null
+++ b/src/lib/asiolink/io_service.cc
@@ -0,0 +1,97 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+#include <config.h>
+
+// unistd is needed for asio.hpp with SunStudio
+#include <unistd.h>
+
+#include <asio.hpp>
+
+#include <asiolink/io_service.h>
+
+namespace asiolink {
+
+class IOServiceImpl {
+private:
+ IOServiceImpl(const IOService& source);
+ IOServiceImpl& operator=(const IOService& source);
+public:
+ /// \brief The constructor
+ IOServiceImpl() :
+ io_service_(),
+ work_(io_service_)
+ {};
+ /// \brief The destructor.
+ ~IOServiceImpl() {};
+ //@}
+
+ /// \brief Start the underlying event loop.
+ ///
+ /// This method does not return control to the caller until
+ /// the \c stop() method is called via some handler.
+ void run() { io_service_.run(); };
+
+ /// \brief Run the underlying event loop for a single event.
+ ///
+ /// This method return control to the caller as soon as the
+ /// first handler has completed. (If no handlers are ready when
+ /// it is run, it will block until one is.)
+ void run_one() { io_service_.run_one();} ;
+
+ /// \brief Stop the underlying event loop.
+ ///
+ /// This will return the control to the caller of the \c run() method.
+ void stop() { io_service_.stop();} ;
+
+ /// \brief Return the native \c io_service object used in this wrapper.
+ ///
+ /// This is a short term work around to support other BIND 10 modules
+ /// that share the same \c io_service with the authoritative server.
+ /// It will eventually be removed once the wrapper interface is
+ /// generalized.
+ asio::io_service& get_io_service() { return io_service_; };
+private:
+ asio::io_service io_service_;
+ asio::io_service::work work_;
+};
+
+IOService::IOService() {
+ io_impl_ = new IOServiceImpl();
+}
+
+IOService::~IOService() {
+ delete io_impl_;
+}
+
+void
+IOService::run() {
+ io_impl_->run();
+}
+
+void
+IOService::run_one() {
+ io_impl_->run_one();
+}
+
+void
+IOService::stop() {
+ io_impl_->stop();
+}
+
+asio::io_service&
+IOService::get_io_service() {
+ return (io_impl_->get_io_service());
+}
+
+} // namepsace asiolink
diff --git a/src/lib/asiolink/io_service.h b/src/lib/asiolink/io_service.h
new file mode 100644
index 0000000..66558b7
--- /dev/null
+++ b/src/lib/asiolink/io_service.h
@@ -0,0 +1,77 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __ASIOLINK_IO_SERVICE_H
+#define __ASIOLINK_IO_SERVICE_H 1
+
+namespace asio {
+ class io_service;
+}
+
+namespace asiolink {
+
+struct IOServiceImpl;
+
+/// \brief The \c IOService class is a wrapper for the ASIO \c io_service
+/// class.
+///
+class IOService {
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non-copyable.
+ //@{
+private:
+ IOService(const IOService& source);
+ IOService& operator=(const IOService& source);
+public:
+ /// \brief The constructor
+ IOService();
+ /// \brief The destructor.
+ ~IOService();
+ //@}
+
+ /// \brief Start the underlying event loop.
+ ///
+ /// This method does not return control to the caller until
+ /// the \c stop() method is called via some handler.
+ void run();
+
+ /// \brief Run the underlying event loop for a single event.
+ ///
+ /// This method return control to the caller as soon as the
+ /// first handler has completed. (If no handlers are ready when
+ /// it is run, it will block until one is.)
+ void run_one();
+
+ /// \brief Stop the underlying event loop.
+ ///
+ /// This will return the control to the caller of the \c run() method.
+ void stop();
+
+ /// \brief Return the native \c io_service object used in this wrapper.
+ ///
+ /// This is a short term work around to support other BIND 10 modules
+ /// that share the same \c io_service with the authoritative server.
+ /// It will eventually be removed once the wrapper interface is
+ /// generalized.
+ asio::io_service& get_io_service();
+
+private:
+ IOServiceImpl* io_impl_;
+};
+
+} // namespace asiolink
+#endif // __ASIOLINK_IO_SERVICE_H
diff --git a/src/lib/asiolink/io_socket.cc b/src/lib/asiolink/io_socket.cc
new file mode 100644
index 0000000..fb325e9
--- /dev/null
+++ b/src/lib/asiolink/io_socket.cc
@@ -0,0 +1,67 @@
+// Copyright (C) 2010 CZ NIC
+// Copyed from other version of auth/asiolink.cc which is:
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include "io_socket.h"
+
+#include <asio.hpp>
+
+using namespace asio;
+
+namespace asiolink {
+
+/// \brief The \c DummySocket class is a concrete derived class of
+/// \c IOSocket that is not associated with any real socket.
+///
+/// This main purpose of this class is tests, where it may be desirable to
+/// instantiate an \c IOSocket object without involving system resource
+/// allocation such as real network sockets.
+class DummySocket : public IOSocket {
+private:
+ DummySocket(const DummySocket& source);
+ DummySocket& operator=(const DummySocket& source);
+public:
+ /// \brief Constructor from the protocol number.
+ ///
+ /// The protocol must validly identify a standard network protocol.
+ /// For example, to specify TCP \c protocol must be \c IPPROTO_TCP.
+ ///
+ /// \param protocol The network protocol number for the socket.
+ DummySocket(const int protocol) : protocol_(protocol) {}
+
+ /// \brief A dummy derived method of \c IOSocket::getNative().
+ ///
+ /// This version of method always returns -1 as the object is not
+ /// associated with a real (native) socket.
+ virtual int getNative() const { return (-1); }
+
+ virtual int getProtocol() const { return (protocol_); }
+private:
+ const int protocol_;
+};
+
+IOSocket&
+IOSocket::getDummyUDPSocket() {
+ static DummySocket socket(IPPROTO_UDP);
+ return (socket);
+}
+
+IOSocket&
+IOSocket::getDummyTCPSocket() {
+ static DummySocket socket(IPPROTO_TCP);
+ return (socket);
+}
+
+}
diff --git a/src/lib/asiolink/io_socket.h b/src/lib/asiolink/io_socket.h
new file mode 100644
index 0000000..df37d71
--- /dev/null
+++ b/src/lib/asiolink/io_socket.h
@@ -0,0 +1,127 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __IOSOCKET_H
+#define __IOSOCKET_H 1
+
+// IMPORTANT NOTE: only very few ASIO headers files can be included in
+// this file. In particular, asio.hpp should never be included here.
+// See the description of the namespace below.
+#include <unistd.h> // for some network system calls
+
+#include <functional>
+#include <string>
+
+#include <exceptions/exceptions.h>
+
+namespace asiolink {
+
+/// \brief The \c IOSocket class is an abstract base class to represent
+/// various types of network sockets.
+///
+/// This class is a wrapper for the ASIO socket classes such as
+/// \c ip::tcp::socket and \c ip::udp::socket.
+///
+/// Derived class implementations are completely hidden within the
+/// implementation. User applications only get access to concrete
+/// \c IOSocket objects via the abstract interfaces.
+///
+/// We may revisit this decision when we generalize the wrapper and more
+/// modules use it. Also, at that point we may define a separate (visible)
+/// derived class for testing purposes rather than providing factory methods
+/// (i.e., getDummy variants below).
+class IOSocket {
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non-copyable.
+ //@{
+private:
+ IOSocket(const IOSocket& source);
+ IOSocket& operator=(const IOSocket& source);
+protected:
+ /// \brief The default constructor.
+ ///
+ /// This is intentionally defined as \c protected as this base class
+ /// should never be instantiated (except as part of a derived class).
+ IOSocket() {}
+public:
+ /// The destructor.
+ virtual ~IOSocket() {}
+ //@}
+
+ /// \brief Return the "native" representation of the socket.
+ ///
+ /// In practice, this is the file descriptor of the socket for
+ /// UNIX-like systems so the current implementation simply uses
+ /// \c int as the type of the return value.
+ /// We may have to need revisit this decision later.
+ ///
+ /// In general, the application should avoid using this method;
+ /// it essentially discloses an implementation specific "handle" that
+ /// can change the internal state of the socket (consider the
+ /// application closes it, for example).
+ /// But we sometimes need to perform very low-level operations that
+ /// requires the native representation. Passing the file descriptor
+ /// to a different process is one example.
+ /// This method is provided as a necessary evil for such limited purposes.
+ ///
+ /// This method never throws an exception.
+ ///
+ /// \return The native representation of the socket. This is the socket
+ /// file descriptor for UNIX-like systems.
+ virtual int getNative() const = 0;
+
+ /// \brief Return the transport protocol of the socket.
+ ///
+ /// Currently, it returns \c IPPROTO_UDP for UDP sockets, and
+ /// \c IPPROTO_TCP for TCP sockets.
+ ///
+ /// This method never throws an exception.
+ ///
+ /// \return IPPROTO_UDP for UDP sockets
+ /// \return IPPROTO_TCP for TCP sockets
+ virtual int getProtocol() const = 0;
+
+ /// \brief Return a non-usable "dummy" UDP socket for testing.
+ ///
+ /// This is a class method that returns a "mock" of UDP socket.
+ /// This is not associated with any actual socket, and its only
+ /// responsibility is to return \c IPPROTO_UDP from \c getProtocol().
+ /// The only feasible usage of this socket is for testing so that
+ /// the test code can prepare some "UDP data" even without opening any
+ /// actual socket.
+ ///
+ /// This method never throws an exception.
+ ///
+ /// \return A reference to an \c IOSocket object whose \c getProtocol()
+ /// returns \c IPPROTO_UDP.
+ static IOSocket& getDummyUDPSocket();
+
+ /// \brief Return a non-usable "dummy" TCP socket for testing.
+ ///
+ /// See \c getDummyUDPSocket(). This method is its TCP version.
+ ///
+ /// \return A reference to an \c IOSocket object whose \c getProtocol()
+ /// returns \c IPPROTO_TCP.
+ static IOSocket& getDummyTCPSocket();
+};
+
+} // asiolink
+#endif // __IOSOCKET_H
+
+// Local Variables:
+// mode: c++
+// End:
diff --git a/src/lib/asiolink/ioaddress.cc b/src/lib/asiolink/ioaddress.cc
deleted file mode 100644
index 990524a..0000000
--- a/src/lib/asiolink/ioaddress.cc
+++ /dev/null
@@ -1,62 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <config.h>
-
-#include <unistd.h> // for some IPC/network system calls
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-#include <asio.hpp>
-
-#include <asiolink/asiolink.h>
-
-using namespace asio;
-using asio::ip::udp;
-using asio::ip::tcp;
-
-using namespace std;
-
-namespace asiolink {
-
-// XXX: we cannot simply construct the address in the initialization list,
-// because we'd like to throw our own exception on failure.
-IOAddress::IOAddress(const string& address_str) {
- error_code err;
- asio_address_ = ip::address::from_string(address_str, err);
- if (err) {
- isc_throw(IOError, "Failed to convert string to address '"
- << address_str << "': " << err.message());
- }
-}
-
-IOAddress::IOAddress(const ip::address& asio_address) :
- asio_address_(asio_address)
-{}
-
-string
-IOAddress::toText() const {
- return (asio_address_.to_string());
-}
-
-short
-IOAddress::getFamily() const {
- if (asio_address_.is_v4()) {
- return (AF_INET);
- } else {
- return (AF_INET6);
- }
-}
-
-}
diff --git a/src/lib/asiolink/ioaddress.h b/src/lib/asiolink/ioaddress.h
deleted file mode 100644
index 5727041..0000000
--- a/src/lib/asiolink/ioaddress.h
+++ /dev/null
@@ -1,88 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#ifndef __IOADDRESS_H
-#define __IOADDRESS_H 1
-
-// IMPORTANT NOTE: only very few ASIO headers files can be included in
-// this file. In particular, asio.hpp should never be included here.
-// See the description of the namespace below.
-#include <unistd.h> // for some network system calls
-#include <asio/ip/address.hpp>
-
-#include <functional>
-#include <string>
-
-#include <exceptions/exceptions.h>
-
-namespace asiolink {
-
-/// \brief The \c IOAddress class represents an IP addresses (version
-/// agnostic)
-///
-/// This class is a wrapper for the ASIO \c ip::address class.
-class IOAddress {
-public:
- ///
- /// \name Constructors and Destructor
- ///
- /// This class is copyable. We use default versions of copy constructor
- /// and the assignment operator.
- /// We use the default destructor.
- //@{
- /// \brief Constructor from string.
- ///
- /// This constructor converts a textual representation of IPv4 and IPv6
- /// addresses into an IOAddress object.
- /// If \c address_str is not a valid representation of any type of
- /// address, an exception of class \c IOError will be thrown.
- /// This constructor allocates memory for the object, and if that fails
- /// a corresponding standard exception will be thrown.
- ///
- /// \param address_str Textual representation of address.
- IOAddress(const std::string& address_str);
-
- /// \brief Constructor from an ASIO \c ip::address object.
- ///
- /// This constructor is intended to be used within the wrapper
- /// implementation; user applications of the wrapper API won't use it.
- ///
- /// This constructor never throws an exception.
- ///
- /// \param asio_address The ASIO \c ip::address to be converted.
- IOAddress(const asio::ip::address& asio_adress);
- //@}
-
- /// \brief Convert the address to a string.
- ///
- /// This method is basically expected to be exception free, but
- /// generating the string will involve resource allocation,
- /// and if it fails the corresponding standard exception will be thrown.
- ///
- /// \return A string representation of the address.
- std::string toText() const;
-
- /// \brief Returns the address family.
- short getFamily() const;
-
-private:
- asio::ip::address asio_address_;
-};
-
-} // asiolink
-#endif // __IOADDRESS_H
-
-// Local Variables:
-// mode: c++
-// End:
diff --git a/src/lib/asiolink/ioendpoint.cc b/src/lib/asiolink/ioendpoint.cc
deleted file mode 100644
index 2807f8d..0000000
--- a/src/lib/asiolink/ioendpoint.cc
+++ /dev/null
@@ -1,43 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <config.h>
-
-#include <unistd.h> // for some IPC/network system calls
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-#include <asiolink/asiolink.h>
-#include <internal/tcpdns.h>
-#include <internal/udpdns.h>
-
-using namespace std;
-
-namespace asiolink {
-
-const IOEndpoint*
-IOEndpoint::create(const int protocol, const IOAddress& address,
- const unsigned short port)
-{
- if (protocol == IPPROTO_UDP) {
- return (new UDPEndpoint(address, port));
- } else if (protocol == IPPROTO_TCP) {
- return (new TCPEndpoint(address, port));
- }
- isc_throw(IOError,
- "IOEndpoint creation attempt for unsupported protocol: " <<
- protocol);
-}
-
-}
diff --git a/src/lib/asiolink/ioendpoint.h b/src/lib/asiolink/ioendpoint.h
deleted file mode 100644
index 926ce50..0000000
--- a/src/lib/asiolink/ioendpoint.h
+++ /dev/null
@@ -1,121 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#ifndef __IOENDPOINT_H
-#define __IOENDPOINT_H 1
-
-// IMPORTANT NOTE: only very few ASIO headers files can be included in
-// this file. In particular, asio.hpp should never be included here.
-// See the description of the namespace below.
-#include <unistd.h> // for some network system calls
-
-#include <functional>
-#include <string>
-
-#include <exceptions/exceptions.h>
-
-namespace asiolink {
-
-/// \brief The \c IOEndpoint class is an abstract base class to represent
-/// a communication endpoint.
-///
-/// This class is a wrapper for the ASIO endpoint classes such as
-/// \c ip::tcp::endpoint and \c ip::udp::endpoint.
-///
-/// Derived class implementations are completely hidden within the
-/// implementation. User applications only get access to concrete
-/// \c IOEndpoint objects via the abstract interfaces.
-class IOEndpoint {
- ///
- /// \name Constructors and Destructor
- ///
- /// Note: The copy constructor and the assignment operator are
- /// intentionally defined as private, making this class non-copyable.
- //@{
-private:
- IOEndpoint(const IOEndpoint& source);
- IOEndpoint& operator=(const IOEndpoint& source);
-protected:
- /// \brief The default constructor.
- ///
- /// This is intentionally defined as \c protected as this base class
- /// should never be instantiated (except as part of a derived class).
- IOEndpoint() {}
-public:
- /// The destructor.
- virtual ~IOEndpoint() {}
- //@}
-
- /// \brief Returns the address of the endpoint.
- ///
- /// This method returns an IOAddress object corresponding to \c this
- /// endpoint.
- ///
- /// Note that the return value is a real object, not a reference or
- /// a pointer.
- ///
- /// This is aligned with the interface of the ASIO counterpart:
- /// the \c address() method of \c ip::xxx::endpoint classes returns
- /// an \c ip::address object.
- ///
- /// This also means handling the address of an endpoint using this method
- /// can be expensive. If the address information is necessary in a
- /// performance sensitive context and there's a more efficient interface
- /// for that purpose, it's probably better to avoid using this method.
- ///
- /// This method never throws an exception.
- ///
- /// \return A copy of \c IOAddress object corresponding to the endpoint.
- virtual IOAddress getAddress() const = 0;
-
- /// \brief Returns the port of the endpoint.
- virtual uint16_t getPort() const = 0;
-
- /// \brief Returns the protocol number of the endpoint (TCP, UDP...)
- virtual short getProtocol() const = 0;
-
- /// \brief Returns the address family of the endpoint.
- virtual short getFamily() const = 0;
-
- /// \brief A polymorphic factory of endpoint from address and port.
- ///
- /// This method creates a new instance of (a derived class of)
- /// \c IOEndpoint object that identifies the pair of given address
- /// and port.
- /// The appropriate derived class is chosen based on the specified
- /// transport protocol. If the \c protocol doesn't specify a protocol
- /// supported in this implementation, an exception of class \c IOError
- /// will be thrown.
- ///
- /// Memory for the created object will be dynamically allocated. It's
- /// the caller's responsibility to \c delete it later.
- /// If resource allocation for the new object fails, a corresponding
- /// standard exception will be thrown.
- ///
- /// \param protocol The transport protocol used for the endpoint.
- /// Currently, only \c IPPROTO_UDP and \c IPPROTO_TCP can be specified.
- /// \param address The (IP) address of the endpoint.
- /// \param port The transport port number of the endpoint
- /// \return A pointer to a newly created \c IOEndpoint object.
- static const IOEndpoint* create(const int protocol,
- const IOAddress& address,
- const unsigned short port);
-};
-
-} // asiolink
-#endif // __IOENDPOINT_H
-
-// Local Variables:
-// mode: c++
-// End:
diff --git a/src/lib/asiolink/iomessage.h b/src/lib/asiolink/iomessage.h
deleted file mode 100644
index 27baa8a..0000000
--- a/src/lib/asiolink/iomessage.h
+++ /dev/null
@@ -1,103 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#ifndef __IOMESSAGE_H
-#define __IOMESSAGE_H 1
-
-// IMPORTANT NOTE: only very few ASIO headers files can be included in
-// this file. In particular, asio.hpp should never be included here.
-// See the description of the namespace below.
-#include <unistd.h> // for some network system calls
-
-#include <functional>
-#include <string>
-
-#include <exceptions/exceptions.h>
-
-#include <asiolink/ioendpoint.h>
-#include <asiolink/iosocket.h>
-
-namespace asiolink {
-
-/// \brief The \c IOMessage class encapsulates an incoming message received
-/// on a socket.
-///
-/// An \c IOMessage object represents a tuple of a chunk of data
-/// (a UDP packet or some segment of TCP stream), the socket over which the
-/// data is passed, the information about the other end point of the
-/// communication, and perhaps more.
-///
-/// The current design and interfaces of this class is tentative.
-/// It only provides a minimal level of support that is necessary for
-/// the current implementation of the authoritative server.
-/// A future version of this class will definitely support more.
-class IOMessage {
- ///
- /// \name Constructors and Destructor
- ///
- /// Note: The copy constructor and the assignment operator are
- /// intentionally defined as private, making this class non-copyable.
- //@{
-private:
- IOMessage(const IOMessage& source);
- IOMessage& operator=(const IOMessage& source);
-public:
- /// \brief Constructor from message data
- ///
- /// This constructor needs to handle the ASIO \c ip::address class,
- /// and is intended to be used within this wrapper implementation.
- /// Once the \c IOMessage object is created, the application can
- /// get access to the information via the wrapper interface such as
- /// \c getRemoteAddress().
- ///
- /// This constructor never throws an exception.
- ///
- /// \param data A pointer to the message data.
- /// \param data_size The size of the message data in bytes.
- /// \param io_socket The socket over which the data is given.
- /// \param remote_endpoint The other endpoint of the socket, that is,
- /// the sender of the message.
- IOMessage(const void* data, const size_t data_size,
- const IOSocket& io_socket, const IOEndpoint& remote_endpoint) :
- data_(data), data_size_(data_size), io_socket_(io_socket),
- remote_endpoint_(remote_endpoint)
- {}
- //@}
-
- /// \brief Returns a pointer to the received data.
- const void* getData() const { return (data_); }
-
- /// \brief Returns the size of the received data in bytes.
- size_t getDataSize() const { return (data_size_); }
-
- /// \brief Returns the socket on which the message arrives.
- const IOSocket& getSocket() const { return (io_socket_); }
-
- /// \brief Returns the endpoint that sends the message.
- const IOEndpoint& getRemoteEndpoint() const { return (remote_endpoint_); }
-
-private:
- const void* data_;
- const size_t data_size_;
- const IOSocket& io_socket_;
- const IOEndpoint& remote_endpoint_;
-};
-
-
-} // asiolink
-#endif // __IOMESSAGE_H
-
-// Local Variables:
-// mode: c++
-// End:
diff --git a/src/lib/asiolink/iosocket.cc b/src/lib/asiolink/iosocket.cc
deleted file mode 100644
index a3967d4..0000000
--- a/src/lib/asiolink/iosocket.cc
+++ /dev/null
@@ -1,67 +0,0 @@
-// Copyright (C) 2010 CZ NIC
-// Copyed from other version of auth/asiolink.cc which is:
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include "iosocket.h"
-
-#include <asio.hpp>
-
-using namespace asio;
-
-namespace asiolink {
-
-/// \brief The \c DummySocket class is a concrete derived class of
-/// \c IOSocket that is not associated with any real socket.
-///
-/// This main purpose of this class is tests, where it may be desirable to
-/// instantiate an \c IOSocket object without involving system resource
-/// allocation such as real network sockets.
-class DummySocket : public IOSocket {
-private:
- DummySocket(const DummySocket& source);
- DummySocket& operator=(const DummySocket& source);
-public:
- /// \brief Constructor from the protocol number.
- ///
- /// The protocol must validly identify a standard network protocol.
- /// For example, to specify TCP \c protocol must be \c IPPROTO_TCP.
- ///
- /// \param protocol The network protocol number for the socket.
- DummySocket(const int protocol) : protocol_(protocol) {}
-
- /// \brief A dummy derived method of \c IOSocket::getNative().
- ///
- /// This version of method always returns -1 as the object is not
- /// associated with a real (native) socket.
- virtual int getNative() const { return (-1); }
-
- virtual int getProtocol() const { return (protocol_); }
-private:
- const int protocol_;
-};
-
-IOSocket&
-IOSocket::getDummyUDPSocket() {
- static DummySocket socket(IPPROTO_UDP);
- return (socket);
-}
-
-IOSocket&
-IOSocket::getDummyTCPSocket() {
- static DummySocket socket(IPPROTO_TCP);
- return (socket);
-}
-
-}
diff --git a/src/lib/asiolink/iosocket.h b/src/lib/asiolink/iosocket.h
deleted file mode 100644
index df37d71..0000000
--- a/src/lib/asiolink/iosocket.h
+++ /dev/null
@@ -1,127 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#ifndef __IOSOCKET_H
-#define __IOSOCKET_H 1
-
-// IMPORTANT NOTE: only very few ASIO headers files can be included in
-// this file. In particular, asio.hpp should never be included here.
-// See the description of the namespace below.
-#include <unistd.h> // for some network system calls
-
-#include <functional>
-#include <string>
-
-#include <exceptions/exceptions.h>
-
-namespace asiolink {
-
-/// \brief The \c IOSocket class is an abstract base class to represent
-/// various types of network sockets.
-///
-/// This class is a wrapper for the ASIO socket classes such as
-/// \c ip::tcp::socket and \c ip::udp::socket.
-///
-/// Derived class implementations are completely hidden within the
-/// implementation. User applications only get access to concrete
-/// \c IOSocket objects via the abstract interfaces.
-///
-/// We may revisit this decision when we generalize the wrapper and more
-/// modules use it. Also, at that point we may define a separate (visible)
-/// derived class for testing purposes rather than providing factory methods
-/// (i.e., getDummy variants below).
-class IOSocket {
- ///
- /// \name Constructors and Destructor
- ///
- /// Note: The copy constructor and the assignment operator are
- /// intentionally defined as private, making this class non-copyable.
- //@{
-private:
- IOSocket(const IOSocket& source);
- IOSocket& operator=(const IOSocket& source);
-protected:
- /// \brief The default constructor.
- ///
- /// This is intentionally defined as \c protected as this base class
- /// should never be instantiated (except as part of a derived class).
- IOSocket() {}
-public:
- /// The destructor.
- virtual ~IOSocket() {}
- //@}
-
- /// \brief Return the "native" representation of the socket.
- ///
- /// In practice, this is the file descriptor of the socket for
- /// UNIX-like systems so the current implementation simply uses
- /// \c int as the type of the return value.
- /// We may have to need revisit this decision later.
- ///
- /// In general, the application should avoid using this method;
- /// it essentially discloses an implementation specific "handle" that
- /// can change the internal state of the socket (consider the
- /// application closes it, for example).
- /// But we sometimes need to perform very low-level operations that
- /// requires the native representation. Passing the file descriptor
- /// to a different process is one example.
- /// This method is provided as a necessary evil for such limited purposes.
- ///
- /// This method never throws an exception.
- ///
- /// \return The native representation of the socket. This is the socket
- /// file descriptor for UNIX-like systems.
- virtual int getNative() const = 0;
-
- /// \brief Return the transport protocol of the socket.
- ///
- /// Currently, it returns \c IPPROTO_UDP for UDP sockets, and
- /// \c IPPROTO_TCP for TCP sockets.
- ///
- /// This method never throws an exception.
- ///
- /// \return IPPROTO_UDP for UDP sockets
- /// \return IPPROTO_TCP for TCP sockets
- virtual int getProtocol() const = 0;
-
- /// \brief Return a non-usable "dummy" UDP socket for testing.
- ///
- /// This is a class method that returns a "mock" of UDP socket.
- /// This is not associated with any actual socket, and its only
- /// responsibility is to return \c IPPROTO_UDP from \c getProtocol().
- /// The only feasible usage of this socket is for testing so that
- /// the test code can prepare some "UDP data" even without opening any
- /// actual socket.
- ///
- /// This method never throws an exception.
- ///
- /// \return A reference to an \c IOSocket object whose \c getProtocol()
- /// returns \c IPPROTO_UDP.
- static IOSocket& getDummyUDPSocket();
-
- /// \brief Return a non-usable "dummy" TCP socket for testing.
- ///
- /// See \c getDummyUDPSocket(). This method is its TCP version.
- ///
- /// \return A reference to an \c IOSocket object whose \c getProtocol()
- /// returns \c IPPROTO_TCP.
- static IOSocket& getDummyTCPSocket();
-};
-
-} // asiolink
-#endif // __IOSOCKET_H
-
-// Local Variables:
-// mode: c++
-// End:
diff --git a/src/lib/asiolink/recursive_query.cc b/src/lib/asiolink/recursive_query.cc
new file mode 100644
index 0000000..9f814b0
--- /dev/null
+++ b/src/lib/asiolink/recursive_query.cc
@@ -0,0 +1,457 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+#include <stdlib.h>
+
+// unistd is needed for asio.hpp with SunStudio
+#include <unistd.h>
+
+#include <asio.hpp>
+
+#include <asiolink/recursive_query.h>
+#include <asiolink/dns_service.h>
+#include <asiolink/udp_query.h>
+
+#include <log/dummylog.h>
+
+#include <boost/lexical_cast.hpp>
+#include <boost/bind.hpp>
+
+#include <dns/question.h>
+#include <dns/message.h>
+
+#include <resolve/resolve.h>
+
+using isc::log::dlog;
+using namespace isc::dns;
+
+namespace asiolink {
+
+typedef std::vector<std::pair<std::string, uint16_t> > AddressVector;
+
+// Here we do not use the typedef above, as the SunStudio compiler
+// mishandles this in its name mangling, and wouldn't compile.
+// We can probably use a typedef, but need to move it to a central
+// location and use it consistently.
+RecursiveQuery::RecursiveQuery(DNSService& dns_service,
+ const std::vector<std::pair<std::string, uint16_t> >& upstream,
+ const std::vector<std::pair<std::string, uint16_t> >& upstream_root,
+ int query_timeout, int client_timeout, int lookup_timeout,
+ unsigned retries) :
+ dns_service_(dns_service), upstream_(new AddressVector(upstream)),
+ upstream_root_(new AddressVector(upstream_root)),
+ query_timeout_(query_timeout), client_timeout_(client_timeout),
+ lookup_timeout_(lookup_timeout), retries_(retries)
+{}
+
+namespace {
+
+typedef std::pair<std::string, uint16_t> addr_t;
+
+/*
+ * This is a query in progress. When a new query is made, this one holds
+ * the context information about it, like how many times we are allowed
+ * to retry on failure, what to do when we succeed, etc.
+ *
+ * Used by RecursiveQuery::sendQuery.
+ */
+class RunningQuery : public UDPQuery::Callback {
+private:
+ // The io service to handle async calls
+ asio::io_service& io_;
+
+ // Info for (re)sending the query (the question and destination)
+ Question question_;
+
+ // This is where we build and store our final answer
+ MessagePtr answer_message_;
+
+ // currently we use upstream as the current list of NS records
+ // we should differentiate between forwarding and resolving
+ boost::shared_ptr<AddressVector> upstream_;
+
+ // root servers...just copied over to the zone_servers_
+ boost::shared_ptr<AddressVector> upstream_root_;
+
+ // Buffer to store the result.
+ OutputBufferPtr buffer_;
+
+ // Server to notify when we succeed or fail
+ //shared_ptr<DNSServer> server_;
+ isc::resolve::ResolverInterface::CallbackPtr resolvercallback_;
+
+ // To prevent both unreasonably long cname chains and cname loops,
+ // we simply keep a counter of the number of CNAMEs we have
+ // followed so far (and error if it exceeds RESOLVER_MAX_CNAME_CHAIN
+ // from lib/resolve/response_classifier.h)
+ unsigned cname_count_;
+
+ /*
+ * TODO Do something more clever with timeouts. In the long term, some
+ * computation of average RTT, increase with each retry, etc.
+ */
+ // Timeout information
+ int query_timeout_;
+ unsigned retries_;
+
+ // normal query state
+
+ // Not using NSAS at this moment, so we keep a list
+ // of 'current' zone servers
+ std::vector<addr_t> zone_servers_;
+
+ // Update the question that will be sent to the server
+ void setQuestion(const Question& new_question) {
+ question_ = new_question;
+ }
+
+ // TODO: replace by our wrapper
+ asio::deadline_timer client_timer;
+ asio::deadline_timer lookup_timer;
+
+ size_t queries_out_;
+
+ // If we timed out ourselves (lookup timeout), stop issuing queries
+ bool done_;
+
+ // If we have a client timeout, we send back an answer, but don't
+ // stop. We use this variable to make sure we don't send another
+ // answer if we do find one later (or if we have a lookup_timeout)
+ bool answer_sent_;
+
+ // (re)send the query to the server.
+ void send() {
+ const int uc = upstream_->size();
+ const int zs = zone_servers_.size();
+ buffer_->clear();
+ if (uc > 0) {
+ int serverIndex = rand() % uc;
+ dlog("Sending upstream query (" + question_.toText() +
+ ") to " + upstream_->at(serverIndex).first);
+ UDPQuery query(io_, question_,
+ upstream_->at(serverIndex).first,
+ upstream_->at(serverIndex).second, buffer_, this,
+ query_timeout_);
+ ++queries_out_;
+ io_.post(query);
+ } else if (zs > 0) {
+ int serverIndex = rand() % zs;
+ dlog("Sending query to zone server (" + question_.toText() +
+ ") to " + zone_servers_.at(serverIndex).first);
+ UDPQuery query(io_, question_,
+ zone_servers_.at(serverIndex).first,
+ zone_servers_.at(serverIndex).second, buffer_, this,
+ query_timeout_);
+ ++queries_out_;
+ io_.post(query);
+ } else {
+ dlog("Error, no upstream servers to send to.");
+ }
+ }
+
+ // This function is called by operator() if there is an actual
+ // answer from a server and we are in recursive mode
+ // depending on the contents, we go on recursing or return
+ //
+ // Note that the footprint may change as this function may
+ // need to append data to the answer we are building later.
+ //
+ // returns true if we are done (either we have an answer or an
+ // error message)
+ // returns false if we are not done
+ bool handleRecursiveAnswer(const Message& incoming) {
+ dlog("Handle response");
+ // In case we get a CNAME, we store the target
+ // here (classify() will set it when it walks through
+ // the cname chain to verify it).
+ Name cname_target(question_.getName());
+
+ isc::resolve::ResponseClassifier::Category category =
+ isc::resolve::ResponseClassifier::classify(
+ question_, incoming, cname_target, cname_count_, true);
+
+ bool found_ns_address = false;
+
+ switch (category) {
+ case isc::resolve::ResponseClassifier::ANSWER:
+ case isc::resolve::ResponseClassifier::ANSWERCNAME:
+ // Done. copy and return.
+ isc::resolve::copyResponseMessage(incoming, answer_message_);
+ return true;
+ break;
+ case isc::resolve::ResponseClassifier::CNAME:
+ dlog("Response is CNAME!");
+ // (unfinished) CNAME. We set our question_ to the CNAME
+ // target, then start over at the beginning (for now, that
+ // is, we reset our 'current servers' to the root servers).
+ if (cname_count_ >= RESOLVER_MAX_CNAME_CHAIN) {
+ // just give up
+ dlog("CNAME chain too long");
+ isc::resolve::makeErrorMessage(answer_message_,
+ Rcode::SERVFAIL());
+ return true;
+ }
+
+ answer_message_->appendSection(Message::SECTION_ANSWER,
+ incoming);
+ setZoneServersToRoot();
+
+ question_ = Question(cname_target, question_.getClass(),
+ question_.getType());
+
+ dlog("Following CNAME chain to " + question_.toText());
+ send();
+ return false;
+ break;
+ case isc::resolve::ResponseClassifier::NXDOMAIN:
+ // NXDOMAIN, just copy and return.
+ isc::resolve::copyResponseMessage(incoming, answer_message_);
+ return true;
+ break;
+ case isc::resolve::ResponseClassifier::REFERRAL:
+ // Referral. For now we just take the first glue address
+ // we find and continue with that
+ zone_servers_.clear();
+
+ for (RRsetIterator rrsi = incoming.beginSection(Message::SECTION_ADDITIONAL);
+ rrsi != incoming.endSection(Message::SECTION_ADDITIONAL) && !found_ns_address;
+ rrsi++) {
+ ConstRRsetPtr rrs = *rrsi;
+ if (rrs->getType() == RRType::A()) {
+ // found address
+ RdataIteratorPtr rdi = rrs->getRdataIterator();
+ // just use the first for now
+ if (!rdi->isLast()) {
+ std::string addr_str = rdi->getCurrent().toText();
+ dlog("[XX] first address found: " + addr_str);
+ // now we have one address, simply
+ // resend that exact same query
+ // to that address and yield, when it
+ // returns, loop again.
+
+ // TODO should use NSAS
+ zone_servers_.push_back(addr_t(addr_str, 53));
+ found_ns_address = true;
+ }
+ }
+ }
+ if (found_ns_address) {
+ // next resolver round
+ send();
+ return false;
+ } else {
+ dlog("[XX] no ready-made addresses in additional. need nsas.");
+ // TODO this will result in answering with the delegation. oh well
+ isc::resolve::copyResponseMessage(incoming, answer_message_);
+ return true;
+ }
+ break;
+ case isc::resolve::ResponseClassifier::EMPTY:
+ case isc::resolve::ResponseClassifier::EXTRADATA:
+ case isc::resolve::ResponseClassifier::INVNAMCLASS:
+ case isc::resolve::ResponseClassifier::INVTYPE:
+ case isc::resolve::ResponseClassifier::MISMATQUEST:
+ case isc::resolve::ResponseClassifier::MULTICLASS:
+ case isc::resolve::ResponseClassifier::NOTONEQUEST:
+ case isc::resolve::ResponseClassifier::NOTRESPONSE:
+ case isc::resolve::ResponseClassifier::NOTSINGLE:
+ case isc::resolve::ResponseClassifier::OPCODE:
+ case isc::resolve::ResponseClassifier::RCODE:
+ case isc::resolve::ResponseClassifier::TRUNCATED:
+ // Should we try a different server rather than SERVFAIL?
+ isc::resolve::makeErrorMessage(answer_message_,
+ Rcode::SERVFAIL());
+ return true;
+ break;
+ }
+ // should not be reached. assert here?
+ dlog("[FATAL] unreachable code");
+ return true;
+ }
+
+public:
+ RunningQuery(asio::io_service& io,
+ const Question &question,
+ MessagePtr answer_message,
+ boost::shared_ptr<AddressVector> upstream,
+ boost::shared_ptr<AddressVector> upstream_root,
+ OutputBufferPtr buffer,
+ isc::resolve::ResolverInterface::CallbackPtr cb,
+ int query_timeout, int client_timeout, int lookup_timeout,
+ unsigned retries) :
+ io_(io),
+ question_(question),
+ answer_message_(answer_message),
+ upstream_(upstream),
+ upstream_root_(upstream_root),
+ buffer_(buffer),
+ resolvercallback_(cb),
+ cname_count_(0),
+ query_timeout_(query_timeout),
+ retries_(retries),
+ client_timer(io),
+ lookup_timer(io),
+ queries_out_(0),
+ done_(false),
+ answer_sent_(false)
+ {
+ // Setup the timer to stop trying (lookup_timeout)
+ if (lookup_timeout >= 0) {
+ lookup_timer.expires_from_now(
+ boost::posix_time::milliseconds(lookup_timeout));
+ lookup_timer.async_wait(boost::bind(&RunningQuery::stop, this, false));
+ }
+
+ // Setup the timer to send an answer (client_timeout)
+ if (client_timeout >= 0) {
+ client_timer.expires_from_now(
+ boost::posix_time::milliseconds(client_timeout));
+ client_timer.async_wait(boost::bind(&RunningQuery::clientTimeout, this));
+ }
+
+ // should use NSAS for root servers
+ // Adding root servers if not a forwarder
+ if (upstream_->empty()) {
+ setZoneServersToRoot();
+ }
+
+ send();
+ }
+
+ void setZoneServersToRoot() {
+ zone_servers_.clear();
+ if (upstream_root_->empty()) { //if no root ips given, use this
+ zone_servers_.push_back(addr_t("192.5.5.241", 53));
+ } else {
+ // copy the list
+ dlog("Size is " +
+ boost::lexical_cast<std::string>(upstream_root_->size()) +
+ "\n");
+ for(AddressVector::iterator it = upstream_root_->begin();
+ it < upstream_root_->end(); ++it) {
+ zone_servers_.push_back(addr_t(it->first,it->second));
+ dlog("Put " + zone_servers_.back().first + "into root list\n");
+ }
+ }
+ }
+ virtual void clientTimeout() {
+ // Return a SERVFAIL, but do not stop until
+ // we have an answer or timeout ourselves
+ isc::resolve::makeErrorMessage(answer_message_,
+ Rcode::SERVFAIL());
+ resolvercallback_->success(answer_message_);
+ answer_sent_ = true;
+ }
+
+ virtual void stop(bool resume) {
+ // if we cancel our timers, we will still get an event for
+ // that, so we cannot delete ourselves just yet (those events
+ // would be bound to a deleted object)
+ // cancel them one by one, both cancels should get us back
+ // here again.
+ // same goes if we have an outstanding query (can't delete
+ // until that one comes back to us)
+ done_ = true;
+ if (resume && !answer_sent_) {
+ resolvercallback_->success(answer_message_);
+ } else {
+ resolvercallback_->failure();
+ }
+ if (lookup_timer.cancel() != 0) {
+ return;
+ }
+ if (client_timer.cancel() != 0) {
+ return;
+ }
+ if (queries_out_ > 0) {
+ return;
+ }
+ delete this;
+ }
+
+ // This function is used as callback from DNSQuery.
+ virtual void operator()(UDPQuery::Result result) {
+ // XXX is this the place for TCP retry?
+ --queries_out_;
+ if (!done_ && result != UDPQuery::TIME_OUT) {
+ // we got an answer
+ Message incoming(Message::PARSE);
+ InputBuffer ibuf(buffer_->getData(), buffer_->getLength());
+ incoming.fromWire(ibuf);
+
+ if (upstream_->size() == 0 &&
+ incoming.getRcode() == Rcode::NOERROR()) {
+ done_ = handleRecursiveAnswer(incoming);
+ } else {
+ isc::resolve::copyResponseMessage(incoming, answer_message_);
+ done_ = true;
+ }
+
+ if (done_) {
+ stop(true);
+ }
+ } else if (!done_ && retries_--) {
+ // We timed out, but we have some retries, so send again
+ dlog("Timeout, resending query");
+ send();
+ } else {
+ // out of retries, give up for now
+ stop(false);
+ }
+ }
+};
+
+}
+
+void
+RecursiveQuery::resolve(const QuestionPtr& question,
+ const isc::resolve::ResolverInterface::CallbackPtr callback)
+{
+ asio::io_service& io = dns_service_.get_io_service();
+
+ MessagePtr answer_message(new Message(Message::RENDER));
+ OutputBufferPtr buffer(new OutputBuffer(0));
+
+ // It will delete itself when it is done
+ new RunningQuery(io, *question, answer_message, upstream_,
+ upstream_root_, buffer, callback, query_timeout_,
+ client_timeout_, lookup_timeout_, retries_);
+}
+
+void
+RecursiveQuery::resolve(const Question& question,
+ MessagePtr answer_message,
+ OutputBufferPtr buffer,
+ DNSServer* server)
+{
+ // XXX: eventually we will need to be able to determine whether
+ // the message should be sent via TCP or UDP, or sent initially via
+ // UDP and then fall back to TCP on failure, but for the moment
+ // we're only going to handle UDP.
+ asio::io_service& io = dns_service_.get_io_service();
+
+ isc::resolve::ResolverInterface::CallbackPtr crs(
+ new isc::resolve::ResolverCallbackServer(server));
+
+ // It will delete itself when it is done
+ new RunningQuery(io, question, answer_message, upstream_, upstream_root_,
+ buffer, crs, query_timeout_, client_timeout_,
+ lookup_timeout_, retries_);
+}
+
+
+
+} // namespace asiolink
diff --git a/src/lib/asiolink/recursive_query.h b/src/lib/asiolink/recursive_query.h
new file mode 100644
index 0000000..0660c09
--- /dev/null
+++ b/src/lib/asiolink/recursive_query.h
@@ -0,0 +1,113 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __ASIOLINK_RECURSIVE_QUERY_H
+#define __ASIOLINK_RECURSIVE_QUERY_H 1
+
+#include <asiolink/dns_service.h>
+#include <asiolink/dns_server.h>
+#include <dns/buffer.h>
+
+namespace asiolink {
+/// \brief The \c RecursiveQuery class provides a layer of abstraction around
+/// the ASIO code that carries out an upstream query.
+///
+/// This design is very preliminary; currently it is only capable of
+/// handling simple forward requests to a single resolver.
+class RecursiveQuery {
+ ///
+ /// \name Constructors
+ ///
+ //@{
+public:
+ /// \brief Constructor
+ ///
+ /// This is currently the only way to construct \c RecursiveQuery
+ /// object. If the addresses of the forward nameservers is specified,
+ /// and every upstream query will be sent to one random address, and
+ /// the result sent back directly. If not, it will do full resolving.
+ ///
+ /// \param dns_service The DNS Service to perform the recursive
+ /// query on.
+ /// \param upstream Addresses and ports of the upstream servers
+ /// to forward queries to.
+ /// \param upstream_root Addresses and ports of the root servers
+ /// to use when resolving.
+ /// \param query_timeout Timeout value for queries we sent, in ms
+ /// \param client_timeout Timeout value for when we send back an
+ /// error, in ms
+ /// \param lookup_timeout Timeout value for when we give up, in ms
+ /// \param retries how many times we try again (0 means just send and
+ /// and return if it returs).
+ RecursiveQuery(DNSService& dns_service,
+ const std::vector<std::pair<std::string, uint16_t> >&
+ upstream,
+ const std::vector<std::pair<std::string, uint16_t> >&
+ upstream_root,
+ int query_timeout = 2000,
+ int client_timeout = 4000,
+ int lookup_timeout = 30000,
+ unsigned retries = 3);
+ //@}
+
+ /// \brief Initiate resolving
+ ///
+ /// When sendQuery() is called, a (set of) message(s) is sent
+ /// asynchronously. If upstream servers are set, one is chosen
+ /// and the response (if any) from that server will be returned.
+ ///
+ /// If not upstream is set, a root server is chosen from the
+ /// root_servers, and the RunningQuery shall do a full resolve
+ /// (i.e. if the answer is a delegation, it will be followed, etc.)
+ /// until there is an answer or an error.
+ ///
+ /// When there is a response or an error and we give up, the given
+ /// CallbackPtr object shall be called (with either success() or
+ /// failure(). See ResolverInterface::Callback for more information.
+ ///
+ /// \param question The question being answered <qname/qclass/qtype>
+ /// \param callback Callback object. See
+ /// \c ResolverInterface::Callback for more information
+ void resolve(const isc::dns::QuestionPtr& question,
+ const isc::resolve::ResolverInterface::CallbackPtr callback);
+
+
+ /// \brief Initiates resolving for the given question.
+ ///
+ /// This actually calls the previous sendQuery() with a default
+ /// callback object, which calls resume() on the given DNSServer
+ /// object.
+ ///
+ /// \param question The question being answered <qname/qclass/qtype>
+ /// \param answer_message An output Message into which the final response will be copied
+ /// \param buffer An output buffer into which the intermediate responses will be copied
+ /// \param server A pointer to the \c DNSServer object handling the client
+ void resolve(const isc::dns::Question& question,
+ isc::dns::MessagePtr answer_message,
+ isc::dns::OutputBufferPtr buffer,
+ DNSServer* server);
+private:
+ DNSService& dns_service_;
+ boost::shared_ptr<std::vector<std::pair<std::string, uint16_t> > >
+ upstream_;
+ boost::shared_ptr<std::vector<std::pair<std::string, uint16_t> > >
+ upstream_root_;
+ int query_timeout_;
+ int client_timeout_;
+ int lookup_timeout_;
+ unsigned retries_;
+};
+
+} // namespace asiolink
+#endif // __ASIOLINK_RECURSIVE_QUERY_H
diff --git a/src/lib/asiolink/simple_callback.h b/src/lib/asiolink/simple_callback.h
new file mode 100644
index 0000000..ab5deaf
--- /dev/null
+++ b/src/lib/asiolink/simple_callback.h
@@ -0,0 +1,71 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __ASIOLINK_SIMPLE_CALLBACK_H
+#define __ASIOLINK_SIMPLE_CALLBACK_H 1
+
+#include <asiolink/io_message.h>
+
+namespace asiolink {
+
+/// \brief The \c SimpleCallback class is an abstract base class for a
+/// simple callback function with the signature:
+///
+/// void simpleCallback(const IOMessage& io_message) const;
+///
+/// Specific derived class implementations are hidden within the
+/// implementation. Instances of the derived classes can be called
+/// as functions via the operator() interface. Pointers to these
+/// instances can then be provided to the \c IOService class
+/// via its constructor.
+///
+/// The \c SimpleCallback is expected to be used for basic, generic
+/// tasks such as checking for configuration changes. It may also be
+/// used for testing purposes.
+class SimpleCallback {
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non-copyable.
+ //@{
+private:
+ SimpleCallback(const SimpleCallback& source);
+ SimpleCallback& operator=(const SimpleCallback& source);
+protected:
+ /// \brief The default constructor.
+ ///
+ /// This is intentionally defined as \c protected as this base class
+ /// should never be instantiated (except as part of a derived class).
+ SimpleCallback() : self_(this) {}
+public:
+ /// \brief The destructor
+ virtual ~SimpleCallback() {}
+ /// \brief The function operator
+ //@}
+ ///
+ /// This makes its call indirectly via the "self" pointer, ensuring
+ /// that the function ultimately invoked will be the one in the derived
+ /// class.
+ ///
+ /// \param io_message The event message to handle
+ virtual void operator()(const IOMessage& io_message) const {
+ (*self_)(io_message);
+ }
+private:
+ SimpleCallback* self_;
+};
+
+} // namespace asiolink
+#endif // __ASIOLINK_SIMPLE_CALLBACK_H
diff --git a/src/lib/asiolink/tcp_endpoint.h b/src/lib/asiolink/tcp_endpoint.h
new file mode 100644
index 0000000..8f6270f
--- /dev/null
+++ b/src/lib/asiolink/tcp_endpoint.h
@@ -0,0 +1,98 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __TCP_ENDPOINT_H
+#define __TCP_ENDPOINT_H 1
+
+#ifndef ASIO_HPP
+#error "asio.hpp must be included before including this, see asiolink.h as to why"
+#endif
+
+#include <asiolink/io_endpoint.h>
+
+namespace asiolink {
+
+/// \brief The \c TCPEndpoint class is a concrete derived class of
+/// \c IOEndpoint that represents an endpoint of a TCP connection.
+///
+/// In the current implementation, an object of this class is always
+/// instantiated within the wrapper routines. Applications are expected to
+/// get access to the object via the abstract base class, \c IOEndpoint.
+/// This design may be changed when we generalize the wrapper interface.
+///
+/// Note: this implementation is optimized for the case where this object
+/// is created from an ASIO endpoint object in a receiving code path
+/// by avoiding to make a copy of the base endpoint. For TCP it may not be
+/// a big deal, but when we receive UDP packets at a high rate, the copy
+/// overhead might be significant.
+class TCPEndpoint : public IOEndpoint {
+public:
+ ///
+ /// \name Constructors and Destructor
+ ///
+ //@{
+ /// \brief Constructor from a pair of address and port.
+ ///
+ /// \param address The IP address of the endpoint.
+ /// \param port The TCP port number of the endpoint.
+ TCPEndpoint(const IOAddress& address, const unsigned short port) :
+ asio_endpoint_placeholder_(
+ new asio::ip::tcp::endpoint(
+ asio::ip::address::from_string(address.toText()), port)),
+ asio_endpoint_(*asio_endpoint_placeholder_)
+ {}
+
+ /// \brief Constructor from an ASIO TCP endpoint.
+ ///
+ /// This constructor is designed to be an efficient wrapper for the
+ /// corresponding ASIO class, \c tcp::endpoint.
+ ///
+ /// \param asio_endpoint The ASIO representation of the TCP endpoint.
+ TCPEndpoint(const asio::ip::tcp::endpoint& asio_endpoint) :
+ asio_endpoint_placeholder_(NULL), asio_endpoint_(asio_endpoint)
+ {}
+
+ /// \brief The destructor.
+ ~TCPEndpoint() { delete asio_endpoint_placeholder_; }
+ //@}
+
+ IOAddress getAddress() const {
+ return (asio_endpoint_.address());
+ }
+
+ uint16_t getPort() const {
+ return (asio_endpoint_.port());
+ }
+
+ short getProtocol() const {
+ return (asio_endpoint_.protocol().protocol());
+ }
+
+ short getFamily() const {
+ return (asio_endpoint_.protocol().family());
+ }
+
+ // This is not part of the exosed IOEndpoint API but allows
+ // direct access to the ASIO implementation of the endpoint
+ const asio::ip::tcp::endpoint& getASIOEndpoint() const {
+ return (asio_endpoint_);
+ }
+
+private:
+ const asio::ip::tcp::endpoint* asio_endpoint_placeholder_;
+ const asio::ip::tcp::endpoint& asio_endpoint_;
+};
+
+} // namespace asiolink
+#endif // __TCP_ENDPOINT_H
diff --git a/src/lib/asiolink/tcp_server.cc b/src/lib/asiolink/tcp_server.cc
new file mode 100644
index 0000000..2bdfc60
--- /dev/null
+++ b/src/lib/asiolink/tcp_server.cc
@@ -0,0 +1,194 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+#include <boost/shared_array.hpp>
+
+// unistd is needed for asio.hpp with SunStudio
+#include <unistd.h>
+
+#include <asio.hpp>
+
+#include <log/dummylog.h>
+
+#include <asiolink/tcp_endpoint.h>
+#include <asiolink/tcp_socket.h>
+
+#include <asiolink/tcp_server.h>
+
+
+using namespace asio;
+using asio::ip::udp;
+using asio::ip::tcp;
+
+using namespace std;
+using namespace isc::dns;
+
+namespace asiolink {
+
+/// The following functions implement the \c TCPServer class.
+///
+/// The constructor
+TCPServer::TCPServer(io_service& io_service,
+ const ip::address& addr, const uint16_t port,
+ const SimpleCallback* checkin,
+ const DNSLookup* lookup,
+ const DNSAnswer* answer) :
+ io_(io_service), done_(false),
+ checkin_callback_(checkin), lookup_callback_(lookup),
+ answer_callback_(answer)
+{
+ tcp::endpoint endpoint(addr, port);
+ acceptor_.reset(new tcp::acceptor(io_service));
+ acceptor_->open(endpoint.protocol());
+ // Set v6-only (we use a separate instantiation for v4,
+ // otherwise asio will bind to both v4 and v6
+ if (addr.is_v6()) {
+ acceptor_->set_option(ip::v6_only(true));
+ }
+ acceptor_->set_option(tcp::acceptor::reuse_address(true));
+ acceptor_->bind(endpoint);
+ acceptor_->listen();
+}
+
+void
+TCPServer::operator()(error_code ec, size_t length) {
+ /// Because the coroutine reeentry block is implemented as
+ /// a switch statement, inline variable declarations are not
+ /// permitted. Certain variables used below can be declared here.
+ boost::array<const_buffer,2> bufs;
+ OutputBuffer lenbuf(TCP_MESSAGE_LENGTHSIZE);
+
+ CORO_REENTER (this) {
+ do {
+ /// Create a socket to listen for connections
+ socket_.reset(new tcp::socket(acceptor_->get_io_service()));
+
+ /// Wait for new connections. In the event of error,
+ /// try again
+ do {
+ CORO_YIELD acceptor_->async_accept(*socket_, *this);
+ } while (!ec);
+
+ /// Fork the coroutine by creating a copy of this one and
+ /// scheduling it on the ASIO service queue. The parent
+ /// will continue listening for DNS connections while the
+ /// handles the one that has just arrived.
+ CORO_FORK io_.post(TCPServer(*this));
+ } while (is_parent());
+
+ /// Instantiate the data buffer that will be used by the
+ /// asynchronous read call.
+ data_.reset(new char[MAX_LENGTH]);
+
+ /// Read the message, in two parts. First, the message length:
+ CORO_YIELD async_read(*socket_, asio::buffer(data_.get(),
+ TCP_MESSAGE_LENGTHSIZE), *this);
+ if (ec) {
+ CORO_YIELD return;
+ }
+
+ /// Now read the message itself. (This is done in a different scope
+ /// to allow inline variable declarations.)
+ CORO_YIELD {
+ InputBuffer dnsbuffer((const void *) data_.get(), length);
+ uint16_t msglen = dnsbuffer.readUint16();
+ async_read(*socket_, asio::buffer(data_.get(), msglen), *this);
+ }
+
+ if (ec) {
+ CORO_YIELD return;
+ }
+
+ // Create an \c IOMessage object to store the query.
+ //
+ // (XXX: It would be good to write a factory function
+ // that would quickly generate an IOMessage object without
+ // all these calls to "new".)
+ peer_.reset(new TCPEndpoint(socket_->remote_endpoint()));
+ iosock_.reset(new TCPSocket(*socket_));
+ io_message_.reset(new IOMessage(data_.get(), length, *iosock_, *peer_));
+ bytes_ = length;
+
+ // Perform any necessary operations prior to processing the incoming
+ // packet (e.g., checking for queued configuration messages).
+ //
+ // (XXX: it may be a performance issue to have this called for
+ // every single incoming packet; we may wish to throttle it somehow
+ // in the future.)
+ if (checkin_callback_ != NULL) {
+ (*checkin_callback_)(*io_message_);
+ }
+
+ // If we don't have a DNS Lookup provider, there's no point in
+ // continuing; we exit the coroutine permanently.
+ if (lookup_callback_ == NULL) {
+ CORO_YIELD return;
+ }
+
+ // Reset or instantiate objects that will be needed by the
+ // DNS lookup and the write call.
+ respbuf_.reset(new OutputBuffer(0));
+ query_message_.reset(new Message(Message::PARSE));
+ answer_message_.reset(new Message(Message::RENDER));
+
+ // Schedule a DNS lookup, and yield. When the lookup is
+ // finished, the coroutine will resume immediately after
+ // this point.
+ CORO_YIELD io_.post(AsyncLookup<TCPServer>(*this));
+
+ // The 'done_' flag indicates whether we have an answer
+ // to send back. If not, exit the coroutine permanently.
+ if (!done_) {
+ CORO_YIELD return;
+ }
+
+ // Call the DNS answer provider to render the answer into
+ // wire format
+ (*answer_callback_)(*io_message_, query_message_,
+ answer_message_, respbuf_);
+
+ // Set up the response, beginning with two length bytes.
+ lenbuf.writeUint16(respbuf_->getLength());
+ bufs[0] = buffer(lenbuf.getData(), lenbuf.getLength());
+ bufs[1] = buffer(respbuf_->getData(), respbuf_->getLength());
+
+ // Begin an asynchronous send, and then yield. When the
+ // send completes, we will resume immediately after this point
+ // (though we have nothing further to do, so the coroutine
+ // will simply exit at that time).
+ CORO_YIELD async_write(*socket_, bufs, *this);
+ }
+}
+
+/// Call the DNS lookup provider. (Expected to be called by the
+/// AsyncLookup<TCPServer> handler.)
+void
+TCPServer::asyncLookup() {
+ (*lookup_callback_)(*io_message_, query_message_,
+ answer_message_, respbuf_, this);
+}
+
+/// Post this coroutine on the ASIO service queue so that it will
+/// resume processing where it left off. The 'done' parameter indicates
+/// whether there is an answer to return to the client.
+void
+TCPServer::resume(const bool done) {
+ done_ = done;
+ io_.post(*this);
+}
+
+} // namespace asiolink
+
diff --git a/src/lib/asiolink/tcp_server.h b/src/lib/asiolink/tcp_server.h
new file mode 100644
index 0000000..9b985ce
--- /dev/null
+++ b/src/lib/asiolink/tcp_server.h
@@ -0,0 +1,119 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __TCP_SERVER_H
+#define __TCP_SERVER_H 1
+
+#ifndef ASIO_HPP
+#error "asio.hpp must be included before including this, see asiolink.h as to why"
+#endif
+
+#include <boost/shared_array.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include <asiolink/asiolink.h>
+#include <coroutine.h>
+
+
+namespace asiolink {
+
+/// \brief A TCP-specific \c DNSServer object.
+///
+/// This class inherits from both \c DNSServer and from \c coroutine,
+/// defined in coroutine.h.
+class TCPServer : public virtual DNSServer, public virtual coroutine {
+public:
+ explicit TCPServer(asio::io_service& io_service,
+ const asio::ip::address& addr, const uint16_t port,
+ const SimpleCallback* checkin = NULL,
+ const DNSLookup* lookup = NULL,
+ const DNSAnswer* answer = NULL);
+
+ void operator()(asio::error_code ec = asio::error_code(),
+ size_t length = 0);
+ void asyncLookup();
+ void resume(const bool done);
+ bool hasAnswer() { return (done_); }
+ int value() { return (get_value()); }
+
+ DNSServer* clone() {
+ TCPServer* s = new TCPServer(*this);
+ return (s);
+ }
+
+private:
+ enum { MAX_LENGTH = 65535 };
+ static const size_t TCP_MESSAGE_LENGTHSIZE = 2;
+
+ // The ASIO service object
+ asio::io_service& io_;
+
+ // Class member variables which are dynamic, and changes to which
+ // need to accessible from both sides of a coroutine fork or from
+ // outside of the coroutine (i.e., from an asynchronous I/O call),
+ // should be declared here as pointers and allocated in the
+ // constructor or in the coroutine. This allows state information
+ // to persist when an individual copy of the coroutine falls out
+ // scope while waiting for an event, *so long as* there is another
+ // object that is referencing the same data. As a side-benefit, using
+ // pointers also reduces copy overhead for coroutine objects.
+ //
+ // Note: Currently these objects are allocated by "new" in the
+ // constructor, or in the function operator while processing a query.
+ // Repeated allocations from the heap for every incoming query is
+ // clearly a performance issue; this must be optimized in the future.
+ // The plan is to have a structure pre-allocate several "server state"
+ // objects which can be pulled off a free list and placed on an in-use
+ // list whenever a query comes in. This will serve the dual purpose
+ // of improving performance and guaranteeing that state information
+ // will *not* be destroyed when any one instance of the coroutine
+ // falls out of scope while waiting for an event.
+ //
+ // An ASIO acceptor object to handle new connections. Created in
+ // the constructor.
+ boost::shared_ptr<asio::ip::tcp::acceptor> acceptor_;
+
+ // Socket used to for listen for queries. Created in the
+ // constructor and stored in a shared_ptr because socket objects
+ // are not copyable.
+ boost::shared_ptr<asio::ip::tcp::socket> socket_;
+
+ // The buffer into which the response is written
+ boost::shared_ptr<isc::dns::OutputBuffer> respbuf_;
+
+ // \c IOMessage and \c Message objects to be passed to the
+ // DNS lookup and answer providers
+ boost::shared_ptr<asiolink::IOMessage> io_message_;
+ isc::dns::MessagePtr query_message_;
+ isc::dns::MessagePtr answer_message_;
+
+ // The buffer into which the query packet is written
+ boost::shared_array<char>data_;
+
+ // State information that is entirely internal to a given instance
+ // of the coroutine can be declared here.
+ size_t bytes_;
+ bool done_;
+
+ // Callback functions provided by the caller
+ const SimpleCallback* checkin_callback_;
+ const DNSLookup* lookup_callback_;
+ const DNSAnswer* answer_callback_;
+
+ boost::shared_ptr<IOEndpoint> peer_;
+ boost::shared_ptr<IOSocket> iosock_;
+};
+
+} // namespace asiolink
+#endif // __TCP_SERVER_H
diff --git a/src/lib/asiolink/tcp_socket.h b/src/lib/asiolink/tcp_socket.h
new file mode 100644
index 0000000..03713a5
--- /dev/null
+++ b/src/lib/asiolink/tcp_socket.h
@@ -0,0 +1,52 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __TCP_SOCKET_H
+#define __TCP_SOCKET_H 1
+
+#ifndef ASIO_HPP
+#error "asio.hpp must be included before including this, see asiolink.h as to why"
+#endif
+
+#include <asiolink/io_socket.h>
+
+namespace asiolink {
+
+/// \brief The \c TCPSocket class is a concrete derived class of
+/// \c IOSocket that represents a TCP socket.
+///
+/// In the current implementation, an object of this class is always
+/// instantiated within the wrapper routines. Applications are expected to
+/// get access to the object via the abstract base class, \c IOSocket.
+/// This design may be changed when we generalize the wrapper interface.
+class TCPSocket : public IOSocket {
+private:
+ TCPSocket(const TCPSocket& source);
+ TCPSocket& operator=(const TCPSocket& source);
+public:
+ /// \brief Constructor from an ASIO TCP socket.
+ ///
+ /// \param socket The ASIO representation of the TCP socket.
+ TCPSocket(asio::ip::tcp::socket& socket) : socket_(socket) {}
+
+ int getNative() const { return (socket_.native()); }
+ int getProtocol() const { return (IPPROTO_TCP); }
+
+private:
+ asio::ip::tcp::socket& socket_;
+};
+
+
+} // namespace asiolink
+#endif // __TCP_SOCKET_H
diff --git a/src/lib/asiolink/tcpdns.cc b/src/lib/asiolink/tcpdns.cc
deleted file mode 100644
index c00b87a..0000000
--- a/src/lib/asiolink/tcpdns.cc
+++ /dev/null
@@ -1,194 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <config.h>
-
-#include <unistd.h> // for some IPC/network system calls
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-#include <asio.hpp>
-#include <asio/ip/address.hpp>
-
-#include <boost/array.hpp>
-#include <boost/shared_ptr.hpp>
-
-#include <dns/buffer.h>
-#include <dns/message.h>
-
-#include <asiolink.h>
-#include <internal/coroutine.h>
-#include <internal/tcpdns.h>
-
-using namespace asio;
-using asio::ip::udp;
-using asio::ip::tcp;
-
-using namespace std;
-using namespace isc::dns;
-
-namespace asiolink {
-/// The following functions implement the \c UDPServer class.
-///
-/// The constructor
-TCPServer::TCPServer(io_service& io_service,
- const ip::address& addr, const uint16_t port,
- const SimpleCallback* checkin,
- const DNSLookup* lookup,
- const DNSAnswer* answer) :
- io_(io_service), done_(false),
- checkin_callback_(checkin), lookup_callback_(lookup),
- answer_callback_(answer)
-{
- tcp::endpoint endpoint(addr, port);
- acceptor_.reset(new tcp::acceptor(io_service));
- acceptor_->open(endpoint.protocol());
- // Set v6-only (we use a separate instantiation for v4,
- // otherwise asio will bind to both v4 and v6
- if (addr.is_v6()) {
- acceptor_->set_option(ip::v6_only(true));
- }
- acceptor_->set_option(tcp::acceptor::reuse_address(true));
- acceptor_->bind(endpoint);
- acceptor_->listen();
-}
-
-void
-TCPServer::operator()(error_code ec, size_t length) {
- /// Because the coroutine reeentry block is implemented as
- /// a switch statement, inline variable declarations are not
- /// permitted. Certain variables used below can be declared here.
- boost::array<const_buffer,2> bufs;
- OutputBuffer lenbuf(TCP_MESSAGE_LENGTHSIZE);
-
- CORO_REENTER (this) {
- do {
- /// Create a socket to listen for connections
- socket_.reset(new tcp::socket(acceptor_->get_io_service()));
-
- /// Wait for new connections. In the event of error,
- /// try again
- do {
- CORO_YIELD acceptor_->async_accept(*socket_, *this);
- } while (!ec);
-
- /// Fork the coroutine by creating a copy of this one and
- /// scheduling it on the ASIO service queue. The parent
- /// will continue listening for DNS connections while the
- /// handles the one that has just arrived.
- CORO_FORK io_.post(TCPServer(*this));
- } while (is_parent());
-
- /// Instantiate the data buffer that will be used by the
- /// asynchronous read call.
- data_.reset(new char[MAX_LENGTH]);
-
- /// Read the message, in two parts. First, the message length:
- CORO_YIELD async_read(*socket_, asio::buffer(data_.get(),
- TCP_MESSAGE_LENGTHSIZE), *this);
- if (ec) {
- CORO_YIELD return;
- }
-
- /// Now read the message itself. (This is done in a different scope
- /// to allow inline variable declarations.)
- CORO_YIELD {
- InputBuffer dnsbuffer((const void *) data_.get(), length);
- uint16_t msglen = dnsbuffer.readUint16();
- async_read(*socket_, asio::buffer(data_.get(), msglen), *this);
- }
-
- if (ec) {
- CORO_YIELD return;
- }
-
- // Create an \c IOMessage object to store the query.
- //
- // (XXX: It would be good to write a factory function
- // that would quickly generate an IOMessage object without
- // all these calls to "new".)
- peer_.reset(new TCPEndpoint(socket_->remote_endpoint()));
- iosock_.reset(new TCPSocket(*socket_));
- io_message_.reset(new IOMessage(data_.get(), length, *iosock_, *peer_));
- bytes_ = length;
-
- // Perform any necessary operations prior to processing the incoming
- // packet (e.g., checking for queued configuration messages).
- //
- // (XXX: it may be a performance issue to have this called for
- // every single incoming packet; we may wish to throttle it somehow
- // in the future.)
- if (checkin_callback_ != NULL) {
- (*checkin_callback_)(*io_message_);
- }
-
- // If we don't have a DNS Lookup provider, there's no point in
- // continuing; we exit the coroutine permanently.
- if (lookup_callback_ == NULL) {
- CORO_YIELD return;
- }
-
- // Reset or instantiate objects that will be needed by the
- // DNS lookup and the write call.
- respbuf_.reset(new OutputBuffer(0));
- query_message_.reset(new Message(Message::PARSE));
- answer_message_.reset(new Message(Message::RENDER));
-
- // Schedule a DNS lookup, and yield. When the lookup is
- // finished, the coroutine will resume immediately after
- // this point.
- CORO_YIELD io_.post(AsyncLookup<TCPServer>(*this));
-
- // The 'done_' flag indicates whether we have an answer
- // to send back. If not, exit the coroutine permanently.
- if (!done_) {
- CORO_YIELD return;
- }
-
- // Call the DNS answer provider to render the answer into
- // wire format
- (*answer_callback_)(*io_message_, query_message_,
- answer_message_, respbuf_);
-
- // Set up the response, beginning with two length bytes.
- lenbuf.writeUint16(respbuf_->getLength());
- bufs[0] = buffer(lenbuf.getData(), lenbuf.getLength());
- bufs[1] = buffer(respbuf_->getData(), respbuf_->getLength());
-
- // Begin an asynchronous send, and then yield. When the
- // send completes, we will resume immediately after this point
- // (though we have nothing further to do, so the coroutine
- // will simply exit at that time).
- CORO_YIELD async_write(*socket_, bufs, *this);
- }
-}
-
-/// Call the DNS lookup provider. (Expected to be called by the
-/// AsyncLookup<TCPServer> handler.)
-void
-TCPServer::asyncLookup() {
- (*lookup_callback_)(*io_message_, query_message_,
- answer_message_, respbuf_, this);
-}
-
-/// Post this coroutine on the ASIO service queue so that it will
-/// resume processing where it left off. The 'done' parameter indicates
-/// whether there is an answer to return to the client.
-void
-TCPServer::resume(const bool done) {
- done_ = done;
- io_.post(*this);
-}
-
-}
diff --git a/src/lib/asiolink/tests/Makefile.am b/src/lib/asiolink/tests/Makefile.am
index 3c6cd3e..7fe268e 100644
--- a/src/lib/asiolink/tests/Makefile.am
+++ b/src/lib/asiolink/tests/Makefile.am
@@ -17,10 +17,16 @@ if HAVE_GTEST
TESTS += run_unittests
run_unittests_SOURCES = $(top_srcdir)/src/lib/dns/tests/unittest_util.h
run_unittests_SOURCES += $(top_srcdir)/src/lib/dns/tests/unittest_util.cc
-run_unittests_SOURCES += asiolink_unittest.cc
+run_unittests_SOURCES += udp_query_unittest.cc
+run_unittests_SOURCES += ioaddress_unittest.cc
+run_unittests_SOURCES += ioendpoint_unittest.cc
+run_unittests_SOURCES += iosocket_unittest.cc
+run_unittests_SOURCES += io_service_unittest.cc
+run_unittests_SOURCES += interval_timer_unittest.cc
+run_unittests_SOURCES += recursive_query_unittest.cc
run_unittests_SOURCES += run_unittests.cc
run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
-run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
+run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS) $(LOG4CXX_LDFLAGS)
run_unittests_LDADD = $(GTEST_LDADD)
run_unittests_LDADD += $(SQLITE_LIBS)
run_unittests_LDADD += $(top_builddir)/src/lib/dns/libdns++.la
@@ -33,6 +39,10 @@ run_unittests_CXXFLAGS = $(AM_CXXFLAGS)
if USE_GXX
run_unittests_CXXFLAGS += -Wno-unused-parameter
endif
+if USE_CLANGPP
+# Same for clang++, but we need to turn off -Werror completely.
+run_unittests_CXXFLAGS += -Wno-error
+endif
endif
noinst_PROGRAMS = $(TESTS)
diff --git a/src/lib/asiolink/tests/asiolink_unittest.cc b/src/lib/asiolink/tests/asiolink_unittest.cc
deleted file mode 100644
index c83c090..0000000
--- a/src/lib/asiolink/tests/asiolink_unittest.cc
+++ /dev/null
@@ -1,1200 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <config.h>
-
-#include <sys/socket.h>
-#include <sys/time.h>
-
-#include <string.h>
-
-#include <boost/lexical_cast.hpp>
-#include <boost/bind.hpp>
-#include <boost/date_time/posix_time/posix_time_types.hpp>
-
-#include <gtest/gtest.h>
-
-#include <exceptions/exceptions.h>
-
-#include <dns/tests/unittest_util.h>
-#include <dns/rcode.h>
-
-#include <dns/buffer.h>
-#include <dns/message.h>
-
-// IMPORTANT: We shouldn't directly use ASIO definitions in this test.
-// In particular, we must not include asio.hpp in this file.
-// The asiolink module is primarily intended to be a wrapper that hide the
-// details of the underlying implementations. We need to test the wrapper
-// level behaviors. In addition, some compilers reject to compile this file
-// if we include asio.hpp unless we specify a special compiler option.
-// If we need to test something at the level of underlying ASIO and need
-// their definition, that test should go to asiolink/internal/tests.
-#include <asiolink/asiolink.h>
-#include <asiolink/iosocket.h>
-
-using isc::UnitTestUtil;
-using namespace std;
-using namespace asiolink;
-using namespace isc::dns;
-
-namespace {
-const char* const TEST_SERVER_PORT = "53535";
-const char* const TEST_CLIENT_PORT = "53536";
-const char* const TEST_IPV6_ADDR = "::1";
-const char* const TEST_IPV4_ADDR = "127.0.0.1";
-// This data is intended to be valid as a DNS/TCP-like message: the first
-// two octets encode the length of the rest of the data. This is crucial
-// for the tests below.
-const uint8_t test_data[] = {0, 4, 1, 2, 3, 4};
-// TODO: Consider this margin
-const boost::posix_time::time_duration TIMER_MARGIN_MSEC =
- boost::posix_time::milliseconds(50);
-
-TEST(IOAddressTest, fromText) {
- IOAddress io_address_v4("192.0.2.1");
- EXPECT_EQ("192.0.2.1", io_address_v4.toText());
-
- IOAddress io_address_v6("2001:db8::1234");
- EXPECT_EQ("2001:db8::1234", io_address_v6.toText());
-
- // bogus IPv4 address-like input
- EXPECT_THROW(IOAddress("192.0.2.2.1"), IOError);
-
- // bogus IPv4 address-like input: out-of-range octet
- EXPECT_THROW(IOAddress("192.0.2.300"), IOError);
-
- // bogus IPv6 address-like input
- EXPECT_THROW(IOAddress("2001:db8:::1234"), IOError);
-
- // bogus IPv6 address-like input
- EXPECT_THROW(IOAddress("2001:db8::efgh"), IOError);
-}
-
-TEST(IOEndpointTest, createUDPv4) {
- const IOEndpoint* ep;
- ep = IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"), 5300);
- EXPECT_EQ("192.0.2.1", ep->getAddress().toText());
- EXPECT_EQ(5300, ep->getPort());
- EXPECT_EQ(AF_INET, ep->getFamily());
- EXPECT_EQ(AF_INET, ep->getAddress().getFamily());
- EXPECT_EQ(IPPROTO_UDP, ep->getProtocol());
-}
-
-TEST(IOEndpointTest, createTCPv4) {
- const IOEndpoint* ep;
- ep = IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"), 5301);
- EXPECT_EQ("192.0.2.1", ep->getAddress().toText());
- EXPECT_EQ(5301, ep->getPort());
- EXPECT_EQ(AF_INET, ep->getFamily());
- EXPECT_EQ(AF_INET, ep->getAddress().getFamily());
- EXPECT_EQ(IPPROTO_TCP, ep->getProtocol());
-}
-
-TEST(IOEndpointTest, createUDPv6) {
- const IOEndpoint* ep;
- ep = IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1234"), 5302);
- EXPECT_EQ("2001:db8::1234", ep->getAddress().toText());
- EXPECT_EQ(5302, ep->getPort());
- EXPECT_EQ(AF_INET6, ep->getFamily());
- EXPECT_EQ(AF_INET6, ep->getAddress().getFamily());
- EXPECT_EQ(IPPROTO_UDP, ep->getProtocol());
-}
-
-TEST(IOEndpointTest, createTCPv6) {
- const IOEndpoint* ep;
- ep = IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1234"), 5303);
- EXPECT_EQ("2001:db8::1234", ep->getAddress().toText());
- EXPECT_EQ(5303, ep->getPort());
- EXPECT_EQ(AF_INET6, ep->getFamily());
- EXPECT_EQ(AF_INET6, ep->getAddress().getFamily());
- EXPECT_EQ(IPPROTO_TCP, ep->getProtocol());
-}
-
-TEST(IOEndpointTest, createIPProto) {
- EXPECT_THROW(IOEndpoint::create(IPPROTO_IP, IOAddress("192.0.2.1"),
- 5300)->getAddress().toText(),
- IOError);
-}
-
-TEST(IOSocketTest, dummySockets) {
- EXPECT_EQ(IPPROTO_UDP, IOSocket::getDummyUDPSocket().getProtocol());
- EXPECT_EQ(IPPROTO_TCP, IOSocket::getDummyTCPSocket().getProtocol());
- EXPECT_EQ(-1, IOSocket::getDummyUDPSocket().getNative());
- EXPECT_EQ(-1, IOSocket::getDummyTCPSocket().getNative());
-}
-
-TEST(IOServiceTest, badPort) {
- IOService io_service;
- EXPECT_THROW(DNSService(io_service, *"65536", true, false, NULL, NULL, NULL), IOError);
- EXPECT_THROW(DNSService(io_service, *"5300.0", true, false, NULL, NULL, NULL), IOError);
- EXPECT_THROW(DNSService(io_service, *"-1", true, false, NULL, NULL, NULL), IOError);
- EXPECT_THROW(DNSService(io_service, *"domain", true, false, NULL, NULL, NULL), IOError);
-}
-
-TEST(IOServiceTest, badAddress) {
- IOService io_service;
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"192.0.2.1.1", NULL, NULL, NULL), IOError);
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"2001:db8:::1", NULL, NULL, NULL), IOError);
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"localhost", NULL, NULL, NULL), IOError);
-}
-
-TEST(IOServiceTest, unavailableAddress) {
- IOService io_service;
- // These addresses should generally be unavailable as a valid local
- // address, although there's no guarantee in theory.
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"255.255.0.0", NULL, NULL, NULL), IOError);
-
- // Some OSes would simply reject binding attempt for an AF_INET6 socket
- // to an IPv4-mapped IPv6 address. Even if those that allow it, since
- // the corresponding IPv4 address is the same as the one used in the
- // AF_INET socket case above, it should at least show the same result
- // as the previous one.
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"::ffff:255.255.0.0", NULL, NULL, NULL), IOError);
-}
-
-TEST(IOServiceTest, duplicateBind_v6) {
- // In each sub test case, second attempt should fail due to duplicate bind
- IOService io_service;
-
- // IPv6, "any" address
- DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, false, true, NULL, NULL, NULL);
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, false, true, NULL, NULL, NULL), IOError);
- delete dns_service;
-
-}
-
-TEST(IOServiceTest, duplicateBind_v6_address) {
- // In each sub test case, second attempt should fail due to duplicate bind
- IOService io_service;
-
- // IPv6, specific address
- DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV6_ADDR, NULL, NULL, NULL);
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV6_ADDR, NULL, NULL, NULL), IOError);
- delete dns_service;
-
-}
-
-TEST(IOServiceTest, duplicateBind_v4) {
- // In each sub test case, second attempt should fail due to duplicate bind
- IOService io_service;
-
- // IPv4, "any" address
- DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, true, false, NULL, NULL, NULL);
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, true, false, NULL, NULL, NULL), IOError);
- delete dns_service;
-
-}
-
-TEST(IOServiceTest, duplicateBind_v4_address) {
- // In each sub test case, second attempt should fail due to duplicate bind
- IOService io_service;
-
- // IPv4, specific address
- DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV4_ADDR, NULL, NULL, NULL);
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV4_ADDR, NULL, NULL, NULL), IOError);
- delete dns_service;
-}
-
-// Disabled because IPv4-mapped addresses don't seem to be working with
-// the IOService constructor
-TEST(IOServiceTest, DISABLED_IPv4MappedDuplicateBind) {
- IOService io_service;
- // Duplicate bind on IPv4-mapped IPv6 address
- DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *"127.0.0.1", NULL, NULL, NULL);
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"::ffff:127.0.0.1", NULL, NULL, NULL), IOError);
- delete dns_service;
-
- // XXX:
- // Currently, this throws an "invalid argument" exception. I have
- // not been able to get IPv4-mapped addresses to work.
- dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *"::ffff:127.0.0.1", NULL, NULL, NULL);
- EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"127.0.0.1", NULL, NULL, NULL), IOError);
- delete dns_service;
-}
-
-// This function returns an addrinfo structure for use by tests, using
-// different addresses and ports depending on whether we're testing
-// IPv4 or v6, TCP or UDP, and client or server operation.
-struct addrinfo*
-resolveAddress(const int family, const int protocol, const bool client) {
- const char* const addr = (family == AF_INET6) ?
- TEST_IPV6_ADDR : TEST_IPV4_ADDR;
- const char* const port = client ? TEST_CLIENT_PORT : TEST_SERVER_PORT;
-
- struct addrinfo hints;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = family;
- hints.ai_socktype = (protocol == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
- hints.ai_protocol = protocol;
- hints.ai_flags = AI_NUMERICSERV;
-
- struct addrinfo* res;
- const int error = getaddrinfo(addr, port, &hints, &res);
- if (error != 0) {
- isc_throw(IOError, "getaddrinfo failed: " << gai_strerror(error));
- }
-
- return (res);
-}
-
-// This fixture is a framework for various types of network operations
-// using the ASIO interfaces. Each test case creates an IOService object,
-// opens a local "client" socket for testing, sends data via the local socket
-// to the service that would run in the IOService object.
-// A mock callback function (an ASIOCallBack object) is registered with the
-// IOService object, so the test code should be able to examine the data
-// received on the server side. It then checks the received data matches
-// expected parameters.
-// If initialization parameters of the IOService should be modified, the test
-// case can do it using the setDNSService() method.
-// Note: the set of tests in ASIOLinkTest use actual network services and may
-// involve undesirable side effects such as blocking.
-class ASIOLinkTest : public ::testing::Test {
-protected:
- ASIOLinkTest();
- ~ASIOLinkTest() {
- if (res_ != NULL) {
- freeaddrinfo(res_);
- }
- if (sock_ != -1) {
- close(sock_);
- }
- delete dns_service_;
- delete callback_;
- delete io_service_;
- }
-
- // Send a test UDP packet to a mock server
- void sendUDP(const int family) {
- res_ = resolveAddress(family, IPPROTO_UDP, false);
-
- sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
- if (sock_ < 0) {
- isc_throw(IOError, "failed to open test socket");
- }
- const int cc = sendto(sock_, test_data, sizeof(test_data), 0,
- res_->ai_addr, res_->ai_addrlen);
- if (cc != sizeof(test_data)) {
- isc_throw(IOError, "unexpected sendto result: " << cc);
- }
- io_service_->run();
- }
-
- // Send a test TCP packet to a mock server
- void sendTCP(const int family) {
- res_ = resolveAddress(family, IPPROTO_TCP, false);
-
- sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
- if (sock_ < 0) {
- isc_throw(IOError, "failed to open test socket");
- }
- if (connect(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
- isc_throw(IOError, "failed to connect to the test server");
- }
- const int cc = send(sock_, test_data, sizeof(test_data), 0);
- if (cc != sizeof(test_data)) {
- isc_throw(IOError, "unexpected send result: " << cc);
- }
- io_service_->run();
- }
-
- // Receive a UDP packet from a mock server; used for testing
- // recursive lookup. The caller must place a RecursiveQuery
- // on the IO Service queue before running this routine.
- void recvUDP(const int family, void* buffer, size_t& size) {
- res_ = resolveAddress(family, IPPROTO_UDP, true);
-
- sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
- if (sock_ < 0) {
- isc_throw(IOError, "failed to open test socket");
- }
-
- if (bind(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
- isc_throw(IOError, "bind failed: " << strerror(errno));
- }
-
- // The IO service queue should have a RecursiveQuery object scheduled
- // to run at this point. This call will cause it to begin an
- // async send, then return.
- io_service_->run_one();
-
- // ... and this one will block until the send has completed
- io_service_->run_one();
-
- // Now we attempt to recv() whatever was sent.
- // XXX: there's no guarantee the receiving socket can immediately get
- // the packet. Normally we can perform blocking recv to wait for it,
- // but in theory it's even possible that the packet is lost.
- // In order to prevent the test from hanging in such a worst case
- // we add an ad hoc timeout.
- const struct timeval timeo = { 10, 0 };
- int recv_options = 0;
- if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo,
- sizeof(timeo))) {
- if (errno == ENOPROTOOPT) {
- // Workaround for Solaris: it doesn't accept SO_RCVTIMEO
- // with the error of ENOPROTOOPT. Since this is a workaround
- // for rare error cases anyway, we simply switch to the
- // "don't wait" mode. If we still find an error in recv()
- // can happen often we'll consider a more complete solution.
- recv_options = MSG_DONTWAIT;
- } else {
- isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
- }
- }
- const int ret = recv(sock_, buffer, size, recv_options);
- if (ret < 0) {
- isc_throw(IOError, "recvfrom failed: " << strerror(errno));
- }
-
- // Pass the message size back via the size parameter
- size = ret;
- }
-
-
- // Set up an IO Service queue using the specified address
- void setDNSService(const char& address) {
- delete dns_service_;
- dns_service_ = NULL;
- delete io_service_;
- io_service_ = new IOService();
- callback_ = new ASIOCallBack(this);
- dns_service_ = new DNSService(*io_service_, *TEST_SERVER_PORT, address, callback_, NULL, NULL);
- }
-
- // Set up an IO Service queue using the "any" address, on IPv4 if
- // 'use_ipv4' is true and on IPv6 if 'use_ipv6' is true.
- void setDNSService(const bool use_ipv4, const bool use_ipv6) {
- delete dns_service_;
- dns_service_ = NULL;
- delete io_service_;
- io_service_ = new IOService();
- callback_ = new ASIOCallBack(this);
- dns_service_ = new DNSService(*io_service_, *TEST_SERVER_PORT, use_ipv4, use_ipv6, callback_,
- NULL, NULL);
- }
-
- // Set up empty DNS Service
- // Set up an IO Service queue without any addresses
- void setDNSService() {
- delete dns_service_;
- dns_service_ = NULL;
- delete io_service_;
- io_service_ = new IOService();
- callback_ = new ASIOCallBack(this);
- dns_service_ = new DNSService(*io_service_, callback_, NULL, NULL);
- }
-
- // Run a simple server test, on either IPv4 or IPv6, and over either
- // UDP or TCP. Calls the sendUDP() or sendTCP() methods, which will
- // start the IO Service queue. The UDPServer or TCPServer that was
- // created by setIOService() will receive the test packet and issue a
- // callback, which enables us to check that the data it received
- // matches what we sent.
- void doTest(const int family, const int protocol) {
- if (protocol == IPPROTO_UDP) {
- sendUDP(family);
- } else {
- sendTCP(family);
- }
-
- // There doesn't seem to be an effective test for the validity of
- // 'native'.
- // One thing we are sure is it must be different from our local socket.
- EXPECT_NE(sock_, callback_native_);
- EXPECT_EQ(protocol, callback_protocol_);
- EXPECT_EQ(family == AF_INET6 ? TEST_IPV6_ADDR : TEST_IPV4_ADDR,
- callback_address_);
-
- const uint8_t* expected_data =
- protocol == IPPROTO_UDP ? test_data : test_data + 2;
- const size_t expected_datasize =
- protocol == IPPROTO_UDP ? sizeof(test_data) :
- sizeof(test_data) - 2;
- EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, &callback_data_[0],
- callback_data_.size(),
- expected_data, expected_datasize);
- }
-
-protected:
- // This is a nonfunctional mockup of a DNSServer object. Its purpose
- // is to resume after a recursive query or other asynchronous call
- // has completed.
- class MockServer : public DNSServer {
- public:
- explicit MockServer(IOService& io_service,
- SimpleCallback* checkin = NULL,
- DNSLookup* lookup = NULL,
- DNSAnswer* answer = NULL) :
- io_(io_service),
- message_(new Message(Message::PARSE)),
- answer_message_(new Message(Message::RENDER)),
- respbuf_(new OutputBuffer(0)),
- checkin_(checkin), lookup_(lookup), answer_(answer)
- {}
-
- void operator()(asio::error_code ec = asio::error_code(),
- size_t length = 0)
- {}
-
- void resume(const bool) {
- // should never be called in our tests
- }
-
- DNSServer* clone() {
- MockServer* s = new MockServer(*this);
- return (s);
- }
-
- inline void asyncLookup() {
- if (lookup_) {
- (*lookup_)(*io_message_, message_, answer_message_,
- respbuf_, this);
- }
- }
-
- protected:
- IOService& io_;
- bool done_;
-
- private:
- // Currently unused; these will be used for testing
- // asynchronous lookup calls via the asyncLookup() method
- boost::shared_ptr<asiolink::IOMessage> io_message_;
- isc::dns::MessagePtr message_;
- isc::dns::MessagePtr answer_message_;
- isc::dns::OutputBufferPtr respbuf_;
-
- // Callback functions provided by the caller
- const SimpleCallback* checkin_;
- const DNSLookup* lookup_;
- const DNSAnswer* answer_;
- };
-
- // This version of mock server just stops the io_service when it is resumed
- class MockServerStop : public MockServer {
- public:
- explicit MockServerStop(IOService& io_service, bool* done) :
- MockServer(io_service),
- done_(done)
- {}
-
- void resume(const bool done) {
- *done_ = done;
- io_.stop();
- }
-
- DNSServer* clone() {
- return (new MockServerStop(*this));
- }
- private:
- bool* done_;
- };
-
-private:
- class ASIOCallBack : public SimpleCallback {
- public:
- ASIOCallBack(ASIOLinkTest* test_obj) : test_obj_(test_obj) {}
- void operator()(const IOMessage& io_message) const {
- test_obj_->callBack(io_message);
- }
- private:
- ASIOLinkTest* test_obj_;
- };
- void callBack(const IOMessage& io_message) {
- callback_protocol_ = io_message.getSocket().getProtocol();
- callback_native_ = io_message.getSocket().getNative();
- callback_address_ =
- io_message.getRemoteEndpoint().getAddress().toText();
- callback_data_.assign(
- static_cast<const uint8_t*>(io_message.getData()),
- static_cast<const uint8_t*>(io_message.getData()) +
- io_message.getDataSize());
- io_service_->stop();
- }
-protected:
- // We use a pointer for io_service_, because for some tests we
- // need to recreate a new one within one onstance of this class
- IOService* io_service_;
- DNSService* dns_service_;
- ASIOCallBack* callback_;
- int callback_protocol_;
- int callback_native_;
- string callback_address_;
- vector<uint8_t> callback_data_;
- int sock_;
- struct addrinfo* res_;
-};
-
-ASIOLinkTest::ASIOLinkTest() :
- dns_service_(NULL), callback_(NULL), sock_(-1), res_(NULL)
-{
- io_service_ = new IOService();
- setDNSService(true, true);
-}
-
-TEST_F(ASIOLinkTest, v6UDPSend) {
- doTest(AF_INET6, IPPROTO_UDP);
-}
-
-TEST_F(ASIOLinkTest, v6TCPSend) {
- doTest(AF_INET6, IPPROTO_TCP);
-}
-
-TEST_F(ASIOLinkTest, v4UDPSend) {
- doTest(AF_INET, IPPROTO_UDP);
-}
-
-TEST_F(ASIOLinkTest, v4TCPSend) {
- doTest(AF_INET, IPPROTO_TCP);
-}
-
-TEST_F(ASIOLinkTest, v6UDPSendSpecific) {
- // Explicitly set a specific address to be bound to the socket.
- // The subsequent test does not directly ensures the underlying socket
- // is bound to the expected address, but the success of the tests should
- // reasonably suggest it works as intended.
- // Specifying an address also implicitly means the service runs in a
- // single address-family mode. In tests using TCP we can confirm that
- // by trying to make a connection and seeing a failure. In UDP, it'd be
- // more complicated because we need to use a connected socket and catch
- // an error on a subsequent read operation. We could do it, but for
- // simplicity we only tests the easier cases for now.
-
- setDNSService(*TEST_IPV6_ADDR);
- doTest(AF_INET6, IPPROTO_UDP);
-}
-
-TEST_F(ASIOLinkTest, v6TCPSendSpecific) {
- setDNSService(*TEST_IPV6_ADDR);
- doTest(AF_INET6, IPPROTO_TCP);
-
- EXPECT_THROW(sendTCP(AF_INET), IOError);
-}
-
-TEST_F(ASIOLinkTest, v4UDPSendSpecific) {
- setDNSService(*TEST_IPV4_ADDR);
- doTest(AF_INET, IPPROTO_UDP);
-}
-
-TEST_F(ASIOLinkTest, v4TCPSendSpecific) {
- setDNSService(*TEST_IPV4_ADDR);
- doTest(AF_INET, IPPROTO_TCP);
-
- EXPECT_THROW(sendTCP(AF_INET6), IOError);
-}
-
-TEST_F(ASIOLinkTest, v6AddServer) {
- setDNSService();
- dns_service_->addServer(*TEST_SERVER_PORT, TEST_IPV6_ADDR);
- doTest(AF_INET6, IPPROTO_TCP);
-
- EXPECT_THROW(sendTCP(AF_INET), IOError);
-}
-
-TEST_F(ASIOLinkTest, v4AddServer) {
- setDNSService();
- dns_service_->addServer(*TEST_SERVER_PORT, TEST_IPV4_ADDR);
- doTest(AF_INET, IPPROTO_TCP);
-
- EXPECT_THROW(sendTCP(AF_INET6), IOError);
-}
-
-TEST_F(ASIOLinkTest, DISABLED_clearServers) {
- // FIXME: Enable when clearServers actually close the sockets
- // See #388
- setDNSService();
- dns_service_->clearServers();
-
- EXPECT_THROW(sendTCP(AF_INET), IOError);
- EXPECT_THROW(sendTCP(AF_INET6), IOError);
-}
-
-TEST_F(ASIOLinkTest, v6TCPOnly) {
- // Open only IPv6 TCP socket. A subsequent attempt of establishing an
- // IPv4/TCP connection should fail. See above for why we only test this
- // for TCP.
- setDNSService(false, true);
- EXPECT_THROW(sendTCP(AF_INET), IOError);
-}
-
-TEST_F(ASIOLinkTest, v4TCPOnly) {
- setDNSService(true, false);
- EXPECT_THROW(sendTCP(AF_INET6), IOError);
-}
-
-vector<pair<string, uint16_t> >
-singleAddress(const string &address, uint16_t port) {
- vector<pair<string, uint16_t> > result;
- result.push_back(pair<string, uint16_t>(address, port));
- return (result);
-}
-
-TEST_F(ASIOLinkTest, recursiveSetupV4) {
- setDNSService(true, false);
- uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
- EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
- singleAddress(TEST_IPV4_ADDR, port),
- singleAddress(TEST_IPV4_ADDR, port)));
-}
-
-TEST_F(ASIOLinkTest, recursiveSetupV6) {
- setDNSService(false, true);
- uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
- EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
- singleAddress(TEST_IPV6_ADDR, port),
- singleAddress(TEST_IPV6_ADDR,port)));
-}
-
-// XXX:
-// This is very inadequate unit testing. It should be generalized into
-// a routine that can do this with variable address family, address, and
-// port, and with the various callbacks defined in such a way as to ensure
-// full code coverage including error cases.
-TEST_F(ASIOLinkTest, forwarderSend) {
- setDNSService(true, false);
-
- // Note: We use the test prot plus one to ensure we aren't binding
- // to the same port as the actual server
- uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
-
- MockServer server(*io_service_);
- RecursiveQuery rq(*dns_service_,
- singleAddress(TEST_IPV4_ADDR, port),
- singleAddress(TEST_IPV4_ADDR, port));
-
- Question q(Name("example.com"), RRClass::IN(), RRType::TXT());
- OutputBufferPtr buffer(new OutputBuffer(0));
- MessagePtr answer(new Message(Message::RENDER));
- rq.sendQuery(q, answer, buffer, &server);
-
- char data[4096];
- size_t size = sizeof(data);
- ASSERT_NO_THROW(recvUDP(AF_INET, data, size));
-
- Message m(Message::PARSE);
- InputBuffer ibuf(data, size);
-
- // Make sure we can parse the message that was sent
- EXPECT_NO_THROW(m.parseHeader(ibuf));
- EXPECT_NO_THROW(m.fromWire(ibuf));
-
- // Check that the question sent matches the one we wanted
- QuestionPtr q2 = *m.beginQuestion();
- EXPECT_EQ(q.getName(), q2->getName());
- EXPECT_EQ(q.getType(), q2->getType());
- EXPECT_EQ(q.getClass(), q2->getClass());
-}
-
-int
-createTestSocket()
-{
- struct addrinfo* res_ = resolveAddress(AF_INET, IPPROTO_UDP, true);
- int sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
- if (sock_ < 0) {
- isc_throw(IOError, "failed to open test socket");
- }
- if (bind(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
- isc_throw(IOError, "failed to bind test socket");
- }
- return sock_;
-}
-
-int
-setSocketTimeout(int sock_, size_t tv_sec, size_t tv_usec) {
- const struct timeval timeo = { tv_sec, tv_usec };
- int recv_options = 0;
- if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo))) {
- if (errno == ENOPROTOOPT) { // see ASIOLinkTest::recvUDP()
- recv_options = MSG_DONTWAIT;
- } else {
- isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
- }
- }
- return recv_options;
-}
-
-// try to read from the socket max time
-// *num is incremented for every succesfull read
-// returns true if it can read max times, false otherwise
-bool tryRead(int sock_, int recv_options, size_t max, int* num) {
- size_t i = 0;
- do {
- char inbuff[512];
- if (recv(sock_, inbuff, sizeof(inbuff), recv_options) < 0) {
- return false;
- } else {
- ++i;
- ++*num;
- }
- } while (i < max);
- return true;
-}
-
-
-// Test it tries the correct amount of times before giving up
-TEST_F(ASIOLinkTest, forwardQueryTimeout) {
- // Prepare the service (we do not use the common setup, we do not answer
- setDNSService();
-
- // Prepare the socket
- sock_ = createTestSocket();
-
- // Prepare the server
- bool done(true);
- MockServerStop server(*io_service_, &done);
-
- // Do the answer
- const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
- RecursiveQuery query(*dns_service_,
- singleAddress(TEST_IPV4_ADDR, port),
- singleAddress(TEST_IPV4_ADDR, port),
- 10, 4000, 3000, 2);
- Question question(Name("example.net"), RRClass::IN(), RRType::A());
- OutputBufferPtr buffer(new OutputBuffer(0));
- MessagePtr answer(new Message(Message::RENDER));
- query.sendQuery(question, answer, buffer, &server);
-
- // Run the test
- io_service_->run();
-
- // Read up to 3 packets. Use some ad hoc timeout to prevent an infinite
- // block (see also recvUDP()).
- int recv_options = setSocketTimeout(sock_, 10, 0);
- int num = 0;
- bool read_success = tryRead(sock_, recv_options, 3, &num);
-
- // The query should fail
- EXPECT_FALSE(done);
- EXPECT_EQ(3, num);
- EXPECT_TRUE(read_success);
-}
-
-// If we set client timeout to lower than querytimeout, we should
-// get a failure answer, but still see retries
-// (no actual answer is given here yet)
-TEST_F(ASIOLinkTest, forwardClientTimeout) {
- // Prepare the service (we do not use the common setup, we do not answer
- setDNSService();
-
- sock_ = createTestSocket();
-
- // Prepare the server
- bool done(true);
- MockServerStop server(*io_service_, &done);
-
- MessagePtr answer(new Message(Message::RENDER));
-
- // Do the answer
- const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
- // Set it up to retry twice before client timeout fires
- // Since the lookup timer has not fired, it should retry
- // a third time
- RecursiveQuery query(*dns_service_,
- singleAddress(TEST_IPV4_ADDR, port),
- singleAddress(TEST_IPV4_ADDR, port),
- 50, 120, 1000, 3);
- Question question(Name("example.net"), RRClass::IN(), RRType::A());
- OutputBufferPtr buffer(new OutputBuffer(0));
- query.sendQuery(question, answer, buffer, &server);
-
- // Run the test
- io_service_->run();
-
- // we know it'll fail, so make it a shorter timeout
- int recv_options = setSocketTimeout(sock_, 1, 0);
-
- // Try to read 5 times, should stop after 3 reads
- int num = 0;
- bool read_success = tryRead(sock_, recv_options, 5, &num);
-
- // The query should fail (for resolver it should send back servfail,
- // but currently, and perhaps for forwarder in general, the effect
- // will be the same as on a lookup timeout, i.e. no answer is sent
- // back)
- EXPECT_FALSE(done);
- EXPECT_EQ(3, num);
- EXPECT_FALSE(read_success);
-}
-
-// If we set lookup timeout to lower than querytimeout*retries, we should
-// fail before the full amount of retries
-TEST_F(ASIOLinkTest, forwardLookupTimeout) {
- // Prepare the service (we do not use the common setup, we do not answer
- setDNSService();
-
- // Prepare the socket
- sock_ = createTestSocket();
-
- // Prepare the server
- bool done(true);
- MockServerStop server(*io_service_, &done);
-
- MessagePtr answer(new Message(Message::RENDER));
-
- // Do the answer
- const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
- // Set up the test so that it will retry 5 times, but the lookup
- // timeout will fire after only 3 normal timeouts
- RecursiveQuery query(*dns_service_,
- singleAddress(TEST_IPV4_ADDR, port),
- singleAddress(TEST_IPV4_ADDR, port),
- 50, 4000, 120, 5);
- Question question(Name("example.net"), RRClass::IN(), RRType::A());
- OutputBufferPtr buffer(new OutputBuffer(0));
- query.sendQuery(question, answer, buffer, &server);
-
- // Run the test
- io_service_->run();
-
- int recv_options = setSocketTimeout(sock_, 1, 0);
-
- // Try to read 5 times, should stop after 3 reads
- int num = 0;
- bool read_success = tryRead(sock_, recv_options, 5, &num);
-
- // The query should fail
- EXPECT_FALSE(done);
- EXPECT_EQ(3, num);
- EXPECT_FALSE(read_success);
-}
-
-// as mentioned above, we need a more better framework for this,
-// in addition to that, this sends out queries into the world
-// (which we should catch somehow and fake replies for)
-// for the skeleton code, it shouldn't be too much of a problem
-// Ok so even we don't all have access to the DNS world right now,
-// so disabling these tests too.
-TEST_F(ASIOLinkTest, DISABLED_recursiveSendOk) {
- setDNSService(true, false);
- bool done;
-
- MockServerStop server(*io_service_, &done);
- vector<pair<string, uint16_t> > empty_vector;
- RecursiveQuery rq(*dns_service_, empty_vector, empty_vector, 10000, 0);
-
- Question q(Name("www.isc.org"), RRClass::IN(), RRType::A());
- OutputBufferPtr buffer(new OutputBuffer(0));
- MessagePtr answer(new Message(Message::RENDER));
- rq.sendQuery(q, answer, buffer, &server);
- io_service_->run();
-
- // Check that the answer we got matches the one we wanted
- EXPECT_EQ(Rcode::NOERROR(), answer->getRcode());
- ASSERT_EQ(1, answer->getRRCount(Message::SECTION_ANSWER));
- RRsetPtr a = *answer->beginSection(Message::SECTION_ANSWER);
- EXPECT_EQ(q.getName(), a->getName());
- EXPECT_EQ(q.getType(), a->getType());
- EXPECT_EQ(q.getClass(), a->getClass());
- EXPECT_EQ(1, a->getRdataCount());
-}
-
-// see comments at previous test
-TEST_F(ASIOLinkTest, DISABLED_recursiveSendNXDOMAIN) {
- setDNSService(true, false);
- bool done;
-
- MockServerStop server(*io_service_, &done);
- vector<pair<string, uint16_t> > empty_vector;
- RecursiveQuery rq(*dns_service_, empty_vector, empty_vector, 10000, 0);
-
- Question q(Name("wwwdoesnotexist.isc.org"), RRClass::IN(), RRType::A());
- OutputBufferPtr buffer(new OutputBuffer(0));
- MessagePtr answer(new Message(Message::RENDER));
- rq.sendQuery(q, answer, buffer, &server);
- io_service_->run();
-
- // Check that the answer we got matches the one we wanted
- EXPECT_EQ(Rcode::NXDOMAIN(), answer->getRcode());
- EXPECT_EQ(0, answer->getRRCount(Message::SECTION_ANSWER));
-}
-
-
-
-// This fixture is for testing IntervalTimer. Some callback functors are
-// registered as callback function of the timer to test if they are called
-// or not.
-class IntervalTimerTest : public ::testing::Test {
-protected:
- IntervalTimerTest() : io_service_() {}
- ~IntervalTimerTest() {}
- class TimerCallBack : public std::unary_function<void, void> {
- public:
- TimerCallBack(IntervalTimerTest* test_obj) : test_obj_(test_obj) {}
- void operator()() const {
- test_obj_->timer_called_ = true;
- test_obj_->io_service_.stop();
- return;
- }
- private:
- IntervalTimerTest* test_obj_;
- };
- class TimerCallBackCounter : public std::unary_function<void, void> {
- public:
- TimerCallBackCounter(IntervalTimerTest* test_obj) : test_obj_(test_obj) {
- counter_ = 0;
- }
- void operator()() {
- ++counter_;
- return;
- }
- int counter_;
- private:
- IntervalTimerTest* test_obj_;
- };
- class TimerCallBackCancelDeleter : public std::unary_function<void, void> {
- public:
- TimerCallBackCancelDeleter(IntervalTimerTest* test_obj,
- IntervalTimer* timer,
- TimerCallBackCounter& counter)
- : test_obj_(test_obj), timer_(timer), counter_(counter), count_(0)
- {}
- void operator()() {
- ++count_;
- if (count_ == 1) {
- // First time of call back.
- // Store the value of counter_.counter_.
- prev_counter_ = counter_.counter_;
- delete timer_;
- } else if (count_ == 2) {
- // Second time of call back.
- // Stop io_service to stop all timers.
- test_obj_->io_service_.stop();
- // Compare the value of counter_.counter_ with stored one.
- // If TimerCallBackCounter was not called (expected behavior),
- // they are same.
- if (counter_.counter_ == prev_counter_) {
- test_obj_->timer_cancel_success_ = true;
- }
- }
- return;
- }
- private:
- IntervalTimerTest* test_obj_;
- IntervalTimer* timer_;
- TimerCallBackCounter& counter_;
- int count_;
- int prev_counter_;
- };
- class TimerCallBackCanceller {
- public:
- TimerCallBackCanceller(unsigned int& counter, IntervalTimer& itimer) :
- counter_(counter), itimer_(itimer)
- {}
- void operator()() {
- ++counter_;
- itimer_.cancel();
- }
- private:
- unsigned int& counter_;
- IntervalTimer& itimer_;
- };
- class TimerCallBackOverwriter : public std::unary_function<void, void> {
- public:
- TimerCallBackOverwriter(IntervalTimerTest* test_obj,
- IntervalTimer& timer)
- : test_obj_(test_obj), timer_(timer), count_(0)
- {}
- void operator()() {
- ++count_;
- if (count_ == 1) {
- // First time of call back.
- // Call setupTimer() to update callback function
- // to TimerCallBack.
- test_obj_->timer_called_ = false;
- timer_.setupTimer(TimerCallBack(test_obj_), 1);
- } else if (count_ == 2) {
- // Second time of call back.
- // If it reaches here, re-setupTimer() is failed (unexpected).
- // We should stop here.
- test_obj_->io_service_.stop();
- }
- return;
- }
- private:
- IntervalTimerTest* test_obj_;
- IntervalTimer& timer_;
- int count_;
- };
-protected:
- IOService io_service_;
- bool timer_called_;
- bool timer_cancel_success_;
-};
-
-TEST_F(IntervalTimerTest, invalidArgumentToIntervalTimer) {
- // Create asio_link::IntervalTimer and setup.
- IntervalTimer itimer(io_service_);
- // expect throw if call back function is empty
- EXPECT_THROW(itimer.setupTimer(IntervalTimer::Callback(), 1),
- isc::InvalidParameter);
- // expect throw if interval is 0
- EXPECT_THROW(itimer.setupTimer(TimerCallBack(this), 0), isc::BadValue);
-}
-
-TEST_F(IntervalTimerTest, startIntervalTimer) {
- // Create asio_link::IntervalTimer and setup.
- // Then run IOService and test if the callback function is called.
- IntervalTimer itimer(io_service_);
- timer_called_ = false;
- // store start time
- boost::posix_time::ptime start;
- start = boost::posix_time::microsec_clock::universal_time();
- // setup timer
- itimer.setupTimer(TimerCallBack(this), 1);
- EXPECT_EQ(1, itimer.getInterval());
- io_service_.run();
- // reaches here after timer expired
- // delta: difference between elapsed time and 1 second
- boost::posix_time::time_duration delta =
- (boost::posix_time::microsec_clock::universal_time() - start)
- - boost::posix_time::seconds(1);
- if (delta.is_negative()) {
- delta.invert_sign();
- }
- // expect TimerCallBack is called; timer_called_ is true
- EXPECT_TRUE(timer_called_);
- // expect interval is 1 second +/- TIMER_MARGIN_MSEC.
- EXPECT_TRUE(delta < TIMER_MARGIN_MSEC);
-}
-
-TEST_F(IntervalTimerTest, destructIntervalTimer) {
- // Note: This test currently takes 6 seconds. The timer should have
- // finer granularity and timer periods in this test should be shorter
- // in the future.
- // This code isn't exception safe, but we'd rather keep the code
- // simpler and more readable as this is only for tests and if it throws
- // the program would immediately terminate anyway.
-
- // The call back function will not be called after the timer is
- // destructed.
- //
- // There are two timers:
- // itimer_counter (A)
- // (Calls TimerCallBackCounter)
- // - increments internal counter in callback function
- // itimer_canceller (B)
- // (Calls TimerCallBackCancelDeleter)
- // - first time of callback, it stores the counter value of
- // callback_canceller and destructs itimer_counter
- // - second time of callback, it compares the counter value of
- // callback_canceller with stored value
- // if they are same the timer was not called; expected result
- // if they are different the timer was called after destructed
- //
- // 0 1 2 3 4 5 6 (s)
- // (A) i-----+--x
- // ^
- // |destruct itimer_counter
- // (B) i--------+--------s
- // ^stop io_service
- // and test itimer_counter have been stopped
- //
-
- // itimer_counter will be deleted in TimerCallBackCancelDeleter
- IntervalTimer* itimer_counter = new IntervalTimer(io_service_);
- IntervalTimer itimer_canceller(io_service_);
- timer_cancel_success_ = false;
- TimerCallBackCounter callback_canceller(this);
- itimer_counter->setupTimer(callback_canceller, 2);
- itimer_canceller.setupTimer(
- TimerCallBackCancelDeleter(this, itimer_counter,
- callback_canceller),
- 3);
- io_service_.run();
- EXPECT_TRUE(timer_cancel_success_);
-}
-
-TEST_F(IntervalTimerTest, cancel) {
- // Similar to destructIntervalTimer test, but the first timer explicitly
- // cancels itself on first callback.
- IntervalTimer itimer_counter(io_service_);
- IntervalTimer itimer_watcher(io_service_);
- unsigned int counter = 0;
- itimer_counter.setupTimer(TimerCallBackCanceller(counter, itimer_counter),
- 1);
- itimer_watcher.setupTimer(TimerCallBack(this), 3);
- io_service_.run();
- EXPECT_EQ(1, counter);
- EXPECT_EQ(0, itimer_counter.getInterval());
-
- // canceling an already canceled timer shouldn't cause any surprise.
- EXPECT_NO_THROW(itimer_counter.cancel());
-}
-
-TEST_F(IntervalTimerTest, overwriteIntervalTimer) {
- // Note: This test currently takes 4 seconds. The timer should have
- // finer granularity and timer periods in this test should be shorter
- // in the future.
-
- // Calling setupTimer() multiple times updates call back function
- // and interval.
- //
- // There are two timers:
- // itimer (A)
- // (Calls TimerCallBackCounter / TimerCallBack)
- // - increments internal counter in callback function
- // (TimerCallBackCounter)
- // interval: 2 seconds
- // - io_service_.stop() (TimerCallBack)
- // interval: 1 second
- // itimer_overwriter (B)
- // (Calls TimerCallBackOverwriter)
- // - first time of callback, it calls setupTimer() to change
- // call back function and interval of itimer to
- // TimerCallBack / 1 second
- // after 3 + 1 seconds from the beginning of this test,
- // TimerCallBack() will be called and io_service_ stops.
- // - second time of callback, it means the test fails.
- //
- // 0 1 2 3 4 5 6 (s)
- // (A) i-----+--C--s
- // ^ ^stop io_service
- // |change call back function
- // (B) i--------+--------S
- // ^(stop io_service on fail)
- //
-
- IntervalTimer itimer(io_service_);
- IntervalTimer itimer_overwriter(io_service_);
- // store start time
- boost::posix_time::ptime start;
- start = boost::posix_time::microsec_clock::universal_time();
- itimer.setupTimer(TimerCallBackCounter(this), 2);
- itimer_overwriter.setupTimer(TimerCallBackOverwriter(this, itimer), 3);
- io_service_.run();
- // reaches here after timer expired
- // if interval is updated, it takes
- // 3 seconds for TimerCallBackOverwriter
- // + 1 second for TimerCallBack (stop)
- // = 4 seconds.
- // otherwise (test fails), it takes
- // 3 seconds for TimerCallBackOverwriter
- // + 3 seconds for TimerCallBackOverwriter (stop)
- // = 6 seconds.
- // delta: difference between elapsed time and 3 + 1 seconds
- boost::posix_time::time_duration delta =
- (boost::posix_time::microsec_clock::universal_time() - start)
- - boost::posix_time::seconds(3 + 1);
- if (delta.is_negative()) {
- delta.invert_sign();
- }
- // expect callback function is updated: TimerCallBack is called
- EXPECT_TRUE(timer_called_);
- // expect interval is updated
- EXPECT_TRUE(delta < TIMER_MARGIN_MSEC);
-}
-
-}
diff --git a/src/lib/asiolink/tests/interval_timer_unittest.cc b/src/lib/asiolink/tests/interval_timer_unittest.cc
new file mode 100644
index 0000000..9a7b0f4
--- /dev/null
+++ b/src/lib/asiolink/tests/interval_timer_unittest.cc
@@ -0,0 +1,292 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+#include <gtest/gtest.h>
+
+#include <asiolink/asiolink.h>
+
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+
+namespace {
+// TODO: Consider this margin
+const boost::posix_time::time_duration TIMER_MARGIN_MSEC =
+ boost::posix_time::milliseconds(50);
+}
+
+using namespace asiolink;
+
+// This fixture is for testing IntervalTimer. Some callback functors are
+// registered as callback function of the timer to test if they are called
+// or not.
+class IntervalTimerTest : public ::testing::Test {
+protected:
+ IntervalTimerTest() : io_service_() {}
+ ~IntervalTimerTest() {}
+ class TimerCallBack : public std::unary_function<void, void> {
+ public:
+ TimerCallBack(IntervalTimerTest* test_obj) : test_obj_(test_obj) {}
+ void operator()() const {
+ test_obj_->timer_called_ = true;
+ test_obj_->io_service_.stop();
+ return;
+ }
+ private:
+ IntervalTimerTest* test_obj_;
+ };
+ class TimerCallBackCounter : public std::unary_function<void, void> {
+ public:
+ TimerCallBackCounter(IntervalTimerTest* test_obj) : test_obj_(test_obj) {
+ counter_ = 0;
+ }
+ void operator()() {
+ ++counter_;
+ return;
+ }
+ int counter_;
+ private:
+ IntervalTimerTest* test_obj_;
+ };
+ class TimerCallBackCancelDeleter : public std::unary_function<void, void> {
+ public:
+ TimerCallBackCancelDeleter(IntervalTimerTest* test_obj,
+ IntervalTimer* timer,
+ TimerCallBackCounter& counter)
+ : test_obj_(test_obj), timer_(timer), counter_(counter), count_(0)
+ {}
+ void operator()() {
+ ++count_;
+ if (count_ == 1) {
+ // First time of call back.
+ // Store the value of counter_.counter_.
+ prev_counter_ = counter_.counter_;
+ delete timer_;
+ } else if (count_ == 2) {
+ // Second time of call back.
+ // Stop io_service to stop all timers.
+ test_obj_->io_service_.stop();
+ // Compare the value of counter_.counter_ with stored one.
+ // If TimerCallBackCounter was not called (expected behavior),
+ // they are same.
+ if (counter_.counter_ == prev_counter_) {
+ test_obj_->timer_cancel_success_ = true;
+ }
+ }
+ return;
+ }
+ private:
+ IntervalTimerTest* test_obj_;
+ IntervalTimer* timer_;
+ TimerCallBackCounter& counter_;
+ int count_;
+ int prev_counter_;
+ };
+ class TimerCallBackCanceller {
+ public:
+ TimerCallBackCanceller(unsigned int& counter, IntervalTimer& itimer) :
+ counter_(counter), itimer_(itimer)
+ {}
+ void operator()() {
+ ++counter_;
+ itimer_.cancel();
+ }
+ private:
+ unsigned int& counter_;
+ IntervalTimer& itimer_;
+ };
+ class TimerCallBackOverwriter : public std::unary_function<void, void> {
+ public:
+ TimerCallBackOverwriter(IntervalTimerTest* test_obj,
+ IntervalTimer& timer)
+ : test_obj_(test_obj), timer_(timer), count_(0)
+ {}
+ void operator()() {
+ ++count_;
+ if (count_ == 1) {
+ // First time of call back.
+ // Call setup() to update callback function to TimerCallBack.
+ test_obj_->timer_called_ = false;
+ timer_.setup(TimerCallBack(test_obj_), 100);
+ } else if (count_ == 2) {
+ // Second time of call back.
+ // If it reaches here, re-setup() is failed (unexpected).
+ // We should stop here.
+ test_obj_->io_service_.stop();
+ }
+ return;
+ }
+ private:
+ IntervalTimerTest* test_obj_;
+ IntervalTimer& timer_;
+ int count_;
+ };
+protected:
+ IOService io_service_;
+ bool timer_called_;
+ bool timer_cancel_success_;
+};
+
+TEST_F(IntervalTimerTest, invalidArgumentToIntervalTimer) {
+ // Create asio_link::IntervalTimer and setup.
+ IntervalTimer itimer(io_service_);
+ // expect throw if call back function is empty
+ EXPECT_THROW(itimer.setup(IntervalTimer::Callback(), 1),
+ isc::InvalidParameter);
+ // expect throw if interval is not greater than 0
+ EXPECT_THROW(itimer.setup(TimerCallBack(this), 0), isc::BadValue);
+ EXPECT_THROW(itimer.setup(TimerCallBack(this), -1), isc::BadValue);
+}
+
+TEST_F(IntervalTimerTest, startIntervalTimer) {
+ // Create asio_link::IntervalTimer and setup.
+ // Then run IOService and test if the callback function is called.
+ IntervalTimer itimer(io_service_);
+ timer_called_ = false;
+ // store start time
+ boost::posix_time::ptime start;
+ start = boost::posix_time::microsec_clock::universal_time();
+ // setup timer
+ itimer.setup(TimerCallBack(this), 100);
+ EXPECT_EQ(100, itimer.getInterval());
+ io_service_.run();
+ // reaches here after timer expired
+ // delta: difference between elapsed time and 100 milliseconds.
+ boost::posix_time::time_duration delta =
+ (boost::posix_time::microsec_clock::universal_time() - start)
+ - boost::posix_time::millisec(100);
+ if (delta.is_negative()) {
+ delta.invert_sign();
+ }
+ // expect TimerCallBack is called; timer_called_ is true
+ EXPECT_TRUE(timer_called_);
+ // expect interval is 100 milliseconds +/- TIMER_MARGIN_MSEC.
+ EXPECT_TRUE(delta < TIMER_MARGIN_MSEC);
+}
+
+TEST_F(IntervalTimerTest, destructIntervalTimer) {
+ // This code isn't exception safe, but we'd rather keep the code
+ // simpler and more readable as this is only for tests and if it throws
+ // the program would immediately terminate anyway.
+
+ // The call back function will not be called after the timer is
+ // destroyed.
+ //
+ // There are two timers:
+ // itimer_counter (A)
+ // (Calls TimerCallBackCounter)
+ // - increments internal counter in callback function
+ // itimer_canceller (B)
+ // (Calls TimerCallBackCancelDeleter)
+ // - first time of callback, it stores the counter value of
+ // callback_canceller and destroys itimer_counter
+ // - second time of callback, it compares the counter value of
+ // callback_canceller with stored value
+ // if they are same the timer was not called; expected result
+ // if they are different the timer was called after destroyed
+ //
+ // 0 100 200 300 400 500 600 (ms)
+ // (A) i--------+----x
+ // ^
+ // |destroy itimer_counter
+ // (B) i-------------+--------------s
+ // ^stop io_service
+ // and check if itimer_counter have been
+ // stopped
+
+ // itimer_counter will be deleted in TimerCallBackCancelDeleter
+ IntervalTimer* itimer_counter = new IntervalTimer(io_service_);
+ IntervalTimer itimer_canceller(io_service_);
+ timer_cancel_success_ = false;
+ TimerCallBackCounter callback_canceller(this);
+ itimer_counter->setup(callback_canceller, 200);
+ itimer_canceller.setup(
+ TimerCallBackCancelDeleter(this, itimer_counter, callback_canceller),
+ 300);
+ io_service_.run();
+ EXPECT_TRUE(timer_cancel_success_);
+}
+
+TEST_F(IntervalTimerTest, cancel) {
+ // Similar to destructIntervalTimer test, but the first timer explicitly
+ // cancels itself on first callback.
+ IntervalTimer itimer_counter(io_service_);
+ IntervalTimer itimer_watcher(io_service_);
+ unsigned int counter = 0;
+ itimer_counter.setup(TimerCallBackCanceller(counter, itimer_counter), 100);
+ itimer_watcher.setup(TimerCallBack(this), 200);
+ io_service_.run();
+ EXPECT_EQ(1, counter);
+ EXPECT_EQ(0, itimer_counter.getInterval());
+
+ // canceling an already canceled timer shouldn't cause any surprise.
+ EXPECT_NO_THROW(itimer_counter.cancel());
+}
+
+TEST_F(IntervalTimerTest, overwriteIntervalTimer) {
+ // Calling setup() multiple times updates call back function and interval.
+ //
+ // There are two timers:
+ // itimer (A)
+ // (Calls TimerCallBackCounter / TimerCallBack)
+ // - increments internal counter in callback function
+ // (TimerCallBackCounter)
+ // interval: 300 milliseconds
+ // - io_service_.stop() (TimerCallBack)
+ // interval: 100 milliseconds
+ // itimer_overwriter (B)
+ // (Calls TimerCallBackOverwriter)
+ // - first time of callback, it calls setup() to change call back
+ // function to TimerCallBack and interval of itimer to 100
+ // milliseconds
+ // after 300 + 100 milliseconds from the beginning of this test,
+ // TimerCallBack() will be called and io_service_ stops.
+ // - second time of callback, it means the test fails.
+ //
+ // 0 100 200 300 400 500 600 700 800 (ms)
+ // (A) i-------------+----C----s
+ // ^ ^stop io_service
+ // |change call back function
+ // (B) i------------------+-------------------S
+ // ^(stop io_service on fail)
+ //
+
+ IntervalTimer itimer(io_service_);
+ IntervalTimer itimer_overwriter(io_service_);
+ // store start time
+ boost::posix_time::ptime start;
+ start = boost::posix_time::microsec_clock::universal_time();
+ itimer.setup(TimerCallBackCounter(this), 300);
+ itimer_overwriter.setup(TimerCallBackOverwriter(this, itimer), 400);
+ io_service_.run();
+ // reaches here after timer expired
+ // if interval is updated, it takes
+ // 400 milliseconds for TimerCallBackOverwriter
+ // + 100 milliseconds for TimerCallBack (stop)
+ // = 500 milliseconds.
+ // otherwise (test fails), it takes
+ // 400 milliseconds for TimerCallBackOverwriter
+ // + 400 milliseconds for TimerCallBackOverwriter (stop)
+ // = 800 milliseconds.
+ // delta: difference between elapsed time and 400 + 100 milliseconds
+ boost::posix_time::time_duration delta =
+ (boost::posix_time::microsec_clock::universal_time() - start)
+ - boost::posix_time::millisec(400 + 100);
+ if (delta.is_negative()) {
+ delta.invert_sign();
+ }
+ // expect callback function is updated: TimerCallBack is called
+ EXPECT_TRUE(timer_called_);
+ // expect interval is updated
+ EXPECT_TRUE(delta < TIMER_MARGIN_MSEC);
+}
diff --git a/src/lib/asiolink/tests/io_service_unittest.cc b/src/lib/asiolink/tests/io_service_unittest.cc
new file mode 100644
index 0000000..49aa67e
--- /dev/null
+++ b/src/lib/asiolink/tests/io_service_unittest.cc
@@ -0,0 +1,115 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+#include <gtest/gtest.h>
+
+#include <asiolink/asiolink.h>
+
+using namespace asiolink;
+
+const char* const TEST_SERVER_PORT = "53535";
+const char* const TEST_CLIENT_PORT = "53536";
+const char* const TEST_IPV6_ADDR = "::1";
+const char* const TEST_IPV4_ADDR = "127.0.0.1";
+
+TEST(IOServiceTest, badPort) {
+ IOService io_service;
+ EXPECT_THROW(DNSService(io_service, *"65536", true, false, NULL, NULL, NULL), IOError);
+ EXPECT_THROW(DNSService(io_service, *"5300.0", true, false, NULL, NULL, NULL), IOError);
+ EXPECT_THROW(DNSService(io_service, *"-1", true, false, NULL, NULL, NULL), IOError);
+ EXPECT_THROW(DNSService(io_service, *"domain", true, false, NULL, NULL, NULL), IOError);
+}
+
+TEST(IOServiceTest, badAddress) {
+ IOService io_service;
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"192.0.2.1.1", NULL, NULL, NULL), IOError);
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"2001:db8:::1", NULL, NULL, NULL), IOError);
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"localhost", NULL, NULL, NULL), IOError);
+}
+
+TEST(IOServiceTest, unavailableAddress) {
+ IOService io_service;
+ // These addresses should generally be unavailable as a valid local
+ // address, although there's no guarantee in theory.
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"192.0.2.0", NULL, NULL, NULL), IOError);
+
+ // Some OSes would simply reject binding attempt for an AF_INET6 socket
+ // to an IPv4-mapped IPv6 address. Even if those that allow it, since
+ // the corresponding IPv4 address is the same as the one used in the
+ // AF_INET socket case above, it should at least show the same result
+ // as the previous one.
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"::ffff:192.0.2.0", NULL, NULL, NULL), IOError);
+}
+
+TEST(IOServiceTest, duplicateBind_v6) {
+ // In each sub test case, second attempt should fail due to duplicate bind
+ IOService io_service;
+
+ // IPv6, "any" address
+ DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, false, true, NULL, NULL, NULL);
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, false, true, NULL, NULL, NULL), IOError);
+ delete dns_service;
+
+}
+
+TEST(IOServiceTest, duplicateBind_v6_address) {
+ // In each sub test case, second attempt should fail due to duplicate bind
+ IOService io_service;
+
+ // IPv6, specific address
+ DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV6_ADDR, NULL, NULL, NULL);
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV6_ADDR, NULL, NULL, NULL), IOError);
+ delete dns_service;
+
+}
+
+TEST(IOServiceTest, duplicateBind_v4) {
+ // In each sub test case, second attempt should fail due to duplicate bind
+ IOService io_service;
+
+ // IPv4, "any" address
+ DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, true, false, NULL, NULL, NULL);
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, true, false, NULL, NULL, NULL), IOError);
+ delete dns_service;
+
+}
+
+TEST(IOServiceTest, duplicateBind_v4_address) {
+ // In each sub test case, second attempt should fail due to duplicate bind
+ IOService io_service;
+
+ // IPv4, specific address
+ DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV4_ADDR, NULL, NULL, NULL);
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV4_ADDR, NULL, NULL, NULL), IOError);
+ delete dns_service;
+}
+
+// Disabled because IPv4-mapped addresses don't seem to be working with
+// the IOService constructor
+TEST(IOServiceTest, DISABLED_IPv4MappedDuplicateBind) {
+ IOService io_service;
+ // Duplicate bind on IPv4-mapped IPv6 address
+ DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *"127.0.0.1", NULL, NULL, NULL);
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"::ffff:127.0.0.1", NULL, NULL, NULL), IOError);
+ delete dns_service;
+
+ // XXX:
+ // Currently, this throws an "invalid argument" exception. I have
+ // not been able to get IPv4-mapped addresses to work.
+ dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *"::ffff:127.0.0.1", NULL, NULL, NULL);
+ EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"127.0.0.1", NULL, NULL, NULL), IOError);
+ delete dns_service;
+}
+
diff --git a/src/lib/asiolink/tests/ioaddress_unittest.cc b/src/lib/asiolink/tests/ioaddress_unittest.cc
new file mode 100644
index 0000000..57c9496
--- /dev/null
+++ b/src/lib/asiolink/tests/ioaddress_unittest.cc
@@ -0,0 +1,57 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+#include <gtest/gtest.h>
+
+#include <asiolink/asiolink.h>
+
+using namespace asiolink;
+
+TEST(IOAddressTest, fromText) {
+ IOAddress io_address_v4("192.0.2.1");
+ EXPECT_EQ("192.0.2.1", io_address_v4.toText());
+
+ IOAddress io_address_v6("2001:db8::1234");
+ EXPECT_EQ("2001:db8::1234", io_address_v6.toText());
+
+ // bogus IPv4 address-like input
+ EXPECT_THROW(IOAddress("192.0.2.2.1"), IOError);
+
+ // bogus IPv4 address-like input: out-of-range octet
+ EXPECT_THROW(IOAddress("192.0.2.300"), IOError);
+
+ // bogus IPv6 address-like input
+ EXPECT_THROW(IOAddress("2001:db8:::1234"), IOError);
+
+ // bogus IPv6 address-like input
+ EXPECT_THROW(IOAddress("2001:db8::efgh"), IOError);
+}
+
+TEST(IOAddressTest, Equality) {
+ EXPECT_TRUE(IOAddress("192.0.2.1") == IOAddress("192.0.2.1"));
+ EXPECT_FALSE(IOAddress("192.0.2.1") != IOAddress("192.0.2.1"));
+
+ EXPECT_TRUE(IOAddress("192.0.2.1") != IOAddress("192.0.2.2"));
+ EXPECT_FALSE(IOAddress("192.0.2.1") == IOAddress("192.0.2.2"));
+
+ EXPECT_TRUE(IOAddress("2001:db8::12") == IOAddress("2001:0DB8:0:0::0012"));
+ EXPECT_FALSE(IOAddress("2001:db8::12") != IOAddress("2001:0DB8:0:0::0012"));
+
+ EXPECT_TRUE(IOAddress("2001:db8::1234") != IOAddress("2001:db8::1235"));
+ EXPECT_FALSE(IOAddress("2001:db8::1234") == IOAddress("2001:db8::1235"));
+
+ EXPECT_TRUE(IOAddress("2001:db8::1234") != IOAddress("192.0.2.3"));
+ EXPECT_FALSE(IOAddress("2001:db8::1234") == IOAddress("192.0.2.3"));
+}
diff --git a/src/lib/asiolink/tests/ioendpoint_unittest.cc b/src/lib/asiolink/tests/ioendpoint_unittest.cc
new file mode 100644
index 0000000..a5b5cd2
--- /dev/null
+++ b/src/lib/asiolink/tests/ioendpoint_unittest.cc
@@ -0,0 +1,67 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+#include <gtest/gtest.h>
+
+#include <asiolink/asiolink.h>
+
+using namespace asiolink;
+
+TEST(IOEndpointTest, createUDPv4) {
+ const IOEndpoint* ep;
+ ep = IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"), 5300);
+ EXPECT_EQ("192.0.2.1", ep->getAddress().toText());
+ EXPECT_EQ(5300, ep->getPort());
+ EXPECT_EQ(AF_INET, ep->getFamily());
+ EXPECT_EQ(AF_INET, ep->getAddress().getFamily());
+ EXPECT_EQ(IPPROTO_UDP, ep->getProtocol());
+}
+
+TEST(IOEndpointTest, createTCPv4) {
+ const IOEndpoint* ep;
+ ep = IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"), 5301);
+ EXPECT_EQ("192.0.2.1", ep->getAddress().toText());
+ EXPECT_EQ(5301, ep->getPort());
+ EXPECT_EQ(AF_INET, ep->getFamily());
+ EXPECT_EQ(AF_INET, ep->getAddress().getFamily());
+ EXPECT_EQ(IPPROTO_TCP, ep->getProtocol());
+}
+
+TEST(IOEndpointTest, createUDPv6) {
+ const IOEndpoint* ep;
+ ep = IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1234"), 5302);
+ EXPECT_EQ("2001:db8::1234", ep->getAddress().toText());
+ EXPECT_EQ(5302, ep->getPort());
+ EXPECT_EQ(AF_INET6, ep->getFamily());
+ EXPECT_EQ(AF_INET6, ep->getAddress().getFamily());
+ EXPECT_EQ(IPPROTO_UDP, ep->getProtocol());
+}
+
+TEST(IOEndpointTest, createTCPv6) {
+ const IOEndpoint* ep;
+ ep = IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1234"), 5303);
+ EXPECT_EQ("2001:db8::1234", ep->getAddress().toText());
+ EXPECT_EQ(5303, ep->getPort());
+ EXPECT_EQ(AF_INET6, ep->getFamily());
+ EXPECT_EQ(AF_INET6, ep->getAddress().getFamily());
+ EXPECT_EQ(IPPROTO_TCP, ep->getProtocol());
+}
+
+TEST(IOEndpointTest, createIPProto) {
+ EXPECT_THROW(IOEndpoint::create(IPPROTO_IP, IOAddress("192.0.2.1"),
+ 5300)->getAddress().toText(),
+ IOError);
+}
+
diff --git a/src/lib/asiolink/tests/iosocket_unittest.cc b/src/lib/asiolink/tests/iosocket_unittest.cc
new file mode 100644
index 0000000..06ae861
--- /dev/null
+++ b/src/lib/asiolink/tests/iosocket_unittest.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+#include <gtest/gtest.h>
+
+#include <asiolink/asiolink.h>
+
+using namespace asiolink;
+
+TEST(IOSocketTest, dummySockets) {
+ EXPECT_EQ(IPPROTO_UDP, IOSocket::getDummyUDPSocket().getProtocol());
+ EXPECT_EQ(IPPROTO_TCP, IOSocket::getDummyTCPSocket().getProtocol());
+ EXPECT_EQ(-1, IOSocket::getDummyUDPSocket().getNative());
+ EXPECT_EQ(-1, IOSocket::getDummyTCPSocket().getNative());
+}
+
+
diff --git a/src/lib/asiolink/tests/recursive_query_unittest.cc b/src/lib/asiolink/tests/recursive_query_unittest.cc
new file mode 100644
index 0000000..ad4e5b4
--- /dev/null
+++ b/src/lib/asiolink/tests/recursive_query_unittest.cc
@@ -0,0 +1,791 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+#include <sys/socket.h>
+#include <sys/time.h>
+
+#include <string.h>
+
+#include <boost/lexical_cast.hpp>
+#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+
+#include <gtest/gtest.h>
+
+#include <exceptions/exceptions.h>
+
+#include <dns/tests/unittest_util.h>
+#include <dns/rcode.h>
+
+#include <dns/buffer.h>
+#include <dns/message.h>
+
+// IMPORTANT: We shouldn't directly use ASIO definitions in this test.
+// In particular, we must not include asio.hpp in this file.
+// The asiolink module is primarily intended to be a wrapper that hide the
+// details of the underlying implementations. We need to test the wrapper
+// level behaviors. In addition, some compilers reject to compile this file
+// if we include asio.hpp unless we specify a special compiler option.
+// If we need to test something at the level of underlying ASIO and need
+// their definition, that test should go to asiolink/internal/tests.
+#include <asiolink/asiolink.h>
+#include <asiolink/io_socket.h>
+
+using isc::UnitTestUtil;
+using namespace std;
+using namespace asiolink;
+using namespace isc::dns;
+
+namespace {
+const char* const TEST_SERVER_PORT = "53535";
+const char* const TEST_CLIENT_PORT = "53536";
+const char* const TEST_IPV6_ADDR = "::1";
+const char* const TEST_IPV4_ADDR = "127.0.0.1";
+// This data is intended to be valid as a DNS/TCP-like message: the first
+// two octets encode the length of the rest of the data. This is crucial
+// for the tests below.
+const uint8_t test_data[] = {0, 4, 1, 2, 3, 4};
+
+// This function returns an addrinfo structure for use by tests, using
+// different addresses and ports depending on whether we're testing
+// IPv4 or v6, TCP or UDP, and client or server operation.
+struct addrinfo*
+resolveAddress(const int family, const int protocol, const bool client) {
+ const char* const addr = (family == AF_INET6) ?
+ TEST_IPV6_ADDR : TEST_IPV4_ADDR;
+ const char* const port = client ? TEST_CLIENT_PORT : TEST_SERVER_PORT;
+
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = family;
+ hints.ai_socktype = (protocol == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
+ hints.ai_protocol = protocol;
+ hints.ai_flags = AI_NUMERICSERV;
+
+ struct addrinfo* res;
+ const int error = getaddrinfo(addr, port, &hints, &res);
+ if (error != 0) {
+ isc_throw(IOError, "getaddrinfo failed: " << gai_strerror(error));
+ }
+
+ return (res);
+}
+
+// This fixture is a framework for various types of network operations
+// using the ASIO interfaces. Each test case creates an IOService object,
+// opens a local "client" socket for testing, sends data via the local socket
+// to the service that would run in the IOService object.
+// A mock callback function (an ASIOCallBack object) is registered with the
+// IOService object, so the test code should be able to examine the data
+// received on the server side. It then checks the received data matches
+// expected parameters.
+// If initialization parameters of the IOService should be modified, the test
+// case can do it using the setDNSService() method.
+// Note: the set of tests in RecursiveQueryTest use actual network services and may
+// involve undesirable side effects such as blocking.
+class RecursiveQueryTest : public ::testing::Test {
+protected:
+ RecursiveQueryTest();
+ ~RecursiveQueryTest() {
+ if (res_ != NULL) {
+ freeaddrinfo(res_);
+ }
+ if (sock_ != -1) {
+ close(sock_);
+ }
+ delete dns_service_;
+ delete callback_;
+ delete io_service_;
+ }
+
+ // Send a test UDP packet to a mock server
+ void sendUDP(const int family) {
+ res_ = resolveAddress(family, IPPROTO_UDP, false);
+
+ sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
+ if (sock_ < 0) {
+ isc_throw(IOError, "failed to open test socket");
+ }
+ const int cc = sendto(sock_, test_data, sizeof(test_data), 0,
+ res_->ai_addr, res_->ai_addrlen);
+ if (cc != sizeof(test_data)) {
+ isc_throw(IOError, "unexpected sendto result: " << cc);
+ }
+ io_service_->run();
+ }
+
+ // Send a test TCP packet to a mock server
+ void sendTCP(const int family) {
+ res_ = resolveAddress(family, IPPROTO_TCP, false);
+
+ sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
+ if (sock_ < 0) {
+ isc_throw(IOError, "failed to open test socket");
+ }
+ if (connect(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
+ isc_throw(IOError, "failed to connect to the test server");
+ }
+ const int cc = send(sock_, test_data, sizeof(test_data), 0);
+ if (cc != sizeof(test_data)) {
+ isc_throw(IOError, "unexpected send result: " << cc);
+ }
+ io_service_->run();
+ }
+
+ // Receive a UDP packet from a mock server; used for testing
+ // recursive lookup. The caller must place a RecursiveQuery
+ // on the IO Service queue before running this routine.
+ void recvUDP(const int family, void* buffer, size_t& size) {
+ res_ = resolveAddress(family, IPPROTO_UDP, true);
+
+ sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
+ if (sock_ < 0) {
+ isc_throw(IOError, "failed to open test socket");
+ }
+
+ if (bind(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
+ isc_throw(IOError, "bind failed: " << strerror(errno));
+ }
+
+ // The IO service queue should have a RecursiveQuery object scheduled
+ // to run at this point. This call will cause it to begin an
+ // async send, then return.
+ io_service_->run_one();
+
+ // ... and this one will block until the send has completed
+ io_service_->run_one();
+
+ // Now we attempt to recv() whatever was sent.
+ // XXX: there's no guarantee the receiving socket can immediately get
+ // the packet. Normally we can perform blocking recv to wait for it,
+ // but in theory it's even possible that the packet is lost.
+ // In order to prevent the test from hanging in such a worst case
+ // we add an ad hoc timeout.
+ const struct timeval timeo = { 10, 0 };
+ int recv_options = 0;
+ if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo,
+ sizeof(timeo))) {
+ if (errno == ENOPROTOOPT) {
+ // Workaround for Solaris: it doesn't accept SO_RCVTIMEO
+ // with the error of ENOPROTOOPT. Since this is a workaround
+ // for rare error cases anyway, we simply switch to the
+ // "don't wait" mode. If we still find an error in recv()
+ // can happen often we'll consider a more complete solution.
+ recv_options = MSG_DONTWAIT;
+ } else {
+ isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
+ }
+ }
+ const int ret = recv(sock_, buffer, size, recv_options);
+ if (ret < 0) {
+ isc_throw(IOError, "recvfrom failed: " << strerror(errno));
+ }
+
+ // Pass the message size back via the size parameter
+ size = ret;
+ }
+
+
+ // Set up an IO Service queue using the specified address
+ void setDNSService(const char& address) {
+ delete dns_service_;
+ dns_service_ = NULL;
+ delete io_service_;
+ io_service_ = new IOService();
+ callback_ = new ASIOCallBack(this);
+ dns_service_ = new DNSService(*io_service_, *TEST_SERVER_PORT, address, callback_, NULL, NULL);
+ }
+
+ // Set up an IO Service queue using the "any" address, on IPv4 if
+ // 'use_ipv4' is true and on IPv6 if 'use_ipv6' is true.
+ void setDNSService(const bool use_ipv4, const bool use_ipv6) {
+ delete dns_service_;
+ dns_service_ = NULL;
+ delete io_service_;
+ io_service_ = new IOService();
+ callback_ = new ASIOCallBack(this);
+ dns_service_ = new DNSService(*io_service_, *TEST_SERVER_PORT, use_ipv4, use_ipv6, callback_,
+ NULL, NULL);
+ }
+
+ // Set up empty DNS Service
+ // Set up an IO Service queue without any addresses
+ void setDNSService() {
+ delete dns_service_;
+ dns_service_ = NULL;
+ delete io_service_;
+ io_service_ = new IOService();
+ callback_ = new ASIOCallBack(this);
+ dns_service_ = new DNSService(*io_service_, callback_, NULL, NULL);
+ }
+
+ // Run a simple server test, on either IPv4 or IPv6, and over either
+ // UDP or TCP. Calls the sendUDP() or sendTCP() methods, which will
+ // start the IO Service queue. The UDPServer or TCPServer that was
+ // created by setIOService() will receive the test packet and issue a
+ // callback, which enables us to check that the data it received
+ // matches what we sent.
+ void doTest(const int family, const int protocol) {
+ if (protocol == IPPROTO_UDP) {
+ sendUDP(family);
+ } else {
+ sendTCP(family);
+ }
+
+ // There doesn't seem to be an effective test for the validity of
+ // 'native'.
+ // One thing we are sure is it must be different from our local socket.
+ EXPECT_NE(sock_, callback_native_);
+ EXPECT_EQ(protocol, callback_protocol_);
+ EXPECT_EQ(family == AF_INET6 ? TEST_IPV6_ADDR : TEST_IPV4_ADDR,
+ callback_address_);
+
+ const uint8_t* expected_data =
+ protocol == IPPROTO_UDP ? test_data : test_data + 2;
+ const size_t expected_datasize =
+ protocol == IPPROTO_UDP ? sizeof(test_data) :
+ sizeof(test_data) - 2;
+ EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, &callback_data_[0],
+ callback_data_.size(),
+ expected_data, expected_datasize);
+ }
+
+protected:
+ // This is a nonfunctional mockup of a DNSServer object. Its purpose
+ // is to resume after a recursive query or other asynchronous call
+ // has completed.
+ class MockServer : public DNSServer {
+ public:
+ explicit MockServer(IOService& io_service,
+ SimpleCallback* checkin = NULL,
+ DNSLookup* lookup = NULL,
+ DNSAnswer* answer = NULL) :
+ io_(io_service),
+ message_(new Message(Message::PARSE)),
+ answer_message_(new Message(Message::RENDER)),
+ respbuf_(new OutputBuffer(0)),
+ checkin_(checkin), lookup_(lookup), answer_(answer)
+ {}
+
+ void operator()(asio::error_code ec = asio::error_code(),
+ size_t length = 0)
+ {}
+
+ void resume(const bool) {
+ // should never be called in our tests
+ }
+
+ DNSServer* clone() {
+ MockServer* s = new MockServer(*this);
+ return (s);
+ }
+
+ inline void asyncLookup() {
+ if (lookup_) {
+ (*lookup_)(*io_message_, message_, answer_message_,
+ respbuf_, this);
+ }
+ }
+
+ protected:
+ IOService& io_;
+ bool done_;
+
+ private:
+ // Currently unused; these will be used for testing
+ // asynchronous lookup calls via the asyncLookup() method
+ boost::shared_ptr<asiolink::IOMessage> io_message_;
+ isc::dns::MessagePtr message_;
+ isc::dns::MessagePtr answer_message_;
+ isc::dns::OutputBufferPtr respbuf_;
+
+ // Callback functions provided by the caller
+ const SimpleCallback* checkin_;
+ const DNSLookup* lookup_;
+ const DNSAnswer* answer_;
+ };
+
+ // This version of mock server just stops the io_service when it is resumed
+ class MockServerStop : public MockServer {
+ public:
+ explicit MockServerStop(IOService& io_service, bool* done) :
+ MockServer(io_service),
+ done_(done)
+ {}
+
+ void resume(const bool done) {
+ *done_ = done;
+ io_.stop();
+ }
+
+ DNSServer* clone() {
+ return (new MockServerStop(*this));
+ }
+ private:
+ bool* done_;
+ };
+
+ // This version of mock server just stops the io_service when it is resumed
+ // the second time. (Used in the clientTimeout test, where resume
+ // is called initially with the error answer, and later when the
+ // lookup times out, it is called without an answer to send back)
+ class MockServerStop2 : public MockServer {
+ public:
+ explicit MockServerStop2(IOService& io_service,
+ bool* done1, bool* done2) :
+ MockServer(io_service),
+ done1_(done1),
+ done2_(done2),
+ stopped_once_(false)
+ {}
+
+ void resume(const bool done) {
+ if (stopped_once_) {
+ *done2_ = done;
+ io_.stop();
+ } else {
+ *done1_ = done;
+ stopped_once_ = true;
+ }
+ }
+
+ DNSServer* clone() {
+ return (new MockServerStop2(*this));
+ }
+ private:
+ bool* done1_;
+ bool* done2_;
+ bool stopped_once_;
+ };
+
+private:
+ class ASIOCallBack : public SimpleCallback {
+ public:
+ ASIOCallBack(RecursiveQueryTest* test_obj) : test_obj_(test_obj) {}
+ void operator()(const IOMessage& io_message) const {
+ test_obj_->callBack(io_message);
+ }
+ private:
+ RecursiveQueryTest* test_obj_;
+ };
+ void callBack(const IOMessage& io_message) {
+ callback_protocol_ = io_message.getSocket().getProtocol();
+ callback_native_ = io_message.getSocket().getNative();
+ callback_address_ =
+ io_message.getRemoteEndpoint().getAddress().toText();
+ callback_data_.assign(
+ static_cast<const uint8_t*>(io_message.getData()),
+ static_cast<const uint8_t*>(io_message.getData()) +
+ io_message.getDataSize());
+ io_service_->stop();
+ }
+protected:
+ // We use a pointer for io_service_, because for some tests we
+ // need to recreate a new one within one onstance of this class
+ IOService* io_service_;
+ DNSService* dns_service_;
+ ASIOCallBack* callback_;
+ int callback_protocol_;
+ int callback_native_;
+ string callback_address_;
+ vector<uint8_t> callback_data_;
+ int sock_;
+ struct addrinfo* res_;
+};
+
+RecursiveQueryTest::RecursiveQueryTest() :
+ dns_service_(NULL), callback_(NULL), sock_(-1), res_(NULL)
+{
+ io_service_ = new IOService();
+ setDNSService(true, true);
+}
+
+TEST_F(RecursiveQueryTest, v6UDPSend) {
+ doTest(AF_INET6, IPPROTO_UDP);
+}
+
+TEST_F(RecursiveQueryTest, v6TCPSend) {
+ doTest(AF_INET6, IPPROTO_TCP);
+}
+
+TEST_F(RecursiveQueryTest, v4UDPSend) {
+ doTest(AF_INET, IPPROTO_UDP);
+}
+
+TEST_F(RecursiveQueryTest, v4TCPSend) {
+ doTest(AF_INET, IPPROTO_TCP);
+}
+
+TEST_F(RecursiveQueryTest, v6UDPSendSpecific) {
+ // Explicitly set a specific address to be bound to the socket.
+ // The subsequent test does not directly ensures the underlying socket
+ // is bound to the expected address, but the success of the tests should
+ // reasonably suggest it works as intended.
+ // Specifying an address also implicitly means the service runs in a
+ // single address-family mode. In tests using TCP we can confirm that
+ // by trying to make a connection and seeing a failure. In UDP, it'd be
+ // more complicated because we need to use a connected socket and catch
+ // an error on a subsequent read operation. We could do it, but for
+ // simplicity we only tests the easier cases for now.
+
+ setDNSService(*TEST_IPV6_ADDR);
+ doTest(AF_INET6, IPPROTO_UDP);
+}
+
+TEST_F(RecursiveQueryTest, v6TCPSendSpecific) {
+ setDNSService(*TEST_IPV6_ADDR);
+ doTest(AF_INET6, IPPROTO_TCP);
+
+ EXPECT_THROW(sendTCP(AF_INET), IOError);
+}
+
+TEST_F(RecursiveQueryTest, v4UDPSendSpecific) {
+ setDNSService(*TEST_IPV4_ADDR);
+ doTest(AF_INET, IPPROTO_UDP);
+}
+
+TEST_F(RecursiveQueryTest, v4TCPSendSpecific) {
+ setDNSService(*TEST_IPV4_ADDR);
+ doTest(AF_INET, IPPROTO_TCP);
+
+ EXPECT_THROW(sendTCP(AF_INET6), IOError);
+}
+
+TEST_F(RecursiveQueryTest, v6AddServer) {
+ setDNSService();
+ dns_service_->addServer(*TEST_SERVER_PORT, TEST_IPV6_ADDR);
+ doTest(AF_INET6, IPPROTO_TCP);
+
+ EXPECT_THROW(sendTCP(AF_INET), IOError);
+}
+
+TEST_F(RecursiveQueryTest, v4AddServer) {
+ setDNSService();
+ dns_service_->addServer(*TEST_SERVER_PORT, TEST_IPV4_ADDR);
+ doTest(AF_INET, IPPROTO_TCP);
+
+ EXPECT_THROW(sendTCP(AF_INET6), IOError);
+}
+
+TEST_F(RecursiveQueryTest, DISABLED_clearServers) {
+ // FIXME: Enable when clearServers actually close the sockets
+ // See #388
+ setDNSService();
+ dns_service_->clearServers();
+
+ EXPECT_THROW(sendTCP(AF_INET), IOError);
+ EXPECT_THROW(sendTCP(AF_INET6), IOError);
+}
+
+TEST_F(RecursiveQueryTest, v6TCPOnly) {
+ // Open only IPv6 TCP socket. A subsequent attempt of establishing an
+ // IPv4/TCP connection should fail. See above for why we only test this
+ // for TCP.
+ setDNSService(false, true);
+ EXPECT_THROW(sendTCP(AF_INET), IOError);
+}
+
+TEST_F(RecursiveQueryTest, v4TCPOnly) {
+ setDNSService(true, false);
+ EXPECT_THROW(sendTCP(AF_INET6), IOError);
+}
+
+vector<pair<string, uint16_t> >
+singleAddress(const string &address, uint16_t port) {
+ vector<pair<string, uint16_t> > result;
+ result.push_back(pair<string, uint16_t>(address, port));
+ return (result);
+}
+
+TEST_F(RecursiveQueryTest, recursiveSetupV4) {
+ setDNSService(true, false);
+ uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
+ EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
+ singleAddress(TEST_IPV4_ADDR, port),
+ singleAddress(TEST_IPV4_ADDR, port)));
+}
+
+TEST_F(RecursiveQueryTest, recursiveSetupV6) {
+ setDNSService(false, true);
+ uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
+ EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
+ singleAddress(TEST_IPV6_ADDR, port),
+ singleAddress(TEST_IPV6_ADDR,port)));
+}
+
+// XXX:
+// This is very inadequate unit testing. It should be generalized into
+// a routine that can do this with variable address family, address, and
+// port, and with the various callbacks defined in such a way as to ensure
+// full code coverage including error cases.
+TEST_F(RecursiveQueryTest, forwarderSend) {
+ setDNSService(true, false);
+
+ // Note: We use the test prot plus one to ensure we aren't binding
+ // to the same port as the actual server
+ uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
+
+ MockServer server(*io_service_);
+ RecursiveQuery rq(*dns_service_,
+ singleAddress(TEST_IPV4_ADDR, port),
+ singleAddress(TEST_IPV4_ADDR, port));
+
+ Question q(Name("example.com"), RRClass::IN(), RRType::TXT());
+ OutputBufferPtr buffer(new OutputBuffer(0));
+ MessagePtr answer(new Message(Message::RENDER));
+ rq.resolve(q, answer, buffer, &server);
+
+ char data[4096];
+ size_t size = sizeof(data);
+ ASSERT_NO_THROW(recvUDP(AF_INET, data, size));
+
+ Message m(Message::PARSE);
+ InputBuffer ibuf(data, size);
+
+ // Make sure we can parse the message that was sent
+ EXPECT_NO_THROW(m.parseHeader(ibuf));
+ EXPECT_NO_THROW(m.fromWire(ibuf));
+
+ // Check that the question sent matches the one we wanted
+ QuestionPtr q2 = *m.beginQuestion();
+ EXPECT_EQ(q.getName(), q2->getName());
+ EXPECT_EQ(q.getType(), q2->getType());
+ EXPECT_EQ(q.getClass(), q2->getClass());
+}
+
+int
+createTestSocket()
+{
+ struct addrinfo* res_ = resolveAddress(AF_INET, IPPROTO_UDP, true);
+ int sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
+ if (sock_ < 0) {
+ isc_throw(IOError, "failed to open test socket");
+ }
+ if (bind(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
+ isc_throw(IOError, "failed to bind test socket");
+ }
+ return sock_;
+}
+
+int
+setSocketTimeout(int sock_, size_t tv_sec, size_t tv_usec) {
+ const struct timeval timeo = { tv_sec, tv_usec };
+ int recv_options = 0;
+ if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo))) {
+ if (errno == ENOPROTOOPT) { // see RecursiveQueryTest::recvUDP()
+ recv_options = MSG_DONTWAIT;
+ } else {
+ isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
+ }
+ }
+ return recv_options;
+}
+
+// try to read from the socket max time
+// *num is incremented for every succesfull read
+// returns true if it can read max times, false otherwise
+bool tryRead(int sock_, int recv_options, size_t max, int* num) {
+ size_t i = 0;
+ do {
+ char inbuff[512];
+ if (recv(sock_, inbuff, sizeof(inbuff), recv_options) < 0) {
+ return false;
+ } else {
+ ++i;
+ ++*num;
+ }
+ } while (i < max);
+ return true;
+}
+
+
+// Test it tries the correct amount of times before giving up
+TEST_F(RecursiveQueryTest, forwardQueryTimeout) {
+ // Prepare the service (we do not use the common setup, we do not answer
+ setDNSService();
+
+ // Prepare the socket
+ sock_ = createTestSocket();
+
+ // Prepare the server
+ bool done(true);
+ MockServerStop server(*io_service_, &done);
+
+ // Do the answer
+ const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
+ RecursiveQuery query(*dns_service_,
+ singleAddress(TEST_IPV4_ADDR, port),
+ singleAddress(TEST_IPV4_ADDR, port),
+ 10, 4000, 3000, 2);
+ Question question(Name("example.net"), RRClass::IN(), RRType::A());
+ OutputBufferPtr buffer(new OutputBuffer(0));
+ MessagePtr answer(new Message(Message::RENDER));
+ query.resolve(question, answer, buffer, &server);
+
+ // Run the test
+ io_service_->run();
+
+ // Read up to 3 packets. Use some ad hoc timeout to prevent an infinite
+ // block (see also recvUDP()).
+ int recv_options = setSocketTimeout(sock_, 10, 0);
+ int num = 0;
+ bool read_success = tryRead(sock_, recv_options, 3, &num);
+
+ // The query should fail
+ EXPECT_FALSE(done);
+ EXPECT_EQ(3, num);
+ EXPECT_TRUE(read_success);
+}
+
+// If we set client timeout to lower than querytimeout, we should
+// get a failure answer, but still see retries
+// (no actual answer is given here yet)
+TEST_F(RecursiveQueryTest, forwardClientTimeout) {
+ // Prepare the service (we do not use the common setup, we do not answer
+ setDNSService();
+
+ sock_ = createTestSocket();
+
+ // Prepare the server
+ bool done1(true);
+ bool done2(true);
+ MockServerStop2 server(*io_service_, &done1, &done2);
+
+ MessagePtr answer(new Message(Message::RENDER));
+
+ // Do the answer
+ const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
+ // Set it up to retry twice before client timeout fires
+ // Since the lookup timer has not fired, it should retry
+ // four times
+ RecursiveQuery query(*dns_service_,
+ singleAddress(TEST_IPV4_ADDR, port),
+ singleAddress(TEST_IPV4_ADDR, port),
+ 50, 120, 1000, 4);
+ Question question(Name("example.net"), RRClass::IN(), RRType::A());
+ OutputBufferPtr buffer(new OutputBuffer(0));
+ query.resolve(question, answer, buffer, &server);
+
+ // Run the test
+ io_service_->run();
+
+ // we know it'll fail, so make it a shorter timeout
+ int recv_options = setSocketTimeout(sock_, 1, 0);
+
+ // Try to read 5 times
+ int num = 0;
+ bool read_success = tryRead(sock_, recv_options, 5, &num);
+
+ // The query should fail, but we should have kept on trying
+ EXPECT_TRUE(done1);
+ EXPECT_FALSE(done2);
+ EXPECT_EQ(5, num);
+ EXPECT_TRUE(read_success);
+}
+
+// If we set lookup timeout to lower than querytimeout*retries, we should
+// fail before the full amount of retries
+TEST_F(RecursiveQueryTest, forwardLookupTimeout) {
+ // Prepare the service (we do not use the common setup, we do not answer
+ setDNSService();
+
+ // Prepare the socket
+ sock_ = createTestSocket();
+
+ // Prepare the server
+ bool done(true);
+ MockServerStop server(*io_service_, &done);
+
+ MessagePtr answer(new Message(Message::RENDER));
+
+ // Do the answer
+ const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
+ // Set up the test so that it will retry 5 times, but the lookup
+ // timeout will fire after only 3 normal timeouts
+ RecursiveQuery query(*dns_service_,
+ singleAddress(TEST_IPV4_ADDR, port),
+ singleAddress(TEST_IPV4_ADDR, port),
+ 50, 4000, 120, 5);
+ Question question(Name("example.net"), RRClass::IN(), RRType::A());
+ OutputBufferPtr buffer(new OutputBuffer(0));
+ query.resolve(question, answer, buffer, &server);
+
+ // Run the test
+ io_service_->run();
+
+ int recv_options = setSocketTimeout(sock_, 1, 0);
+
+ // Try to read 5 times, should stop after 3 reads
+ int num = 0;
+ bool read_success = tryRead(sock_, recv_options, 5, &num);
+
+ // The query should fail
+ EXPECT_FALSE(done);
+ EXPECT_EQ(3, num);
+ EXPECT_FALSE(read_success);
+}
+
+// as mentioned above, we need a more better framework for this,
+// in addition to that, this sends out queries into the world
+// (which we should catch somehow and fake replies for)
+// for the skeleton code, it shouldn't be too much of a problem
+// Ok so even we don't all have access to the DNS world right now,
+// so disabling these tests too.
+TEST_F(RecursiveQueryTest, DISABLED_recursiveSendOk) {
+ setDNSService(true, false);
+ bool done;
+
+ MockServerStop server(*io_service_, &done);
+ vector<pair<string, uint16_t> > empty_vector;
+ RecursiveQuery rq(*dns_service_, empty_vector, empty_vector, 10000, 0);
+
+ Question q(Name("www.isc.org"), RRClass::IN(), RRType::A());
+ OutputBufferPtr buffer(new OutputBuffer(0));
+ MessagePtr answer(new Message(Message::RENDER));
+ rq.resolve(q, answer, buffer, &server);
+ io_service_->run();
+
+ // Check that the answer we got matches the one we wanted
+ EXPECT_EQ(Rcode::NOERROR(), answer->getRcode());
+ ASSERT_EQ(1, answer->getRRCount(Message::SECTION_ANSWER));
+ RRsetPtr a = *answer->beginSection(Message::SECTION_ANSWER);
+ EXPECT_EQ(q.getName(), a->getName());
+ EXPECT_EQ(q.getType(), a->getType());
+ EXPECT_EQ(q.getClass(), a->getClass());
+ EXPECT_EQ(1, a->getRdataCount());
+}
+
+// see comments at previous test
+TEST_F(RecursiveQueryTest, DISABLED_recursiveSendNXDOMAIN) {
+ setDNSService(true, false);
+ bool done;
+
+ MockServerStop server(*io_service_, &done);
+ vector<pair<string, uint16_t> > empty_vector;
+ RecursiveQuery rq(*dns_service_, empty_vector, empty_vector, 10000, 0);
+
+ Question q(Name("wwwdoesnotexist.isc.org"), RRClass::IN(), RRType::A());
+ OutputBufferPtr buffer(new OutputBuffer(0));
+ MessagePtr answer(new Message(Message::RENDER));
+ rq.resolve(q, answer, buffer, &server);
+ io_service_->run();
+
+ // Check that the answer we got matches the one we wanted
+ EXPECT_EQ(Rcode::NXDOMAIN(), answer->getRcode());
+ EXPECT_EQ(0, answer->getRRCount(Message::SECTION_ANSWER));
+}
+
+}
diff --git a/src/lib/asiolink/tests/udp_query_unittest.cc b/src/lib/asiolink/tests/udp_query_unittest.cc
new file mode 100644
index 0000000..9eb1aba
--- /dev/null
+++ b/src/lib/asiolink/tests/udp_query_unittest.cc
@@ -0,0 +1,145 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <gtest/gtest.h>
+#include <asio.hpp>
+#include <boost/bind.hpp>
+#include <cstdlib>
+
+#include <dns/question.h>
+
+#include <asiolink/udp_query.h>
+
+using namespace asio;
+using namespace isc::dns;
+using asio::ip::udp;
+
+namespace {
+
+const asio::ip::address TEST_HOST(asio::ip::address::from_string("127.0.0.1"));
+const uint16_t TEST_PORT(5301);
+// FIXME Shouldn't we send something that is real message?
+const char TEST_DATA[] = "TEST DATA";
+
+// Test fixture for the asiolink::UDPQuery.
+class UDPQueryTest : public ::testing::Test,
+ public asiolink::UDPQuery::Callback
+{
+ public:
+ // Expected result of the callback
+ asiolink::UDPQuery::Result expected_;
+ // Did the callback run already?
+ bool run_;
+ // We use an io_service to run the query
+ io_service service_;
+ // Something to ask
+ Question question_;
+ // Buffer where the UDPQuery will store response
+ OutputBufferPtr buffer_;
+ // The query we are testing
+ asiolink::UDPQuery query_;
+
+ UDPQueryTest() :
+ run_(false),
+ question_(Name("example.net"), RRClass::IN(), RRType::A()),
+ buffer_(new OutputBuffer(512)),
+ query_(service_, question_, asiolink::IOAddress(TEST_HOST),
+ TEST_PORT, buffer_, this, 100)
+ { }
+
+ // This is the callback's (), so it can be called.
+ void operator()(asiolink::UDPQuery::Result result) {
+ // We check the query returns the correct result
+ EXPECT_EQ(expected_, result);
+ // Check it is called only once
+ EXPECT_FALSE(run_);
+ // And mark the callback was called
+ run_ = true;
+ }
+ // A response handler, pretending to be remote DNS server
+ void respond(udp::endpoint* remote, udp::socket* socket) {
+ // Some data came, just send something back.
+ socket->send_to(asio::buffer(TEST_DATA, sizeof TEST_DATA),
+ *remote);
+ socket->close();
+ }
+};
+
+/*
+ * Test that when we run the query and stop it after it was run,
+ * it returns "stopped" correctly.
+ *
+ * That is why stop() is posted to the service_ as well instead
+ * of calling it.
+ */
+TEST_F(UDPQueryTest, stop) {
+ expected_ = asiolink::UDPQuery::STOPPED;
+ // Post the query
+ service_.post(query_);
+ // Post query_.stop() (yes, the boost::bind thing is just
+ // query_.stop()).
+ service_.post(boost::bind(&asiolink::UDPQuery::stop, query_,
+ asiolink::UDPQuery::STOPPED));
+ // Run both of them
+ service_.run();
+ EXPECT_TRUE(run_);
+}
+
+/*
+ * Test that when we queue the query to service_ and call stop()
+ * before it gets executed, it acts sanely as well (eg. has the
+ * same result as running stop() after - calls the callback).
+ */
+TEST_F(UDPQueryTest, prematureStop) {
+ expected_ = asiolink::UDPQuery::STOPPED;
+ // Stop before it is started
+ query_.stop();
+ service_.post(query_);
+ service_.run();
+ EXPECT_TRUE(run_);
+}
+
+/*
+ * Test that it will timeout when no answer will arrive.
+ */
+TEST_F(UDPQueryTest, timeout) {
+ expected_ = asiolink::UDPQuery::TIME_OUT;
+ service_.post(query_);
+ service_.run();
+ EXPECT_TRUE(run_);
+}
+
+/*
+ * Test that it will succeed when we fake an answer and
+ * stores the same data we send.
+ *
+ * This is done through a real socket on loopback address.
+ */
+TEST_F(UDPQueryTest, receive) {
+ expected_ = asiolink::UDPQuery::SUCCESS;
+ udp::socket socket(service_, udp::v4());
+ socket.set_option(socket_base::reuse_address(true));
+ socket.bind(udp::endpoint(TEST_HOST, TEST_PORT));
+ char inbuff[512];
+ udp::endpoint remote;
+ socket.async_receive_from(asio::buffer(inbuff, 512), remote, boost::bind(
+ &UDPQueryTest::respond, this, &remote, &socket));
+ service_.post(query_);
+ service_.run();
+ EXPECT_TRUE(run_);
+ ASSERT_EQ(sizeof TEST_DATA, buffer_->getLength());
+ EXPECT_EQ(0, memcmp(TEST_DATA, buffer_->getData(), sizeof TEST_DATA));
+}
+
+}
diff --git a/src/lib/asiolink/udp_endpoint.h b/src/lib/asiolink/udp_endpoint.h
new file mode 100644
index 0000000..27541e0
--- /dev/null
+++ b/src/lib/asiolink/udp_endpoint.h
@@ -0,0 +1,89 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __UDP_ENDPOINT_H
+#define __UDP_ENDPOINT_H 1
+
+#ifndef ASIO_HPP
+#error "asio.hpp must be included before including this, see asiolink.h as to why"
+#endif
+
+#include <asiolink/io_endpoint.h>
+
+namespace asiolink {
+
+/// \brief The \c UDPEndpoint class is a concrete derived class of
+/// \c IOEndpoint that represents an endpoint of a UDP packet.
+///
+/// Other notes about \c TCPEndpoint applies to this class, too.
+class UDPEndpoint : public IOEndpoint {
+public:
+ ///
+ /// \name Constructors and Destructor.
+ ///
+ //@{
+ /// \brief Constructor from a pair of address and port.
+ ///
+ /// \param address The IP address of the endpoint.
+ /// \param port The UDP port number of the endpoint.
+ UDPEndpoint(const IOAddress& address, const unsigned short port) :
+ asio_endpoint_placeholder_(
+ new asio::ip::udp::endpoint(asio::ip::address::from_string(address.toText()),
+ port)),
+ asio_endpoint_(*asio_endpoint_placeholder_)
+ {}
+
+ /// \brief Constructor from an ASIO UDP endpoint.
+ ///
+ /// This constructor is designed to be an efficient wrapper for the
+ /// corresponding ASIO class, \c udp::endpoint.
+ ///
+ /// \param asio_endpoint The ASIO representation of the UDP endpoint.
+ UDPEndpoint(const asio::ip::udp::endpoint& asio_endpoint) :
+ asio_endpoint_placeholder_(NULL), asio_endpoint_(asio_endpoint)
+ {}
+
+ /// \brief The destructor.
+ ~UDPEndpoint() { delete asio_endpoint_placeholder_; }
+ //@}
+
+ inline IOAddress getAddress() const {
+ return (asio_endpoint_.address());
+ }
+
+ inline uint16_t getPort() const {
+ return (asio_endpoint_.port());
+ }
+
+ inline short getProtocol() const {
+ return (asio_endpoint_.protocol().protocol());
+ }
+
+ inline short getFamily() const {
+ return (asio_endpoint_.protocol().family());
+ }
+
+ // This is not part of the exosed IOEndpoint API but allows
+ // direct access to the ASIO implementation of the endpoint
+ inline const asio::ip::udp::endpoint& getASIOEndpoint() const {
+ return (asio_endpoint_);
+ }
+
+private:
+ const asio::ip::udp::endpoint* asio_endpoint_placeholder_;
+ const asio::ip::udp::endpoint& asio_endpoint_;
+};
+
+} // namespace asiolink
+#endif // __UDP_ENDPOINT_H
diff --git a/src/lib/asiolink/udp_query.cc b/src/lib/asiolink/udp_query.cc
new file mode 100644
index 0000000..a793814
--- /dev/null
+++ b/src/lib/asiolink/udp_query.cc
@@ -0,0 +1,189 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+// unistd is needed for asio.hpp with SunStudio
+#include <unistd.h>
+
+#include <asio.hpp>
+
+#include <boost/bind.hpp>
+#include <boost/shared_array.hpp>
+
+#include <dns/messagerenderer.h>
+#include <dns/opcode.h>
+#include <dns/rcode.h>
+
+#include <log/dummylog.h>
+
+#include <asio.hpp>
+
+#include <asiolink.h>
+
+#include <coroutine.h>
+#include <asiolink/udp_endpoint.h>
+
+#include <asiolink/udp_query.h>
+
+using namespace asio;
+using asio::ip::udp;
+using asio::ip::tcp;
+using isc::log::dlog;
+
+using namespace std;
+using namespace isc::dns;
+
+namespace asiolink {
+
+
+// Private UDPQuery data (see internal/udpdns.h for reasons)
+struct UDPQuery::PrivateData {
+ // Socket we send query to and expect reply from there
+ udp::socket socket;
+ // Where was the query sent
+ udp::endpoint remote;
+ // What we ask the server
+ Question question;
+ // We will store the answer here
+ OutputBufferPtr buffer;
+ OutputBufferPtr msgbuf;
+ // Temporary buffer for answer
+ boost::shared_array<char> data;
+ // This will be called when the data arrive or timeouts
+ Callback* callback;
+ // Did we already stop operating (data arrived, we timed out, someone
+ // called stop). This can be so when we are cleaning up/there are
+ // still pointers to us.
+ bool stopped;
+ // Timer to measure timeouts.
+ deadline_timer timer;
+ // How many milliseconds are we willing to wait for answer?
+ int timeout;
+
+ PrivateData(io_service& service,
+ const udp::socket::protocol_type& protocol, const Question &q,
+ OutputBufferPtr b, Callback *c) :
+ socket(service, protocol),
+ question(q),
+ buffer(b),
+ msgbuf(new OutputBuffer(512)),
+ callback(c),
+ stopped(false),
+ timer(service)
+ { }
+};
+
+/// The following functions implement the \c UDPQuery class.
+///
+/// The constructor
+UDPQuery::UDPQuery(io_service& io_service,
+ const Question& q, const IOAddress& addr, uint16_t port,
+ OutputBufferPtr buffer, Callback *callback, int timeout) :
+ data_(new PrivateData(io_service,
+ addr.getFamily() == AF_INET ? udp::v4() : udp::v6(), q, buffer,
+ callback))
+{
+ data_->remote = UDPEndpoint(addr, port).getASIOEndpoint();
+ data_->timeout = timeout;
+}
+
+/// The function operator is implemented with the "stackless coroutine"
+/// pattern; see internal/coroutine.h for details.
+void
+UDPQuery::operator()(error_code ec, size_t length) {
+ if (ec || data_->stopped) {
+ return;
+ }
+
+ CORO_REENTER (this) {
+ /// Generate the upstream query and render it to wire format
+ /// This is done in a different scope to allow inline variable
+ /// declarations.
+ {
+ Message msg(Message::RENDER);
+
+ // XXX: replace with boost::random or some other suitable PRNG
+ msg.setQid(0);
+ msg.setOpcode(Opcode::QUERY());
+ msg.setRcode(Rcode::NOERROR());
+ msg.setHeaderFlag(Message::HEADERFLAG_RD);
+ msg.addQuestion(data_->question);
+ MessageRenderer renderer(*data_->msgbuf);
+ msg.toWire(renderer);
+ dlog("Sending " + msg.toText() + " to " +
+ data_->remote.address().to_string());
+ }
+
+
+ // If we timeout, we stop, which will shutdown everything and
+ // cancel all other attempts to run inside the coroutine
+ if (data_->timeout != -1) {
+ data_->timer.expires_from_now(boost::posix_time::milliseconds(
+ data_->timeout));
+ data_->timer.async_wait(boost::bind(&UDPQuery::stop, *this,
+ TIME_OUT));
+ }
+
+ // Begin an asynchronous send, and then yield. When the
+ // send completes, we will resume immediately after this point.
+ CORO_YIELD data_->socket.async_send_to(buffer(data_->msgbuf->getData(),
+ data_->msgbuf->getLength()), data_->remote, *this);
+
+ /// Allocate space for the response. (XXX: This should be
+ /// optimized by maintaining a free list of pre-allocated blocks)
+ data_->data.reset(new char[MAX_LENGTH]);
+
+ /// Begin an asynchronous receive, and yield. When the receive
+ /// completes, we will resume immediately after this point.
+ CORO_YIELD data_->socket.async_receive_from(buffer(data_->data.get(),
+ MAX_LENGTH), data_->remote, *this);
+ // The message is not rendered yet, so we can't print it easilly
+ dlog("Received response from " + data_->remote.address().to_string());
+
+ /// Copy the answer into the response buffer. (XXX: If the
+ /// OutputBuffer object were made to meet the requirements of
+ /// a MutableBufferSequence, then it could be written to directly
+ /// by async_recieve_from() and this additional copy step would
+ /// be unnecessary.)
+ data_->buffer->writeData(data_->data.get(), length);
+
+ /// We are done
+ stop(SUCCESS);
+ }
+}
+
+void
+UDPQuery::stop(Result result) {
+ if (!data_->stopped) {
+ switch (result) {
+ case TIME_OUT:
+ dlog("Query timed out");
+ break;
+ case STOPPED:
+ dlog("Query stopped");
+ break;
+ default:;
+ }
+ data_->stopped = true;
+ data_->socket.cancel();
+ data_->socket.close();
+ data_->timer.cancel();
+ if (data_->callback) {
+ (*data_->callback)(result);
+ }
+ }
+}
+
+} // namespace asiolink
diff --git a/src/lib/asiolink/udp_query.h b/src/lib/asiolink/udp_query.h
new file mode 100644
index 0000000..3ed44ad
--- /dev/null
+++ b/src/lib/asiolink/udp_query.h
@@ -0,0 +1,88 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __UDP_QUERY_H
+#define __UDP_QUERY_H 1
+
+#ifndef ASIO_HPP
+#error "asio.hpp must be included before including this, see asiolink.h as to why"
+#endif
+
+#include <dns/buffer.h>
+
+#include <asiolink/io_address.h>
+#include <coroutine.h>
+
+namespace asiolink {
+
+//
+// Asynchronous UDP coroutine for upstream queries
+//
+class UDPQuery : public coroutine {
+public:
+ // TODO Maybe this should be more generic than just for UDPQuery?
+ ///
+ /// \brief Result of the query
+ ///
+ /// This is related only to contacting the remote server. If the answer
+ ///indicates error, it is still counted as SUCCESS here, if it comes back.
+ ///
+ enum Result {
+ SUCCESS,
+ TIME_OUT,
+ STOPPED
+ };
+ /// Abstract callback for the UDPQuery.
+ class Callback {
+ public:
+ virtual ~Callback() {}
+
+ /// This will be called when the UDPQuery is completed
+ virtual void operator()(Result result) = 0;
+ };
+ ///
+ /// \brief Constructor.
+ ///
+ /// It creates the query.
+ /// @param callback will be called when we terminate. It is your task to
+ /// delete it if allocated on heap.
+ ///@param timeout in ms.
+ ///
+ explicit UDPQuery(asio::io_service& io_service,
+ const isc::dns::Question& q,
+ const IOAddress& addr, uint16_t port,
+ isc::dns::OutputBufferPtr buffer,
+ Callback* callback, int timeout = -1);
+ void operator()(asio::error_code ec = asio::error_code(),
+ size_t length = 0);
+ /// Terminate the query.
+ void stop(Result reason = STOPPED);
+private:
+ enum { MAX_LENGTH = 4096 };
+
+ ///
+ /// \short Private data
+ ///
+ /// They are not private because of stability of the
+ /// interface (this is private class anyway), but because this class
+ /// will be copyed often (it is used as a coroutine and passed as callback
+ /// to many async_*() functions) and we want keep the same data. Some of
+ /// the data is not copyable too.
+ ///
+ struct PrivateData;
+ boost::shared_ptr<PrivateData> data_;
+};
+
+} // namespace asiolink
+#endif // __UDP_QUERY_H
diff --git a/src/lib/asiolink/udp_server.cc b/src/lib/asiolink/udp_server.cc
new file mode 100644
index 0000000..9a18d76
--- /dev/null
+++ b/src/lib/asiolink/udp_server.cc
@@ -0,0 +1,284 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+#include <boost/shared_array.hpp>
+
+// unistd is needed for asio.hpp with SunStudio
+#include <unistd.h>
+
+#include <asio.hpp>
+
+#include <log/dummylog.h>
+
+#include <asiolink/udp_endpoint.h>
+#include <asiolink/udp_socket.h>
+
+#include <asiolink/udp_server.h>
+
+using namespace asio;
+using asio::ip::udp;
+using asio::ip::tcp;
+using isc::log::dlog;
+
+using namespace std;
+using namespace isc::dns;
+
+namespace asiolink {
+
+/*
+ * Some of the member variables here are shared_ptrs and some are
+ * auto_ptrs. There will be one instance of Data for the lifetime
+ * of packet. The variables that are state only for a single packet
+ * use auto_ptr, as it is more lightweight. In the case of shared
+ * configuration (eg. the callbacks, socket), we use shared_ptrs.
+ */
+struct UDPServer::Data {
+ /*
+ * Constructor from parameters passed to UDPServer constructor.
+ * This instance will not be used to retrieve and answer the actual
+ * query, it will only hold parameters until we wait for the
+ * first packet. But we do initialize the socket in here.
+ */
+ Data(io_service& io_service, const ip::address& addr, const uint16_t port,
+ SimpleCallback* checkin, DNSLookup* lookup, DNSAnswer* answer) :
+ io_(io_service), done_(false), checkin_callback_(checkin),
+ lookup_callback_(lookup), answer_callback_(answer)
+ {
+ // We must use different instantiations for v4 and v6;
+ // otherwise ASIO will bind to both
+ udp proto = addr.is_v4() ? udp::v4() : udp::v6();
+ socket_.reset(new udp::socket(io_service, proto));
+ socket_->set_option(socket_base::reuse_address(true));
+ if (addr.is_v6()) {
+ socket_->set_option(asio::ip::v6_only(true));
+ }
+ socket_->bind(udp::endpoint(addr, port));
+ }
+
+ /*
+ * Copy constructor. Default one would probably do, but it is unnecessary
+ * to copy many of the member variables every time we fork to handle
+ * another packet.
+ *
+ * We also allocate data for receiving the packet here.
+ */
+ Data(const Data& other) :
+ io_(other.io_), socket_(other.socket_), done_(false),
+ checkin_callback_(other.checkin_callback_),
+ lookup_callback_(other.lookup_callback_),
+ answer_callback_(other.answer_callback_)
+ {
+ // Instantiate the data buffer and endpoint that will
+ // be used by the asynchronous receive call.
+ data_.reset(new char[MAX_LENGTH]);
+ sender_.reset(new udp::endpoint());
+ }
+
+ // The ASIO service object
+ asio::io_service& io_;
+
+ // Class member variables which are dynamic, and changes to which
+ // need to accessible from both sides of a coroutine fork or from
+ // outside of the coroutine (i.e., from an asynchronous I/O call),
+ // should be declared here as pointers and allocated in the
+ // constructor or in the coroutine. This allows state information
+ // to persist when an individual copy of the coroutine falls out
+ // scope while waiting for an event, *so long as* there is another
+ // object that is referencing the same data. As a side-benefit, using
+ // pointers also reduces copy overhead for coroutine objects.
+ //
+ // Note: Currently these objects are allocated by "new" in the
+ // constructor, or in the function operator while processing a query.
+ // Repeated allocations from the heap for every incoming query is
+ // clearly a performance issue; this must be optimized in the future.
+ // The plan is to have a structure pre-allocate several "Data"
+ // objects which can be pulled off a free list and placed on an in-use
+ // list whenever a query comes in. This will serve the dual purpose
+ // of improving performance and guaranteeing that state information
+ // will *not* be destroyed when any one instance of the coroutine
+ // falls out of scope while waiting for an event.
+ //
+ // Socket used to for listen for queries. Created in the
+ // constructor and stored in a shared_ptr because socket objects
+ // are not copyable.
+ boost::shared_ptr<asio::ip::udp::socket> socket_;
+
+ // The ASIO-internal endpoint object representing the client
+ std::auto_ptr<asio::ip::udp::endpoint> sender_;
+
+ // \c IOMessage and \c Message objects to be passed to the
+ // DNS lookup and answer providers
+ std::auto_ptr<asiolink::IOMessage> io_message_;
+
+ // The original query as sent by the client
+ isc::dns::MessagePtr query_message_;
+
+ // The response message we are building
+ isc::dns::MessagePtr answer_message_;
+
+ // The buffer into which the response is written
+ isc::dns::OutputBufferPtr respbuf_;
+
+ // The buffer into which the query packet is written
+ boost::shared_array<char> data_;
+
+ // State information that is entirely internal to a given instance
+ // of the coroutine can be declared here.
+ size_t bytes_;
+ bool done_;
+
+ // Callback functions provided by the caller
+ const SimpleCallback* checkin_callback_;
+ const DNSLookup* lookup_callback_;
+ const DNSAnswer* answer_callback_;
+
+ std::auto_ptr<IOEndpoint> peer_;
+ std::auto_ptr<IOSocket> iosock_;
+};
+
+/// The following functions implement the \c UDPServer class.
+///
+/// The constructor. It just creates new internal state object
+/// and lets it handle the initialization.
+UDPServer::UDPServer(io_service& io_service, const ip::address& addr,
+ const uint16_t port, SimpleCallback* checkin, DNSLookup* lookup,
+ DNSAnswer* answer) :
+ data_(new Data(io_service, addr, port, checkin, lookup, answer))
+{ }
+
+/// The function operator is implemented with the "stackless coroutine"
+/// pattern; see internal/coroutine.h for details.
+void
+UDPServer::operator()(error_code ec, size_t length) {
+ /// Because the coroutine reeentry block is implemented as
+ /// a switch statement, inline variable declarations are not
+ /// permitted. Certain variables used below can be declared here.
+
+ CORO_REENTER (this) {
+ do {
+ /*
+ * This is preparation for receiving a packet. We get a new
+ * state object for the lifetime of the next packet to come.
+ * It allocates the buffers to receive data into.
+ */
+ data_.reset(new Data(*data_));
+
+ do {
+ // Begin an asynchronous receive, then yield.
+ // When the receive event is posted, the coroutine
+ // will resume immediately after this point.
+ CORO_YIELD data_->socket_->async_receive_from(
+ buffer(data_->data_.get(), MAX_LENGTH), *data_->sender_,
+ *this);
+ } while (ec || length == 0);
+
+ data_->bytes_ = length;
+
+ /*
+ * We fork the coroutine now. One (the child) will keep
+ * the current state and handle the packet, then die and
+ * drop ownership of the state. The other (parent) will just
+ * go into the loop again and replace the current state with
+ * a new one for a new object.
+ *
+ * Actually, both of the coroutines will be a copy of this
+ * one, but that's just internal implementation detail.
+ */
+ CORO_FORK data_->io_.post(UDPServer(*this));
+ } while (is_parent());
+
+ // Create an \c IOMessage object to store the query.
+ //
+ // (XXX: It would be good to write a factory function
+ // that would quickly generate an IOMessage object without
+ // all these calls to "new".)
+ data_->peer_.reset(new UDPEndpoint(*data_->sender_));
+ data_->iosock_.reset(new UDPSocket(*data_->socket_));
+ data_->io_message_.reset(new IOMessage(data_->data_.get(),
+ data_->bytes_, *data_->iosock_, *data_->peer_));
+
+ // Perform any necessary operations prior to processing an incoming
+ // query (e.g., checking for queued configuration messages).
+ //
+ // (XXX: it may be a performance issue to check in for every single
+ // incoming query; we may wish to throttle this in the future.)
+ if (data_->checkin_callback_ != NULL) {
+ (*data_->checkin_callback_)(*data_->io_message_);
+ }
+
+ // If we don't have a DNS Lookup provider, there's no point in
+ // continuing; we exit the coroutine permanently.
+ if (data_->lookup_callback_ == NULL) {
+ CORO_YIELD return;
+ }
+
+ // Instantiate objects that will be needed by the
+ // asynchronous DNS lookup and/or by the send call.
+ data_->respbuf_.reset(new OutputBuffer(0));
+ data_->query_message_.reset(new Message(Message::PARSE));
+ data_->answer_message_.reset(new Message(Message::RENDER));
+
+ // Schedule a DNS lookup, and yield. When the lookup is
+ // finished, the coroutine will resume immediately after
+ // this point.
+ CORO_YIELD data_->io_.post(AsyncLookup<UDPServer>(*this));
+
+ dlog("[XX] got an answer");
+
+ // The 'done_' flag indicates whether we have an answer
+ // to send back. If not, exit the coroutine permanently.
+ if (!data_->done_) {
+ CORO_YIELD return;
+ }
+
+ // Call the DNS answer provider to render the answer into
+ // wire format
+ (*data_->answer_callback_)(*data_->io_message_, data_->query_message_,
+ data_->answer_message_, data_->respbuf_);
+
+ // Begin an asynchronous send, and then yield. When the
+ // send completes, we will resume immediately after this point
+ // (though we have nothing further to do, so the coroutine
+ // will simply exit at that time).
+ CORO_YIELD data_->socket_->async_send_to(
+ buffer(data_->respbuf_->getData(), data_->respbuf_->getLength()),
+ *data_->sender_, *this);
+ }
+}
+
+/// Call the DNS lookup provider. (Expected to be called by the
+/// AsyncLookup<UDPServer> handler.)
+void
+UDPServer::asyncLookup() {
+ (*data_->lookup_callback_)(*data_->io_message_,
+ data_->query_message_, data_->answer_message_, data_->respbuf_, this);
+}
+
+/// Post this coroutine on the ASIO service queue so that it will
+/// resume processing where it left off. The 'done' parameter indicates
+/// whether there is an answer to return to the client.
+void
+UDPServer::resume(const bool done) {
+ data_->done_ = done;
+ data_->io_.post(*this);
+}
+
+bool
+UDPServer::hasAnswer() {
+ return (data_->done_);
+}
+
+} // namespace asiolink
diff --git a/src/lib/asiolink/udp_server.h b/src/lib/asiolink/udp_server.h
new file mode 100644
index 0000000..16a03dd
--- /dev/null
+++ b/src/lib/asiolink/udp_server.h
@@ -0,0 +1,102 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __UDP_SERVER_H
+#define __UDP_SERVER_H 1
+
+#ifndef ASIO_HPP
+#error "asio.hpp must be included before including this, see asiolink.h as to why"
+#endif
+
+#include <asiolink/dns_server.h>
+#include <asiolink/simple_callback.h>
+#include <asiolink/dns_lookup.h>
+#include <asiolink/dns_answer.h>
+
+#include <coroutine.h>
+
+namespace asiolink {
+
+//
+// Asynchronous UDP server coroutine
+//
+///
+/// \brief This class implements the coroutine to handle UDP
+/// DNS query event. As such, it is both a \c DNSServer and
+/// a \c coroutine
+///
+class UDPServer : public virtual DNSServer, public virtual coroutine {
+public:
+ /// \brief Constructor
+ /// \param io_service the asio::io_service to work with
+ /// \param addr the IP address to listen for queries on
+ /// \param port the port to listen for queries on
+ /// \param checkin the callbackprovider for non-DNS events
+ /// \param lookup the callbackprovider for DNS lookup events
+ /// \param answer the callbackprovider for DNS answer events
+ explicit UDPServer(asio::io_service& io_service,
+ const asio::ip::address& addr, const uint16_t port,
+ SimpleCallback* checkin = NULL,
+ DNSLookup* lookup = NULL,
+ DNSAnswer* answer = NULL);
+
+ /// \brief The function operator
+ void operator()(asio::error_code ec = asio::error_code(),
+ size_t length = 0);
+
+ /// \brief Calls the lookup callback
+ void asyncLookup();
+
+ /// \brief Resume operation
+ ///
+ /// \param done Set this to true if the lookup action is done and
+ /// we have an answer
+ void resume(const bool done);
+
+ /// \brief Check if we have an answer
+ ///
+ /// \return true if we have an answer
+ bool hasAnswer();
+
+ /// \brief Returns the coroutine state value
+ ///
+ /// \return the coroutine state value
+ int value() { return (get_value()); }
+
+ /// \brief Clones the object
+ ///
+ /// \return a newly allocated copy of this object
+ DNSServer* clone() {
+ UDPServer* s = new UDPServer(*this);
+ return (s);
+ }
+
+private:
+ enum { MAX_LENGTH = 4096 };
+
+ /**
+ * \brief Internal state and data.
+ *
+ * We use the pimple design pattern, but not because we need to hide
+ * internal data. This class and whole header is for private use anyway.
+ * It turned out that UDPServer is copied a lot, because it is a coroutine.
+ * This way the overhead of copying is lower, we copy only one shared
+ * pointer instead of about 10 of them.
+ */
+ class Data;
+ boost::shared_ptr<Data> data_;
+};
+
+} // namespace asiolink
+#endif // __UDP_SERVER_H
diff --git a/src/lib/asiolink/udp_socket.h b/src/lib/asiolink/udp_socket.h
new file mode 100644
index 0000000..5c641ff
--- /dev/null
+++ b/src/lib/asiolink/udp_socket.h
@@ -0,0 +1,48 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __UDP_SOCKET_H
+#define __UDP_SOCKET_H 1
+
+#ifndef ASIO_HPP
+#error "asio.hpp must be included before including this, see asiolink.h as to why"
+#endif
+
+#include <asiolink/io_socket.h>
+
+namespace asiolink {
+
+/// \brief The \c UDPSocket class is a concrete derived class of
+/// \c IOSocket that represents a UDP socket.
+///
+/// Other notes about \c TCPSocket applies to this class, too.
+class UDPSocket : public IOSocket {
+private:
+ UDPSocket(const UDPSocket& source);
+ UDPSocket& operator=(const UDPSocket& source);
+public:
+ /// \brief Constructor from an ASIO UDP socket.
+ ///
+ /// \param socket The ASIO representation of the UDP socket.
+ UDPSocket(asio::ip::udp::socket& socket) : socket_(socket) {}
+
+ virtual int getNative() const { return (socket_.native()); }
+ virtual int getProtocol() const { return (IPPROTO_UDP); }
+
+private:
+ asio::ip::udp::socket& socket_;
+};
+
+} // namespace asiolink
+#endif // __UDP_SOCKET_H
diff --git a/src/lib/asiolink/udpdns.cc b/src/lib/asiolink/udpdns.cc
deleted file mode 100644
index 5641802..0000000
--- a/src/lib/asiolink/udpdns.cc
+++ /dev/null
@@ -1,319 +0,0 @@
-// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <config.h>
-
-#include <unistd.h> // for some IPC/network system calls
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-#include <boost/bind.hpp>
-
-#include <asio.hpp>
-#include <asio/deadline_timer.hpp>
-
-#include <boost/shared_ptr.hpp>
-#include <boost/date_time/posix_time/posix_time_types.hpp>
-
-#include <dns/buffer.h>
-#include <dns/message.h>
-#include <dns/messagerenderer.h>
-#include <log/dummylog.h>
-#include <dns/opcode.h>
-#include <dns/rcode.h>
-
-#include <asiolink.h>
-#include <internal/coroutine.h>
-#include <internal/udpdns.h>
-
-using namespace asio;
-using asio::ip::udp;
-using asio::ip::tcp;
-using isc::log::dlog;
-
-using namespace std;
-using namespace isc::dns;
-
-namespace asiolink {
-/// The following functions implement the \c UDPServer class.
-///
-/// The constructor
-UDPServer::UDPServer(io_service& io_service,
- const ip::address& addr, const uint16_t port,
- SimpleCallback* checkin,
- DNSLookup* lookup,
- DNSAnswer* answer) :
- io_(io_service), done_(false),
- checkin_callback_(checkin),
- lookup_callback_(lookup),
- answer_callback_(answer)
-{
- // We must use different instantiations for v4 and v6;
- // otherwise ASIO will bind to both
- udp proto = addr.is_v4() ? udp::v4() : udp::v6();
- socket_.reset(new udp::socket(io_service, proto));
- socket_->set_option(socket_base::reuse_address(true));
- if (addr.is_v6()) {
- socket_->set_option(asio::ip::v6_only(true));
- }
- socket_->bind(udp::endpoint(addr, port));
-}
-
-/// The function operator is implemented with the "stackless coroutine"
-/// pattern; see internal/coroutine.h for details.
-void
-UDPServer::operator()(error_code ec, size_t length) {
- /// Because the coroutine reeentry block is implemented as
- /// a switch statement, inline variable declarations are not
- /// permitted. Certain variables used below can be declared here.
-
- CORO_REENTER (this) {
- do {
- // Instantiate the data buffer and endpoint that will
- // be used by the asynchronous receive call.
- data_.reset(new char[MAX_LENGTH]);
- sender_.reset(new udp::endpoint());
-
- do {
- // Begin an asynchronous receive, then yield.
- // When the receive event is posted, the coroutine
- // will resume immediately after this point.
- CORO_YIELD socket_->async_receive_from(buffer(data_.get(),
- MAX_LENGTH),
- *sender_, *this);
- } while (ec || length == 0);
-
- bytes_ = length;
-
- /// Fork the coroutine by creating a copy of this one and
- /// scheduling it on the ASIO service queue. The parent
- /// will continue listening for DNS packets while the child
- /// processes the one that has just arrived.
- CORO_FORK io_.post(UDPServer(*this));
- } while (is_parent());
-
- // Create an \c IOMessage object to store the query.
- //
- // (XXX: It would be good to write a factory function
- // that would quickly generate an IOMessage object without
- // all these calls to "new".)
- peer_.reset(new UDPEndpoint(*sender_));
- iosock_.reset(new UDPSocket(*socket_));
- io_message_.reset(new IOMessage(data_.get(), bytes_, *iosock_, *peer_));
-
- // Perform any necessary operations prior to processing an incoming
- // query (e.g., checking for queued configuration messages).
- //
- // (XXX: it may be a performance issue to check in for every single
- // incoming query; we may wish to throttle this in the future.)
- if (checkin_callback_ != NULL) {
- (*checkin_callback_)(*io_message_);
- }
-
- // If we don't have a DNS Lookup provider, there's no point in
- // continuing; we exit the coroutine permanently.
- if (lookup_callback_ == NULL) {
- CORO_YIELD return;
- }
-
- // Instantiate objects that will be needed by the
- // asynchronous DNS lookup and/or by the send call.
- respbuf_.reset(new OutputBuffer(0));
- query_message_.reset(new Message(Message::PARSE));
- answer_message_.reset(new Message(Message::RENDER));
-
- // Schedule a DNS lookup, and yield. When the lookup is
- // finished, the coroutine will resume immediately after
- // this point.
- CORO_YIELD io_.post(AsyncLookup<UDPServer>(*this));
-
- dlog("[XX] got an answer");
-
- // The 'done_' flag indicates whether we have an answer
- // to send back. If not, exit the coroutine permanently.
- if (!done_) {
- CORO_YIELD return;
- }
-
- // Call the DNS answer provider to render the answer into
- // wire format
- (*answer_callback_)(*io_message_, query_message_,
- answer_message_, respbuf_);
-
- // Begin an asynchronous send, and then yield. When the
- // send completes, we will resume immediately after this point
- // (though we have nothing further to do, so the coroutine
- // will simply exit at that time).
- CORO_YIELD socket_->async_send_to(buffer(respbuf_->getData(),
- respbuf_->getLength()),
- *sender_, *this);
- }
-}
-
-/// Call the DNS lookup provider. (Expected to be called by the
-/// AsyncLookup<UDPServer> handler.)
-void
-UDPServer::asyncLookup() {
- (*lookup_callback_)(*io_message_, query_message_, answer_message_,
- respbuf_, this);
-}
-
-/// Post this coroutine on the ASIO service queue so that it will
-/// resume processing where it left off. The 'done' parameter indicates
-/// whether there is an answer to return to the client.
-void
-UDPServer::resume(const bool done) {
- done_ = done;
- io_.post(*this);
-}
-
-// Private UDPQuery data (see internal/udpdns.h for reasons)
-struct UDPQuery::PrivateData {
- // Socket we send query to and expect reply from there
- udp::socket socket;
- // Where was the query sent
- udp::endpoint remote;
- // What we ask the server
- Question question;
- // We will store the answer here
- OutputBufferPtr buffer;
- OutputBufferPtr msgbuf;
- // Temporary buffer for answer
- boost::shared_array<char> data;
- // This will be called when the data arrive or timeouts
- Callback* callback;
- // Did we already stop operating (data arrived, we timed out, someone
- // called stop). This can be so when we are cleaning up/there are
- // still pointers to us.
- bool stopped;
- // Timer to measure timeouts.
- deadline_timer timer;
- // How many milliseconds are we willing to wait for answer?
- int timeout;
-
- PrivateData(io_service& service,
- const udp::socket::protocol_type& protocol, const Question &q,
- OutputBufferPtr b, Callback *c) :
- socket(service, protocol),
- question(q),
- buffer(b),
- msgbuf(new OutputBuffer(512)),
- callback(c),
- stopped(false),
- timer(service)
- { }
-};
-
-/// The following functions implement the \c UDPQuery class.
-///
-/// The constructor
-UDPQuery::UDPQuery(io_service& io_service,
- const Question& q, const IOAddress& addr, uint16_t port,
- OutputBufferPtr buffer, Callback *callback, int timeout) :
- data_(new PrivateData(io_service,
- addr.getFamily() == AF_INET ? udp::v4() : udp::v6(), q, buffer,
- callback))
-{
- data_->remote = UDPEndpoint(addr, port).getASIOEndpoint();
- data_->timeout = timeout;
-}
-
-/// The function operator is implemented with the "stackless coroutine"
-/// pattern; see internal/coroutine.h for details.
-void
-UDPQuery::operator()(error_code ec, size_t length) {
- if (ec || data_->stopped) {
- return;
- }
-
- CORO_REENTER (this) {
- /// Generate the upstream query and render it to wire format
- /// This is done in a different scope to allow inline variable
- /// declarations.
- {
- Message msg(Message::RENDER);
-
- // XXX: replace with boost::random or some other suitable PRNG
- msg.setQid(0);
- msg.setOpcode(Opcode::QUERY());
- msg.setRcode(Rcode::NOERROR());
- msg.setHeaderFlag(Message::HEADERFLAG_RD);
- msg.addQuestion(data_->question);
- MessageRenderer renderer(*data_->msgbuf);
- msg.toWire(renderer);
- dlog("Sending " + msg.toText() + " to " +
- data_->remote.address().to_string());
- }
-
-
- // If we timeout, we stop, which will shutdown everything and
- // cancel all other attempts to run inside the coroutine
- if (data_->timeout != -1) {
- data_->timer.expires_from_now(boost::posix_time::milliseconds(
- data_->timeout));
- data_->timer.async_wait(boost::bind(&UDPQuery::stop, *this,
- TIME_OUT));
- }
-
- // Begin an asynchronous send, and then yield. When the
- // send completes, we will resume immediately after this point.
- CORO_YIELD data_->socket.async_send_to(buffer(data_->msgbuf->getData(),
- data_->msgbuf->getLength()), data_->remote, *this);
-
- /// Allocate space for the response. (XXX: This should be
- /// optimized by maintaining a free list of pre-allocated blocks)
- data_->data.reset(new char[MAX_LENGTH]);
-
- /// Begin an asynchronous receive, and yield. When the receive
- /// completes, we will resume immediately after this point.
- CORO_YIELD data_->socket.async_receive_from(buffer(data_->data.get(),
- MAX_LENGTH), data_->remote, *this);
- // The message is not rendered yet, so we can't print it easilly
- dlog("Received response from " + data_->remote.address().to_string());
-
- /// Copy the answer into the response buffer. (XXX: If the
- /// OutputBuffer object were made to meet the requirements of
- /// a MutableBufferSequence, then it could be written to directly
- /// by async_recieve_from() and this additional copy step would
- /// be unnecessary.)
- data_->buffer->writeData(data_->data.get(), length);
-
- /// We are done
- stop(SUCCESS);
- }
-}
-
-void
-UDPQuery::stop(Result result) {
- if (!data_->stopped) {
- switch (result) {
- case TIME_OUT:
- dlog("Query timed out");
- break;
- case STOPPED:
- dlog("Query stopped");
- break;
- default:;
- }
- data_->stopped = true;
- data_->socket.cancel();
- data_->socket.close();
- data_->timer.cancel();
- if (data_->callback) {
- (*data_->callback)(result);
- }
- }
-}
-
-}
diff --git a/src/lib/cache/Makefile.am b/src/lib/cache/Makefile.am
new file mode 100644
index 0000000..264aca6
--- /dev/null
+++ b/src/lib/cache/Makefile.am
@@ -0,0 +1,33 @@
+SUBDIRS = . tests
+
+AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
+AM_CPPFLAGS += $(BOOST_INCLUDES) $(MULTITHREADING_FLAG)
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/dns -I$(top_builddir)/src/lib/dns
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/nsas -I$(top_builddir)/src/lib/nsas
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/cache -I$(top_builddir)/src/lib/cache
+AM_CPPFLAGS += $(SQLITE_CFLAGS)
+AM_CXXFLAGS = $(B10_CXXFLAGS)
+
+# Some versions of GCC warn about some versions of Boost regarding
+# missing initializer for members in its posix_time.
+# https://svn.boost.org/trac/boost/ticket/3477
+# But older GCC compilers don't have the flag.
+AM_CXXFLAGS += $(WARNING_NO_MISSING_FIELD_INITIALIZERS_CFLAG)
+
+if USE_CLANGPP
+# clang++ complains about unused function parameters in some boost header
+# files.
+AM_CXXFLAGS += -Wno-unused-parameter
+endif
+
+lib_LTLIBRARIES = libcache.la
+libcache_la_SOURCES = resolver_cache.h resolver_cache.cc
+libcache_la_SOURCES += message_cache.h message_cache.cc
+libcache_la_SOURCES += message_entry.h message_entry.cc
+libcache_la_SOURCES += rrset_cache.h rrset_cache.cc
+libcache_la_SOURCES += rrset_entry.h rrset_entry.cc
+libcache_la_SOURCES += cache_entry_key.h cache_entry_key.cc
+libcache_la_SOURCES += rrset_copy.h rrset_copy.cc
+libcache_la_SOURCES += local_zone_data.h local_zone_data.cc
+
+CLEANFILES = *.gcno *.gcda
diff --git a/src/lib/cache/TODO b/src/lib/cache/TODO
new file mode 100644
index 0000000..a7d2458
--- /dev/null
+++ b/src/lib/cache/TODO
@@ -0,0 +1,14 @@
+* Revisit the algorithm used by getRRsetTrustLevel() in message_entry.cc.
+* Implement dump/load/resize interfaces of rrset/message/recursor cache.
+* Once LRU hash table is implemented, it should be used by message/rrset cache.
+* Once the hash/lrulist related files in /lib/nsas is moved to seperated
+ folder, the code of recursor cache has to be updated.
+* Set proper AD flags once DNSSEC is supported by the cache.
+* When the message or rrset entry has expired, it should be removed
+ from the cache, or just moved to the head of LRU list, so that it
+ can removed first.
+* Make resolver cache be smart to refetch the messages that are about
+ to expire.
+* When the rrset beging updated is an NS rrset, NSAS should be updated
+ together.
+
diff --git a/src/lib/cache/cache_entry_key.cc b/src/lib/cache/cache_entry_key.cc
new file mode 100644
index 0000000..35917a0
--- /dev/null
+++ b/src/lib/cache/cache_entry_key.cc
@@ -0,0 +1,44 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <sstream>
+#include "cache_entry_key.h"
+
+using namespace std;
+
+namespace isc {
+namespace cache {
+const std::string
+genCacheEntryName(const isc::dns::Name& name, const isc::dns::RRType& type) {
+ std::string keystr = name.toText();
+ ostringstream stream;
+ stream << type.getCode();
+ keystr += stream.str();
+ return (keystr);
+}
+
+const std::string
+genCacheEntryName(const std::string& namestr, const uint16_t type) {
+ std::string keystr = namestr;
+ ostringstream stream;
+ stream << type;
+ keystr += stream.str();
+ return (keystr);
+}
+
+} // namespace cache
+} // namespace isc
+
diff --git a/src/lib/cache/cache_entry_key.h b/src/lib/cache/cache_entry_key.h
new file mode 100644
index 0000000..002f958
--- /dev/null
+++ b/src/lib/cache/cache_entry_key.h
@@ -0,0 +1,56 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __CACHE_ENTRY_KEY_H
+#define __CACHE_ENTRY_KEY_H
+
+#include <string>
+#include <dns/name.h>
+#include <dns/rrtype.h>
+
+namespace isc {
+namespace cache {
+
+/// \brief Entry Name Generation Functions
+///
+/// Generate the name for message/rrset entries.
+///
+/// Concatenates the string representation of the Name and the
+/// string representation of the type number.
+///
+/// Note: the returned name is a text string, not wire format.
+/// eg. if name is 'example.com.', type is 'A', the return
+/// value is 'example.com.1'
+///
+/// \param name The Name to create a text entry for
+/// \param type The RRType to create a text entry for
+/// \return return the entry name.
+const std::string
+genCacheEntryName(const isc::dns::Name& name, const isc::dns::RRType& type);
+
+///
+/// \overload
+///
+/// \param namestr A string representation of a DNS Name
+/// \param type The value of a DNS RRType
+const std::string
+genCacheEntryName(const std::string& namestr, const uint16_t type);
+
+} // namespace cache
+} // namespace isc
+
+#endif // __CACHE_ENTRY_KEY_H
+
diff --git a/src/lib/cache/local_zone_data.cc b/src/lib/cache/local_zone_data.cc
new file mode 100644
index 0000000..2dcc113
--- /dev/null
+++ b/src/lib/cache/local_zone_data.cc
@@ -0,0 +1,58 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <dns/rrset.h>
+#include "local_zone_data.h"
+#include "cache_entry_key.h"
+#include "rrset_copy.h"
+
+using namespace std;
+using namespace isc::dns;
+
+namespace isc {
+namespace cache {
+
+typedef pair<std::string, RRsetPtr> RRsetMapPair;
+typedef map<std::string, RRsetPtr>::iterator RRsetMapIterator;
+
+isc::dns::RRsetPtr
+LocalZoneData::lookup(const isc::dns::Name& name,
+ const isc::dns::RRType& type)
+{
+ string key = genCacheEntryName(name, type);
+ RRsetMapIterator iter = rrsets_map_.find(key);
+ if (iter == rrsets_map_.end()) {
+ return (RRsetPtr());
+ } else {
+ return (iter->second);
+ }
+}
+
+void
+LocalZoneData::update(const isc::dns::RRset& rrset) {
+ //TODO Do we really need to recreate the rrset again?
+ string key = genCacheEntryName(rrset.getName(), rrset.getType());
+ RRset* rrset_copy = new RRset(rrset.getName(), rrset.getClass(),
+ rrset.getType(), rrset.getTTL());
+
+ rrsetCopy(rrset, *rrset_copy);
+ RRsetPtr rrset_ptr(rrset_copy);
+ rrsets_map_[key] = rrset_ptr;
+}
+
+} // namespace cache
+} // namespace isc
+
diff --git a/src/lib/cache/local_zone_data.h b/src/lib/cache/local_zone_data.h
new file mode 100644
index 0000000..bcf5a94
--- /dev/null
+++ b/src/lib/cache/local_zone_data.h
@@ -0,0 +1,66 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef _LOCAL_ZONE_DATA
+#define _LOCAL_ZONE_DATA
+
+#include <map>
+#include <string>
+#include <boost/shared_ptr.hpp>
+#include <dns/rrset.h>
+
+namespace isc {
+namespace cache {
+
+/// \brief Local Zone Data
+/// The object of LocalZoneData represents the data of one
+/// local zone. It provides the interface for lookup the rrsets
+/// in the zone.
+class LocalZoneData {
+public:
+ LocalZoneData(uint16_t rrset_class) : class_(rrset_class)
+ {}
+
+ /// \brief Look up one rrset.
+ ///
+ /// \param qname The query name to look up
+ /// \param qtype The query type to look up
+ /// \return return the shared_ptr of rrset if it is
+ /// found in the local zone, or else, return NULL.
+ isc::dns::RRsetPtr lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype);
+
+ /// \brief Update the rrset in the local zone.
+ ///
+ /// If the rrset doesn't exist, it will be added.
+ /// Otherwise, the existed one will be overwritten.
+ ///
+ /// \param rrset The rrset to update
+ void update(const isc::dns::RRset& rrset);
+
+private:
+ std::map<std::string, isc::dns::RRsetPtr> rrsets_map_; // RRsets of the zone
+ uint16_t class_; // The class of the zone
+};
+
+typedef boost::shared_ptr<LocalZoneData> LocalZoneDataPtr;
+typedef boost::shared_ptr<const LocalZoneData> ConstLocalZoneDataPtr;
+
+} // namespace cache
+} // namespace isc
+
+#endif // _LOCAL_ZONE_DATA
+
diff --git a/src/lib/cache/message_cache.cc b/src/lib/cache/message_cache.cc
new file mode 100644
index 0000000..6582199
--- /dev/null
+++ b/src/lib/cache/message_cache.cc
@@ -0,0 +1,97 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <config.h>
+
+#include <nsas/nsas_entry_compare.h>
+#include <nsas/hash_table.h>
+#include <nsas/hash_deleter.h>
+#include "message_cache.h"
+#include "cache_entry_key.h"
+
+using namespace isc::nsas;
+using namespace isc::dns;
+using namespace std;
+
+namespace isc {
+namespace cache {
+
+MessageCache::MessageCache(boost::shared_ptr<RRsetCache> rrset_cache,
+ uint32_t cache_size, uint16_t message_class):
+ message_class_(message_class),
+ rrset_cache_(rrset_cache),
+ message_table_(new NsasEntryCompare<MessageEntry>, cache_size),
+ message_lru_((3 * cache_size),
+ new HashDeleter<MessageEntry>(message_table_))
+{
+}
+
+bool
+MessageCache::lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ isc::dns::Message& response)
+{
+ std::string entry_name = genCacheEntryName(qname, qtype);
+ HashKey entry_key = HashKey(entry_name, RRClass(message_class_));
+ MessageEntryPtr msg_entry = message_table_.get(entry_key);
+ if(msg_entry) {
+ message_lru_.touch(msg_entry);
+ return (msg_entry->genMessage(time(NULL), response));
+ }
+
+ return (false);
+}
+
+bool
+MessageCache::update(const Message& msg) {
+ QuestionIterator iter = msg.beginQuestion();
+ std::string entry_name = genCacheEntryName((*iter)->getName(), (*iter)->getType());
+ HashKey entry_key = HashKey(entry_name, RRClass(message_class_));
+
+ // The simplest way to update is removing the old message entry directly.
+ // We have find the existed message entry, since we need to delete it
+ // from lru list too.
+ // TODO, but there should be a better way, since we here have to remove and
+ // add the message entry, maybe there is one way to touch it once.
+ MessageEntryPtr old_msg_entry = message_table_.get(entry_key);
+ if (old_msg_entry) {
+ message_lru_.remove(old_msg_entry);
+ }
+
+ MessageEntryPtr msg_entry(new MessageEntry(msg, rrset_cache_));
+ message_lru_.add(msg_entry);
+ return (message_table_.add(msg_entry, entry_key, true));
+}
+
+void
+MessageCache::dump(const std::string&) {
+ //TODO
+}
+
+void
+MessageCache::load(const std::string&) {
+ //TODO
+}
+
+bool
+MessageCache::resize(uint32_t) {
+ //TODO
+ return (true);
+}
+
+} // namespace cache
+} // namespace isc
+
diff --git a/src/lib/cache/message_cache.h b/src/lib/cache/message_cache.h
new file mode 100644
index 0000000..0131b30
--- /dev/null
+++ b/src/lib/cache/message_cache.h
@@ -0,0 +1,95 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __MESSAGE_CACHE_H
+#define __MESSAGE_CACHE_H
+
+#include <string>
+#include <boost/shared_ptr.hpp>
+#include <dns/message.h>
+#include "message_entry.h"
+#include <nsas/hash_table.h>
+#include <nsas/lru_list.h>
+
+namespace isc {
+namespace cache {
+
+class RRsetCache;
+
+/// \brief Message Cache
+/// The object of MessageCache represents the cache for class-specific
+/// messages.
+///
+class MessageCache {
+// Noncopyable
+private:
+ MessageCache(const MessageCache& source);
+ MessageCache& operator=(const MessageCache& source);
+public:
+ /// \param cache_size The size of message cache.
+ MessageCache(boost::shared_ptr<RRsetCache> rrset_cache_,
+ uint32_t cache_size, uint16_t message_class);
+
+ /// \brief Look up message in cache.
+ /// \param message generated response message if the message entry
+ /// can be found.
+ ///
+ /// \return return true if the message can be found in cache, or else,
+ /// return false.
+ //TODO Maybe some user just want to get the message_entry.
+ bool lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ isc::dns::Message& message);
+
+ /// \brief Update the message in the cache with the new one.
+ /// If the message doesn't exist in the cache, it will be added
+ /// directly.
+ bool update(const isc::dns::Message& msg);
+
+ /// \brief Dump the message cache to specified file.
+ /// \todo It should can be dumped to one configured database.
+ void dump(const std::string& file_name);
+
+ /// \brief Load the cache from one file.
+ /// \todo It should can be loaded from one configured database.
+ void load(const std::string& file_name);
+
+ /// \brief Resize the size of message cache in runtime.
+ bool resize(uint32_t size);
+
+protected:
+ /// \brief Get the hash key for the message entry in the cache.
+ /// \param name query name of the message.
+ /// \param type query type of the message.
+ /// \return return the hash key.
+ HashKey getEntryHashKey(const isc::dns::Name& name,
+ const isc::dns::RRType& type) const;
+
+ // Make these variants be protected for easy unittest.
+protected:
+ uint16_t message_class_; // The class of the message cache.
+ boost::shared_ptr<RRsetCache> rrset_cache_;
+ isc::nsas::HashTable<MessageEntry> message_table_;
+ isc::nsas::LruList<MessageEntry> message_lru_;
+};
+
+typedef boost::shared_ptr<MessageCache> MessageCachePtr;
+
+} // namespace cache
+} // namespace isc
+
+#endif // __MESSAGE_CACHE_H
+
diff --git a/src/lib/cache/message_entry.cc b/src/lib/cache/message_entry.cc
new file mode 100644
index 0000000..2fdb07c
--- /dev/null
+++ b/src/lib/cache/message_entry.cc
@@ -0,0 +1,251 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <config.h>
+
+#include <limits>
+#include <dns/message.h>
+#include <nsas/nsas_entry.h>
+#include "message_entry.h"
+#include "rrset_cache.h"
+
+using namespace isc::dns;
+using namespace std;
+
+namespace isc {
+namespace cache {
+
+static uint32_t MAX_UINT32 = numeric_limits<uint32_t>::max();
+
+MessageEntry::MessageEntry(const isc::dns::Message& msg,
+ boost::shared_ptr<RRsetCache> rrset_cache):
+ rrset_cache_(rrset_cache),
+ headerflag_aa_(false),
+ headerflag_tc_(false)
+{
+ initMessageEntry(msg);
+ entry_name_ = genCacheEntryName(query_name_, query_type_);
+ hash_key_ptr_ = new HashKey(entry_name_, RRClass(query_class_));
+}
+
+bool
+MessageEntry::getRRsetEntries(vector<RRsetEntryPtr>& rrset_entry_vec,
+ const time_t time_now)
+{
+ uint16_t entry_count = answer_count_ + authority_count_ + additional_count_;
+ rrset_entry_vec.reserve(rrset_entry_vec.size() + entry_count);
+ for (int index = 0; index < entry_count; ++index) {
+ RRsetEntryPtr rrset_entry = rrset_cache_->lookup(rrsets_[index].name_,
+ rrsets_[index].type_);
+ if (time_now < rrset_entry->getExpireTime()) {
+ rrset_entry_vec.push_back(rrset_entry);
+ } else {
+ return (false);
+ }
+ }
+
+ return (true);
+}
+
+void
+MessageEntry::addRRset(isc::dns::Message& message,
+ const vector<RRsetEntryPtr>& rrset_entry_vec,
+ const isc::dns::Message::Section& section,
+ bool dnssec_need)
+{
+ uint16_t start_index = 0;
+ uint16_t end_index = answer_count_;
+ assert(section != Message::SECTION_QUESTION);
+
+ if (section == Message::SECTION_AUTHORITY) {
+ start_index = answer_count_;
+ end_index = answer_count_ + authority_count_;
+ } else if (section == Message::SECTION_ADDITIONAL) {
+ start_index = answer_count_ + authority_count_;
+ end_index = start_index + additional_count_;
+ }
+
+ for(uint16_t index = start_index; index < end_index; ++index) {
+ message.addRRset(section, rrset_entry_vec[index]->getRRset(), dnssec_need);
+ }
+}
+
+bool
+MessageEntry::genMessage(const time_t& time_now,
+ isc::dns::Message& msg)
+{
+ if (time_now >= expire_time_) {
+ // The message entry has expired.
+ return (false);
+ } else {
+ // Before do any generation, we should check if some rrset
+ // has expired, if it is, return false.
+ vector<RRsetEntryPtr> rrset_entry_vec;
+ if (false == getRRsetEntries(rrset_entry_vec, time_now)) {
+ return (false);
+ }
+
+ // Begin message generation. We don't need to add question
+ // section, since it has been included in the message.
+ // Set cached header flags.
+ msg.setHeaderFlag(Message::HEADERFLAG_AA, headerflag_aa_);
+ msg.setHeaderFlag(Message::HEADERFLAG_TC, headerflag_tc_);
+
+ bool dnssec_need = msg.getEDNS().get();
+ addRRset(msg, rrset_entry_vec, Message::SECTION_ANSWER, dnssec_need);
+ addRRset(msg, rrset_entry_vec, Message::SECTION_AUTHORITY, dnssec_need);
+ addRRset(msg, rrset_entry_vec, Message::SECTION_ADDITIONAL, dnssec_need);
+
+ return (true);
+ }
+}
+
+RRsetTrustLevel
+MessageEntry::getRRsetTrustLevel(const Message& message,
+ const isc::dns::RRsetPtr& rrset,
+ const isc::dns::Message::Section& section)
+{
+ bool aa = message.getHeaderFlag(Message::HEADERFLAG_AA);
+ switch(section) {
+ case Message::SECTION_ANSWER: {
+ if (aa) {
+ RRsetIterator rrset_iter = message.beginSection(section);
+
+ // Make sure we are inspecting the right RRset
+ while((*rrset_iter)->getName() != rrset->getName() &&
+ (*rrset_iter)->getType() != rrset->getType() &&
+ rrset_iter != message.endSection(section)) {
+ ++rrset_iter;
+ }
+ assert(rrset_iter != message.endSection(section));
+
+ // According RFC2181 section 5.4.1, only the record
+ // describing that ailas is necessarily authoritative.
+ // If there is one or more CNAME records in answer section.
+ // CNAME records is assumed as the first rrset.
+ if ((*rrset_iter)->getType() == RRType::CNAME()) {
+ // TODO: real equals for RRsets?
+ if ((*rrset_iter).get() == rrset.get()) {
+ return (RRSET_TRUST_ANSWER_AA);
+ } else {
+ return (RRSET_TRUST_ANSWER_NONAA);
+ }
+ }
+
+ // Here, if the first rrset is DNAME, then assume the
+ // second rrset is synchronized CNAME record, except
+ // these two records, any other records in answer section
+ // should be treated as non-authoritative.
+ // TODO, this part logic should be revisited later,
+ // since it's not mentioned by RFC2181.
+ if ((*rrset_iter)->getType() == RRType::DNAME()) {
+ // TODO: real equals for RRsets?
+ if ((*rrset_iter).get() == rrset.get() ||
+ ((++rrset_iter) != message.endSection(section) &&
+ (*rrset_iter).get() == rrset.get())) {
+ return (RRSET_TRUST_ANSWER_AA);
+ } else {
+ return (RRSET_TRUST_ANSWER_NONAA);
+ }
+ }
+
+ return (RRSET_TRUST_ANSWER_AA);
+
+ } else {
+ return (RRSET_TRUST_ANSWER_NONAA);
+ }
+ break;
+ }
+
+ case Message::SECTION_AUTHORITY: {
+ if (aa) {
+ return (RRSET_TRUST_AUTHORITY_AA);
+ } else {
+ return (RRSET_TRUST_AUTHORITY_NONAA);
+ }
+ break;
+ }
+
+ case Message::SECTION_ADDITIONAL: {
+ if (aa) {
+ return (RRSET_TRUST_ADDITIONAL_AA);
+ } else {
+ return (RRSET_TRUST_ADDITIONAL_NONAA);
+ }
+ break;
+ }
+
+ default:
+ return (RRSET_TRUST_DEFAULT);
+ }
+}
+
+void
+MessageEntry::parseSection(const isc::dns::Message& msg,
+ const Message::Section& section,
+ uint32_t& smaller_ttl,
+ uint16_t& rrset_count)
+{
+ RRsetIterator iter;
+ int count = 0;
+ for (iter = msg.beginSection(section);
+ iter != msg.endSection(section);
+ ++iter) {
+ // Add the rrset entry to rrset_cache or update the existed
+ // rrset entry if the new one is more authoritative.
+ //TODO set proper rrset trust level.
+ RRsetPtr rrset_ptr = *iter;
+ RRsetTrustLevel level = getRRsetTrustLevel(msg, rrset_ptr, section);
+ RRsetEntryPtr rrset_entry = rrset_cache_->update(*rrset_ptr, level);
+ rrsets_.push_back(RRsetRef(rrset_ptr->getName(), rrset_ptr->getType()));
+
+ uint32_t rrset_ttl = rrset_entry->getTTL();
+ if (smaller_ttl > rrset_ttl) {
+ smaller_ttl = rrset_ttl;
+ }
+
+ count++;
+ }
+
+ rrset_count = count;
+}
+
+void
+MessageEntry::initMessageEntry(const isc::dns::Message& msg) {
+ //TODO better way to cache the header flags?
+ headerflag_aa_ = msg.getHeaderFlag(Message::HEADERFLAG_AA);
+ headerflag_tc_ = msg.getHeaderFlag(Message::HEADERFLAG_TC);
+
+ // We only cache the first question in question section.
+ // TODO, do we need to support muptiple questions?
+ query_count_ = 1;
+ QuestionIterator iter = msg.beginQuestion();
+ query_name_ = (*iter)->getName().toText();
+ query_type_ = (*iter)->getType().getCode();
+ query_class_ = (*iter)->getClass().getCode();
+
+ uint32_t min_ttl = MAX_UINT32;
+ parseSection(msg, Message::SECTION_ANSWER, min_ttl, answer_count_);
+ parseSection(msg, Message::SECTION_AUTHORITY, min_ttl, authority_count_);
+ parseSection(msg, Message::SECTION_ADDITIONAL, min_ttl, additional_count_);
+
+ expire_time_ = time(NULL) + min_ttl;
+}
+
+} // namespace cache
+} // namespace isc
+
+
diff --git a/src/lib/cache/message_entry.h b/src/lib/cache/message_entry.h
new file mode 100644
index 0000000..20b2472
--- /dev/null
+++ b/src/lib/cache/message_entry.h
@@ -0,0 +1,180 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __MESSAGE_ENTRY_H
+#define __MESSAGE_ENTRY_H
+
+#include <vector>
+#include <dns/message.h>
+#include <dns/rrset.h>
+#include <nsas/nsas_entry.h>
+#include "rrset_entry.h"
+
+
+using namespace isc::nsas;
+
+namespace isc {
+namespace cache {
+
+class RRsetEntry;
+class RRsetCache;
+
+/// \brief Information to refer an RRset.
+///
+/// There is no class information here, since the rrsets are cached in
+/// the class-specific rrset cache.
+struct RRsetRef{
+ /// \brief Constructor
+ ///
+ /// \param name The Name for the RRset
+ /// \param type the RRType for the RRrset
+ RRsetRef(const isc::dns::Name& name, const isc::dns::RRType& type):
+ name_(name), type_(type)
+ {}
+
+ isc::dns::Name name_; // Name of rrset.
+ isc::dns::RRType type_; // Type of rrset.
+};
+
+/// \brief Message Entry
+///
+/// The object of MessageEntry represents one response message
+/// answered to the resolver client.
+class MessageEntry : public NsasEntry<MessageEntry> {
+// Noncopyable
+private:
+ MessageEntry(const MessageEntry& source);
+ MessageEntry& operator=(const MessageEntry& source);
+public:
+
+ /// \brief Initialize the message entry object with one dns
+ /// message.
+ /// \param message The message used to initialize MessageEntry.
+ /// \param rrset_cache the pointer of RRsetCache. When one message
+ /// entry is created, rrset cache needs to be updated,
+ /// since some new rrset entries may be inserted into
+ /// rrset cache, or the existed rrset entries need
+ /// to be updated.
+ MessageEntry(const isc::dns::Message& message,
+ boost::shared_ptr<RRsetCache> rrset_cache);
+
+ /// \brief generate one dns message according
+ /// the rrsets information of the message.
+ ///
+ /// \param time_now set the ttl of each rrset in the message
+ /// as "expire_time - time_now" (expire_time is the
+ /// expiration time of the rrset).
+ /// \param response generated dns message.
+ /// \return return true if the response message can be generated
+ /// from the cached information, or else, return false.
+ bool genMessage(const time_t& time_now, isc::dns::Message& response);
+
+ /// \brief Get the hash key of the message entry.
+ ///
+ /// \return return hash key
+ virtual HashKey hashKey() const {
+ return (*hash_key_ptr_);
+ }
+
+ /// \short Protected memebers, so they can be accessed by tests.
+ //@{
+protected:
+ /// \brief Initialize the message entry with dns message.
+ ///
+ /// \param message The Message to initialize the entry with
+ void initMessageEntry(const isc::dns::Message& message);
+
+ /// \brief Parse the rrsets in specified section.
+ ///
+ /// \param msg The message to parse the RRsets from
+ /// \param section The Section to parse the RRsets from
+ /// \param smaller_ttl Get the smallest ttl of rrsets in
+ /// specified section, if it's smaller than the given value.
+ /// \param rrset_count the rrset count of the section.
+ /// (TODO for Message, getRRsetCount() should be one
+ /// interface provided by Message.)
+ void parseSection(const isc::dns::Message& msg,
+ const isc::dns::Message::Section& section,
+ uint32_t& smaller_ttl,
+ uint16_t& rrset_count);
+
+ /// \brief Get RRset Trustworthiness
+ /// The algorithm refers to RFC2181 section 5.4.1
+ /// Only the rrset can be updated by the rrsets
+ /// with higher trust level.
+ ///
+ /// \param message Message that the rrset belongs to
+ /// \param rrset specified rrset which needs to get its
+ /// trust worthiness
+ /// \param section Section of the rrset
+ /// \return return rrset trust level.
+ RRsetTrustLevel getRRsetTrustLevel(const isc::dns::Message& message,
+ const isc::dns::RRsetPtr& rrset,
+ const isc::dns::Message::Section& section);
+
+ /// \brief Add rrset to one section of message.
+ ///
+ /// \param message The message to add rrsets to.
+ /// \param rrset_entry_vec vector for rrset entries in
+ /// different sections.
+ /// \param section The section to add to
+ /// \param dnssec_need need dnssec records or not.
+ void addRRset(isc::dns::Message& message,
+ const std::vector<RRsetEntryPtr>& rrset_entry_vec,
+ const isc::dns::Message::Section& section,
+ bool dnssec_need);
+
+ /// \brief Get the all the rrset entries for the message entry.
+ ///
+ /// \param rrset_entry_vec vector to add unexpired rrset entries to
+ /// \param time_now the time of now. Used to compare with rrset
+ /// entry's expire time.
+ /// \return return false if any rrset entry has expired, true
+ /// otherwise.
+ bool getRRsetEntries(std::vector<RRsetEntryPtr>& rrset_entry_vec,
+ const time_t time_now);
+
+ time_t expire_time_; // Expiration time of the message.
+ //@}
+
+private:
+ std::string entry_name_; // The name for this entry(name + type)
+ HashKey* hash_key_ptr_; // the key for messag entry in hash table.
+
+ std::vector<RRsetRef> rrsets_;
+ boost::shared_ptr<RRsetCache> rrset_cache_;
+
+ std::string query_name_; // query name of the message.
+ uint16_t query_class_; // query class of the message.
+ uint16_t query_type_; // query type of message.
+
+ uint16_t query_count_; // query count in query section.
+ uint16_t answer_count_; // rrset count in answer section.
+ uint16_t authority_count_; // rrset count in authority section.
+ uint16_t additional_count_; // rrset count in addition section.
+
+ //TODO, there should be a better way to cache these header flags
+ bool headerflag_aa_; // Whether AA bit is set.
+ bool headerflag_tc_; // Whether TC bit is set.
+};
+
+typedef boost::shared_ptr<MessageEntry> MessageEntryPtr;
+
+} // namespace cache
+} // namespace isc
+
+#endif // __MESSAGE_ENTRY_H
+
diff --git a/src/lib/cache/resolver_cache.cc b/src/lib/cache/resolver_cache.cc
new file mode 100644
index 0000000..7ebdb4f
--- /dev/null
+++ b/src/lib/cache/resolver_cache.cc
@@ -0,0 +1,246 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <config.h>
+
+#include "resolver_cache.h"
+#include "dns/message.h"
+#include "rrset_cache.h"
+#include <string>
+#include <algorithm>
+
+using namespace isc::dns;
+using namespace std;
+
+namespace isc {
+namespace cache {
+
+ResolverClassCache::ResolverClassCache(const RRClass& cache_class) :
+ cache_class_(cache_class)
+{
+ local_zone_data_ = LocalZoneDataPtr(new LocalZoneData(cache_class_.getCode()));
+ rrsets_cache_ = RRsetCachePtr(new RRsetCache(RRSET_CACHE_DEFAULT_SIZE,
+ cache_class_.getCode()));
+ messages_cache_ = MessageCachePtr(new MessageCache(rrsets_cache_,
+ MESSAGE_CACHE_DEFAULT_SIZE,
+ cache_class_.getCode()));
+}
+
+ResolverClassCache::ResolverClassCache(CacheSizeInfo cache_info) :
+ cache_class_(cache_info.cclass)
+{
+ uint16_t klass = cache_class_.getCode();
+ // TODO We should find one way to load local zone data.
+ local_zone_data_ = LocalZoneDataPtr(new LocalZoneData(klass));
+ rrsets_cache_ = RRsetCachePtr(new
+ RRsetCache(cache_info.rrset_cache_size, klass));
+ messages_cache_ = MessageCachePtr(new MessageCache(rrsets_cache_,
+ cache_info.message_cache_size,
+ klass));
+}
+
+const RRClass&
+ResolverClassCache::getClass() const {
+ return cache_class_;
+}
+
+bool
+ResolverClassCache::lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ isc::dns::Message& response) const
+{
+ // message response should has question section already.
+ if (response.beginQuestion() == response.endQuestion()) {
+ isc_throw(MessageNoQuestionSection, "Message has no question section");
+ }
+
+ // First, query in local zone, if the rrset(qname, qtype, qclass) can be
+ // found in local zone, generated reply message with only the rrset in
+ // answer section.
+ RRsetPtr rrset_ptr = local_zone_data_->lookup(qname, qtype);
+ if (rrset_ptr) {
+ response.addRRset(Message::SECTION_ANSWER, rrset_ptr);
+ return (true);
+ }
+
+ // Search in class-specific message cache.
+ return (messages_cache_->lookup(qname, qtype, response));
+}
+
+isc::dns::RRsetPtr
+ResolverClassCache::lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype) const
+{
+ // Algorithm:
+ // 1. Search in local zone data first,
+ // 2. Then do search in rrsets_cache_.
+ RRsetPtr rrset_ptr = local_zone_data_->lookup(qname, qtype);
+ if (rrset_ptr) {
+ return (rrset_ptr);
+ } else {
+ RRsetEntryPtr rrset_entry = rrsets_cache_->lookup(qname, qtype);
+ if (rrset_entry) {
+ return (rrset_entry->getRRset());
+ } else {
+ return (RRsetPtr());
+ }
+ }
+}
+
+bool
+ResolverClassCache::update(const isc::dns::Message& msg) {
+ return (messages_cache_->update(msg));
+}
+
+bool
+ResolverClassCache::updateRRsetCache(const isc::dns::ConstRRsetPtr rrset_ptr,
+ RRsetCachePtr rrset_cache_ptr)
+{
+ RRsetTrustLevel level;
+ if (rrset_ptr->getType() == RRType::A() ||
+ rrset_ptr->getType() == RRType::AAAA()) {
+ level = RRSET_TRUST_PRIM_GLUE;
+ } else {
+ level = RRSET_TRUST_PRIM_ZONE_NONGLUE;
+ }
+
+ rrset_cache_ptr->update((*rrset_ptr.get()), level);
+ return (true);
+}
+
+bool
+ResolverClassCache::update(const isc::dns::ConstRRsetPtr rrset_ptr) {
+ // First update local zone, then update rrset cache.
+ local_zone_data_->update((*rrset_ptr.get()));
+ updateRRsetCache(rrset_ptr, rrsets_cache_);
+ return (true);
+}
+
+
+ResolverCache::ResolverCache()
+{
+ class_caches_.push_back(new ResolverClassCache(RRClass::IN()));
+}
+
+ResolverCache::ResolverCache(std::vector<CacheSizeInfo> caches_info)
+{
+ for (int i = 0; i < caches_info.size(); ++i) {
+ class_caches_.push_back(new ResolverClassCache(caches_info[i]));
+ }
+}
+
+ResolverCache::~ResolverCache()
+{
+ for (int i = 0; i < class_caches_.size(); ++i) {
+ delete class_caches_[i];
+ }
+}
+
+bool
+ResolverCache::lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ const isc::dns::RRClass& qclass,
+ isc::dns::Message& response) const
+{
+ ResolverClassCache* cc = getClassCache(qclass);
+ if (cc) {
+ return (cc->lookup(qname, qtype, response));
+ } else {
+ return (false);
+ }
+}
+
+isc::dns::RRsetPtr
+ResolverCache::lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ const isc::dns::RRClass& qclass) const
+{
+ ResolverClassCache* cc = getClassCache(qclass);
+ if (cc) {
+ return (cc->lookup(qname, qtype));
+ } else {
+ return (RRsetPtr());
+ }
+}
+
+isc::dns::RRsetPtr
+ResolverCache::lookupClosestRRset(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ const isc::dns::RRClass& qclass) const
+{
+ ResolverClassCache* cc = getClassCache(qclass);
+ if (cc) {
+ unsigned int count = qname.getLabelCount();
+ unsigned int level = 0;
+ while(level < count) {
+ Name close_name = qname.split(level);
+ RRsetPtr rrset_ptr = cc->lookup(close_name, qtype);
+ if (rrset_ptr) {
+ return (rrset_ptr);
+ } else {
+ ++level;
+ }
+ }
+ }
+
+ return (RRsetPtr());
+}
+
+bool
+ResolverCache::update(const isc::dns::Message& msg) {
+
+ QuestionIterator iter = msg.beginQuestion();
+ ResolverClassCache* cc = getClassCache((*iter)->getClass());
+ if (cc) {
+ return (cc->update(msg));
+ } else {
+ return (false);
+ }
+}
+
+bool
+ResolverCache::update(const isc::dns::ConstRRsetPtr rrset_ptr) {
+ ResolverClassCache* cc = getClassCache(rrset_ptr->getClass());
+ if (cc) {
+ return (cc->update(rrset_ptr));
+ } else {
+ return (false);
+ }
+}
+
+void
+ResolverCache::dump(const std::string&) {
+ //TODO
+}
+
+void
+ResolverCache::load(const std::string&) {
+ //TODO
+}
+
+ResolverClassCache*
+ResolverCache::getClassCache(const isc::dns::RRClass& cache_class) const {
+ for (int i = 0; i < class_caches_.size(); ++i) {
+ if (class_caches_[i]->getClass() == cache_class) {
+ return class_caches_[i];
+ }
+ }
+ return NULL;
+}
+
+} // namespace cache
+} // namespace isc
+
diff --git a/src/lib/cache/resolver_cache.h b/src/lib/cache/resolver_cache.h
new file mode 100644
index 0000000..7a0fdab
--- /dev/null
+++ b/src/lib/cache/resolver_cache.h
@@ -0,0 +1,331 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __RESOLVER_CACHE_H
+#define __RESOLVER_CACHE_H
+
+#include <map>
+#include <string>
+#include <boost/shared_ptr.hpp>
+#include <dns/rrclass.h>
+#include <dns/message.h>
+#include <exceptions/exceptions.h>
+#include "message_cache.h"
+#include "rrset_cache.h"
+#include "local_zone_data.h"
+
+namespace isc {
+namespace cache {
+class RRsetCache;
+
+//TODO a better proper default cache size
+#define MESSAGE_CACHE_DEFAULT_SIZE 10000
+#define RRSET_CACHE_DEFAULT_SIZE 20000
+
+/// \brief Cache Size Information.
+///
+/// Used to initialize the size of class-specific rrset/message cache.
+struct CacheSizeInfo
+{
+public:
+ /// \brief Constructor
+ ///
+ /// \param cls The RRClass code
+ /// \param msg_cache_size The size for the message cache
+ /// \param rst_cache_size The size for the RRset cache
+ CacheSizeInfo(const isc::dns::RRClass& cls,
+ uint32_t msg_cache_size,
+ uint32_t rst_cache_size):
+ cclass(cls),
+ message_cache_size(msg_cache_size),
+ rrset_cache_size(rst_cache_size)
+ {}
+
+ isc::dns::RRClass cclass; // class of the cache.
+ uint32_t message_cache_size; // the size for message cache.
+ uint32_t rrset_cache_size; // The size for rrset cache.
+};
+
+/// \brief Message has no question section.
+///
+/// Thrown if the given message has no question section when looking up
+/// the message in cache.
+class MessageNoQuestionSection : public isc::Exception {
+public:
+ MessageNoQuestionSection(const char*file, size_t line, const char*what) :
+ isc::Exception(file, line, what)
+ {}
+};
+
+/// \brief Class-specific Resolver Cache.
+///
+/// The object of ResolverCache represents the cache of the resolver. It may hold
+/// a list of message/rrset cache which are in different class.
+///
+/// \note Public interaction with the cache should be through ResolverCache,
+/// not directly with this one. (TODO: make this private/hidden/local to the .cc?)
+class ResolverClassCache {
+public:
+ /// \brief Default Constructor.
+ ///
+ /// Only support for class "IN", and message cache size is
+ /// MESSAGE_CACHE_DEFAULT_SIZE, rrset cache size is
+ /// RRSET_CACHE_DEFAULT_SIZE
+ ResolverClassCache(const isc::dns::RRClass& cache_class);
+
+ /// \brief Construct Function.
+ /// \param caches_size cache size information for each
+ /// messages/rrsets of different classes.
+ ResolverClassCache(CacheSizeInfo cache_info);
+
+ /// \name Lookup Interfaces
+ //@{
+ /// \brief Look up message in cache.
+ ///
+ /// \param qname The query name to look up
+ /// \param qtype The query type to look up
+ /// \param response the query message (must be in RENDER mode)
+ /// which has question section already (exception
+ /// MessageNoQeustionSection will be thrown if it has
+ /// no question section). If the message can be found
+ /// in cache, rrsets for the message will be added to
+ /// different sections(answer, authority, additional).
+ /// \return return true if the message can be found, or else,
+ /// return false.
+ bool lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ isc::dns::Message& response) const;
+
+ /// \brief Look up rrset in cache.
+ ///
+ /// \param qname The query name to look up
+ /// \param qtype The query type to look up
+ ///
+ /// \return return the shared_ptr of rrset if it can be found,
+ /// or else, return NULL. When looking up, local zone
+ /// data will be searched first, if not found, then
+ /// search in rrset cache.
+ ///
+ /// \overload
+ ///
+ isc::dns::RRsetPtr lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype) const;
+
+ /// \brief Update the message in the cache with the new one.
+ ///
+ /// \param msg The message to update
+ ///
+ /// \return return true if the message is updated successfully,
+ /// or else, return false.
+ ///
+ /// \note the function doesn't do any message validation check,
+ /// the user should make sure the message is valid, and of
+ /// the right class
+ bool update(const isc::dns::Message& msg);
+
+ /// \brief Update the rrset in the cache with the new one.
+ ///
+ /// local zone data and rrset cache will be updated together.
+ /// If the rrset doesn't exist in both of them, then the rrset
+ /// will be added into both of them.
+ ///
+ /// \param rrset_ptr The RRset to update
+ ///
+ /// \return return false, if the class of the parameter rrset is
+ /// allowed to be cached.
+ ///
+ /// \overload
+ ///
+ /// \note The class of the RRset must have been checked. It is not
+ /// here.
+ bool update(const isc::dns::ConstRRsetPtr rrset_ptr);
+
+ /// \brief Get the RRClass this cache is for
+ ///
+ /// \return The RRClass of this cache
+ const isc::dns::RRClass& getClass() const;
+
+private:
+ /// \brief Update rrset cache.
+ ///
+ /// \param rrset_ptr The rrset to update with
+ /// \param rrset_cache_ptr the rrset cache to update
+ ///
+ /// \return return true if the rrset is updated in the rrset cache,
+ /// or else return false if failed.
+ /// \param rrset_cache_ptr The rrset cache need to be updated.
+ bool updateRRsetCache(const isc::dns::ConstRRsetPtr rrset_ptr,
+ RRsetCachePtr rrset_cache_ptr);
+
+ /// \brief Class this cache is for.
+ const isc::dns::RRClass cache_class_;
+
+ /// \brief map of message caches for configured classes(each message
+ /// cache is class-specific)
+ MessageCachePtr messages_cache_;
+
+ /// \name rrset caches
+ //@{
+ /// \brief Local Zone data cache
+ /// Cache for rrsets in local zones, rrsets
+ /// in it never expire.
+ LocalZoneDataPtr local_zone_data_;
+
+ /// \brief cache the rrsets parsed from the received message.
+ RRsetCachePtr rrsets_cache_;
+ //@}
+};
+
+class ResolverCache {
+public:
+ /// \brief Default Constructor.
+ ///
+ /// Right now, only support for class "IN", and message cache size is
+ /// MESSAGE_CACHE_DEFAULT_SIZE, rrset cache size is
+ /// RRSET_CACHE_DEFAULT_SIZE
+ ResolverCache();
+
+ /// \brief Construct Function.
+ /// \param caches_size cache size information for each
+ /// messages/rrsets of different classes.
+ ResolverCache(std::vector<CacheSizeInfo> caches_size);
+
+ /// \brief Destructor
+ ~ResolverCache();
+
+ /// \name Lookup Interfaces
+ //@{
+ /// \brief Look up message in cache.
+ ///
+ /// \param qname The query name to look up
+ /// \param qtype The query type to look up
+ /// \param qclass The query class to look up
+ /// \param response the query message (must be in RENDER mode)
+ /// which has question section already (exception
+ /// MessageNoQeustionSection will be thrown if it has
+ /// no question section). If the message can be found
+ /// in cache, rrsets for the message will be added to
+ /// different sections(answer, authority, additional).
+ /// \return return true if the message can be found, or else,
+ /// return false.
+ bool lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ const isc::dns::RRClass& qclass,
+ isc::dns::Message& response) const;
+
+ /// \brief Look up rrset in cache.
+ ///
+ /// \param qname The query name to look up
+ /// \param qtype The query type to look up
+ /// \param qclass The query class to look up
+ ///
+ /// \return return the shared_ptr of rrset if it can be found,
+ /// or else, return NULL. When looking up, local zone
+ /// data will be searched first, if not found, then
+ /// search in rrset cache.
+ ///
+ /// \overload
+ ///
+ isc::dns::RRsetPtr lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ const isc::dns::RRClass& qclass) const;
+
+ /// \brief Look up closest rrset in cache.
+ ///
+ /// \param qname The query name to look up
+ /// \param qtype The query type to look up
+ /// \param qclass The query class to look up
+ ///
+ /// \return return the shared_ptr of rrset if it can be found in
+ /// cache, or else return NULL.
+ ///
+ /// Currently the implementation is: search exact rrset
+ /// label by lable, If the rrset can't be found, remove the last
+ /// label, then search again. The efficiency may be very low when
+ /// the name of rrset is very long but it's closest rrset's name
+ /// is very short.
+ /// If a good perfermance is needed when looking up the closest rrset,
+ /// rrset cache structure(HashTable) should be redesigned. By using
+ /// HashTable, it can only garantee the performance for looking
+ /// up exact rrset.
+ /// So here there is another question, which rrset looking up interface
+ /// is used frequently? Exact or closest looking up.
+ isc::dns::RRsetPtr lookupClosestRRset(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ const isc::dns::RRClass& qclass) const;
+ //@}
+
+ /// \brief Update the message in the cache with the new one.
+ ///
+ /// \param msg The message to update
+ ///
+ /// \return return true if the message is updated successfully,
+ /// or else, return false.
+ ///
+ /// \note the function doesn't do any message validation check,
+ /// the user should make sure the message is valid.
+ bool update(const isc::dns::Message& msg);
+
+ /// \brief Update the rrset in the cache with the new one.
+ ///
+ /// local zone data and rrset cache will be updated together.
+ /// If the rrset doesn't exist in both of them, then the rrset
+ /// will be added into both of them.
+ ///
+ /// \param rrset_ptr The RRset to update
+ ///
+ /// \return return false, if the class of the parameter rrset is
+ /// allowed to be cached.
+ ///
+ /// \overload
+ ///
+ bool update(const isc::dns::ConstRRsetPtr rrset_ptr);
+
+ /// \name Cache Serialization
+ //@{
+ /// \brief Dump the cache content to one file.
+ ///
+ /// \param file_name file to write to
+ ///
+ /// \todo It should can be dumped to one configured database.
+ void dump(const std::string& file_name);
+
+ /// \brief Load the cache from one file.
+ ///
+ /// \param file to load from
+ ///
+ /// \todo It should can be loaded from one configured database.
+ void load(const std::string& file_name);
+ //@}
+
+private:
+ /// \brief Returns the class-specific subcache
+ ///
+ /// \param cache_class the class to get the subcache for
+ /// \return The subcache, or NULL if there is no cache for this class
+ ResolverClassCache* getClassCache(const isc::dns::RRClass& cache_class) const;
+
+ /// The class-specific caches.
+ /// TODO: I think we can optimize for IN, and always have that
+ /// one directly available, use the vector for the rest?
+ std::vector<ResolverClassCache*> class_caches_;
+};
+
+} // namespace cache
+} // namespace isc
+
+#endif // __RESOLVER_CACHE_H
+
diff --git a/src/lib/cache/rrset_cache.cc b/src/lib/cache/rrset_cache.cc
new file mode 100644
index 0000000..86ee867
--- /dev/null
+++ b/src/lib/cache/rrset_cache.cc
@@ -0,0 +1,106 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <config.h>
+
+#include <string>
+#include "rrset_cache.h"
+#include <nsas/nsas_entry_compare.h>
+#include <nsas/hash_table.h>
+#include <nsas/hash_deleter.h>
+
+using namespace isc::nsas;
+using namespace isc::dns;
+using namespace std;
+
+namespace isc {
+namespace cache {
+
+RRsetCache::RRsetCache(uint32_t cache_size,
+ uint16_t rrset_class):
+ class_(rrset_class),
+ rrset_table_(new NsasEntryCompare<RRsetEntry>, cache_size),
+ rrset_lru_((3 * cache_size),
+ new HashDeleter<RRsetEntry>(rrset_table_))
+{
+}
+
+RRsetEntryPtr
+RRsetCache::lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype)
+{
+ const string entry_name = genCacheEntryName(qname, qtype);
+ RRsetEntryPtr entry_ptr = rrset_table_.get(HashKey(entry_name, RRClass(class_)));
+
+ //If the rrset entry has expired, return NULL.
+ if(entry_ptr && (time(NULL) > entry_ptr->getExpireTime())) {
+ return (RRsetEntryPtr());
+ }
+ return (entry_ptr);
+}
+
+RRsetEntryPtr
+RRsetCache::update(const isc::dns::RRset& rrset, const RRsetTrustLevel& level) {
+ // TODO: If the RRset is an NS, we should update the NSAS as well
+
+ // lookup first
+ RRsetEntryPtr entry_ptr = lookup(rrset.getName(), rrset.getType());
+ if(!entry_ptr) {
+ // rrset entry doesn't exist, create one rrset entry for the rrset
+ // and add it directly.
+ entry_ptr.reset(new RRsetEntry(rrset, level));
+ // Replace the expired rrset entry if it exists.
+ rrset_table_.add(entry_ptr, entry_ptr->hashKey(), true);
+ //TODO , lru list touch.
+ return (entry_ptr);
+ } else {
+ // there is one rrset entry in the cache, need to check whether
+ // the new rrset is more authoritative.
+ if (entry_ptr->getTrustLevel() > level) {
+ // existed rrset entry is more authoritative, do nothing,
+ // just return it.
+ //TODO, lru list touch
+ return (entry_ptr);
+ } else {
+ HashKey key = entry_ptr->hashKey();
+ entry_ptr.reset(new RRsetEntry(rrset, level));
+ //TODO, lru list touch.
+ // Replace the expired rrset entry if it exists.
+ rrset_table_.add(entry_ptr, entry_ptr->hashKey(), true);
+ return (entry_ptr);
+ }
+ }
+}
+
+void
+RRsetCache::dump(const std::string&) {
+ //TODO
+}
+
+void
+RRsetCache::load(const std::string&) {
+ //TODO
+}
+
+bool
+RRsetCache::resize(uint32_t) {
+ //TODO
+ return (true);
+}
+
+} // namespace cache
+} // namespace isc
+
diff --git a/src/lib/cache/rrset_cache.h b/src/lib/cache/rrset_cache.h
new file mode 100644
index 0000000..d082b3c
--- /dev/null
+++ b/src/lib/cache/rrset_cache.h
@@ -0,0 +1,108 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __RRSET_CACHE_H
+#define __RRSET_CACHE_H
+
+#include <rrset_entry.h>
+#include <nsas/hash_table.h>
+#include <nsas/lru_list.h>
+
+using namespace isc::nsas;
+
+namespace isc {
+namespace cache {
+
+class RRsetEntry;
+
+/// \brief RRset Cache
+/// The object of RRsetCache represented the cache for class-specific
+/// RRsets.
+class RRsetCache{
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are intentionally
+ /// defined as private to make it uncopyable
+ //@{
+private:
+ RRsetCache(const RRsetCache&);
+ RRsetCache& operator=(const RRsetCache&);
+public:
+ /// \brief Constructor
+ ///
+ /// \param cache_size the size of rrset cache.
+ /// \param rrset_class the class of rrset cache.
+ RRsetCache(uint32_t cache_size, uint16_t rrset_class);
+ ~RRsetCache() {}
+ //@}
+
+ /// \brief Look up rrset in cache.
+ ///
+ /// \param qname The query name to look up
+ /// \param qtype The query type
+ /// \return return the shared_ptr of rrset entry if it can be
+ /// found in the cache, or else, return NULL.
+ RRsetEntryPtr lookup(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype);
+
+ /// \brief Update RRset Cache
+ /// Update the rrset entry in the cache with the new one.
+ /// If the rrset has expired or doesn't exist in the cache,
+ /// it will be added directly. It may be ingored if the new
+ /// rrset is not more authoritative than the old rrset in cache.
+ ///
+ /// \param rrset The new rrset used to update cache.
+ /// \param level trustworthiness of the rrset.
+ /// \return return the rrset entry in the cache, it may be the
+ /// new added rrset entry or existed one if it is not replaced.
+ RRsetEntryPtr update(const isc::dns::RRset& rrset,
+ const RRsetTrustLevel& level);
+
+ /// \brief Dump the rrset cache to specified file.
+ ///
+ /// \param file_name The file to write to
+ ///
+ /// \todo It should can be dumped to one configured database.
+ void dump(const std::string& file_name);
+
+ /// \brief Load the cache from one file.
+ ///
+ /// \param file_name The file to read from
+ ///
+ /// \todo It should can be loaded from one configured database.
+ void load(const std::string& file_name);
+
+ /// \brief Resize the size of rrset cache in runtime.
+ ///
+ /// \param The size to resize to
+ /// \return true
+ bool resize(uint32_t size);
+
+private:
+ uint16_t class_; // The class of the rrset cache.
+ isc::nsas::HashTable<RRsetEntry> rrset_table_;
+ isc::nsas::LruList<RRsetEntry> rrset_lru_;
+};
+
+typedef boost::shared_ptr<RRsetCache> RRsetCachePtr;
+typedef boost::shared_ptr<const RRsetCache> ConstRRsetCachePtr;
+
+} // namespace cache
+} // namespace isc
+
+#endif // __RRSET_CACHE_H
+
diff --git a/src/lib/cache/rrset_copy.cc b/src/lib/cache/rrset_copy.cc
new file mode 100644
index 0000000..85ba153
--- /dev/null
+++ b/src/lib/cache/rrset_copy.cc
@@ -0,0 +1,40 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include "rrset_copy.h"
+
+using namespace isc::dns;
+
+namespace isc {
+namespace cache {
+
+void
+rrsetCopy(const isc::dns::RRset& src, isc::dns::RRset& dst) {
+ RdataIteratorPtr rdata_itor = src.getRdataIterator();
+ rdata_itor->first();
+ while(!rdata_itor->isLast()){
+ dst.addRdata(rdata_itor->getCurrent());
+ rdata_itor->next();
+ }
+
+ RRsetPtr rrsig = src.getRRsig();
+ if (rrsig != NULL){
+ dst.addRRsig(rrsig);
+ }
+}
+
+} // namespace cache
+} // namespace isc
diff --git a/src/lib/cache/rrset_copy.h b/src/lib/cache/rrset_copy.h
new file mode 100644
index 0000000..f6bee55
--- /dev/null
+++ b/src/lib/cache/rrset_copy.h
@@ -0,0 +1,44 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __RRSET_COPY_
+#define __RRSET_COPY_
+
+#include <dns/rrset.h>
+
+namespace isc {
+namespace cache {
+
+/// \brief RRset Copy Function
+///
+/// Adds all Rdatas and the RRsig in the source RRset to the target
+/// RRset
+///
+/// \param src RRset to copy from
+/// \param dst RRset to copy to
+///
+/// \note RRset class doesn't provide the interface for
+/// doing RRset copy. But in cache's code, sometime
+/// we have to do the copy.
+
+void
+rrsetCopy(const isc::dns::RRset& src, isc::dns::RRset& dst);
+
+} // namespace cache
+} // namespace isc
+
+#endif // __RRSET_COPY_
+
diff --git a/src/lib/cache/rrset_entry.cc b/src/lib/cache/rrset_entry.cc
new file mode 100644
index 0000000..9407e97
--- /dev/null
+++ b/src/lib/cache/rrset_entry.cc
@@ -0,0 +1,68 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <config.h>
+
+#include <dns/message.h>
+#include <nsas/nsas_entry.h>
+#include <nsas/fetchable.h>
+#include "rrset_entry.h"
+#include "rrset_copy.h"
+
+using namespace isc::dns;
+
+namespace isc {
+namespace cache {
+
+RRsetEntry::RRsetEntry(const isc::dns::RRset& rrset, const RRsetTrustLevel& level):
+ entry_name_(genCacheEntryName(rrset.getName(), rrset.getType())),
+ expire_time_(time(NULL) + rrset.getTTL().getValue()),
+ trust_level_(level),
+ rrset_(new RRset(rrset.getName(), rrset.getClass(), rrset.getType(), rrset.getTTL())),
+ hash_key_(HashKey(entry_name_, rrset_->getClass()))
+{
+ rrsetCopy(rrset, *(rrset_.get()));
+}
+
+isc::dns::RRsetPtr
+RRsetEntry::getRRset() {
+ updateTTL();
+ return (rrset_);
+}
+
+time_t
+RRsetEntry::getExpireTime() const {
+ return (expire_time_);
+}
+
+void
+RRsetEntry::updateTTL(){
+ uint32_t oldTTL = rrset_->getTTL().getValue();
+ if(oldTTL == 0) {
+ return;
+ }
+
+ uint32_t now = time(NULL);
+ uint32_t newTTL = now < expire_time_ ? (expire_time_ - now) : 0;
+
+ RRTTL ttl(newTTL);
+ rrset_->setTTL(ttl);
+}
+
+} // namespace cache
+} // namespace isc
+
+
diff --git a/src/lib/cache/rrset_entry.h b/src/lib/cache/rrset_entry.h
new file mode 100644
index 0000000..f0149a4
--- /dev/null
+++ b/src/lib/cache/rrset_entry.h
@@ -0,0 +1,137 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __RRSET_ENTRY_H
+#define __RRSET_ENTRY_H
+
+#include <dns/rrset.h>
+#include <dns/message.h>
+#include <dns/rrttl.h>
+#include <nsas/nsas_entry.h>
+#include <nsas/fetchable.h>
+#include "cache_entry_key.h"
+
+using namespace isc::nsas;
+
+namespace isc {
+namespace cache {
+
+/// \enum RRset Trustworthiness
+/// For detail of RRset trustworthiness, please refer to
+/// RFC2181 section5.4.1.
+/// Bigger value is more trustworthy.
+enum RRsetTrustLevel {
+ /// Default trust for RRset.
+ RRSET_TRUST_DEFAULT = 0,
+ /// Additional information from non-authoritative answer.
+ RRSET_TRUST_ADDITIONAL_NONAA,
+ /// Data from the authority section of a non-authoritative answer
+ RRSET_TRUST_AUTHORITY_NONAA,
+ /// Additional information from an authoritative answer.
+ RRSET_TRUST_ADDITIONAL_AA,
+ /// Non-authoritative data from the answer section of authoritative
+ /// answers
+ RRSET_TRUST_NONAUTH_ANSWER_AA,
+ /// Data from the answer section of a non-authoritative answer.
+ RRSET_TRUST_ANSWER_NONAA,
+ /// Glue from a primary zone, or glue from a zone transfer.
+ RRSET_TRUST_PRIM_GLUE,
+ /// Data from the authority section of an authoritative answer.
+ RRSET_TRUST_AUTHORITY_AA,
+ /// Authoritative data included in the answer section of
+ /// an authoritative reply.
+ RRSET_TRUST_ANSWER_AA,
+ /// Data from a primary zone file, other than glue data.
+ RRSET_TRUST_PRIM_ZONE_NONGLUE
+};
+
+/// \brief RRset Entry
+/// The object of RRsetEntry represents one cached RRset.
+/// Each RRset entry may be refered using shared_ptr by several message
+/// entries.
+class RRsetEntry : public NsasEntry<RRsetEntry>
+{
+ ///
+ /// \name Constructors and Destructor
+ ///
+ /// Note: The copy constructor and the assignment operator are intentionally
+ /// defined as private to make it uncopyable
+ //@{
+private:
+ RRsetEntry(const RRsetEntry&);
+ RRsetEntry& operator=(const RRsetEntry&);
+public:
+ /// \brief Constructor
+ /// \param rrset The RRset used to initialize the RRset entry.
+ /// \param level trustworthiness of the RRset.
+ RRsetEntry(const isc::dns::RRset& rrset, const RRsetTrustLevel& level);
+
+ /// The destructor.
+ ~RRsetEntry() {}
+ //@}
+
+ /// \brief Return a pointer to a generated RRset
+ ///
+ /// \return Pointer to the generated RRset
+ isc::dns::RRsetPtr getRRset();
+
+ /// \brief Get the expiration time of the RRset.
+ ///
+ /// \return The expiration time of the RRset
+ ///
+ /// \todo RRsig expiration processing
+ time_t getExpireTime() const;
+
+ /// \brief Get the ttl of the RRset.
+ ///
+ /// \return The TTL of the RRset
+ uint32_t getTTL() {
+ updateTTL();
+ return (rrset_->getTTL().getValue());
+ }
+
+ /// \brief Get the hash key
+ ///
+ /// \return return hash key
+ HashKey hashKey() const {
+ return (hash_key_);
+ }
+
+ /// \brief get RRset trustworthiness
+ ///
+ /// \return return the trust level
+ RRsetTrustLevel getTrustLevel() const {
+ return (trust_level_);
+ }
+private:
+ /// \brief Update TTL according to expiration time
+ void updateTTL();
+
+private:
+ std::string entry_name_; // The entry name for this rrset entry.
+ time_t expire_time_; // Expiration time of rrset.
+ RRsetTrustLevel trust_level_; // RRset trustworthiness.
+ boost::shared_ptr<isc::dns::RRset> rrset_;
+ HashKey hash_key_; // RRsetEntry hash key
+};
+
+typedef boost::shared_ptr<RRsetEntry> RRsetEntryPtr;
+
+} // namespace cache
+} // namespace isc
+
+#endif // __RRSET_ENTRY_H
+
diff --git a/src/lib/cache/tests/Makefile.am b/src/lib/cache/tests/Makefile.am
new file mode 100644
index 0000000..b93c9a7
--- /dev/null
+++ b/src/lib/cache/tests/Makefile.am
@@ -0,0 +1,66 @@
+SUBDIRS = .
+
+AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
+AM_CPPFLAGS += $(BOOST_INCLUDES) $(MULTITHREADING_FLAG)
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/dns -I$(top_builddir)/src/lib/dns
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/cache -I$(top_builddir)/src/lib/cache
+AM_CPPFLAGS += -DTEST_DATA_SRCDIR=\"$(srcdir)/testdata\"
+AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/lib/cache/tests/testdata\"
+AM_CXXFLAGS = $(B10_CXXFLAGS)
+
+
+AM_LDFLAGS = $(PTHREAD_LDFLAGS)
+if USE_STATIC_LINK
+AM_LDFLAGS += -static
+endif
+
+# Some versions of GCC warn about some versions of Boost regarding
+# missing initializer for members in its posix_time.
+# https://svn.boost.org/trac/boost/ticket/3477
+# But older GCC compilers don't have the flag.
+AM_CXXFLAGS += $(WARNING_NO_MISSING_FIELD_INITIALIZERS_CFLAG)
+
+if USE_CLANGPP
+# see ../Makefile.am
+AM_CXXFLAGS += -Wno-unused-parameter
+endif
+
+CLEANFILES = *.gcno *.gcda
+
+TESTS =
+if HAVE_GTEST
+TESTS += run_unittests
+run_unittests_SOURCES = run_unittests.cc
+run_unittests_SOURCES += $(top_srcdir)/src/lib/dns/tests/unittest_util.cc
+run_unittests_SOURCES += rrset_entry_unittest.cc
+run_unittests_SOURCES += rrset_cache_unittest.cc
+run_unittests_SOURCES += message_cache_unittest.cc
+run_unittests_SOURCES += message_entry_unittest.cc
+run_unittests_SOURCES += local_zone_data_unittest.cc
+run_unittests_SOURCES += resolver_cache_unittest.cc
+run_unittests_SOURCES += cache_test_messagefromfile.h
+run_unittests_SOURCES += cache_test_sectioncount.h
+
+run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
+run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
+run_unittests_LDADD = $(GTEST_LDADD)
+
+# NOTE: we may have to clean up this hack later (see the note in configure.ac)
+if NEED_LIBBOOST_THREAD
+run_unittests_LDADD += -lboost_thread
+endif
+
+run_unittests_LDADD += $(top_builddir)/src/lib/cache/libcache.la
+run_unittests_LDADD += $(top_builddir)/src/lib/nsas/libnsas.la
+run_unittests_LDADD += $(top_builddir)/src/lib/dns/libdns++.la
+run_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libexceptions.la
+endif
+
+noinst_PROGRAMS = $(TESTS)
+
+EXTRA_DIST = testdata/message_fromWire1
+EXTRA_DIST += testdata/message_fromWire2
+EXTRA_DIST += testdata/message_fromWire3
+EXTRA_DIST += testdata/message_fromWire4
+EXTRA_DIST += testdata/message_fromWire5
+EXTRA_DIST += testdata/message_fromWire6
diff --git a/src/lib/cache/tests/cache_test_messagefromfile.h b/src/lib/cache/tests/cache_test_messagefromfile.h
new file mode 100644
index 0000000..820d822
--- /dev/null
+++ b/src/lib/cache/tests/cache_test_messagefromfile.h
@@ -0,0 +1,40 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+#include <vector>
+#include <dns/tests/unittest_util.h>
+#include <dns/buffer.h>
+#include <dns/message.h>
+
+using namespace isc;
+using namespace isc::dns;
+
+namespace {
+
+/// \brief Reads a Message from a data file
+///
+/// \param message Message to put the read data in
+/// \param datafile The file to read from
+void
+messageFromFile(Message& message, const char* datafile) {
+ std::vector<unsigned char> data;
+ UnitTestUtil::readWireData(datafile, data);
+
+ InputBuffer buffer(&data[0], data.size());
+ message.fromWire(buffer);
+}
+
+} // namespace
+
diff --git a/src/lib/cache/tests/cache_test_sectioncount.h b/src/lib/cache/tests/cache_test_sectioncount.h
new file mode 100644
index 0000000..a385133
--- /dev/null
+++ b/src/lib/cache/tests/cache_test_sectioncount.h
@@ -0,0 +1,45 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+#include <vector>
+#include <dns/tests/unittest_util.h>
+#include <dns/buffer.h>
+#include <dns/message.h>
+
+using namespace isc;
+using namespace isc::dns;
+
+namespace {
+
+/// \brief Counts the number of rrsets in the given section
+///
+/// \param msg The message to count in
+/// \param section The section to count
+///
+/// \return The number of RRsets in the given section
+int
+sectionRRsetCount(Message& msg, Message::Section section) {
+ int count = 0;
+ for (RRsetIterator rrset_iter = msg.beginSection(section);
+ rrset_iter != msg.endSection(section);
+ ++rrset_iter) {
+ ++count;
+ }
+
+ return count;
+}
+
+} // namespace
+
diff --git a/src/lib/cache/tests/local_zone_data_unittest.cc b/src/lib/cache/tests/local_zone_data_unittest.cc
new file mode 100644
index 0000000..28de4be
--- /dev/null
+++ b/src/lib/cache/tests/local_zone_data_unittest.cc
@@ -0,0 +1,65 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+#include <config.h>
+#include <string>
+#include <gtest/gtest.h>
+#include <cache/local_zone_data.h>
+#include <dns/rrset.h>
+#include <dns/rrttl.h>
+#include "cache_test_messagefromfile.h"
+
+using namespace isc::cache;
+using namespace isc::dns;
+using namespace std;
+
+namespace {
+
+class LocalZoneDataTest: public testing::Test {
+protected:
+ LocalZoneDataTest(): local_zone_data(1)
+ {
+ }
+
+ LocalZoneData local_zone_data;
+};
+
+TEST_F(LocalZoneDataTest, updateAndLookup) {
+ Message msg(Message::PARSE);
+ messageFromFile(msg, "message_fromWire3");
+ RRsetIterator rrset_iter = msg.beginSection(Message::SECTION_AUTHORITY);
+ Name name = (*rrset_iter)->getName();
+ RRType type = (*rrset_iter)->getType();
+
+ EXPECT_FALSE(local_zone_data.lookup(name, type));
+ local_zone_data.update((*(*rrset_iter).get()));
+ EXPECT_TRUE(local_zone_data.lookup(name, type));
+
+ // Test whether the old one is replaced
+ uint32_t ttl = (*rrset_iter)->getTTL().getValue();
+ // Make sure it is not zero
+ ASSERT_NE(ttl / 2, ttl);
+
+ RRsetPtr rrset_ptr = local_zone_data.lookup(name, type);
+ EXPECT_EQ(ttl, rrset_ptr->getTTL().getValue());
+
+ (*rrset_iter)->setTTL(RRTTL(ttl/2));
+
+ local_zone_data.update((*(*rrset_iter).get()));
+ rrset_ptr = local_zone_data.lookup(name, type);
+ EXPECT_EQ(ttl/2, rrset_ptr->getTTL().getValue());
+}
+
+}
diff --git a/src/lib/cache/tests/message_cache_unittest.cc b/src/lib/cache/tests/message_cache_unittest.cc
new file mode 100644
index 0000000..f984312
--- /dev/null
+++ b/src/lib/cache/tests/message_cache_unittest.cc
@@ -0,0 +1,98 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+#include <config.h>
+#include <string>
+#include <gtest/gtest.h>
+#include <dns/tests/unittest_util.h>
+#include <dns/buffer.h>
+#include "../message_cache.h"
+#include "../rrset_cache.h"
+#include "../resolver_cache.h"
+#include "cache_test_messagefromfile.h"
+
+using namespace isc::cache;
+using namespace isc;
+using namespace isc::dns;
+using namespace std;
+
+namespace {
+
+/// \brief Derived from base class to make it easy to test
+/// its internals.
+class DerivedMessageCache: public MessageCache {
+public:
+ DerivedMessageCache(boost::shared_ptr<RRsetCache> rrset_cache_,
+ uint32_t cache_size, uint16_t message_class):
+ MessageCache(rrset_cache_, cache_size, message_class)
+ {}
+
+ uint16_t messages_count() {
+ return message_lru_.size();
+ }
+};
+
+class MessageCacheTest: public testing::Test {
+public:
+ MessageCacheTest(): message_parse(Message::PARSE),
+ message_render(Message::RENDER)
+ {
+ uint16_t class_ = RRClass::IN().getCode();
+ rrset_cache_.reset(new RRsetCache(RRSET_CACHE_DEFAULT_SIZE, class_));
+ message_cache_.reset(new DerivedMessageCache(rrset_cache_,
+ MESSAGE_CACHE_DEFAULT_SIZE, class_ ));
+ }
+
+protected:
+ boost::shared_ptr<DerivedMessageCache> message_cache_;
+ RRsetCachePtr rrset_cache_;
+ Message message_parse;
+ Message message_render;
+};
+
+TEST_F(MessageCacheTest, testLookup) {
+ messageFromFile(message_parse, "message_fromWire1");
+ EXPECT_TRUE(message_cache_->update(message_parse));
+ Name qname("test.example.com.");
+ EXPECT_TRUE(message_cache_->lookup(qname, RRType::A(), message_render));
+ EXPECT_EQ(message_cache_->messages_count(), 1);
+
+ Message message_net(Message::PARSE);
+ messageFromFile(message_net, "message_fromWire2");
+ EXPECT_TRUE(message_cache_->update(message_net));
+ EXPECT_EQ(message_cache_->messages_count(), 2);
+
+ Name qname1("test.example.net.");
+ EXPECT_TRUE(message_cache_->lookup(qname1, RRType::A(), message_render));
+}
+
+TEST_F(MessageCacheTest, testUpdate) {
+ messageFromFile(message_parse, "message_fromWire4");
+ EXPECT_TRUE(message_cache_->update(message_parse));
+
+ Name qname("example.com.");
+ EXPECT_TRUE(message_cache_->lookup(qname, RRType::SOA(), message_render));
+ EXPECT_FALSE(message_render.getHeaderFlag(Message::HEADERFLAG_AA));
+
+ Message new_msg(Message::PARSE);
+ messageFromFile(new_msg, "message_fromWire3");
+ EXPECT_TRUE(message_cache_->update(new_msg));
+ Message new_msg_render(Message::RENDER);
+ EXPECT_TRUE(message_cache_->lookup(qname, RRType::SOA(), new_msg_render));
+ EXPECT_TRUE(new_msg_render.getHeaderFlag(Message::HEADERFLAG_AA));
+}
+
+} // namespace
+
diff --git a/src/lib/cache/tests/message_entry_unittest.cc b/src/lib/cache/tests/message_entry_unittest.cc
new file mode 100644
index 0000000..f0fc777
--- /dev/null
+++ b/src/lib/cache/tests/message_entry_unittest.cc
@@ -0,0 +1,237 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+#include <config.h>
+#include <string>
+#include <gtest/gtest.h>
+#include <dns/tests/unittest_util.h>
+#include <dns/message.h>
+#include <dns/buffer.h>
+#include "../message_entry.h"
+#include "../rrset_cache.h"
+#include "../resolver_cache.h"
+#include "cache_test_messagefromfile.h"
+#include "cache_test_sectioncount.h"
+
+using namespace isc::cache;
+using namespace isc;
+using namespace isc::dns;
+using namespace std;
+
+static uint32_t MAX_UINT32 = numeric_limits<uint32_t>::max();
+
+namespace {
+
+/// \brief Derived from base class to make it easy to test
+/// its internals.
+class DerivedMessageEntry: public MessageEntry {
+public:
+ DerivedMessageEntry(const isc::dns::Message& message,
+ boost::shared_ptr<RRsetCache> rrset_cache_):
+ MessageEntry(message, rrset_cache_)
+ {}
+
+ /// \brief Wrap the protected function so that it can be tested.
+ void parseSectionForTest(const Message& msg,
+ const Message::Section& section,
+ uint32_t& smaller_ttl,
+ uint16_t& rrset_count)
+ {
+ parseSection(msg, section, smaller_ttl, rrset_count);
+ }
+
+ RRsetTrustLevel getRRsetTrustLevelForTest(const Message& message,
+ const RRsetPtr rrset,
+ const Message::Section& section)
+ {
+ return getRRsetTrustLevel(message, rrset, section);
+ }
+
+ bool getRRsetEntriesForTest(vector<RRsetEntryPtr> vec, time_t now) {
+ return getRRsetEntries(vec, now);
+ }
+
+ time_t getExpireTime() {
+ return expire_time_;
+ }
+
+};
+
+class MessageEntryTest: public testing::Test {
+public:
+ MessageEntryTest(): class_(1),
+ message_parse(Message::PARSE),
+ message_render(Message::RENDER)
+ {
+
+ rrset_cache_.reset(new RRsetCache(RRSET_CACHE_DEFAULT_SIZE, class_));
+ }
+
+protected:
+ uint16_t class_;
+ RRsetCachePtr rrset_cache_;
+ Message message_parse;
+ Message message_render;
+};
+
+TEST_F(MessageEntryTest, testParseRRset) {
+ messageFromFile(message_parse, "message_fromWire3");
+ DerivedMessageEntry message_entry(message_parse, rrset_cache_);
+ uint32_t ttl = MAX_UINT32;
+ uint16_t rrset_count = 0;
+ message_entry.parseSectionForTest(message_parse, Message::SECTION_ANSWER, ttl, rrset_count);
+ EXPECT_EQ(ttl, 21600);
+ EXPECT_EQ(rrset_count, 1);
+
+ ttl = MAX_UINT32;
+ message_entry.parseSectionForTest(message_parse, Message::SECTION_AUTHORITY, ttl, rrset_count);
+ EXPECT_EQ(ttl, 21600);
+ EXPECT_EQ(rrset_count, 1);
+
+ ttl = MAX_UINT32;
+ message_entry.parseSectionForTest(message_parse, Message::SECTION_ADDITIONAL, ttl, rrset_count);
+ EXPECT_EQ(ttl, 10800);
+ EXPECT_EQ(rrset_count, 5);
+}
+
+TEST_F(MessageEntryTest, testGetRRsetTrustLevel_AA) {
+ messageFromFile(message_parse, "message_fromWire3");
+ DerivedMessageEntry message_entry(message_parse, rrset_cache_);
+
+
+ RRsetIterator rrset_iter = message_parse.beginSection(Message::SECTION_ANSWER);
+ RRsetTrustLevel level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_ANSWER);
+ EXPECT_EQ(level, RRSET_TRUST_ANSWER_AA);
+
+ rrset_iter = message_parse.beginSection(Message::SECTION_AUTHORITY);
+ level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_AUTHORITY);
+ EXPECT_EQ(level, RRSET_TRUST_AUTHORITY_AA);
+
+ rrset_iter = message_parse.beginSection(Message::SECTION_ADDITIONAL);
+ level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_ADDITIONAL);
+ EXPECT_EQ(level, RRSET_TRUST_ADDITIONAL_AA);
+}
+
+TEST_F(MessageEntryTest, testGetRRsetTrustLevel_NONAA) {
+ messageFromFile(message_parse, "message_fromWire4");
+ DerivedMessageEntry message_entry(message_parse, rrset_cache_);
+ RRsetIterator rrset_iter = message_parse.beginSection(Message::SECTION_ANSWER);
+ RRsetTrustLevel level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_ANSWER);
+ EXPECT_EQ(level, RRSET_TRUST_ANSWER_NONAA);
+
+ rrset_iter = message_parse.beginSection(Message::SECTION_AUTHORITY);
+ level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_AUTHORITY);
+ EXPECT_EQ(level, RRSET_TRUST_AUTHORITY_NONAA);
+
+ rrset_iter = message_parse.beginSection(Message::SECTION_ADDITIONAL);
+ level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_ADDITIONAL);
+ EXPECT_EQ(level, RRSET_TRUST_ADDITIONAL_NONAA);
+}
+
+TEST_F(MessageEntryTest, testGetRRsetTrustLevel_CNAME) {
+ messageFromFile(message_parse, "message_fromWire5");
+ DerivedMessageEntry message_entry(message_parse, rrset_cache_);
+ RRsetIterator rrset_iter = message_parse.beginSection(Message::SECTION_ANSWER);
+ RRsetTrustLevel level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_ANSWER);
+ EXPECT_EQ(level, RRSET_TRUST_ANSWER_AA);
+
+ ++rrset_iter; // Get the rrset after the first cname rrset.
+ level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_ANSWER);
+ EXPECT_EQ(level, RRSET_TRUST_ANSWER_AA);
+}
+
+TEST_F(MessageEntryTest, testGetRRsetTrustLevel_DNAME) {
+ messageFromFile(message_parse, "message_fromWire6");
+ DerivedMessageEntry message_entry(message_parse, rrset_cache_);
+ RRsetIterator rrset_iter = message_parse.beginSection(Message::SECTION_ANSWER);
+ RRsetTrustLevel level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_ANSWER);
+ EXPECT_EQ(level, RRSET_TRUST_ANSWER_AA);
+
+ ++rrset_iter; // Get the rrset after the first dname rrset.
+ level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_ANSWER);
+ EXPECT_EQ(level, RRSET_TRUST_ANSWER_AA);
+
+ ++rrset_iter; // Get the second cname rrset
+ level = message_entry.getRRsetTrustLevelForTest(message_parse,
+ *rrset_iter,
+ Message::SECTION_ANSWER);
+ EXPECT_EQ(level, RRSET_TRUST_ANSWER_AA);
+}
+
+// We only test the expire_time of the message entry.
+// The test for genMessage() will make sure whether InitMessageEntry()
+// is right
+TEST_F(MessageEntryTest, testInitMessageEntry) {
+ messageFromFile(message_parse, "message_fromWire3");
+ DerivedMessageEntry message_entry(message_parse, rrset_cache_);
+ time_t expire_time = message_entry.getExpireTime();
+ // 1 second should be enough to do the compare
+ EXPECT_TRUE((time(NULL) + 10801) > expire_time);
+}
+
+TEST_F(MessageEntryTest, testGetRRsetEntries) {
+ messageFromFile(message_parse, "message_fromWire3");
+ DerivedMessageEntry message_entry(message_parse, rrset_cache_);
+ vector<RRsetEntryPtr> vec;
+
+ // the time is bigger than the smallest expire time of
+ // the rrset in message.
+ time_t expire_time = time(NULL) + 10802;
+ EXPECT_FALSE(message_entry.getRRsetEntriesForTest(vec, expire_time));
+}
+
+TEST_F(MessageEntryTest, testGenMessage) {
+ messageFromFile(message_parse, "message_fromWire3");
+ DerivedMessageEntry message_entry(message_parse, rrset_cache_);
+ time_t expire_time = message_entry.getExpireTime();
+
+ Message msg(Message::RENDER);
+ EXPECT_FALSE(message_entry.genMessage(expire_time + 2, msg));
+ message_entry.genMessage(time(NULL), msg);
+ // Check whether the generated message is same with cached one.
+
+ EXPECT_TRUE(msg.getHeaderFlag(Message::HEADERFLAG_AA));
+ EXPECT_FALSE(msg.getHeaderFlag(Message::HEADERFLAG_TC));
+ EXPECT_EQ(1, sectionRRsetCount(msg, Message::SECTION_ANSWER));
+ EXPECT_EQ(1, sectionRRsetCount(msg, Message::SECTION_AUTHORITY));
+ EXPECT_EQ(5, sectionRRsetCount(msg, Message::SECTION_ADDITIONAL));
+
+ // Check the rrset in answer section.
+ EXPECT_EQ(1, msg.getRRCount(Message::SECTION_ANSWER));
+ EXPECT_EQ(5, msg.getRRCount(Message::SECTION_AUTHORITY));
+ EXPECT_EQ(7, msg.getRRCount(Message::SECTION_ADDITIONAL));
+}
+
+} // namespace
diff --git a/src/lib/cache/tests/resolver_cache_unittest.cc b/src/lib/cache/tests/resolver_cache_unittest.cc
new file mode 100644
index 0000000..a3cf728
--- /dev/null
+++ b/src/lib/cache/tests/resolver_cache_unittest.cc
@@ -0,0 +1,138 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+#include <config.h>
+#include <string>
+#include <gtest/gtest.h>
+#include <dns/rrset.h>
+#include "resolver_cache.h"
+#include "cache_test_messagefromfile.h"
+
+using namespace isc::cache;
+using namespace isc::dns;
+using namespace std;
+
+namespace {
+
+class ResolverCacheTest: public testing::Test {
+public:
+ ResolverCacheTest() {
+ vector<CacheSizeInfo> vec;
+ CacheSizeInfo class_in(RRClass::IN(), 100, 200);
+ CacheSizeInfo class_ch(RRClass::CH(), 100, 200);
+ vec.push_back(class_in);
+ vec.push_back(class_ch);
+ cache = new ResolverCache(vec);
+ }
+
+ ~ResolverCacheTest() {
+ delete cache;
+ }
+
+ ResolverCache* cache;
+};
+
+TEST_F(ResolverCacheTest, testUpdateMessage) {
+ Message msg(Message::PARSE);
+ messageFromFile(msg, "message_fromWire3");
+ cache->update(msg);
+
+ Name qname("example.com.");
+
+ msg.makeResponse();
+ EXPECT_TRUE(cache->lookup(qname, RRType::SOA(), RRClass::IN(), msg));
+ EXPECT_TRUE(msg.getHeaderFlag(Message::HEADERFLAG_AA));
+
+ // Test whether the old message can be updated
+ Message new_msg(Message::PARSE);
+ messageFromFile(new_msg, "message_fromWire4");
+ cache->update(new_msg);
+
+ new_msg.makeResponse();
+ EXPECT_TRUE(cache->lookup(qname, RRType::SOA(), RRClass::IN(), new_msg));
+ EXPECT_FALSE(new_msg.getHeaderFlag(Message::HEADERFLAG_AA));
+}
+#if 0
+TEST_F(ResolverCacheTest, testUpdateRRset) {
+ Message msg(Message::PARSE);
+ messageFromFile(msg, "message_fromWire3");
+ cache->update(msg);
+
+ Name qname("example.com.");
+
+ msg.makeResponse();
+ EXPECT_TRUE(cache->lookup(qname, RRType::SOA(), RRClass::IN(), msg));
+
+ Message except_msg(Message::RENDER);
+ EXPECT_THROW(cache->lookup(qname, RRType::SOA(), RRClass::IN(), except_msg),
+ MessageNoQuestionSection);
+
+ // Get one rrset in the message, then use it to
+ // update rrset cache-> Test whether the local zone
+ // data is updated.
+ RRsetIterator iter = msg.beginSection(Message::SECTION_AUTHORITY);
+ RRsetPtr rrset_ptr = *iter;
+ cache->update(rrset_ptr);
+
+ Message new_msg(Message::RENDER);
+ Question question(qname, klass, RRType::NS());
+ new_msg.addQuestion(question);
+ EXPECT_TRUE(cache->lookup(qname, RRType::NS(), RRClass::IN(), new_msg));
+ EXPECT_EQ(0, sectionRRsetCount(new_msg, Message::SECTION_AUTHORITY));
+ EXPECT_EQ(0, sectionRRsetCount(new_msg, Message::SECTION_ADDITIONAL));
+}
+
+TEST_F(ResolverCacheTest, testLookupUnsupportedClass) {
+ Message msg(Message::PARSE);
+ messageFromFile(msg, "message_fromWire3");
+ cache->update(msg);
+
+ Name qname("example.com.");
+
+ msg.makeResponse();
+ EXPECT_FALSE(cache->lookup(qname, RRType::SOA(), RRClass::CH(), msg));
+ EXPECT_FALSE(cache->lookup(qname, RRType::SOA(), RRClass::CH()));
+}
+
+TEST_F(ResolverCacheTest, testLookupClosestRRset) {
+ Message msg(Message::PARSE);
+ messageFromFile(msg, "message_fromWire3");
+ cache->update(msg);
+
+ Name qname("www.test.example.com.");
+
+ RRsetPtr rrset_ptr = cache->lookupClosestRRset(qname, RRType::NS(),
+ RRClass::IN());
+ EXPECT_TRUE(rrset_ptr);
+ EXPECT_EQ(rrset_ptr->getName(), Name("example.com."));
+
+ rrset_ptr = cache->lookupClosestRRset(Name("example.com."),
+ RRType::NS(), RRClass::IN());
+ EXPECT_TRUE(rrset_ptr);
+ EXPECT_EQ(rrset_ptr->getName(), Name("example.com."));
+
+ rrset_ptr = cache->lookupClosestRRset(Name("com."),
+ RRType::NS(), RRClass::IN());
+ EXPECT_FALSE(rrset_ptr);
+}
+
+TEST_F(ResolverCacheTest, testHasClass) {
+ EXPECT_TRUE(cache->getClassCache(RRClass::IN()));
+ EXPECT_TRUE(cache->getClassCache(RRClass::CH()));
+ EXPECT_FALSE(cache->getClassCache(RRClass::ANY()));
+}
+#endif
+
+}
diff --git a/src/lib/cache/tests/rrset_cache_unittest.cc b/src/lib/cache/tests/rrset_cache_unittest.cc
new file mode 100644
index 0000000..1263406
--- /dev/null
+++ b/src/lib/cache/tests/rrset_cache_unittest.cc
@@ -0,0 +1,84 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+#include <config.h>
+#include <string>
+#include <gtest/gtest.h>
+#include <cache/resolver_cache.h>
+#include <cache/cache_entry_key.h>
+#include <cache/rrset_entry.h>
+#include <cache/rrset_cache.h>
+#include <dns/name.h>
+#include <dns/rrclass.h>
+#include <dns/rrtype.h>
+#include <dns/rrttl.h>
+#include <dns/rrset.h>
+
+using namespace isc::cache;
+using namespace isc::dns;
+using namespace std;
+
+namespace {
+
+class RRsetCacheTest : public testing::Test {
+protected:
+ RRsetCacheTest():
+ cache(RRSET_CACHE_DEFAULT_SIZE, RRClass::IN().getCode()),
+ name("example.com"),
+ rrset1(name, RRClass::IN(), RRType::A(), RRTTL(20)),
+ rrset2(name, RRClass::IN(), RRType::A(), RRTTL(10)),
+ rrset_entry1(rrset1, RRSET_TRUST_ADDITIONAL_AA),
+ rrset_entry2(rrset2, RRSET_TRUST_PRIM_ZONE_NONGLUE)
+ {
+ }
+
+ RRsetCache cache;
+ Name name;
+ RRset rrset1;
+ RRset rrset2;
+ RRsetEntry rrset_entry1;
+ RRsetEntry rrset_entry2;
+};
+
+TEST_F(RRsetCacheTest, lookup) {
+ const RRType& type = RRType::A();
+ EXPECT_TRUE(cache.lookup(name, type) == NULL);
+
+ cache.update(rrset1, rrset_entry1.getTrustLevel());
+ RRsetEntryPtr rrset_entry_ptr = cache.lookup(name, type);
+ EXPECT_EQ(rrset_entry_ptr->getTrustLevel(), rrset_entry1.getTrustLevel());
+ EXPECT_EQ(rrset_entry_ptr->getRRset()->getName(), rrset_entry1.getRRset()->getName());
+ EXPECT_EQ(rrset_entry_ptr->getRRset()->getType(), rrset_entry1.getRRset()->getType());
+ EXPECT_EQ(rrset_entry_ptr->getRRset()->getClass(), rrset_entry1.getRRset()->getClass());
+}
+
+TEST_F(RRsetCacheTest, update) {
+ const RRType& type = RRType::A();
+
+ cache.update(rrset1, rrset_entry1.getTrustLevel());
+ RRsetEntryPtr rrset_entry_ptr = cache.lookup(name, type);
+ EXPECT_EQ(rrset_entry_ptr->getTrustLevel(), rrset_entry1.getTrustLevel());
+
+ cache.update(rrset2, rrset_entry2.getTrustLevel());
+ rrset_entry_ptr = cache.lookup(name, type);
+ // The trust level should be updated
+ EXPECT_EQ(rrset_entry_ptr->getTrustLevel(), rrset_entry2.getTrustLevel());
+
+ cache.update(rrset1, rrset_entry1.getTrustLevel());
+ // The trust level should not be updated
+ EXPECT_EQ(rrset_entry_ptr->getTrustLevel(), rrset_entry2.getTrustLevel());
+}
+
+}
diff --git a/src/lib/cache/tests/rrset_entry_unittest.cc b/src/lib/cache/tests/rrset_entry_unittest.cc
new file mode 100644
index 0000000..29c05b5
--- /dev/null
+++ b/src/lib/cache/tests/rrset_entry_unittest.cc
@@ -0,0 +1,107 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+#include <config.h>
+#include <string>
+#include <gtest/gtest.h>
+#include <cache/cache_entry_key.h>
+#include <cache/rrset_entry.h>
+#include <dns/name.h>
+#include <dns/rrclass.h>
+#include <dns/rrtype.h>
+#include <dns/rrttl.h>
+#include <dns/rrset.h>
+
+using namespace isc::cache;
+using namespace isc::dns;
+using namespace std;
+
+namespace {
+class GenCacheKeyTest: public testing::Test {
+};
+
+TEST_F(GenCacheKeyTest, genCacheEntryKey1) {
+ string name = "example.com.";
+ uint16_t type = 12;
+ string name_type = "example.com.12";
+
+ EXPECT_EQ(name_type, genCacheEntryName(name, type));
+}
+
+TEST_F(GenCacheKeyTest, genCacheEntryKey2) {
+ Name name("example.com");
+ RRType type(1234);
+ string keystr = "example.com.1234";
+ EXPECT_EQ(keystr, genCacheEntryName(name, type));
+}
+
+class DerivedRRsetEntry: public RRsetEntry {
+public:
+ DerivedRRsetEntry(const isc::dns::RRset& rrset, const RRsetTrustLevel& level) : RRsetEntry(rrset, level) {};
+
+ void updateTTLForTest() {
+
+ }
+};
+
+#define TEST_TTL 100
+class RRsetEntryTest : public ::testing::Test {
+protected:
+ RRsetEntryTest():
+ name("test.example.com"),
+ rrset(name, RRClass::IN(), RRType::A(), RRTTL(TEST_TTL)),
+ trust_level(RRSET_TRUST_ADDITIONAL_AA),
+ rrset_entry(rrset, trust_level)
+ {
+ }
+ Name name;
+ RRset rrset;
+ RRsetTrustLevel trust_level;
+ RRsetEntry rrset_entry;
+};
+
+TEST_F(RRsetEntryTest, constructor) {
+ EXPECT_EQ(trust_level, rrset_entry.getTrustLevel());
+ EXPECT_EQ(rrset.getName(), rrset_entry.getRRset()->getName());
+ EXPECT_EQ(rrset.getClass(), rrset_entry.getRRset()->getClass());
+ EXPECT_EQ(rrset.getType(), rrset_entry.getRRset()->getType());
+ EXPECT_EQ(rrset.getRdataCount(), rrset_entry.getRRset()->getRdataCount());
+}
+
+TEST_F(RRsetEntryTest, updateTTL) {
+ uint32_t ttl = rrset_entry.getTTL();
+ sleep(1);
+ // The TTL should be decreased
+ EXPECT_TRUE(rrset_entry.getTTL() < ttl);
+}
+
+TEST_F(RRsetEntryTest, TTLExpire) {
+ RRset exp_rrset(name, RRClass::IN(), RRType::A(), RRTTL(1));
+ RRsetEntry rrset_entry(exp_rrset, RRSET_TRUST_ANSWER_AA);
+ sleep(1);
+ uint32_t ttl = rrset_entry.getTTL();
+ EXPECT_LT(ttl, 1);
+ sleep(1);
+ ttl = rrset_entry.getTTL();
+ EXPECT_LT(ttl, 1);
+}
+
+TEST_F(RRsetEntryTest, getExpireTime){
+ uint32_t exp_time = time(NULL) + TEST_TTL;
+ EXPECT_EQ(exp_time, rrset_entry.getExpireTime());
+}
+
+} // namespace
+
diff --git a/src/lib/cache/tests/run_unittests.cc b/src/lib/cache/tests/run_unittests.cc
new file mode 100644
index 0000000..b34b3dd
--- /dev/null
+++ b/src/lib/cache/tests/run_unittests.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: run_unittests.cc 3020 2010-09-26 03:47:26Z jinmei $
+#include <config.h>
+
+#include <gtest/gtest.h>
+
+#include <dns/tests/unittest_util.h>
+
+int
+main(int argc, char* argv[]) {
+ ::testing::InitGoogleTest(&argc, argv);
+ isc::UnitTestUtil::addDataPath(TEST_DATA_SRCDIR);
+ isc::UnitTestUtil::addDataPath(TEST_DATA_BUILDDIR);
+
+ return (RUN_ALL_TESTS());
+}
diff --git a/src/lib/cache/tests/testdata/message_fromWire1 b/src/lib/cache/tests/testdata/message_fromWire1
new file mode 100644
index 0000000..5b76e3f
--- /dev/null
+++ b/src/lib/cache/tests/testdata/message_fromWire1
@@ -0,0 +1,22 @@
+#
+# A simple DNS response message
+# ID = 0x1035
+# QR=1 (response), Opcode=0, AA=1, RD=1 (other fields are 0)
+# QDCOUNT=1, ANCOUNT=2, other COUNTS=0
+# Question: test.example.com. IN A
+# Answer:
+# test.example.com. 3600 IN A 192.0.2.1
+# test.example.com. 7200 IN A 192.0.2.2
+#
+1035 8500
+0001 0002 0000 0000
+#(4) t e s t (7) e x a m p l e (3) c o m .
+ 04 74 65 73 74 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
+0001 0001
+# same name, fully compressed
+c0 0c
+# TTL=3600, A, IN, RDLENGTH=4, RDATA
+0001 0001 00000e10 0004 c0 00 02 01
+# mostly same, with the slight difference in RDATA and TTL
+c0 0c
+0001 0001 00001c20 0004 c0 00 02 02
diff --git a/src/lib/cache/tests/testdata/message_fromWire2 b/src/lib/cache/tests/testdata/message_fromWire2
new file mode 100644
index 0000000..c8fddbd
--- /dev/null
+++ b/src/lib/cache/tests/testdata/message_fromWire2
@@ -0,0 +1,22 @@
+#
+# A simple DNS response message
+# ID = 0x1035
+# QR=1 (response), Opcode=0, AA=1, RD=1 (other fields are 0)
+# QDCOUNT=1, ANCOUNT=2, other COUNTS=0
+# Question: test.example.net. IN A
+# Answer:
+# test.example.net. 3600 IN A 192.0.2.1
+# test.example.net. 7200 IN A 192.0.2.2
+#
+1035 8500
+0001 0002 0000 0000
+#(4) t e s t (7) e x a m p l e (3) n e t .
+ 04 74 65 73 74 07 65 78 61 6d 70 6c 65 03 6e 65 74 00
+0001 0001
+# same name, fully compressed
+c0 0c
+# TTL=3600, A, IN, RDLENGTH=4, RDATA
+0001 0001 00000e10 0004 c0 00 02 01
+# mostly same, with the slight difference in RDATA and TTL
+c0 0c
+0001 0001 00001c20 0004 c0 00 02 02
diff --git a/src/lib/cache/tests/testdata/message_fromWire3 b/src/lib/cache/tests/testdata/message_fromWire3
new file mode 100644
index 0000000..f7b3a4a
--- /dev/null
+++ b/src/lib/cache/tests/testdata/message_fromWire3
@@ -0,0 +1,76 @@
+#
+# A simple DNS response message
+# ID = 0x0513
+# QR=1 (response), Opcode=0, AA=1, RD=1 (other fields are 0)
+# QDCOUNT=1, ANCOUNT=1, AUTHORITY COUNT=5, ADDITIONAL COUNT=7
+# Question: example.com. IN SOA
+# ANSWER:
+# ;; QUESTION SECTION:
+# ;example.com. IN SOA
+
+# ;; ANSWER SECTION:
+# example.com. 21600 IN SOA a.dns.example.com. root.example.com. 2009070811 7200 3600 2419200 21600
+
+# ;; AUTHORITY SECTION:
+# example.com. 21600 IN NS b.dns.example.com.
+# example.com. 21600 IN NS c.dns.example.com.
+# example.com. 21600 IN NS a.dns.example.com.
+# example.com. 21600 IN NS e.dns.example.com.
+# example.com. 21600 IN NS d.dns.example.com.
+
+# ;; ADDITIONAL SECTION:
+# a.dns.example.com. 21600 IN A 1.1.1.1
+# a.dns.example.com. 21600 IN A 2.2.2.2
+# b.dns.example.com. 21600 IN A 3.3.3.3
+# c.dns.example.com. 10800 IN A 4.4.4.4
+# d.dns.example.com. 43200 IN A 5.5.5.5
+# e.dns.example.com. 21600 IN A 7.7.7.7
+# e.dns.example.com. 21600 IN A 6.6.6.6
+
+0513 8500
+0001 0001 0005 0007
+#(7) e x a m p l e (3) c o m .
+ 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
+0006 0001
+# same name, fully compressed
+c0 0c
+# SOA IN TTL=6h RDLENGTH=35 rdata
+0006 0001 00005460 0023 01 61 03 64 6e 73 c0 0c 04 72 6f 6f 74 c0 0c 77 bf fc db 00 00 1c 20 00 00 0e 10 00 24 ea 00 00 00 54 60
+#Authority section begin
+c0 0c
+# NS IN TTL=6h RDLENGTH=4 b.dns.example.com.
+0002 0001 00005460 0004 01 62 c0 2b
+# NS IN TTL=6h c.dns.example.com.
+c0 0c
+0002 0001 00005460 00 04 01 63 c0 2b
+# NS IN a.dns.example.com.
+c0 0c
+0002 0001 00005460 00 02 c0 29
+# NS IN e.dns.example.com.
+c0 0c
+0002 0001 00005460 0004 01 65 c0 2b
+# NS IN d.dns.example.com.
+c0 0c
+0002 0001 00005460 0004 01 64 c0 2b
+# additional section begin
+# a.dns.example.com. A
+c0 29
+0001 0001 00005460 0004 01 01 01 01
+# a.dns.example.com. A
+c0 29
+0001 0001 00005460 0004 02 02 02 02
+#b.dns.example.com. A
+c0 58
+0001 0001 00002A30 0004 03 03 03 03
+#c.dns.example.com. A
+c0 68
+0001 0001 00005460 0004 04 04 04 04
+# d.dns.example.com. A
+c0 96
+0001 0001 0000A8C0 0004 05 05 05 05
+# e.dns.example.com. A
+c0 86
+0001 0001 00005460 0004 07 07 07 07
+# e.dns.example.com. A
+c0 86
+0001 0001 00005460 0004 06 06 06 06
diff --git a/src/lib/cache/tests/testdata/message_fromWire4 b/src/lib/cache/tests/testdata/message_fromWire4
new file mode 100644
index 0000000..251abd5
--- /dev/null
+++ b/src/lib/cache/tests/testdata/message_fromWire4
@@ -0,0 +1,80 @@
+# Note: This message is same with message_fromWire3, except
+# AA bit is not set. There should be a better way to
+# avoid the duplicated file by clear the AA bit flags
+# after reading the message from message_fromWire4.
+#
+# A simple DNS response message
+# ID = 0x0513
+# QR=1 (response), Opcode=0, RD=1 (other fields are 0)
+# QDCOUNT=1, ANCOUNT=1, AUTHORITY COUNT=5, ADDITIONAL COUNT=7
+# Question: example.com. IN SOA
+# ANSWER:
+# ;; QUESTION SECTION:
+# ;example.com. IN SOA
+
+# ;; ANSWER SECTION:
+# example.com. 21600 IN SOA a.dns.example.com. root.example.com. 2009070811 7200 3600 2419200 21600
+
+# ;; AUTHORITY SECTION:
+# example.com. 21600 IN NS b.dns.example.com.
+# example.com. 21600 IN NS c.dns.example.com.
+# example.com. 21600 IN NS a.dns.example.com.
+# example.com. 21600 IN NS e.dns.example.com.
+# example.com. 21600 IN NS d.dns.example.com.
+
+# ;; ADDITIONAL SECTION:
+# a.dns.example.com. 21600 IN A 1.1.1.1
+# a.dns.example.com. 21600 IN A 2.2.2.2
+# b.dns.example.com. 21600 IN A 3.3.3.3
+# c.dns.example.com. 10800 IN A 4.4.4.4
+# d.dns.example.com. 43200 IN A 5.5.5.5
+# e.dns.example.com. 21600 IN A 7.7.7.7
+# e.dns.example.com. 21600 IN A 6.6.6.6
+
+0513 8100
+0001 0001 0005 0007
+#(7) e x a m p l e (3) c o m .
+ 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
+0006 0001
+# same name, fully compressed
+c0 0c
+# SOA IN TTL=6h RDLENGTH=35 rdata
+0006 0001 00005460 0023 01 61 03 64 6e 73 c0 0c 04 72 6f 6f 74 c0 0c 77 bf fc db 00 00 1c 20 00 00 0e 10 00 24 ea 00 00 00 54 60
+#Authority section begin
+c0 0c
+# NS IN TTL=6h RDLENGTH=4 b.dns.example.com.
+0002 0001 00005460 0004 01 62 c0 2b
+# NS IN TTL=6h c.dns.example.com.
+c0 0c
+0002 0001 00005460 00 04 01 63 c0 2b
+# NS IN a.dns.example.com.
+c0 0c
+0002 0001 00005460 00 02 c0 29
+# NS IN e.dns.example.com.
+c0 0c
+0002 0001 00005460 0004 01 65 c0 2b
+# NS IN d.dns.example.com.
+c0 0c
+0002 0001 00005460 0004 01 64 c0 2b
+# additional section begin
+# a.dns.example.com. A
+c0 29
+0001 0001 00005460 0004 01 01 01 01
+# a.dns.example.com. A
+c0 29
+0001 0001 00005460 0004 02 02 02 02
+#b.dns.example.com. A
+c0 58
+0001 0001 00002A30 0004 03 03 03 03
+#c.dns.example.com. A
+c0 68
+0001 0001 00005460 0004 04 04 04 04
+# d.dns.example.com. A
+c0 96
+0001 0001 0000A8C0 0004 05 05 05 05
+# e.dns.example.com. A
+c0 86
+0001 0001 00005460 0004 07 07 07 07
+# e.dns.example.com. A
+c0 86
+0001 0001 00005460 0004 06 06 06 06
diff --git a/src/lib/cache/tests/testdata/message_fromWire5 b/src/lib/cache/tests/testdata/message_fromWire5
new file mode 100644
index 0000000..965f250
--- /dev/null
+++ b/src/lib/cache/tests/testdata/message_fromWire5
@@ -0,0 +1,36 @@
+#
+# A simple DNS response message
+# ID = 0x07b2
+# QR=1 (response), Opcode=0, AA=1, RD=1 (other fields are 0)
+# QDCOUNT=1, ANCOUNT=2, other COUNTS=0
+# Question: a.example.net. IN A
+# Answer:
+# ANSWER SECTION:
+# a.example.com. 21600 IN CNAME cname.example.com.
+# cname.example.com. 21600 IN A 1.1.1.1
+#
+# AUTHORITY SECTION:
+# example.com. 21600 IN NS a.dns.example.com.
+#
+# ADDITIONAL SECTION:
+# a.dns.example.com. 21600 IN A 1.1.1.1
+#
+07b2 8500
+0001 0002 0001 0001
+#(1) a (7) e x a m p l e (3) c o m .
+ 01 61 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
+# A IN
+0001 0001
+#
+c0 0c
+#CNAME IN TTL RDATA_LEN
+0005 0001 00005460 0008 05 63 6e 61 6d 65 c0 0e
+#
+c0 2b
+0001 0001 00005460 0004 01 01 01 01
+#
+c0 0e
+0002 0001 00005460 0008 01 61 03 64 6e 73 c0 0e
+#
+c0 4f
+0001 0001 00005460 0004 01 01 01 01
diff --git a/src/lib/cache/tests/testdata/message_fromWire6 b/src/lib/cache/tests/testdata/message_fromWire6
new file mode 100644
index 0000000..23684ba
--- /dev/null
+++ b/src/lib/cache/tests/testdata/message_fromWire6
@@ -0,0 +1,40 @@
+#
+# A simple DNS response message
+# ID = 0x005e
+# QR=1 (response), Opcode=0, AA=1, RD=1 (other fields are 0)
+# QDCOUNT=1, ANCOUNT=2, other COUNTS=0
+# Question: a.d.example.net. IN A
+# Answer:
+# ;; ANSWER SECTION:
+# d.example.com. 21600 IN DNAME dname.example.com.
+# a.d.example.com. 21600 IN CNAME a.dname.example.com.
+# a.dname.example.com. 21600 IN A 1.1.1.1
+#
+# ;; AUTHORITY SECTION:
+# example.com. 21600 IN NS a.dns.example.com.
+#
+# ;; ADDITIONAL SECTION:
+# a.dns.example.com. 21600 IN A 1.1.1.1
+#
+#
+005e 8500
+0001 0003 0001 0001
+#(1)a (1) b (7) e x a m p l e (3) c o m .
+ 01 61 01 64 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
+# A IN
+0001 0001
+#
+c0 0e
+0027 0001 00005460 0013 05 64 6e 61 6d 65 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
+#
+c0 0c
+0005 0001 00005460 0004 01 61 c0 2d
+#
+c0 4c
+0001 0001 00005460 0004 01 01 01 01
+#
+c0 33
+0002 0001 00005460 0008 01 61 03 64 6e 73 c0 33
+#
+c0 6c
+0001 0001 00005460 0004 01 01 01 01
diff --git a/src/lib/datasrc/memory_datasrc.cc b/src/lib/datasrc/memory_datasrc.cc
index f0b8b53..fdef888 100644
--- a/src/lib/datasrc/memory_datasrc.cc
+++ b/src/lib/datasrc/memory_datasrc.cc
@@ -35,13 +35,14 @@ namespace datasrc {
struct MemoryZone::MemoryZoneImpl {
// Constructor
MemoryZoneImpl(const RRClass& zone_class, const Name& origin) :
- zone_class_(zone_class), origin_(origin)
- {}
-
- // Information about the zone
- RRClass zone_class_;
- Name origin_;
- string file_name_;
+ zone_class_(zone_class), origin_(origin), origin_data_(NULL),
+ domains_(true)
+ {
+ // We create the node for origin (it needs to exist anyway in future)
+ domains_.insert(origin, &origin_data_);
+ DomainPtr origin_domain(new Domain);
+ origin_data_->setData(origin_domain);
+ }
// Some type aliases
/*
@@ -61,48 +62,169 @@ struct MemoryZone::MemoryZoneImpl {
// The tree stores domains
typedef RBTree<Domain> DomainTree;
typedef RBNode<Domain> DomainNode;
+ static const DomainNode::Flags DOMAINFLAG_WILD = DomainNode::FLAG_USER1;
+
+ // Information about the zone
+ RRClass zone_class_;
+ Name origin_;
+ DomainNode* origin_data_;
+ string file_name_;
+
// The actual zone data
DomainTree domains_;
+ // Add the necessary magic for any wildcard contained in 'name'
+ // (including itself) to be found in the zone.
+ //
+ // In order for wildcard matching to work correctly in find(),
+ // we must ensure that a node for the wildcarding level exists in the
+ // backend RBTree.
+ // E.g. if the wildcard name is "*.sub.example." then we must ensure
+ // that "sub.example." exists and is marked as a wildcard level.
+ // Note: the "wildcarding level" is for the parent name of the wildcard
+ // name (such as "sub.example.").
+ //
+ // We also perform the same trick for empty wild card names possibly
+ // contained in 'name' (e.g., '*.foo.example' in 'bar.*.foo.example').
+ void addWildcards(DomainTree& domains, const Name& name) {
+ Name wname(name);
+ const unsigned int labels(wname.getLabelCount());
+ const unsigned int origin_labels(origin_.getLabelCount());
+ for (unsigned int l = labels;
+ l > origin_labels;
+ --l, wname = wname.split(1)) {
+ if (wname.isWildcard()) {
+ // Ensure a separate level exists for the wildcard name.
+ // Note: for 'name' itself we do this later anyway, but the
+ // overhead should be marginal because wildcard names should
+ // be rare.
+ DomainNode* node;
+ DomainTree::Result result(domains.insert(wname.split(1),
+ &node));
+ assert(result == DomainTree::SUCCESS ||
+ result == DomainTree::ALREADYEXISTS);
+
+ // Ensure a separate level exists for the "wildcarding" name,
+ // and mark the node as "wild".
+ result = domains.insert(wname, &node);
+ assert(result == DomainTree::SUCCESS ||
+ result == DomainTree::ALREADYEXISTS);
+ node->setFlag(DOMAINFLAG_WILD);
+ }
+ }
+ }
+
/*
- * Implementation of longer methods. We put them here, because the
- * access is without the impl_-> and it will get inlined anyway.
+ * Does some checks in context of the data that are already in the zone.
+ * Currently checks for forbidden combinations of RRsets in the same
+ * domain (CNAME+anything, DNAME+NS).
+ *
+ * If such condition is found, it throws AddError.
*/
- // Implementation of MemoryZone::add
- result::Result add(const ConstRRsetPtr& rrset, DomainTree* domains) {
- // Sanitize input
+ void contextCheck(const ConstRRsetPtr& rrset,
+ const DomainPtr& domain) const {
+ // Ensure CNAME and other type of RR don't coexist for the same
+ // owner name.
+ if (rrset->getType() == RRType::CNAME()) {
+ // XXX: this check will become incorrect when we support DNSSEC
+ // (depending on how we support DNSSEC). We should revisit it
+ // at that point.
+ if (!domain->empty()) {
+ isc_throw(AddError, "CNAME can't be added with other data for "
+ << rrset->getName());
+ }
+ } else if (domain->find(RRType::CNAME()) != domain->end()) {
+ isc_throw(AddError, "CNAME and " << rrset->getType() <<
+ " can't coexist for " << rrset->getName());
+ }
+
+ /*
+ * Similar with DNAME, but it must not coexist only with NS and only in
+ * non-apex domains.
+ * RFC 2672 section 3 mentions that it is implied from it and RFC 2181
+ */
+ if (rrset->getName() != origin_ &&
+ // Adding DNAME, NS already there
+ ((rrset->getType() == RRType::DNAME() &&
+ domain->find(RRType::NS()) != domain->end()) ||
+ // Adding NS, DNAME already there
+ (rrset->getType() == RRType::NS() &&
+ domain->find(RRType::DNAME()) != domain->end())))
+ {
+ isc_throw(AddError, "DNAME can't coexist with NS in non-apex "
+ "domain " << rrset->getName());
+ }
+ }
+
+ // Validate rrset before adding it to the zone. If something is wrong
+ // it throws an exception. It doesn't modify the zone, and provides
+ // the strong exception guarantee.
+ void addValidation(const ConstRRsetPtr rrset) {
if (!rrset) {
isc_throw(NullRRset, "The rrset provided is NULL");
}
- if (rrset->getType() == RRType::CNAME() &&
- rrset->getRdataCount() > 1) {
- // XXX: this is not only for CNAME. We should generalize this
- // code for all other "singleton RR types" (such as SOA) in a
+ // Check for singleton RRs. It should probably handled at a different
+ // in future.
+ if ((rrset->getType() == RRType::CNAME() ||
+ rrset->getType() == RRType::DNAME()) &&
+ rrset->getRdataCount() > 1)
+ {
+ // XXX: this is not only for CNAME or DNAME. We should generalize
+ // this code for all other "singleton RR types" (such as SOA) in a
// separate task.
isc_throw(AddError, "multiple RRs of singleton type for "
<< rrset->getName());
}
- Name name(rrset->getName());
- NameComparisonResult compare(origin_.compare(name));
+ NameComparisonResult compare(origin_.compare(rrset->getName()));
if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
compare.getRelation() != NameComparisonResult::EQUAL)
{
- isc_throw(OutOfZone, "The name " << name <<
+ isc_throw(OutOfZone, "The name " << rrset->getName() <<
" is not contained in zone " << origin_);
}
+
+ // Some RR types do not really work well with a wildcard.
+ // Even though the protocol specifically doesn't completely ban such
+ // usage, we refuse to load a zone containing such RR in order to
+ // keep the lookup logic simpler and more predictable.
+ // See RFC4592 and (for DNAME) draft-ietf-dnsext-rfc2672bis-dname
+ // for more technical background. Note also that BIND 9 refuses
+ // NS at a wildcard, so in that sense we simply provide compatible
+ // behavior.
+ if (rrset->getName().isWildcard()) {
+ if (rrset->getType() == RRType::NS()) {
+ isc_throw(AddError, "Invalid NS owner name (wildcard): " <<
+ rrset->getName());
+ }
+ if (rrset->getType() == RRType::DNAME()) {
+ isc_throw(AddError, "Invalid DNAME owner name (wildcard): " <<
+ rrset->getName());
+ }
+ }
+ }
+
+ /*
+ * Implementation of longer methods. We put them here, because the
+ * access is without the impl_-> and it will get inlined anyway.
+ */
+ // Implementation of MemoryZone::add
+ result::Result add(const ConstRRsetPtr& rrset, DomainTree* domains) {
+ // Sanitize input
+ addValidation(rrset);
+
+ // Add wildcards possibly contained in the owner name to the domain
+ // tree.
+ // Note: this can throw an exception, breaking strong exception
+ // guarantee. (see also the note for contextCheck() below).
+ addWildcards(*domains, rrset->getName());
+
// Get the node
DomainNode* node;
- switch (domains->insert(name, &node)) {
- // Just check it returns reasonable results
- case DomainTree::SUCCESS:
- case DomainTree::ALREADYEXISTS:
- break;
- // Something odd got out
- default:
- assert(0);
- }
- assert(node != NULL);
+ DomainTree::Result result = domains->insert(rrset->getName(), &node);
+ // Just check it returns reasonable results
+ assert((result == DomainTree::SUCCESS ||
+ result == DomainTree::ALREADYEXISTS) && node!= NULL);
// Now get the domain
DomainPtr domain;
@@ -114,24 +236,12 @@ struct MemoryZone::MemoryZoneImpl {
domain = node->getData();
}
- // Ensure CNAME and other type of RR don't coexist for the same
- // owner name.
+ // Checks related to the surrounding data.
// Note: when the check fails and the exception is thrown, it may
// break strong exception guarantee. At the moment we prefer
// code simplicity and don't bother to introduce complicated
// recovery code.
- if (rrset->getType() == RRType::CNAME()) {
- // XXX: this check will become incorrect when we support DNSSEC
- // (depending on how we support DNSSEC). We should revisit it
- // at that point.
- if (!domain->empty()) {
- isc_throw(AddError, "CNAME can't be added with other data for "
- << rrset->getName());
- }
- } else if (domain->find(RRType::CNAME()) != domain->end()) {
- isc_throw(AddError, "CNAME and " << rrset->getType() <<
- " can't coexist for " << rrset->getName());
- }
+ contextCheck(rrset, domain);
// Try inserting the rrset there
if (domain->insert(DomainPair(rrset->getType(), rrset)).second) {
@@ -139,10 +249,12 @@ struct MemoryZone::MemoryZoneImpl {
// If this RRset creates a zone cut at this node, mark the node
// indicating the need for callback in find().
- // TBD: handle DNAME, too
if (rrset->getType() == RRType::NS() &&
rrset->getName() != origin_) {
- node->enableCallback();
+ node->setFlag(DomainNode::FLAG_CALLBACK);
+ // If it is DNAME, we have a callback as well here
+ } else if (rrset->getType() == RRType::DNAME()) {
+ node->setFlag(DomainNode::FLAG_CALLBACK);
}
return (result::SUCCESS);
@@ -174,31 +286,56 @@ struct MemoryZone::MemoryZoneImpl {
/// It will be passed to \c zonecutCallback() and record a possible
/// zone cut node and related RRset (normally NS or DNAME).
struct FindState {
- FindState(FindOptions options) : zonecut_node_(NULL),
- options_(options)
+ FindState(FindOptions options) :
+ zonecut_node_(NULL),
+ dname_node_(NULL),
+ options_(options)
{}
const DomainNode* zonecut_node_;
+ const DomainNode* dname_node_;
ConstRRsetPtr rrset_;
const FindOptions options_;
};
- // A callback called from possible zone cut nodes. This will be passed
- // from the \c find() method to \c RBTree::find().
- static bool zonecutCallback(const DomainNode& node, FindState* state) {
- // We perform callback check only for the highest zone cut in the
- // rare case of nested zone cuts.
- if (state->zonecut_node_ != NULL) {
- return (false);
+ // A callback called from possible zone cut nodes and nodes with DNAME.
+ // This will be passed from the \c find() method to \c RBTree::find().
+ static bool cutCallback(const DomainNode& node, FindState* state) {
+ // We need to look for DNAME first, there's allowed case where
+ // DNAME and NS coexist in the apex. DNAME is the one to notice,
+ // the NS is authoritative, not delegation (corner case explicitly
+ // allowed by section 3 of 2672)
+ const Domain::const_iterator foundDNAME(node.getData()->find(
+ RRType::DNAME()));
+ if (foundDNAME != node.getData()->end()) {
+ state->dname_node_ = &node;
+ state->rrset_ = foundDNAME->second;
+ // No more processing below the DNAME (RFC 2672, section 3
+ // forbids anything to exist below it, so there's no need
+ // to actually search for it). This is strictly speaking
+ // a different way than described in 4.1 of that RFC,
+ // but because of the assumption in section 3, it has the
+ // same behaviour.
+ return (true);
}
- const Domain::const_iterator found(node.getData()->find(RRType::NS()));
- if (found != node.getData()->end()) {
- // BIND 9 checks if this node is not the origin. But it cannot
- // be the origin because we don't enable the callback at the
- // origin node (see MemoryZoneImpl::add()). Or should we do a
- // double check for it?
+ // Look for NS
+ const Domain::const_iterator foundNS(node.getData()->find(
+ RRType::NS()));
+ if (foundNS != node.getData()->end()) {
+ // We perform callback check only for the highest zone cut in the
+ // rare case of nested zone cuts.
+ if (state->zonecut_node_ != NULL) {
+ return (false);
+ }
+
+ // BIND 9 checks if this node is not the origin. That's probably
+ // because it can support multiple versions for dynamic updates
+ // and IXFR, and it's possible that the callback is called at
+ // the apex and the DNAME doesn't exist for a particular version.
+ // It cannot happen for us (at least for now), so we don't do
+ // that check.
state->zonecut_node_ = &node;
- state->rrset_ = found->second;
+ state->rrset_ = foundNS->second;
// Unless glue is allowed the search stops here, so we return
// false; otherwise return true to continue the search.
@@ -222,15 +359,45 @@ struct MemoryZone::MemoryZoneImpl {
// Get the node
DomainNode* node(NULL);
FindState state(options);
- switch (domains_.find(name, &node, zonecutCallback, &state)) {
+ RBTreeNodeChain<Domain> node_path;
+ switch (domains_.find(name, &node, node_path, cutCallback, &state)) {
case DomainTree::PARTIALMATCH:
+ /*
+ * In fact, we could use a single variable instead of
+ * dname_node_ and zonecut_node_. But then we would need
+ * to distinquish these two cases by something else and
+ * it seemed little more confusing to me when I wrote it.
+ *
+ * Usually at most one of them will be something else than
+ * NULL (it might happen both are NULL, in which case we
+ * consider it NOT FOUND). There's one corner case when
+ * both might be something else than NULL and it is in case
+ * there's a DNAME under a zone cut and we search in
+ * glue OK mode â in that case we don't stop on the domain
+ * with NS and ignore it for the answer, but it gets set
+ * anyway. Then we find the DNAME and we need to act by it,
+ * therefore we first check for DNAME and then for NS. In
+ * all other cases it doesn't matter, as at least one of them
+ * is NULL.
+ */
+ if (state.dname_node_ != NULL) {
+ // We were traversing a DNAME node (and wanted to go
+ // lower below it), so return the DNAME
+ return (FindResult(DNAME, state.rrset_));
+ }
if (state.zonecut_node_ != NULL) {
return (FindResult(DELEGATION, state.rrset_));
}
- // TODO: we should also cover empty non-terminal cases, which
- // will require non trivial code and is deferred for later
- // development. For now, we regard any partial match that
- // didn't hit a zone cut as "not found".
+
+ // If the RBTree search stopped at a node for a super domain
+ // of the search name, it means the search name exists in
+ // the zone but is empty. Treat it as NXRRSET.
+ if (node_path.getLastComparisonResult().getRelation() ==
+ NameComparisonResult::SUPERDOMAIN) {
+ return (FindResult(NXRRSET, ConstRRsetPtr()));
+ }
+
+ // fall through
case DomainTree::NOTFOUND:
return (FindResult(NXDOMAIN, ConstRRsetPtr()));
case DomainTree::EXACTMATCH: // This one is OK, handle it
@@ -238,14 +405,19 @@ struct MemoryZone::MemoryZoneImpl {
default:
assert(0);
}
- assert(node);
- assert(!node->isEmpty());
+ assert(node != NULL);
+
+ // If there is an exact match but the node is empty, it's equivalent
+ // to NXRRSET.
+ if (node->isEmpty()) {
+ return (FindResult(NXRRSET, ConstRRsetPtr()));
+ }
Domain::const_iterator found;
// If the node callback is enabled, this may be a zone cut. If it
- // has a NS RR, we should return a delegation.
- if (node->isCallbackEnabled()) {
+ // has a NS RR, we should return a delegation, but not in the apex.
+ if (node->getFlag(DomainNode::FLAG_CALLBACK) && node != origin_data_) {
found = node->getData()->find(RRType::NS());
if (found != node->getData()->end()) {
return (FindResult(DELEGATION, found->second));
@@ -253,7 +425,7 @@ struct MemoryZone::MemoryZoneImpl {
}
// handle type any query
- if (target && !node->getData()->empty()) {
+ if (target != NULL && !node->getData()->empty()) {
// Empty domain will be handled as NXRRSET by normal processing
for (found = node->getData()->begin();
found != node->getData()->end(); found++)
diff --git a/src/lib/datasrc/rbtree.h b/src/lib/datasrc/rbtree.h
index 643b185..bd04066 100644
--- a/src/lib/datasrc/rbtree.h
+++ b/src/lib/datasrc/rbtree.h
@@ -23,12 +23,15 @@
/// issue, the design and interface are not fixed, and RBTree isn't ready
/// to be used as a base data structure by other modules.
+#include <exceptions/exceptions.h>
+
#include <dns/name.h>
#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
-#include <exception>
+#include <exceptions/exceptions.h>
#include <ostream>
#include <algorithm>
+#include <cassert>
namespace isc {
namespace datasrc {
@@ -54,7 +57,9 @@ operator-(const isc::dns::Name& super_name, const isc::dns::Name& sub_name) {
}
}
-template <typename T, bool returnEmptyNode>
+/// Forward declare RBTree class here is convinent for following friend
+/// class declare inside RBNode and RBTreeNodeChain
+template <typename T>
class RBTree;
/// \brief \c RBNode is used by RBTree to store any data related to one domain
@@ -82,8 +87,7 @@ class RBNode : public boost::noncopyable {
private:
/// The RBNode is meant for use from within RBTree, so it has access to
/// it.
- template <typename U, bool returnEmptyNode>
- friend class RBTree;
+ friend class RBTree<T>;
/// \name Constructors
///
@@ -108,6 +112,29 @@ public:
/// \brief Alias for shared pointer to the data.
typedef boost::shared_ptr<T> NodeDataPtr;
+ /// Node flags.
+ ///
+ /// Each flag value defines a non default property for a specific node.
+ /// These are defined as bitmask type values for the convenience of
+ /// internal implementation, but applications are expected to use
+ /// each flag separately via the enum definitions.
+ ///
+ /// All (settable) flags are off by default; they must be explicitly
+ /// set to on by the \c setFlag() method.
+ enum Flags {
+ FLAG_CALLBACK = 1, ///< Callback enabled. See \ref callback
+ FLAG_USER1 = 0x80000000U ///< Application specific flag
+ };
+private:
+ // Some flag values are expected to be used for internal purposes
+ // (e.g., representing the node color) in future versions, so we
+ // limit the settable flags via the \c setFlag() method to those
+ // explicitly defined in \c Flags. This constant represents all
+ // such flags.
+ static const uint32_t SETTABLE_FLAGS = (FLAG_CALLBACK | FLAG_USER1);
+
+public:
+
/// \brief Destructor
///
/// It might seem strange that constructors are private and destructor
@@ -142,6 +169,7 @@ public:
/// non-terminal domains, but it is possible (yet probably meaningless)
/// empty nodes anywhere.
bool isEmpty() const { return (data_.get() == NULL); }
+
//@}
/// \name Setter functions.
@@ -150,6 +178,52 @@ public:
void setData(const NodeDataPtr& data) { data_ = data; }
//@}
+ /// \name Node flag manipulation methods
+ //@{
+ /// Get the status of a node flag.
+ ///
+ /// This method returns whether the given node flag is set (enabled)
+ /// on the node. The \c flag parameter is expected to be one of the
+ /// defined \c Flags constants. For simplicity, the method interface
+ /// does not prohibit passing an undefined flag or combined flags, but
+ /// the return value in such a case will be meaningless for the caller
+ /// (an application would have to use an ugly cast for such an unintended
+ /// form of call, which will hopefully avoid accidental misuse).
+ ///
+ /// \exception None
+ /// \param flag The flag to be tested.
+ /// \return \c true if the \c flag is set; \c false otherwise.
+ bool getFlag(Flags flag) const {
+ return ((flags_ & flag) != 0);
+ }
+
+ /// Set or clear a node flag.
+ ///
+ /// This method changes the status of the specified node flag to either
+ /// "on" (enabled) or "off" (disabled). The new status is specified by
+ /// the \c on parameter.
+ /// Like the \c getFlag() method, \c flag is expected to be one of the
+ /// defined \c Flags constants. If an undefined or unsettable flag is
+ /// specified, \c isc::InvalidParameter exception will be thrown.
+ ///
+ /// \exception isc::InvalidParameter Unsettable flag is specified
+ /// \exception None otherwise
+ /// \param flag The node flag to be changed.
+ /// \on If \c true, set the flag to on; otherwise set it to off.
+ void setFlag(Flags flag, bool on = true) {
+ if ((flag & ~SETTABLE_FLAGS) != 0) {
+ isc_throw(isc::InvalidParameter,
+ "Unsettable RBTree flag is being set");
+ }
+ if (on) {
+ flags_ |= flag;
+ } else {
+ flags_ &= ~flag;
+ }
+ }
+ //@}
+
+private:
/// \name Callback related methods
///
/// See the description of \c RBTree<T>::find() about callbacks.
@@ -157,16 +231,8 @@ public:
/// These methods never throw an exception.
//@{
/// Return if callback is enabled at the node.
- bool isCallbackEnabled() const { return (callback_required_); }
-
- /// Enable callback at the node.
- void enableCallback() { callback_required_ = true; }
-
- /// Disable callback at the node.
- void disableCallback() { callback_required_ = false; }
//@}
-
private:
/// \brief Define rbnode color
enum RBNodeColor {BLACK, RED};
@@ -176,6 +242,23 @@ private:
return (&null_node);
}
+ /// \brief return the next node which is bigger than current node
+ /// in the same subtree
+ ///
+ /// The next successor for this node is the next bigger node in terms of
+ /// the DNSSEC order relation within the same single subtree.
+ /// Note that it may NOT be the next bigger node in the entire RBTree;
+ /// RBTree is a tree in tree, and the real next node may reside in
+ /// an upper or lower subtree of the subtree where this node belongs.
+ /// For example, if this node has a sub domain, the real next node is
+ /// the smallest node in the sub domain tree.
+ ///
+ /// If this node is the biggest node within the subtree, this method
+ /// returns \c NULL_NODE().
+ ///
+ /// This method never throws an exception.
+ const RBNode<T>* successor() const;
+
/// \name Data to maintain the rbtree structure.
//@{
RBNode<T>* parent_;
@@ -204,7 +287,7 @@ private:
/// RBTree::find().
///
/// \todo It might be needed to put it into more general attributes field.
- bool callback_required_;
+ uint32_t flags_;
};
@@ -218,7 +301,7 @@ RBNode<T>::RBNode() :
// dummy name, the value doesn't matter:
name_(isc::dns::Name::ROOT_NAME()),
down_(this),
- callback_required_(false)
+ flags_(0)
{
}
@@ -230,7 +313,7 @@ RBNode<T>::RBNode(const isc::dns::Name& name) :
color_(RED),
name_(name),
down_(NULL_NODE()),
- callback_required_(false)
+ flags_(0)
{
}
@@ -239,6 +322,230 @@ template <typename T>
RBNode<T>::~RBNode() {
}
+template <typename T>
+const RBNode<T>*
+RBNode<T>::successor() const {
+ const RBNode<T>* current = this;
+ // If it has right node, the successor is the left-most node of the right
+ // subtree.
+ if (right_ != NULL_NODE()) {
+ current = right_;
+ while (current->left_ != NULL_NODE()) {
+ current = current->left_;
+ }
+ return (current);
+ }
+
+
+ // Otherwise go up until we find the first left branch on our path to
+ // root. If found, the parent of the branch is the successor.
+ // Otherwise, we return the null node
+ const RBNode<T>* parent = current->parent_;
+ while (parent != NULL_NODE() && current == parent->right_) {
+ current = parent;
+ parent = parent->parent_;
+ }
+ return (parent);
+}
+
+
+/// \brief RBTreeNodeChain stores detailed information of \c RBTree::find()
+/// result.
+///
+/// - The \c RBNode that was last compared with the search name, and
+/// the comparison result at that point in the form of
+/// \c isc::dns::NameComparisonResult.
+/// - A sequence of nodes that forms a path to the found node (which is
+/// not yet implemented).
+///
+/// The comparison result can be used to handle some rare cases such as
+/// empty node processing.
+/// The node sequence keeps track of the nodes to reach any given node from
+/// the root of RBTree.
+///
+/// Currently, RBNode does not have "up" pointers in them (i.e., back pointers
+/// from the root of one level of tree of trees to the node in the parent
+/// tree whose down pointer points to that root node) for memory usage
+/// reasons, so there is no other way to find the path back to the root from
+/// any given RBNode.
+///
+/// \note This design may change in future versions. In particular, it's
+/// quite likely we want to have that pointer if we want to optimize name
+/// compression by exploiting the structure of the zone. If and when that
+/// happens we should also revisit the need for the chaining.
+/// Also, the class name may not be appropriate now that it contains other
+/// information than a node "chain", and the chain itself may even be
+/// deprecated. Something like "RBTreeFindContext" may be a better name.
+/// This point should be revisited later.
+///
+/// RBTreeNodeChain is constructed and manipulated only inside the \c RBTree
+/// class.
+/// \c RBTree uses it as an inner data structure to iterate over the whole
+/// RBTree.
+/// This is the reason why manipulation methods such as \c push() and \c pop()
+/// are private (and not shown in the doxygen document).
+template <typename T>
+class RBTreeNodeChain {
+ /// RBTreeNodeChain is initialized by RBTree, only RBTree has
+ /// knowledge to manipuate it.
+ friend class RBTree<T>;
+public:
+ /// \name Constructors and Assignment Operator.
+ ///
+ /// \note The copy constructor and the assignment operator are
+ /// intentionally defined as private, making this class non copyable.
+ /// This may have to be changed in a future version with newer need.
+ /// For now we explicitly disable copy to avoid accidental copy happens
+ /// unintentionally.
+ //{@
+ /// The default constructor.
+ ///
+ /// \exception None
+ RBTreeNodeChain() : node_count_(0), last_compared_(NULL),
+ // XXX: meaningless initial values:
+ last_comparison_(0, 0,
+ isc::dns::NameComparisonResult::EQUAL)
+ {}
+
+private:
+ RBTreeNodeChain(const RBTreeNodeChain<T>&);
+ RBTreeNodeChain<T>& operator=(const RBTreeNodeChain<T>&);
+ //@}
+
+public:
+ /// Clear the state of the chain.
+ ///
+ /// This method re-initializes the internal state of the chain so that
+ /// it can be reused for subsequent operations.
+ ///
+ /// \exception None
+ void clear() {
+ node_count_ = 0;
+ last_compared_ = NULL;
+ }
+
+ /// Return the \c RBNode that was last compared in \c RBTree::find().
+ ///
+ /// If this chain has been passed to \c RBTree::find() and there has
+ /// been name comparison against the search name, the last compared
+ /// \c RBNode is recorded within the chain. This method returns that
+ /// node.
+ /// If \c RBTree::find() hasn't been called with this chain or name
+ /// comparison hasn't taken place (which is possible if the tree is empty),
+ /// this method returns \c NULL.
+ ///
+ /// \exception None
+ const RBNode<T>* getLastComparedNode() const {
+ return (last_compared_);
+ }
+
+ /// Return the result of last name comparison in \c RBTree::find().
+ ///
+ /// Like \c getLastComparedNode(), \c RBTree::find() records the result
+ /// of the last name comparison in the chain. This method returns the
+ /// result.
+ /// The return value of this method is only meaningful when comparison
+ /// has taken place, i.e, when \c getLastComparedNode() would return a
+ /// non \c NULL value.
+ ///
+ /// \exception None
+ const isc::dns::NameComparisonResult& getLastComparisonResult() const {
+ return (last_comparison_);
+ }
+
+ /// \brief Return the number of levels stored in the chain.
+ ///
+ /// It's equal to the number of nodes in the chain; for an empty
+ /// chain, 0 will be returned.
+ ///
+ /// \exception None
+ unsigned int getLevelCount() const { return (node_count_); }
+
+ /// \brief return the absolute name for the node which this
+ /// \c RBTreeNodeChain currently refers to.
+ ///
+ /// The chain must not be empty.
+ ///
+ /// \exception isc::BadValue the chain is empty.
+ /// \exception std::bad_alloc memory allocation for the new name fails.
+ isc::dns::Name getAbsoluteName() const {
+ if (isEmpty()) {
+ isc_throw(isc::BadValue,
+ "RBTreeNodeChain::getAbsoluteName is called on an empty "
+ "chain");
+ }
+
+ const RBNode<T>* top_node = top();
+ isc::dns::Name absolute_name = top_node->getName();
+ int node_count = node_count_ - 1;
+ while (node_count > 0) {
+ top_node = nodes_[node_count - 1];
+ absolute_name = absolute_name.concatenate(top_node->getName());
+ --node_count;
+ }
+ return (absolute_name);
+ }
+
+private:
+ // the following private functions check invariants about the internal
+ // state using assert() instead of exception. The state of a chain
+ // can only be modified operations within this file, so if any of the
+ // assumptions fails it means an internal bug.
+
+ /// \brief return whther node chain has node in it.
+ ///
+ /// \exception None
+ bool isEmpty() const { return (node_count_ == 0); }
+
+ /// \brief return the top node for the node chain
+ ///
+ /// RBTreeNodeChain store all the nodes along top node to
+ /// root node of RBTree
+ ///
+ /// \exception None
+ const RBNode<T>* top() const {
+ assert(!isEmpty());
+ return (nodes_[node_count_ - 1]);
+ }
+
+ /// \brief pop the top node from the node chain
+ ///
+ /// After pop, up/super node of original top node will be
+ /// the top node
+ ///
+ /// \exception None
+ void pop() {
+ assert(!isEmpty());
+ --node_count_;
+ }
+
+ /// \brief add the node into the node chain
+ ///
+ /// If the node chain isn't empty, the node should be
+ /// the sub domain of the original top node in node chain
+ /// otherwise the node should be the root node of RBTree.
+ ///
+ /// \exception None
+ void push(const RBNode<T>* node) {
+ assert(node_count_ < RBT_MAX_LEVEL);
+ nodes_[node_count_++] = node;
+ }
+
+private:
+ // The max label count for one domain name is Name::MAX_LABELS (128).
+ // Since each node in rbtree stores at least one label, and the root
+ // name always shares the same level with some others (which means
+ // all top level nodes except the one for the root name contain at least
+ // two labels), the possible maximum level is MAX_LABELS - 1.
+ // It's also the possible maximum nodes stored in a chain.
+ const static int RBT_MAX_LEVEL = isc::dns::Name::MAX_LABELS - 1;
+
+ int node_count_;
+ const RBNode<T>* nodes_[RBT_MAX_LEVEL];
+ const RBNode<T>* last_compared_;
+ isc::dns::NameComparisonResult last_comparison_;
+};
+
// note: the following class description is documented using multiline comments
// because the verbatim diagram contain a backslash, which could be interpreted
@@ -258,11 +565,16 @@ RBNode<T>::~RBNode() {
* - Decreases the memory footprint, as it doesn't store the suffix labels
* multiple times.
*
- * Depending on different usage, rbtree will support different search policy.
- * Whether return empty node to end user is one policy among them. Search
- * policy is as the last template parameter, the default policy will NOT
- * return empty node to end user, pass ture will get empty node during find
- * is needed
+ * Depending on different usage, rbtree will support different search policies.
+ * Whether to return an empty node to end user is one policy among them.
+ * The default policy is to NOT return an empty node to end user;
+ * to change the behavior, specify \c true for the constructor parameter
+ * \c returnEmptyNode.
+ * \note The search policy only affects the \c find() behavior of RBTree.
+ * When inserting one name into RBTree, if the node with the name already
+ * exists in the RBTree and it's an empty node which doesn't have any data,
+ * the \c insert() method will still return \c ALREADYEXISTS regardless of
+ * the search policy.
*
* \anchor diagram
*
@@ -277,7 +589,7 @@ RBNode<T>::~RBNode() {
* - p.w.y.d.e.f
* - q.w.y.d.e.f
*
- * the tree will looks like:
+ * the tree will look like:
* \verbatim
b
/ \
@@ -297,10 +609,8 @@ RBNode<T>::~RBNode() {
* - add remove interface
* - add iterator to iterate over the whole \c RBTree. This may be necessary,
* for example, to support AXFR.
- * - since \c RBNode only has down pointer without up pointer, the node path
- * during finding should be recorded for later use
*/
-template <typename T, bool returnEmptyNode = false>
+template <typename T>
class RBTree : public boost::noncopyable {
friend class RBNode<T>;
public:
@@ -320,7 +630,7 @@ public:
/// The constructor.
///
/// It never throws an exception.
- explicit RBTree();
+ explicit RBTree(bool returnEmptyNode = false);
/// \b Note: RBTree is not intended to be inherited so the destructor
/// is not virtual
@@ -333,22 +643,25 @@ public:
///
/// \anchor find
///
- /// These methods search the RBTree for a node whose name is a longest
+ /// These methods search the RBTree for a node whose name is longest
/// against name. The found node, if any, is returned via the node pointer.
///
/// By default, nodes that don't have data (see RBNode::isEmpty) are
/// ignored and the result can be NOTFOUND even if there's a node whose
- /// name mathes. The plan is to introduce a "no data OK" mode for this
- /// method, that would match any node of the tree regardless of wheather
- /// the node has any data or not.
+ /// name matches. If the \c RBTree is constructed with its
+ /// \c returnEmptyNode parameter being \c true, an empty node will also
+ /// be match candidates.
///
- /// The case with "no data OK" mode is not as easy as it seems. For example
- /// in the diagram shown in the class description, the name y.d.e.f is
- /// logically contained in the tree as part of the node w.y. It cannot be
- /// identified simply by checking whether existing nodes (such as
- /// d.e.f or w.y) has data.
+ /// \note Even when \c returnEmptyNode is \c true, not all empty nodes
+ /// in terms of the DNS protocol may necessarily be found by this method.
+ /// For example, in the \ref diagram shown in the class description,
+ /// the name y.d.e.f is logically contained in the tree as part of the
+ /// node w.y, but the \c find() variants cannot find the former for
+ /// the search key of y.d.e.f, no matter how the \c RBTree is constructed.
+ /// The caller of this method must use a different way to identify the
+ /// hidden match when necessary.
///
- /// These methods involves operations on names that can throw an exception.
+ /// These methods involve operations on names that can throw an exception.
/// If that happens the exception will be propagated to the caller.
/// The callback function should generally not throw an exception, but
/// if it throws, the exception will be propagated to the caller.
@@ -369,13 +682,39 @@ public:
/// of it. In that case, node parameter is left intact.
//@{
- /// \brief Find with callback.
+ /// \brief Simple find.
+ ///
+ /// Acts as described in the \ref find section.
+ Result find(const isc::dns::Name& name, RBNode<T>** node) const {
+ RBTreeNodeChain<T> node_path;
+ return (find<void*>(name, node, node_path, NULL, NULL));
+ }
+
+ /// \brief Simple find returning immutable node.
+ ///
+ /// Acts as described in the \ref find section, but returns immutable node
+ /// pointer.
+ Result find(const isc::dns::Name& name, const RBNode<T>** node) const {
+ RBTreeNodeChain<T> node_path;
+ RBNode<T> *target_node = NULL;
+ Result ret = (find<void*>(name, &target_node, node_path, NULL, NULL));
+ if (ret != NOTFOUND) {
+ *node = target_node;
+ }
+ return (ret);
+ }
+
+ /// \brief Find with callback and node chain.
///
- /// \anchor callback
+ /// This version of \c find() is specifically designed for the backend
+ /// of the \c MemoryZone class, and implements all necessary features
+ /// for that purpose. Other applications shouldn't need these additional
+ /// features, and should normally use the simpler versions.
///
- /// This version of find calls the callback whenever traversing (on the
- /// way from root down the tree) a marked node on the way down through the
- /// domain namespace (see RBNode::enableCallback and related functions).
+ /// This version of \c find() calls the callback whenever traversing (on
+ /// the way from root down the tree) a marked node on the way down through
+ /// the domain namespace (see \c RBNode::enableCallback and related
+ /// functions).
///
/// If you return true from the callback, the search is stopped and a
/// PARTIALMATCH is returned with the given node. Note that this node
@@ -388,9 +727,38 @@ public:
/// The callbacks are not general functors for the same reason - we don't
/// expect it to be needed.
///
+ /// Another special feature of this version is the ability to record
+ /// more detailed information regarding the search result.
+ ///
+ /// This information will be returned via the \c node_path parameter,
+ /// which is an object of class \c RBTreeNodeChain.
+ /// The passed parameter must be empty.
+ ///
+ /// \note The rest of the description isn't yet implemented. It will be
+ /// handled in Trac ticket #517.
+ ///
+ /// On success, the node sequence stoed in \c node_path will contain all
+ /// the ancestor nodes from the found node towards the root.
+ /// For example, if we look for o.w.y.d.e.f in the example \ref diagram,
+ /// \c node_path will contain w.y and d.e.f; the \c top() node of the
+ /// chain will be o, w.f and d.e.f will be stored below it.
+ ///
+ /// This feature can be used to get the absolute name for a node;
+ /// to do so, we need to travel upside from the node toward the root,
+ /// concatenating all ancestor names. With the current implementation
+ /// it's not possible without a node chain, because there is a no pointer
+ /// from the root of a subtree to the parent subtree (this may change
+ /// in a future version). A node chain can also be used to find the next
+ /// node of a given node in the entire RBTree; the \c nextNode() method
+ /// takes a node chain as a parameter.
+ ///
+ /// \exception isc::BadValue node_path is not empty (not yet implemented).
+ ///
/// \param name Target to be found
/// \param node On success (either \c EXACTMATCH or \c PARTIALMATCH)
/// it will store a pointer to the matching node
+ /// \param node_path Other search details will be stored (see the
+ /// description)
/// \param callback If non \c NULL, a call back function to be called
/// at marked nodes (see above).
/// \param callback_arg A caller supplied argument to be passed to
@@ -399,33 +767,57 @@ public:
/// \return As described above, but in case of callback returning true,
/// it returns immediately with the current node.
template <typename CBARG>
- Result find(const isc::dns::Name& name, RBNode<T>** node,
+ Result find(const isc::dns::Name& name,
+ RBNode<T>** node,
+ RBTreeNodeChain<T>& node_path,
bool (*callback)(const RBNode<T>&, CBARG),
CBARG callback_arg) const;
- /// \brief Find with callback returning immutable node.
+ /// \brief Simple find returning immutable node.
///
- /// It has the same behaviour as the find with \ref callback version.
+ /// Acts as described in the \ref find section, but returns immutable
+ /// node pointer.
template <typename CBARG>
- Result find(const isc::dns::Name& name, const RBNode<T>** node,
+ Result find(const isc::dns::Name& name,
+ const RBNode<T>** node,
+ RBTreeNodeChain<T>& node_path,
bool (*callback)(const RBNode<T>&, CBARG),
- CBARG callback_arg) const;
-
- /// \brief Simple find.
- ///
- /// Acts as described in the \ref find section.
- Result find(const isc::dns::Name& name, RBNode<T>** node) const {
- return (find<void*>(name, node, NULL, NULL));
+ CBARG callback_arg) const
+ {
+ RBNode<T>* target_node = NULL;
+ Result ret = find(name, &target_node, node_path, callback,
+ callback_arg);
+ if (ret != NOTFOUND) {
+ *node = target_node;
+ }
+ return (ret);
}
+ //@}
- /// \brieg Simple find returning immutable node.
+ /// \brief return the next bigger node in DNSSEC order from a given node
+ /// chain.
///
- /// Acts as described in the \ref find section, but returns immutable node
- /// pointer.
- Result find(const isc::dns::Name& name, const RBNode<T>** node) const {
- return (find<void*>(name, node, NULL, NULL));
- }
- //@}
+ /// This method identifies the next bigger node of the node currently
+ /// referenced in \c node_path and returns it.
+ /// This method also updates the passed \c node_path so that it will store
+ /// the path for the returned next node.
+ /// It will be convenient when we want to iterate over the all nodes
+ /// of \c RBTree; we can do this by calling this method repeatedly
+ /// starting from the root node.
+ ///
+ /// \note \c nextNode() will iterate over all the nodes in RBTree including
+ /// empty nodes. If empty node isn't desired, it's easy to add logic to
+ /// check return node and keep invoking \c nextNode() until the non-empty
+ /// node is retrieved.
+ ///
+ /// \exception isc::BadValue node_path is empty.
+ ///
+ /// \param node_path A node chain that stores all the nodes along the path
+ /// from root to node.
+ ///
+ /// \return An \c RBNode that is next bigger than \c node; if \c node is
+ /// the largest, \c NULL will be returned.
+ const RBNode<T>* nextNode(RBTreeNodeChain<T>& node_path) const;
/// \brief Get the total number of nodes in the tree
///
@@ -502,23 +894,11 @@ private:
//@{
/// \brief delete tree whose root is equal to node
void deleteHelper(RBNode<T> *node);
- /// \brief find the node with name
- ///
- /// Internal searching function.
- ///
- /// \param name What should be found.
- /// \param up It will point to the node whose down pointer points
- /// to the tree containing node. If we looked for o.w.y.d.e.f in the
- /// \ref diagram, the up would point to the w.y node.
- /// This parameter is not used currently, but it will be soon.
- /// \param node The found node.
- template <typename CBARG>
- Result findHelper(const isc::dns::Name& name, const RBNode<T>** up,
- RBNode<T>** node,
- bool (*callback)(const RBNode<T>&, CBARG),
- CBARG callback_arg) const;
+
+ /// \brief Print the information of given RBNode.
void dumpTreeHelper(std::ostream& os, const RBNode<T>* node,
unsigned int depth) const;
+
/// \brief Indentation helper function for dumpTree
static void indent(std::ostream& os, unsigned int depth);
@@ -529,39 +909,43 @@ private:
void nodeFission(RBNode<T>& node, const isc::dns::Name& sub_name);
//@}
- RBNode<T>* root_;
RBNode<T>* NULLNODE;
+ RBNode<T>* root_;
/// the node count of current tree
unsigned int node_count_;
+ /// search policy for rbtree
+ const bool needsReturnEmptyNode_;
};
-template <typename T, bool S>
-RBTree<T,S>::RBTree() {
- NULLNODE = RBNode<T>::NULL_NODE();
- root_ = NULLNODE;
- node_count_ = 0;
+template <typename T>
+RBTree<T>::RBTree(bool returnEmptyNode) :
+ NULLNODE(RBNode<T>::NULL_NODE()),
+ root_(NULLNODE),
+ node_count_(0),
+ needsReturnEmptyNode_(returnEmptyNode)
+{
}
-template <typename T, bool S>
-RBTree<T,S>::~RBTree() {
+template <typename T>
+RBTree<T>::~RBTree() {
deleteHelper(root_);
assert(node_count_ == 0);
}
-template <typename T, bool S>
-void
-RBTree<T,S>::deleteHelper(RBNode<T> *root) {
+template <typename T>
+void
+RBTree<T>::deleteHelper(RBNode<T>* root) {
if (root == NULLNODE) {
return;
}
- RBNode<T> *node = root;
+ RBNode<T>* node = root;
while (root->left_ != NULLNODE || root->right_ != NULLNODE) {
while (node->left_ != NULLNODE || node->right_ != NULLNODE) {
node = (node->left_ != NULLNODE) ? node->left_ : node->right_;
}
- RBNode<T> *parent = node->parent_;
+ RBNode<T>* parent = node->parent_;
if (parent->left_ == node) {
parent->left_ = NULLNODE;
} else {
@@ -579,79 +963,58 @@ RBTree<T,S>::deleteHelper(RBNode<T> *root) {
--node_count_;
}
-template <typename T, bool S>
+template <typename T>
template <typename CBARG>
-typename RBTree<T,S>::Result
-RBTree<T,S>::find(const isc::dns::Name& name, RBNode<T>** node,
+typename RBTree<T>::Result
+RBTree<T>::find(const isc::dns::Name& target_name,
+ RBNode<T>** target,
+ RBTreeNodeChain<T>& node_path,
bool (*callback)(const RBNode<T>&, CBARG),
CBARG callback_arg) const
{
- const RBNode<T>* up_node = NULLNODE;
- return (findHelper(name, &up_node, node, callback, callback_arg));
-}
+ using namespace helper;
-template <typename T, bool S>
-template <typename CBARG>
-typename RBTree<T,S>::Result
-RBTree<T,S>::find(const isc::dns::Name& name, const RBNode<T>** node,
- bool (*callback)(const RBNode<T>&, CBARG),
- CBARG callback_arg) const
-{
- const RBNode<T>* up_node;
- RBNode<T>* target_node;
- const typename RBTree<T,S>::Result ret =
- findHelper(name, &up_node, &target_node, callback, callback_arg);
- if (ret != NOTFOUND) {
- *node = target_node;
+ if (!node_path.isEmpty()) {
+ isc_throw(isc::BadValue, "RBTree::find is given a non empty chain");
}
- return (ret);
-}
-
-template <typename T, bool returnEmptyNode>
-template <typename CBARG>
-typename RBTree<T,returnEmptyNode>::Result
-RBTree<T,returnEmptyNode>::findHelper(const isc::dns::Name& target_name,
- const RBNode<T>** up_node,
- RBNode<T>** target,
- bool (*callback)(const RBNode<T>&, CBARG),
- CBARG callback_arg) const
-{
- using namespace helper;
RBNode<T>* node = root_;
- typename RBTree<T,returnEmptyNode>::Result ret = NOTFOUND;
- *up_node = NULLNODE;
+ Result ret = NOTFOUND;
isc::dns::Name name = target_name;
while (node != NULLNODE) {
- const isc::dns::NameComparisonResult compare_result =
- name.compare(node->name_);
+ node_path.last_compared_ = node;
+ node_path.last_comparison_ = name.compare(node->name_);
const isc::dns::NameComparisonResult::NameRelation relation =
- compare_result.getRelation();
+ node_path.last_comparison_.getRelation();
+
if (relation == isc::dns::NameComparisonResult::EQUAL) {
- if (returnEmptyNode || !node->isEmpty()) {
+ if (needsReturnEmptyNode_ || !node->isEmpty()) {
+ node_path.push(node);
*target = node;
ret = EXACTMATCH;
}
break;
} else {
- const int common_label_count = compare_result.getCommonLabels();
+ const int common_label_count =
+ node_path.last_comparison_.getCommonLabels();
// If the common label count is 1, there is no common label between
// the two names, except the trailing "dot".
if (common_label_count == 1) {
- node = (compare_result.getOrder() < 0) ?
+ node = (node_path.last_comparison_.getOrder() < 0) ?
node->left_ : node->right_;
} else if (relation == isc::dns::NameComparisonResult::SUBDOMAIN) {
- if (returnEmptyNode || !node->isEmpty()) {
- ret = RBTree<T,returnEmptyNode>::PARTIALMATCH;
+ if (needsReturnEmptyNode_ || !node->isEmpty()) {
+ ret = PARTIALMATCH;
*target = node;
- if (callback != NULL && node->callback_required_) {
+ if (callback != NULL &&
+ node->getFlag(RBNode<T>::FLAG_CALLBACK)) {
if ((callback)(*node, callback_arg)) {
break;
}
}
}
- *up_node = node;
+ node_path.push(node);
name = name - node->name_;
node = node->down_;
} else {
@@ -663,11 +1026,54 @@ RBTree<T,returnEmptyNode>::findHelper(const isc::dns::Name& target_name,
return (ret);
}
+template <typename T>
+const RBNode<T>*
+RBTree<T>::nextNode(RBTreeNodeChain<T>& node_path) const {
+ if (node_path.isEmpty()) {
+ isc_throw(isc::BadValue, "RBTree::nextNode is given an empty chain");
+ }
-template <typename T, bool returnEmptyNode>
-typename RBTree<T,returnEmptyNode>::Result
-RBTree<T,returnEmptyNode>::insert(const isc::dns::Name& target_name,
- RBNode<T>** new_node) {
+ const RBNode<T>* node = node_path.top();
+ // if node has sub domain, the next domain is the smallest
+ // domain in sub domain tree
+ if (node->down_ != NULLNODE) {
+ const RBNode<T>* left_most = node->down_;
+ while (left_most->left_ != NULLNODE) {
+ left_most = left_most->left_;
+ }
+ node_path.push(left_most);
+ return (left_most);
+ }
+
+ // node_path go to up level
+ node_path.pop();
+ // otherwise found the successor node in current level
+ const RBNode<T>* successor = node->successor();
+ if (successor != NULLNODE) {
+ node_path.push(successor);
+ return (successor);
+ }
+
+ // if no successor found move to up level, the next successor
+ // is the successor of up node in the up level tree, if
+ // up node doesn't have successor we gonna keep moving to up
+ // level
+ while (!node_path.isEmpty()) {
+ const RBNode<T>* up_node_successor = node_path.top()->successor();
+ node_path.pop();
+ if (up_node_successor != NULLNODE) {
+ node_path.push(up_node_successor);
+ return (up_node_successor);
+ }
+ }
+
+ return (NULL);
+}
+
+
+template <typename T>
+typename RBTree<T>::Result
+RBTree<T>::insert(const isc::dns::Name& target_name, RBNode<T>** new_node) {
using namespace helper;
RBNode<T>* parent = NULLNODE;
RBNode<T>* current = root_;
@@ -684,12 +1090,7 @@ RBTree<T,returnEmptyNode>::insert(const isc::dns::Name& target_name,
if (new_node != NULL) {
*new_node = current;
}
-
- if (current->isEmpty() && !returnEmptyNode) {
- return (SUCCESS);
- } else {
- return (ALREADYEXISTS);
- }
+ return (ALREADYEXISTS);
} else {
const int common_label_count = compare_result.getCommonLabels();
if (common_label_count == 1) {
@@ -746,9 +1147,9 @@ RBTree<T,returnEmptyNode>::insert(const isc::dns::Name& target_name,
}
-template <typename T, bool S>
+template <typename T>
void
-RBTree<T,S>::nodeFission(RBNode<T>& node, const isc::dns::Name& base_name) {
+RBTree<T>::nodeFission(RBNode<T>& node, const isc::dns::Name& base_name) {
using namespace helper;
const isc::dns::Name sub_name = node.name_ - base_name;
// using auto_ptr here is to avoid memory leak in case of exception raised
@@ -759,7 +1160,7 @@ RBTree<T,S>::nodeFission(RBNode<T>& node, const isc::dns::Name& base_name) {
// consistent behavior (i.e., a weak form of strong exception guarantee)
// even if code after the call to this function throws an exception.
std::swap(node.data_, down_node->data_);
- std::swap(node.callback_required_, down_node->callback_required_);
+ std::swap(node.flags_, down_node->flags_);
down_node->down_ = node.down_;
node.down_ = down_node.get();
// root node of sub tree, the initial color is BLACK
@@ -769,9 +1170,9 @@ RBTree<T,S>::nodeFission(RBNode<T>& node, const isc::dns::Name& base_name) {
}
-template <typename T, bool S>
+template <typename T>
void
-RBTree<T,S>::insertRebalance(RBNode<T>** root, RBNode<T>* node) {
+RBTree<T>::insertRebalance(RBNode<T>** root, RBNode<T>* node) {
RBNode<T>* uncle;
while (node != *root && node->parent_->color_ == RBNode<T>::RED) {
@@ -815,9 +1216,9 @@ RBTree<T,S>::insertRebalance(RBNode<T>** root, RBNode<T>* node) {
}
-template <typename T, bool S>
+template <typename T>
RBNode<T>*
-RBTree<T,S>::leftRotate(RBNode<T>** root, RBNode<T>* node) {
+RBTree<T>::leftRotate(RBNode<T>** root, RBNode<T>* node) {
RBNode<T>* right = node->right_;
node->right_ = right->left_;
if (right->left_ != NULLNODE)
@@ -840,9 +1241,9 @@ RBTree<T,S>::leftRotate(RBNode<T>** root, RBNode<T>* node) {
return (node);
}
-template <typename T, bool S>
+template <typename T>
RBNode<T>*
-RBTree<T,S>::rightRotate(RBNode<T>** root, RBNode<T>* node) {
+RBTree<T>::rightRotate(RBNode<T>** root, RBNode<T>* node) {
RBNode<T>* left = node->left_;
node->left_ = left->right_;
if (left->right_ != NULLNODE)
@@ -865,17 +1266,17 @@ RBTree<T,S>::rightRotate(RBNode<T>** root, RBNode<T>* node) {
}
-template <typename T, bool S>
+template <typename T>
void
-RBTree<T,S>::dumpTree(std::ostream& os, unsigned int depth) const {
+RBTree<T>::dumpTree(std::ostream& os, unsigned int depth) const {
indent(os, depth);
os << "tree has " << node_count_ << " node(s)\n";
dumpTreeHelper(os, root_, depth);
}
-template <typename T, bool S>
+template <typename T>
void
-RBTree<T,S>::dumpTreeHelper(std::ostream& os, const RBNode<T>* node,
+RBTree<T>::dumpTreeHelper(std::ostream& os, const RBNode<T>* node,
unsigned int depth) const
{
if (node == NULLNODE) {
@@ -900,15 +1301,13 @@ RBTree<T,S>::dumpTreeHelper(std::ostream& os, const RBNode<T>* node,
dumpTreeHelper(os, node->right_, depth + 1);
}
-template <typename T, bool S>
+template <typename T>
void
-RBTree<T,S>::indent(std::ostream& os, unsigned int depth) {
+RBTree<T>::indent(std::ostream& os, unsigned int depth) {
static const unsigned int INDENT_FOR_EACH_DEPTH = 5;
os << std::string(depth * INDENT_FOR_EACH_DEPTH, ' ');
}
-
-
}
}
diff --git a/src/lib/datasrc/tests/memory_datasrc_unittest.cc b/src/lib/datasrc/tests/memory_datasrc_unittest.cc
index 8d1703d..15b06b4 100644
--- a/src/lib/datasrc/tests/memory_datasrc_unittest.cc
+++ b/src/lib/datasrc/tests/memory_datasrc_unittest.cc
@@ -1,4 +1,5 @@
// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011 CZ NIC
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
@@ -12,8 +13,14 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
+#include <sstream>
+#include <vector>
+
+#include <boost/bind.hpp>
+
#include <exceptions/exceptions.h>
+#include <dns/masterload.h>
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
@@ -26,6 +33,7 @@
#include <gtest/gtest.h>
+using namespace std;
using namespace isc::dns;
using namespace isc::dns::rdata;
using namespace isc::datasrc;
@@ -139,51 +147,86 @@ TEST_F(MemoryDataSrcTest, getZoneCount) {
EXPECT_EQ(2, memory_datasrc.getZoneCount());
}
+// A helper callback of masterLoad() used in MemoryZoneTest.
+void
+setRRset(RRsetPtr rrset, vector<RRsetPtr*>::iterator& it) {
+ *(*it) = rrset;
+ ++it;
+}
+
/// \brief Test fixture for the MemoryZone class
class MemoryZoneTest : public ::testing::Test {
+ // A straightforward pair of textual RR(set) and a RRsetPtr variable
+ // to store the RRset. Used to build test data below.
+ struct RRsetData {
+ const char* const text; // textual representation of an RRset
+ RRsetPtr* rrset;
+ };
public:
MemoryZoneTest() :
class_(RRClass::IN()),
origin_("example.org"),
- ns_name_("ns.example.org"),
- cname_name_("cname.example.org"),
- child_ns_name_("child.example.org"),
- child_glue_name_("ns.child.example.org"),
- grandchild_ns_name_("grand.child.example.org"),
- grandchild_glue_name_("ns.grand.child.example.org"),
- zone_(class_, origin_),
- rr_out_(new RRset(Name("example.com"), class_, RRType::A(),
- RRTTL(300))),
- rr_ns_(new RRset(origin_, class_, RRType::NS(), RRTTL(300))),
- rr_ns_a_(new RRset(ns_name_, class_, RRType::A(), RRTTL(300))),
- rr_ns_aaaa_(new RRset(ns_name_, class_, RRType::AAAA(), RRTTL(300))),
- rr_a_(new RRset(origin_, class_, RRType::A(), RRTTL(300))),
- rr_cname_(new RRset(cname_name_, class_, RRType::CNAME(), RRTTL(300))),
- rr_cname_a_(new RRset(cname_name_, class_, RRType::A(), RRTTL(300))),
- rr_child_ns_(new RRset(child_ns_name_, class_, RRType::NS(),
- RRTTL(300))),
- rr_child_glue_(new RRset(child_glue_name_, class_, RRType::A(),
- RRTTL(300))),
- rr_grandchild_ns_(new RRset(grandchild_ns_name_, class_, RRType::NS(),
- RRTTL(300))),
- rr_grandchild_glue_(new RRset(grandchild_glue_name_, class_,
- RRType::AAAA(), RRTTL(300)))
+ zone_(class_, origin_)
{
+ // Build test RRsets. Below, we construct an RRset for
+ // each textual RR(s) of zone_data, and assign it to the corresponding
+ // rr_xxx.
+ const RRsetData zone_data[] = {
+ {"example.org. 300 IN NS ns.example.org.", &rr_ns_},
+ {"example.org. 300 IN A 192.0.2.1", &rr_a_},
+ {"ns.example.org. 300 IN A 192.0.2.2", &rr_ns_a_},
+ {"ns.example.org. 300 IN AAAA 2001:db8::2", &rr_ns_aaaa_},
+ {"cname.example.org. 300 IN CNAME canonical.example.org",
+ &rr_cname_},
+ {"cname.example.org. 300 IN A 192.0.2.3", &rr_cname_a_},
+ {"dname.example.org. 300 IN DNAME target.example.org.",
+ &rr_dname_},
+ {"dname.example.org. 300 IN A 192.0.2.39", &rr_dname_a_},
+ {"dname.example.org. 300 IN NS ns.dname.example.org.",
+ &rr_dname_ns_},
+ {"example.org. 300 IN DNAME example.com.", &rr_dname_apex_},
+ {"child.example.org. 300 IN NS ns.child.example.org.",
+ &rr_child_ns_},
+ {"ns.child.example.org. 300 IN A 192.0.2.153",
+ &rr_child_glue_},
+ {"grand.child.example.org. 300 IN NS ns.grand.child.example.org.",
+ &rr_grandchild_ns_},
+ {"ns.grand.child.example.org. 300 IN AAAA 2001:db8::253",
+ &rr_grandchild_glue_},
+ {"dname.child.example.org. 300 IN DNAME example.com.",
+ &rr_child_dname_},
+ {"example.com. 300 IN A 192.0.2.10", &rr_out_},
+ {"*.wild.example.org. 300 IN A 192.0.2.1", &rr_wild_},
+ {"wild.*.foo.example.org. 300 IN A 192.0.2.1", &rr_emptywild_},
+ {"wild.*.foo.*.bar.example.org. 300 IN A 192.0.2.1",
+ &rr_nested_emptywild_},
+ {"*.nswild.example.org. 300 IN NS nswild.example.", &rr_nswild_},
+ {"*.dnamewild.example.org. 300 IN DNAME dnamewild.example.",
+ &rr_dnamewild_},
+ {NULL, NULL}
+ };
+
+ stringstream zone_data_stream;
+ vector<RRsetPtr*> rrsets;
+ for (unsigned int i = 0; zone_data[i].text != NULL; ++i) {
+ zone_data_stream << zone_data[i].text << "\n";
+ rrsets.push_back(zone_data[i].rrset);
+ }
+
+ vector<RRsetPtr*>::iterator it = rrsets.begin();
+ masterLoad(zone_data_stream, Name::ROOT_NAME(), class_,
+ boost::bind(setRRset, _1, it));
}
// Some data to test with
const RRClass class_;
- const Name origin_, ns_name_, cname_name_, child_ns_name_,
- child_glue_name_, grandchild_ns_name_, grandchild_glue_name_;
+ const Name origin_;
// The zone to torture by tests
MemoryZone zone_;
/*
* Some RRsets to put inside the zone.
- * They are empty, but the MemoryZone does not have a reason to look
- * inside anyway. We will check it finds them and does not change
- * the pointer.
*/
- ConstRRsetPtr
+ RRsetPtr
// Out of zone RRset
rr_out_,
// NS of example.org
@@ -195,11 +238,20 @@ public:
// A of example.org
rr_a_;
RRsetPtr rr_cname_; // CNAME in example.org (RDATA will be added)
- ConstRRsetPtr rr_cname_a_; // for mixed CNAME + A case
- ConstRRsetPtr rr_child_ns_; // NS of a child domain (for delegation)
- ConstRRsetPtr rr_child_glue_; // glue RR of the child domain
- ConstRRsetPtr rr_grandchild_ns_; // NS below a zone cut (unusual)
- ConstRRsetPtr rr_grandchild_glue_; // glue RR below a deeper zone cut
+ RRsetPtr rr_cname_a_; // for mixed CNAME + A case
+ RRsetPtr rr_dname_; // DNAME in example.org (RDATA will be added)
+ RRsetPtr rr_dname_a_; // for mixed DNAME + A case
+ RRsetPtr rr_dname_ns_; // for mixed DNAME + NS case
+ RRsetPtr rr_dname_apex_; // for mixed DNAME + NS case in the apex
+ RRsetPtr rr_child_ns_; // NS of a child domain (for delegation)
+ RRsetPtr rr_child_glue_; // glue RR of the child domain
+ RRsetPtr rr_grandchild_ns_; // NS below a zone cut (unusual)
+ RRsetPtr rr_grandchild_glue_; // glue RR below a deeper zone cut
+ RRsetPtr rr_child_dname_; // A DNAME under NS
+ RRsetPtr rr_wild_;
+ RRsetPtr rr_emptywild_;
+ RRsetPtr rr_nested_emptywild_;
+ RRsetPtr rr_nswild_, rr_dnamewild_;
/**
* \brief Test one find query to the zone.
@@ -275,33 +327,30 @@ TEST_F(MemoryZoneTest, add) {
}
TEST_F(MemoryZoneTest, addMultipleCNAMEs) {
- rr_cname_->addRdata(generic::CNAME("canonical1.example.org."));
rr_cname_->addRdata(generic::CNAME("canonical2.example.org."));
EXPECT_THROW(zone_.add(rr_cname_), MemoryZone::AddError);
}
TEST_F(MemoryZoneTest, addCNAMEThenOther) {
- rr_cname_->addRdata(generic::CNAME("canonical.example.org."));
EXPECT_EQ(SUCCESS, zone_.add(rr_cname_));
EXPECT_THROW(zone_.add(rr_cname_a_), MemoryZone::AddError);
}
TEST_F(MemoryZoneTest, addOtherThenCNAME) {
- rr_cname_->addRdata(generic::CNAME("canonical.example.org."));
EXPECT_EQ(SUCCESS, zone_.add(rr_cname_a_));
EXPECT_THROW(zone_.add(rr_cname_), MemoryZone::AddError);
}
TEST_F(MemoryZoneTest, findCNAME) {
// install CNAME RR
- rr_cname_->addRdata(generic::CNAME("canonical.example.org."));
EXPECT_EQ(SUCCESS, zone_.add(rr_cname_));
// Find A RR of the same. Should match the CNAME
- findTest(cname_name_, RRType::NS(), Zone::CNAME, true, rr_cname_);
+ findTest(rr_cname_->getName(), RRType::NS(), Zone::CNAME, true, rr_cname_);
// Find the CNAME itself. Should result in normal SUCCESS
- findTest(cname_name_, RRType::CNAME(), Zone::SUCCESS, true, rr_cname_);
+ findTest(rr_cname_->getName(), RRType::CNAME(), Zone::SUCCESS, true,
+ rr_cname_);
}
TEST_F(MemoryZoneTest, findCNAMEUnderZoneCut) {
@@ -318,6 +367,80 @@ TEST_F(MemoryZoneTest, findCNAMEUnderZoneCut) {
Zone::FIND_GLUE_OK);
}
+// Two DNAMEs at single domain are disallowed by RFC 2672, section 3)
+// Having a CNAME there is disallowed too, but it is tested by
+// addOtherThenCNAME and addCNAMEThenOther.
+TEST_F(MemoryZoneTest, addMultipleDNAMEs) {
+ rr_dname_->addRdata(generic::DNAME("target2.example.org."));
+ EXPECT_THROW(zone_.add(rr_dname_), MemoryZone::AddError);
+}
+
+/*
+ * These two tests ensure that we can't have DNAME and NS at the same
+ * node with the exception of the apex of zone (forbidden by RFC 2672)
+ */
+TEST_F(MemoryZoneTest, addDNAMEThenNS) {
+ EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_)));
+ EXPECT_THROW(zone_.add(rr_dname_ns_), MemoryZone::AddError);
+}
+
+TEST_F(MemoryZoneTest, addNSThenDNAME) {
+ EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_ns_)));
+ EXPECT_THROW(zone_.add(rr_dname_), MemoryZone::AddError);
+}
+
+// It is allowed to have NS and DNAME at apex
+TEST_F(MemoryZoneTest, DNAMEAndNSAtApex) {
+ EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_apex_)));
+ EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
+
+ // The NS should be possible to be found, below should be DNAME, not
+ // delegation
+ findTest(origin_, RRType::NS(), Zone::SUCCESS, true, rr_ns_);
+ findTest(rr_child_ns_->getName(), RRType::A(), Zone::DNAME, true,
+ rr_dname_apex_);
+}
+
+TEST_F(MemoryZoneTest, NSAndDNAMEAtApex) {
+ EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
+ EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_apex_)));
+}
+
+// TODO: Test (and implement) adding data under DNAME. That is forbidden by
+// 2672 as well.
+
+// Search under a DNAME record. It should return the DNAME
+TEST_F(MemoryZoneTest, findBelowDNAME) {
+ EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_)));
+ findTest(Name("below.dname.example.org"), RRType::A(), Zone::DNAME, true,
+ rr_dname_);
+}
+
+// Search at the domain with DNAME. It should act as DNAME isn't there, DNAME
+// influences only the data below (see RFC 2672, section 3)
+TEST_F(MemoryZoneTest, findAtDNAME) {
+ EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_)));
+ EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_a_)));
+
+ const Name dname_name(rr_dname_->getName());
+ findTest(dname_name, RRType::A(), Zone::SUCCESS, true, rr_dname_a_);
+ findTest(dname_name, RRType::DNAME(), Zone::SUCCESS, true, rr_dname_);
+ findTest(dname_name, RRType::TXT(), Zone::NXRRSET, true);
+}
+
+// Try searching something that is both under NS and DNAME, without and with
+// GLUE_OK mode (it should stop at the NS and DNAME respectively).
+TEST_F(MemoryZoneTest, DNAMEUnderNS) {
+ zone_.add(rr_child_ns_);
+ zone_.add(rr_child_dname_);
+
+ Name lowName("below.dname.child.example.org.");
+
+ findTest(lowName, RRType::A(), Zone::DELEGATION, true, rr_child_ns_);
+ findTest(lowName, RRType::A(), Zone::DNAME, true, rr_child_dname_, NULL,
+ NULL, Zone::FIND_GLUE_OK);
+}
+
// Test adding child zones and zone cut handling
TEST_F(MemoryZoneTest, delegationNS) {
// add in-zone data
@@ -366,7 +489,7 @@ TEST_F(MemoryZoneTest, findAny) {
EXPECT_EQ(0, out_rrsets.size());
RRsetList glue_child_rrsets;
- findTest(child_glue_name_, RRType::ANY(), Zone::SUCCESS, true,
+ findTest(rr_child_glue_->getName(), RRType::ANY(), Zone::SUCCESS, true,
ConstRRsetPtr(), &glue_child_rrsets);
EXPECT_EQ(rr_child_glue_, glue_child_rrsets.findRRset(RRType::A(),
RRClass::IN()));
@@ -380,13 +503,13 @@ TEST_F(MemoryZoneTest, findAny) {
// zone cut
RRsetList child_rrsets;
- findTest(child_ns_name_, RRType::ANY(), Zone::DELEGATION, true,
+ findTest(rr_child_ns_->getName(), RRType::ANY(), Zone::DELEGATION, true,
rr_child_ns_, &child_rrsets);
EXPECT_EQ(0, child_rrsets.size());
// glue for this zone cut
RRsetList new_glue_child_rrsets;
- findTest(child_glue_name_, RRType::ANY(), Zone::DELEGATION, true,
+ findTest(rr_child_glue_->getName(), RRType::ANY(), Zone::DELEGATION, true,
rr_child_ns_, &new_glue_child_rrsets);
EXPECT_EQ(0, new_glue_child_rrsets.size());
}
@@ -403,16 +526,16 @@ TEST_F(MemoryZoneTest, glue) {
EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_grandchild_glue_)));
// by default glue is hidden due to the zone cut
- findTest(child_glue_name_, RRType::A(), Zone::DELEGATION, true,
+ findTest(rr_child_glue_->getName(), RRType::A(), Zone::DELEGATION, true,
rr_child_ns_);
// If we do it in the "glue OK" mode, we should find the exact match.
- findTest(child_glue_name_, RRType::A(), Zone::SUCCESS, true,
+ findTest(rr_child_glue_->getName(), RRType::A(), Zone::SUCCESS, true,
rr_child_glue_, NULL, NULL, Zone::FIND_GLUE_OK);
// glue OK + NXRRSET case
- findTest(child_glue_name_, RRType::AAAA(), Zone::NXRRSET, true,
+ findTest(rr_child_glue_->getName(), RRType::AAAA(), Zone::NXRRSET, true,
ConstRRsetPtr(), NULL, NULL, Zone::FIND_GLUE_OK);
// glue OK + NXDOMAIN case
@@ -425,7 +548,8 @@ TEST_F(MemoryZoneTest, glue) {
// (This case cannot be tested yet)
// nested cut case. The glue should be found.
- findTest(grandchild_glue_name_, RRType::AAAA(), Zone::SUCCESS,
+ findTest(rr_grandchild_glue_->getName(), RRType::AAAA(),
+ Zone::SUCCESS,
true, rr_grandchild_glue_, NULL, NULL, Zone::FIND_GLUE_OK);
// A non-existent name in nested cut. This should result in delegation
@@ -435,18 +559,6 @@ TEST_F(MemoryZoneTest, glue) {
Zone::FIND_GLUE_OK);
}
-// Test adding DNAMEs and resulting delegation handling
-// Listing ideas only for now
-TEST_F(MemoryZoneTest, delegationDNAME) {
- // apex DNAME: allowed by spec. No DNAME delegation at the apex;
- // descendants are subject to delegation.
-
- // Other cases of NS and DNAME mixture are prohibited.
- // BIND 9 doesn't reject such cases at load time, however.
-
- // DNAME and ordinary types (allowed by spec)
-}
-
/**
* \brief Test searching.
*
@@ -464,17 +576,56 @@ TEST_F(MemoryZoneTest, find) {
// These two should be successful
findTest(origin_, RRType::NS(), Zone::SUCCESS, true, rr_ns_);
- findTest(ns_name_, RRType::A(), Zone::SUCCESS, true, rr_ns_a_);
+ findTest(rr_ns_a_->getName(), RRType::A(), Zone::SUCCESS, true, rr_ns_a_);
// These domain exist but don't have the provided RRType
findTest(origin_, RRType::AAAA(), Zone::NXRRSET);
- findTest(ns_name_, RRType::NS(), Zone::NXRRSET);
+ findTest(rr_ns_a_->getName(), RRType::NS(), Zone::NXRRSET);
// These domains don't exist (and one is out of the zone)
findTest(Name("nothere.example.org"), RRType::A(), Zone::NXDOMAIN);
findTest(Name("example.net"), RRType::A(), Zone::NXDOMAIN);
}
+TEST_F(MemoryZoneTest, emptyNode) {
+ /*
+ * The backend RBTree for this test should look like as follows:
+ * example.org
+ * |
+ * baz (empty; easy case)
+ * / | \
+ * bar | x.foo ('foo' part is empty; a bit trickier)
+ * bbb
+ * /
+ * aaa
+ */
+
+ // Construct the test zone
+ const char* const names[] = {
+ "bar.example.org", "x.foo.example.org", "aaa.baz.example.org",
+ "bbb.baz.example.org.", NULL};
+ for (int i = 0; names[i] != NULL; ++i) {
+ ConstRRsetPtr rrset(new RRset(Name(names[i]), class_, RRType::A(),
+ RRTTL(300)));
+ EXPECT_EQ(SUCCESS, zone_.add(rrset));
+ }
+
+ // empty node matching, easy case: the node for 'baz' exists with
+ // no data.
+ findTest(Name("baz.example.org"), RRType::A(), Zone::NXRRSET);
+
+ // empty node matching, a trickier case: the node for 'foo' is part of
+ // "x.foo", which should be considered an empty node.
+ findTest(Name("foo.example.org"), RRType::A(), Zone::NXRRSET);
+
+ // "org" is contained in "example.org", but it shouldn't be treated as
+ // NXRRSET because it's out of zone.
+ // Note: basically we don't expect such a query to be performed (the common
+ // operation is to identify the best matching zone first then perform
+ // search it), but we shouldn't be confused even in the unexpected case.
+ findTest(Name("org"), RRType::A(), Zone::NXDOMAIN);
+}
+
TEST_F(MemoryZoneTest, load) {
// Put some data inside the zone
EXPECT_NO_THROW(EXPECT_EQ(result::SUCCESS, zone_.add(rr_ns_)));
@@ -497,14 +648,59 @@ TEST_F(MemoryZoneTest, load) {
findTest(Name("a.root-servers.net."), RRType::A(), Zone::SUCCESS, false,
ConstRRsetPtr(), NULL, &rootzone);
// But this should no longer be here
- findTest(ns_name_, RRType::AAAA(), Zone::NXDOMAIN, true, ConstRRsetPtr(),
- NULL, &rootzone);
+ findTest(rr_ns_a_->getName(), RRType::AAAA(), Zone::NXDOMAIN, true,
+ ConstRRsetPtr(), NULL, &rootzone);
// Try loading zone that is wrong in a different way
EXPECT_THROW(zone_.load(TEST_DATA_DIR "/duplicate_rrset.zone"),
MasterLoadError);
}
+// Note: once #507 is merged, findTest() would succeed whether or not
+// we load the wildcard correctly, so the test will become meaningless.
+// The plan is to clean them up when we complete #551 (then the effect of
+// load will be indirectly tested via find() tests).
+TEST_F(MemoryZoneTest, loadWildcard) {
+ /*
+ * example.org.
+ * |
+ * wild (not *.wild, should have wild mark)
+ * |
+ * *
+ */
+ EXPECT_EQ(SUCCESS, zone_.add(rr_wild_));
+ findTest(Name("wild.example.org"), RRType::A(), Zone::NXRRSET);
+}
+
+// same note as loadWildcard applies.
+TEST_F(MemoryZoneTest, loadEmptyWildcard) {
+ /*
+ * example.org.
+ * foo
+ * *
+ * wild
+ */
+ EXPECT_EQ(SUCCESS, zone_.add(rr_emptywild_));
+ findTest(Name("*.foo.example.org"), RRType::A(), Zone::NXRRSET);
+ findTest(Name("foo.example.org"), RRType::A(), Zone::NXRRSET);
+}
+
+// same note as loadWildcard applies.
+TEST_F(MemoryZoneTest, loadNestedEmptyWildcard) {
+ EXPECT_EQ(SUCCESS, zone_.add(rr_nested_emptywild_));
+ findTest(Name("*.foo.*.bar.example.org"), RRType::A(), Zone::NXRRSET);
+ findTest(Name("foo.*.bar.example.org"), RRType::A(), Zone::NXRRSET);
+ findTest(Name("*.bar.example.org"), RRType::A(), Zone::NXRRSET);
+ findTest(Name("bar.example.org"), RRType::A(), Zone::NXRRSET);
+}
+
+TEST_F(MemoryZoneTest, loadBadWildcard) {
+ // We reject loading the zone if it contains a wildcard name for
+ // NS or DNAME.
+ EXPECT_THROW(zone_.add(rr_nswild_), MemoryZone::AddError);
+ EXPECT_THROW(zone_.add(rr_dnamewild_), MemoryZone::AddError);
+}
+
TEST_F(MemoryZoneTest, swap) {
// build one zone with some data
MemoryZone zone1(class_, origin_);
diff --git a/src/lib/datasrc/tests/rbtree_unittest.cc b/src/lib/datasrc/tests/rbtree_unittest.cc
index 0105cad..82eed63 100644
--- a/src/lib/datasrc/tests/rbtree_unittest.cc
+++ b/src/lib/datasrc/tests/rbtree_unittest.cc
@@ -12,9 +12,10 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
-
#include <gtest/gtest.h>
+#include <exceptions/exceptions.h>
+
#include <dns/name.h>
#include <dns/rrclass.h>
#include <dns/rrset.h>
@@ -26,10 +27,15 @@
#include <dns/tests/unittest_util.h>
using namespace std;
+using namespace isc;
using namespace isc::dns;
using isc::UnitTestUtil;
using namespace isc::datasrc;
+// XXX: some compilers cannot find class static constants used in
+// EXPECT_xxx macros, for which we need an explicit empty definition.
+const size_t Name::MAX_LABELS;
+
/* The initial structure of rbtree
*
* b
@@ -50,9 +56,10 @@ using namespace isc::datasrc;
namespace {
class RBTreeTest : public::testing::Test {
protected:
- RBTreeTest() : rbtree() {
- const char * domain_names[] = {"c", "b", "a", "x.d.e.f", "z.d.e.f", "g.h", "i.g.h",
- "o.w.y.d.e.f", "j.z.d.e.f", "p.w.y.d.e.f", "q.w.y.d.e.f"};
+ RBTreeTest() : rbtree_expose_empty_node(true) {
+ const char* const domain_names[] = {
+ "c", "b", "a", "x.d.e.f", "z.d.e.f", "g.h", "i.g.h", "o.w.y.d.e.f",
+ "j.z.d.e.f", "p.w.y.d.e.f", "q.w.y.d.e.f"};
int name_count = sizeof(domain_names) / sizeof(domain_names[0]);
for (int i = 0; i < name_count; ++i) {
rbtree.insert(Name(domain_names[i]), &rbtnode);
@@ -65,8 +72,7 @@ protected:
}
RBTree<int> rbtree;
- typedef RBTree<int, true> ExposeRBTree;
- ExposeRBTree rbtree_expose_empty_node;
+ RBTree<int> rbtree_expose_empty_node;
RBNode<int>* rbtnode;
const RBNode<int>* crbtnode;
};
@@ -82,69 +88,34 @@ TEST_F(RBTreeTest, setGetData) {
}
TEST_F(RBTreeTest, insertNames) {
- //if don't expose empty node, even the node already exsit which is caused by node fission
- //we will return succeed
- EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("d.e.f"), &rbtnode));
+ EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("d.e.f"),
+ &rbtnode));
EXPECT_EQ(Name("d.e.f"), rbtnode->getName());
EXPECT_EQ(13, rbtree.getNodeCount());
- EXPECT_EQ(ExposeRBTree::ALREADYEXISTS,
- rbtree_expose_empty_node.insert(Name("d.e.f"), &rbtnode));
- EXPECT_EQ(Name("d.e.f"), rbtnode->getName());
- EXPECT_EQ(13, rbtree_expose_empty_node.getNodeCount());
-
-
//insert not exist node
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("."), &rbtnode));
EXPECT_EQ(Name("."), rbtnode->getName());
EXPECT_EQ(14, rbtree.getNodeCount());
- EXPECT_EQ(ExposeRBTree::SUCCESS, rbtree_expose_empty_node.insert(
- Name("."), &rbtnode));
- EXPECT_EQ(Name("."), rbtnode->getName());
- EXPECT_EQ(14, rbtree_expose_empty_node.getNodeCount());
-
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("example.com"), &rbtnode));
EXPECT_EQ(15, rbtree.getNodeCount());
rbtnode->setData(RBNode<int>::NodeDataPtr(new int(12)));
- EXPECT_EQ(ExposeRBTree::SUCCESS, rbtree_expose_empty_node.insert(
- Name("example.com"), &rbtnode));
- EXPECT_EQ(15, rbtree_expose_empty_node.getNodeCount());
- rbtnode->setData(RBNode<int>::NodeDataPtr(new int(12)));
-
-
// return ALREADYEXISTS, since node "example.com" already has been explicitly inserted
EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("example.com"), &rbtnode));
EXPECT_EQ(15, rbtree.getNodeCount());
- EXPECT_EQ(ExposeRBTree::ALREADYEXISTS,
- rbtree_expose_empty_node.insert(Name("example.com"), &rbtnode));
- EXPECT_EQ(15, rbtree_expose_empty_node.getNodeCount());
-
// split the node "d.e.f"
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("k.e.f"), &rbtnode));
EXPECT_EQ(Name("k"), rbtnode->getName());
EXPECT_EQ(17, rbtree.getNodeCount());
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("k.e.f"), &rbtnode));
- EXPECT_EQ(Name("k"), rbtnode->getName());
- EXPECT_EQ(17, rbtree_expose_empty_node.getNodeCount());
-
-
// split the node "g.h"
- EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("h"), &rbtnode));
+ EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("h"), &rbtnode));
EXPECT_EQ(Name("h"), rbtnode->getName());
EXPECT_EQ(18, rbtree.getNodeCount());
- //node fission will create node "h"
- EXPECT_EQ(ExposeRBTree::ALREADYEXISTS,
- rbtree_expose_empty_node.insert(Name("h"), &rbtnode));
- EXPECT_EQ(Name("h"), rbtnode->getName());
- EXPECT_EQ(18, rbtree_expose_empty_node.getNodeCount());
-
-
// add child domain
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("m.p.w.y.d.e.f"), &rbtnode));
EXPECT_EQ(Name("m"), rbtnode->getName());
@@ -153,41 +124,18 @@ TEST_F(RBTreeTest, insertNames) {
EXPECT_EQ(Name("n"), rbtnode->getName());
EXPECT_EQ(20, rbtree.getNodeCount());
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("m.p.w.y.d.e.f"), &rbtnode));
- EXPECT_EQ(Name("m"), rbtnode->getName());
- EXPECT_EQ(19, rbtree_expose_empty_node.getNodeCount());
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("n.p.w.y.d.e.f"), &rbtnode));
- EXPECT_EQ(Name("n"), rbtnode->getName());
- EXPECT_EQ(20, rbtree_expose_empty_node.getNodeCount());
-
-
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("l.a"), &rbtnode));
EXPECT_EQ(Name("l"), rbtnode->getName());
EXPECT_EQ(21, rbtree.getNodeCount());
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("l.a"), &rbtnode));
- EXPECT_EQ(Name("l"), rbtnode->getName());
- EXPECT_EQ(21, rbtree_expose_empty_node.getNodeCount());
-
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("r.d.e.f"), &rbtnode));
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("s.d.e.f"), &rbtnode));
EXPECT_EQ(23, rbtree.getNodeCount());
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("r.d.e.f"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("s.d.e.f"), &rbtnode));
- EXPECT_EQ(23, rbtree_expose_empty_node.getNodeCount());
-
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("h.w.y.d.e.f"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("h.w.y.d.e.f"), &rbtnode));
// add more nodes one by one to cover leftRotate and rightRotate
- EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("f"), &rbtnode));
+ EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("f"), &rbtnode));
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("m"), &rbtnode));
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("nm"), &rbtnode));
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("om"), &rbtnode));
@@ -198,32 +146,8 @@ TEST_F(RBTreeTest, insertNames) {
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("i"), &rbtnode));
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("ae"), &rbtnode));
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("n"), &rbtnode));
-
- EXPECT_EQ(ExposeRBTree::ALREADYEXISTS,
- rbtree_expose_empty_node.insert(Name("f"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("m"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("nm"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("om"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("k"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("l"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("fe"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("ge"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("i"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("ae"), &rbtnode));
- EXPECT_EQ(ExposeRBTree::SUCCESS,
- rbtree_expose_empty_node.insert(Name("n"), &rbtnode));
}
-
TEST_F(RBTreeTest, findName) {
// find const rbtnode
// exact match
@@ -236,15 +160,57 @@ TEST_F(RBTreeTest, findName) {
EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("x"), &crbtnode));
EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("m.n"), &crbtnode));
+ // if we expose empty node, we can get the empty node created during insert
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ rbtree_expose_empty_node.find(Name("d.e.f"), &crbtnode));
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ rbtree_expose_empty_node.find(Name("w.y.d.e.f"), &crbtnode));
+
// partial match
EXPECT_EQ(RBTree<int>::PARTIALMATCH, rbtree.find(Name("m.b"), &crbtnode));
EXPECT_EQ(Name("b"), crbtnode->getName());
+ EXPECT_EQ(RBTree<int>::PARTIALMATCH,
+ rbtree_expose_empty_node.find(Name("m.d.e.f"), &crbtnode));
// find rbtnode
EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("q.w.y.d.e.f"), &rbtnode));
EXPECT_EQ(Name("q"), rbtnode->getName());
}
+TEST_F(RBTreeTest, findError) {
+ // For the version that takes a node chain, the chain must be empty.
+ RBTreeNodeChain<int> chain;
+ EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find<void*>(Name("a"), &crbtnode,
+ chain, NULL, NULL));
+ // trying to reuse the same chain. it should result in an exception.
+ EXPECT_THROW(rbtree.find<void*>(Name("a"), &crbtnode, chain, NULL, NULL),
+ BadValue);
+}
+
+TEST_F(RBTreeTest, flags) {
+ EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("flags.example"),
+ &rbtnode));
+
+ // by default, flags are all off
+ EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
+
+ // set operation, by default it enables the flag
+ rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
+ EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
+
+ // try disable the flag explicitly
+ rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK, false);
+ EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
+
+ // try enable the flag explicitly
+ rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK, true);
+ EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
+
+ // setting an unknown flag will trigger an exception
+ EXPECT_THROW(rbtnode->setFlag(static_cast<RBNode<int>::Flags>(2), true),
+ isc::InvalidParameter);
+}
+
bool
testCallback(const RBNode<int>&, bool* callack_checker) {
*callack_checker = true;
@@ -256,50 +222,271 @@ TEST_F(RBTreeTest, callback) {
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("callback.example"),
&rbtnode));
rbtnode->setData(RBNode<int>::NodeDataPtr(new int(1)));
- EXPECT_FALSE(rbtnode->isCallbackEnabled());
+ EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
// enable/re-disable callback
- rbtnode->enableCallback();
- EXPECT_TRUE(rbtnode->isCallbackEnabled());
- rbtnode->disableCallback();
- EXPECT_FALSE(rbtnode->isCallbackEnabled());
+ rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
+ EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
+ rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK, false);
+ EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
// enable again for subsequent tests
- rbtnode->enableCallback();
+ rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
// add more levels below and above the callback node for partial match.
RBNode<int>* subrbtnode;
EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("sub.callback.example"),
&subrbtnode));
subrbtnode->setData(RBNode<int>::NodeDataPtr(new int(2)));
RBNode<int>* parentrbtnode;
- EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("example"),
+ EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("example"),
&parentrbtnode));
// the chilld/parent nodes shouldn't "inherit" the callback flag.
// "rbtnode" may be invalid due to the insertion, so we need to re-find
// it.
EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("callback.example"),
&rbtnode));
- EXPECT_TRUE(rbtnode->isCallbackEnabled());
- EXPECT_FALSE(subrbtnode->isCallbackEnabled());
- EXPECT_FALSE(parentrbtnode->isCallbackEnabled());
+ EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
+ EXPECT_FALSE(subrbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
+ EXPECT_FALSE(parentrbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
// check if the callback is called from find()
+ RBTreeNodeChain<int> node_path1;
bool callback_called = false;
EXPECT_EQ(RBTree<int>::EXACTMATCH,
- rbtree.find(Name("sub.callback.example"), &crbtnode,
+ rbtree.find(Name("sub.callback.example"), &crbtnode, node_path1,
testCallback, &callback_called));
EXPECT_TRUE(callback_called);
// enable callback at the parent node, but it doesn't have data so
// the callback shouldn't be called.
- parentrbtnode->enableCallback();
+ RBTreeNodeChain<int> node_path2;
+ parentrbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
callback_called = false;
EXPECT_EQ(RBTree<int>::EXACTMATCH,
- rbtree.find(Name("callback.example"), &crbtnode,
+ rbtree.find(Name("callback.example"), &crbtnode, node_path2,
testCallback, &callback_called));
EXPECT_FALSE(callback_called);
}
+TEST_F(RBTreeTest, chainLevel) {
+ RBTreeNodeChain<int> chain;
+
+ // by default there should be no level in the chain.
+ EXPECT_EQ(0, chain.getLevelCount());
+
+ // insert one node to the tree and find it. there should be exactly
+ // one level in the chain.
+ RBTree<int> tree(true);
+ Name node_name(Name::ROOT_NAME());
+ EXPECT_EQ(RBTree<int>::SUCCESS, tree.insert(node_name, &rbtnode));
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ tree.find<void*>(node_name, &crbtnode, chain, NULL, NULL));
+ EXPECT_EQ(1, chain.getLevelCount());
+
+ /*
+ * Now creating a possibly deepest tree with MAX_LABELS - 1 levels.
+ * it should look like:
+ * a
+ * /|
+ * (.)a
+ * |
+ * a
+ * : (MAX_LABELS - 1) "a"'s
+ *
+ * then confirm that find() for the deepest name succeeds without any
+ * disruption, and the resulting chain has the expected level.
+ * Note that "a." and the root name (".") belong to the same level.
+ * So the possible maximum level is MAX_LABELS - 1, not MAX_LABELS.
+ */
+ for (unsigned int i = 1; i < Name::MAX_LABELS; ++i) {
+ node_name = Name("a.").concatenate(node_name);
+ EXPECT_EQ(RBTree<int>::SUCCESS, tree.insert(node_name, &rbtnode));
+ RBTreeNodeChain<int> found_chain;
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ tree.find<void*>(node_name, &crbtnode, found_chain,
+ NULL, NULL));
+ EXPECT_EQ(i, found_chain.getLevelCount());
+ }
+
+ // Confirm the last inserted name has the possible maximum length with
+ // maximum label count. This confirms the rbtree and chain level cannot
+ // be larger.
+ EXPECT_EQ(Name::MAX_LABELS, node_name.getLabelCount());
+ EXPECT_THROW(node_name.concatenate(Name("a.")), TooLongName);
+}
+
+TEST_F(RBTreeTest, getAbsoluteNameError) {
+ // an empty chain isn't allowed.
+ RBTreeNodeChain<int> chain;
+ EXPECT_THROW(chain.getAbsoluteName(), BadValue);
+}
+
+/*
+ *the domain order should be:
+ * a, b, c, d.e.f, x.d.e.f, w.y.d.e.f, o.w.y.d.e.f, p.w.y.d.e.f, q.w.y.d.e.f,
+ * z.d.e.f, j.z.d.e.f, g.h, i.g.h
+ * b
+ * / \
+ * a d.e.f
+ * / | \
+ * c | g.h
+ * | |
+ * w.y i
+ * / | \
+ * x | z
+ * | |
+ * p j
+ * / \
+ * o q
+ */
+TEST_F(RBTreeTest, nextNode) {
+ const char* const names[] = {
+ "a", "b", "c", "d.e.f", "x.d.e.f", "w.y.d.e.f", "o.w.y.d.e.f",
+ "p.w.y.d.e.f", "q.w.y.d.e.f", "z.d.e.f", "j.z.d.e.f", "g.h", "i.g.h"};
+ const int name_count = sizeof(names) / sizeof(names[0]);
+ RBTreeNodeChain<int> node_path;
+ const RBNode<int>* node = NULL;
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ rbtree.find<void*>(Name(names[0]), &node, node_path, NULL,
+ NULL));
+ for (int i = 0; i < name_count; ++i) {
+ EXPECT_NE(static_cast<void*>(NULL), node);
+ EXPECT_EQ(Name(names[i]), node_path.getAbsoluteName());
+ node = rbtree.nextNode(node_path);
+ }
+
+ // We should have reached the end of the tree.
+ EXPECT_EQ(static_cast<void*>(NULL), node);
+}
+
+TEST_F(RBTreeTest, nextNodeError) {
+ // Empty chain for nextNode() is invalid.
+ RBTreeNodeChain<int> chain;
+ EXPECT_THROW(rbtree.nextNode(chain), BadValue);
+}
+
+// A helper function for getLastComparedNode() below.
+void
+comparisonChecks(const RBTreeNodeChain<int>& chain,
+ int expected_order, int expected_common_labels,
+ NameComparisonResult::NameRelation expected_reln)
+{
+ if (expected_order > 0) {
+ EXPECT_LT(0, chain.getLastComparisonResult().getOrder());
+ } else if (expected_order < 0) {
+ EXPECT_GT(0, chain.getLastComparisonResult().getOrder());
+ } else {
+ EXPECT_EQ(0, chain.getLastComparisonResult().getOrder());
+ }
+ EXPECT_EQ(expected_common_labels,
+ chain.getLastComparisonResult().getCommonLabels());
+ EXPECT_EQ(expected_reln,
+ chain.getLastComparisonResult().getRelation());
+}
+
+TEST_F(RBTreeTest, getLastComparedNode) {
+ RBTree<int>& tree = rbtree_expose_empty_node; // use the "empty OK" mode
+ RBTreeNodeChain<int> chain;
+
+ // initially there should be no 'last compared'.
+ EXPECT_EQ(static_cast<void*>(NULL), chain.getLastComparedNode());
+
+ // A search for an empty tree should result in no 'last compared', too.
+ RBTree<int> empty_tree;
+ EXPECT_EQ(RBTree<int>::NOTFOUND,
+ empty_tree.find<void*>(Name("a"), &crbtnode, chain, NULL, NULL));
+ EXPECT_EQ(static_cast<void*>(NULL), chain.getLastComparedNode());
+ chain.clear();
+
+ const RBNode<int>* expected_node;
+
+ // Exact match case. The returned node should be last compared.
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ tree.find<void*>(Name("x.d.e.f"), &expected_node, chain,
+ NULL, NULL));
+ EXPECT_EQ(expected_node, chain.getLastComparedNode());
+ // 2 = # labels of "x."
+ comparisonChecks(chain, 0, 2, NameComparisonResult::EQUAL);
+ chain.clear();
+
+ // Partial match, search stopped at the matching node, which should be
+ // the last compared node.
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ tree.find(Name("i.g.h"), &expected_node));
+ EXPECT_EQ(RBTree<int>::PARTIALMATCH,
+ tree.find<void*>(Name("x.i.g.h"), &crbtnode, chain,
+ NULL, NULL));
+ EXPECT_EQ(expected_node, chain.getLastComparedNode());
+ // i.g.h < x.i.g.h, 2 = # labels of "i."
+ comparisonChecks(chain, 1, 2, NameComparisonResult::SUBDOMAIN);
+ chain.clear();
+
+ // Partial match, search stopped in the subtree below the matching node
+ // after following a left branch.
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ tree.find(Name("x.d.e.f"), &expected_node));
+ EXPECT_EQ(RBTree<int>::PARTIALMATCH,
+ tree.find<void*>(Name("a.d.e.f"), &crbtnode, chain,
+ NULL, NULL));
+ EXPECT_EQ(expected_node, chain.getLastComparedNode());
+ // a < x, 1 = # labels of "." (trailing dot)
+ comparisonChecks(chain, -1, 1, NameComparisonResult::COMMONANCESTOR);
+ chain.clear();
+
+ // Partial match, search stopped in the subtree below the matching node
+ // after following a right branch.
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ tree.find(Name("z.d.e.f"), &expected_node));
+ EXPECT_EQ(RBTree<int>::PARTIALMATCH,
+ tree.find<void*>(Name("zz.d.e.f"), &crbtnode, chain,
+ NULL, NULL));
+ EXPECT_EQ(expected_node, chain.getLastComparedNode());
+ // zz > z, 1 = # labels of "." (trailing dot)
+ comparisonChecks(chain, 1, 1, NameComparisonResult::COMMONANCESTOR);
+ chain.clear();
+
+ // Partial match, search stopped at a node for a super domain of the
+ // search name in the subtree below the matching node.
+ EXPECT_EQ(RBTree<int>::EXACTMATCH,
+ tree.find(Name("w.y.d.e.f"), &expected_node));
+ EXPECT_EQ(RBTree<int>::PARTIALMATCH,
+ tree.find<void*>(Name("y.d.e.f"), &crbtnode, chain,
+ NULL, NULL));
+ EXPECT_EQ(expected_node, chain.getLastComparedNode());
+ // y < w.y, 2 = # labels of "y."
+ comparisonChecks(chain, -1, 2, NameComparisonResult::SUPERDOMAIN);
+ chain.clear();
+
+ // Partial match, search stopped at a node that share a common ancestor
+ // with the search name in the subtree below the matching node.
+ // (the expected node is the same as the previous case)
+ EXPECT_EQ(RBTree<int>::PARTIALMATCH,
+ tree.find<void*>(Name("z.y.d.e.f"), &crbtnode, chain,
+ NULL, NULL));
+ EXPECT_EQ(expected_node, chain.getLastComparedNode());
+ // z.y > w.y, 2 = # labels of "y."
+ comparisonChecks(chain, 1, 2, NameComparisonResult::COMMONANCESTOR);
+ chain.clear();
+
+ // Search stops in the highest level after following a left branch.
+ EXPECT_EQ(RBTree<int>::EXACTMATCH, tree.find(Name("c"), &expected_node));
+ EXPECT_EQ(RBTree<int>::NOTFOUND,
+ tree.find<void*>(Name("bb"), &crbtnode, chain, NULL, NULL));
+ EXPECT_EQ(expected_node, chain.getLastComparedNode());
+ // bb < c, 1 = # labels of "." (trailing dot)
+ comparisonChecks(chain, -1, 1, NameComparisonResult::COMMONANCESTOR);
+ chain.clear();
+
+ // Search stops in the highest level after following a right branch.
+ // (the expected node is the same as the previous case)
+ EXPECT_EQ(RBTree<int>::NOTFOUND,
+ tree.find<void*>(Name("d"), &crbtnode, chain, NULL, NULL));
+ EXPECT_EQ(expected_node, chain.getLastComparedNode());
+ // d > c, 1 = # labels of "." (trailing dot)
+ comparisonChecks(chain, 1, 1, NameComparisonResult::COMMONANCESTOR);
+ chain.clear();
+}
+
TEST_F(RBTreeTest, dumpTree) {
std::ostringstream str;
std::ostringstream str2;
@@ -336,5 +523,4 @@ TEST_F(RBTreeTest, swap) {
tree2.dumpTree(out);
ASSERT_EQ(str1.str(), out.str());
}
-
}
diff --git a/src/lib/dns/message.cc b/src/lib/dns/message.cc
index b96e86d..c966116 100644
--- a/src/lib/dns/message.cc
+++ b/src/lib/dns/message.cc
@@ -309,6 +309,44 @@ Message::hasRRset(const Section section, const Name& name,
return (false);
}
+bool
+Message::hasRRset(const Section section, const RRsetPtr& rrset) {
+ return (hasRRset(section, rrset->getName(), rrset->getClass(), rrset->getType()));
+}
+
+bool
+Message::removeRRset(const Section section, RRsetIterator& iterator) {
+ if (section >= MessageImpl::NUM_SECTIONS) {
+ isc_throw(OutOfRange, "Invalid message section: " << section);
+ }
+
+ bool removed = false;
+ for (vector<RRsetPtr>::iterator i = impl_->rrsets_[section].begin();
+ i != impl_->rrsets_[section].end(); ++i) {
+ if (((*i)->getName() == (*iterator)->getName()) &&
+ ((*i)->getClass() == (*iterator)->getClass()) &&
+ ((*i)->getType() == (*iterator)->getType())) {
+
+ // Found the matching RRset so remove it & ignore rest
+ impl_->counts_[section] -= (*iterator)->getRdataCount();
+ impl_->rrsets_[section].erase(i);
+ removed = true;
+ break;
+ }
+ }
+
+ return (removed);
+}
+
+void
+Message::clearSection(const Section section) {
+ if (section >= MessageImpl::NUM_SECTIONS) {
+ isc_throw(OutOfRange, "Invalid message section: " << section);
+ }
+ impl_->rrsets_[section].clear();
+ impl_->counts_[section] = 0;
+}
+
void
Message::addQuestion(const QuestionPtr question) {
if (impl_->mode_ != Message::RENDER) {
@@ -739,6 +777,27 @@ Message::clear(Mode mode) {
}
void
+Message::appendSection(const Section section, const Message& source) {
+ if (section >= MessageImpl::NUM_SECTIONS) {
+ isc_throw(OutOfRange, "Invalid message section: " << section);
+ }
+
+ if (section == SECTION_QUESTION) {
+ for (QuestionIterator qi = source.beginQuestion();
+ qi != source.endQuestion();
+ ++qi) {
+ addQuestion(*qi);
+ }
+ } else {
+ for (RRsetIterator rrsi = source.beginSection(section);
+ rrsi != source.endSection(section);
+ ++rrsi) {
+ addRRset(section, *rrsi);
+ }
+ }
+}
+
+void
Message::makeResponse() {
if (impl_->mode_ != Message::PARSE) {
isc_throw(InvalidMessageOperation,
diff --git a/src/lib/dns/message.h b/src/lib/dns/message.h
index 153c7a9..11167d2 100644
--- a/src/lib/dns/message.h
+++ b/src/lib/dns/message.h
@@ -460,9 +460,36 @@ public:
bool hasRRset(const Section section, const Name& name,
const RRClass& rrclass, const RRType& rrtype);
+ /// \brief Determine whether the given section already has an RRset
+ /// matching the one pointed to by the argumet
+ ///
+ /// \c section must be a valid constant of the \c Section type;
+ /// otherwise, an exception of class \c OutOfRange will be thrown.
+ bool hasRRset(const Section section, const RRsetPtr& rrset);
+
+ /// \brief Remove RRSet from Message
+ ///
+ /// Removes the RRset identified by the section iterator from the message.
+ /// Note: if,.for some reason, the RRset is duplicated in the section, only
+ /// one occurrence is removed.
+ ///
+ /// If the operation is successful, all iterators into the section are
+ /// invalidated.
+ ///
+ /// \param section Section to which the iterator belongs
+ /// \param iterator Iterator pointing to the element to be removed
+ ///
+ /// \return true if the element was removed, false if the iterator was not
+ /// found in the specified section.
+ bool removeRRset(const Section section, RRsetIterator& iterator);
+
+ /// \brief Remove all RRSets from the given Section
+ ///
+ /// \param section Section to remove all rrsets from
+ void clearSection(const Section section);
+
// The following methods are not currently implemented.
//void removeQuestion(QuestionPtr question);
- //void removeRRset(const Section section, RRsetPtr rrset);
// notyet:
//void addRR(const Section section, const RR& rr);
//void removeRR(const Section section, const RR& rr);
@@ -471,6 +498,13 @@ public:
/// specified mode.
void clear(Mode mode);
+ /// \brief Adds all rrsets from the source the given section in the
+ /// source message to the same section of this message
+ ///
+ /// \param section the section to append
+ /// \param target The source Message
+ void appendSection(const Section section, const Message& source);
+
/// \brief Prepare for making a response from a request.
///
/// This will clear the DNS header except those fields that should be kept
diff --git a/src/lib/dns/rrset.h b/src/lib/dns/rrset.h
index 4fc6cdc..acb4224 100644
--- a/src/lib/dns/rrset.h
+++ b/src/lib/dns/rrset.h
@@ -229,8 +229,8 @@ public:
/// \brief Updates the owner name of the \c RRset.
///
- /// \param name A reference to a \c RRTTL class object to be copied as the
- /// new TTL.
+ /// \param name A reference to a \c Name class object to be copied as the
+ /// new name.
virtual void setName(const Name& name) = 0;
/// \brief Updates the TTL of the \c RRset.
@@ -588,8 +588,8 @@ public:
/// internal copy of the \c name involves resource allocation and it
/// fails.
///
- /// \param name A reference to a \c RRTTL class object to be copied as the
- /// new TTL.
+ /// \param name A reference to a \c Name class object to be copied as the
+ /// new name.
virtual void setName(const Name& name);
/// \brief Updates the TTL of the \c RRset.
@@ -720,7 +720,7 @@ public:
void removeRRsig() { rrsig_ = RRsetPtr(); }
/// \brief Return a pointer to this RRset's RRSIG RRset
- RRsetPtr getRRsig() { return (rrsig_); }
+ RRsetPtr getRRsig() const { return (rrsig_); }
private:
RRsetPtr rrsig_;
};
diff --git a/src/lib/dns/tests/message_unittest.cc b/src/lib/dns/tests/message_unittest.cc
index 65c5813..92adbc9 100644
--- a/src/lib/dns/tests/message_unittest.cc
+++ b/src/lib/dns/tests/message_unittest.cc
@@ -250,8 +250,122 @@ TEST_F(MessageTest, hasRRset) {
EXPECT_THROW(message_render.hasRRset(bogus_section, test_name,
RRClass::IN(), RRType::A()),
OutOfRange);
+
+ // Repeat the checks having created an RRset of the appropriate type.
+
+ RRsetPtr rrs1(new RRset(test_name, RRClass::IN(), RRType::A(), RRTTL(60)));
+ EXPECT_TRUE(message_render.hasRRset(Message::SECTION_ANSWER, rrs1));
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ADDITIONAL, rrs1));
+
+ RRsetPtr rrs2(new RRset(Name("nomatch.example"), RRClass::IN(), RRType::A(),
+ RRTTL(5)));
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ANSWER, rrs2));
+
+ RRsetPtr rrs3(new RRset(test_name, RRClass::CH(), RRType::A(), RRTTL(60)));
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ANSWER, rrs3));
+
+ RRsetPtr rrs4(new RRset(test_name, RRClass::IN(), RRType::AAAA(), RRTTL(5)));
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ANSWER, rrs4));
+
+ RRsetPtr rrs5(new RRset(test_name, RRClass::IN(), RRType::AAAA(), RRTTL(5)));
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ANSWER, rrs4));
+
+ EXPECT_THROW(message_render.hasRRset(bogus_section, rrs1), OutOfRange);
+}
+
+TEST_F(MessageTest, removeRRset) {
+ message_render.addRRset(Message::SECTION_ANSWER, rrset_a);
+ message_render.addRRset(Message::SECTION_ANSWER, rrset_aaaa);
+ EXPECT_TRUE(message_render.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::A()));
+ EXPECT_TRUE(message_render.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::AAAA()));
+ EXPECT_EQ(3, message_render.getRRCount(Message::SECTION_ANSWER));
+
+ // Locate the AAAA RRset and remove it; this has one RR in it.
+ RRsetIterator i = message_render.beginSection(Message::SECTION_ANSWER);
+ if ((*i)->getType() == RRType::A()) {
+ ++i;
+ }
+ EXPECT_EQ(RRType::AAAA(), (*i)->getType());
+ message_render.removeRRset(Message::SECTION_ANSWER, i);
+
+ EXPECT_TRUE(message_render.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::A()));
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::AAAA()));
+ EXPECT_EQ(2, message_render.getRRCount(Message::SECTION_ANSWER));
+}
+
+TEST_F(MessageTest, clearQuestionSection) {
+ QuestionPtr q(new Question(Name("www.example.com"), RRClass::IN(),
+ RRType::A()));
+ message_render.addQuestion(q);
+ ASSERT_EQ(1, message_render.getRRCount(Message::SECTION_QUESTION));
+
+ message_render.clearSection(Message::SECTION_QUESTION);
+ EXPECT_EQ(0, message_render.getRRCount(Message::SECTION_QUESTION));
+}
+
+
+TEST_F(MessageTest, clearAnswerSection) {
+ // Add two RRsets, check they are present, clear the section,
+ // check if they are gone.
+ message_render.addRRset(Message::SECTION_ANSWER, rrset_a);
+ message_render.addRRset(Message::SECTION_ANSWER, rrset_aaaa);
+ ASSERT_TRUE(message_render.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::A()));
+ ASSERT_TRUE(message_render.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::AAAA()));
+ ASSERT_EQ(3, message_render.getRRCount(Message::SECTION_ANSWER));
+
+ message_render.clearSection(Message::SECTION_ANSWER);
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::A()));
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::AAAA()));
+ EXPECT_EQ(0, message_render.getRRCount(Message::SECTION_ANSWER));
}
+TEST_F(MessageTest, clearAuthoritySection) {
+ // Add two RRsets, check they are present, clear the section,
+ // check if they are gone.
+ message_render.addRRset(Message::SECTION_AUTHORITY, rrset_a);
+ message_render.addRRset(Message::SECTION_AUTHORITY, rrset_aaaa);
+ ASSERT_TRUE(message_render.hasRRset(Message::SECTION_AUTHORITY, test_name,
+ RRClass::IN(), RRType::A()));
+ ASSERT_TRUE(message_render.hasRRset(Message::SECTION_AUTHORITY, test_name,
+ RRClass::IN(), RRType::AAAA()));
+ ASSERT_EQ(3, message_render.getRRCount(Message::SECTION_AUTHORITY));
+
+ message_render.clearSection(Message::SECTION_AUTHORITY);
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_AUTHORITY, test_name,
+ RRClass::IN(), RRType::A()));
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_AUTHORITY, test_name,
+ RRClass::IN(), RRType::AAAA()));
+ EXPECT_EQ(0, message_render.getRRCount(Message::SECTION_AUTHORITY));
+}
+
+TEST_F(MessageTest, clearAdditionalSection) {
+ // Add two RRsets, check they are present, clear the section,
+ // check if they are gone.
+ message_render.addRRset(Message::SECTION_ADDITIONAL, rrset_a);
+ message_render.addRRset(Message::SECTION_ADDITIONAL, rrset_aaaa);
+ ASSERT_TRUE(message_render.hasRRset(Message::SECTION_ADDITIONAL, test_name,
+ RRClass::IN(), RRType::A()));
+ ASSERT_TRUE(message_render.hasRRset(Message::SECTION_ADDITIONAL, test_name,
+ RRClass::IN(), RRType::AAAA()));
+ ASSERT_EQ(3, message_render.getRRCount(Message::SECTION_ADDITIONAL));
+
+ message_render.clearSection(Message::SECTION_ADDITIONAL);
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ADDITIONAL, test_name,
+ RRClass::IN(), RRType::A()));
+ EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ADDITIONAL, test_name,
+ RRClass::IN(), RRType::AAAA()));
+ EXPECT_EQ(0, message_render.getRRCount(Message::SECTION_ADDITIONAL));
+}
+
+
TEST_F(MessageTest, badBeginSection) {
// valid cases are tested via other tests
EXPECT_THROW(message_render.beginSection(Message::SECTION_QUESTION),
@@ -266,6 +380,63 @@ TEST_F(MessageTest, badEndSection) {
EXPECT_THROW(message_render.endSection(bogus_section), OutOfRange);
}
+TEST_F(MessageTest, appendSection) {
+ Message target(Message::RENDER);
+
+ // Section check
+ EXPECT_THROW(target.appendSection(bogus_section, message_render),
+ OutOfRange);
+
+ // Make sure nothing is copied if there is nothing to copy
+ target.appendSection(Message::SECTION_QUESTION, message_render);
+ EXPECT_EQ(0, target.getRRCount(Message::SECTION_QUESTION));
+ target.appendSection(Message::SECTION_ANSWER, message_render);
+ EXPECT_EQ(0, target.getRRCount(Message::SECTION_ANSWER));
+ target.appendSection(Message::SECTION_AUTHORITY, message_render);
+ EXPECT_EQ(0, target.getRRCount(Message::SECTION_AUTHORITY));
+ target.appendSection(Message::SECTION_ADDITIONAL, message_render);
+ EXPECT_EQ(0, target.getRRCount(Message::SECTION_ADDITIONAL));
+
+ // Now add some data, copy again, and see if it got added
+ message_render.addQuestion(Question(Name("test.example.com"),
+ RRClass::IN(), RRType::A()));
+ message_render.addRRset(Message::SECTION_ANSWER, rrset_a);
+ message_render.addRRset(Message::SECTION_AUTHORITY, rrset_a);
+ message_render.addRRset(Message::SECTION_ADDITIONAL, rrset_a);
+ message_render.addRRset(Message::SECTION_ADDITIONAL, rrset_aaaa);
+
+ target.appendSection(Message::SECTION_QUESTION, message_render);
+ EXPECT_EQ(1, target.getRRCount(Message::SECTION_QUESTION));
+
+ target.appendSection(Message::SECTION_ANSWER, message_render);
+ EXPECT_EQ(2, target.getRRCount(Message::SECTION_ANSWER));
+ EXPECT_TRUE(target.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::A()));
+
+ target.appendSection(Message::SECTION_AUTHORITY, message_render);
+ EXPECT_EQ(2, target.getRRCount(Message::SECTION_AUTHORITY));
+ EXPECT_TRUE(target.hasRRset(Message::SECTION_AUTHORITY, test_name,
+ RRClass::IN(), RRType::A()));
+
+ target.appendSection(Message::SECTION_ADDITIONAL, message_render);
+ EXPECT_EQ(3, target.getRRCount(Message::SECTION_ADDITIONAL));
+ EXPECT_TRUE(target.hasRRset(Message::SECTION_ADDITIONAL, test_name,
+ RRClass::IN(), RRType::A()));
+ EXPECT_TRUE(target.hasRRset(Message::SECTION_ADDITIONAL, test_name,
+ RRClass::IN(), RRType::AAAA()));
+
+ // One more test, test to see if the section gets added, not replaced
+ Message source2(Message::RENDER);
+ source2.addRRset(Message::SECTION_ANSWER, rrset_aaaa);
+ target.appendSection(Message::SECTION_ANSWER, source2);
+ EXPECT_EQ(3, target.getRRCount(Message::SECTION_ANSWER));
+ EXPECT_TRUE(target.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::A()));
+ EXPECT_TRUE(target.hasRRset(Message::SECTION_ANSWER, test_name,
+ RRClass::IN(), RRType::AAAA()));
+
+}
+
TEST_F(MessageTest, fromWire) {
factoryFromFile(message_parse, "message_fromWire1");
EXPECT_EQ(0x1035, message_parse.getQid());
diff --git a/src/lib/log/Makefile.am b/src/lib/log/Makefile.am
index 4afb7be..900f11b 100644
--- a/src/lib/log/Makefile.am
+++ b/src/lib/log/Makefile.am
@@ -1,4 +1,43 @@
-AM_CXXFLAGS = $(B10_CXXFLAGS)
+if USE_LOG4CXX
+SUBDIRS = . compiler tests
+endif
+
+AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
+AM_CPPFLAGS += $(BOOST_INCLUDES)
+AM_CPPFLAGS += $(LOG4CXX_INCLUDES)
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/log -I$(top_builddir)/src/lib/log
+
+CLEANFILES = *.gcno *.gcda
lib_LTLIBRARIES = liblog.la
-liblog_la_SOURCES = dummylog.cc dummylog.h
+liblog_la_SOURCES =
+liblog_la_SOURCES += dbglevels.h
+liblog_la_SOURCES += dummylog.h dummylog.cc
+if USE_LOG4CXX
+liblog_la_SOURCES += filename.h filename.cc
+liblog_la_SOURCES += logger.cc logger.h
+liblog_la_SOURCES += logger_support.cc logger_support.h
+liblog_la_SOURCES += messagedef.cc messagedef.h
+liblog_la_SOURCES += message_dictionary.cc message_dictionary.h
+liblog_la_SOURCES += message_exception.h message_exception.cc
+liblog_la_SOURCES += message_initializer.cc message_initializer.h
+liblog_la_SOURCES += message_reader.cc message_reader.h
+liblog_la_SOURCES += message_types.h
+liblog_la_SOURCES += root_logger_name.cc root_logger_name.h
+liblog_la_SOURCES += strutil.h strutil.cc
+liblog_la_SOURCES += xdebuglevel.cc xdebuglevel.h
+
+liblog_la_LDFLAGS = $(LOG4CXX_LDFLAGS)
+endif
+
+# Note: the ordering matters: -Wno-... must follow -Wextra (defined in
+# B10_CXXFLAGS)
+liblog_la_CXXFLAGS = $(AM_CXXFLAGS)
+if USE_GXX
+liblog_la_CXXFLAGS += -Wno-unused-parameter
+endif
+if USE_CLANGPP
+# Same for clang++, but we need to turn off -Werror completely.
+liblog_la_CXXFLAGS += -Wno-error
+endif
+liblog_la_CPPFLAGS = $(AM_CPPFLAGS)
diff --git a/src/lib/log/compiler/Makefile.am b/src/lib/log/compiler/Makefile.am
new file mode 100644
index 0000000..2475036
--- /dev/null
+++ b/src/lib/log/compiler/Makefile.am
@@ -0,0 +1,20 @@
+SUBDIRS = .
+
+AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/log -I$(top_builddir)/src/lib/log
+AM_CPPFLAGS += $(BOOST_INCLUDES)
+
+AM_CXXFLAGS = $(B10_CXXFLAGS)
+
+if USE_STATIC_LINK
+AM_LDFLAGS = -static
+endif
+
+pkglibexecdir = $(libexecdir)/@PACKAGE@
+
+CLEANFILES = *.gcno *.gcda
+
+pkglibexec_PROGRAMS = message
+message_SOURCES = message.cc
+message_LDADD = $(top_builddir)/src/lib/log/liblog.la
+
diff --git a/src/lib/log/compiler/message.cc b/src/lib/log/compiler/message.cc
new file mode 100644
index 0000000..50e33a6
--- /dev/null
+++ b/src/lib/log/compiler/message.cc
@@ -0,0 +1,450 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <cctype>
+#include <fstream>
+#include <iostream>
+#include <string>
+#include <vector>
+
+#include <errno.h>
+#include <getopt.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <log/filename.h>
+#include <log/message_dictionary.h>
+#include <log/message_exception.h>
+#include <log/message_reader.h>
+#include <log/messagedef.h>
+#include <log/strutil.h>
+
+#include <log/logger.h>
+
+using namespace std;
+using namespace isc::log;
+
+static const char* VERSION = "1.0-0";
+
+/// \brief Message Compiler
+///
+/// \b Overview<BR>
+/// This is the program that takes as input a message file and produces:
+///
+/// \li A .h file containing message definition
+/// \li A .cc file containing code that adds the messages to the program's
+/// message disctionary at start-up time.
+///
+/// Alternatively, the program can produce a .py file that contains the
+/// message definitions.
+///
+
+/// \b Invocation<BR>
+/// The program is invoked with the command:
+///
+/// <tt>message [-p] \<message-file\></tt>
+///
+/// It reads the message file and writes out two files of the same name but with
+/// extensions of .h and .cc.
+///
+/// If \c -p is specified, the C++ files are not written; instead a Python file
+/// of the same name (but with the file extension .py) is written.
+
+
+/// \brief Print Version
+///
+/// Prints the program's version number.
+
+static void version() {
+ cout << VERSION << "\n";
+}
+
+/// \brief Print Usage
+///
+/// Prints program usage to stdout.
+
+static void usage() {
+ cout <<
+ "Usage: message [-h] [-p] [-v] <message-file>\n" <<
+ "\n" <<
+ "-h Print this message and exit\n" <<
+ "-p Output a Python module holding the message definitions.\n" <<
+ " By default a C++ header file and implementation file are\n" <<
+
+
+ " written.\n" <<
+ "-v Print the program version and exit\n" <<
+ "\n" <<
+ "<message-file> is the name of the input message file.\n";
+}
+
+
+/// \brief Create Time
+///
+/// Returns the current time as a suitably-formatted string.
+///
+/// \return Current time
+
+static string currentTime() {
+
+ // Get the current time.
+ time_t curtime;
+ time(&curtime);
+
+ // Format it
+ char buffer[32];
+ ctime_r(&curtime, buffer);
+
+ // Convert to string and strip out the trailing newline
+ string current_time = buffer;
+ return isc::strutil::trim(current_time);
+}
+
+
+
+
+/// \brief Create Header Sentinel
+///
+/// Given the name of a file, create an #ifdef sentinel name. The name is
+/// __<name>_<ext>, where <name> is the name of the file, and <ext> is the
+/// extension less the leading period. The sentinel will be upper-case.
+///
+/// \param file Filename object representing the file.
+///
+/// \return Sentinel name
+
+static string sentinel(Filename& file) {
+
+ string name = file.name();
+ string ext = file.extension();
+ string sentinel_text = "__" + name + "_" + ext.substr(1);
+ isc::strutil::uppercase(sentinel_text);
+ return sentinel_text;
+}
+
+
+/// \brief Quote String
+///
+/// Inserts an escape character (a backslash) prior to any double quote
+/// characters. This is used to handle the fact that the input file does not
+/// contain quotes, yet the string will be included in a C++ literal string.
+
+string quoteString(const string& instring) {
+
+ // Create the output string and reserve the space needed to hold the input
+ // string. (Most input strings will not contain quotes, so this single
+ // reservation should be all that is needed.)
+ string outstring;
+ outstring.reserve(instring.size());
+
+ // Iterate through the input string, preceding quotes with a slash.
+ for (size_t i = 0; i < instring.size(); ++i) {
+ if (instring[i] == '"') {
+ outstring += '\\';
+ }
+ outstring += instring[i];
+ }
+
+ return outstring;
+}
+
+
+/// \brief Sorted Identifiers
+///
+/// Given a dictionary, return a vector holding the message IDs in sorted
+/// order.
+///
+/// \param dictionary Dictionary to examine
+///
+/// \return Sorted list of message IDs
+
+vector<MessageID> sortedIdentifiers(MessageDictionary* dictionary) {
+ vector<MessageID> ident;
+
+ for (MessageDictionary::const_iterator i = dictionary->begin();
+ i != dictionary->end(); ++i) {
+ ident.push_back(i->first);
+ }
+ sort(ident.begin(), ident.end());
+
+ return ident;
+}
+
+
+/// \brief Write Header File
+///
+/// Writes the C++ header file containing the symbol definitions.
+///
+/// \param file Name of the message file. The header file is written to a
+/// file of the same name but with a .h suffix.
+/// \param prefix Prefix string to use in symbols
+/// \param dictionary Dictionary holding the message definitions.
+
+void writeHeaderFile(const string& file, const string& prefix,
+ MessageDictionary* dictionary)
+{
+ Filename message_file(file);
+ Filename header_file(message_file.useAsDefault(".h"));
+
+ // Text to use as the sentinels.
+ string sentinel_text = sentinel(header_file);
+
+ // Open the output file for writing
+ ofstream hfile(header_file.fullName().c_str());
+
+ try {
+ if (hfile.fail()) {
+ throw MessageException(MSG_OPENOUT, header_file.fullName(),
+ strerror(errno));
+ }
+
+ // Write the header preamble. If there is an error, we'll pick it up
+ // after the last write.
+
+ hfile <<
+ "// File created from " << message_file.fullName() << " on " <<
+ currentTime() << "\n" <<
+ "\n" <<
+ "#ifndef " << sentinel_text << "\n" <<
+ "#define " << sentinel_text << "\n" <<
+ "\n" <<
+ "#include <log/message_types.h>\n" <<
+ "\n" <<
+ "namespace {\n" <<
+ "\n";
+
+ vector<MessageID> idents = sortedIdentifiers(dictionary);
+ for (vector<MessageID>::const_iterator j = idents.begin();
+ j != idents.end(); ++j) {
+ hfile << "isc::log::MessageID " << prefix << *j <<
+ " = \"" << *j << "\";\n";
+ }
+
+ // ... and finally the postamble
+ hfile <<
+ "\n" <<
+ "} // Anonymous namespace\n" <<
+ "\n" <<
+ "#endif // " << sentinel_text << "\n";
+
+ // Report errors (if any) and exit
+ if (hfile.fail()) {
+ throw MessageException(MSG_WRITERR, header_file.fullName(),
+ strerror(errno));
+ }
+
+ hfile.close();
+ }
+ catch (MessageException&) {
+ hfile.close();
+ throw;
+ }
+}
+
+
+/// \brief Convert Non Alpha-Numeric Characters to Underscores
+///
+/// Simple function for use in a call to transform
+
+char replaceNonAlphaNum(char c) {
+ return (isalnum(c) ? c : '_');
+}
+
+
+/// \brief Write Program File
+///
+/// Writes the C++ source code file. This defines an external objects whose
+/// constructor is run at initialization time. The constructor adds the message
+/// definitions to the main global dictionary.
+
+void writeProgramFile(const string& file, MessageDictionary* dictionary)
+{
+ Filename message_file(file);
+ Filename program_file(message_file.useAsDefault(".cc"));
+
+ // Open the output file for writing
+ ofstream ccfile(program_file.fullName().c_str());
+ try {
+ if (ccfile.fail()) {
+ throw MessageException(MSG_OPENOUT, program_file.fullName(),
+ strerror(errno));
+ }
+
+ // Write the preamble. If there is an error, we'll pick it up after
+ // the last write.
+
+ ccfile <<
+ "// File created from " << message_file.fullName() << " on " <<
+ currentTime() << "\n" <<
+ "\n" <<
+ "#include <cstddef>\n" <<
+ "#include <log/message_initializer.h>\n" <<
+ "\n" <<
+ "using namespace isc::log;\n" <<
+ "\n" <<
+ "namespace {\n" <<
+ "\n" <<
+ "const char* values[] = {\n";
+
+ // Output the identifiers and the associated text.
+ vector<MessageID> idents = sortedIdentifiers(dictionary);
+ for (vector<MessageID>::const_iterator i = idents.begin();
+ i != idents.end(); ++i) {
+ ccfile << " \"" << *i << "\", \"" <<
+ quoteString(dictionary->getText(*i)) << "\",\n";
+ }
+
+ // ... and the postamble
+ ccfile <<
+ " NULL\n" <<
+ "};\n" <<
+ "\n" <<
+ "} // Anonymous namespace\n" <<
+ "\n";
+
+ // Now construct a unique name. We don't put the message initializer as
+ // a static variable or in an anonymous namespace lest the C++
+ // compiler's optimizer decides it can optimise it away.
+ string unique_name = program_file.name() + program_file.extension() +
+ "_" + currentTime();
+ transform(unique_name.begin(), unique_name.end(), unique_name.begin(),
+ replaceNonAlphaNum);
+
+ // ... and write the initialization code
+ ccfile <<
+ "MessageInitializer " << unique_name << "(values);\n";
+
+ // Report errors (if any) and exit
+ if (ccfile.fail()) {
+ throw MessageException(MSG_WRITERR, program_file.fullName(),
+ strerror(errno));
+ }
+
+ ccfile.close();
+ }
+ catch (MessageException&) {
+ ccfile.close();
+ throw;
+ }
+}
+
+
+/// \brief Warn of Duplicate Entries
+///
+/// If the input file contained duplicate message IDs, only the first will be
+/// processed. However, we should warn about it.
+///
+/// \param reader Message Reader used to read the file
+
+static void warnDuplicates(MessageReader& reader) {
+
+ // Get the duplicates (the overflow) and, if present, sort them into some
+ // order and remove those which occur more than once (which mean that they
+ // occur more than twice in the input file).
+ MessageReader::MessageIDCollection duplicates = reader.getNotAdded();
+ if (duplicates.size() > 0) {
+ cout << "Warning: the following duplicate IDs were found:\n";
+
+ sort(duplicates.begin(), duplicates.end());
+ MessageReader::MessageIDCollection::iterator new_end =
+ unique(duplicates.begin(), duplicates.end());
+ for (MessageReader::MessageIDCollection::iterator i = duplicates.begin();
+ i != new_end; ++i) {
+ cout << " " << *i << "\n";
+ }
+ }
+}
+
+
+/// \brief Main Program
+///
+/// Parses the options then dispatches to the appropriate function. See the
+/// main file header for the invocation.
+
+int main(int argc, char** argv) {
+
+ const struct option loptions[] = { // Long options
+ {"help", no_argument, NULL, 'h'},
+ {"version", no_argument, NULL, 'v'},
+ {NULL, 0, NULL, 0 }
+ };
+ const char* soptions = "hv"; // Short options
+
+ optind = 1; // Ensure we start a new scan
+ int opt; // Value of the option
+
+ while ((opt = getopt_long(argc, argv, soptions, loptions, NULL)) != -1) {
+ switch (opt) {
+ case 'h':
+ usage();
+ return 0;
+
+ case 'v':
+ version();
+ return 0;
+
+ default:
+ // A message will have already been output about the error.
+ return 1;
+ }
+ }
+
+ // Do we have the message file?
+ if (optind < (argc - 1)) {
+ cout << "Error: excess arguments in command line\n";
+ usage();
+ return 1;
+ } else if (optind >= argc) {
+ cout << "Error: missing message file\n";
+ usage();
+ return 1;
+ }
+ string message_file = argv[optind];
+
+ try {
+ // Have identified the file, so process it. First create a local
+ // dictionary into which the data will be put.
+ MessageDictionary dictionary;
+
+ // Read the data into it.
+ MessageReader reader(&dictionary);
+ reader.readFile(message_file);
+
+ // Now write the header file.
+ writeHeaderFile(message_file, reader.getPrefix(), &dictionary);
+
+ // ... and the message text file.
+ writeProgramFile(message_file, &dictionary);
+
+ // Finally, warn of any duplicates encountered.
+ warnDuplicates(reader);
+ }
+ catch (MessageException& e) {
+ // Create an error message from the ID and the text
+ MessageDictionary* global = MessageDictionary::globalDictionary();
+ string text = e.id() + ", " + global->getText(e.id());
+
+ // Format with arguments
+ text = isc::strutil::format(text, e.arguments());
+ cerr << text << "\n";
+
+ return 1;
+ }
+
+ return 0;
+
+}
diff --git a/src/lib/log/dbglevels.h b/src/lib/log/dbglevels.h
new file mode 100644
index 0000000..35c6878
--- /dev/null
+++ b/src/lib/log/dbglevels.h
@@ -0,0 +1,31 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __DBGLEVELS_H
+#define __DBGLEVELS_H
+
+/// \brief Defines Debug Levels
+///
+/// Defines the maximum and minimum debug levels and the number of levels.
+/// These are defined using #define as they are referenced in the construction
+/// of variables declared outside execution units. (In this way we avoid the
+/// "static initialization fiasco" problem.)
+
+#define MIN_DEBUG_LEVEL (0)
+#define MAX_DEBUG_LEVEL (99)
+#define NUM_DEBUG_LEVEL (MAX_DEBUG_LEVEL - MIN_DEBUG_LEVEL + 1)
+
+#endif // __DBGLEVELS_H
diff --git a/src/lib/log/documentation.txt b/src/lib/log/documentation.txt
new file mode 100644
index 0000000..1439ad5
--- /dev/null
+++ b/src/lib/log/documentation.txt
@@ -0,0 +1,371 @@
+This directory holds the first release of the logging system.
+
+Basic Ideas
+===========
+The BIND-10 logging system merges two ideas:
+
+* A hierarchical logging system similar to that used in Java (i.e. log4j)
+* Separation of message definitions and text
+
+
+Hierarchical Logging System
+===========================
+When a program writes a message to the logging system, it does so using an
+instance of the Logger class. As well as performing the write of the message,
+the logger identifies the source of the message: different sources can write
+to different destinations and can log different severities of messages. For
+example, the "cache" logger could write messages of DEBUG severity or above
+to a file while all other components write messages of "INFO" severity or above
+to the Syslog file.
+
+The loggers are hierarchical in that each logger is the child of another logger.
+The top of the hierarchy is the root logger, which does not have a parent. The
+point of the hierarchy is that unless a logger is explicitly assigned an
+attribute (such as severity of message being logger), it picks it up from the
+parent. (In BIND-10, there is the root logger (named after the program) and
+every other logger is a child of that.) So in the example above, the
+INFO/Syslog attributes could be associated with the root logger while the
+DEBUG/file attributes are associated with the "cache" logger.
+
+
+Separation of Messages Definitions And Text
+===========================================
+The reason for this is to allow the message text to be overridden by versions
+in a local language. To do this, each message is identified by an identifier
+e.g. "OPENIN". Within the program, this is the symbol passed to the logging
+system. The logger system uses the symbol as an index into a dictionary to
+retrieve the message associated with it (e.g. "unable to open %s for input").
+substitutes any message parameters (in this example, the string that is an
+invalid filename) and logs it to the destination.
+
+In the BIND-10 system, a set of default messages are linked into the program.
+At run-time. each program reads a message file, updating the stored definitions;
+this updated text is logged. However, to aid support, the message identifier
+so in the example above, the message finally logged would be something like:
+
+ OPENIN, unable to open a.txt for input
+
+
+Using The System
+================
+The steps in using the system are:
+
+1. Create a message file. This defines messages by an identification - a
+ mnemonic for the message, typically 6-12 characters long - and a message.
+ The file is described in more detail below.
+
+ Ideally the file should have a file type of ".msg".
+
+2. Run it through the message compiler to produce the .h and .cc files. It
+ is intended that this step be included in the build process. However, for
+ not run the compiler (found in the "compiler" subdirectory) manually. The
+ only argument is the name of the message file: it will produce as output
+ two files, having the same name as the input file but with file types of
+ ".h" and ".cc".
+
+ The compiler is built in the "compiler" subdirectory of the "src/lib/log"
+ directory.
+
+3. Include the .h file in your source code to define message symbols, and
+ make sure that the .cc file is compiled and linked into your program -
+ static initialization will add the symbols to the global dictionary.
+
+4. Declare loggers in your code and use them to log messages. This is described
+ in more detail below.
+
+5. To set the debug level and run-time message file, call runTimeInit (declared
+ in logger_support.h) in the main program unit. This is a temporary solution
+ for Year 2, and will be replaced at a later date, the information coming from
+ the configuration database.
+
+
+Message Files
+=============
+
+File Contents and Format
+------------------------
+A message file is a file containing message definitions. Typically there will
+be one message file for each component that declares message symbols. An
+example file could be:
+
+-- BEGIN --
+
+# Example message file
+# $ID:$
+
+$PREFIX TEST_
+TEST1 message %s is much too large
++ This message is a test for the general message code
+
+UNKNOWN unknown message
++ Issued when the message is unknown.
+
+-- END --
+
+Points to note:
+* Leading and trailing space are trimmed from the line. Although the above
+ exampl,e has every line starting at column 1, the lines could be indented if
+ desired.
+
+* Blank lines are ignored.
+
+* Lines starting with "#" are comments are are ignored. Comments must be on
+ a line by themselves - inline comments will be interpreted as part of the
+ text of the line.
+
+* Lines starting $ are directives. At present, the only directive recognised
+ is $PREFIX, which has one argument: the string used to prefix symbols. If
+ there is no facility directive, there is no prefix to the symbols. (Prefixes
+ are explained below.)
+
+* Lines starting + indicate an explanation for the preceding message. These
+ are intended to be processed by a separate program and used to generate an
+ error messages manual. However they are treated like comments by the message
+ compiler. As with comments, these must be on a line by themselves; if inline,
+ the text (including the leading "+") will be interpreted as part of the line.
+
+* Message lines. These comprise a symbol name and a message, which may
+ include zero or more printf-style tokens. Symbol names will be upper-cased
+ by the compiler.
+
+
+Message Compiler
+----------------
+The message compiler is a program built in the src/log/compiler directory.
+It processes the message file to produce two files:
+
+1) A C++ header file (called <message-file-name>.h) that holds lines of
+the form:
+
+ namespace {
+ isc::log::MessageID PREFIX_IDENTIFIER = "IDENTIFIER";
+ :
+ }
+
+The symbols define the keys in the global message dictionary. At present
+they are defined as std::strings, but a future implementation could redefine
+them as numeric values.
+
+The "PREFIX_" part of the symbol name is the string defined in the $PREFIX
+the argument to the directive. So "$PREFIX MSG_" would prefix the identifer
+ABC with "MSG_" to give the symbol MSG_ABC. Similarly "$PREFIX E" would
+prefix it with "E" to give the symbol EABC. If no $PREFIX is given, no
+prefix appears (so the symbol in this example would be ABC).
+
+
+2) A C++ source file (called <message-file-name>.cc) that holds the code to
+insert the symbols and messages into the map.
+
+This file declares an array of identifiers/messages in the form:
+
+ namespace {
+ const char* values[] = {
+ identifier1, text1,
+ identifier2, text2,
+ :
+ NULL
+ };
+ }
+
+(A more complex structure to group identifiers and their messages could be
+imposed, but as the array is generated by code and will be read by code,
+it is not needed.)
+
+It then declares an object that will add information to the global dictionary:
+
+ MessageInitializer <message-file-name>_<time>(values);
+
+(Declaring the object as "static" or in the anonymous namespace runs the risk
+of it being optimised away when the module is compiled with optimisation.
+But giving it a standard name would cause a clash when multiple files are
+used, hence an attempt at disambiguation.)
+
+The constructor of the MessageInitializer object retrieves the singleton
+global Dictionary object (created using standard methods to avoid the
+"static initialization fiasco") and adds each identifier and text to it.
+A check is made as each is added; if the identifier already exists, it is
+added to "overflow" vector; the vector is printed to the main logging output
+when logging is finally enabled (to indicate a programming error).
+
+
+Using the Logging
+=================
+To use the current version of the logging:
+
+1. Build message header file and source file as describe above.
+
+2. In the main module of the program, declare an instance of the
+ RootLoggerName class to define the name of the program's root logger, e.g.
+
+ #include <log/root_logger_name.h>
+
+ isc::log::RootLoggerName("b10-auth");
+
+ It should be declared outside an execution unit to allow other statically-
+ declared loggers to pick it up.
+
+2. In the code that needs to do logging, declare a logger with a given name,
+ e.g.
+
+ #include <log/logger.h>
+ :
+ isc::log::Logger logger("myname"); // "myname" can be anything
+
+ The above example assumes declaration outside a function. If declaring
+ non-statically within a function, declare it as:
+
+ isc::log::Logger logger("myname", true);
+
+ This is due to an apparent bug in the underlying log4cxx, where the deletion
+ of a statically-declared object at program termination can cause a segment
+ fault. (The destruction of internal memory structures can sometimes happen
+ out of order.) By default the Logger class creates the structures in its
+ constructor but does not delete them in the destruction. The default
+ behavious works because instead of reclaiming memory at program run-down,
+ the operating system reclaims it when the process is deleted.
+
+ Setting the second argument "true" causes the Logger's destructor to delete
+ the log4cxx structures. This does not cause a problem if the program is
+ not terminating. So use the second form when declaring an automatic
+ instance of isc::log::Logger on the stack.
+
+3. The main program unit should include a call to isc::log::runTimeInit()
+ (defined in logger_support.h) to set the logging severity, debug log level,
+ and external message file.
+
+ a) The logging severity is one of the enum defined in logger.h, i.e.
+
+ isc::log::Logger::DEBUG
+ isc::log::Logger::INFO
+ isc::log::Logger::WARN
+ isc::log::Logger::ERROR
+ isc::log::Logger::FATAL
+ isc::log::Logger::NONE
+
+ b) The debug log level is only interpreted when the severity is DEBUG and
+ is an integer raning from 0 to 99. 0 should be used for the highest-level
+ debug messages and 99 for the lowest-level (and typically more verbose)
+ messages.
+
+ c) Name of an external message file. This is the same as a standard message
+ file, although it should not include the $PREFIX directive. (A single
+ $PREFIX directive will be ignored; multiple directives will cause the
+ read of the file to fail with an error.) If a message is replaced, the
+ message should include the same printf-format directives in the same order
+ as the original message.
+
+4. Issue logging calls using methods on logger, e.g.
+
+ logger.error(DPS_NSTIMEOUT, "isc.org");
+
+ (where, in the example above we might have defined the symbol in the message
+ file with something along the lines of:
+
+ $PREFIX DPS_
+ :
+ NSTIMEOUT queries to all nameservers for %s have timed out
+
+ At present, the only logging is to the console.
+
+
+Severity Guidelines
+===================
+When using logging, the question arises, what severity should a message be
+logged at? The following is a suggestion - as always, the decision must be
+made in the context of which the message is logged.
+
+FATAL
+-----
+The program has encountered an error that is so severe that it cannot
+continue (or there is no point in continuing). When a fatal error has been
+logged, the program will usually exit immediately (via a call to abort()) or
+shortly afterwards, after dumping some diagnostic information.
+
+ERROR
+-----
+Something has happened such that the program can continue but the results
+for the current (or future) operations cannot be guaranteed to be correct,
+or the results will be correct but the service is impaired. For example,
+the program started but attempts to open one or more network interfaces failed.
+
+WARN
+----
+An unusual event happened. Although the program will continue working
+normally, the event was sufficiently out of the ordinary to warrant drawings
+attention to it. For example, at program start-up a zone was loaded that
+contained no resource records,
+
+INFO
+----
+A normal but significant event has occurred that should be recorded,
+e.g. the program has started or is just about to terminate, a new zone has
+been created, etc.
+
+DEBUG
+-----
+This severity is only enabled on for debugging purposes. A debug level is
+associated with debug messages, level 0 (the default) being for high-level
+messages and level 99 (the maximum) for the lowest level. How the messages
+are distributed between the levels is up to the developer. So if debugging
+the NSAS (for example), a level 0 message might record the creation of a new
+zone, a level 10 recording a timeout when trying to get a nameserver address,
+but a level 50 would record every query for an address. (And we might add
+level 51 to record every update of the RTT.)
+
+Note that like severities, levels are cumulative; so if level 25 is set as the
+debug level, all debug levels from 0 to 25 will be output. In fact, it is
+probably easier to visualise the debug levels as part of the severity system:
+
+ FATAL High
+ ERROR
+ WARN
+ INFO
+ DEBUG level 0
+ DEBUG level 1
+ :
+ DEBUG level 99 Low
+
+When a particular severity is set, it - and all severities and/or debug
+levels above it - will be logged.
+
+Logging Sources v Logging Severities
+------------------------------------
+When logging events, make a distinction between events related to the server
+and events related to DNS messages received. Caution needs to be exercised
+with the latter as, if the logging is enabled in the normal course of events,
+such logging could be a denoial of service vector. For example, suppose that
+the main authoritiative service logger were to log both zone loading and
+unloading as INFO and a warning message if it received an invalid packet. An
+attacker could make the INFO messages unusable by flooding the server with
+malformed packets.
+
+There are two approaches to get round this:
+
+a) Make the logging of packet-dependent events a DEBUG-severity message.
+DEBUG is not enabled by default, so these events will not be recorded unless
+DEBUG is specifically chosen.
+
+b) Record system-related and packet-related messages via different loggers
+(e.g. in the example given, sever events could be logged using the logger
+"auth" and packet-related events at that level logged using the logger
+"pkt-auth".)
+As the loggers are independent and the severity levels independent, fine-tuning
+of what and what is not recorded can be achieved.
+
+
+Outstanding Issues
+==================
+* Ability to configure system according to configuration database.
+* Update the build procedure to create .cc and .h files from the .msg file
+ during the build process. (Requires that the message compiler is built first.)
+
+
+Notes
+=====
+The message compiler is written in C++ (instead of Python) because it
+contains a component that reads the message file. This component is used
+in both the message compiler and the server; in the server it is used when
+the server starts up (or when triggered by a command) to read in a message
+file to overwrite the internal dictionary. Writing it in C++ means there
+is only one piece of code that does this functionality.
+
diff --git a/src/lib/log/filename.cc b/src/lib/log/filename.cc
new file mode 100644
index 0000000..949ed9f
--- /dev/null
+++ b/src/lib/log/filename.cc
@@ -0,0 +1,140 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <iostream>
+#include <algorithm>
+#include <string>
+
+#include <ctype.h>
+
+#include <log/filename.h>
+#include <log/strutil.h>
+
+using namespace std;
+
+
+namespace isc {
+namespace log {
+
+// Split string into components. Any backslashes are assumed to have
+// been replaced by forward slashes.
+
+void
+Filename::split(const string& full_name, string& directory,
+ string& name, string& extension) const
+{
+ directory = name = extension = "";
+ bool dir_present = false;
+ if (!full_name.empty()) {
+
+ // Find the directory.
+ size_t last_slash = full_name.find_last_of('/');
+ if (last_slash != string::npos) {
+
+ // Found the last slash, so extract directory component and
+ // set where the scan for the last_dot should terminate.
+ directory = full_name.substr(0, last_slash + 1);
+ if (last_slash == full_name.size()) {
+
+ // The entire string was a directory, so exit not and don't
+ // do any more searching.
+ return;
+ }
+
+ // Found a directory so note the fact.
+ dir_present = true;
+ }
+
+ // Now search backwards for the last ".".
+ size_t last_dot = full_name.find_last_of('.');
+ if ((last_dot == string::npos) ||
+ (dir_present && (last_dot < last_slash))) {
+
+ // Last "." either not found or it occurs to the left of the last
+ // slash if a directory was present (so it is part of a directory
+ // name). In this case, the remainder of the string after the slash
+ // is the name part.
+ name = full_name.substr(last_slash + 1);
+ return;
+ }
+
+ // Did find a valid dot, so it and everything to the right is the
+ // extension...
+ extension = full_name.substr(last_dot);
+
+ // ... and the name of the file is everything in between.
+ if ((last_dot - last_slash) > 1) {
+ name = full_name.substr(last_slash + 1, last_dot - last_slash - 1);
+ }
+ }
+
+}
+
+// Expand the stored filename with the default.
+
+string
+Filename::expandWithDefault(const string& defname) const {
+
+ string def_directory("");
+ string def_name("");
+ string def_extension("");
+
+ // Normalize the input string.
+ string copy_defname = isc::strutil::trim(defname);
+#ifdef WIN32
+ isc::strutil::normalizeSlash(copy_defname);
+#endif
+
+ // Split into the components
+ split(copy_defname, def_directory, def_name, def_extension);
+
+ // Now construct the result.
+ string retstring =
+ (directory_.empty() ? def_directory : directory_) +
+ (name_.empty() ? def_name : name_) +
+ (extension_.empty() ? def_extension : extension_);
+ return retstring;
+}
+
+// Use the stored name as default for a given name
+
+string
+Filename::useAsDefault(const string& name) const {
+
+ string name_directory("");
+ string name_name("");
+ string name_extension("");
+
+ // Normalize the input string.
+ string copy_name = isc::strutil::trim(name);
+#ifdef WIN32
+ isc::strutil::normalizeSlash(copy_name);
+#endif
+
+ // Split into the components
+ split(copy_name, name_directory, name_name, name_extension);
+
+ // Now construct the result.
+ string retstring =
+ (name_directory.empty() ? directory_ : name_directory) +
+ (name_name.empty() ? name_ : name_name) +
+ (name_extension.empty() ? extension_ : name_extension);
+ return retstring;
+}
+
+
+} // namespace log
+} // namespace isc
diff --git a/src/lib/log/filename.h b/src/lib/log/filename.h
new file mode 100644
index 0000000..29a9cc8
--- /dev/null
+++ b/src/lib/log/filename.h
@@ -0,0 +1,163 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __FILENAME_H
+#define __FILENAME_H
+
+#include <string>
+
+#include <strutil.h>
+
+namespace isc {
+namespace log {
+
+/// \brief Class to Manipulate Filenames
+///
+/// This is a utility class to manipulate filenames. It repeats some of the
+/// features found in the Boost filename class, but is self-contained so avoids
+/// the need to link in the Boost library.
+///
+/// A Unix-style filename comprises three parts:
+///
+/// Directory - everything up to and including the last "/". If there is no
+/// "/" in the string, there is no directory component. Note that the
+/// requirement of a trailing slash eliminates the ambiguity of whether a
+/// component is a directory or not, e.g. in /alpha/beta", "beta" could be the
+/// name of a directory or is could be a file. The interpretation here is that
+/// "beta" is the name of a file (although that file could be a directory).
+///
+/// Note: Under Windows, the drive letter is considered to be part of the
+/// directory specification. Unless this class becomes more widely-used on
+/// Windows, there is no point in adding redundant code.
+///
+/// Name - everthing from the character after the last "/" up to but not
+/// including the last ".".
+///
+/// Extension - everthing from the right-most "." (after the right-most "/") to
+/// the end of the string. If there is no "." after the last "/", there is
+/// no file extension.
+///
+/// (Note that on Windows, this function will replace all "\" characters
+/// with "/" characters on input strings.)
+///
+/// This class provides functions for extracting the components and for
+/// substituting components.
+
+
+class Filename {
+public:
+
+ /// \brief Constructor
+ Filename(const std::string& name) :
+ full_name_(""), directory_(""), name_(""), extension_("")
+ {
+ setName(name);
+ }
+
+ /// \brief Sets Stored Filename
+ ///
+ /// \param name New name to replaced currently stored name
+ void setName(const std::string& name) {
+ full_name_ = isc::strutil::trim(name);
+#ifdef WIN32
+ isc::strutil::normalizeSlash(full_name_);
+#endif
+ split(full_name_, directory_, name_, extension_);
+ }
+
+ /// \return Stored Filename
+ std::string fullName() const {
+ return full_name_;
+ }
+
+ /// \return Directory of Given File Name
+ std::string directory() const {
+ return directory_;
+ }
+
+ /// \return Name of Given File Name
+ std::string name() const {
+ return name_;
+ }
+
+ /// \return Extension of Given File Name
+ std::string extension() const {
+ return extension_;
+ }
+
+ /// \brief Expand Name with Default
+ ///
+ /// A default file specified is supplied and used to fill in any missing
+ /// fields. For example, if the name stored is "/a/b" and the supplied
+ /// name is "c.d", the result is "/a/b.d": the only field missing from the
+ /// stored name is the extension, which is supplied by the default.
+ /// Another example would be to store "a.b" and to supply a default of
+ /// "/c/d/" - the result is "/c/d/a.b". (Note that if the supplied default
+ /// was "/c/d", the result would be "/c/a.b", even if "/c/d" were actually
+ /// a directory.)
+ ///
+ /// \param defname Default name
+ ///
+ /// \return Name expanded with defname.
+ std::string expandWithDefault(const std::string& defname) const;
+
+ /// \brief Use as Default and Substitute into String
+ ///
+ /// Does essentially the inverse of expand(); that filled in the stored
+ /// name with a default and returned the result. This treats the stored
+ /// name as the default and uses it to fill in a given name. In essence,
+ /// the code:
+ /// \code
+ /// Filename f("/a/b");
+ /// result = f.expandWithdefault("c.d");
+ /// \endcode
+ /// gives as a result "/a/b.d". This is the same as:
+ /// \code
+ /// Filename f("c.d");
+ /// result = f.useAsDefault("/a/b");
+ /// \endcode
+ ///
+ /// \param name Name to expand
+ ///
+ /// \return Name expanded with stored name
+ std::string useAsDefault(const std::string&) const;
+
+private:
+ /// \brief Split Name into Components
+ ///
+ /// Splits the file name into the directory, name and extension parts.
+ /// The name is assumed to have had back slashes replaced by forward
+ /// slashes (if appropriate).
+ ///
+ /// \param full_name Name to split
+ /// \param directory Returned directory part
+ /// \param name Returned name part
+ /// \param extension Returned extension part
+ void split(const std::string& full_name, std::string& directory,
+ std::string& name, std::string& extension) const;
+
+ // Members
+
+ std::string full_name_; ///< Given name
+ std::string directory_; ///< Directory part
+ std::string name_; ///< Name part
+ std::string extension_; ///< Extension part
+};
+
+} // namespace log
+} // namespace isc
+
+#endif // __FILENAME_H
diff --git a/src/lib/log/logger.cc b/src/lib/log/logger.cc
new file mode 100644
index 0000000..494e7bc
--- /dev/null
+++ b/src/lib/log/logger.cc
@@ -0,0 +1,307 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE
+
+// $Id$
+
+#include <iostream>
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <log4cxx/appender.h>
+#include <log4cxx/basicconfigurator.h>
+#include <log4cxx/patternlayout.h>
+#include <log4cxx/consoleappender.h>
+
+#include <log/root_logger_name.h>
+#include <log/logger.h>
+#include <log/message_dictionary.h>
+#include <log/message_types.h>
+#include <log/strutil.h>
+#include <log/xdebuglevel.h>
+
+using namespace std;
+
+namespace isc {
+namespace log {
+
+// Static initializations
+
+bool Logger::init_ = false;
+
+// Destructor. Delete log4cxx stuff if "don't delete" is clear.
+
+Logger::~Logger() {
+ if (exit_delete_) {
+ delete loggerptr_;
+ }
+}
+
+// Initialize logger - create a logger as a child of the root logger. With
+// log4cxx this is assured by naming the logger <parent>.<child>.
+
+void
+Logger::initLogger() {
+
+ // Initialize basic logging if not already done. This is a one-off for
+ // all loggers.
+ if (!init_) {
+
+ // TEMPORARY
+ // Add a suitable console logger to the log4cxx root logger. (This
+ // is the logger at the root of the log4cxx tree, not the BIND-10 root
+ // logger, which is one level down.) The chosen format is:
+ //
+ // YYYY-MM-DD hh:mm:ss.sss [logger] SEVERITY: text
+ //
+ // As noted, this is a temporary hack: it is done here to ensure that
+ // a suitable output and output pattern is set. Future versions of the
+ // software will set this based on configuration data.
+
+ log4cxx::LayoutPtr layout(
+ new log4cxx::PatternLayout(
+ "%d{yyyy-MM-DD HH:mm:ss.SSS} %-5p [%c] %m\n"));
+ log4cxx::AppenderPtr console(
+ new log4cxx::ConsoleAppender(layout));
+ log4cxx::LoggerPtr sys_root_logger = log4cxx::Logger::getRootLogger();
+ sys_root_logger->addAppender(console);
+
+ // Set the default logging to INFO
+ sys_root_logger->setLevel(log4cxx::Level::getInfo());
+
+ // All static stuff initialized
+ init_ = true;
+ }
+
+ // Initialize this logger. Name this as to whether the BIND-10 root logger
+ // name has been set. (If not, this mucks up the hierarchy :-( ).
+ string root_name = RootLoggerName::getName();
+ if (root_name.empty() || (name_ == root_name)) {
+ loggerptr_ = new log4cxx::LoggerPtr(log4cxx::Logger::getLogger(name_));
+ }
+ else {
+ loggerptr_ = new log4cxx::LoggerPtr(
+ log4cxx::Logger::getLogger(root_name + "." + name_)
+ );
+ }
+}
+
+
+// Set the severity for logging. There is a 1:1 mapping between the logging
+// severity and the log4cxx logging levels, apart from DEBUG.
+//
+// In log4cxx, each of the logging levels (DEBUG, INFO, WARN etc.) has a numeric
+// value. The level is set to one of these and any numeric level equal to or
+// above it that is reported. For example INFO has a value of 20000 and ERROR
+// a value of 40000. So if a message of WARN severity (= 30000) is logged, it is
+// not logged when the logger's severity level is ERROR (as 30000 !>= 40000).
+// It is reported if the logger's severity level is set to WARN (as 30000 >=
+/// 30000) or INFO (30000 >= 20000).
+//
+// This gives a simple system for handling different debug levels. The debug
+// level is a number between 0 and 99, with 0 being least verbose and 99 the
+// most. To implement this seamlessly, when DEBUG is set, the numeric value
+// of the logging level is actually set to (DEBUG - debug-level). Similarly
+// messages of level "n" are logged at a logging level of (DEBUG - n). Thus if
+// the logging level is set to DEBUG and the debug level set to 25, the actual
+// level set is 10000 - 25 = 99975.
+//
+// Attempting to log a debug message of level 26 is an attempt to log a message
+// of level 10000 - 26 = 9974. As 9974 !>= 9975, it is not logged. A
+// message of level 25 is, because 9975 >= 9975.
+//
+// The extended set of logging levels is implemented by the XDebugLevel class.
+
+void
+Logger::setSeverity(Severity severity, int dbglevel) {
+ switch (severity) {
+ case NONE:
+ getLogger()->setLevel(log4cxx::Level::getOff());
+ break;
+
+ case FATAL:
+ getLogger()->setLevel(log4cxx::Level::getFatal());
+ break;
+
+ case ERROR:
+ getLogger()->setLevel(log4cxx::Level::getError());
+ break;
+
+ case WARN:
+ getLogger()->setLevel(log4cxx::Level::getWarn());
+ break;
+
+ case INFO:
+ getLogger()->setLevel(log4cxx::Level::getInfo());
+ break;
+
+ case DEBUG:
+ getLogger()->setLevel(
+ log4cxx::XDebugLevel::getExtendedDebug(dbglevel));
+ break;
+
+ // Will get here for DEFAULT or any other value. This disables the
+ // logger's own severity and it defaults to the severity of the parent
+ // logger.
+ default:
+ getLogger()->setLevel(0);
+ }
+}
+
+// Convert between numeric log4cxx logging level and BIND-10 logging severity.
+
+Logger::Severity
+Logger::convertLevel(int value) const {
+
+ // The order is optimised. This is only likely to be called when testing
+ // for writing debug messages, so the check for DEBUG_INT is first.
+ if (value <= log4cxx::Level::DEBUG_INT) {
+ return (DEBUG);
+ } else if (value <= log4cxx::Level::INFO_INT) {
+ return (INFO);
+ } else if (value <= log4cxx::Level::WARN_INT) {
+ return (WARN);
+ } else if (value <= log4cxx::Level::ERROR_INT) {
+ return (ERROR);
+ } else if (value <= log4cxx::Level::FATAL_INT) {
+ return (FATAL);
+ } else {
+ return (NONE);
+ }
+}
+
+
+// Return the logging severity associated with this logger.
+
+Logger::Severity
+Logger::getSeverityCommon(const log4cxx::LoggerPtr& ptrlogger,
+ bool check_parent) const {
+
+ log4cxx::LevelPtr level = ptrlogger->getLevel();
+ if (level == log4cxx::LevelPtr()) {
+
+ // Null level returned, logging should be that of the parent.
+
+ if (check_parent) {
+ log4cxx::LoggerPtr parent = ptrlogger->getParent();
+ if (parent == log4cxx::LoggerPtr()) {
+
+ // No parent, so reached the end of the chain. Return INFO
+ // severity.
+ return (INFO);
+ }
+ else {
+ return getSeverityCommon(parent, check_parent);
+ }
+ }
+ else {
+ return (DEFAULT);
+ }
+ } else {
+ return convertLevel(level->toInt());
+ }
+}
+
+
+// Get the debug level. This returns 0 unless the severity is DEBUG.
+
+int
+Logger::getDebugLevel() {
+
+ log4cxx::LevelPtr level = getLogger()->getLevel();
+ if (level == log4cxx::LevelPtr()) {
+
+ // Null pointer returned, logging should be that of the parent.
+ return (0);
+
+ } else {
+ int severity = level->toInt();
+ if (severity <= log4cxx::Level::DEBUG_INT) {
+ return (log4cxx::Level::DEBUG_INT - severity);
+ }
+ else {
+ return (0);
+ }
+ }
+}
+
+// Log an error message:
+// Common code. Owing to the use of variable arguments, this must be inline
+// (hence the definition of the macro). Also note that it expects that the
+// message buffer "message" is declared in the compilation unit.
+
+#define MESSAGE_SIZE (256)
+
+#define FORMAT_MESSAGE(message) \
+ { \
+ MessageDictionary* global = MessageDictionary::globalDictionary(); \
+ string format = global->getText(ident); \
+ va_list ap; \
+ va_start(ap, ident); \
+ vsnprintf(message, sizeof(message), format.c_str(), ap); \
+ message[sizeof(message) - 1] = '\0'; \
+ va_end(ap); \
+ }
+
+
+// Output methods
+
+void
+Logger::debug(int dbglevel, isc::log::MessageID ident, ...) {
+ if (isDebugEnabled(dbglevel)) {
+ char message[MESSAGE_SIZE];
+ FORMAT_MESSAGE(message);
+ LOG4CXX_DEBUG(getLogger(), ident << ", " << message);
+ }
+}
+
+void
+Logger::info(isc::log::MessageID ident, ...) {
+ if (isInfoEnabled()) {
+ char message[MESSAGE_SIZE];
+ FORMAT_MESSAGE(message);
+ LOG4CXX_INFO(getLogger(), ident << ", " << message);
+ }
+}
+
+void
+Logger::warn(isc::log::MessageID ident, ...) {
+ if (isWarnEnabled()) {
+ char message[MESSAGE_SIZE];
+ FORMAT_MESSAGE(message);
+ LOG4CXX_WARN(getLogger(), ident << ", " << message);
+ }
+}
+
+void
+Logger::error(isc::log::MessageID ident, ...) {
+ if (isErrorEnabled()) {
+ char message[MESSAGE_SIZE];
+ FORMAT_MESSAGE(message);
+ LOG4CXX_ERROR(getLogger(), ident << ", " << message);
+ }
+}
+
+void
+Logger::fatal(isc::log::MessageID ident, ...) {
+ if (isFatalEnabled()) {
+ char message[MESSAGE_SIZE];
+ FORMAT_MESSAGE(message);
+ LOG4CXX_FATAL(getLogger(), ident << ", " << message);
+ }
+}
+
+
+} // namespace log
+} // namespace isc
diff --git a/src/lib/log/logger.h b/src/lib/log/logger.h
new file mode 100644
index 0000000..f26f6d1
--- /dev/null
+++ b/src/lib/log/logger.h
@@ -0,0 +1,327 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __LOGGER_H
+#define __LOGGER_H
+
+#include <cstdlib>
+#include <string>
+#include <boost/lexical_cast.hpp>
+#include <log4cxx/logger.h>
+
+#include <log/dbglevels.h>
+#include <log/message_types.h>
+
+namespace isc {
+namespace log {
+
+class Logger {
+public:
+
+ /// \brief Severity Levels
+ typedef enum {
+ DEFAULT, // Default to logging level of parent
+ DEBUG,
+ INFO,
+ WARN,
+ ERROR,
+ FATAL,
+ NONE // Disable logging
+ } Severity;
+
+ /// \brief Constructor
+ ///
+ /// Creates/attaches to a logger of a specific name.
+ ///
+ /// \param name Name of the logger. If the name is that of the root name,
+ /// this creates an instance of the root logger; otherwise it creates a
+ /// child of the root logger.
+ ///
+ /// \param exit_delete This argument is present to get round a bug in
+ /// log4cxx. If a log4cxx logger is declared outside an execution unit, it
+ /// is not deleted until the program runs down. At that point all such
+ /// objects - including internal log4cxx objects - are deleted. However,
+ /// there seems to be a bug in log4cxx where the way that such objects are
+ /// destroyed causes a MutexException to be thrown (this is described in
+ /// https://issues.apache.org/jira/browse/LOGCXX-322). As this only occurs
+ /// during program rundown, the issue is not serious - it just looks bad to
+ /// have the program crash instead of shut down cleanly.\n
+ /// \n
+ /// The original implementation of the isc::log::Logger had as a member a
+ /// log4cxx logger (actually a LoggerPtr). If the isc:: Logger was declared
+ /// statically, when it was destroyed at the end of the program the internal
+ /// LoggerPtr was destroyed, which triggered the problem. The problem did
+ /// not occur if the isc::log::Logger was created on the stack. To get
+ /// round this, the internal LoggerPtr is now created dynamically. The
+ /// exit_delete argument controls its destruction: if true, it is destroyed
+ /// in the ISC Logger destructor. If false, it is not.\n
+ /// \n
+ /// When creating an isc::log::Logger on the stack, the argument should be
+ /// false (the default); when the Logger is destroyed, all the internal
+ /// log4cxx objects are destroyed. As only the logger (and not the internal
+ /// log4cxx data structures are being destroyed), all is well. However,
+ /// when creating the logger statically, the argument should be false. This
+ /// means that the log4cxx objects are not destroyed at program rundown;
+ /// instead memory is reclaimed and files are closed when the process is
+ /// destroyed, something that does not trigger the bug.
+ Logger(const std::string& name, bool exit_delete = false) :
+ loggerptr_(), name_(name), exit_delete_(exit_delete)
+ {}
+
+
+ /// \brief Destructor
+ virtual ~Logger();
+
+
+ /// \brief Configure Options
+ ///
+ /// TEMPORARY: Pass in the command-line options to set the logging severity
+ /// for the root logger. Future versions of the logger will get this
+ /// information from the configuration database.
+ ///
+ /// \param severity Severity level to log
+ /// \param dbglevel If the severity is DEBUG, this is the debug level.
+ /// This can be in the range 1 to 100 and controls the verbosity. A value
+ /// outside these limits is silently coerced to the nearest boundary.
+ /// \param local_file If provided, the name of a message file to read in and
+ /// supersede one or more of the current messages.
+ static void runTimeInit(Severity severity = INFO, int dbglevel = 1,
+ const char* local_file = NULL);
+
+
+ /// \brief Get Name of Logger
+ ///
+ /// \return The full name of the logger (including the root name)
+ virtual std::string getName() {
+ return getLogger()->getName();
+ }
+
+
+ /// \brief Set Severity Level for Logger
+ ///
+ /// Sets the level at which this logger will log messages. If none is set,
+ /// the level is inherited from the parent.
+ ///
+ /// \param severity Severity level to log
+ /// \param dbglevel If the severity is DEBUG, this is the debug level.
+ /// This can be in the range 1 to 100 and controls the verbosity. A value
+ /// outside these limits is silently coerced to the nearest boundary.
+ virtual void setSeverity(Severity severity, int dbglevel = 1);
+
+
+ /// \brief Get Severity Level for Logger
+ ///
+ /// \return The current logging level of this logger. In most cases though,
+ /// the effective logging level is what is required.
+ virtual Severity getSeverity() {
+ return getSeverityCommon(getLogger(), false);
+ }
+
+ /// \brief Get Effective Severity Level for Logger
+ ///
+ /// \return The effective severity level of the logger. This is the same
+ /// as getSeverity() if the logger has a severity level set, but otherwise
+ /// is the severity of the parent.
+ virtual Severity getEffectiveSeverity() {
+ return getSeverityCommon(getLogger(), true);
+ }
+
+
+ /// \brief Return DEBUG Level
+ ///
+ /// \return Current setting of debug level. This is returned regardless of
+ /// whether the
+ virtual int getDebugLevel();
+
+
+ /// \brief Returns if Debug Message Should Be Output
+ ///
+ /// \param dbglevel Level for which debugging is checked. Debugging is
+ /// enabled only if the logger has DEBUG enabled and if the dbglevel
+ /// checked is less than or equal to the debug level set for the logger.
+ virtual bool
+ isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL) {
+ return (getLogger()->getEffectiveLevel()->toInt() <=
+ (log4cxx::Level::DEBUG_INT - dbglevel));
+ }
+
+
+ /// \brief Is INFO Enabled?
+ virtual bool isInfoEnabled() {
+ return (getLogger()->isInfoEnabled());
+ }
+
+
+ /// \brief Is WARNING Enabled?
+ virtual bool isWarnEnabled() {
+ return (getLogger()->isWarnEnabled());
+ }
+
+
+ /// \brief Is ERROR Enabled?
+ virtual bool isErrorEnabled() {
+ return (getLogger()->isErrorEnabled());
+ }
+
+
+ /// \brief Is FATAL Enabled?
+ virtual bool isFatalEnabled() {
+ return (getLogger()->isFatalEnabled());
+ }
+
+
+ /// \brief Output Debug Message
+ ///
+ /// \param dbglevel Debug level, ranging between 0 and 99. Higher numbers
+ /// are used for more verbose output.
+ /// \param ident Message identification.
+ /// \param ... Optional arguments for the message.
+ void debug(int dbglevel, MessageID ident, ...);
+
+
+ /// \brief Output Informational Message
+ ///
+ /// \param ident Message identification.
+ /// \param ... Optional arguments for the message.
+ void info(MessageID ident, ...);
+
+
+ /// \brief Output Warning Message
+ ///
+ /// \param ident Message identification.
+ /// \param ... Optional arguments for the message.
+ void warn(MessageID ident, ...);
+
+
+ /// \brief Output Error Message
+ ///
+ /// \param ident Message identification.
+ /// \param ... Optional arguments for the message.
+ void error(MessageID ident, ...);
+
+
+ /// \brief Output Fatal Message
+ ///
+ /// \param ident Message identification.
+ /// \param ... Optional arguments for the message.
+ void fatal(MessageID ident, ...);
+
+
+protected:
+
+ /// \brief Equality
+ ///
+ /// Check if two instances of this logger refer to the same stream.
+ /// (This method is principally for testing.)
+ ///
+ /// \return true if the logger objects are instances of the same logger.
+ bool operator==(const Logger& other) const {
+ return (*loggerptr_ == *other.loggerptr_);
+ }
+
+
+ /// \brief Logger Initialized
+ ///
+ /// Check that the logger has been properly initialized. (This method
+ /// is principally for testing.)
+ ///
+ /// \return true if this logger object has been initialized.
+ bool isInitialized() const {
+ return (loggerptr_ != NULL);
+ }
+
+
+ /// \brief Get Severity Level for Logger
+ ///
+ /// This is common code for getSeverity() and getEffectiveSeverity() -
+ /// it returns the severity of the logger; if not set (and the check_parent)
+ /// flag is set, it searches up the parent-child tree until a severity
+ /// level is found and uses that.
+ ///
+ /// \param ptrlogger Pointer to the log4cxx logger to check.
+ /// \param check_parent true to search up the tree, false to return the
+ /// current level.
+ ///
+ /// \return The effective severity level of the logger. This is the same
+ /// as getSeverity() if the logger has a severity level set, but otherwise
+ /// is the severity of the parent.
+ Logger::Severity getSeverityCommon(const log4cxx::LoggerPtr& ptrlogger,
+ bool check_parent) const;
+
+
+ /// \brief Convert Between BIND-10 and log4cxx Logging Levels
+ ///
+ /// Converts between the numeric value of the log4cxx logging level
+ /// and the BIND-10 severity level.
+ ///
+ /// \param value log4cxx numeric logging level
+ ///
+ /// \return BIND-10 logging severity
+ Severity convertLevel(int value) const;
+
+
+ /// \brief Initialize log4cxx Logger
+ ///
+ /// Creates the log4cxx logger used internally. A function is provided for
+ /// this so that the creation does not take place when this Logger object
+ /// is created but when it is used. As the latter occurs in executable
+ /// code but the former can occur during initialization, this order
+ /// guarantees that anything that is statically initialized has completed
+ /// its initialization by the time the logger is used.
+ void initLogger();
+
+
+ /// \brief Return log4cxx Logger
+ ///
+ /// Returns the log4cxx logger, initializing it if not already initialized.
+ ///
+ /// \return Loggerptr object
+ log4cxx::LoggerPtr& getLogger() {
+ if (loggerptr_ == NULL) {
+ initLogger();
+ }
+ return *loggerptr_;
+ }
+
+
+ /// \brief Read Local Message File
+ ///
+ /// Reads a local message file into the global dictionary, replacing any
+ /// definitions there. Any messages found in the local file that do not
+ /// replace ones in the global dictionary are listed.
+ ///
+ /// \param file Local message file to be read.
+ static void readLocalMessageFile(const char* file);
+
+private:
+ // Note that loggerptr_ is a pointer to a LoggerPtr, which is itself a
+ // pointer to the underlying log4cxx logger. This is due to the problems
+ // with memory deletion on program exit, explained in the comments for
+ // the "exit_delete" parameter in this class's constructor.
+
+ log4cxx::LoggerPtr* loggerptr_; ///< Pointer to the underlying logger
+ std::string name_; ///< Name of this logger]
+ bool exit_delete_; ///< Delete loggerptr_ on exit?
+
+ // NOTE - THIS IS A PLACE HOLDER
+ static bool init_; ///< Set true when initialized
+};
+
+} // namespace log
+} // namespace isc
+
+
+#endif // __LOGGER_H
diff --git a/src/lib/log/logger_support.cc b/src/lib/log/logger_support.cc
new file mode 100644
index 0000000..c9ba858
--- /dev/null
+++ b/src/lib/log/logger_support.cc
@@ -0,0 +1,116 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE
+
+// $Id$
+
+
+
+/// \brief Temporary Logger Support
+///
+/// Performs run-time initialization of the logger system. In particular, it
+/// is passed information from the command line and:
+///
+/// a) Sets the severity of the messages being logged (and debug level if
+/// appropriate).
+/// b) Reads in the local message file is one has been supplied.
+///
+/// These functions will be replaced once the code has bneen written to obtain
+/// the logging parameters from the configuration database.
+
+#include <vector>
+
+#include <log/logger.h>
+#include <log/logger_support.h>
+#include <log/messagedef.h>
+#include <log/message_dictionary.h>
+#include <log/message_exception.h>
+#include <log/message_reader.h>
+#include <log/message_types.h>
+#include <log/root_logger_name.h>
+
+namespace isc {
+namespace log {
+
+using namespace std;
+
+// Declare a logger for the logging subsystem
+Logger logger("log");
+
+
+/// \brief Reads Local Message File
+///
+/// Reads the local message file into the global dictionary, overwriting
+/// existing messages. If the file contained any message IDs not in the
+/// dictionary, they are listed in a warning message.
+///
+/// \param file Name of the local message file
+static void
+readLocalMessageFile(const char* file) {
+
+ MessageDictionary* dictionary = MessageDictionary::globalDictionary();
+ MessageReader reader(dictionary);
+ try {
+ reader.readFile(file, MessageReader::REPLACE);
+
+ // File successfully read, list the duplicates
+ MessageReader::MessageIDCollection unknown = reader.getNotAdded();
+ for (MessageReader::MessageIDCollection::const_iterator
+ i = unknown.begin(); i != unknown.end(); ++i) {
+ logger.warn(MSG_IDNOTFND, (*i).c_str());
+ }
+ }
+ catch (MessageException& e) {
+ MessageID ident = e.id();
+ vector<MessageID> args = e.arguments();
+ switch (args.size()) {
+ case 0:
+ logger.error(ident);
+ break;
+
+ case 1:
+ logger.error(ident, args[0].c_str());
+ break;
+
+ default: // 2 or more (2 should be the maximum)
+ logger.error(ident, args[0].c_str(), args[1].c_str());
+ }
+ }
+}
+
+/// Logger Run-Time Initialization
+
+void
+runTimeInit(Logger::Severity severity, int dbglevel, const char* file) {
+
+ // Create the application root logger. This is the logger that has the
+ // name of the application (and is one level down from the log4cxx root
+ // logger). All other loggers created in this application will be its
+ // child.
+ //
+ // The main purpose of the application root logger is to provide the root
+ // name in output message for all other loggers.
+ Logger logger(RootLoggerName::getName());
+
+ // Set the severity associated with it. If no other logger has a severity,
+ // this will be the default.
+ logger.setSeverity(severity, dbglevel);
+
+ // Replace any messages with local ones (if given)
+ if (file) {
+ readLocalMessageFile(file);
+ }
+}
+
+} // namespace log
+} // namespace isc
diff --git a/src/lib/log/logger_support.h b/src/lib/log/logger_support.h
new file mode 100644
index 0000000..85f838f
--- /dev/null
+++ b/src/lib/log/logger_support.h
@@ -0,0 +1,43 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __LOGGER_SUPPORT_H
+#define __LOGGER_SUPPORT_H
+
+#include <log/logger.h>
+
+namespace isc {
+namespace log {
+
+/// \brief Run-Time Initialization
+///
+/// This code will be used until the logger is fully integrated into the BIND-10
+/// configuration database. It performs run-time initialization of th logger,
+/// in particular supplying run-time choices to it:
+///
+/// * The severity (and if applicable, debug level) at which to log
+/// * Name of a local message file, containing localisation of message text.
+///
+/// \param severity Severity at which to log
+/// \param dbglevel Debug severiy (ignored if "severity" is not "DEBUG")
+/// \param file Name of the local message file.
+void runTimeInit(Logger::Severity severity, int dbglevel, const char* file);
+
+} // namespace log
+} // namespace isc
+
+
+#endif // __LOGGER_SUPPORT_H
diff --git a/src/lib/log/message_dictionary.cc b/src/lib/log/message_dictionary.cc
new file mode 100644
index 0000000..05c79f8
--- /dev/null
+++ b/src/lib/log/message_dictionary.cc
@@ -0,0 +1,116 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <cstddef>
+#include <log/message_dictionary.h>
+#include <log/message_types.h>
+
+using namespace std;
+
+namespace isc {
+namespace log {
+
+// (Virtual) Destructor
+
+MessageDictionary::~MessageDictionary() {
+}
+
+// Add message and note if ID already exists
+
+bool
+MessageDictionary::add(const MessageID& ident, const std::string& text) {
+ map<MessageID, string>::iterator i = dictionary_.find(ident);
+ bool not_found = (i == dictionary_.end());
+ if (not_found) {
+
+ // Message not already in the dictionary, so add it.
+ dictionary_[ident] = text;
+ }
+
+ return (not_found);
+}
+
+// Add message and note if ID does not already exist
+
+bool
+MessageDictionary::replace(const MessageID& ident, const std::string& text) {
+ map<MessageID, string>::iterator i = dictionary_.find(ident);
+ bool found = (i != dictionary_.end());
+ if (found) {
+
+ // Exists, so replace it.
+ dictionary_[ident] = text;
+ }
+
+ return (found);
+}
+
+// Load a set of messages
+
+vector<MessageID>
+MessageDictionary::load(const char* messages[]) {
+ vector<MessageID> duplicates;
+ int i = 0;
+ while (messages[i]) {
+
+ // ID present, so note it and point to text.
+ MessageID ident(messages[i++]);
+ if (messages[i]) {
+
+ // Text not null, note it and point to next ident.
+ string text(messages[i++]);
+
+ // Add ID and text to message dictionary, noting if the ID was
+ // already present.
+ bool added = add(ident, text);
+ if (!added) {
+ duplicates.push_back(ident);
+ }
+ }
+ }
+ return duplicates;
+}
+
+// Return message text or blank string
+
+string
+MessageDictionary::getText(const MessageID& ident) const {
+ map<MessageID, string>::const_iterator i = dictionary_.find(ident);
+ if (i == dictionary_.end()) {
+ return string("");
+ }
+ else {
+ return i->second;
+ }
+}
+
+// Return global dictionary
+
+MessageDictionary*
+MessageDictionary::globalDictionary() {
+ static MessageDictionary* global = NULL;
+
+ if (global == NULL) {
+ global = new MessageDictionary();
+ }
+ return global;
+}
+
+
+
+
+} // namspace log
+} // namespace isc
diff --git a/src/lib/log/message_dictionary.h b/src/lib/log/message_dictionary.h
new file mode 100644
index 0000000..0b2e704
--- /dev/null
+++ b/src/lib/log/message_dictionary.h
@@ -0,0 +1,150 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __MESSAGE_DICTIONARY_H
+#define __MESSAGE_DICTIONARY_H
+
+#include <cstddef>
+#include <string>
+#include <map>
+#include <vector>
+
+#include <log/message_types.h>
+
+namespace isc {
+namespace log {
+
+/// \brief Message Dictionary
+///
+/// The message dictionary is a wrapper around a std::map object, and allows
+/// message text to be retrieved given the string identification.
+///
+/// Adding text occurs in two modes:
+///
+/// Through the "Add" method, ID/text mappings are added to the dictionary
+/// unless the ID already exists. This is designed for use during program
+/// initialization, where a local message may supplant a compiled-in message.
+///
+/// Through the "Replace" method, ID/text mappings are added to the dictionary
+/// only if the ID already exists. This is for use when a message file is
+/// supplied to replace messages provided with the program.
+///
+/// Although the class can be used stand-alone, it does supply a static method
+/// to return a particular instance - the "global" dictionary.
+
+class MessageDictionary {
+public:
+
+ // Default constructor and assignment operator are OK for this class
+
+ /// \brief Virtual Destructor
+ virtual ~MessageDictionary();
+
+ /// \brief Add Message
+ ///
+ /// Adds a message to the dictionary. If the ID already exists, the ID is
+ /// added to the overflow vector.
+ ///
+ /// \param ident Identification of the message to add
+ /// \param text Message text
+ ///
+ /// \return true if the message was added to the dictionary, false if the
+ /// message existed and it was not added.
+ virtual bool add(const MessageID& ident, const std::string& text);
+
+
+ /// \brief Replace Message
+ ///
+ /// Replaces a message in the dictionary. If the ID does not exist, it is
+ /// added to the overflow vector.
+ ///
+ /// \param ident Identification of the message to replace
+ /// \param text Message text
+ ///
+ /// \return true if the message was added to the dictionary, false if the
+ /// message did not exist and it was not added.
+ virtual bool replace(const MessageID& ident, const std::string& text);
+
+
+ /// \brief Load Dictionary
+ ///
+ /// Designed to be used during the initialization of programs, this
+ /// accepts a set of (ID, text) pairs as a one-dimensional array of
+ /// const char* and adds them to the dictionary. The messages are added
+ /// using "Add".
+ ///
+ /// \param data null-terminated array of const char* alternating ID and
+ /// message text. This should be an odd number of elements long, the last
+ /// elemnent being NULL. If it is an even number of elements long, the
+ /// last ID is ignored.
+ ///
+ /// \return Vector of message IDs that were not loaded because an ID of the
+ /// same name already existing in the dictionary. This vector may be
+ /// empty.
+ virtual std::vector<MessageID> load(const char* elements[]);
+
+
+ /// \brief Get Message Text
+ ///
+ /// Given an ID, retrieve associated message text.
+ ///
+ /// \param ident Message identification
+ ///
+ /// \return Text associated with message or empty string if the ID is not
+ /// recognised. (Note: this precludes an ID being associated with an empty
+ /// string.)
+ virtual std::string getText(const MessageID& ident) const;
+
+
+ /// \brief Number of Items in Dictionary
+ ///
+ /// \return Number of items in the dictionary
+ virtual size_t size() const {
+ return dictionary_.size();
+ }
+
+
+ // Allow access to the internal map structure, but don't allow alteration.
+ typedef std::map<MessageID, std::string>::const_iterator const_iterator;
+
+
+ /// \brief Return begin() iterator of internal map
+ const_iterator begin() const {
+ return dictionary_.begin();
+ }
+
+
+ /// \brief Return end() iterator of internal map
+ const_iterator end() const {
+ return dictionary_.end();
+ }
+
+
+ /// \brief Return Global Dictionary
+ ///
+ /// Returns a pointer to the singleton global dictionary.
+ ///
+ /// \return Pointer to global dictionary.
+ static MessageDictionary* globalDictionary();
+
+private:
+ std::map<MessageID, std::string> dictionary_;
+};
+
+} // namespace log
+} // namespace isc
+
+#endif // __MESSAGE_DICTIONARY_H
diff --git a/src/lib/log/message_exception.cc b/src/lib/log/message_exception.cc
new file mode 100644
index 0000000..562c381
--- /dev/null
+++ b/src/lib/log/message_exception.cc
@@ -0,0 +1,28 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+/// \brief Body of Virtual Destructor
+
+#include <log/message_exception.h>
+
+namespace isc {
+namespace log {
+
+MessageException::~MessageException() throw() {
+}
+
+} // namespace log
+} // namespace isc
diff --git a/src/lib/log/message_exception.h b/src/lib/log/message_exception.h
new file mode 100644
index 0000000..537392d
--- /dev/null
+++ b/src/lib/log/message_exception.h
@@ -0,0 +1,90 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __MESSAGE_EXCEPTION_H
+#define __MESSAGE_EXCEPTION_H
+
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+#include <log/message_types.h>
+
+namespace isc {
+namespace log {
+
+/// \brief Message Exception
+///
+/// Used in the message reader, this simple exception class allows a message
+/// code and its arguments to be encapsulated in an exception and thrown
+/// up the stack.
+
+class MessageException : public std::exception {
+public:
+
+ /// \brief Constructor
+ ///
+ /// \param id Message identification
+ MessageException(MessageID id) : id_(id)
+ {}
+
+ /// \brief Constructor
+ ///
+ /// \param id Message identification
+ /// \param arg1 First message argument
+ MessageException(MessageID id, const std::string& arg1) : id_(id)
+ {
+ args_.push_back(arg1);
+ }
+
+ /// \brief Constructor
+ ///
+ /// \param id Message identification
+ /// \param arg1 First message argument
+ /// \param arg2 Second message argument
+ MessageException(MessageID id, const std::string& arg1,
+ const std::string& arg2) : id_(id)
+ {
+ args_.push_back(arg1);
+ args_.push_back(arg2);
+ }
+
+ /// \brief Destructor
+ virtual ~MessageException() throw();
+
+ /// \brief Return Message ID
+ ///
+ /// \return Message identification
+ MessageID id() const {
+ return id_;
+ }
+
+ /// \brief Return Arguments
+ ///
+ /// \return Exception Arguments
+ std::vector<std::string> arguments() const {
+ return args_;
+ }
+
+private:
+ MessageID id_; // Exception ID
+ std::vector<std::string> args_; // Exception arguments
+};
+
+} // namespace log
+} // namespace isc
+
+#endif // __MESSAGE_EXCEPTION_H
diff --git a/src/lib/log/message_initializer.cc b/src/lib/log/message_initializer.cc
new file mode 100644
index 0000000..914ed17
--- /dev/null
+++ b/src/lib/log/message_initializer.cc
@@ -0,0 +1,32 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <log/message_dictionary.h>
+#include <log/message_initializer.h>
+
+namespace isc {
+namespace log {
+
+// Constructor. Just retrieve the global dictionary and load the IDs and
+// associated text into it.
+
+MessageInitializer::MessageInitializer(const char* values[]) {
+ MessageDictionary* global = MessageDictionary::globalDictionary();
+ global->load(values);
+}
+
+} // namespace log
+} // namespace isc
diff --git a/src/lib/log/message_initializer.h b/src/lib/log/message_initializer.h
new file mode 100644
index 0000000..a776a02
--- /dev/null
+++ b/src/lib/log/message_initializer.h
@@ -0,0 +1,63 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __MESSAGEINITIALIZER_H
+#define __MESSAGEINITIALIZER_H
+
+#include <log/message_dictionary.h>
+
+namespace isc {
+namespace log {
+
+/// \brief Initialize Message Dictionary
+///
+/// This is a helper class to add a set of message IDs and associated text to
+/// the global dictionary.
+///
+/// It should be declared outside an execution unit and initialized with a
+/// an array of values, alternating identifier, associated text and ending with
+/// a NULL, e.g.
+///
+/// static const char* values[] = {
+/// "IDENT1", "message for ident 1",
+/// "IDENT2", "message for ident 2",
+/// :
+/// NULL
+/// };
+/// MessageDictionaryHelper xyz(values);
+///
+/// This will automatically add the message ID/text pairs to the global
+/// dictionary during initialization - all that is required is that the module
+/// containing the definition is included into the final executable.
+///
+/// Messages are added via the MessageDictionary::add() method, so any
+/// duplicates are stored in the the global dictionary's overflow vector whence
+/// they can be retrieved at run-time.
+
+class MessageInitializer {
+public:
+
+ /// \brief Constructor
+ ///
+ /// The only method in the class, this adds the array of values to the
+ /// global dictionary.
+ MessageInitializer(const char* values[]);
+};
+
+} // namespace log
+} // namespace isc
+
+#endif // __MESSAGEINITIALIZER_H
diff --git a/src/lib/log/message_reader.cc b/src/lib/log/message_reader.cc
new file mode 100644
index 0000000..203b836
--- /dev/null
+++ b/src/lib/log/message_reader.cc
@@ -0,0 +1,184 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <errno.h>
+#include <string.h>
+
+#include <iostream>
+#include <fstream>
+
+#include <log/message_exception.h>
+#include <log/messagedef.h>
+#include <log/message_reader.h>
+#include <log/strutil.h>
+
+using namespace std;
+
+namespace isc {
+namespace log {
+
+// Virtual destructor.
+MessageReader::~MessageReader() {
+}
+
+
+// Read the file.
+
+void
+MessageReader::readFile(const string& file, MessageReader::Mode mode) {
+
+ // Ensure the non-added collection is empty: this object might be
+ // being reused.
+ not_added_.clear();
+
+ // Open the file
+ ifstream infile(file.c_str());
+ if (infile.fail()) {
+ throw MessageException(MSG_OPENIN, file, strerror(errno));
+ }
+
+ // Loop round reading it.
+ string line;
+ getline(infile, line);
+ while (infile.good()) {
+ processLine(line, mode);
+ getline(infile, line);
+ }
+
+ // Why did the loop terminate?
+ if (!infile.eof()) {
+ throw MessageException(MSG_READERR, file, strerror(errno));
+ }
+ infile.close();
+}
+
+// Parse a line of the file
+
+void
+MessageReader::processLine(const string& line, MessageReader::Mode mode) {
+
+ // Get rid of leading and trailing spaces
+ string text = isc::strutil::trim(line);
+
+ if (text.empty()) {
+ ; // Ignore blank lines
+
+ } else if ((text[0] == '#') || (text[0] == '+')) {
+ ; // Ignore comments or descriptions
+
+ } else if (text[0] == '$') {
+ parseDirective(text); // Process directives
+
+ } else {
+ parseMessage(text, mode); // Process other lines
+
+ }
+}
+
+// Process directive
+
+void
+MessageReader::parseDirective(const std::string& text) {
+
+ static string valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
+
+ // Regardless of what happens, all prefixes will be uppercase (as will
+ // all symbols).
+ string line = text;
+ isc::strutil::uppercase(line);
+ vector<string> tokens = isc::strutil::tokens(line);
+
+ // Only $PREFIX is recognised so far, so we'll handle it here.
+ if (tokens[0] != string("$PREFIX")) {
+ throw MessageException(MSG_UNRECDIR, tokens[0]);
+
+ } else if (tokens.size() < 2) {
+ throw MessageException(MSG_PRFNOARG);
+
+ } else if (tokens.size() > 2) {
+ throw MessageException(MSG_PRFEXTRARG);
+
+ }
+
+ // Token is potentially valid providing it only contains alphabetic
+ // and numeric characters (and underscores) and does not start with a
+ // digit.
+
+ if ((tokens[1].find_first_not_of(valid) != string::npos) ||
+ (std::isdigit(tokens[1][0]))) {
+
+ // Invalid character in string or it starts with a digit.
+ throw MessageException(MSG_PRFINVARG, tokens[1]);
+ }
+
+ // All OK - unless the prefix has already been set.
+
+ if (prefix_.size() != 0) {
+ throw MessageException(MSG_DUPLPRFX);
+ }
+
+ // Prefix has not been set, so set it and return success.
+
+ prefix_ = tokens[1];
+}
+
+// Process message. By the time this method is called, the line has been
+// stripped of leading and trailing spaces, and we believe that it is a line
+// defining a message. The first token on the line is convered to uppercase
+// and becomes the message ID; the rest of the line is the message text.
+
+void
+MessageReader::parseMessage(const std::string& text, MessageReader::Mode mode) {
+
+ static string delimiters("\t\n "); // Delimiters
+
+ // Look for the first delimiter.
+ size_t first_delim = text.find_first_of(delimiters);
+ if (first_delim == string::npos) {
+
+ // Just a single token in the line - this is not valid
+ throw MessageException(MSG_ONETOKEN, text);
+ }
+
+ // Extract the first token into the message ID
+ MessageID ident = text.substr(0, first_delim);
+
+ // Locate the start of the message text
+ size_t first_text = text.find_first_not_of(delimiters, first_delim);
+ if (first_text == string::npos) {
+
+ // ?? This happens if there are trailing delimiters, which should not
+ // occur as we have stripped trailing spaces off the line. Just treat
+ // this as a single-token error for simplicity's sake.
+ throw MessageException(MSG_ONETOKEN, text);
+ }
+
+ // Add the result to the dictionary and to the non-added list if the add to
+ // the dictionary fails.
+ bool added;
+ if (mode == ADD) {
+ added = dictionary_->add(ident, text.substr(first_text));
+ }
+ else {
+ added = dictionary_->replace(ident, text.substr(first_text));
+ }
+ if (!added) {
+ not_added_.push_back(ident);
+ }
+}
+
+} // namespace log
+} // namespace isc
diff --git a/src/lib/log/message_reader.h b/src/lib/log/message_reader.h
new file mode 100644
index 0000000..84ffce9
--- /dev/null
+++ b/src/lib/log/message_reader.h
@@ -0,0 +1,175 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __MESSAGE_READER_H
+#define __MESSAGE_READER_H
+
+#include <iostream>
+#include <map>
+#include <string>
+#include <vector>
+
+#include <log/message_dictionary.h>
+#include <log/message_types.h>
+
+namespace isc {
+namespace log {
+
+/// \brief Read Message File
+///
+/// Reads a message file and creates a map of identifier against the text of the
+/// message. This map can be retrieved for subsequent processing.
+
+class MessageReader {
+public:
+
+ /// \brief Read Mode
+ ///
+ /// If ADD, messages are added to the dictionary if the ID does not exist
+ /// there. If it does, the ID is added to the dictionary's overflow
+ /// vector.
+ ///
+ /// If REPLACE, the dictionary is only modified if the message ID already
+ /// exists in it. New message IDs are added to the overflow vector.
+ typedef enum {
+ ADD,
+ REPLACE
+ } Mode;
+
+ /// \brief Visible collection types
+ typedef std::vector<MessageID> MessageIDCollection;
+
+ /// \brief Constructor
+ ///
+ /// Default constructor. All work is done in the main readFile code (so
+ /// that a status return can be returned instead of needing to throw an
+ /// exception).
+ ///
+ /// \param dictionary Dictionary to which messages read read from the file
+ /// are added. (This should be a local dictionary when the class is used in
+ /// the message compiler, and the global dictionary when used in a server.
+ /// The ownership of the dictionary object is not transferred - the caller
+ /// is responsible for managing the lifetime of the dictionary.
+ MessageReader(MessageDictionary* dictionary = NULL) :
+ dictionary_(dictionary)
+ {}
+
+
+ /// \brief Virtual Destructor
+ virtual ~MessageReader();
+
+
+ /// \brief Get Dictionary
+ ///
+ /// Returns the pointer to the dictionary object. Note that ownership is
+ /// not transferred - the caller should not delete it.
+ ///
+ /// \return Pointer to current dictionary object
+ MessageDictionary* getDictionary() const {
+ return dictionary_;
+ }
+
+
+ /// \brief Set Dictionary
+ ///
+ /// Sets the current dictionary object.
+ ///
+ /// \param dictionary New dictionary object. The ownership of the dictionary
+ /// object is not transferred - the caller is responsible for managing the
+ /// lifetime of the dictionary.
+ void setDictionary(MessageDictionary* dictionary) {
+ dictionary_ = dictionary;
+ }
+
+
+ /// \brief Read File
+ ///
+ /// This is the main method of the class and reads in the file, parses it,
+ /// and stores the result in the message dictionary.
+ ///
+ /// \param file Name of the message file.
+ /// \param mode Addition mode. See the description of the "Mode" enum.
+ virtual void readFile(const std::string& file, Mode mode = ADD);
+
+
+ /// \brief Process Line
+ ///
+ /// Parses a text line and adds it to the message map. Although this is
+ /// for use in readFile, it can also be used to add individual messages
+ /// to the message map.
+ ///
+ /// \param line Line of text to process
+ /// \param mode If a message line, how to add the message to the dictionary.
+ virtual void processLine(const std::string& line, Mode mode = ADD);
+
+
+ /// \brief Get Prefix
+ ///
+ /// \return Argument to the $PREFIX directive (if present)
+ virtual std::string getPrefix() const {
+ return prefix_;
+ }
+
+
+ /// \brief Clear Prefix
+ ///
+ /// Clears the current prefix.
+ virtual void clearPrefix() {
+ prefix_ = "";
+ }
+
+
+ /// \brief Get Not-Added List
+ ///
+ /// Returns the list of IDs that were not added during the last
+ /// read of the file.
+ ///
+ /// \return Collection of messages not added
+ MessageIDCollection getNotAdded() const {
+ return not_added_;
+ }
+
+private:
+
+ /// \brief Handle a Message Definition
+ ///
+ /// Passed a line that should contain a message, this processes that line
+ /// and adds it to the dictionary according to the mode setting.
+ ///
+ /// \param line Line of text
+ /// \param ADD or REPLACE depending on how the reader is operating. (See
+ /// the description of the Mode typedef for details.)
+ void parseMessage(const std::string& line, Mode mode);
+
+
+ /// \brief Handle Directive
+ ///
+ /// Passed a line starting with a "$", this handles the processing of
+ /// directives.
+ ///
+ /// \param line Line of text that starts with "$",
+ void parseDirective(const std::string& line);
+
+ /// Attributes
+ MessageDictionary* dictionary_; ///< Dictionary to add messages to
+ MessageIDCollection not_added_; ///< List of IDs not added
+ std::string prefix_; ///< Input of $PREFIX statement
+};
+
+} // namespace log
+} // namespace isc
+
+#endif // __MESSAGE_READER_H
diff --git a/src/lib/log/message_types.h b/src/lib/log/message_types.h
new file mode 100644
index 0000000..b101401
--- /dev/null
+++ b/src/lib/log/message_types.h
@@ -0,0 +1,32 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __MESSAGE_TYPES_H
+#define __MESSAGE_TYPES_H
+
+#include <string>
+
+namespace isc {
+namespace log {
+
+typedef std::string MessageID;
+
+} // namespace log
+} // namespace isc
+
+
+
+#endif // __MESSAGE_TYPES_H
diff --git a/src/lib/log/messagedef.cc b/src/lib/log/messagedef.cc
new file mode 100644
index 0000000..7dfa4f6
--- /dev/null
+++ b/src/lib/log/messagedef.cc
@@ -0,0 +1,27 @@
+// File created from messagedef.mes on Mon Jan 17 15:25:32 2011
+
+#include <cstddef>
+#include <log/message_initializer.h>
+
+using namespace isc::log;
+
+namespace {
+
+const char* values[] = {
+ "DUPLPRFX", "duplicate $PREFIX directive found",
+ "IDNOTFND", "could not replace message for '%s': no such message identification",
+ "ONETOKEN", "a line containing a message ID ('%s') and nothing else was found",
+ "OPENIN", "unable to open message file %s for input: %s",
+ "OPENOUT", "unable to open %s for output: %s",
+ "PRFEXTRARG", "$PREFIX directive has too many arguments",
+ "PRFINVARG", "$PREFIX directive has an invalid argument ('%s')",
+ "PRFNOARG", "no arguments were given to the $PREFIX directive",
+ "READERR", "error reading from %s: %s",
+ "UNRECDIR", "unrecognised directive '%s'",
+ "WRITERR", "error writing to %s: %s",
+ NULL
+};
+
+} // Anonymous namespace
+
+MessageInitializer messagedef_cc_Mon_Jan_17_15_25_32_2011(values);
diff --git a/src/lib/log/messagedef.h b/src/lib/log/messagedef.h
new file mode 100644
index 0000000..ae0a99d
--- /dev/null
+++ b/src/lib/log/messagedef.h
@@ -0,0 +1,24 @@
+// File created from messagedef.mes on Mon Jan 17 15:25:32 2011
+
+#ifndef __MESSAGEDEF_H
+#define __MESSAGEDEF_H
+
+#include <log/message_types.h>
+
+namespace {
+
+isc::log::MessageID MSG_DUPLPRFX = "DUPLPRFX";
+isc::log::MessageID MSG_IDNOTFND = "IDNOTFND";
+isc::log::MessageID MSG_ONETOKEN = "ONETOKEN";
+isc::log::MessageID MSG_OPENIN = "OPENIN";
+isc::log::MessageID MSG_OPENOUT = "OPENOUT";
+isc::log::MessageID MSG_PRFEXTRARG = "PRFEXTRARG";
+isc::log::MessageID MSG_PRFINVARG = "PRFINVARG";
+isc::log::MessageID MSG_PRFNOARG = "PRFNOARG";
+isc::log::MessageID MSG_READERR = "READERR";
+isc::log::MessageID MSG_UNRECDIR = "UNRECDIR";
+isc::log::MessageID MSG_WRITERR = "WRITERR";
+
+} // Anonymous namespace
+
+#endif // __MESSAGEDEF_H
diff --git a/src/lib/log/messagedef.mes b/src/lib/log/messagedef.mes
new file mode 100644
index 0000000..1535fc6
--- /dev/null
+++ b/src/lib/log/messagedef.mes
@@ -0,0 +1,82 @@
+# Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+# $Id$
+
+$PREFIX MSG_
+
+# \brief Message Utility Message File
+#
+# This is the source of the set of messages generated by the message and logging
+# components. The associated .h and .cc files are created by hand from this
+# file though and are not built during the build process; this is to avoid the
+# chicken-and-egg situation where we need the files to build the message
+# compiler, yet we need the compiler to build the files.
+
+DUPLPRFX duplicate $PREFIX directive found
++ When reading a message file, more than one $PREFIX directive was found. In
++ this version of the code, such a condition is regarded as an error and the
++ read will be abandonded.
+
+IDNOTFND could not replace message for '%s': no such message identification
++ During start-up a local message file was read. A line with the listed
++ message identification was found in the file, but the identification is not
++ one contained in the compiled-in message dictionary. Either the message
++ identification has been mis-spelled in the file, or the local file was used
++ for an earlier version of the software and the message with that
++ identification has been removed.
++
++ This message may appear a number of times in the file, once for every such
++ unknown mnessage identification.
+
+ONETOKEN a line containing a message ID ('%s') and nothing else was found
++ Message definitions comprise lines starting with a message identification (a
++ symbolic name for the message) and followed by the text of the message. This
++ error is generated when a line is found in the message file that contains just
++ the message identification.
+
+OPENIN unable to open message file %s for input: %s
++ The program was not able to open the specified input message file for the
++ reason given.
+
+OPENOUT unable to open %s for output: %s
++ The program was not able to open the specified output file for the reason
++ given.
+
+PRFEXTRARG $PREFIX directive has too many arguments
++ The $PREFIX directive takes a single argument, a prefix to be added to the
++ symbol names when a C++ .h file is created. This error is generated when the
++ compiler finds a $PREFIX directive with more than one argument.
+
+PRFINVARG $PREFIX directive has an invalid argument ('%s')
++ The $PREFIX argument is used in a symbol name in a C++ header file. As such,
++ it must adhere to restrictions on C++ symbol names (e.g. may only contain
++ alphanumeric characters or underscores, and may nor start with a digit). A
++ $PREFIX directive was found with an argument (given in the message) that
++ violates those restictions.
+
+PRFNOARG no arguments were given to the $PREFIX directive
++ The $PREFIX directive takes a single argument, a prefix to be added to the
++ symbol names when a C++ .h file is created. This error is generated when the
++ compiler finds a $PREFIX directive with noa rguments.
+
+READERR error reading from %s: %s
++ The specified error was encountered reading from the named input file.
+
+UNRECDIR unrecognised directive '%s'
++ A line starting with a dollar symbol was found, but the first word on the line
++ (shown in the message) was not a recognised message compiler directive.
+
+WRITERR error writing to %s: %s
++ The specified error was encountered writing to the named output file.
diff --git a/src/lib/log/root_logger_name.cc b/src/lib/log/root_logger_name.cc
new file mode 100644
index 0000000..9378857
--- /dev/null
+++ b/src/lib/log/root_logger_name.cc
@@ -0,0 +1,26 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <string>
+#include <root_logger_name.h>
+
+namespace isc {
+namespace log {
+
+std::string RootLoggerName::name_("");
+
+}
+}
diff --git a/src/lib/log/root_logger_name.h b/src/lib/log/root_logger_name.h
new file mode 100644
index 0000000..80691d1
--- /dev/null
+++ b/src/lib/log/root_logger_name.h
@@ -0,0 +1,66 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __ROOT_LOGGER_NAME_H
+#define __ROOT_LOGGER_NAME_H
+
+#include <string>
+
+/// \brief Define Name of Root Logger
+///
+/// In the log4cxx system, the root logger is ".". The definition for the
+/// BIND-10 system is that the root logger of a program has the name of the
+/// program. This (trivial) class stores the name of the program in a
+/// location accessible to the logger classes.
+
+namespace isc {
+namespace log {
+
+class RootLoggerName {
+public:
+
+ /// \brief Constructor
+ ///
+ /// Sets the root logger name. Although the name is static, setting the
+ /// name in the constructor allows static initialization of the name by
+ /// declaring an external instance of the class in the main execution unit.
+ RootLoggerName(const std::string& name) {
+ setName(name);
+ }
+
+ /// \brief Set Root Logger Name
+ ///
+ /// \param name Name of the root logger. This should be the program
+ /// name.
+ static void setName(const std::string& name) {
+ name_ = name;
+ }
+
+ /// \brief Get Root Logger Name
+ ///
+ /// \return Name of the root logger.
+ static std::string getName() {
+ return name_;
+ }
+
+private:
+ static std::string name_; ///< Name of the root logger
+};
+
+}
+}
+
+#endif // __ROOT_LOGGER_NAME_H
diff --git a/src/lib/log/strutil.cc b/src/lib/log/strutil.cc
new file mode 100644
index 0000000..4b96601
--- /dev/null
+++ b/src/lib/log/strutil.cc
@@ -0,0 +1,138 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <numeric>
+#include <iostream>
+
+#include <string.h>
+#include <strutil.h>
+
+using namespace std;
+
+namespace isc {
+namespace strutil {
+
+// Normalize slashes
+
+void
+normalizeSlash(std::string& name) {
+ if (!name.empty()) {
+ size_t pos = 0;
+ while ((pos = name.find('\\', pos)) != std::string::npos) {
+ name[pos] = '/';
+ }
+ }
+}
+
+// Trim String
+
+string
+trim(const string& instring) {
+ static const char* blanks = " \t\n";
+
+ string retstring = "";
+ if (!instring.empty()) {
+
+ // Search for first non-blank character in the string
+ size_t first = instring.find_first_not_of(blanks);
+ if (first != string::npos) {
+
+ // String not all blanks, so look for last character
+ size_t last = instring.find_last_not_of(blanks);
+
+ // Extract the trimmed substring
+ retstring = instring.substr(first, (last - first + 1));
+ }
+ }
+
+ return retstring;
+}
+
+// Tokenise string. As noted in the header, this is locally written to avoid
+// another dependency on a Boost library.
+
+vector<string>
+tokens(const std::string text, const std::string& delim) {
+ vector<string> result;
+
+ // Search for the first non-delimiter character
+ size_t start = text.find_first_not_of(delim);
+ while (start != string::npos) {
+
+ // Non-delimiter found, look for next delimiter
+ size_t end = text.find_first_of(delim, start);
+ if (end != string::npos) {
+
+ // Delimiter found, so extract string & search for start of next
+ // non-delimiter segment.
+ result.push_back(text.substr(start, (end - start)));
+ start = text.find_first_not_of(delim, end);
+
+ } else {
+
+ // End of string found, extract rest of string and flag to exit
+ result.push_back(text.substr(start));
+ start = string::npos;
+ }
+ }
+
+ return result;
+}
+
+// Local function to pass to accumulate() for summing up string lengths.
+
+namespace {
+
+size_t
+lengthSum(string::size_type curlen, const string& cur_string) {
+ return (curlen + cur_string.size());
+}
+
+}
+
+// Provide printf-style formatting.
+
+std::string
+format(const std::string& format, const std::vector<std::string>& args) {
+
+ static const string flag = "%s";
+
+ // Initialize return string. To speed things up, we'll reserve an
+ // appropriate amount of space - current string size, plus length of all
+ // the argument strings, less two characters for each argument (the %s in
+ // the format string is being replaced).
+ string result;
+ size_t length = accumulate(args.begin(), args.end(), format.size(),
+ lengthSum) - (args.size() * flag.size());
+ result.reserve(length);
+
+ // Iterate through replacing all tokens
+ result = format;
+ size_t tokenpos = 0; // Position of last token replaced
+ int i = 0; // Index into argument array
+
+ while ((i < args.size()) && (tokenpos != string::npos)) {
+ tokenpos = result.find(flag, tokenpos);
+ if (tokenpos != string::npos) {
+ result.replace(tokenpos, flag.size(), args[i++]);
+ }
+ }
+
+ return result;
+}
+
+} // namespace log
+} // namespace isc
diff --git a/src/lib/log/strutil.h b/src/lib/log/strutil.h
new file mode 100644
index 0000000..cb0b793
--- /dev/null
+++ b/src/lib/log/strutil.h
@@ -0,0 +1,147 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __STRUTIL_H
+#define __STRUTIL_H
+
+#include <algorithm>
+#include <cctype>
+#include <string>
+#include <vector>
+
+namespace isc {
+namespace strutil {
+
+/// \brief A Set of C++ Utilities for Manipulating Strings
+
+/// \brief Normalize Backslash
+///
+/// Only relevant to Windows, this replaces all "\" in a string with "/" and
+/// returns the result. On other systems it is a no-op. Note that Windows does
+/// recognise file names with the "\" replaced by "/" (at least in system calls,
+/// if not the command line).
+///
+/// \param name Name to be substituted
+void normalizeSlash(std::string& name);
+
+
+/// \brief Trim Leading and Trailing Spaces
+///
+/// Returns a copy of the input string but with any leading or trailing spaces
+/// or tabs removed.
+///
+/// \param instring Input string to modify
+///
+/// \return String with leading and trailing spaces removed
+std::string trim(const std::string& instring);
+
+
+/// \brief Split String into Tokens
+///
+/// Splits a string into tokens (the tokens being delimited by one or more of
+/// the delimiter characters) and returns the tokens in a vector array. Note
+/// that adjacent delimiters are considered to be a single delimiter.
+///
+/// Special cases are:
+/// -# The empty string is considered to be zero tokens.
+/// -# A string comprising nothing but delimiters is considered to be zero
+/// tokens.
+///
+/// The reasoning behind this is that the string can be thought of as having
+/// invisible leading and trailing delimiter characters. Therefore both cases
+/// reduce to a set of contiguous delimiters, which are considered a single
+/// delimiter (so getting rid of the string).
+///
+/// We could use Boost for this, but this (simple) function eliminates one
+/// dependency in the code.
+///
+/// \param text String to be split. Passed by value as the internal copy is
+/// altered during the processing.
+/// \param delim Delimiter characters
+///
+/// \return Vector of tokens.
+std::vector<std::string> tokens(const std::string text,
+ const std::string& delim = std::string(" \t\n"));
+
+
+/// \brief Uppercase Character
+///
+/// Used in uppercase() to pass as an argument to std::transform(). The
+/// function std::toupper() can't be used as it takes an "int" as its argument;
+/// this confuses the template expansion mechanism because defererencing a
+/// string::iterator returns a char.
+///
+/// \param chr Character to be upper-cased.
+///
+/// \return Uppercase version of the argument
+inline char toUpper(char chr) {
+ return static_cast<char>(std::toupper(static_cast<int>(chr)));
+}
+
+
+/// \brief Uppercase String
+///
+/// A convenience function to uppercase a string.
+///
+/// \param text String to be upper-cased.
+inline void uppercase(std::string& text) {
+ std::transform(text.begin(), text.end(), text.begin(),
+ isc::strutil::toUpper);
+}
+
+/// \brief Lowercase Character
+///
+/// Used in lowercase() to pass as an argument to std::transform(). The
+/// function std::tolower() can't be used as it takes an "int" as its argument;
+/// this confuses the template expansion mechanism because defererencing a
+/// string::iterator returns a char.
+///
+/// \param chr Character to be lower-cased.
+///
+/// \return Lowercase version of the argument
+inline char toLower(char chr) {
+ return static_cast<char>(std::tolower(static_cast<int>(chr)));
+}
+
+/// \brief Lowercase String
+///
+/// A convenience function to lowercase a string
+///
+/// \param text String to be lower-cased.
+inline void lowercase(std::string& text) {
+ std::transform(text.begin(), text.end(), text.begin(),
+ isc::strutil::toLower);
+}
+
+
+/// \brief Apply Formatting
+///
+/// Given a printf-style format string containing only "%s" place holders
+/// (others are ignored) and a vector of strings, this produces a single string
+/// with the placeholders replaced.
+///
+/// \param format Format string
+/// \param args Vector of argument strings
+///
+/// \return Resultant string
+std::string format(const std::string& format,
+ const std::vector<std::string>& args);
+
+
+} // namespace strutil
+} // namespace isc
+
+#endif // __STRUTIL_H
diff --git a/src/lib/log/tests/Makefile.am b/src/lib/log/tests/Makefile.am
new file mode 100644
index 0000000..e3a023a
--- /dev/null
+++ b/src/lib/log/tests/Makefile.am
@@ -0,0 +1,47 @@
+SUBDIRS = .
+
+AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
+AM_CPPFLAGS += $(BOOST_INCLUDES)
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/log -I$(top_builddir)/src/lib/log
+AM_CXXFLAGS = $(B10_CXXFLAGS)
+
+if USE_STATIC_LINK
+AM_LDFLAGS = -static
+endif
+
+CLEANFILES = *.gcno *.gcda
+
+EXTRA_DIST = localdef.mes
+
+TESTS =
+if HAVE_GTEST
+TESTS += run_unittests
+run_unittests_SOURCES = root_logger_name_unittest.cc
+run_unittests_SOURCES += filename_unittest.cc
+run_unittests_SOURCES += logger_unittest.cc
+run_unittests_SOURCES += message_dictionary_unittest.cc
+run_unittests_SOURCES += message_reader_unittest.cc
+run_unittests_SOURCES += message_initializer_unittest.cc
+run_unittests_SOURCES += message_initializer_unittest_2.cc
+run_unittests_SOURCES += strutil_unittest.cc
+run_unittests_SOURCES += xdebuglevel_unittest.cc
+run_unittests_SOURCES += run_unittests.cc
+run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
+run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
+run_unittests_LDADD = $(GTEST_LDADD)
+run_unittests_LDADD += $(top_builddir)/src/lib/log/liblog.la
+run_unittests_LDADD += -llog4cxx
+endif
+
+TESTS += logger_support_test
+logger_support_test_SOURCES = logger_support_test.cc
+logger_support_test_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
+logger_support_test_LDFLAGS = $(AM_LDFLAGS)
+logger_support_test_LDADD = $(top_builddir)/src/lib/log/liblog.la
+
+noinst_PROGRAMS = $(TESTS)
+
+# Additional test using the shell
+PYTESTS = run_time_init_test.sh
+check-local:
+ $(SHELL) $(abs_builddir)/run_time_init_test.sh
diff --git a/src/lib/log/tests/filename_unittest.cc b/src/lib/log/tests/filename_unittest.cc
new file mode 100644
index 0000000..c33be9f
--- /dev/null
+++ b/src/lib/log/tests/filename_unittest.cc
@@ -0,0 +1,181 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: base64_unittest.cc 2549 2010-07-20 19:09:37Z jinmei $
+
+#include <string>
+
+#include <gtest/gtest.h>
+
+#include <log/filename.h>
+
+using namespace isc;
+using namespace isc::log;
+using namespace std;
+
+class FilenameTest : public ::testing::Test {
+protected:
+ FilenameTest()
+ {
+ }
+};
+
+
+// Check that the name can be changed
+
+TEST_F(FilenameTest, SetName) {
+ Filename fname("/a/b/c.d");
+ EXPECT_EQ("/a/b/c.d", fname.fullName());
+
+ fname.setName("test.txt");
+ EXPECT_EQ("test.txt", fname.fullName());
+}
+
+
+// Check that the components are split correctly. This is a check of the
+// private member split() method.
+
+TEST_F(FilenameTest, Components) {
+
+ // Complete name
+ Filename fname("/alpha/beta/gamma.delta");
+ EXPECT_EQ("/alpha/beta/", fname.directory());
+ EXPECT_EQ("gamma", fname.name());
+ EXPECT_EQ(".delta", fname.extension());
+
+ // Directory only
+ fname.setName("/gamma/delta/");
+ EXPECT_EQ("/gamma/delta/", fname.directory());
+ EXPECT_EQ("", fname.name());
+ EXPECT_EQ("", fname.extension());
+
+ // Filename only
+ fname.setName("epsilon");
+ EXPECT_EQ("", fname.directory());
+ EXPECT_EQ("epsilon", fname.name());
+ EXPECT_EQ("", fname.extension());
+
+ // Extension only
+ fname.setName(".zeta");
+ EXPECT_EQ("", fname.directory());
+ EXPECT_EQ("", fname.name());
+ EXPECT_EQ(".zeta", fname.extension());
+
+ // Missing directory
+ fname.setName("eta.theta");
+ EXPECT_EQ("", fname.directory());
+ EXPECT_EQ("eta", fname.name());
+ EXPECT_EQ(".theta", fname.extension());
+
+ // Missing filename
+ fname.setName("/iota/.kappa");
+ EXPECT_EQ("/iota/", fname.directory());
+ EXPECT_EQ("", fname.name());
+ EXPECT_EQ(".kappa", fname.extension());
+
+ // Missing extension
+ fname.setName("lambda/mu/nu");
+ EXPECT_EQ("lambda/mu/", fname.directory());
+ EXPECT_EQ("nu", fname.name());
+ EXPECT_EQ("", fname.extension());
+
+ // Check that the decomposition can occur in the presence of leading and
+ // trailing spaces
+ fname.setName(" lambda/mu/nu\t ");
+ EXPECT_EQ("lambda/mu/", fname.directory());
+ EXPECT_EQ("nu", fname.name());
+ EXPECT_EQ("", fname.extension());
+
+ // Empty string
+ fname.setName("");
+ EXPECT_EQ("", fname.directory());
+ EXPECT_EQ("", fname.name());
+ EXPECT_EQ("", fname.extension());
+
+ // ... and just spaces
+ fname.setName(" ");
+ EXPECT_EQ("", fname.directory());
+ EXPECT_EQ("", fname.name());
+ EXPECT_EQ("", fname.extension());
+
+ // Check corner cases - where separators are present, but strings are
+ // absent.
+ fname.setName("/");
+ EXPECT_EQ("/", fname.directory());
+ EXPECT_EQ("", fname.name());
+ EXPECT_EQ("", fname.extension());
+
+ fname.setName(".");
+ EXPECT_EQ("", fname.directory());
+ EXPECT_EQ("", fname.name());
+ EXPECT_EQ(".", fname.extension());
+
+ fname.setName("/.");
+ EXPECT_EQ("/", fname.directory());
+ EXPECT_EQ("", fname.name());
+ EXPECT_EQ(".", fname.extension());
+
+ // Note that the space is a valid filename here; only leading and trailing
+ // spaces should be trimmed.
+ fname.setName("/ .");
+ EXPECT_EQ("/", fname.directory());
+ EXPECT_EQ(" ", fname.name());
+ EXPECT_EQ(".", fname.extension());
+
+ fname.setName(" / . ");
+ EXPECT_EQ("/", fname.directory());
+ EXPECT_EQ(" ", fname.name());
+ EXPECT_EQ(".", fname.extension());
+}
+
+// Check that the expansion with a default works.
+
+TEST_F(FilenameTest, ExpandWithDefault) {
+ Filename fname("a.b");
+
+ // These tests also check that the trimming of the default component is
+ // done properly.
+ EXPECT_EQ("/c/d/a.b", fname.expandWithDefault(" /c/d/ "));
+ EXPECT_EQ("/c/d/a.b", fname.expandWithDefault("/c/d/e.f"));
+ EXPECT_EQ("a.b", fname.expandWithDefault("e.f"));
+
+ fname.setName("/a/b/c");
+ EXPECT_EQ("/a/b/c.d", fname.expandWithDefault(".d"));
+ EXPECT_EQ("/a/b/c.d", fname.expandWithDefault("x.d"));
+ EXPECT_EQ("/a/b/c.d", fname.expandWithDefault("/s/t/u.d"));
+ EXPECT_EQ("/a/b/c", fname.expandWithDefault("/s/t/u"));
+
+ fname.setName(".h");
+ EXPECT_EQ("/a/b/c.h", fname.expandWithDefault("/a/b/c.msg"));
+}
+
+// Check that we can use this as a default in expanding a filename
+
+TEST_F(FilenameTest, UseAsDefault) {
+
+ Filename fname("a.b");
+
+ // These tests also check that the trimming of the default component is
+ // done properly.
+ EXPECT_EQ("/c/d/a.b", fname.useAsDefault(" /c/d/ "));
+ EXPECT_EQ("/c/d/e.f", fname.useAsDefault("/c/d/e.f"));
+ EXPECT_EQ("e.f", fname.useAsDefault("e.f"));
+
+ fname.setName("/a/b/c");
+ EXPECT_EQ("/a/b/c.d", fname.useAsDefault(".d"));
+ EXPECT_EQ("/a/b/x.d", fname.useAsDefault("x.d"));
+ EXPECT_EQ("/s/t/u.d", fname.useAsDefault("/s/t/u.d"));
+ EXPECT_EQ("/s/t/u", fname.useAsDefault("/s/t/u"));
+ EXPECT_EQ("/a/b/c", fname.useAsDefault(""));
+}
diff --git a/src/lib/log/tests/localdef.mes b/src/lib/log/tests/localdef.mes
new file mode 100644
index 0000000..98e197d
--- /dev/null
+++ b/src/lib/log/tests/localdef.mes
@@ -0,0 +1,23 @@
+# Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+# \brief Local Definitions
+#
+# Holds local definitions of some of the messages produced by the program
+# logger_support_test, and is used as input to check that run-time message
+# replacement works.
+
+NOTHERE this message is not in the global dictionary
+READERR replacement read error, parameters: '%s' and '%s'
+UNRECDIR replacement unrecognised directive message, parameter is '%s'
diff --git a/src/lib/log/tests/logger_support_test.cc b/src/lib/log/tests/logger_support_test.cc
new file mode 100644
index 0000000..acca4f6
--- /dev/null
+++ b/src/lib/log/tests/logger_support_test.cc
@@ -0,0 +1,109 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: $
+
+/// \brief Example Program
+///
+/// Simple example program showing how to use the logger.
+
+#include <unistd.h>
+#include <string.h>
+
+#include <iostream>
+
+#include <log/logger.h>
+#include <log/logger_support.h>
+#include <log/root_logger_name.h>
+
+// Include a set of message definitions.
+#include <log/messagedef.h>
+
+using namespace isc::log;
+
+// Declare root logger and a logger to use an example.
+//RootLoggerName root_name("testing");
+
+RootLoggerName root("alpha");
+Logger logger_ex("example");
+Logger logger_dlm("dlm");
+
+// The program is invoked:
+//
+// logger_support_test [-s severity] [-d level ] [local_file]
+//
+// "severity" is one of "debug", "info", "warn", "error", "fatal"
+// "level" is the debug level, a number between 0 and 99
+// "local_file" is the name of a local file.
+//
+// The program sets the attributes on the root logger. Looking
+// at the output determines whether the program worked.e root logger. Looking
+// at the output determines whether the
+
+int main(int argc, char** argv) {
+
+ Logger::Severity severity = Logger::INFO;
+ int dbglevel = -1;
+ const char* localfile = NULL;
+ int option;
+
+ // Parse options
+ while ((option = getopt(argc, argv, "s:d:")) != -1) {
+ switch (option) {
+ case 's':
+ if (strcmp(optarg, "debug") == 0) {
+ severity = Logger::DEBUG;
+ } else if (strcmp(optarg, "info") == 0) {
+ severity = Logger::INFO;
+ } else if (strcmp(optarg, "warn") == 0) {
+ severity = Logger::WARN;
+ } else if (strcmp(optarg, "error") == 0) {
+ severity = Logger::ERROR;
+ } else if (strcmp(optarg, "fatal") == 0) {
+ severity = Logger::FATAL;
+ } else {
+ std::cout << "Unrecognised severity option: " <<
+ optarg << "\n";
+ exit(1);
+ }
+ break;
+
+ case 'd':
+ dbglevel = atoi(optarg);
+ break;
+
+ default:
+ std::cout << "Unrecognised option: " <<
+ static_cast<char>(option) << "\n";
+ }
+ }
+
+ if (optind < argc) {
+ localfile = argv[optind];
+ }
+
+ // Update the logging parameters
+ runTimeInit(severity, dbglevel, localfile);
+
+ // Log a few messages
+ logger_ex.fatal(MSG_WRITERR, "test1", "42");
+ logger_ex.error(MSG_UNRECDIR, "false");
+ logger_dlm.warn(MSG_READERR, "a.txt", "dummy test");
+ logger_dlm.info(MSG_OPENIN, "example.msg", "dummy test");
+ logger_ex.debug(0, MSG_UNRECDIR, "[abc]");
+ logger_ex.debug(24, MSG_UNRECDIR, "[24]");
+ logger_ex.debug(25, MSG_UNRECDIR, "[25]");
+ logger_ex.debug(26, MSG_UNRECDIR, "[26]");
+ return 0;
+}
diff --git a/src/lib/log/tests/logger_unittest.cc b/src/lib/log/tests/logger_unittest.cc
new file mode 100644
index 0000000..e15ec42
--- /dev/null
+++ b/src/lib/log/tests/logger_unittest.cc
@@ -0,0 +1,395 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: $
+
+#include <iostream>
+#include <string>
+
+#include <gtest/gtest.h>
+
+#include <log/root_logger_name.h>
+#include <log/logger.h>
+#include <log/messagedef.h>
+
+using namespace isc;
+using namespace isc::log;
+using namespace std;
+
+namespace isc {
+namespace log {
+
+/// \brief Test Logger
+///
+/// This logger is a subclass of the logger class under test, but makes
+/// protected methods public (for testing)
+
+class TestLogger : public Logger {
+public:
+ /// \brief constructor
+ TestLogger(const string& name) : Logger(name, true)
+ {}
+
+ /// \brief Logger Equality
+ bool operator==(const TestLogger& other) {
+ return Logger::operator==(other);
+ }
+
+ /// \brief Logger is Null
+ bool isInitialized() const {
+ return Logger::isInitialized();
+ }
+
+ /// \brief Conversion Between log4cxx Number and BIND-10 Severity
+ Severity convertLevel(int value) {
+ return Logger::convertLevel(value);
+ }
+};
+
+} // namespace log
+} // namespace isc
+
+
+class LoggerTest : public ::testing::Test {
+protected:
+ LoggerTest()
+ {
+ }
+};
+
+
+// Checks that the logger is named correctly.
+
+TEST_F(LoggerTest, Name) {
+
+ // Create a logger
+ RootLoggerName::setName("test1");
+ Logger logger("alpha");
+
+ // ... and check the name
+ EXPECT_EQ(string("test1.alpha"), logger.getName());
+}
+
+// This test attempts to get two instances of a logger with the same name
+// and checks that they are in fact the same logger.
+
+TEST_F(LoggerTest, GetLogger) {
+
+ // Set the root logger name (not strictly needed, but this will be the
+ // case in the program(.
+ RootLoggerName::setName("test2");
+
+ const string name1 = "alpha";
+ const string name2 = "beta";
+
+ // Instantiate two loggers that should be the same
+ TestLogger logger1(name1);
+ TestLogger logger2(name1);
+
+ // And check they are null at this point.
+ EXPECT_FALSE(logger1.isInitialized());
+ EXPECT_FALSE(logger2.isInitialized());
+
+ // Do some random operation
+ EXPECT_TRUE(logger1.isFatalEnabled());
+ EXPECT_TRUE(logger2.isFatalEnabled());
+
+ // And check they initialized and equal
+ EXPECT_TRUE(logger1.isInitialized());
+ EXPECT_TRUE(logger2.isInitialized());
+ EXPECT_TRUE(logger1 == logger2);
+
+ // Instantiate another logger with another name and check that it
+ // is different to the previously instantiated ones.
+ TestLogger logger3(name2);
+ EXPECT_FALSE(logger3.isInitialized());
+ EXPECT_TRUE(logger3.isFatalEnabled());
+ EXPECT_TRUE(logger3.isInitialized());
+ EXPECT_FALSE(logger1 == logger3);
+}
+
+// Test the number to severity conversion function
+
+TEST_F(LoggerTest, ConvertLevel) {
+
+ // Create a logger
+ RootLoggerName::setName("test3");
+ TestLogger logger("alpha");
+
+ // Basic 1:1
+ EXPECT_EQ(Logger::DEBUG, logger.convertLevel(log4cxx::Level::DEBUG_INT));
+ EXPECT_EQ(Logger::INFO, logger.convertLevel(log4cxx::Level::INFO_INT));
+ EXPECT_EQ(Logger::WARN, logger.convertLevel(log4cxx::Level::WARN_INT));
+ EXPECT_EQ(Logger::WARN, logger.convertLevel(log4cxx::Level::WARN_INT));
+ EXPECT_EQ(Logger::ERROR, logger.convertLevel(log4cxx::Level::ERROR_INT));
+ EXPECT_EQ(Logger::FATAL, logger.convertLevel(log4cxx::Level::FATAL_INT));
+ EXPECT_EQ(Logger::FATAL, logger.convertLevel(log4cxx::Level::FATAL_INT));
+ EXPECT_EQ(Logger::NONE, logger.convertLevel(log4cxx::Level::OFF_INT));
+
+ // Now some debug levels
+ EXPECT_EQ(Logger::DEBUG,
+ logger.convertLevel(log4cxx::Level::DEBUG_INT - 1));
+ EXPECT_EQ(Logger::DEBUG,
+ logger.convertLevel(log4cxx::Level::DEBUG_INT - MAX_DEBUG_LEVEL));
+ EXPECT_EQ(Logger::DEBUG,
+ logger.convertLevel(log4cxx::Level::DEBUG_INT - 2 * MAX_DEBUG_LEVEL));
+}
+
+// Check that the logger levels are get set properly.
+
+TEST_F(LoggerTest, Severity) {
+
+ // Create a logger
+ RootLoggerName::setName("test3");
+ TestLogger logger("alpha");
+
+ // Now check the levels
+ logger.setSeverity(Logger::NONE);
+ EXPECT_EQ(Logger::NONE, logger.getSeverity());
+
+ logger.setSeverity(Logger::FATAL);
+ EXPECT_EQ(Logger::FATAL, logger.getSeverity());
+
+ logger.setSeverity(Logger::ERROR);
+ EXPECT_EQ(Logger::ERROR, logger.getSeverity());
+
+ logger.setSeverity(Logger::WARN);
+ EXPECT_EQ(Logger::WARN, logger.getSeverity());
+
+ logger.setSeverity(Logger::INFO);
+ EXPECT_EQ(Logger::INFO, logger.getSeverity());
+
+ logger.setSeverity(Logger::DEBUG);
+ EXPECT_EQ(Logger::DEBUG, logger.getSeverity());
+
+ logger.setSeverity(Logger::DEFAULT);
+ EXPECT_EQ(Logger::DEFAULT, logger.getSeverity());
+}
+
+// Check that the debug level is set correctly.
+
+TEST_F(LoggerTest, DebugLevels) {
+
+ // Create a logger
+ RootLoggerName::setName("test4");
+ TestLogger logger("alpha");
+
+ // Debug level should be 0 if not at debug severity
+ logger.setSeverity(Logger::NONE, 20);
+ EXPECT_EQ(0, logger.getDebugLevel());
+
+ logger.setSeverity(Logger::INFO, 42);
+ EXPECT_EQ(0, logger.getDebugLevel());
+
+ // Should be the value set if the severity is set to DEBUG though.
+ logger.setSeverity(Logger::DEBUG, 32);
+ EXPECT_EQ(32, logger.getDebugLevel());
+
+ logger.setSeverity(Logger::DEBUG, 97);
+ EXPECT_EQ(97, logger.getDebugLevel());
+
+ // Try the limits
+ logger.setSeverity(Logger::DEBUG, -1);
+ EXPECT_EQ(0, logger.getDebugLevel());
+
+ logger.setSeverity(Logger::DEBUG, 0);
+ EXPECT_EQ(0, logger.getDebugLevel());
+
+ logger.setSeverity(Logger::DEBUG, 1);
+ EXPECT_EQ(1, logger.getDebugLevel());
+
+ logger.setSeverity(Logger::DEBUG, 98);
+ EXPECT_EQ(98, logger.getDebugLevel());
+
+ logger.setSeverity(Logger::DEBUG, 99);
+ EXPECT_EQ(99, logger.getDebugLevel());
+
+ logger.setSeverity(Logger::DEBUG, 100);
+ EXPECT_EQ(99, logger.getDebugLevel());
+}
+
+// Check that changing the parent and child severity does not affect the
+// other.
+
+TEST_F(LoggerTest, SeverityInheritance) {
+
+ // Create to loggers. We cheat here as we know that the underlying
+ // implementation (in this case log4cxx) will set a parent-child
+ // relationship if the loggers are named <parent> and <parent>.<child>.
+
+ RootLoggerName::setName("test5");
+ TestLogger parent("alpha");
+ TestLogger child("alpha.beta");
+
+ // By default, newly created loggers should have a level of DEFAULT
+ // (i.e. default to parent)
+ EXPECT_EQ(Logger::DEFAULT, parent.getSeverity());
+ EXPECT_EQ(Logger::DEFAULT, child.getSeverity());
+
+ // Set the severity of the child to something other than the default -
+ // check it changes and that of the parent does not.
+ child.setSeverity(Logger::INFO);
+ EXPECT_EQ(Logger::DEFAULT, parent.getSeverity());
+ EXPECT_EQ(Logger::INFO, child.getSeverity());
+
+ // Reset the child severity and set that of the parent
+ child.setSeverity(Logger::DEFAULT);
+ EXPECT_EQ(Logger::DEFAULT, parent.getSeverity());
+ EXPECT_EQ(Logger::DEFAULT, child.getSeverity());
+ parent.setSeverity(Logger::WARN);
+ EXPECT_EQ(Logger::WARN, parent.getSeverity());
+ EXPECT_EQ(Logger::DEFAULT, child.getSeverity());
+}
+
+// Check that severity is inherited.
+
+TEST_F(LoggerTest, EffectiveSeverityInheritance) {
+
+ // Create to loggers. We cheat here as we know that the underlying
+ // implementation (in this case log4cxx) will set a parent-child
+ // relationship if the loggers are named <parent> and <parent>.<child>.
+
+ RootLoggerName::setName("test6");
+ Logger parent("test6");
+ Logger child("test6.beta");
+
+ // By default, newly created loggers should have a level of DEFAULT
+ // (i.e. default to parent) and the root should have a default severity
+ // of INFO. However, the latter is only enforced when created by the
+ // RootLogger class, so explicitly set it for the parent for now.
+ parent.setSeverity(Logger::INFO);
+ EXPECT_EQ(Logger::INFO, parent.getEffectiveSeverity());
+
+ EXPECT_EQ(Logger::DEFAULT, child.getSeverity());
+ EXPECT_EQ(Logger::INFO, child.getEffectiveSeverity());
+
+ // Set the severity of the child to something other than the default -
+ // check it changes and that of the parent does not.
+ child.setSeverity(Logger::FATAL);
+ EXPECT_EQ(Logger::INFO, parent.getEffectiveSeverity());
+ EXPECT_EQ(Logger::FATAL, child.getEffectiveSeverity());
+
+ // Reset the child severity and check again.
+ child.setSeverity(Logger::DEFAULT);
+ EXPECT_EQ(Logger::INFO, parent.getEffectiveSeverity());
+ EXPECT_EQ(Logger::INFO, child.getEffectiveSeverity());
+
+ // Change the parwnt's severity and check it is reflects in the child.
+ parent.setSeverity(Logger::WARN);
+ EXPECT_EQ(Logger::WARN, parent.getEffectiveSeverity());
+ EXPECT_EQ(Logger::WARN, child.getEffectiveSeverity());
+}
+
+// Test the isXxxxEnabled methods.
+
+TEST_F(LoggerTest, IsXxxEnabled) {
+
+ RootLoggerName::setName("test7");
+ Logger logger("test7");
+
+ logger.setSeverity(Logger::INFO);
+ EXPECT_FALSE(logger.isDebugEnabled());
+ EXPECT_TRUE(logger.isInfoEnabled());
+ EXPECT_TRUE(logger.isWarnEnabled());
+ EXPECT_TRUE(logger.isErrorEnabled());
+ EXPECT_TRUE(logger.isFatalEnabled());
+
+ logger.setSeverity(Logger::WARN);
+ EXPECT_FALSE(logger.isDebugEnabled());
+ EXPECT_FALSE(logger.isInfoEnabled());
+ EXPECT_TRUE(logger.isWarnEnabled());
+ EXPECT_TRUE(logger.isErrorEnabled());
+ EXPECT_TRUE(logger.isFatalEnabled());
+
+ logger.setSeverity(Logger::ERROR);
+ EXPECT_FALSE(logger.isDebugEnabled());
+ EXPECT_FALSE(logger.isInfoEnabled());
+ EXPECT_FALSE(logger.isWarnEnabled());
+ EXPECT_TRUE(logger.isErrorEnabled());
+ EXPECT_TRUE(logger.isFatalEnabled());
+
+ logger.setSeverity(Logger::FATAL);
+ EXPECT_FALSE(logger.isDebugEnabled());
+ EXPECT_FALSE(logger.isInfoEnabled());
+ EXPECT_FALSE(logger.isWarnEnabled());
+ EXPECT_FALSE(logger.isErrorEnabled());
+ EXPECT_TRUE(logger.isFatalEnabled());
+
+ // Check various debug levels
+
+ logger.setSeverity(Logger::DEBUG);
+ EXPECT_TRUE(logger.isDebugEnabled());
+ EXPECT_TRUE(logger.isInfoEnabled());
+ EXPECT_TRUE(logger.isWarnEnabled());
+ EXPECT_TRUE(logger.isErrorEnabled());
+ EXPECT_TRUE(logger.isFatalEnabled());
+
+ logger.setSeverity(Logger::DEBUG, 45);
+ EXPECT_TRUE(logger.isDebugEnabled());
+ EXPECT_TRUE(logger.isInfoEnabled());
+ EXPECT_TRUE(logger.isWarnEnabled());
+ EXPECT_TRUE(logger.isErrorEnabled());
+ EXPECT_TRUE(logger.isFatalEnabled());
+
+ // Create a child logger with no severity set, and check that it reflects
+ // the severity of the parent logger.
+
+ Logger child("test7.child");
+ logger.setSeverity(Logger::FATAL);
+ EXPECT_FALSE(child.isDebugEnabled());
+ EXPECT_FALSE(child.isInfoEnabled());
+ EXPECT_FALSE(child.isWarnEnabled());
+ EXPECT_FALSE(child.isErrorEnabled());
+ EXPECT_TRUE(child.isFatalEnabled());
+
+ logger.setSeverity(Logger::INFO);
+ EXPECT_FALSE(child.isDebugEnabled());
+ EXPECT_TRUE(child.isInfoEnabled());
+ EXPECT_TRUE(child.isWarnEnabled());
+ EXPECT_TRUE(child.isErrorEnabled());
+ EXPECT_TRUE(child.isFatalEnabled());
+}
+
+// Within the Debug level there are 100 debug levels. Test that we know
+// when to issue a debug message.
+
+TEST_F(LoggerTest, IsDebugEnabledLevel) {
+
+ RootLoggerName::setName("test8");
+ Logger logger("test8");
+
+ int MID_LEVEL = (MIN_DEBUG_LEVEL + MAX_DEBUG_LEVEL) / 2;
+
+ logger.setSeverity(Logger::DEBUG);
+ EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
+ EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL));
+ EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
+
+ logger.setSeverity(Logger::DEBUG, MIN_DEBUG_LEVEL);
+ EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
+ EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL));
+ EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
+
+ logger.setSeverity(Logger::DEBUG, MID_LEVEL);
+ EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
+ EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL - 1));
+ EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL));
+ EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL + 1));
+ EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
+
+ logger.setSeverity(Logger::DEBUG, MAX_DEBUG_LEVEL);
+ EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
+ EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL));
+ EXPECT_TRUE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
+}
diff --git a/src/lib/log/tests/message_dictionary_unittest.cc b/src/lib/log/tests/message_dictionary_unittest.cc
new file mode 100644
index 0000000..78aa851
--- /dev/null
+++ b/src/lib/log/tests/message_dictionary_unittest.cc
@@ -0,0 +1,173 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: base64_unittest.cc 2549 2010-07-20 19:09:37Z jinmei $
+
+#include <cstddef>
+#include <string>
+#include <gtest/gtest.h>
+#include <log/message_dictionary.h>
+#include <log/message_types.h>
+
+using namespace isc;
+using namespace isc::log;
+using namespace std;
+
+class MessageDictionaryTest : public ::testing::Test {
+protected:
+ MessageDictionaryTest() :
+ alpha_id("ALPHA"), alpha_text("This is alpha"),
+ beta_id("BETA"), beta_text("This is beta"),
+ gamma_id("GAMMA"), gamma_text("This is gamma")
+ {
+ }
+
+ MessageID alpha_id;
+ std::string alpha_text;
+ MessageID beta_id;
+ std::string beta_text;
+ MessageID gamma_id;
+ std::string gamma_text;
+
+};
+
+
+// Check that the global dictionary is a singleton.
+
+TEST_F(MessageDictionaryTest, GlobalTest) {
+ MessageDictionary* global = MessageDictionary::globalDictionary();
+ EXPECT_FALSE(NULL == global);
+
+ MessageDictionary* global2 = MessageDictionary::globalDictionary();
+ EXPECT_EQ(global2, global);
+}
+
+// Check that adding messages works
+
+TEST_F(MessageDictionaryTest, Add) {
+ MessageDictionary dictionary;
+ EXPECT_EQ(0, dictionary.size());
+
+ // Add a few messages and check that we can look them up and that there is
+ // nothing in the overflow vector.
+ EXPECT_TRUE(dictionary.add(alpha_id, alpha_text));
+ EXPECT_TRUE(dictionary.add(beta_id, beta_text));
+ EXPECT_EQ(2, dictionary.size());
+
+ EXPECT_EQ(alpha_text, dictionary.getText(alpha_id));
+ EXPECT_EQ(beta_text, dictionary.getText(beta_id));
+ EXPECT_EQ(string(""), dictionary.getText(gamma_id));
+
+ // Try adding a duplicate with different text. It should not replace the
+ // current text and the ID should be in the overflow section.
+ EXPECT_FALSE(dictionary.add(alpha_id, gamma_text));
+ EXPECT_EQ(2, dictionary.size());
+}
+
+// Check that replacing messages works.
+
+TEST_F(MessageDictionaryTest, Replace) {
+ MessageDictionary dictionary;
+ EXPECT_EQ(0, dictionary.size());
+
+ // Try to replace a non-existent message
+ EXPECT_FALSE(dictionary.replace(alpha_id, alpha_text));
+ EXPECT_EQ(0, dictionary.size());
+
+ // Add a couple of messages.
+ EXPECT_TRUE(dictionary.add(alpha_id, alpha_text));
+ EXPECT_TRUE(dictionary.add(beta_id, beta_text));
+ EXPECT_EQ(2, dictionary.size());
+
+ // Replace an existing message
+ EXPECT_TRUE(dictionary.replace(alpha_id, gamma_text));
+ EXPECT_EQ(2, dictionary.size());
+ EXPECT_EQ(gamma_text, dictionary.getText(alpha_id));
+
+ // ... and replace non-existent message (but now the dictionary has some
+ // items in it).
+ EXPECT_FALSE(dictionary.replace(gamma_id, alpha_text));
+ EXPECT_EQ(2, dictionary.size());
+ EXPECT_EQ(string(""), dictionary.getText(gamma_id));
+}
+
+// Load test
+
+TEST_F(MessageDictionaryTest, LoadTest) {
+ static const char* data1[] = {
+ "ALPHA", "This is alpha",
+ "BETA", "This is beta",
+ "GAMMA", "This is gamma",
+ NULL
+ };
+
+ static const char* data2[] = {
+ "DELTA", "This is delta",
+ "EPSILON", "This is epsilon",
+ "ETA", NULL
+ };
+
+ MessageDictionary dictionary1;
+ EXPECT_EQ(0, dictionary1.size());
+
+ // Load a dictionary1.
+ vector<MessageID> duplicates = dictionary1.load(data1);
+ EXPECT_EQ(3, dictionary1.size());
+ EXPECT_EQ(string(data1[1]), dictionary1.getText(data1[0]));
+ EXPECT_EQ(string(data1[3]), dictionary1.getText(data1[2]));
+ EXPECT_EQ(string(data1[5]), dictionary1.getText(data1[4]));
+ EXPECT_EQ(0, duplicates.size());
+
+ // Attempt an overwrite
+ duplicates = dictionary1.load(data1);
+ EXPECT_EQ(3, dictionary1.size());
+ EXPECT_EQ(3, duplicates.size());
+
+ // Try a new dictionary but with an incorrect number of elements
+ MessageDictionary dictionary2;
+ EXPECT_EQ(0, dictionary2.size());
+
+ duplicates = dictionary2.load(data2);
+ EXPECT_EQ(2, dictionary2.size());
+ EXPECT_EQ(string(data2[1]), dictionary2.getText(data2[0]));
+ EXPECT_EQ(string(data2[3]), dictionary2.getText(data2[2]));
+ EXPECT_EQ(string(""), dictionary2.getText(data2[4]));
+ EXPECT_EQ(0, duplicates.size());
+}
+
+// Check for some non-existent items
+
+TEST_F(MessageDictionaryTest, Lookups) {
+ static const char* data[] = {
+ "ALPHA", "This is alpha",
+ "BETA", "This is beta",
+ "GAMMA", "This is gamma",
+ NULL
+ };
+
+ MessageDictionary dictionary;
+ vector<MessageID> duplicates = dictionary.load(data);
+ EXPECT_EQ(3, dictionary.size());
+ EXPECT_EQ(0, duplicates.size());
+
+ // Valid lookups
+ EXPECT_EQ(string("This is alpha"), dictionary.getText("ALPHA"));
+ EXPECT_EQ(string("This is beta"), dictionary.getText("BETA"));
+ EXPECT_EQ(string("This is gamma"), dictionary.getText("GAMMA"));
+
+ // ... and invalid ones
+ EXPECT_EQ(string(""), dictionary.getText("XYZZY"));
+ EXPECT_EQ(string(""), dictionary.getText(""));
+ EXPECT_EQ(string(""), dictionary.getText("\n\n\n"));
+}
diff --git a/src/lib/log/tests/message_initializer_unittest.cc b/src/lib/log/tests/message_initializer_unittest.cc
new file mode 100644
index 0000000..6a1019f
--- /dev/null
+++ b/src/lib/log/tests/message_initializer_unittest.cc
@@ -0,0 +1,72 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: base64_unittest.cc 2549 2010-07-20 19:09:37Z jinmei $
+
+#include <cstddef>
+#include <string>
+#include <gtest/gtest.h>
+#include <log/message_dictionary.h>
+#include <log/message_initializer.h>
+
+using namespace isc;
+using namespace isc::log;
+using namespace std;
+
+// Declare a set of messages to go into the global dictionary.
+
+namespace {
+const char* values1[] = {
+ "GLOBAL1", "global message one",
+ "GLOBAL2", "global message two",
+ NULL
+};
+
+const char* values2[] = {
+ "GLOBAL3", "global message three",
+ "GLOBAL4", "global message four",
+ NULL
+};
+
+}
+
+// Statically initialize the global dictionary with those messages. Three sets
+// are used to check that the declaration of separate initializer objects really// does combine the messages. (The third set is declared in the separately-
+// compiled file message_identifier_initializer_unittest_2.cc.)
+
+MessageInitializer init_message_initializer_unittest_1(values1);
+MessageInitializer init_message_initializer_unittest_2(values2);
+
+
+class MessageInitializerTest : public ::testing::Test {
+protected:
+ MessageInitializerTest()
+ {
+ }
+};
+
+
+// Check that the global dictionary is initialized with the specified
+// messages.
+
+TEST_F(MessageInitializerTest, MessageTest) {
+ MessageDictionary* global = MessageDictionary::globalDictionary();
+
+ EXPECT_EQ(string("global message one"), global->getText("GLOBAL1"));
+ EXPECT_EQ(string("global message two"), global->getText("GLOBAL2"));
+ EXPECT_EQ(string("global message three"), global->getText("GLOBAL3"));
+ EXPECT_EQ(string("global message four"), global->getText("GLOBAL4"));
+ EXPECT_EQ(string("global message five"), global->getText("GLOBAL5"));
+ EXPECT_EQ(string("global message six"), global->getText("GLOBAL6"));
+}
diff --git a/src/lib/log/tests/message_initializer_unittest_2.cc b/src/lib/log/tests/message_initializer_unittest_2.cc
new file mode 100644
index 0000000..c005033
--- /dev/null
+++ b/src/lib/log/tests/message_initializer_unittest_2.cc
@@ -0,0 +1,41 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: base64_unittest.cc 2549 2010-07-20 19:09:37Z jinmei $
+
+// The sole purpose of this file is to provide a set of message definitions
+// in a separate compilation unit from the one in which their presence is
+// checked. This tests that merely declaring the MessageInitializer object
+// is enough to include the definitions in the global dictionary.
+
+#include <log/message_initializer.h>
+
+using namespace isc::log;
+
+// Declare a set of messages to go into the global dictionary.
+
+namespace {
+
+const char* values3[] = {
+ "GLOBAL5", "global message five",
+ "GLOBAL6", "global message six",
+ NULL
+};
+
+}
+
+// Statically initialize the global dictionary with those messages.
+// Three sets are used to check that the declaration of separate
+// initializer objects really does combine the messages.
+MessageInitializer init_message_initializer_unittest_3(values3);
diff --git a/src/lib/log/tests/message_reader_unittest.cc b/src/lib/log/tests/message_reader_unittest.cc
new file mode 100644
index 0000000..2891805
--- /dev/null
+++ b/src/lib/log/tests/message_reader_unittest.cc
@@ -0,0 +1,228 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: base64_unittest.cc 2549 2010-07-20 19:09:37Z jinmei $
+
+#include <algorithm>
+#include <string>
+#include <gtest/gtest.h>
+
+#include <log/messagedef.h>
+#include <log/message_dictionary.h>
+#include <log/message_exception.h>
+#include <log/message_reader.h>
+
+using namespace isc;
+using namespace isc::log;
+using namespace std;
+
+class MessageReaderTest : public ::testing::Test {
+protected:
+ MessageReaderTest() : dictionary_(), reader_()
+ {
+ dictionary_ = new MessageDictionary();
+ reader_.setDictionary(dictionary_);
+ }
+
+ ~MessageReaderTest() {
+ delete dictionary_;
+ }
+
+ MessageDictionary* dictionary_; // Dictionary to add messages to
+ MessageReader reader_; // Default reader object
+};
+
+
+// Check the get/set dictionary calls (using a local reader and dictionary).
+
+TEST_F(MessageReaderTest, GetSetDictionary) {
+ MessageReader reader;
+ EXPECT_TRUE(reader.getDictionary() == NULL);
+
+ MessageDictionary dictionary;
+ reader.setDictionary(&dictionary);
+ EXPECT_EQ(&dictionary, reader.getDictionary());
+}
+
+// Check for parsing blank lines and comments. These should not add to the
+// dictionary and each parse should return success.
+
+TEST_F(MessageReaderTest, BlanksAndComments) {
+
+ // Ensure that the dictionary is empty.
+ EXPECT_EQ(0, dictionary_->size());
+
+ // Add a number of blank lines and comments and check that (a) they are
+ // parsed successfully ...
+ EXPECT_NO_THROW(reader_.processLine(""));
+ EXPECT_NO_THROW(reader_.processLine(" "));
+ EXPECT_NO_THROW(reader_.processLine(" \n "));
+ EXPECT_NO_THROW(reader_.processLine("# This is a comment"));
+ EXPECT_NO_THROW(reader_.processLine("\t\t # Another comment"));
+ EXPECT_NO_THROW(reader_.processLine(" + A description line"));
+ EXPECT_NO_THROW(reader_.processLine("#+ A comment"));
+ EXPECT_NO_THROW(reader_.processLine(" +# A description line"));
+
+ // ... and (b) nothing gets added to either the map or the not-added section.
+ EXPECT_EQ(0, dictionary_->size());
+ vector<MessageID> not_added = reader_.getNotAdded();
+ EXPECT_EQ(0, not_added.size());
+}
+
+
+// Local test to check that processLine generates the right exception.
+
+void
+processLineException(MessageReader& reader, const char* what,
+ MessageID& expected) {
+
+ try {
+ reader.processLine(what);
+ FAIL() << "MessageReader::processLine() should throw an exception " <<
+ " with message ID " << expected << " for '" << what << "'\n";
+ } catch (MessageException& e) {
+ EXPECT_EQ(expected, e.id());
+ } catch (...) {
+ FAIL() << "Unknown exception thrown by MessageReader::processLine()\n";
+ }
+}
+
+// Check that it can parse a prefix
+
+TEST_F(MessageReaderTest, Prefix) {
+
+ // Check that no prefix is present
+ EXPECT_EQ(string(""), reader_.getPrefix());
+
+ // Check that a prefix directive with no argument generates an error.
+ processLineException(reader_, "$PREFIX", MSG_PRFNOARG);
+
+ // Check a prefix with multiple arguments is invalid
+ processLineException(reader_, "$prefix A B", MSG_PRFEXTRARG);
+
+ // Prefixes should be alphanumeric (with underscores) and not start
+ // with a number.
+ processLineException(reader_, "$prefix ab[cd", MSG_PRFINVARG);
+ processLineException(reader_, "$prefix 123", MSG_PRFINVARG);
+ processLineException(reader_, "$prefix 1ABC", MSG_PRFINVARG);
+
+ // A valid prefix should be accepted
+ EXPECT_NO_THROW(reader_.processLine("$PREFIX dlm__"));
+ EXPECT_EQ(string("DLM__"), reader_.getPrefix());
+
+ // And check that the parser fails on invalid prefixes...
+ processLineException(reader_, "$prefix 1ABC", MSG_PRFINVARG);
+
+ // ... and rejects another valid one
+ processLineException(reader_, "$PREFIX ABC", MSG_DUPLPRFX);
+
+ // Check that we can clear the prefix as well
+ reader_.clearPrefix();
+ EXPECT_EQ(string(""), reader_.getPrefix());
+}
+
+// Check that it can parse a line
+
+TEST_F(MessageReaderTest, ValidMessageAddDefault) {
+
+ // Add a couple of valid messages
+ reader_.processLine("GLOBAL1\t\tthis is message global one\n");
+ reader_.processLine("GLOBAL2 this is message global two");
+
+ // ... and check them
+ EXPECT_EQ(string("this is message global one"),
+ dictionary_->getText("GLOBAL1"));
+ EXPECT_EQ(string("this is message global two"),
+ dictionary_->getText("GLOBAL2"));
+ EXPECT_EQ(2, dictionary_->size());
+
+ // ... and ensure no messages were not added
+ vector<MessageID> not_added = reader_.getNotAdded();
+ EXPECT_EQ(0, not_added.size());
+}
+
+TEST_F(MessageReaderTest, ValidMessageAdd) {
+
+ // Add a couple of valid messages
+ reader_.processLine("GLOBAL1\t\tthis is message global one\n",
+ MessageReader::ADD);
+ reader_.processLine("GLOBAL2 this is message global two",
+ MessageReader::ADD);
+
+ // ... and check them
+ EXPECT_EQ(string("this is message global one"),
+ dictionary_->getText("GLOBAL1"));
+ EXPECT_EQ(string("this is message global two"),
+ dictionary_->getText("GLOBAL2"));
+ EXPECT_EQ(2, dictionary_->size());
+
+ // ... and ensure no messages were not added
+ vector<MessageID> not_added = reader_.getNotAdded();
+ EXPECT_EQ(0, not_added.size());
+}
+
+TEST_F(MessageReaderTest, ValidMessageReplace) {
+
+ dictionary_->add("GLOBAL1", "original global1 message");
+ dictionary_->add("GLOBAL2", "original global2 message");
+
+ // Replace a couple of valid messages
+ reader_.processLine("GLOBAL1\t\tthis is message global one\n",
+ MessageReader::REPLACE);
+ reader_.processLine("GLOBAL2 this is message global two",
+ MessageReader::REPLACE);
+
+ // ... and check them
+ EXPECT_EQ(string("this is message global one"),
+ dictionary_->getText("GLOBAL1"));
+ EXPECT_EQ(string("this is message global two"),
+ dictionary_->getText("GLOBAL2"));
+ EXPECT_EQ(2, dictionary_->size());
+
+ // ... and ensure no messages were not added
+ vector<MessageID> not_added = reader_.getNotAdded();
+ EXPECT_EQ(0, not_added.size());
+}
+
+// Do checks on overflows, although this essentially duplicates the checks
+// in MessageDictionary.
+
+TEST_F(MessageReaderTest, Overflows) {
+
+ // Add a couple of valid messages
+ reader_.processLine("GLOBAL1\t\tthis is message global one\n");
+ reader_.processLine("GLOBAL2 this is message global two");
+
+ // Add a duplicate in ADD mode.
+ reader_.processLine("GLOBAL1\t\tthis is a replacement for global one");
+
+ // Replace a non-existent one in REPLACE mode
+ reader_.processLine("LOCAL\t\tthis is a new message",
+ MessageReader::REPLACE);
+
+ // Check what is in the dictionary.
+ EXPECT_EQ(string("this is message global one"),
+ dictionary_->getText("GLOBAL1"));
+ EXPECT_EQ(string("this is message global two"),
+ dictionary_->getText("GLOBAL2"));
+ EXPECT_EQ(2, dictionary_->size());
+
+ // ... and ensure no overflows
+ vector<MessageID> not_added = reader_.getNotAdded();
+ ASSERT_EQ(2, not_added.size());
+
+ sort(not_added.begin(), not_added.end());
+ EXPECT_EQ(string("GLOBAL1"), not_added[0]);
+ EXPECT_EQ(string("LOCAL"), not_added[1]);
+}
diff --git a/src/lib/log/tests/root_logger_name_unittest.cc b/src/lib/log/tests/root_logger_name_unittest.cc
new file mode 100644
index 0000000..6994dc6
--- /dev/null
+++ b/src/lib/log/tests/root_logger_name_unittest.cc
@@ -0,0 +1,52 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: base64_unittest.cc 2549 2010-07-20 19:09:37Z jinmei $
+
+#include <string>
+
+#include <gtest/gtest.h>
+
+#include <log/root_logger_name.h>
+
+using namespace isc;
+using namespace isc::log;
+
+class RootLoggerNameTest : public ::testing::Test {
+protected:
+ RootLoggerNameTest()
+ {
+ }
+};
+
+// Check of the (only) functionality of the class.
+
+TEST_F(RootLoggerNameTest, SetGet) {
+ const std::string name1 = "test1";
+ const std::string name2 = "test2";
+
+ // Check that Set/Get works
+ RootLoggerName::setName(name1);
+ EXPECT_EQ(name1, RootLoggerName::getName());
+
+ // We could not test that the root logger name is initialised
+ // correctly (as there is one instance of it and we don't know
+ // when this test will be run) so to check that setName() actually
+ // does change the name, run the test again with a different name.
+ //
+ // (There was always the outside chance that the root logger name
+ // was initialised with name1 and that setName() has no effect.)
+ RootLoggerName::setName(name2);
+ EXPECT_EQ(name2, RootLoggerName::getName());
+}
diff --git a/src/lib/log/tests/run_time_init_test.sh.in b/src/lib/log/tests/run_time_init_test.sh.in
new file mode 100755
index 0000000..be52ded
--- /dev/null
+++ b/src/lib/log/tests/run_time_init_test.sh.in
@@ -0,0 +1,81 @@
+#!/bin/sh
+# Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+failcount=0
+localmes=@abs_srcdir@/localdef.mes
+tempfile=@abs_builddir@/run_time_init_test_tempfile_$$
+
+passfail() {
+ if [ $1 -eq 0 ]; then
+ echo "pass"
+ else
+ echo "FAIL"
+ fi
+ failcount=`expr $failcount + $1`
+}
+
+echo -n "1. runInitTest default parameters: "
+cat > $tempfile << .
+FATAL [alpha.example] WRITERR, error writing to test1: 42
+ERROR [alpha.example] UNRECDIR, unrecognised directive 'false'
+WARN [alpha.dlm] READERR, error reading from a.txt: dummy test
+INFO [alpha.dlm] OPENIN, unable to open message file example.msg for input: dummy test
+.
+./logger_support_test | cut -d' ' -f3- | diff $tempfile -
+passfail $?
+
+echo -n "2. Severity filter: "
+cat > $tempfile << .
+FATAL [alpha.example] WRITERR, error writing to test1: 42
+ERROR [alpha.example] UNRECDIR, unrecognised directive 'false'
+.
+./logger_support_test -s error | cut -d' ' -f3- | diff $tempfile -
+passfail $?
+
+echo -n "3. Debug level: "
+cat > $tempfile << .
+FATAL [alpha.example] WRITERR, error writing to test1: 42
+ERROR [alpha.example] UNRECDIR, unrecognised directive 'false'
+WARN [alpha.dlm] READERR, error reading from a.txt: dummy test
+INFO [alpha.dlm] OPENIN, unable to open message file example.msg for input: dummy test
+DEBUG [alpha.example] UNRECDIR, unrecognised directive '[abc]'
+DEBUG [alpha.example] UNRECDIR, unrecognised directive '[24]'
+DEBUG [alpha.example] UNRECDIR, unrecognised directive '[25]'
+.
+./logger_support_test -s debug -d 25 | cut -d' ' -f3- | diff $tempfile -
+passfail $?
+
+echo -n "4. Local message replacement: "
+cat > $tempfile << .
+WARN [alpha.log] IDNOTFND, could not replace message for 'NOTHERE': no such message identification
+FATAL [alpha.example] WRITERR, error writing to test1: 42
+ERROR [alpha.example] UNRECDIR, replacement unrecognised directive message, parameter is 'false'
+WARN [alpha.dlm] READERR, replacement read error, parameters: 'a.txt' and 'dummy test'
+INFO [alpha.dlm] OPENIN, unable to open message file example.msg for input: dummy test
+.
+./logger_support_test $localmes | cut -d' ' -f3- | diff $tempfile -
+passfail $?
+
+rm -f $tempfile
+
+if [ $failcount -eq 0 ]; then
+ echo "PASS: run_time_init_test"
+elif [ $failcount -eq 1 ]; then
+ echo "FAIL: run_time_init_test - 1 test failed"
+else
+ echo "FAIL: run_time_init_test - $failcount tests failed"
+fi
+
+exit $failcount
diff --git a/src/lib/log/tests/run_unittests.cc b/src/lib/log/tests/run_unittests.cc
new file mode 100644
index 0000000..b91ce24
--- /dev/null
+++ b/src/lib/log/tests/run_unittests.cc
@@ -0,0 +1,23 @@
+// Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: run_unittests.cc 3020 2010-09-26 03:47:26Z jinmei $
+
+#include <gtest/gtest.h>
+
+int
+main(int argc, char* argv[]) {
+ ::testing::InitGoogleTest(&argc, argv);
+ return (RUN_ALL_TESTS());
+}
diff --git a/src/lib/log/tests/strutil_unittest.cc b/src/lib/log/tests/strutil_unittest.cc
new file mode 100644
index 0000000..6e657f3
--- /dev/null
+++ b/src/lib/log/tests/strutil_unittest.cc
@@ -0,0 +1,216 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: base64_unittest.cc 2549 2010-07-20 19:09:37Z jinmei $
+
+#include <string>
+
+#include <gtest/gtest.h>
+
+#include <log/strutil.h>
+
+using namespace isc;
+using namespace std;
+
+class StringUtilTest : public ::testing::Test {
+protected:
+ StringUtilTest()
+ {
+ }
+};
+
+
+// Check for slash replacement
+
+TEST_F(StringUtilTest, Slash) {
+
+ string instring = "";
+ isc::strutil::normalizeSlash(instring);
+ EXPECT_EQ("", instring);
+
+ instring = "C:\\A\\B\\C.D";
+ isc::strutil::normalizeSlash(instring);
+ EXPECT_EQ("C:/A/B/C.D", instring);
+
+ instring = "// \\ //";
+ isc::strutil::normalizeSlash(instring);
+ EXPECT_EQ("// / //", instring);
+}
+
+// Check that leading and trailing space trimming works
+
+TEST_F(StringUtilTest, Trim) {
+
+ // Empty and full string.
+ EXPECT_EQ("", isc::strutil::trim(""));
+ EXPECT_EQ("abcxyz", isc::strutil::trim("abcxyz"));
+
+ // Trim right-most blanks
+ EXPECT_EQ("ABC", isc::strutil::trim("ABC "));
+ EXPECT_EQ("ABC", isc::strutil::trim("ABC\t\t \n\t"));
+
+ // Left-most blank trimming
+ EXPECT_EQ("XYZ", isc::strutil::trim(" XYZ"));
+ EXPECT_EQ("XYZ", isc::strutil::trim("\t\t \tXYZ"));
+
+ // Right and left, with embedded spaces
+ EXPECT_EQ("MN \t OP", isc::strutil::trim("\t\tMN \t OP \t"));
+}
+
+// Check tokenization. Note that ASSERT_EQ is used to check the size of the
+// returned vector; if not as expected, the following references may be invalid
+// so should not be used.
+
+TEST_F(StringUtilTest, Tokens) {
+ vector<string> result;
+
+ // Default delimiters
+
+ // Degenerate cases
+ result = isc::strutil::tokens(""); // Empty string
+ EXPECT_EQ(0, result.size());
+
+ result = isc::strutil::tokens(" \n "); // String is all delimiters
+ EXPECT_EQ(0, result.size());
+
+ result = isc::strutil::tokens("abc"); // String has no delimiters
+ ASSERT_EQ(1, result.size());
+ EXPECT_EQ(string("abc"), result[0]);
+
+ // String containing leading and/or trailing delimiters, no embedded ones.
+ result = isc::strutil::tokens("\txyz"); // One leading delimiter
+ ASSERT_EQ(1, result.size());
+ EXPECT_EQ(string("xyz"), result[0]);
+
+ result = isc::strutil::tokens("\t \nxyz"); // Multiple leading delimiters
+ ASSERT_EQ(1, result.size());
+ EXPECT_EQ(string("xyz"), result[0]);
+
+ result = isc::strutil::tokens("xyz\n"); // One trailing delimiter
+ ASSERT_EQ(1, result.size());
+ EXPECT_EQ(string("xyz"), result[0]);
+
+ result = isc::strutil::tokens("xyz \t"); // Multiple trailing
+ ASSERT_EQ(1, result.size());
+ EXPECT_EQ(string("xyz"), result[0]);
+
+ result = isc::strutil::tokens("\t xyz \n"); // Leading and trailing
+ ASSERT_EQ(1, result.size());
+ EXPECT_EQ(string("xyz"), result[0]);
+
+ // Embedded delimiters
+ result = isc::strutil::tokens("abc\ndef"); // 2 tokens, one separator
+ ASSERT_EQ(2, result.size());
+ EXPECT_EQ(string("abc"), result[0]);
+ EXPECT_EQ(string("def"), result[1]);
+
+ result = isc::strutil::tokens("abc\t\t\ndef"); // 2 tokens, 3 separators
+ ASSERT_EQ(2, result.size());
+ EXPECT_EQ(string("abc"), result[0]);
+ EXPECT_EQ(string("def"), result[1]);
+
+ result = isc::strutil::tokens("abc\n \tdef\t\tghi");
+ ASSERT_EQ(3, result.size()); // Multiple tokens, many delims
+ EXPECT_EQ(string("abc"), result[0]);
+ EXPECT_EQ(string("def"), result[1]);
+ EXPECT_EQ(string("ghi"), result[2]);
+
+ // Embedded and non-embedded delimiters
+
+ result = isc::strutil::tokens("\t\t \nabc\n \tdef\t\tghi \n\n");
+ ASSERT_EQ(3, result.size()); // Multiple tokens, many delims
+ EXPECT_EQ(string("abc"), result[0]);
+ EXPECT_EQ(string("def"), result[1]);
+ EXPECT_EQ(string("ghi"), result[2]);
+
+ // Non-default delimiter
+ result = isc::strutil::tokens("alpha/beta/ /gamma//delta/epsilon/", "/");
+ ASSERT_EQ(6, result.size());
+ EXPECT_EQ(string("alpha"), result[0]);
+ EXPECT_EQ(string("beta"), result[1]);
+ EXPECT_EQ(string(" "), result[2]);
+ EXPECT_EQ(string("gamma"), result[3]);
+ EXPECT_EQ(string("delta"), result[4]);
+ EXPECT_EQ(string("epsilon"), result[5]);
+
+ // Non-default delimiters (plural)
+ result = isc::strutil::tokens("+*--alpha*beta+ -gamma**delta+epsilon-+**",
+ "*+-");
+ ASSERT_EQ(6, result.size());
+ EXPECT_EQ(string("alpha"), result[0]);
+ EXPECT_EQ(string("beta"), result[1]);
+ EXPECT_EQ(string(" "), result[2]);
+ EXPECT_EQ(string("gamma"), result[3]);
+ EXPECT_EQ(string("delta"), result[4]);
+ EXPECT_EQ(string("epsilon"), result[5]);
+}
+
+// Changing case
+
+TEST_F(StringUtilTest, ChangeCase) {
+ string mixed("abcDEFghiJKLmno123[]{=+--+]}");
+ string upper("ABCDEFGHIJKLMNO123[]{=+--+]}");
+ string lower("abcdefghijklmno123[]{=+--+]}");
+
+ string test = mixed;
+ isc::strutil::lowercase(test);
+ EXPECT_EQ(lower, test);
+
+ test = mixed;
+ isc::strutil::uppercase(test);
+ EXPECT_EQ(upper, test);
+}
+
+// Formatting
+
+TEST_F(StringUtilTest, Formatting) {
+
+ vector<string> args;
+ args.push_back("arg1");
+ args.push_back("arg2");
+ args.push_back("arg3");
+
+ string format1 = "This is a string with no tokens";
+ EXPECT_EQ(format1, isc::strutil::format(format1, args));
+
+ string format2 = ""; // Empty string
+ EXPECT_EQ(format2, isc::strutil::format(format2, args));
+
+ string format3 = " "; // Empty string
+ EXPECT_EQ(format3, isc::strutil::format(format3, args));
+
+ string format4 = "String with %d non-string tokens %lf";
+ EXPECT_EQ(format4, isc::strutil::format(format4, args));
+
+ string format5 = "String with %s correct %s number of tokens %s";
+ string result5 = "String with arg1 correct arg2 number of tokens arg3";
+ EXPECT_EQ(result5, isc::strutil::format(format5, args));
+
+ string format6 = "String with %s too %s few tokens";
+ string result6 = "String with arg1 too arg2 few tokens";
+ EXPECT_EQ(result6, isc::strutil::format(format6, args));
+
+ string format7 = "String with %s too %s many %s tokens %s !";
+ string result7 = "String with arg1 too arg2 many arg3 tokens %s !";
+ EXPECT_EQ(result7, isc::strutil::format(format7, args));
+
+ string format8 = "String with embedded%s%s%stokens";
+ string result8 = "String with embeddedarg1arg2arg3tokens";
+ EXPECT_EQ(result8, isc::strutil::format(format8, args));
+
+ // Handle an empty vector
+ args.clear();
+ string format9 = "%s %s";
+ EXPECT_EQ(format9, isc::strutil::format(format9, args));
+}
diff --git a/src/lib/log/tests/xdebuglevel_unittest.cc b/src/lib/log/tests/xdebuglevel_unittest.cc
new file mode 100644
index 0000000..2cb0952
--- /dev/null
+++ b/src/lib/log/tests/xdebuglevel_unittest.cc
@@ -0,0 +1,205 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: $
+
+#include <iostream>
+#include <string>
+
+#include <gtest/gtest.h>
+
+#include <log4cxx/level.h>
+#include <log/xdebuglevel.h>
+#include <log/dbglevels.h>
+
+/// \brief XDebugLevel (Debug Extension to Level Class)
+///
+/// The class is an extension of the log4cxx Level class; this set of tests
+/// only test the extensions, they do not test the underlying Level class
+/// itself.
+
+using namespace log4cxx;
+
+class XDebugLevelTest : public ::testing::Test {
+protected:
+ XDebugLevelTest()
+ {
+ }
+};
+
+// Check a basic assertion about the numeric values of the debug levels
+
+TEST_F(XDebugLevelTest, NumericValues) {
+ EXPECT_EQ(XDebugLevel::XDEBUG_MIN_LEVEL_INT, Level::DEBUG_INT);
+ EXPECT_EQ(XDebugLevel::XDEBUG_MAX_LEVEL_INT,
+ Level::DEBUG_INT - MAX_DEBUG_LEVEL);
+
+ // ... and check that assumptions used below - that the debug levels
+ // range from 0 to 99 - are valid.
+ EXPECT_EQ(0, MIN_DEBUG_LEVEL);
+ EXPECT_EQ(99, MAX_DEBUG_LEVEL);
+}
+
+
+// Checks that the main function for generating logging level objects from
+// debug levels is working.
+
+TEST_F(XDebugLevelTest, GetExtendedDebug) {
+
+ // Get a debug level of 0. This should be the same as the main DEBUG
+ // level.
+ LevelPtr debug0 = XDebugLevel::getExtendedDebug(0);
+ EXPECT_EQ(std::string("DEBUG"), debug0->toString());
+ EXPECT_EQ(Level::DEBUG_INT, debug0->toInt());
+ EXPECT_TRUE(*Level::getDebug() == *debug0);
+
+ // Get an arbitrary debug level in the allowed range.
+ LevelPtr debug32 = XDebugLevel::getExtendedDebug(32);
+ EXPECT_EQ(std::string("DEBUG32"), debug32->toString());
+ EXPECT_TRUE((XDebugLevel::XDEBUG_MIN_LEVEL_INT - 32) == debug32->toInt());
+
+ // Check that a value outside the range gives the nearest level.
+ LevelPtr debug_more = XDebugLevel::getExtendedDebug(MAX_DEBUG_LEVEL + 1);
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MAX_DEBUG_LEVEL) == *debug_more);
+
+ LevelPtr debug_less = XDebugLevel::getExtendedDebug(MIN_DEBUG_LEVEL - 1);
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MIN_DEBUG_LEVEL) == *debug_less);
+}
+
+
+// Creation of a level from an int - should return the default debug level
+// if outside the range.
+
+TEST_F(XDebugLevelTest, FromIntOneArg) {
+
+ // Check that a valid debug level is as expected
+ LevelPtr debug42 = XDebugLevel::toLevel(
+ XDebugLevel::XDEBUG_MIN_LEVEL_INT - 42);
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(42) == *debug42);
+
+ // ... and that an invalid one returns an object of type debug.
+ LevelPtr debug_invalid = XDebugLevel::toLevel(Level::getInfo()->toInt());
+ EXPECT_TRUE(*Level::getDebug() == *debug_invalid);
+}
+
+
+// Creation of a level from an int - should return the default level
+// if outside the range.
+
+TEST_F(XDebugLevelTest, FromIntTwoArg) {
+
+ // Check that a valid debug level is as expected
+ LevelPtr debug42 = XDebugLevel::toLevel(
+ (XDebugLevel::XDEBUG_MIN_LEVEL_INT - 42), Level::getFatal());
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(42) == *debug42);
+
+ // ... and that an invalid one returns an object of type debug.
+ LevelPtr debug_invalid = XDebugLevel::toLevel(
+ Level::getInfo()->toInt(), Level::getFatal());
+ EXPECT_TRUE(*Level::getFatal() == *debug_invalid);
+}
+
+
+// Creation of a level from a string - should return the default debug level
+// if outside the range.
+
+TEST_F(XDebugLevelTest, FromStringOneArg) {
+
+ // Check that a valid debug levels are as expected
+ LevelPtr debug85 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG85"));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(85) == *debug85);
+
+ LevelPtr debug92 = XDebugLevel::toLevelLS(LOG4CXX_STR("debug92"));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(92) == *debug92);
+
+ LevelPtr debug27 = XDebugLevel::toLevelLS(LOG4CXX_STR("Debug27"));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(27) == *debug27);
+
+ LevelPtr debug0 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG"));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug0);
+
+ // ... and that an invalid one returns an object of type debug (which is
+ // the equivalent of a debug level 0 object).
+ LevelPtr debug_invalid1 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBU"));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug_invalid1);
+
+ LevelPtr debug_invalid2 = XDebugLevel::toLevelLS(LOG4CXX_STR("EBU"));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug_invalid2);
+
+ LevelPtr debug_invalid3 = XDebugLevel::toLevelLS(LOG4CXX_STR(""));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug_invalid3);
+
+ LevelPtr debug_invalid4 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUGTEN"));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug_invalid4);
+
+ LevelPtr debug_invalid5 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG105"));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MAX_DEBUG_LEVEL) ==
+ *debug_invalid5);
+
+ LevelPtr debug_invalid6 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG-7"));
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MIN_DEBUG_LEVEL) ==
+ *debug_invalid6);
+}
+
+
+// Creation of a level from a string - should return the default level
+// if outside the range.
+
+TEST_F(XDebugLevelTest, FromStringTwoArg) {
+
+ // Check that a valid debug levels are as expected
+ LevelPtr debug85 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG85"),
+ Level::getFatal());
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(85) == *debug85);
+
+ LevelPtr debug92 = XDebugLevel::toLevelLS(LOG4CXX_STR("debug92"),
+ Level::getFatal());
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(92) == *debug92);
+
+ LevelPtr debug27 = XDebugLevel::toLevelLS(LOG4CXX_STR("Debug27"),
+ Level::getFatal());
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(27) == *debug27);
+
+ LevelPtr debug0 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG"),
+ Level::getFatal());
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug0);
+
+ // ... and that an invalid one returns an object of type debug (which is
+ // the equivalent of a debug level 0 object).
+ LevelPtr debug_invalid1 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBU"),
+ Level::getFatal());
+ EXPECT_TRUE(*Level::getFatal() == *debug_invalid1);
+
+ LevelPtr debug_invalid2 = XDebugLevel::toLevelLS(LOG4CXX_STR("EBU"),
+ Level::getFatal());
+ EXPECT_TRUE(*Level::getFatal() == *debug_invalid2);
+
+ LevelPtr debug_invalid3 = XDebugLevel::toLevelLS(LOG4CXX_STR(""),
+ Level::getFatal());
+ EXPECT_TRUE(*Level::getFatal() == *debug_invalid3);
+
+ LevelPtr debug_invalid4 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUGTEN"),
+ Level::getFatal());
+ EXPECT_TRUE(*Level::getFatal() == *debug_invalid4);
+
+ LevelPtr debug_invalid5 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG105"),
+ Level::getFatal());
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MAX_DEBUG_LEVEL) ==
+ *debug_invalid5);
+
+ LevelPtr debug_invalid6 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG-7"),
+ Level::getFatal());
+ EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MIN_DEBUG_LEVEL) ==
+ *debug_invalid6);
+}
diff --git a/src/lib/log/xdebuglevel.cc b/src/lib/log/xdebuglevel.cc
new file mode 100644
index 0000000..7dddcff
--- /dev/null
+++ b/src/lib/log/xdebuglevel.cc
@@ -0,0 +1,148 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <cassert>
+#include <algorithm>
+#include <syslog.h>
+#include <string.h>
+#include <boost/lexical_cast.hpp>
+
+#include <xdebuglevel.h>
+#include <dbglevels.h>
+#include <log4cxx/helpers/stringhelper.h>
+
+using namespace log4cxx;
+using namespace log4cxx::helpers;
+
+// Storage for the logging level objects corresponding to each debug level
+
+bool XDebugLevel::dbglevels_unset_ = true;
+LevelPtr XDebugLevel::dbglevels_[NUM_DEBUG_LEVEL];
+
+// Register the class
+
+IMPLEMENT_LOG4CXX_LEVEL(XDebugLevel)
+
+
+// Create Extended Debug Level Objects
+
+LevelPtr
+XDebugLevel::getExtendedDebug(int level) {
+
+ // Initialize the logging levels corresponding to the possible range of
+ // debug if we have not already done so
+ if (dbglevels_unset_) {
+
+ // Asserting that the minimum debug level is zero - so corresponds
+ // to DEBUG_INT - means that the lowest level is set to main DEBUG
+ // level. This means that the existing logging level object can be
+ // used.
+ assert(MIN_DEBUG_LEVEL == 0);
+ dbglevels_[0] = Level::getDebug();
+
+ // Create the logging level objects for the rest of the debug levels.
+ // They are given names of the form DEBUG<debug level> (e.g. DEBUG42).
+ // They will all correspond to a syslog level of DEBUG.
+ for (int i = 1; i < NUM_DEBUG_LEVEL; ++i) {
+ std::string name = std::string("DEBUG") +
+ boost::lexical_cast<std::string>(i);
+ dbglevels_[i] = new XDebugLevel(
+ (XDebugLevel::XDEBUG_MIN_LEVEL_INT - i),
+ LOG4CXX_STR(name.c_str()), LOG_DEBUG);
+ }
+ dbglevels_unset_ = false;
+ }
+
+ // Now get the logging level object asked for. Coerce the debug level to
+ // lie in the acceptable range.
+ int actual = std::max(MIN_DEBUG_LEVEL, std::min(MAX_DEBUG_LEVEL, level));
+
+ // ... and return a pointer to the appropriate logging level object
+ return dbglevels_[actual - MIN_DEBUG_LEVEL];
+}
+
+// Convert an integer (an absolute logging level number, not a debug level) to a
+// logging level object. If it lies outside the valid range, an object
+// corresponding to the minimum debug value is returned.
+
+LevelPtr
+XDebugLevel::toLevel(int val) {
+ return toLevel(val, getExtendedDebug(MIN_DEBUG_LEVEL));
+}
+
+LevelPtr
+XDebugLevel::toLevel(int val, const LevelPtr& defaultLevel) {
+
+ // Note the reversal of the notion of MIN and MAX - see the header file for
+ // details.
+ if ((val >= XDEBUG_MAX_LEVEL_INT) && (val <= XDEBUG_MIN_LEVEL_INT)) {
+ return getExtendedDebug(XDEBUG_MIN_LEVEL_INT - val);
+ }
+ else {
+ return defaultLevel;
+ }
+}
+
+// Convert string passed to a logging level or return default level.
+
+LevelPtr
+XDebugLevel::toLevelLS(const LogString& sArg) {
+ return toLevelLS(sArg, getExtendedDebug(0));
+}
+
+LevelPtr
+XDebugLevel::toLevelLS(const LogString& sArg, const LevelPtr& defaultLevel) {
+ std::string name = sArg; // Get to known type
+ size_t length = name.size(); // Length of the string
+
+ if (length < 5) {
+
+ // String can't possibly start DEBUG so we don't know what it is.
+ return defaultLevel;
+ }
+ else {
+ if (strncasecmp(name.c_str(), "DEBUG", 5) == 0) {
+
+ // String starts "DEBUG" (or "debug" or any case mixture). The
+ // rest of the string -if any - should be a number.
+ if (length == 5) {
+
+ // It is plain "DEBUG". Take this as level 0.
+ return getExtendedDebug(0);
+ }
+ else {
+
+ // Try converting the remainder to an integer. The "5" is
+ // the length of the string "DEBUG". Note that if the number
+ // is outside the rangeof debug levels, it is coerced to the
+ // nearest limit. Thus a level of DEBUG509 will end up as
+ // if DEBUG99 has been specified.
+ try {
+ int level = boost::lexical_cast<int>(name.substr(5));
+ return getExtendedDebug(level);
+ }
+ catch (boost::bad_lexical_cast&) {
+ return defaultLevel;
+ }
+ }
+ }
+ else {
+
+ // Unknown string - return default.
+ return defaultLevel;
+ }
+ }
+}
diff --git a/src/lib/log/xdebuglevel.h b/src/lib/log/xdebuglevel.h
new file mode 100644
index 0000000..4d28ead
--- /dev/null
+++ b/src/lib/log/xdebuglevel.h
@@ -0,0 +1,164 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __XDEBUGLEVEL_H
+#define __XDEBUGLEVEL_H
+
+#include <syslog.h>
+#include <log4cxx/level.h>
+
+#include <dbglevels.h>
+
+namespace log4cxx {
+
+/// \brief Debug Extension to Level Class
+///
+/// Based on the example given in the log4cxx distribution, this extends the
+/// log4cxx Level class to allow 100 debug levels.
+///
+/// First some terminology, as the use of the term "level" gets confusing. The
+/// code and comments here use the term "level" in two contexts:
+///
+/// Logging level: The category of messages to log. By default log4cxx defines
+/// the following logging levels: OFF, FATAL, ERROR, WARNING, INFO, DEBUG,
+/// TRACE, ALL. Within the context of BIND-10, OFF, TRACE and ALL are not used
+/// and the idea of DEBUG has been extended, as will be seen below.
+///
+/// Debug level: This is a number that ranges from 0 to 99 and is used by the
+/// application to control the detail of debug output. A value of 0 gives the
+/// highest-level debug output; a value of 99 gives the most verbose and most
+/// detailed. Debug messages (or whatever debug level) are only ever output
+/// when the logging level is set to DEBUG.
+///
+///
+/// With log4cxx, the various logging levels have a numeric value associated
+/// with them, such that FATAL > ERROR > WARNING etc. This suggests that the
+/// idea of debug levels can be incorporated into the existing logging level
+/// scheme by assigning them appropriate numeric values, i.e.
+///
+/// WARNING > INFO > DEBUG(0) > DEBUG(2) > ... > DEBUG(99)
+///
+/// Setting a numeric level of DEBUG enables the basic messages; setting lower
+/// numeric levels will enable progressively more messages. The lowest debug
+/// level (0) is chosen such that setting the general DEBUG logging level will
+/// automatically select that debug level.
+///
+/// This sub-class is needed because the log4cxx::Level class does not allow
+/// the setting of the numeric value of the current level to something other
+/// than the values enumerated in the class. It creates a set of log4cxx
+/// logging levels to correspond to the various debug levels. These levels have
+/// names in the range DEBUG1 to DEBUG99 (the existing Level DEBUG is used for
+/// a debug level of 0), although they are not used in BIND-10: instead the
+/// BIND-10 Logger class treats the logging levels and debug levels separately
+/// and combines them to choose the underlying log4cxx logging level.
+
+
+/// \brief Debug-Extended Level
+
+class XDebugLevel : public Level {
+ DECLARE_LOG4CXX_LEVEL(XDebugLevel)
+
+ /// Array of pointers to logging level objects, one for each debug level.
+ /// The pointer corresponding to a debug level of 0 points to the DEBUG
+ /// logging level object.
+ static LevelPtr dbglevels_[NUM_DEBUG_LEVEL];
+ static bool dbglevels_unset_;
+
+public:
+
+ // Minimum and maximum debug levels. Note that XDEBUG_MIN_LEVEL_INT is the
+ // number corresponding to the minimum debug level - and is actually larger
+ // that XDEBUG_MAX_LEVEL_INT, the number corresponding to the maximum debug
+ // level.
+ enum {
+ XDEBUG_MIN_LEVEL_INT = Level::DEBUG_INT - MIN_DEBUG_LEVEL,
+ XDEBUG_MAX_LEVEL_INT = Level::DEBUG_INT - MAX_DEBUG_LEVEL
+ };
+
+ /// \brief Constructor
+ ///
+ /// \param level Numeric value of the logging level.
+ /// \param name Name given to this logging level.
+ /// \param syslogEquivalent The category to be used by syslog when it logs
+ /// an event associated with the specified logging level.
+ XDebugLevel(int level, const LogString& name, int syslogEquivalent) :
+ Level(level, name, syslogEquivalent)
+ {}
+
+ /// \brief Create Logging Level Object
+ ///
+ /// Creates a logging level object corresponding to one of the debug levels.
+ ///
+ /// \param dbglevel The debug level, which ranges from MIN_DEBUG_LEVEL to
+ /// MAX_DEBUG_LEVEL. It is coerced to that range if it lies outside it.
+ ///
+ /// \return Pointer to the desired logging level object.
+ static LevelPtr getExtendedDebug(int dbglevel);
+
+ /// \brief Convert Integer to a Logging Level
+ ///
+ /// Returns a logging level object corresponding to the given value (which
+ /// is an absolute value of a logging level - it is not a debug level).
+ /// If the number is invalid, an object of logging level DEBUG (the
+ /// minimum debug logging level) is returned.
+ ///
+ /// \param val Number to convert to a logging level. This is an absolute
+ /// logging level number, not a debug level.
+ ///
+ /// \return Pointer to the desired logging level object.
+ static LevelPtr toLevel(int val);
+
+ /// \brief Convert Integer to a Level
+ ///
+ /// Returns a logging level object corresponding to the given value (which
+ /// is an absolute value of a logging level - it is not a debug level).
+ /// If the number is invalid, the given default is returned.
+ ///
+ /// \param val Number to convert to a logging level. This is an absolute
+ /// logging level number, not a debug level.
+ /// \param defaultLevel Logging level to return if value is not recognised.
+ ///
+ /// \return Pointer to the desired logging level object.
+ static LevelPtr toLevel(int val, const LevelPtr& defaultLevel);
+
+ /// \param Convert String to Logging Level
+ ///
+ /// Returns a logging level object corresponding to the given name. If the
+ /// name is invalid, an object of logging level DEBUG (the minimum debug
+ /// logging level) is returned.
+ ///
+ /// \param sArg Name of the logging level.
+ ///
+ /// \return Pointer to the desired logging level object.
+ static LevelPtr toLevelLS(const LogString& sArg);
+
+ /// \param Convert String to Logging Level
+ ///
+ /// Returns a logging level object corresponding to the given name. If the
+ /// name is invalid, the given default is returned.
+ ///
+ /// \param sArg name of the level.
+ /// \param defaultLevel Logging level to return if name doesn't exist.
+ ///
+ /// \return Pointer to the desired logging level object.
+ static LevelPtr toLevelLS(const LogString& sArg,
+ const LevelPtr& defaultLevel);
+};
+
+} // namespace log4cxx
+
+
+#endif // __XDEBUGLEVEL_H
diff --git a/src/lib/nsas/Makefile.am b/src/lib/nsas/Makefile.am
index 02f4132..a88bd22 100644
--- a/src/lib/nsas/Makefile.am
+++ b/src/lib/nsas/Makefile.am
@@ -35,7 +35,6 @@ libnsas_la_SOURCES += nsas_entry.h nsas_types.h
libnsas_la_SOURCES += zone_entry.cc zone_entry.h
libnsas_la_SOURCES += fetchable.h
libnsas_la_SOURCES += address_request_callback.h
-libnsas_la_SOURCES += resolver_interface.h
libnsas_la_SOURCES += random_number_generator.h
CLEANFILES = *.gcno *.gcda
diff --git a/src/lib/nsas/nameserver_address_store.cc b/src/lib/nsas/nameserver_address_store.cc
index e52047a..0ba9c8e 100644
--- a/src/lib/nsas/nameserver_address_store.cc
+++ b/src/lib/nsas/nameserver_address_store.cc
@@ -56,8 +56,8 @@ namespace nsas {
// hash table, on the assumption that three elements is the longest linear
// search we want to do when looking up names in the hash table.
NameserverAddressStore::NameserverAddressStore(
- boost::shared_ptr<ResolverInterface> resolver, uint32_t zonehashsize,
- uint32_t nshashsize) :
+ boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
+ uint32_t zonehashsize, uint32_t nshashsize) :
zone_hash_(new HashTable<ZoneEntry>(new NsasEntryCompare<ZoneEntry>,
zonehashsize)),
nameserver_hash_(new HashTable<NameserverEntry>(
@@ -78,8 +78,9 @@ namespace {
* called at all to create the object, just call the function.
*/
boost::shared_ptr<ZoneEntry>
-newZone(const boost::shared_ptr<ResolverInterface>* resolver, const string* zone,
- const RRClass* class_code,
+newZone(
+ const boost::shared_ptr<isc::resolve::ResolverInterface>* resolver,
+ const string* zone, const RRClass* class_code,
const boost::shared_ptr<HashTable<NameserverEntry> >* ns_hash,
const boost::shared_ptr<LruList<NameserverEntry> >* ns_lru)
{
diff --git a/src/lib/nsas/nameserver_address_store.h b/src/lib/nsas/nameserver_address_store.h
index b435d12..f183871 100644
--- a/src/lib/nsas/nameserver_address_store.h
+++ b/src/lib/nsas/nameserver_address_store.h
@@ -20,6 +20,8 @@
#include <boost/shared_ptr.hpp>
+#include <resolve/resolver_interface.h>
+
#include "nsas_types.h"
namespace isc {
@@ -62,7 +64,8 @@ public:
/// value of 3001 is the first prime number over 3000, and by implication,
/// there is an assumption that there will be more nameservers than zones
/// in the store.
- NameserverAddressStore(boost::shared_ptr<ResolverInterface> resolver,
+ NameserverAddressStore(
+ boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
uint32_t zonehashsize = 1009, uint32_t nshashsize = 3001);
/// \brief Destructor
@@ -105,7 +108,7 @@ protected:
boost::shared_ptr<LruList<NameserverEntry> > nameserver_lru_;
// The resolver we use
private:
- boost::shared_ptr<ResolverInterface> resolver_;
+ boost::shared_ptr<isc::resolve::ResolverInterface> resolver_;
//}@
};
diff --git a/src/lib/nsas/nameserver_entry.cc b/src/lib/nsas/nameserver_entry.cc
index e48b9fc..53f4233 100644
--- a/src/lib/nsas/nameserver_entry.cc
+++ b/src/lib/nsas/nameserver_entry.cc
@@ -30,12 +30,14 @@
#include <dns/name.h>
#include <dns/rrclass.h>
#include <dns/rrttl.h>
+#include <dns/rcode.h>
+#include <dns/opcode.h>
#include <dns/question.h>
+#include <resolve/resolver_interface.h>
#include "address_entry.h"
#include "nameserver_address.h"
#include "nameserver_entry.h"
-#include "resolver_interface.h"
using namespace asiolink;
using namespace isc::nsas;
@@ -199,7 +201,8 @@ NameserverEntry::setAddressUnreachable(const IOAddress& address) {
* fed back trough this. It holds a shared pointer to the entry so it is not
* destroyed too soon.
*/
-class NameserverEntry::ResolverCallback : public ResolverInterface::Callback {
+class NameserverEntry::ResolverCallback :
+ public isc::resolve::ResolverInterface::Callback {
public:
ResolverCallback(boost::shared_ptr<NameserverEntry> entry,
AddressFamily family, const RRType& type) :
@@ -213,11 +216,22 @@ class NameserverEntry::ResolverCallback : public ResolverInterface::Callback {
* This extracts the addresses out from the response and puts them
* inside the entry. It tries to reuse the address entries from before (if there were any), to keep their RTTs.
*/
- virtual void success(const boost::shared_ptr<AbstractRRset>& response) {
+ virtual void success(MessagePtr response_message) {
time_t now = time(NULL);
Lock lock(entry_->mutex_);
+ // TODO: find the correct RRset, not simply the first
+ if (!response_message ||
+ response_message->getRcode() != isc::dns::Rcode::NOERROR() ||
+ response_message->getRRCount(isc::dns::Message::SECTION_ANSWER) == 0) {
+ failureInternal(lock);
+ }
+
+ isc::dns::RRsetIterator rrsi =
+ response_message->beginSection(isc::dns::Message::SECTION_ANSWER);
+ const isc::dns::RRsetPtr response = *rrsi;
+
vector<AddressEntry> entries;
if (response->getType() != type_ ||
@@ -363,7 +377,8 @@ class NameserverEntry::ResolverCallback : public ResolverInterface::Callback {
};
void
-NameserverEntry::askIP(boost::shared_ptr<ResolverInterface> resolver,
+NameserverEntry::askIP(
+ boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
const RRType& type, AddressFamily family)
{
QuestionPtr question(new Question(Name(getName()), RRClass(getClass()),
@@ -374,7 +389,8 @@ NameserverEntry::askIP(boost::shared_ptr<ResolverInterface> resolver,
}
void
-NameserverEntry::askIP(boost::shared_ptr<ResolverInterface> resolver,
+NameserverEntry::askIP(
+ boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
boost::shared_ptr<Callback> callback, AddressFamily family)
{
Lock lock(mutex_);
diff --git a/src/lib/nsas/nameserver_entry.h b/src/lib/nsas/nameserver_entry.h
index 1024b7b..9a8e542 100644
--- a/src/lib/nsas/nameserver_entry.h
+++ b/src/lib/nsas/nameserver_entry.h
@@ -36,13 +36,14 @@
#include <dns/rrset.h>
#include <dns/rrtype.h>
+#include <resolve/resolver_interface.h>
+
#include "address_entry.h"
#include "asiolink.h"
#include "nsas_types.h"
#include "hash_key.h"
#include "lru_list.h"
#include "fetchable.h"
-#include "resolver_interface.h"
#include "nsas_entry.h"
#include "nameserver_address.h"
@@ -84,7 +85,6 @@ public:
};
class ZoneEntry;
-class ResolverInterface;
/// \brief Nameserver Entry
///
@@ -247,7 +247,7 @@ public:
* even when there are addresses, if there are no addresses for this
* family.
*/
- void askIP(boost::shared_ptr<ResolverInterface> resolver,
+ void askIP(boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
boost::shared_ptr<Callback> callback, AddressFamily family);
//@}
@@ -279,7 +279,7 @@ private:
/// \short Private version that does the actual asking of one address type
///
/// Call unlocked.
- void askIP(boost::shared_ptr<ResolverInterface> resolver,
+ void askIP(boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
const isc::dns::RRType&, AddressFamily);
};
diff --git a/src/lib/nsas/resolver_interface.h b/src/lib/nsas/resolver_interface.h
deleted file mode 100644
index 7f93487..0000000
--- a/src/lib/nsas/resolver_interface.h
+++ /dev/null
@@ -1,78 +0,0 @@
-// Copyright (C) 2010 CZ NIC
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#ifndef __RESOLVER_INTERFACE_H
-#define __RESOLVER_INTERFACE_H
-
-#include <dns/message.h>
-#include <dns/rrset.h>
-
-/**
- * \file resolver_interface.h
- * \short Temporary interface to resolver.
- *
- * This file contains a dummy interface for the resolver, which does not yet
- * exist. When the resolver appears, this file should either wrap its
- * interface or, better, be removed completely.
- */
-
-namespace isc {
-namespace nsas {
-
-/**
- * \short Abstract interface to the resolver.
- *
- * Abstract interface to the resolver. The NameserverAddressStore uses this
- * to ask for addresses. It is here because resolver does not yet exist.
- *
- * It is abstract to allow tests pass dummy resolvers.
- */
-class ResolverInterface {
- public:
- /// \short An abstract callback when data from resolver are ready.
- class Callback {
- public:
- /// \short Some data arrived.
- virtual void success(
- const boost::shared_ptr<isc::dns::AbstractRRset>&
- response) = 0;
- /**
- * \short No data available.
- *
- * \todo Pass some reason.
- */
- virtual void failure() = 0;
- /// \short Virtual destructor, so descendants are cleaned up
- virtual ~ Callback() {};
- };
- typedef boost::shared_ptr<Callback> CallbackPtr;
- /**
- * \short Ask a question.
- *
- * Asks the resolver a question. Once the answer is ready
- * the callback is called.
- *
- * \param question What to ask. The resolver will decide who.
- * \param callback What should happen when the answer is ready.
- */
- virtual void resolve(const isc::dns::QuestionPtr& question,
- const CallbackPtr& callback) = 0;
- /// \short Virtual destructor, so descendants are properly cleaned up
- virtual ~ ResolverInterface() {}
-};
-
-} // namespace nsas
-} // namespace isc
-
-#endif //__RESOLVER_INTERFACE_H
diff --git a/src/lib/nsas/tests/nameserver_address_store_unittest.cc b/src/lib/nsas/tests/nameserver_address_store_unittest.cc
index a2b3d42..95b46a8 100644
--- a/src/lib/nsas/tests/nameserver_address_store_unittest.cc
+++ b/src/lib/nsas/tests/nameserver_address_store_unittest.cc
@@ -86,7 +86,7 @@ public:
* if it is asked for by the resolver.
*/
void lookupAndAnswer(const string& name, const RRClass& class_code,
- boost::shared_ptr<AbstractRRset> authority,
+ RRsetPtr authority,
boost::shared_ptr<AddressRequestCallback> callback)
{
size_t size(resolver_->requests.size());
diff --git a/src/lib/nsas/tests/nameserver_address_unittest.cc b/src/lib/nsas/tests/nameserver_address_unittest.cc
index 2f12f41..35a46f0 100644
--- a/src/lib/nsas/tests/nameserver_address_unittest.cc
+++ b/src/lib/nsas/tests/nameserver_address_unittest.cc
@@ -39,7 +39,7 @@ class NameserverEntrySample {
public:
NameserverEntrySample():
name_("example.org"),
- rrv4_(new BasicRRset(name_, RRClass::IN(), RRType::A(), RRTTL(1200)))
+ rrv4_(new RRset(name_, RRClass::IN(), RRType::A(), RRTTL(1200)))
{
// Add some sample A records
rrv4_->addRdata(ConstRdataPtr(new RdataTest<A>("1.2.3.4")));
@@ -50,7 +50,7 @@ public:
boost::shared_ptr<TestResolver> resolver(new TestResolver);
ns_->askIP(resolver, boost::shared_ptr<Callback>(new Callback), ANY_OK);
resolver->asksIPs(name_, 0, 1);
- resolver->requests[0].second->success(rrv4_);
+ resolver->requests[0].second->success(createResponseMessage(rrv4_));
}
// Return the sample NameserverEntry
@@ -73,7 +73,7 @@ public:
private:
Name name_; ///< Name of the sample
- boost::shared_ptr<BasicRRset> rrv4_; ///< Standard RRSet - IN, A, lowercase name
+ RRsetPtr rrv4_; ///< Standard RRSet - IN, A, lowercase name
boost::shared_ptr<NameserverEntry> ns_; ///< Shared_ptr that points to a NameserverEntry object
class Callback : public NameserverEntry::Callback {
diff --git a/src/lib/nsas/tests/nameserver_entry_unittest.cc b/src/lib/nsas/tests/nameserver_entry_unittest.cc
index 2f5dc2a..9e4cec7 100644
--- a/src/lib/nsas/tests/nameserver_entry_unittest.cc
+++ b/src/lib/nsas/tests/nameserver_entry_unittest.cc
@@ -69,10 +69,10 @@ private:
* as a failure.
*/
void fillSet(boost::shared_ptr<TestResolver> resolver, size_t index,
- boost::shared_ptr<BasicRRset> set)
+ RRsetPtr set)
{
if (set) {
- resolver->requests[index].second->success(set);
+ resolver->requests[index].second->success(createResponseMessage(set));
} else {
resolver->requests[index].second->failure();
}
@@ -80,7 +80,7 @@ private:
protected:
/// Fills the nameserver entry with data trough ask IP
void fillNSEntry(boost::shared_ptr<NameserverEntry> entry,
- boost::shared_ptr<BasicRRset> rrv4, boost::shared_ptr<BasicRRset> rrv6)
+ RRsetPtr rrv4, RRsetPtr rrv6)
{
// Prepare data to run askIP
boost::shared_ptr<TestResolver> resolver(new TestResolver);
@@ -212,13 +212,13 @@ TEST_F(NameserverEntryTest, ExpirationTime) {
// Test where there is a single TTL
boost::shared_ptr<NameserverEntry> alpha(new NameserverEntry(EXAMPLE_CO_UK,
RRClass::IN()));
- fillNSEntry(alpha, rrv4_, boost::shared_ptr<BasicRRset>());
+ fillNSEntry(alpha, rrv4_, RRsetPtr());
expiration = alpha->getExpiration();
EXPECT_EQ(expiration, curtime + rrv4_->getTTL().getValue());
boost::shared_ptr<NameserverEntry> beta(new NameserverEntry(EXAMPLE_CO_UK,
RRClass::IN()));
- fillNSEntry(beta, boost::shared_ptr<BasicRRset>(), rrv6_);
+ fillNSEntry(beta, RRsetPtr(), rrv6_);
expiration = beta->getExpiration();
EXPECT_EQ(expiration, curtime + rrv6_->getTTL().getValue());
@@ -237,7 +237,7 @@ TEST_F(NameserverEntryTest, ExpirationTime) {
boost::shared_ptr<NameserverEntry> delta(new NameserverEntry(EXAMPLE_CO_UK,
RRClass::IN()));
- fillNSEntry(delta, rrv4_, boost::shared_ptr<BasicRRset>());
+ fillNSEntry(delta, rrv4_, RRsetPtr());
EXPECT_GT(delta->getExpiration(), rrv4_->getTTL().getValue());
}
@@ -347,9 +347,9 @@ TEST_F(NameserverEntryTest, DirectAnswer) {
resolver->addPresetAnswer(Question(Name(EXAMPLE_CO_UK), RRClass::IN(),
RRType::AAAA()), rrv6_);
resolver->addPresetAnswer(Question(Name(EXAMPLE_NET), RRClass::IN(),
- RRType::A()), boost::shared_ptr<AbstractRRset>());
+ RRType::A()), RRsetPtr());
resolver->addPresetAnswer(Question(Name(EXAMPLE_NET), RRClass::IN(),
- RRType::AAAA()), boost::shared_ptr<AbstractRRset>());
+ RRType::AAAA()), RRsetPtr());
// A successfull test first
entry->askIP(resolver, callback, ANY_OK);
diff --git a/src/lib/nsas/tests/nsas_test.h b/src/lib/nsas/tests/nsas_test.h
index a0b86df..b4446a4 100644
--- a/src/lib/nsas/tests/nsas_test.h
+++ b/src/lib/nsas/tests/nsas_test.h
@@ -25,18 +25,33 @@
#include <config.h>
+#include <dns/message.h>
#include <dns/buffer.h>
#include <dns/rdata.h>
#include <dns/rrtype.h>
#include <dns/rrttl.h>
+#include <dns/opcode.h>
+#include <dns/rcode.h>
#include <dns/messagerenderer.h>
#include <dns/rdataclass.h>
+#include <resolve/resolver_interface.h>
#include "../nsas_entry.h"
-#include "../resolver_interface.h"
using namespace isc::dns::rdata;
using namespace isc::dns;
+namespace {
+ MessagePtr
+ createResponseMessage(RRsetPtr answer_rrset)
+ {
+ MessagePtr response(new Message(Message::RENDER));
+ response->setOpcode(Opcode::QUERY());
+ response->setRcode(Rcode::NOERROR());
+ response->addRRset(Message::SECTION_ANSWER, answer_rrset);
+ return response;
+ }
+}
+
namespace isc {
namespace dns {
@@ -217,13 +232,13 @@ using namespace std;
* This pretends to be a resolver. It stores the queries and
* they can be answered.
*/
-class TestResolver : public isc::nsas::ResolverInterface {
+class TestResolver : public isc::resolve::ResolverInterface {
private:
bool checkIndex(size_t index) {
return (requests.size() > index);
}
- typedef std::map<isc::dns::Question, boost::shared_ptr<AbstractRRset> >
+ typedef std::map<isc::dns::Question, RRsetPtr >
PresetAnswers;
PresetAnswers answers_;
public:
@@ -235,7 +250,7 @@ class TestResolver : public isc::nsas::ResolverInterface {
requests.push_back(Request(q, c));
} else {
if (it->second) {
- c->success(it->second);
+ c->success(createResponseMessage(it->second));
} else {
c->failure();
}
@@ -248,7 +263,7 @@ class TestResolver : public isc::nsas::ResolverInterface {
* it goes to requests and you can answer later.
*/
void addPresetAnswer(const isc::dns::Question& question,
- boost::shared_ptr<AbstractRRset> answer)
+ RRsetPtr answer)
{
answers_[question] = answer;
}
@@ -308,11 +323,11 @@ class TestResolver : public isc::nsas::ResolverInterface {
RRsetPtr set(new RRset(name, RRClass::IN(),
type, RRTTL(TTL)));
set->addRdata(rdata);
- requests[index].second->success(set);
+ requests[index].second->success(createResponseMessage(set));
}
void provideNS(size_t index,
- boost::shared_ptr<AbstractRRset> nameservers)
+ RRsetPtr nameservers)
{
if (index >= requests.size()) {
throw NoSuchRequest();
@@ -322,7 +337,7 @@ class TestResolver : public isc::nsas::ResolverInterface {
{
throw DifferentRequest();
}
- requests[index].second->success(nameservers);
+ requests[index].second->success(createResponseMessage(nameservers));
}
};
diff --git a/src/lib/nsas/tests/zone_entry_unittest.cc b/src/lib/nsas/tests/zone_entry_unittest.cc
index ca19110..8a3c6f2 100644
--- a/src/lib/nsas/tests/zone_entry_unittest.cc
+++ b/src/lib/nsas/tests/zone_entry_unittest.cc
@@ -42,7 +42,8 @@ namespace {
/// \brief Inherited version with access into its internals for tests
class InheritedZoneEntry : public ZoneEntry {
public:
- InheritedZoneEntry(boost::shared_ptr<ResolverInterface> resolver,
+ InheritedZoneEntry(
+ boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
const std::string& name, const RRClass& class_code,
boost::shared_ptr<HashTable<NameserverEntry> > nameserver_table,
boost::shared_ptr<LruList<NameserverEntry> > nameserver_lru) :
@@ -459,7 +460,7 @@ TEST_F(ZoneEntryTest, DirectAnswer) {
// One unsuccessfull attempt, nameservers fail
resolver_->addPresetAnswer(Question(Name(EXAMPLE_CO_UK), RRClass::IN(),
- RRType::NS()), boost::shared_ptr<AbstractRRset>());
+ RRType::NS()), RRsetPtr());
zone->addCallback(callback_, ANY_OK);
EXPECT_EQ(0, callback_->successes_.size());
EXPECT_EQ(1, callback_->unreachable_count_);
@@ -493,9 +494,9 @@ TEST_F(ZoneEntryTest, DirectAnswer) {
callback_->successes_.clear();
// Now, pretend we do not have IP addresses
resolver_->addPresetAnswer(Question(ns_name, RRClass::IN(), RRType::A()),
- boost::shared_ptr<AbstractRRset>());
+ RRsetPtr());
resolver_->addPresetAnswer(Question(ns_name, RRClass::IN(),
- RRType::AAAA()), boost::shared_ptr<AbstractRRset>());
+ RRType::AAAA()), RRsetPtr());
// Get another zone and ask it again. It should fail.
// Clean the table first, though, so it does not find the old nameserver
nameserver_table_->remove(HashKey(ns_name.toText(), RRClass::IN()));
diff --git a/src/lib/nsas/zone_entry.cc b/src/lib/nsas/zone_entry.cc
index 4009fe7..395b06c 100644
--- a/src/lib/nsas/zone_entry.cc
+++ b/src/lib/nsas/zone_entry.cc
@@ -22,6 +22,7 @@
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
#include <dns/rrttl.h>
+#include <dns/rcode.h>
#include <dns/rdataclass.h>
using namespace std;
@@ -32,7 +33,8 @@ using namespace dns;
namespace nsas {
-ZoneEntry::ZoneEntry(boost::shared_ptr<ResolverInterface> resolver,
+ZoneEntry::ZoneEntry(
+ boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
const std::string& name, const isc::dns::RRClass& class_code,
boost::shared_ptr<HashTable<NameserverEntry> > nameserver_table,
boost::shared_ptr<LruList<NameserverEntry> > nameserver_lru) :
@@ -73,7 +75,8 @@ newNs(const std::string* name, const RRClass* class_code) {
* code. It manipulates directly ZoneEntry's data members, locks it and like
* that. Mostly eliminates C++ bad design of missing lambda functions.
*/
-class ZoneEntry::ResolverCallback : public ResolverInterface::Callback {
+class ZoneEntry::ResolverCallback :
+ public isc::resolve::ResolverInterface::Callback {
public:
/// \short Constructor. Pass "this" zone entry
ResolverCallback(boost::shared_ptr<ZoneEntry> entry) :
@@ -90,8 +93,21 @@ class ZoneEntry::ResolverCallback : public ResolverInterface::Callback {
* examining them and seeing if some addresses are already there
* and to ask for the rest of them.
*/
- virtual void success(const boost::shared_ptr<AbstractRRset>& answer) {
+ virtual void success(MessagePtr response_message) {
Lock lock(entry_->mutex_);
+
+ // TODO: find the correct RRset, not simply the first
+ if (!response_message ||
+ response_message->getRcode() != isc::dns::Rcode::NOERROR() ||
+ response_message->getRRCount(isc::dns::Message::SECTION_ANSWER) == 0) {
+ // todo: define this
+ failureInternal(300);
+ }
+
+ isc::dns::RRsetIterator rrsi =
+ response_message->beginSection(isc::dns::Message::SECTION_ANSWER);
+ const isc::dns::RRsetPtr answer = *rrsi;
+
RdataIteratorPtr iterator(answer->getRdataIterator());
// If there are no data
if (iterator->isLast()) {
diff --git a/src/lib/nsas/zone_entry.h b/src/lib/nsas/zone_entry.h
index 8ae36f7..c819692 100644
--- a/src/lib/nsas/zone_entry.h
+++ b/src/lib/nsas/zone_entry.h
@@ -36,11 +36,12 @@
#include <dns/rrset.h>
+#include <resolve/resolver_interface.h>
+
#include "hash_key.h"
#include "nsas_entry.h"
#include "asiolink.h"
#include "fetchable.h"
-#include "resolver_interface.h"
#include "nsas_types.h"
#include "random_number_generator.h"
@@ -73,10 +74,14 @@ public:
* \param name Name of the zone
* \param class_code Class of this zone (zones of different classes have
* different objects.
+ * \param nameserver_table Hashtable of NameServerEntry objects for
+ * this zone
+ * \param namesever_lru LRU for the nameserver entries
* \todo Move to cc file, include the lookup (if NSAS uses resolver for
* everything)
*/
- ZoneEntry(boost::shared_ptr<ResolverInterface> resolver,
+ ZoneEntry(
+ boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
const std::string& name, const isc::dns::RRClass& class_code,
boost::shared_ptr<HashTable<NameserverEntry> > nameserver_table,
boost::shared_ptr<LruList<NameserverEntry> > nameserver_lru);
@@ -151,7 +156,7 @@ private:
void process(AddressFamily family,
const boost::shared_ptr<NameserverEntry>& nameserver);
// Resolver we use
- boost::shared_ptr<ResolverInterface> resolver_;
+ boost::shared_ptr<isc::resolve::ResolverInterface> resolver_;
// We store the nameserver table and lru, so we can look up when there's
// update
boost::shared_ptr<HashTable<NameserverEntry> > nameserver_table_;
diff --git a/src/lib/python/isc/config/ccsession.py b/src/lib/python/isc/config/ccsession.py
index 38e9349..e7fdb86 100644
--- a/src/lib/python/isc/config/ccsession.py
+++ b/src/lib/python/isc/config/ccsession.py
@@ -24,17 +24,17 @@
# made there as well
"""Classes and functions for handling configuration and commands
- This module provides the ModuleCCSession and UICCSession classes,
- as well as a set of utility functions to create and parse messages
- related to commands and configuration
+ This module provides the ModuleCCSession and UIModuleCCSession
+ classes, as well as a set of utility functions to create and parse
+ messages related to commands and configuration
Modules should use the ModuleCCSession class to connect to the
configuration manager, and receive updates and commands from
other modules.
- Configuration user interfaces should use the UICCSession to connect
- to b10-cmdctl, and receive and send configuration and commands
- through that to the configuration manager.
+ Configuration user interfaces should use the UIModuleCCSession
+ to connect to b10-cmdctl, and receive and send configuration and
+ commands through that to the configuration manager.
"""
from isc.cc import Session
diff --git a/src/lib/resolve/Makefile.am b/src/lib/resolve/Makefile.am
new file mode 100644
index 0000000..c9f1326
--- /dev/null
+++ b/src/lib/resolve/Makefile.am
@@ -0,0 +1,18 @@
+SUBDIRS = . tests
+
+AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/dns -I$(top_builddir)/src/lib/dns
+AM_CPPFLAGS += $(BOOST_INCLUDES)
+AM_CPPFLAGS += $(SQLITE_CFLAGS)
+
+AM_CXXFLAGS = $(B10_CXXFLAGS)
+
+CLEANFILES = *.gcno *.gcda
+
+lib_LTLIBRARIES = libresolve.la
+libresolve_la_SOURCES = resolve.h resolve.cc
+libresolve_la_SOURCES += resolver_interface.h
+libresolve_la_SOURCES += resolver_callback.h resolver_callback.cc
+libresolve_la_SOURCES += response_classifier.cc response_classifier.h
+libresolve_la_LIBADD = $(top_builddir)/src/lib/dns/libdns++.la
+libresolve_la_LIBADD += $(top_builddir)/src/lib/exceptions/libexceptions.la
diff --git a/src/lib/resolve/resolve.cc b/src/lib/resolve/resolve.cc
new file mode 100644
index 0000000..312c89d
--- /dev/null
+++ b/src/lib/resolve/resolve.cc
@@ -0,0 +1,58 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <resolve/resolve.h>
+
+using namespace isc::dns;
+
+namespace {
+ class SectionInserter {
+ public:
+ SectionInserter(MessagePtr message, const Message::Section sect) :
+ message_(message), section_(sect)
+ {}
+ void operator()(const RRsetPtr rrset) {
+ message_->addRRset(section_, rrset, true);
+ }
+ MessagePtr message_;
+ const Message::Section section_;
+ };
+}
+
+namespace isc {
+namespace resolve {
+
+void
+makeErrorMessage(MessagePtr answer_message,
+ const Rcode& error_code)
+{
+ answer_message->clearSection(Message::SECTION_ANSWER);
+ answer_message->clearSection(Message::SECTION_AUTHORITY);
+ answer_message->clearSection(Message::SECTION_ADDITIONAL);
+
+ answer_message->setRcode(error_code);
+}
+
+void copyResponseMessage(const Message& source, MessagePtr target) {
+ target->setRcode(source.getRcode());
+
+ target->appendSection(Message::SECTION_ANSWER, source);
+ target->appendSection(Message::SECTION_AUTHORITY, source);
+ target->appendSection(Message::SECTION_ADDITIONAL, source);
+}
+
+
+} // namespace resolve
+} // namespace isc
+
diff --git a/src/lib/resolve/resolve.h b/src/lib/resolve/resolve.h
new file mode 100644
index 0000000..b08c30c
--- /dev/null
+++ b/src/lib/resolve/resolve.h
@@ -0,0 +1,60 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef _ISC_RESOLVE_H
+#define _ISC_RESOLVE_H 1
+
+/// This file includes all other libresolve headers, and provides
+/// several helper functions used in resolving.
+
+#include <resolve/resolver_interface.h>
+#include <resolve/resolver_callback.h>
+#include <resolve/response_classifier.h>
+
+#include <dns/rcode.h>
+
+namespace isc {
+namespace resolve {
+
+/// \brief Create an error response
+///
+/// Clears the answer, authority, and additional section of the
+/// given MessagePtr and sets the given error code
+///
+/// Notes: Assuming you have already done initial preparations
+/// on the given answer message (copy the opcode, qid and question
+/// section), you can simply use this to create an error response.
+///
+/// \param answer_message The message to clear and place the error in
+/// \param question The question to add to the
+/// \param error_code The error Rcode
+void makeErrorMessage(isc::dns::MessagePtr answer_message,
+ const isc::dns::Rcode& error_code);
+
+/// \brief Copies the parts relevant for a DNS response to the
+/// target message
+///
+/// This adds all the RRsets in the answer, authority and
+/// additional sections to the target, as well as the response
+/// code
+/// \param source The Message to copy the data from
+/// \param target The Message to copy the data to
+void copyResponseMessage(const isc::dns::Message& source,
+ isc::dns::MessagePtr target);
+
+
+} // namespace resolve
+} // namespace isc
+
+#endif // ISC_RESOLVE_H_
diff --git a/src/lib/resolve/resolver_callback.cc b/src/lib/resolve/resolver_callback.cc
new file mode 100644
index 0000000..c0db55e
--- /dev/null
+++ b/src/lib/resolve/resolver_callback.cc
@@ -0,0 +1,36 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <resolve/resolver_callback.h>
+
+namespace isc {
+namespace resolve {
+
+void
+ResolverCallbackServer::success(const isc::dns::MessagePtr response)
+{
+ // ignore our response here
+ (void)response;
+
+ server_->resume(true);
+}
+
+void
+ResolverCallbackServer::failure()
+{
+ server_->resume(false);
+}
+
+} // namespace resolve
+} // namespace isc
diff --git a/src/lib/resolve/resolver_callback.h b/src/lib/resolve/resolver_callback.h
new file mode 100644
index 0000000..f69d8a7
--- /dev/null
+++ b/src/lib/resolve/resolver_callback.h
@@ -0,0 +1,48 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef _ISC_RESOLVER_CALLBACK_H
+#define _ISC_RESOLVER_CALLBACK_H 1
+
+#include <asiolink/asiolink.h>
+#include <dns/message.h>
+
+namespace isc {
+namespace resolve {
+
+/// \short Standard Callback for sendQuery for DNSServer instances
+///
+/// This is a standard ResolverInterface::Callback implementation
+/// that is used by Resolver; when RunningQuery finishes and has either
+/// some data or an error, DNSServer::resume() will be called.
+///
+/// This class will ignore the response MessagePtr in the callback,
+/// as the server itself should also have a reference.
+class ResolverCallbackServer : public ResolverInterface::Callback {
+public:
+ ResolverCallbackServer(asiolink::DNSServer* server) :
+ server_(server->clone()) {}
+ ~ResolverCallbackServer() { delete server_; };
+
+ void success(const isc::dns::MessagePtr response);
+ void failure();
+
+private:
+ asiolink::DNSServer* server_;
+};
+
+} //namespace resolve
+} //namespace isc
+
+#endif // ISC_RESOLVER_CALLBACK_H_
diff --git a/src/lib/resolve/resolver_interface.h b/src/lib/resolve/resolver_interface.h
new file mode 100644
index 0000000..e08bb64
--- /dev/null
+++ b/src/lib/resolve/resolver_interface.h
@@ -0,0 +1,98 @@
+// Copyright (C) 2010 CZ NIC
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __RESOLVER_INTERFACE_H
+#define __RESOLVER_INTERFACE_H
+
+#include <dns/message.h>
+
+///
+/// \file resolver_interface.h
+/// \short Interface to resolver.
+///
+/// This file contains an interface for the resolver. By subclassing
+/// this abstract interface, other parts of the system can ask the
+/// resolver to do some resolving too.
+///
+/// This is done by creating a subclass of ResolverInterface::Callback,
+/// which defines what to do with the result, and then calling resolve()
+/// on the ResolverInterface implementation.
+///
+/// One default Callback subclass is provided right now, in
+/// resolver_callback.[h|cc], which calls resumse() on a given DNSServer
+///
+
+namespace isc {
+namespace resolve {
+
+///
+/// \short Abstract interface to the resolver.
+///
+/// Abstract interface to the resolver. The NameserverAddressStore uses this
+/// to ask for addresses. It is here because resolver does not yet exist.
+///
+/// It is abstract to allow tests pass dummy resolvers.
+///
+class ResolverInterface {
+ public:
+ /// \short An abstract callback for when the resolver is done.
+ ///
+ /// You can pass an instance of a subclass of this (as a
+ /// CallbackPtr) to RecursiveQuery::sendQuery(), and when it
+ /// is done, it will either call success() if there is an
+ /// answer MessagePtr, or failure(), if the resolver was not
+ /// able to find anything.
+ ///
+ /// Note that a result Message does not necessarily contain
+ /// the actual answer (it could be a noerror/nodata response).
+ class Callback {
+ public:
+ /// \short Some data arrived.
+ virtual void success(const isc::dns::MessagePtr response) = 0;
+
+ ///
+ ///\short No data available.
+ ///
+ ///\todo Provide error reason (result of the
+ /// classification call, for instance? We'd also
+ /// need some way to say 'everything times out')
+ ///
+ virtual void failure() = 0;
+
+ /// \short Virtual destructor, so descendants are cleaned up
+ virtual ~Callback() {};
+ };
+
+ typedef boost::shared_ptr<Callback> CallbackPtr;
+
+ ///
+ ///\short Ask a question.
+ ///
+ /// Asks the resolver a question. Once the answer is ready
+ /// the callback is called.
+ ///
+ /// \param question What to ask. The resolver will decide who.
+ /// \param callback What should happen when the answer is ready.
+ ///
+ virtual void resolve(const isc::dns::QuestionPtr& question,
+ const CallbackPtr& callback) = 0;
+
+ /// \short Virtual destructor, so descendants are properly cleaned up
+ virtual ~ ResolverInterface() {}
+};
+
+} // namespace nsas
+} // namespace isc
+
+#endif //__RESOLVER_INTERFACE_H
diff --git a/src/lib/resolve/response_classifier.cc b/src/lib/resolve/response_classifier.cc
new file mode 100644
index 0000000..45e9cbc
--- /dev/null
+++ b/src/lib/resolve/response_classifier.cc
@@ -0,0 +1,274 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <cstddef>
+#include <vector>
+
+#include <resolve/response_classifier.h>
+#include <dns/name.h>
+#include <dns/opcode.h>
+#include <dns/rcode.h>
+#include <dns/rrset.h>
+
+using namespace isc::dns;
+using namespace std;
+
+namespace isc {
+namespace resolve {
+
+// Classify the response in the "message" object.
+
+ResponseClassifier::Category ResponseClassifier::classify(
+ const Question& question, const Message& message,
+ Name& cname_target, unsigned int& cname_count, bool tcignore
+ )
+{
+ // Check header bits
+ if (!message.getHeaderFlag(Message::HEADERFLAG_QR)) {
+ return (NOTRESPONSE); // Query-response bit not set in the response
+ }
+
+ // We only recognise responses to queries here
+ if (message.getOpcode() != Opcode::QUERY()) {
+ return (OPCODE);
+ }
+
+ // Apparently have a response. There must be a single question in it...
+ const vector<QuestionPtr> msgquestion(message.beginQuestion(),
+ message.endQuestion());
+ if (msgquestion.size() != 1) {
+ return (NOTONEQUEST); // Not one question in response question section
+ }
+
+ // ... and the question should be equal to the question given.
+ // XXX: This means that "question" may not be the question sent by the
+ // client. In the case of a CNAME response, the qname of subsequent
+ // questions needs to be altered.
+ if (question != *(msgquestion[0])) {
+ return (MISMATQUEST);
+ }
+
+ // Check for Rcode-related errors.
+ const Rcode& rcode = message.getRcode();
+ if (rcode != Rcode::NOERROR()) {
+ if (rcode == Rcode::NXDOMAIN()) {
+
+ // No such domain. According to RFC2308, the domain referred to by
+ // the QNAME does not exist, although there may be a CNAME in the
+ // answer section and there may be an SOA and/or NS RRs in the
+ // authority section (ignoring any DNSSEC RRs for now).
+ //
+ // Note the "may". There may not be anything. Also, note that if
+ // there is a CNAME in the answer section, the authoritative server
+ // has verified that the name given in the CNAME's RDATA field does
+ // not exist. And that if a CNAME is returned in the answer, then
+ // the QNAME of the RRs in the authority section will refer to the
+ // authority for the CNAME's RDATA and not to the original question.
+ //
+ // Without doing further classification, it is sufficient to say
+ // that if an NXDOMAIN is received, there was no translation of the
+ // QNAME available.
+ return (NXDOMAIN); // Received NXDOMAIN from parent.
+
+ } else {
+
+ // Not NXDOMAIN but not NOERROR either. Must be an RCODE-related
+ // error.
+ return (RCODE);
+ }
+ }
+
+ // All seems OK and we can start looking at the content. However, one
+ // more header check remains - was the response truncated? If so, we'll
+ // probably want to re-query over TCP. However, in some circumstances we
+ // might want to go with what we have. So give the caller the option of
+ // ignoring the TC bit.
+ if (message.getHeaderFlag(Message::HEADERFLAG_TC) && (!tcignore)) {
+ return (TRUNCATED);
+ }
+
+ // By the time we get here, we're assured that the packet format is correct.
+ // We now need to decide as to whether it is an answer, a CNAME, or a
+ // referral. For this, we need to inspect the contents of the answer
+ // and authority sections.
+ const vector<RRsetPtr> answer(
+ message.beginSection(Message::SECTION_ANSWER),
+ message.endSection(Message::SECTION_ANSWER)
+ );
+ const vector<RRsetPtr> authority(
+ message.beginSection(Message::SECTION_AUTHORITY),
+ message.endSection(Message::SECTION_AUTHORITY)
+ );
+
+ // If there is nothing in the answer section, it is a referral - unless
+ // there is nothing in the authority section
+ if (answer.empty()) {
+ if (authority.empty()) {
+ return (EMPTY);
+ } else {
+ return (REFERRAL);
+ }
+ }
+
+ // Look at two cases - one RRset in the answer and multiple RRsets in
+ // the answer.
+ if (answer.size() == 1) {
+
+ // Does the name and class of the answer match that of the question?
+ if ((answer[0]->getName() == question.getName()) &&
+ (answer[0]->getClass() == question.getClass())) {
+
+ // It does. How about the type of the response? The response
+ // is an answer if the type matches that of the question, or if the
+ // question was for type ANY. It is a CNAME reply if the answer
+ // type is CNAME. And it is an error for anything else.
+ if ((answer[0]->getType() == question.getType()) ||
+ (question.getType() == RRType::ANY())) {
+ return (ANSWER);
+ } else if (answer[0]->getType() == RRType::CNAME()) {
+ RdataIteratorPtr it = answer[0]->getRdataIterator();
+ cname_target = Name(it->getCurrent().toText());
+ ++cname_count;
+ return (CNAME);
+ } else {
+ return (INVTYPE);
+ }
+ }
+ else {
+
+ // Either the name and/or class of the reply don't match that of
+ // the question.
+ return (INVNAMCLASS);
+ }
+ }
+
+ // There are multiple RRsets in the answer. They should all have the same
+ // QCLASS, else there is some error in the response.
+ for (int i = 1; i < answer.size(); ++i) {
+ if (answer[0]->getClass() != answer[i]->getClass()) {
+ return (MULTICLASS);
+ }
+ }
+
+ // If the request type was ANY and they all have the same QNAME, we have
+ // an answer. But if they don't have the same QNAME, we must have an error;
+ // the only way we could get different QNAMES in an answer is if one were a
+ // CNAME - in which case there should no other record types at that QNAME.
+ if (question.getType() == RRType::ANY()) {
+ bool all_same = true;
+ for (int i = 1; (i < answer.size()) && all_same; ++i) {
+ all_same = (answer[0]->getName() == answer[i]->getName());
+ }
+ if (all_same) {
+ return (ANSWER);
+ } else {
+ return (EXTRADATA);
+ }
+ }
+
+ // Multiple RRs in the answer, and not all the same QNAME. This
+ // is either an answer, a CNAME (in either case, there could be multiple
+ // CNAMEs in the chain) or an error.
+ //
+ // So we need to follow the CNAME chain to resolve this. For this to work:
+ //
+ // a) There must be one RR that matches the name, class and type of
+ // the question, and this is a CNAME.
+ // b) The CNAME chain is followed until the end of the chain does not
+ // exist (answer is a CNAME) or it is not of type CNAME (ANSWER).
+ //
+ // In the latter case, if there are additional RRs, it must be an error.
+
+ vector<RRsetPtr> ansrrset(answer);
+ vector<int> present(ansrrset.size(), 1);
+ return cnameChase(question.getName(), question.getType(),
+ cname_target, cname_count,
+ ansrrset, present, ansrrset.size());
+}
+
+// Search the CNAME chain.
+ResponseClassifier::Category ResponseClassifier::cnameChase(
+ const Name& qname, const RRType& qtype,
+ Name& cname_target, unsigned int& cname_count,
+ vector<RRsetPtr>& ansrrset, vector<int>& present, size_t size)
+{
+ // Search through the vector of RRset pointers until we find one with the
+ // right QNAME.
+ for (int i = 0; i < ansrrset.size(); ++i) {
+ if (present[i]) {
+
+ // This entry has not been logically removed, so look at it.
+ if (ansrrset[i]->getName() == qname) {
+
+ // QNAME match. If this RRset is a CNAME, remove it from
+ // further consideration. If nothing is left, the end of the
+ // chain is a CNAME so this is a CNAME. Otherwise replace
+ // the name with the RDATA of the CNAME and call ourself
+ // recursively.
+ if (ansrrset[i]->getType() == RRType::CNAME()) {
+
+ // Don't consider it in the next iteration (although we
+ // can still access it for now).
+ present[i] = 0;
+ --size;
+ if (size == 0) {
+ RdataIteratorPtr it = ansrrset[i]->getRdataIterator();
+ cname_target = Name(it->getCurrent().toText());
+ return (CNAME);
+ } else {
+ if (ansrrset[i]->getRdataCount() != 1) {
+
+ // Multiple RDATA for a CNAME? This is invalid.
+
+ return (NOTSINGLE);
+ }
+ RdataIteratorPtr it = ansrrset[i]->getRdataIterator();
+ Name newname(it->getCurrent().toText());
+
+ // Increase CNAME count, and continue
+ return cnameChase(newname, qtype, cname_target,
+ ++cname_count, ansrrset, present, size);
+ }
+
+ } else {
+
+ // We've got here because the element is not a CNAME. If
+ // this is the last element and the type is the one we are
+ // after, we've found the answer, or it is an error. If
+ // there is more than one RRset left in the list we are
+ // searching, we have extra data in the answer.
+ if (ansrrset[i]->getType() == qtype) {
+ if (size == 1) {
+ return (ANSWERCNAME);
+ } else {
+ return (EXTRADATA);
+ }
+ }
+ return (INVTYPE);
+ }
+ }
+ }
+ }
+
+ // We get here if we've dropped off the end of the list without finding the
+ // QNAME we are looking for. This means that the CNAME chain has ended
+ // but there are additional RRsets in the data.
+
+ return (EXTRADATA);
+}
+
+} // namespace resolve
+} // namespace isc
diff --git a/src/lib/resolve/response_classifier.h b/src/lib/resolve/response_classifier.h
new file mode 100644
index 0000000..bee0628
--- /dev/null
+++ b/src/lib/resolve/response_classifier.h
@@ -0,0 +1,156 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __RESPONSE_CLASSIFIER_H
+#define __RESPONSE_CLASSIFIER_H
+
+#include <cstddef>
+
+#include <dns/question.h>
+#include <dns/message.h>
+#include <dns/question.h>
+
+#define RESOLVER_MAX_CNAME_CHAIN 16
+
+namespace isc {
+namespace resolve {
+
+/// \brief Classify Server Response
+///
+/// This class is used in the recursive server. It is passed an answer received
+/// from an upstream server and categorises it.
+///
+/// TODO: The code here does not take into account any EDNS0 fields.
+
+class ResponseClassifier {
+public:
+
+ /// \brief Category of Answer
+ ///
+ /// In the valid answers, not the distinction between REFERRAL and CNAME.
+ /// A REFERRAL answer means that the answer section of the message is
+ /// empty, but there is something in the authority section. A CNAME means
+ /// that the answer section contains one or more CNAMES in a chain that
+ /// do not end with a non-CNAME RRset.
+ enum Category {
+
+ // Codes indicating that a message is valid.
+
+ ANSWER, ///< Response contains the answer
+ ANSWERCNAME, ///< Response was a CNAME chain ending in an answer
+ CNAME, ///< Response was a CNAME
+ NXDOMAIN, ///< Response was an NXDOMAIN
+ REFERRAL, ///< Response contains a referral
+
+ // Codes indicating that a message is invalid. Note that the error()
+ // method relies on these appearing after the "message valid" codes.
+
+ EMPTY, ///< No answer or authority sections
+ EXTRADATA, ///< Answer section contains more RRsets than needed
+ INVNAMCLASS, ///< Invalid name or class in answer
+ INVTYPE, ///< Name/class of answer correct, type is wrong
+ MISMATQUEST, ///< Response question section != question
+ MULTICLASS, ///< Multiple classes in multi-RR answer
+ NOTONEQUEST, ///< Not one question in response question section
+ NOTRESPONSE, ///< Response has the Query/Response bit clear
+ NOTSINGLE, ///< CNAME has multiple RDATA elements.
+ OPCODE, ///< Opcode field does not indicate a query
+ RCODE, ///< RCODE indicated an error
+ TRUNCATED ///< Response was truncated
+ };
+
+ /// \brief Check Error
+ ///
+ /// An inline routine to quickly classify whether the return category is
+ /// an error or not. This makes use of internal knowledge of the order of
+ /// codes in the Category enum.
+ ///
+ /// \param code Return category from classify()
+ ///
+ /// \return true if the category is an error, false if not.
+ static bool error(Category code) {
+ return (code > REFERRAL);
+ }
+
+ /// \brief Classify
+ ///
+ /// Classify the response in the "message" object.
+ ///
+ /// \param question Question that was sent to the server
+ /// \param message Pointer to the associated response from the server.
+ /// \param cname_target If the message contains an (unfinished) CNAME
+ /// chain, this Name will be replaced by the target of the last CNAME
+ /// in the chain
+ /// \param cname_count This unsigned int will be incremented with
+ /// the number of CNAMEs followed
+ /// \param tcignore If set, the TC bit in a response packet is
+ /// ignored. Otherwise the error code TRUNCATED will be returned. The
+ /// only time this is likely to be used is in development where we are not
+ /// going to fail over to TCP and will want to use what is returned, even
+ /// if some of the response was lost.
+ static Category classify(const isc::dns::Question& question,
+ const isc::dns::Message& message,
+ isc::dns::Name& cname_target, unsigned int& cname_count,
+ bool tcignore = false);
+
+private:
+ /// \brief Follow CNAMEs
+ ///
+ /// Given a QNAME and an answer section that contains CNAMEs, assume that
+ /// they form a CNAME chain and search through them. Possible outcomes
+ /// are:
+ ///
+ /// a) All CNAMES and they form a chain. The result is a referral.
+ /// b) All but one are CNAMES and they form a chain. The other is pointed
+ /// to by the last element of the chain and is the correct QTYPE. The
+ /// result is an answer.
+ /// c) Having followed the CNAME chain as far as we can, there is one
+ /// remaining RRset that is of the wrong type, or there are multiple
+ /// RRsets remaining. return the EXTRADATA code.
+ ///
+ /// \param qname Question name we are searching for
+ /// \param qtype Question type we are search for. (This is assumed not
+ /// to be "ANY".)
+ /// \param ansrrset Vector of RRsetPtr pointing to the RRsets we are
+ /// considering.
+ /// \param present Array of "int" the same size of ansrrset, with each
+ /// element set to "1" to allow the corresponding element of ansrrset to
+ /// be checked, and "0" to skip it. This might be premature optimisation,
+ /// but the algorithm would otherwise involve duplicating the RRset
+ /// vector then removing elements from random positions one by one. As
+ /// each removal involves the destruction of an "xxxPtr" element (which
+ /// presently is implemented by boost::shared_ptr), the overhad of memory
+ /// management seemed high. This solution imposes some additional loop
+ /// cycles, but that should be minimal compared with the overhead of the
+ /// memory management.
+ /// \param cname_target If the message contains an (unfinished) CNAME
+ /// chain, this Name will be replaced by the target of the last CNAME
+ /// in the chain
+ /// \param cname_count This unsigned int will be incremented with
+ /// the number of CNAMEs followed
+ /// \param size Number of elements to check. See description of \c present
+ /// for details.
+ static Category cnameChase(const isc::dns::Name& qname,
+ const isc::dns::RRType& qtype,
+ isc::dns::Name& cname_target, unsigned int& cname_count,
+ std::vector<isc::dns::RRsetPtr>& ansrrset, std::vector<int>& present,
+ size_t size);
+};
+
+#endif // __RESPONSE_CLASSIFIER_H
+
+} // namespace resolve
+} // namespace isc
diff --git a/src/lib/resolve/tests/Makefile.am b/src/lib/resolve/tests/Makefile.am
new file mode 100644
index 0000000..3b5f41a
--- /dev/null
+++ b/src/lib/resolve/tests/Makefile.am
@@ -0,0 +1,27 @@
+AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
+AM_CPPFLAGS += $(BOOST_INCLUDES)
+AM_CPPFLAGS += -I$(top_srcdir)/src/lib/dns -I$(top_builddir)/src/lib/dns
+
+if USE_STATIC_LINK
+AM_LDFLAGS = -static
+endif
+
+CLEANFILES = *.gcno *.gcda
+
+TESTS =
+if HAVE_GTEST
+TESTS += run_unittests
+run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
+run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
+run_unittests_SOURCES = run_unittests.cc
+run_unittests_SOURCES += resolve_unittest.cc
+run_unittests_SOURCES += resolver_callback_unittest.cc
+run_unittests_SOURCES += response_classifier_unittest.cc
+
+run_unittests_LDADD = $(GTEST_LDADD)
+run_unittests_LDADD += $(top_builddir)/src/lib/resolve/libresolve.la
+run_unittests_LDADD += $(top_builddir)/src/lib/dns/libdns++.la
+
+endif
+
+noinst_PROGRAMS = $(TESTS)
diff --git a/src/lib/resolve/tests/resolve_unittest.cc b/src/lib/resolve/tests/resolve_unittest.cc
new file mode 100644
index 0000000..fd68f16
--- /dev/null
+++ b/src/lib/resolve/tests/resolve_unittest.cc
@@ -0,0 +1,172 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <iostream>
+#include <gtest/gtest.h>
+
+#include <dns/message.h>
+#include <dns/question.h>
+#include <dns/opcode.h>
+#include <dns/rcode.h>
+#include <dns/rrttl.h>
+#include <dns/rdata.h>
+#include <resolve/resolve.h>
+
+using namespace isc::dns;
+
+namespace {
+
+class ResolveHelperFunctionsTest : public ::testing::Test {
+public:
+ ResolveHelperFunctionsTest() :
+ message_a_(new Message(Message::RENDER)),
+ message_b_(new Message(Message::RENDER)),
+ question_(new Question(Name("www.example.com"), RRClass::IN(), RRType::A()))
+ {
+ createMessageA();
+ createMessageB();
+ };
+
+ void createMessageA() {
+ message_a_->setOpcode(Opcode::QUERY());
+ message_a_->setRcode(Rcode::NOERROR());
+ message_a_->addQuestion(question_);
+ }
+
+ void createMessageB() {
+ message_b_->setOpcode(Opcode::QUERY());
+ message_b_->setRcode(Rcode::NOERROR());
+ message_b_->addQuestion(question_);
+
+ // We could reuse the same rrset in the different sections,
+ // but to be sure, we create separate ones
+ RRsetPtr answer_rrset(new RRset(Name("www.example.com"),
+ RRClass::IN(), RRType::TXT(),
+ RRTTL(3600)));
+ answer_rrset->addRdata(rdata::createRdata(RRType::TXT(),
+ RRClass::IN(),
+ "Answer"));
+ message_b_->addRRset(Message::SECTION_ANSWER, answer_rrset);
+
+ RRsetPtr auth_rrset(new RRset(Name("www.example.com"),
+ RRClass::IN(), RRType::TXT(),
+ RRTTL(3600)));
+ auth_rrset->addRdata(rdata::createRdata(RRType::TXT(),
+ RRClass::IN(),
+ "Authority"));
+ auth_rrset->addRdata(rdata::createRdata(RRType::TXT(),
+ RRClass::IN(),
+ "Rdata"));
+ message_b_->addRRset(Message::SECTION_AUTHORITY, auth_rrset);
+
+ RRsetPtr add_rrset(new RRset(Name("www.example.com"),
+ RRClass::IN(), RRType::TXT(),
+ RRTTL(3600)));
+ add_rrset->addRdata(rdata::createRdata(RRType::TXT(),
+ RRClass::IN(),
+ "Additional"));
+ add_rrset->addRdata(rdata::createRdata(RRType::TXT(),
+ RRClass::IN(),
+ "Rdata"));
+ add_rrset->addRdata(rdata::createRdata(RRType::TXT(),
+ RRClass::IN(),
+ "fields."));
+ message_b_->addRRset(Message::SECTION_ADDITIONAL, add_rrset);
+ };
+
+ MessagePtr message_a_;
+ MessagePtr message_b_;
+ QuestionPtr question_;
+};
+
+TEST_F(ResolveHelperFunctionsTest, makeErrorMessageEmptyMessage) {
+ ASSERT_EQ(Rcode::NOERROR(), message_a_->getRcode());
+ ASSERT_EQ(1, message_a_->getRRCount(Message::SECTION_QUESTION));
+ ASSERT_EQ(0, message_a_->getRRCount(Message::SECTION_ANSWER));
+ ASSERT_EQ(0, message_a_->getRRCount(Message::SECTION_AUTHORITY));
+ ASSERT_EQ(0, message_a_->getRRCount(Message::SECTION_ADDITIONAL));
+
+ isc::resolve::makeErrorMessage(message_a_, Rcode::SERVFAIL());
+ EXPECT_EQ(Rcode::SERVFAIL(), message_a_->getRcode());
+ EXPECT_EQ(1, message_a_->getRRCount(Message::SECTION_QUESTION));
+ EXPECT_EQ(0, message_a_->getRRCount(Message::SECTION_ANSWER));
+ EXPECT_EQ(0, message_a_->getRRCount(Message::SECTION_AUTHORITY));
+ EXPECT_EQ(0, message_a_->getRRCount(Message::SECTION_ADDITIONAL));
+}
+
+TEST_F(ResolveHelperFunctionsTest, makeErrorMessageNonEmptyMessage) {
+
+ ASSERT_EQ(Rcode::NOERROR(), message_b_->getRcode());
+ ASSERT_EQ(1, message_b_->getRRCount(Message::SECTION_QUESTION));
+ ASSERT_EQ(1, message_b_->getRRCount(Message::SECTION_ANSWER));
+ ASSERT_EQ(2, message_b_->getRRCount(Message::SECTION_AUTHORITY));
+ ASSERT_EQ(3, message_b_->getRRCount(Message::SECTION_ADDITIONAL));
+
+ isc::resolve::makeErrorMessage(message_b_, Rcode::FORMERR());
+ EXPECT_EQ(Rcode::FORMERR(), message_b_->getRcode());
+ EXPECT_EQ(1, message_b_->getRRCount(Message::SECTION_QUESTION));
+ EXPECT_EQ(0, message_b_->getRRCount(Message::SECTION_ANSWER));
+ EXPECT_EQ(0, message_b_->getRRCount(Message::SECTION_AUTHORITY));
+ EXPECT_EQ(0, message_b_->getRRCount(Message::SECTION_ADDITIONAL));
+}
+
+void
+compareSections(const MessagePtr message_a, const MessagePtr message_b,
+ Message::Section section)
+{
+ RRsetIterator rrs_a = message_a->beginSection(section);
+ RRsetIterator rrs_b = message_b->beginSection(section);
+ while (rrs_a != message_a->endSection(section) &&
+ rrs_b != message_b->endSection(section)
+ ) {
+ EXPECT_EQ(*rrs_a, *rrs_b);
+ ++rrs_a;
+ ++rrs_b;
+ }
+ // can't use EXPECT_EQ here, no eqHelper for endsection comparison
+ EXPECT_TRUE(rrs_a == message_a->endSection(section));
+ EXPECT_TRUE(rrs_b == message_b->endSection(section));
+}
+
+TEST_F(ResolveHelperFunctionsTest, copyAnswerMessage) {
+ message_b_->setRcode(Rcode::NXDOMAIN());
+
+ ASSERT_NE(message_b_->getRcode(), message_a_->getRcode());
+ ASSERT_NE(message_b_->getRRCount(Message::SECTION_ANSWER),
+ message_a_->getRRCount(Message::SECTION_ANSWER));
+ ASSERT_NE(message_b_->getRRCount(Message::SECTION_AUTHORITY),
+ message_a_->getRRCount(Message::SECTION_AUTHORITY));
+ ASSERT_NE(message_b_->getRRCount(Message::SECTION_ADDITIONAL),
+ message_a_->getRRCount(Message::SECTION_ADDITIONAL));
+ ASSERT_EQ(0, message_a_->getRRCount(Message::SECTION_ANSWER));
+ ASSERT_EQ(0, message_a_->getRRCount(Message::SECTION_AUTHORITY));
+ ASSERT_EQ(0, message_a_->getRRCount(Message::SECTION_ADDITIONAL));
+
+ isc::resolve::copyResponseMessage(*message_b_, message_a_);
+
+ EXPECT_EQ(message_b_->getRcode(), message_a_->getRcode());
+ ASSERT_EQ(message_b_->getRRCount(Message::SECTION_ANSWER),
+ message_a_->getRRCount(Message::SECTION_ANSWER));
+ ASSERT_EQ(message_b_->getRRCount(Message::SECTION_AUTHORITY),
+ message_a_->getRRCount(Message::SECTION_AUTHORITY));
+ ASSERT_EQ(message_b_->getRRCount(Message::SECTION_ADDITIONAL),
+ message_a_->getRRCount(Message::SECTION_ADDITIONAL));
+
+
+ compareSections(message_a_, message_b_, Message::SECTION_ANSWER);
+ compareSections(message_a_, message_b_, Message::SECTION_AUTHORITY);
+ compareSections(message_a_, message_b_, Message::SECTION_ADDITIONAL);
+}
+
+} // Anonymous namespace
diff --git a/src/lib/resolve/tests/resolver_callback_unittest.cc b/src/lib/resolve/tests/resolver_callback_unittest.cc
new file mode 100644
index 0000000..6370e22
--- /dev/null
+++ b/src/lib/resolve/tests/resolver_callback_unittest.cc
@@ -0,0 +1,90 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <gtest/gtest.h>
+#include <resolve/resolver_callback.h>
+#include <asiolink/asiolink.h>
+
+using namespace isc::resolve;
+
+// Dummy subclass for DNSServer*
+// We want to check if resume is called
+// Since the server will get cloned(), we want the clones to share
+// our bools for whether resume got called and with what value
+class DummyServer : public asiolink::DNSServer {
+public:
+ DummyServer(DummyServer* orig) {
+ resume_called_ = orig->getResumeCalled();
+ resume_value_ = orig->getResumeValue();
+ }
+ DummyServer(bool* resume_called, bool* resume_value) :
+ resume_called_(resume_called), resume_value_(resume_value)
+ {}
+
+ bool* getResumeCalled() { return resume_called_; }
+ bool* getResumeValue() { return resume_value_; }
+
+ DNSServer* clone() {
+ DummyServer* n = new DummyServer(this);
+ return n;
+ }
+
+ void resume(bool value) {
+ *resume_called_ = true;
+ *resume_value_ = value;
+ }
+
+private:
+ bool* resume_called_;
+ bool* resume_value_;
+};
+
+class ResolverCallbackServerTest : public ::testing::Test {
+public:
+ ResolverCallbackServerTest() : resume_called_(false),
+ resume_value_(false) {
+ server_ = new DummyServer(&resume_called_, &resume_value_);
+ callback_ = new ResolverCallbackServer(server_);
+ };
+
+ ~ResolverCallbackServerTest() {
+ delete callback_;
+ delete server_;
+ }
+
+ DummyServer* getServer() { return server_; }
+ ResolverCallbackServer* getCallback() { return callback_; }
+ bool getResumeCalled() { return resume_called_; }
+ bool getResumeValue() { return resume_value_; }
+
+private:
+ DummyServer* server_;
+ ResolverCallbackServer* callback_;
+ bool resume_called_;
+ bool resume_value_;
+};
+
+TEST_F(ResolverCallbackServerTest, testSuccess) {
+ EXPECT_FALSE(getResumeCalled());
+ getCallback()->success(isc::dns::MessagePtr());
+ EXPECT_TRUE(getResumeCalled());
+ EXPECT_TRUE(getResumeValue());
+}
+
+TEST_F(ResolverCallbackServerTest, testFailure) {
+ EXPECT_FALSE(getResumeCalled());
+ getCallback()->failure();
+ EXPECT_TRUE(getResumeCalled());
+ EXPECT_FALSE(getResumeValue());
+}
diff --git a/src/lib/resolve/tests/response_classifier_unittest.cc b/src/lib/resolve/tests/response_classifier_unittest.cc
new file mode 100644
index 0000000..b37ded7
--- /dev/null
+++ b/src/lib/resolve/tests/response_classifier_unittest.cc
@@ -0,0 +1,537 @@
+// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <iostream>
+#include <gtest/gtest.h>
+
+#include <dns/tests/unittest_util.h>
+
+#include <resolve/response_classifier.h>
+
+#include <dns/name.h>
+#include <dns/opcode.h>
+#include <dns/question.h>
+#include <dns/rdata.h>
+#include <dns/rdataclass.h>
+#include <dns/rcode.h>
+#include <dns/rrclass.h>
+#include <dns/rrset.h>
+#include <dns/rrtype.h>
+#include <dns/rrttl.h>
+
+using namespace std;
+using namespace isc::dns;
+using namespace rdata;
+using namespace isc::dns::rdata::generic;
+using namespace isc::dns::rdata::in;
+using namespace isc::resolve;
+
+
+namespace {
+class ResponseClassifierTest : public ::testing::Test {
+public:
+ /// \brief Constructor
+ ///
+ /// The naming convention is:
+ ///
+ /// <category>_<class>_<type>_<name>
+ ///
+ /// <category> is "qu" (question), "rrs" (rrset),
+ /// <qclass> is self-explanatory
+ /// <qtype> is self-explanatory
+ /// <name> is the first part of the domain name (all expected to be in
+ /// example.com)
+ ///
+ /// Message variables
+ ///
+ /// msg_<qtype> Where <qtype> is the type of query. These are only used
+ /// in the early tests where simple messages are required.
+
+ ResponseClassifierTest() :
+ msg_a(Message::RENDER),
+ msg_any(Message::RENDER),
+ qu_ch_a_www(Name("www.example.com"), RRClass::CH(), RRType::A()),
+ qu_in_any_www(Name("www.example.com"), RRClass::IN(), RRType::ANY()),
+ qu_in_a_www2(Name("www2.example.com"), RRClass::IN(), RRType::A()),
+ qu_in_a_www(Name("www.example.com"), RRClass::IN(), RRType::A()),
+ qu_in_cname_www1(Name("www1.example.com"), RRClass::IN(), RRType::A()),
+ qu_in_ns_(Name("example.com"), RRClass::IN(), RRType::NS()),
+ qu_in_txt_www(Name("www.example.com"), RRClass::IN(), RRType::TXT()),
+ rrs_hs_txt_www(new RRset(Name("www.example.com"), RRClass::HS(),
+ RRType::TXT(), RRTTL(300))),
+ rrs_in_a_mail(new RRset(Name("mail.example.com"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+ rrs_in_a_www(new RRset(Name("www.example.com"), RRClass::IN(),
+ RRType::A(), RRTTL(300))),
+ rrs_in_cname_www1(new RRset(Name("www1.example.com"), RRClass::IN(),
+ RRType::CNAME(), RRTTL(300))),
+ rrs_in_cname_www2(new RRset(Name("www2.example.com"), RRClass::IN(),
+ RRType::CNAME(), RRTTL(300))),
+ rrs_in_ns_(new RRset(Name("example.com"), RRClass::IN(),
+ RRType::NS(), RRTTL(300))),
+ rrs_in_txt_www(new RRset(Name("www.example.com"), RRClass::IN(),
+ RRType::TXT(), RRTTL(300))),
+ cname_target("."),
+ cname_count(0)
+ {
+ // Set up the message to indicate a successful response to the question
+ // "www.example.com A", but don't add in any response sections.
+ msg_a.setHeaderFlag(Message::HEADERFLAG_QR);
+ msg_a.setOpcode(Opcode::QUERY());
+ msg_a.setRcode(Rcode::NOERROR());
+ msg_a.addQuestion(qu_in_a_www);
+
+ // ditto for the query "www.example.com ANY"
+ msg_any.setHeaderFlag(Message::HEADERFLAG_QR);
+ msg_any.setOpcode(Opcode::QUERY());
+ msg_any.setRcode(Rcode::NOERROR());
+ msg_any.addQuestion(qu_in_any_www);
+
+ // The next set of assignments set up the following zone records
+ //
+ // example.com NS ns0.isc.org
+ // NS ns0.example.org
+ //
+ // www.example.com A 1.2.3.4
+ // TXT "An example text string"
+ //
+ // mail.example.com A 4.5.6.7
+ //
+ // www1.example.com CNAME www.example.com
+ //
+ // www2.example.com CNAME www1.example.com
+
+ // Set up an imaginary NS RRset for an authority section
+ rrs_in_ns_->addRdata(ConstRdataPtr(new NS(Name("ns0.isc.org"))));
+ rrs_in_ns_->addRdata(ConstRdataPtr(new NS(Name("ns0.example.org"))));
+
+ // Set up the records for the www host
+ rrs_in_a_www->addRdata(ConstRdataPtr(new A("1.2.3.4")));
+ rrs_in_txt_www->addRdata(ConstRdataPtr(
+ new TXT("An example text string")));
+
+ // ... for the mail host
+ rrs_in_a_mail->addRdata(ConstRdataPtr(new A("5.6.7.8")));
+
+ // ... the CNAME records
+ rrs_in_cname_www1->addRdata(ConstRdataPtr(
+ new CNAME("www.example.com")));
+ rrs_in_cname_www2->addRdata(ConstRdataPtr(
+ new CNAME("www1.example.com")));
+ }
+
+ Message msg_a; // Pointer to message in RENDER state
+ Message msg_any; // Pointer to message in RENDER state
+ Question qu_ch_a_www; // www.example.com CH A
+ Question qu_in_any_www; // www.example.com IN ANY
+ Question qu_in_a_www2; // www.example.com IN ANY
+ Question qu_in_a_www; // www.example.com IN A
+ Question qu_in_cname_www1; // www1.example.com IN CNAME
+ Question qu_in_ns_; // example.com IN NS
+ Question qu_in_txt_www; // www.example.com IN TXT
+ RRsetPtr rrs_hs_txt_www; // www.example.com HS TXT
+ RRsetPtr rrs_in_a_mail; // mail.example.com IN A
+ RRsetPtr rrs_in_a_www; // www.example.com IN A
+ RRsetPtr rrs_in_cname_www1; // www1.example.com IN CNAME
+ RRsetPtr rrs_in_cname_www2; // www2.example.com IN CNAME
+ RRsetPtr rrs_in_ns_; // example.com IN NS
+ RRsetPtr rrs_in_txt_www; // www.example.com IN TXT
+ Name cname_target; // Used in response classifier to
+ // store the target of a possible
+ // CNAME chain
+ unsigned int cname_count; // Used to count cnames in a chain
+};
+
+// Test that the error() function categorises the codes correctly.
+
+TEST_F(ResponseClassifierTest, StatusCodes) {
+ EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::ANSWER));
+ EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::ANSWERCNAME));
+ EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::CNAME));
+ EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::NXDOMAIN));
+ EXPECT_FALSE(ResponseClassifier::error(ResponseClassifier::REFERRAL));
+
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::EMPTY));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::EXTRADATA));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::INVNAMCLASS));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::INVTYPE));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::MISMATQUEST));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::MULTICLASS));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::NOTONEQUEST));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::NOTRESPONSE));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::NOTSINGLE));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::OPCODE));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::RCODE));
+ EXPECT_TRUE(ResponseClassifier::error(ResponseClassifier::TRUNCATED));
+}
+
+// Test that the system will reject a message which is a query.
+
+TEST_F(ResponseClassifierTest, Query) {
+
+ // Set up message to indicate a query (QR flag = 0, one question). By
+ // default the opcode will be 0 (query)
+ msg_a.setHeaderFlag(Message::HEADERFLAG_QR, false);
+
+ // Should be rejected as it is a query, not a response
+ EXPECT_EQ(ResponseClassifier::NOTRESPONSE,
+ ResponseClassifier::classify(qu_in_a_www, msg_a,
+ cname_target, cname_count));
+}
+
+// Check that we get an OPCODE error on all but QUERY opcodes.
+
+TEST_F(ResponseClassifierTest, Opcode) {
+
+ uint8_t query = static_cast<uint8_t>(Opcode::QUERY().getCode());
+
+ for (uint8_t i = 0; i < (1 << 4); ++i) {
+ msg_a.setOpcode(Opcode(i));
+ if (i == query) {
+ EXPECT_NE(ResponseClassifier::OPCODE,
+ ResponseClassifier::classify(qu_in_a_www, msg_a,
+ cname_target, cname_count));
+ } else {
+ EXPECT_EQ(ResponseClassifier::OPCODE,
+ ResponseClassifier::classify(qu_in_a_www, msg_a,
+ cname_target, cname_count));
+ }
+ }
+}
+
+// Test that the system will reject a response with anything other than one
+// question.
+
+TEST_F(ResponseClassifierTest, MultipleQuestions) {
+
+ // Create a message object for this test that has no question section.
+ Message message(Message::RENDER);
+ message.setHeaderFlag(Message::HEADERFLAG_QR);
+ message.setOpcode(Opcode::QUERY());
+ message.setRcode(Rcode::NOERROR());
+
+ // Zero questions
+ EXPECT_EQ(ResponseClassifier::NOTONEQUEST,
+ ResponseClassifier::classify(qu_in_a_www, message,
+ cname_target, cname_count));
+
+ // One question
+ message.addQuestion(qu_in_a_www);
+ EXPECT_NE(ResponseClassifier::NOTONEQUEST,
+ ResponseClassifier::classify(qu_in_a_www, message,
+ cname_target, cname_count));
+
+ // Two questions
+ message.addQuestion(qu_in_ns_);
+ EXPECT_EQ(ResponseClassifier::NOTONEQUEST,
+ ResponseClassifier::classify(qu_in_a_www, message,
+ cname_target, cname_count));
+
+ // And finish the check with three questions
+ message.addQuestion(qu_in_txt_www);
+ EXPECT_EQ(ResponseClassifier::NOTONEQUEST,
+ ResponseClassifier::classify(qu_in_a_www, message,
+ cname_target, cname_count));
+}
+
+// Test that the question in the question section in the message response
+// is equal to the question supplied.
+
+TEST_F(ResponseClassifierTest, SameQuestion) {
+
+ EXPECT_EQ(ResponseClassifier::MISMATQUEST,
+ ResponseClassifier::classify(qu_in_ns_, msg_a,
+ cname_target, cname_count));
+ EXPECT_NE(ResponseClassifier::MISMATQUEST,
+ ResponseClassifier::classify(qu_in_a_www, msg_a,
+ cname_target, cname_count));
+}
+
+// Should get an NXDOMAIN response only on an NXDOMAIN RCODE.
+
+TEST_F(ResponseClassifierTest, NXDOMAIN) {
+
+ uint16_t nxdomain = static_cast<uint16_t>(Rcode::NXDOMAIN().getCode());
+
+ for (uint8_t i = 0; i < (1 << 4); ++i) {
+ msg_a.setRcode(Rcode(i));
+ if (i == nxdomain) {
+ EXPECT_EQ(ResponseClassifier::NXDOMAIN,
+ ResponseClassifier::classify(qu_in_a_www, msg_a,
+ cname_target, cname_count));
+ } else {
+ EXPECT_NE(ResponseClassifier::NXDOMAIN,
+ ResponseClassifier::classify(qu_in_a_www, msg_a,
+ cname_target, cname_count));
+ }
+ }
+}
+
+// Check that we get an RCODE error on all but NXDOMAIN and NOERROR responses.
+
+TEST_F(ResponseClassifierTest, RCODE) {
+
+ uint16_t nxdomain = static_cast<uint16_t>(Rcode::NXDOMAIN().getCode());
+ uint16_t noerror = static_cast<uint16_t>(Rcode::NOERROR().getCode());
+
+ for (uint8_t i = 0; i < (1 << 4); ++i) {
+ msg_a.setRcode(Rcode(i));
+ if ((i == nxdomain) || (i == noerror)) {
+ EXPECT_NE(ResponseClassifier::RCODE,
+ ResponseClassifier::classify(qu_in_a_www, msg_a,
+ cname_target, cname_count));
+ } else {
+ EXPECT_EQ(ResponseClassifier::RCODE,
+ ResponseClassifier::classify(qu_in_a_www, msg_a,
+ cname_target, cname_count));
+ }
+ }
+}
+
+// Test that the code will detect a truncated message. Even if nothing else
+// is wrong, we'll want to retry the query if we receive a truncated code.
+// However, we give the option to the user of the code aws to whether they
+// want to take into account the truncated bit.
+
+TEST_F(ResponseClassifierTest, Truncated) {
+
+ // Don't expect the truncated code whatever option we ask for if the TC
+ // bit is not set.
+ msg_a.setHeaderFlag(Message::HEADERFLAG_TC, false);
+ EXPECT_NE(ResponseClassifier::TRUNCATED,
+ ResponseClassifier::classify(qu_in_a_www, msg_a, cname_target,
+ cname_count, true));
+ EXPECT_NE(ResponseClassifier::TRUNCATED,
+ ResponseClassifier::classify(qu_in_a_www, msg_a, cname_target,
+ cname_count, false));
+
+ // Expect the truncated code if the TC bit is set, only if we don't ignore
+ // it.
+ msg_a.setHeaderFlag(Message::HEADERFLAG_TC, true);
+ EXPECT_NE(ResponseClassifier::TRUNCATED,
+ ResponseClassifier::classify(qu_in_a_www, msg_a, cname_target,
+ cname_count, true));
+ EXPECT_EQ(ResponseClassifier::TRUNCATED,
+ ResponseClassifier::classify(qu_in_a_www, msg_a, cname_target,
+ cname_count, false));
+}
+
+// Check for an empty packet (i.e. no error, but with the answer and additional
+// sections empty).
+
+TEST_F(ResponseClassifierTest, Empty) {
+
+ EXPECT_EQ(ResponseClassifier::EMPTY,
+ ResponseClassifier::classify(qu_in_a_www, msg_a, cname_target,
+ cname_count));
+}
+
+// Anything where we have an empty answer section but something in the
+// authority section is a referral (if the status is NOERROR).
+
+TEST_F(ResponseClassifierTest, EmptyAnswerReferral) {
+
+ msg_a.addRRset(Message::SECTION_AUTHORITY, rrs_in_ns_);
+ EXPECT_EQ(ResponseClassifier::REFERRAL,
+ ResponseClassifier::classify(qu_in_a_www, msg_a, cname_target,
+ cname_count));
+
+}
+
+// Check the case where we have a simple answer in the answer section. This
+// occurs when the QNAME/QTYPE/QCLASS matches one of the RRsets in the
+// answer section - expect when the QTYPE is ANY, in which case the match
+// must be on the QNAME/QCLASS alone.
+
+TEST_F(ResponseClassifierTest, SingleAnswer) {
+
+ // Check a question that matches the answer
+ msg_a.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ EXPECT_EQ(ResponseClassifier::ANSWER,
+ ResponseClassifier::classify(qu_in_a_www, msg_a, cname_target,
+ cname_count));
+
+ // Check an ANY question that matches the answer
+ msg_any.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ EXPECT_EQ(ResponseClassifier::ANSWER,
+ ResponseClassifier::classify(qu_in_any_www, msg_any, cname_target,
+ cname_count));
+
+ // Check a CNAME response that matches the QNAME.
+ Message message_a(Message::RENDER);
+ message_a.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_a.setOpcode(Opcode::QUERY());
+ message_a.setRcode(Rcode::NOERROR());
+ message_a.addQuestion(qu_in_cname_www1);
+ message_a.addRRset(Message::SECTION_ANSWER, rrs_in_cname_www1);
+ EXPECT_EQ(ResponseClassifier::CNAME,
+ ResponseClassifier::classify(qu_in_cname_www1, message_a,
+ cname_target, cname_count));
+
+ // Check if the answer QNAME does not match the question
+ // Q: www.example.com IN A
+ // A: mail.example.com IN A
+ Message message_b(Message::RENDER);
+ message_b.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_b.setOpcode(Opcode::QUERY());
+ message_b.setRcode(Rcode::NOERROR());
+ message_b.addQuestion(qu_in_a_www);
+ message_b.addRRset(Message::SECTION_ANSWER, rrs_in_a_mail);
+ EXPECT_EQ(ResponseClassifier::INVNAMCLASS,
+ ResponseClassifier::classify(qu_in_a_www, message_b,
+ cname_target, cname_count));
+
+ // Check if the answer class does not match the question
+ // Q: www.example.com CH A
+ // A: www.example.com IN A
+ Message message_c(Message::RENDER);
+ message_c.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_c.setOpcode(Opcode::QUERY());
+ message_c.setRcode(Rcode::NOERROR());
+ message_c.addQuestion(qu_ch_a_www);
+ message_c.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ EXPECT_EQ(ResponseClassifier::INVNAMCLASS,
+ ResponseClassifier::classify(qu_ch_a_www, message_c,
+ cname_target, cname_count));
+
+ // Check if the answer type does not match the question
+ // Q: www.example.com IN A
+ // A: www.example.com IN TXT
+ Message message_d(Message::RENDER);
+ message_d.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_d.setOpcode(Opcode::QUERY());
+ message_d.setRcode(Rcode::NOERROR());
+ message_d.addQuestion(qu_in_a_www);
+ message_d.addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
+ EXPECT_EQ(ResponseClassifier::INVTYPE,
+ ResponseClassifier::classify(qu_in_a_www, message_d,
+ cname_target, cname_count));
+}
+
+// Check what happens if we have multiple RRsets in the answer.
+
+TEST_F(ResponseClassifierTest, MultipleAnswerRRsets) {
+
+ // All the same QNAME but different types is only valid on an ANY query.
+ Message message_a(Message::RENDER);
+ message_a.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_a.setOpcode(Opcode::QUERY());
+ message_a.setRcode(Rcode::NOERROR());
+ message_a.addQuestion(qu_in_any_www);
+ message_a.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ message_a.addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
+ EXPECT_EQ(ResponseClassifier::ANSWER,
+ ResponseClassifier::classify(qu_in_any_www, message_a,
+ cname_target, cname_count));
+
+ // On another type of query, it results in an EXTRADATA error
+ Message message_b(Message::RENDER);
+ message_b.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_b.setOpcode(Opcode::QUERY());
+ message_b.setRcode(Rcode::NOERROR());
+ message_b.addQuestion(qu_in_a_www);
+ message_b.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ message_b.addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
+ EXPECT_EQ(ResponseClassifier::EXTRADATA,
+ ResponseClassifier::classify(qu_in_a_www, message_b,
+ cname_target, cname_count));
+
+ // Same QNAME on an ANY query is not valid with mixed classes
+ Message message_c(Message::RENDER);
+ message_c.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_c.setOpcode(Opcode::QUERY());
+ message_c.setRcode(Rcode::NOERROR());
+ message_c.addQuestion(qu_in_any_www);
+ message_c.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ message_c.addRRset(Message::SECTION_ANSWER, rrs_hs_txt_www);
+ EXPECT_EQ(ResponseClassifier::MULTICLASS,
+ ResponseClassifier::classify(qu_in_any_www, message_c,
+ cname_target, cname_count));
+
+ // Mixed QNAME is not valid unless QNAME requested is a CNAME.
+ Message message_d(Message::RENDER);
+ message_d.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_d.setOpcode(Opcode::QUERY());
+ message_d.setRcode(Rcode::NOERROR());
+ message_d.addQuestion(qu_in_a_www);
+ message_d.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ message_d.addRRset(Message::SECTION_ANSWER, rrs_in_a_mail);
+ EXPECT_EQ(ResponseClassifier::EXTRADATA,
+ ResponseClassifier::classify(qu_in_a_www, message_d,
+ cname_target, cname_count));
+
+ // Mixed QNAME is not valid when the query is an ANY.
+ Message message_e(Message::RENDER);
+ message_e.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_e.setOpcode(Opcode::QUERY());
+ message_e.setRcode(Rcode::NOERROR());
+ message_e.addQuestion(qu_in_any_www);
+ message_e.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ message_e.addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
+ message_e.addRRset(Message::SECTION_ANSWER, rrs_in_a_mail);
+ EXPECT_EQ(ResponseClassifier::EXTRADATA,
+ ResponseClassifier::classify(qu_in_any_www, message_e,
+ cname_target, cname_count));
+}
+
+// CNAME chain is CNAME if it terminates in a CNAME, answer if it
+// does not, and error if there are RRs left over.
+TEST_F(ResponseClassifierTest, CNAMEChain) {
+
+ // Answer contains a single CNAME
+ Message message_a(Message::RENDER);
+ message_a.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_a.setOpcode(Opcode::QUERY());
+ message_a.setRcode(Rcode::NOERROR());
+ message_a.addQuestion(qu_in_a_www2);
+ message_a.addRRset(Message::SECTION_ANSWER, rrs_in_cname_www2);
+ EXPECT_EQ(ResponseClassifier::CNAME,
+ ResponseClassifier::classify(qu_in_a_www2, message_a,
+ cname_target, cname_count));
+
+ // Add a CNAME for www1, and it should still return a CNAME
+ message_a.addRRset(Message::SECTION_ANSWER, rrs_in_cname_www1);
+ EXPECT_EQ(ResponseClassifier::CNAME,
+ ResponseClassifier::classify(qu_in_a_www2, message_a,
+ cname_target, cname_count));
+
+ // Add the A record for www and it should be an answer
+ message_a.addRRset(Message::SECTION_ANSWER, rrs_in_a_www);
+ EXPECT_EQ(ResponseClassifier::ANSWERCNAME,
+ ResponseClassifier::classify(qu_in_a_www2, message_a,
+ cname_target, cname_count));
+
+ // Adding an unrelated TXT record should result in EXTRADATA
+ message_a.addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
+ EXPECT_EQ(ResponseClassifier::EXTRADATA,
+ ResponseClassifier::classify(qu_in_a_www2, message_a,
+ cname_target, cname_count));
+
+ // Recreate the chain, but this time end with a TXT RR and not the A
+ // record. This should return INVTYPE.
+ Message message_b(Message::RENDER);
+ message_b.setHeaderFlag(Message::HEADERFLAG_QR);
+ message_b.setOpcode(Opcode::QUERY());
+ message_b.setRcode(Rcode::NOERROR());
+ message_b.addQuestion(qu_in_a_www2);
+ message_b.addRRset(Message::SECTION_ANSWER, rrs_in_cname_www2);
+ message_b.addRRset(Message::SECTION_ANSWER, rrs_in_cname_www1);
+ message_b.addRRset(Message::SECTION_ANSWER, rrs_in_txt_www);
+
+ EXPECT_EQ(ResponseClassifier::INVTYPE,
+ ResponseClassifier::classify(qu_in_a_www2, message_b,
+ cname_target, cname_count));
+}
+
+} // Anonymous namespace
diff --git a/src/lib/resolve/tests/run_unittests.cc b/src/lib/resolve/tests/run_unittests.cc
new file mode 100644
index 0000000..f80e167
--- /dev/null
+++ b/src/lib/resolve/tests/run_unittests.cc
@@ -0,0 +1,24 @@
+// Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <gtest/gtest.h>
+
+#include <dns/tests/unittest_util.h>
+
+int
+main(int argc, char* argv[]) {
+ ::testing::InitGoogleTest(&argc, argv);
+
+ return (RUN_ALL_TESTS());
+}
diff --git a/src/lib/testutils/srv_test.cc b/src/lib/testutils/srv_test.cc
index 0a27eba..c0d6e0f 100644
--- a/src/lib/testutils/srv_test.cc
+++ b/src/lib/testutils/srv_test.cc
@@ -33,6 +33,7 @@ const char* const DEFAULT_REMOTE_ADDRESS = "192.0.2.1";
SrvTestBase::SrvTestBase() : request_message(Message::RENDER),
parse_message(new Message(Message::PARSE)),
+ response_message(new Message(Message::RENDER)),
default_qid(0x1035),
opcode(Opcode(Opcode::QUERY())),
qname("www.example.com"),
diff --git a/src/lib/testutils/srv_test.h b/src/lib/testutils/srv_test.h
index 061076d..7361a76 100644
--- a/src/lib/testutils/srv_test.h
+++ b/src/lib/testutils/srv_test.h
@@ -89,8 +89,8 @@ protected:
MockSession notify_session;
MockServer dnsserv;
isc::dns::Message request_message;
- isc::dns::MessagePtr response_message;
isc::dns::MessagePtr parse_message;
+ isc::dns::MessagePtr response_message;
const isc::dns::qid_t default_qid;
const isc::dns::Opcode opcode;
const isc::dns::Name qname;
More information about the bind10-changes
mailing list