convert udp chardev to QemuOpts.
While being at it: create a new inet_dgram_opts() function for udp setup, so udp can handle IPv6 now. new cmd line syntax: -chardev udp,id=name,host=remotehost,port=remoteport,\ localaddr=bindaddr,localport=bindport Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
parent
7591c5c199
commit
7e1b35b44c
73
qemu-char.c
73
qemu-char.c
@ -1729,7 +1729,6 @@ static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
|
||||
|
||||
typedef struct {
|
||||
int fd;
|
||||
struct sockaddr_in daddr;
|
||||
uint8_t buf[1024];
|
||||
int bufcnt;
|
||||
int bufptr;
|
||||
@ -1740,8 +1739,7 @@ static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
|
||||
{
|
||||
NetCharDriver *s = chr->opaque;
|
||||
|
||||
return sendto(s->fd, (const void *)buf, len, 0,
|
||||
(struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
|
||||
return send(s->fd, (const void *)buf, len, 0);
|
||||
}
|
||||
|
||||
static int udp_chr_read_poll(void *opaque)
|
||||
@ -1803,30 +1801,18 @@ static void udp_chr_close(CharDriverState *chr)
|
||||
qemu_chr_event(chr, CHR_EVENT_CLOSED);
|
||||
}
|
||||
|
||||
static CharDriverState *qemu_chr_open_udp(const char *def)
|
||||
static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
|
||||
{
|
||||
CharDriverState *chr = NULL;
|
||||
NetCharDriver *s = NULL;
|
||||
int fd = -1;
|
||||
struct sockaddr_in saddr;
|
||||
|
||||
chr = qemu_mallocz(sizeof(CharDriverState));
|
||||
s = qemu_mallocz(sizeof(NetCharDriver));
|
||||
|
||||
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
fd = inet_dgram_opts(opts);
|
||||
if (fd < 0) {
|
||||
perror("socket(PF_INET, SOCK_DGRAM)");
|
||||
goto return_err;
|
||||
}
|
||||
|
||||
if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
|
||||
printf("Could not parse: %s\n", def);
|
||||
goto return_err;
|
||||
}
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
|
||||
{
|
||||
perror("bind");
|
||||
fprintf(stderr, "inet_dgram_opts failed\n");
|
||||
goto return_err;
|
||||
}
|
||||
|
||||
@ -2297,6 +2283,31 @@ static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
|
||||
qemu_opt_set(opts, "telnet", "on");
|
||||
return opts;
|
||||
}
|
||||
if (strstart(filename, "udp:", &p)) {
|
||||
qemu_opt_set(opts, "backend", "udp");
|
||||
if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
|
||||
host[0] = 0;
|
||||
if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
|
||||
fprintf(stderr, "udp #1\n");
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
qemu_opt_set(opts, "host", host);
|
||||
qemu_opt_set(opts, "port", port);
|
||||
if (p[pos] == '@') {
|
||||
p += pos + 1;
|
||||
if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
|
||||
host[0] = 0;
|
||||
if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
|
||||
fprintf(stderr, "udp #2\n");
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
qemu_opt_set(opts, "localaddr", host);
|
||||
qemu_opt_set(opts, "localport", port);
|
||||
}
|
||||
return opts;
|
||||
}
|
||||
if (strstart(filename, "unix:", &p)) {
|
||||
qemu_opt_set(opts, "backend", "socket");
|
||||
if (qemu_opts_do_parse(opts, p, "path") != 0)
|
||||
@ -2327,6 +2338,7 @@ static const struct {
|
||||
} backend_table[] = {
|
||||
{ .name = "null", .open = qemu_chr_open_null },
|
||||
{ .name = "socket", .open = qemu_chr_open_socket },
|
||||
{ .name = "udp", .open = qemu_chr_open_udp },
|
||||
{ .name = "msmouse", .open = qemu_chr_open_msmouse },
|
||||
{ .name = "vc", .open = text_console_init },
|
||||
#ifdef _WIN32
|
||||
@ -2405,27 +2417,12 @@ CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*i
|
||||
QemuOpts *opts;
|
||||
|
||||
opts = qemu_chr_parse_compat(label, filename);
|
||||
if (opts) {
|
||||
chr = qemu_chr_open_opts(opts, init);
|
||||
if (qemu_opt_get_bool(opts, "mux", 0)) {
|
||||
monitor_init(chr, MONITOR_USE_READLINE);
|
||||
}
|
||||
return chr;
|
||||
}
|
||||
if (!opts)
|
||||
return NULL;
|
||||
|
||||
if (strstart(filename, "udp:", &p)) {
|
||||
chr = qemu_chr_open_udp(p);
|
||||
} else
|
||||
{
|
||||
chr = NULL;
|
||||
}
|
||||
|
||||
if (chr) {
|
||||
if (!chr->filename)
|
||||
chr->filename = qemu_strdup(filename);
|
||||
chr->init = init;
|
||||
chr->label = qemu_strdup(label);
|
||||
TAILQ_INSERT_TAIL(&chardevs, chr, next);
|
||||
chr = qemu_chr_open_opts(opts, init);
|
||||
if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
|
||||
monitor_init(chr, MONITOR_USE_READLINE);
|
||||
}
|
||||
return chr;
|
||||
}
|
||||
|
@ -91,6 +91,12 @@ QemuOptsList qemu_chardev_opts = {
|
||||
},{
|
||||
.name = "port",
|
||||
.type = QEMU_OPT_STRING,
|
||||
},{
|
||||
.name = "localaddr",
|
||||
.type = QEMU_OPT_STRING,
|
||||
},{
|
||||
.name = "localport",
|
||||
.type = QEMU_OPT_STRING,
|
||||
},{
|
||||
.name = "to",
|
||||
.type = QEMU_OPT_NUMBER,
|
||||
|
114
qemu-sockets.c
114
qemu-sockets.c
@ -286,6 +286,120 @@ int inet_connect_opts(QemuOpts *opts)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int inet_dgram_opts(QemuOpts *opts)
|
||||
{
|
||||
struct addrinfo ai, *peer = NULL, *local = NULL;
|
||||
const char *addr;
|
||||
const char *port;
|
||||
char uaddr[INET6_ADDRSTRLEN+1];
|
||||
char uport[33];
|
||||
int sock = -1, rc;
|
||||
|
||||
/* lookup peer addr */
|
||||
memset(&ai,0, sizeof(ai));
|
||||
ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
|
||||
ai.ai_family = PF_UNSPEC;
|
||||
ai.ai_socktype = SOCK_DGRAM;
|
||||
|
||||
addr = qemu_opt_get(opts, "host");
|
||||
port = qemu_opt_get(opts, "port");
|
||||
if (addr == NULL || strlen(addr) == 0) {
|
||||
addr = "localhost";
|
||||
}
|
||||
if (port == NULL || strlen(port) == 0) {
|
||||
fprintf(stderr, "inet_dgram: port not specified\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (qemu_opt_get_bool(opts, "ipv4", 0))
|
||||
ai.ai_family = PF_INET;
|
||||
if (qemu_opt_get_bool(opts, "ipv6", 0))
|
||||
ai.ai_family = PF_INET6;
|
||||
|
||||
if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
|
||||
fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
|
||||
gai_strerror(rc));
|
||||
return -1;
|
||||
}
|
||||
if (sockets_debug) {
|
||||
fprintf(stderr, "%s: peer (%s:%s)\n", __FUNCTION__, addr, port);
|
||||
inet_print_addrinfo(__FUNCTION__, peer);
|
||||
}
|
||||
|
||||
/* lookup local addr */
|
||||
memset(&ai,0, sizeof(ai));
|
||||
ai.ai_flags = AI_PASSIVE;
|
||||
ai.ai_family = peer->ai_family;
|
||||
ai.ai_socktype = SOCK_DGRAM;
|
||||
|
||||
addr = qemu_opt_get(opts, "localaddr");
|
||||
port = qemu_opt_get(opts, "localport");
|
||||
if (addr == NULL || strlen(addr) == 0) {
|
||||
addr = NULL;
|
||||
}
|
||||
if (!port || strlen(port) == 0)
|
||||
port = "0";
|
||||
|
||||
if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
|
||||
fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
|
||||
gai_strerror(rc));
|
||||
return -1;
|
||||
}
|
||||
if (sockets_debug) {
|
||||
fprintf(stderr, "%s: local (%s:%s)\n", __FUNCTION__, addr, port);
|
||||
inet_print_addrinfo(__FUNCTION__, local);
|
||||
}
|
||||
|
||||
/* create socket */
|
||||
sock = socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
|
||||
if (sock < 0) {
|
||||
fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
|
||||
inet_strfamily(peer->ai_family), strerror(errno));
|
||||
goto err;
|
||||
}
|
||||
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
|
||||
|
||||
/* bind socket */
|
||||
if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
|
||||
uaddr,INET6_ADDRSTRLEN,uport,32,
|
||||
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
|
||||
fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
|
||||
goto err;
|
||||
}
|
||||
if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
|
||||
fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
|
||||
inet_strfamily(local->ai_family), uaddr, inet_getport(local));
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* connect to peer */
|
||||
if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
|
||||
uaddr, INET6_ADDRSTRLEN, uport, 32,
|
||||
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
|
||||
fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
|
||||
goto err;
|
||||
}
|
||||
if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
|
||||
fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
|
||||
inet_strfamily(peer->ai_family),
|
||||
peer->ai_canonname, uaddr, uport, strerror(errno));
|
||||
goto err;
|
||||
}
|
||||
|
||||
freeaddrinfo(local);
|
||||
freeaddrinfo(peer);
|
||||
return sock;
|
||||
|
||||
err:
|
||||
if (-1 != sock)
|
||||
closesocket(sock);
|
||||
if (local)
|
||||
freeaddrinfo(local);
|
||||
if (peer)
|
||||
freeaddrinfo(peer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* compatibility wrapper */
|
||||
static int inet_parse(QemuOpts *opts, const char *str)
|
||||
{
|
||||
|
@ -41,6 +41,7 @@ int inet_listen(const char *str, char *ostr, int olen,
|
||||
int socktype, int port_offset);
|
||||
int inet_connect_opts(QemuOpts *opts);
|
||||
int inet_connect(const char *str, int socktype);
|
||||
int inet_dgram_opts(QemuOpts *opts);
|
||||
|
||||
int unix_listen_opts(QemuOpts *opts);
|
||||
int unix_listen(const char *path, char *ostr, int olen);
|
||||
|
Loading…
Reference in New Issue
Block a user