dnsmasq.c 7.3 KB
Newer Older
S
Satoru SATOH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/*
 * Copyright (C) 2007-2010 Red Hat, Inc.
 * Copyright (C) 2010 Satoru SATOH <satoru.satoh@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Based on iptables.c
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>

#ifdef HAVE_PATHS_H
# include <paths.h>
#endif

#include "internal.h"
#include "datatypes.h"
#include "dnsmasq.h"
#include "util.h"
#include "memory.h"
#include "virterror_internal.h"
#include "logging.h"

#define VIR_FROM_THIS VIR_FROM_NETWORK
#define DNSMASQ_HOSTSFILE_SUFFIX "hostsfile"

static void
dhcphostFree(dnsmasqDhcpHost *host)
{
    VIR_FREE(host->host);
}

static void
hostsfileFree(dnsmasqHostsfile *hostsfile)
{
    unsigned int i;

    if (hostsfile->hosts) {
        for (i = 0; i < hostsfile->nhosts; i++)
            dhcphostFree(&hostsfile->hosts[i]);

        VIR_FREE(hostsfile->hosts);

        hostsfile->nhosts = 0;
    }

    VIR_FREE(hostsfile->path);

    VIR_FREE(hostsfile);
}

static int
hostsfileAdd(dnsmasqHostsfile *hostsfile,
             const char *mac,
79
             virSocketAddr *ip,
S
Satoru SATOH 已提交
80 81
             const char *name)
{
E
Eric Blake 已提交
82
    char *ipstr = NULL;
S
Satoru SATOH 已提交
83 84 85
    if (VIR_REALLOC_N(hostsfile->hosts, hostsfile->nhosts + 1) < 0)
        goto alloc_error;

86 87 88
    if (!(ipstr = virSocketFormatAddr(ip)))
        return -1;

S
Satoru SATOH 已提交
89 90
    if (name) {
        if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s,%s",
91
                        mac, ipstr, name) < 0) {
S
Satoru SATOH 已提交
92 93 94 95
            goto alloc_error;
        }
    } else {
        if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s",
96
                        mac, ipstr) < 0) {
S
Satoru SATOH 已提交
97 98 99
            goto alloc_error;
        }
    }
100
    VIR_FREE(ipstr);
S
Satoru SATOH 已提交
101 102 103 104 105 106

    hostsfile->nhosts++;

    return 0;

 alloc_error:
107
    virReportOOMError();
108
    VIR_FREE(ipstr);
S
Satoru SATOH 已提交
109 110 111 112 113 114 115 116 117 118
    return -1;
}

static dnsmasqHostsfile *
hostsfileNew(const char *name,
             const char *config_dir)
{
    int err;
    dnsmasqHostsfile *hostsfile;

119 120
    if (VIR_ALLOC(hostsfile) < 0) {
        virReportOOMError();
S
Satoru SATOH 已提交
121
        return NULL;
122
    }
S
Satoru SATOH 已提交
123 124 125 126 127

    hostsfile->hosts = NULL;
    hostsfile->nhosts = 0;

    if (virAsprintf(&hostsfile->path, "%s/%s.%s", config_dir, name,
128 129
                    DNSMASQ_HOSTSFILE_SUFFIX) < 0) {
        virReportOOMError();
S
Satoru SATOH 已提交
130 131 132 133 134
        goto error;
    }

    if ((err = virFileMakePath(config_dir))) {
        virReportSystemError(err, _("cannot create config directory '%s'"),
135
                             config_dir);
S
Satoru SATOH 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        goto error;
    }

    return hostsfile;

 error:
    hostsfileFree(hostsfile);
    return NULL;
}

static int
hostsfileWrite(const char *path,
               dnsmasqDhcpHost *hosts,
               unsigned int nhosts)
{
    char *tmp;
    FILE *f;
    bool istmp = true;
    unsigned int i;
    int rc = 0;

    if (nhosts == 0)
        return rc;

    if (virAsprintf(&tmp, "%s.new", path) < 0)
        return ENOMEM;

    if (!(f = fopen(tmp, "w"))) {
        istmp = false;
        if (!(f = fopen(path, "w"))) {
            rc = errno;
            goto cleanup;
        }
    }

    for (i = 0; i < nhosts; i++) {
        if (fputs(hosts[i].host, f) == EOF || fputc('\n', f) == EOF) {
            rc = errno;
            fclose(f);

            if (istmp)
                unlink(tmp);

            goto cleanup;
        }
    }

    if (fclose(f) == EOF) {
        rc = errno;
        goto cleanup;
    }

    if (istmp) {
        if (rename(tmp, path) < 0) {
            rc = errno;
            unlink(tmp);
            goto cleanup;
        }

        if (unlink(tmp) < 0) {
            rc = errno;
            goto cleanup;
        }
    }

 cleanup:
    VIR_FREE(tmp);

    return rc;
}

static int
hostsfileSave(dnsmasqHostsfile *hostsfile)
{
    int err = hostsfileWrite(hostsfile->path, hostsfile->hosts,
211
                             hostsfile->nhosts);
S
Satoru SATOH 已提交
212 213 214

    if (err < 0) {
        virReportSystemError(err, _("cannot write config file '%s'"),
215
                             hostsfile->path);
S
Satoru SATOH 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229
        return -1;
    }

    return 0;
}

static int
hostsfileDelete(dnsmasqHostsfile *hostsfile)
{
    if (!virFileExists(hostsfile->path))
        return 0;

    if (unlink(hostsfile->path) < 0) {
        virReportSystemError(errno, _("cannot remove config file '%s'"),
230
                             hostsfile->path);
S
Satoru SATOH 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        return -1;
    }

    return 0;
}

/**
 * dnsmasqContextNew:
 *
 * Create a new Dnsmasq context
 *
 * Returns a pointer to the new structure or NULL in case of error
 */
