[5d1cb8a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup dnscfg
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file DNS configuration utility.
|
---|
| 33 | *
|
---|
| 34 | * Controls the DNS resolution server (@c dnsrsrv).
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
[a2e3ee6] | 38 | #include <inet/addr.h>
|
---|
[5d1cb8a] | 39 | #include <inet/dnsr.h>
|
---|
| 40 | #include <ipc/services.h>
|
---|
| 41 | #include <loc.h>
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 44 | #include <stdint.h>
|
---|
[1d6dd2a] | 45 | #include <str.h>
|
---|
[5d1cb8a] | 46 | #include <str_error.h>
|
---|
| 47 |
|
---|
| 48 | #define NAME "dnscfg"
|
---|
| 49 |
|
---|
| 50 | static void print_syntax(void)
|
---|
| 51 | {
|
---|
[3e66428] | 52 | printf("Syntax:\n");
|
---|
| 53 | printf("\t%s get-ns\n", NAME);
|
---|
| 54 | printf("\t%s set-ns <server-addr>\n", NAME);
|
---|
| 55 | printf("\t%s unset-ns\n", NAME);
|
---|
[5d1cb8a] | 56 | }
|
---|
| 57 |
|
---|
[b7fd2a0] | 58 | static errno_t dnscfg_set_ns(int argc, char *argv[])
|
---|
[5d1cb8a] | 59 | {
|
---|
| 60 | if (argc < 1) {
|
---|
[3e66428] | 61 | printf("%s: Missing arguments.\n", NAME);
|
---|
[5d1cb8a] | 62 | print_syntax();
|
---|
| 63 | return EINVAL;
|
---|
| 64 | }
|
---|
[a35b458] | 65 |
|
---|
[5d1cb8a] | 66 | if (argc > 1) {
|
---|
[3e66428] | 67 | printf("%s: Too many arguments.\n", NAME);
|
---|
[5d1cb8a] | 68 | print_syntax();
|
---|
| 69 | return EINVAL;
|
---|
| 70 | }
|
---|
[a35b458] | 71 |
|
---|
[3e66428] | 72 | char *srv_addr = argv[0];
|
---|
[a35b458] | 73 |
|
---|
[a2e3ee6] | 74 | inet_addr_t addr;
|
---|
[b7fd2a0] | 75 | errno_t rc = inet_addr_parse(srv_addr, &addr, NULL);
|
---|
[a35b458] | 76 |
|
---|
[5d1cb8a] | 77 | if (rc != EOK) {
|
---|
[3e66428] | 78 | printf("%s: Invalid address format '%s'.\n", NAME, srv_addr);
|
---|
| 79 | return rc;
|
---|
[5d1cb8a] | 80 | }
|
---|
[a35b458] | 81 |
|
---|
[5d1cb8a] | 82 | rc = dnsr_set_srvaddr(&addr);
|
---|
| 83 | if (rc != EOK) {
|
---|
[3e66428] | 84 | printf("%s: Failed setting nameserver address '%s' (%s)\n",
|
---|
| 85 | NAME, srv_addr, str_error(rc));
|
---|
| 86 | return rc;
|
---|
[5d1cb8a] | 87 | }
|
---|
[a35b458] | 88 |
|
---|
[5d1cb8a] | 89 | return EOK;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[b7fd2a0] | 92 | static errno_t dnscfg_unset_ns(void)
|
---|
[5d1cb8a] | 93 | {
|
---|
[a2e3ee6] | 94 | inet_addr_t addr;
|
---|
| 95 | inet_addr_any(&addr);
|
---|
[a35b458] | 96 |
|
---|
[b7fd2a0] | 97 | errno_t rc = dnsr_set_srvaddr(&addr);
|
---|
[5d1cb8a] | 98 | if (rc != EOK) {
|
---|
[3e66428] | 99 | printf("%s: Failed unsetting server address (%s)\n",
|
---|
| 100 | NAME, str_error(rc));
|
---|
| 101 | return rc;
|
---|
[5d1cb8a] | 102 | }
|
---|
[a35b458] | 103 |
|
---|
[5d1cb8a] | 104 | return EOK;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[b7fd2a0] | 107 | static errno_t dnscfg_print(void)
|
---|
[5d1cb8a] | 108 | {
|
---|
[a2e3ee6] | 109 | inet_addr_t addr;
|
---|
[b7fd2a0] | 110 | errno_t rc = dnsr_get_srvaddr(&addr);
|
---|
[5d1cb8a] | 111 | if (rc != EOK) {
|
---|
[3e66428] | 112 | printf("%s: Failed getting DNS server address.\n", NAME);
|
---|
[5d1cb8a] | 113 | return rc;
|
---|
| 114 | }
|
---|
[a35b458] | 115 |
|
---|
[3e66428] | 116 | char *addr_str;
|
---|
[a2e3ee6] | 117 | rc = inet_addr_format(&addr, &addr_str);
|
---|
[5d1cb8a] | 118 | if (rc != EOK) {
|
---|
[3e66428] | 119 | printf("%s: Out of memory.\n", NAME);
|
---|
[5d1cb8a] | 120 | return rc;
|
---|
| 121 | }
|
---|
[a35b458] | 122 |
|
---|
[3e66428] | 123 | printf("Nameserver: %s\n", addr_str);
|
---|
[5d1cb8a] | 124 | free(addr_str);
|
---|
| 125 | return EOK;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | int main(int argc, char *argv[])
|
---|
| 129 | {
|
---|
[3e66428] | 130 | if ((argc < 2) || (str_cmp(argv[1], "get-ns") == 0))
|
---|
| 131 | return dnscfg_print();
|
---|
| 132 | else if (str_cmp(argv[1], "set-ns") == 0)
|
---|
| 133 | return dnscfg_set_ns(argc - 2, argv + 2);
|
---|
| 134 | else if (str_cmp(argv[1], "unset-ns") == 0)
|
---|
| 135 | return dnscfg_unset_ns();
|
---|
| 136 | else {
|
---|
| 137 | printf("%s: Unknown command '%s'.\n", NAME, argv[1]);
|
---|
[5d1cb8a] | 138 | print_syntax();
|
---|
| 139 | return 1;
|
---|
| 140 | }
|
---|
[a35b458] | 141 |
|
---|
[5d1cb8a] | 142 | return 0;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /** @}
|
---|
| 146 | */
|
---|