const char *name,
IOReadHandler *fd_read,
IOCanRWHandler *fd_can_read,
+ NetCleanup *cleanup,
void *opaque)
{
VLANClientState *vc, **pvc;
vc->name = assign_name(vc, model);
vc->fd_read = fd_read;
vc->fd_can_read = fd_can_read;
+ vc->cleanup = cleanup;
vc->opaque = opaque;
vc->vlan = vlan;
while (*pvc != NULL)
if (*pvc == vc) {
*pvc = vc->next;
+ if (vc->cleanup) {
+ vc->cleanup(vc);
+ }
free(vc->name);
free(vc->model);
- free(vc);
+ qemu_free(vc);
break;
} else
pvc = &(*pvc)->next;
slirp_init(slirp_restrict, slirp_ip);
}
slirp_vc = qemu_new_vlan_client(vlan, model, name,
- slirp_receive, NULL, NULL);
+ slirp_receive, NULL, NULL, NULL);
slirp_vc->info_str[0] = '\0';
return 0;
}
char down_script_arg[128];
} TAPState;
+static int launch_script(const char *setup_script, const char *ifname, int fd);
+
static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
int iovcnt)
{
}
}
+static void tap_cleanup(VLANClientState *vc)
+{
+ TAPState *s = vc->opaque;
+
+ if (s->down_script[0])
+ launch_script(s->down_script, s->down_script_arg, s->fd);
+
+ qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+ close(s->fd);
+ qemu_free(s);
+}
+
/* fd support */
static TAPState *net_tap_fd_init(VLANState *vlan,
s = qemu_mallocz(sizeof(TAPState));
s->fd = fd;
- s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive, NULL, s);
+ s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive,
+ NULL, tap_cleanup, s);
s->vc->fd_readv = tap_receive_iov;
qemu_set_fd_handler(s->fd, tap_send, NULL, s);
snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
return -1;
}
s = net_tap_fd_init(vlan, model, name, fd);
- if (!s)
- return -1;
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
"ifname=%s,script=%s,downscript=%s",
ifname, setup_script, down_script);
}
}
+static void vde_cleanup(VLANClientState *vc)
+{
+ VDEState *s = vc->opaque;
+ qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
+ vde_close(s->vde);
+ qemu_free(s);
+}
+
static int net_vde_init(VLANState *vlan, const char *model,
const char *name, const char *sock,
int port, const char *group, int mode)
free(s);
return -1;
}
- s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu, NULL, s);
+ s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu,
+ NULL, vde_cleanup, s);
qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
sock, vde_datafd(s->vde));
return -1;
}
+static void net_socket_cleanup(VLANClientState *vc)
+{
+ NetSocketState *s = vc->opaque;
+ qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+ close(s->fd);
+ qemu_free(s);
+}
+
static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
const char *model,
const char *name,
s = qemu_mallocz(sizeof(NetSocketState));
s->fd = fd;
- s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram, NULL, s);
+ s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram,
+ NULL, net_socket_cleanup, s);
qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
/* mcast: save bound address as dst */
NetSocketState *s;
s = qemu_mallocz(sizeof(NetSocketState));
s->fd = fd;
- s->vc = qemu_new_vlan_client(vlan, model, name,
- net_socket_receive, NULL, s);
+ s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive,
+ NULL, net_socket_cleanup, s);
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
"socket: fd=%d", fd);
if (is_connected) {
}
s->vlan = vlan;
s->model = strdup(model);
- s->name = strdup(name);
+ s->name = name ? strdup(name) : NULL;
s->fd = fd;
qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
return 0;
vlan_id = strtol(buf, NULL, 0);
}
vlan = qemu_find_vlan(vlan_id);
- if (!vlan) {
- fprintf(stderr, "Could not create vlan %d\n", vlan_id);
- return -1;
- }
+
if (get_param_value(buf, sizeof(buf), "name", p)) {
name = strdup(buf);
}
if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
fd = strtol(buf, NULL, 0);
fcntl(fd, F_SETFL, O_NONBLOCK);
- ret = -1;
- if (net_tap_fd_init(vlan, device, name, fd))
- ret = 0;
+ net_tap_fd_init(vlan, device, name, fd);
+ ret = 0;
} else {
if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
ifname[0] = '\0';
VLANClientState *vc;
vlan = qemu_find_vlan(vlan_id);
- if (!vlan) {
- term_printf("can't find vlan %d\n", vlan_id);
- return;
- }
for(vc = vlan->first_client; vc != NULL; vc = vc->next)
if (!strcmp(vc->name, device))
{
VLANState *vlan;
-#if !defined(_WIN32)
/* close network clients */
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
- VLANClientState *vc;
+ VLANClientState *vc = vlan->first_client;
- for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
- if (vc->fd_read == tap_receive) {
- TAPState *s = vc->opaque;
+ while (vc) {
+ VLANClientState *next = vc->next;
- if (s->down_script[0])
- launch_script(s->down_script, s->down_script_arg, s->fd);
- }
-#if defined(CONFIG_VDE)
- if (vc->fd_read == vde_from_qemu) {
- VDEState *s = vc->opaque;
- vde_close(s->vde);
- }
-#endif
+ qemu_del_vlan_client(vc);
+
+ vc = next;
}
}
-#endif
}
void net_client_check(void)