chardev/char-socket: Function headers refactoring

Upcoming websocket support requires additional parameters in function
headers that are already overloaded. This patch replaces the bunch of
parameters with a single structure pointer.

Signed-off-by: Julia Suvorova <jusual@mail.ru>
Message-Id: <20181018223501.21683-2-jusual@mail.ru>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Julia Suvorova 2018-10-19 01:34:59 +03:00 committed by Marc-André Lureau
parent 4591bd46e6
commit 4493b6a54c

View File

@ -389,30 +389,37 @@ static void tcp_chr_free_connection(Chardev *chr)
s->connected = 0; s->connected = 0;
} }
static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr, static const char *qemu_chr_socket_protocol(SocketChardev *s)
bool is_listen, bool is_telnet)
{ {
switch (addr->type) { if (s->is_telnet) {
return "telnet";
}
return "tcp";
}
static char *qemu_chr_socket_address(SocketChardev *s, const char *prefix)
{
switch (s->addr->type) {
case SOCKET_ADDRESS_TYPE_INET: case SOCKET_ADDRESS_TYPE_INET:
return g_strdup_printf("%s%s:%s:%s%s", prefix, return g_strdup_printf("%s%s:%s:%s%s", prefix,
is_telnet ? "telnet" : "tcp", qemu_chr_socket_protocol(s),
addr->u.inet.host, s->addr->u.inet.host,
addr->u.inet.port, s->addr->u.inet.port,
is_listen ? ",server" : ""); s->is_listen ? ",server" : "");
break; break;
case SOCKET_ADDRESS_TYPE_UNIX: case SOCKET_ADDRESS_TYPE_UNIX:
return g_strdup_printf("%sunix:%s%s", prefix, return g_strdup_printf("%sunix:%s%s", prefix,
addr->u.q_unix.path, s->addr->u.q_unix.path,
is_listen ? ",server" : ""); s->is_listen ? ",server" : "");
break; break;
case SOCKET_ADDRESS_TYPE_FD: case SOCKET_ADDRESS_TYPE_FD:
return g_strdup_printf("%sfd:%s%s", prefix, addr->u.fd.str, return g_strdup_printf("%sfd:%s%s", prefix, s->addr->u.fd.str,
is_listen ? ",server" : ""); s->is_listen ? ",server" : "");
break; break;
case SOCKET_ADDRESS_TYPE_VSOCK: case SOCKET_ADDRESS_TYPE_VSOCK:
return g_strdup_printf("%svsock:%s:%s", prefix, return g_strdup_printf("%svsock:%s:%s", prefix,
addr->u.vsock.cid, s->addr->u.vsock.cid,
addr->u.vsock.port); s->addr->u.vsock.port);
default: default:
abort(); abort();
} }
@ -424,8 +431,7 @@ static void update_disconnected_filename(SocketChardev *s)
g_free(chr->filename); g_free(chr->filename);
if (s->addr) { if (s->addr) {
chr->filename = SocketAddress_to_str("disconnected:", s->addr, chr->filename = qemu_chr_socket_address(s, "disconnected:");
s->is_listen, s->is_telnet);
} else { } else {
chr->filename = g_strdup("disconnected:socket"); chr->filename = g_strdup("disconnected:socket");
} }
@ -514,10 +520,12 @@ static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
return size; return size;
} }
static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len, static char *qemu_chr_compute_filename(SocketChardev *s)
struct sockaddr_storage *ps, socklen_t ps_len,
bool is_listen, bool is_telnet)
{ {
struct sockaddr_storage *ss = &s->sioc->localAddr;
struct sockaddr_storage *ps = &s->sioc->remoteAddr;
socklen_t ss_len = s->sioc->localAddrLen;
socklen_t ps_len = s->sioc->remoteAddrLen;
char shost[NI_MAXHOST], sserv[NI_MAXSERV]; char shost[NI_MAXHOST], sserv[NI_MAXSERV];
char phost[NI_MAXHOST], pserv[NI_MAXSERV]; char phost[NI_MAXHOST], pserv[NI_MAXSERV];
const char *left = "", *right = ""; const char *left = "", *right = "";
@ -527,7 +535,7 @@ static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len,
case AF_UNIX: case AF_UNIX:
return g_strdup_printf("unix:%s%s", return g_strdup_printf("unix:%s%s",
((struct sockaddr_un *)(ss))->sun_path, ((struct sockaddr_un *)(ss))->sun_path,
is_listen ? ",server" : ""); s->is_listen ? ",server" : "");
#endif #endif
case AF_INET6: case AF_INET6:
left = "["; left = "[";
@ -539,9 +547,9 @@ static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len,
getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost), getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV); pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s", return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
is_telnet ? "telnet" : "tcp", qemu_chr_socket_protocol(s),
left, shost, right, sserv, left, shost, right, sserv,
is_listen ? ",server" : "", s->is_listen ? ",server" : "",
left, phost, right, pserv); left, phost, right, pserv);
default: default:
@ -576,10 +584,7 @@ static void tcp_chr_connect(void *opaque)
SocketChardev *s = SOCKET_CHARDEV(opaque); SocketChardev *s = SOCKET_CHARDEV(opaque);
g_free(chr->filename); g_free(chr->filename);
chr->filename = sockaddr_to_str( chr->filename = qemu_chr_compute_filename(s);
&s->sioc->localAddr, s->sioc->localAddrLen,
&s->sioc->remoteAddr, s->sioc->remoteAddrLen,
s->is_listen, s->is_telnet);
s->connected = 1; s->connected = 1;
update_ioc_handlers(s); update_ioc_handlers(s);