dnsmasqContext *
dnsmasqContextNew(const char *network_name,
                  const char *config_dir)
{
    dnsmasqContext *ctx;

250 251
    if (VIR_ALLOC(ctx) < 0) {
        virReportOOMError();
S
Satoru SATOH 已提交
252
        return NULL;
253
    }
S
Satoru SATOH 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

    if (!(ctx->hostsfile = hostsfileNew(network_name, config_dir)))
        goto error;

    return ctx;

 error:
    dnsmasqContextFree(ctx);
    return NULL;
}

/**
 * dnsmasqContextFree:
 * @ctx: pointer to the dnsmasq context
 *
 * Free the resources associated with an dnsmasq context
 */
void
dnsmasqContextFree(dnsmasqContext *ctx)
{
    if (!ctx)
        return;

    if (ctx->hostsfile)
        hostsfileFree(ctx->hostsfile);

    VIR_FREE(ctx);
}

/**
 * dnsmasqAddDhcpHost:
 * @ctx: pointer to the dnsmasq context for each network
 * @mac: pointer to the string contains mac address of the host
287
 * @ip: pointer to the socket address contains ip of the host
S
Satoru SATOH 已提交
288 289 290 291 292 293 294
 * @name: pointer to the string contains hostname of the host or NULL
 *
 * Add dhcp-host entry.
 */
void
dnsmasqAddDhcpHost(dnsmasqContext *ctx,
                   const char *mac,
295
                   virSocketAddr *ip,
S
Satoru SATOH 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
                   const char *name)
{
    if (ctx->hostsfile)
        hostsfileAdd(ctx->hostsfile, mac, ip, name);
}

/**
 * dnsmasqSave:
 * @ctx: pointer to the dnsmasq context for each network
 *
 * Saves all the configurations associated with a context to disk.
 */
int
dnsmasqSave(const dnsmasqContext *ctx)
{
    if (ctx->hostsfile)
        return hostsfileSave(ctx->hostsfile);

    return 0;
}


/**
 * dnsmasqDelete:
 * @ctx: pointer to the dnsmasq context for each network
 *
 * Delete all the configuration files associated with a context.
 */
int
dnsmasqDelete(const dnsmasqContext *ctx)
{
    if (ctx->hostsfile)
        return hostsfileDelete(ctx->hostsfile);

    return 0;
}

/**
 * dnsmasqReload:
 * @pid: the pid of the target dnsmasq process
 *
 * Reloads all the configurations associated to a context
 */
int
340
dnsmasqReload(pid_t pid ATTRIBUTE_UNUSED)
S
Satoru SATOH 已提交
341
{
342
#ifndef WIN32
S
Satoru SATOH 已提交
343 344
    if (kill(pid, SIGHUP) != 0) {
        virReportSystemError(errno,
345
            _("Failed to make dnsmasq (PID: %d) reload config files."),
S
Satoru SATOH 已提交
346 347 348
            pid);
        return -1;
    }
349
#endif /* WIN32 */
S
Satoru SATOH 已提交
350 351 352

    return 0;
}