Commit 63a01ef8 authored by aliguori's avatar aliguori
Browse files

Move network redirection code out of vl.c and into net.c



Mostly code motion.
Signed-off-by: default avatarAnthony Liguori <aliguori@us.ibm.com>



git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@5581 c046a42c-6fe2-441c-8c8c-71466251a162
parent 6f97dba0
......@@ -580,6 +580,7 @@ ifndef CONFIG_USER_ONLY
OBJS=vl.o osdep.o monitor.o pci.o loader.o isa_mmio.o machine.o net-checksum.o
OBJS+=fw_cfg.o aio.o buffered_file.o migration.o migration-tcp.o qemu-char.o
OBJS+=net.o
ifdef CONFIG_WIN32
OBJS+=block-raw-win32.o
else
......
/*
* QEMU System Emulator
*
* Copyright (c) 2003-2008 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "hw/hw.h"
#include "hw/boards.h"
#include "hw/usb.h"
#include "hw/pcmcia.h"
#include "hw/pc.h"
#include "hw/audiodev.h"
#include "hw/isa.h"
#include "hw/baum.h"
#include "hw/bt.h"
#include "net.h"
#include "console.h"
#include "sysemu.h"
#include "gdbstub.h"
#include "qemu-timer.h"
#include "qemu-char.h"
#include "block.h"
#include "audio/audio.h"
#include "migration.h"
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <sys/time.h>
#include <zlib.h>
#ifndef _WIN32
#include <sys/times.h>
#include <sys/wait.h>
#include <termios.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <dirent.h>
#include <netdb.h>
#include <sys/select.h>
#include <arpa/inet.h>
#ifdef _BSD
#include <sys/stat.h>
#if !defined(__APPLE__) && !defined(__OpenBSD__)
#include <libutil.h>
#endif
#ifdef __OpenBSD__
#include <net/if.h>
#endif
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
#include <freebsd/stdlib.h>
#else
#ifdef __linux__
#include <linux/if.h>
#include <linux/if_tun.h>
#include <pty.h>
#include <malloc.h>
#include <linux/rtc.h>
/* For the benefit of older linux systems which don't supply it,
we use a local copy of hpet.h. */
/* #include <linux/hpet.h> */
#include "hpet.h"
#include <linux/ppdev.h>
#include <linux/parport.h>
#endif
#ifdef __sun__
#include <sys/stat.h>
#include <sys/ethernet.h>
#include <sys/sockio.h>
#include <netinet/arp.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h> // must come after ip.h
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <net/if.h>
#include <syslog.h>
#include <stropts.h>
#endif
#endif
#endif
#include "qemu_socket.h"
#if defined(CONFIG_SLIRP)
#include "libslirp.h"
#endif
#if defined(__OpenBSD__)
#include <util.h>
#endif
#if defined(CONFIG_VDE)
#include <libvdeplug.h>
#endif
#ifdef _WIN32
#include <malloc.h>
#include <sys/timeb.h>
#include <mmsystem.h>
#define getopt_long_only getopt_long
#define memalign(align, size) malloc(size)
#endif
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
#ifdef __sun__
#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
#else
#define SMBD_COMMAND "/usr/sbin/smbd"
#endif
static VLANState *first_vlan;
/***********************************************************/
/* network device redirectors */
#if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
static void hex_dump(FILE *f, const uint8_t *buf, int size)
{
int len, i, j, c;
for(i=0;i<size;i+=16) {
len = size - i;
if (len > 16)
len = 16;
fprintf(f, "%08x ", i);
for(j=0;j<16;j++) {
if (j < len)
fprintf(f, " %02x", buf[i+j]);
else
fprintf(f, " ");
}
fprintf(f, " ");
for(j=0;j<len;j++) {
c = buf[i+j];
if (c < ' ' || c > '~')
c = '.';
fprintf(f, "%c", c);
}
fprintf(f, "\n");
}
}
#endif
static int parse_macaddr(uint8_t *macaddr, const char *p)
{
int i;
char *last_char;
long int offset;
errno = 0;
offset = strtol(p, &last_char, 0);
if (0 == errno && '\0' == *last_char &&
offset >= 0 && offset <= 0xFFFFFF) {
macaddr[3] = (offset & 0xFF0000) >> 16;
macaddr[4] = (offset & 0xFF00) >> 8;
macaddr[5] = offset & 0xFF;
return 0;
} else {
for(i = 0; i < 6; i++) {
macaddr[i] = strtol(p, (char **)&p, 16);
if (i == 5) {
if (*p != '\0')
return -1;
} else {
if (*p != ':' && *p != '-')
return -1;
p++;
}
}
return 0;
}
return -1;
}
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
{
const char *p, *p1;
int len;
p = *pp;
p1 = strchr(p, sep);
if (!p1)
return -1;
len = p1 - p;
p1++;
if (buf_size > 0) {
if (len > buf_size - 1)
len = buf_size - 1;
memcpy(buf, p, len);
buf[len] = '\0';
}
*pp = p1;
return 0;
}
int parse_host_src_port(struct sockaddr_in *haddr,
struct sockaddr_in *saddr,
const char *input_str)
{
char *str = strdup(input_str);
char *host_str = str;
char *src_str;
const char *src_str2;
char *ptr;
/*
* Chop off any extra arguments at the end of the string which
* would start with a comma, then fill in the src port information
* if it was provided else use the "any address" and "any port".
*/
if ((ptr = strchr(str,',')))
*ptr = '\0';
if ((src_str = strchr(input_str,'@'))) {
*src_str = '\0';
src_str++;
}
if (parse_host_port(haddr, host_str) < 0)
goto fail;
src_str2 = src_str;
if (!src_str || *src_str == '\0')
src_str2 = ":0";
if (parse_host_port(saddr, src_str2) < 0)
goto fail;
free(str);
return(0);
fail:
free(str);
return -1;
}
int parse_host_port(struct sockaddr_in *saddr, const char *str)
{
char buf[512];
struct hostent *he;
const char *p, *r;
int port;
p = str;
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
return -1;
saddr->sin_family = AF_INET;
if (buf[0] == '\0') {
saddr->sin_addr.s_addr = 0;
} else {
if (isdigit(buf[0])) {
if (!inet_aton(buf, &saddr->sin_addr))
return -1;
} else {
if ((he = gethostbyname(buf)) == NULL)
return - 1;
saddr->sin_addr = *(struct in_addr *)he->h_addr;
}
}
port = strtol(p, (char **)&r, 0);
if (r == p)
return -1;
saddr->sin_port = htons(port);
return 0;
}
#ifndef _WIN32
int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
{
const char *p;
int len;
len = MIN(108, strlen(str));
p = strchr(str, ',');
if (p)
len = MIN(len, p - str);
memset(uaddr, 0, sizeof(*uaddr));
uaddr->sun_family = AF_UNIX;
memcpy(uaddr->sun_path, str, len);
return 0;
}
#endif
VLANClientState *qemu_new_vlan_client(VLANState *vlan,
IOReadHandler *fd_read,
IOCanRWHandler *fd_can_read,
void *opaque)
{
VLANClientState *vc, **pvc;
vc = qemu_mallocz(sizeof(VLANClientState));
if (!vc)
return NULL;
vc->fd_read = fd_read;
vc->fd_can_read = fd_can_read;
vc->opaque = opaque;
vc->vlan = vlan;
vc->next = NULL;
pvc = &vlan->first_client;
while (*pvc != NULL)
pvc = &(*pvc)->next;
*pvc = vc;
return vc;
}
void qemu_del_vlan_client(VLANClientState *vc)
{
VLANClientState **pvc = &vc->vlan->first_client;
while (*pvc != NULL)
if (*pvc == vc) {
*pvc = vc->next;
free(vc);
break;
} else
pvc = &(*pvc)->next;
}
int qemu_can_send_packet(VLANClientState *vc1)
{
VLANState *vlan = vc1->vlan;
VLANClientState *vc;
for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
if (vc != vc1) {
if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
return 1;
}
}
return 0;
}
void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
{
VLANState *vlan = vc1->vlan;
VLANClientState *vc;
#ifdef DEBUG_NET
printf("vlan %d send:\n", vlan->id);
hex_dump(stdout, buf, size);
#endif
for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
if (vc != vc1) {
vc->fd_read(vc->opaque, buf, size);
}
}
}
#if defined(CONFIG_SLIRP)
/* slirp network adapter */
static int slirp_inited;
static VLANClientState *slirp_vc;
int slirp_can_output(void)
{
return !slirp_vc || qemu_can_send_packet(slirp_vc);
}
void slirp_output(const uint8_t *pkt, int pkt_len)
{
#ifdef DEBUG_SLIRP
printf("slirp output:\n");
hex_dump(stdout, pkt, pkt_len);
#endif
if (!slirp_vc)
return;
qemu_send_packet(slirp_vc, pkt, pkt_len);
}
int slirp_is_inited(void)
{
return slirp_inited;
}
static void slirp_receive(void *opaque, const uint8_t *buf, int size)
{
#ifdef DEBUG_SLIRP
printf("slirp input:\n");
hex_dump(stdout, buf, size);
#endif
slirp_input(buf, size);
}
static int net_slirp_init(VLANState *vlan)
{
if (!slirp_inited) {
slirp_inited = 1;
slirp_init();
}
slirp_vc = qemu_new_vlan_client(vlan,
slirp_receive, NULL, NULL);
snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
return 0;
}
void net_slirp_redir(const char *redir_str)
{
int is_udp;
char buf[256], *r;
const char *p;
struct in_addr guest_addr;
int host_port, guest_port;
if (!slirp_inited) {
slirp_inited = 1;
slirp_init();
}
p = redir_str;
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
goto fail;
if (!strcmp(buf, "tcp")) {
is_udp = 0;
} else if (!strcmp(buf, "udp")) {
is_udp = 1;
} else {
goto fail;
}
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
goto fail;
host_port = strtol(buf, &r, 0);
if (r == buf)
goto fail;
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
goto fail;
if (buf[0] == '\0') {
pstrcpy(buf, sizeof(buf), "10.0.2.15");
}
if (!inet_aton(buf, &guest_addr))
goto fail;
guest_port = strtol(p, &r, 0);
if (r == p)
goto fail;
if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
fprintf(stderr, "qemu: could not set up redirection\n");
exit(1);
}
return;
fail:
fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
exit(1);
}
#ifndef _WIN32
static char smb_dir[1024];
static void erase_dir(char *dir_name)
{
DIR *d;
struct dirent *de;
char filename[1024];
/* erase all the files in the directory */
if ((d = opendir(dir_name)) != 0) {
for(;;) {
de = readdir(d);
if (!de)
break;
if (strcmp(de->d_name, ".") != 0 &&
strcmp(de->d_name, "..") != 0) {
snprintf(filename, sizeof(filename), "%s/%s",
smb_dir, de->d_name);
if (unlink(filename) != 0) /* is it a directory? */
erase_dir(filename);
}
}
closedir(d);
rmdir(dir_name);
}
}
/* automatic user mode samba server configuration */
static void smb_exit(void)
{
erase_dir(smb_dir);
}
/* automatic user mode samba server configuration */
void net_slirp_smb(const char *exported_dir)
{
char smb_conf[1024];
char smb_cmdline[1024];
FILE *f;
if (!slirp_inited) {
slirp_inited = 1;
slirp_init();
}
/* XXX: better tmp dir construction */
snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
if (mkdir(smb_dir, 0700) < 0) {
fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
exit(1);
}
snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
f = fopen(smb_conf, "w");
if (!f) {
fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
exit(1);
}
fprintf(f,
"[global]\n"
"private dir=%s\n"
"smb ports=0\n"
"socket address=127.0.0.1\n"
"pid directory=%s\n"
"lock directory=%s\n"
"log file=%s/log.smbd\n"
"smb passwd file=%s/smbpasswd\n"
"security = share\n"
"[qemu]\n"
"path=%s\n"
"read only=no\n"
"guest ok=yes\n",
smb_dir,
smb_dir,
smb_dir,
smb_dir,
smb_dir,
exported_dir
);
fclose(f);
atexit(smb_exit);
snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
SMBD_COMMAND, smb_conf);
slirp_add_exec(0, smb_cmdline, 4, 139);
}
#endif /* !defined(_WIN32) */
void do_info_slirp(void)
{
slirp_stats();
}
#endif /* CONFIG_SLIRP */
#if !defined(_WIN32)
typedef struct TAPState {
VLANClientState *vc;
int fd;
char down_script[1024];
} TAPState;
static void tap_receive(void *opaque, const uint8_t *buf, int size)
{
TAPState *s = opaque;
int ret;
for(;;) {
ret = write(s->fd, buf, size);
if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
} else {
break;
}
}
}
static void tap_send(void *opaque)
{
TAPState *s = opaque;
uint8_t buf[4096];
int size;
#ifdef __sun__
struct strbuf sbuf;
int f = 0;
sbuf.maxlen = sizeof(buf);
sbuf.buf = buf;
size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
#else
size = read(s->fd, buf, sizeof(buf));
#endif
if (size > 0) {
qemu_send_packet(s->vc, buf, size);
}
}
/* fd support */
static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
{
TAPState *s;
s = qemu_mallocz(sizeof(TAPState));
if (!s)
return NULL;
s->fd = fd;
s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
qemu_set_fd_handler(s->fd, tap_send, NULL, s);
snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
return s;
}