2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 * Copyright (C) 2001-2004 Jeff Dike
6 * Based on the "uml_mconsole" utilty from Jeff Dike.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #include <sys/types.h>
22 #include <sys/socket.h>
29 #define MCONSOLE_MAGIC 0xcafebabe
30 #define MCONSOLE_VERSION 2
31 #define MCONSOLE_MAX_DATA 512
33 typedef struct private_mconsole_t private_mconsole_t
;
35 struct private_mconsole_t
{
36 /** public interface */
38 /** mconsole socket */
40 /** address of uml socket */
41 struct sockaddr_un uml
;
45 * send a request to UML using mconsole
47 static bool request(private_mconsole_t
*this, char *command
)
53 char data
[MCONSOLE_MAX_DATA
];
59 char data
[MCONSOLE_MAX_DATA
];
61 bool first
= TRUE
, good
= TRUE
;
64 memset(&request
, 0, sizeof(request
));
65 request
.magic
= MCONSOLE_MAGIC
;
66 request
.version
= MCONSOLE_VERSION
;
67 request
.len
= min(strlen(command
), sizeof(reply
.data
) - 1);
68 strncpy(request
.data
, command
, request
.len
);
70 if (sendto(this->socket
, &request
, sizeof(request
), 0,
71 (struct sockaddr
*)&this->uml
, sizeof(this->uml
)) < 0)
73 DBG1("sending mconsole command to UML failed: %m");
78 len
= recvfrom(this->socket
, &reply
, sizeof(reply
), 0, NULL
, 0);
81 DBG1("receiving from mconsole failed: %m");
84 if (first
&& reply
.err
)
87 DBG1("received error from UML mconsole: %s", reply
.data
);
96 * Implementation of mconsole_t.add_iface.
98 static bool add_iface(private_mconsole_t
*this, char *guest
, char *host
)
103 len
= snprintf(buf
, sizeof(buf
), "config %s=tuntap,%s", guest
, host
);
104 if (len
< 0 || len
>= sizeof(buf
))
108 return request(this, buf
);
112 * Implementation of mconsole_t.del_iface.
114 static bool del_iface(private_mconsole_t
*this, char *guest
)
119 len
= snprintf(buf
, sizeof(buf
), "remove %s", guest
);
120 if (len
< 0 || len
>= sizeof(buf
))
124 return request(this, buf
);
128 * Implementation of mconsole_t.destroy.
130 static void destroy(private_mconsole_t
*this)
137 * create the mconsole instance
139 mconsole_t
*mconsole_create(char *sock
)
141 struct sockaddr_un addr
;
142 private_mconsole_t
*this = malloc_thing(private_mconsole_t
);
144 this->public.add_iface
= (bool(*)(mconsole_t
*, char *guest
, char *host
))add_iface
;
145 this->public.del_iface
= (bool(*)(mconsole_t
*, char *guest
))del_iface
;
146 this->public.destroy
= (void*)destroy
;
148 this->socket
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
149 if (this->socket
< 0)
151 DBG1("opening mconsole socket failed: %m");
155 memset(&addr
, 0, sizeof(addr
));
156 addr
.sun_family
= AF_UNIX
;
157 sprintf(&addr
.sun_path
[1], "%5d", getpid());
158 if (bind(this->socket
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
160 DBG1("binding mconsole socket failed: %m");
164 memset(&this->uml
, 0, sizeof(this->uml
));
165 this->uml
.sun_family
= AF_UNIX
;
166 strncpy(this->uml
.sun_path
, sock
, sizeof(this->uml
.sun_path
));
168 return &this->public;