Add initial implementation for uDHCP support
[connman] / plugins / resolvfile.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <sys/stat.h>
32
33 #include <connman/plugin.h>
34 #include <connman/resolver.h>
35 #include <connman/log.h>
36
37 #include <glib.h>
38
39 static int resolvfile_append(const char *interface, const char *domain,
40                                                         const char *server)
41 {
42         char *cmd;
43         int fd, len, err;
44
45         DBG("server %s", server);
46
47         fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
48                                         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
49         if (fd < 0)
50                 return errno;
51
52         err = ftruncate(fd, 0);
53
54         cmd = g_strdup_printf("nameserver %s\n", server);
55
56         len = write(fd, cmd, strlen(cmd));
57
58         g_free(cmd);
59
60         close(fd);
61
62         return 0;
63 }
64
65 static int resolvfile_remove(const char *interface, const char *domain,
66                                                         const char *server)
67 {
68         DBG("server %s", server);
69
70         return 0;
71 }
72
73 static struct connman_resolver resolvfile_resolver = {
74         .name           = "resolvfile",
75         .priority       = CONNMAN_RESOLVER_PRIORITY_LOW,
76         .append         = resolvfile_append,
77         .remove         = resolvfile_remove,
78 };
79
80 static int resolvfile_init(void)
81 {
82         return connman_resolver_register(&resolvfile_resolver);
83 }
84
85 static void resolvfile_exit(void)
86 {
87         connman_resolver_unregister(&resolvfile_resolver);
88 }
89
90 CONNMAN_PLUGIN_DEFINE(resolvfile, "Name resolver plugin", VERSION,
91                                         resolvfile_init, resolvfile_exit)