qga added functionality
cover qga added functionality
From: Itamar Tal <[email protected]>
This bundle of patches adds support for some more functionality in the qemu-guest-agent, both for Windows and Linux. Main added features are:
- Process list enumeration
- Network connections enumeration
- File removal and attributes
- File hashing
- System information such as uptime and hostname
Itamar,
Guardicore
[email protected]
Itamar Tal (4):
added qga hash file command (win/linux)
addedd missing file commands (attributes, deletion)
added qga system uptime and hostname commands
added qga processes and connections enumeration commands (win/linux)
qga/Makefile.objs | 2 +- qga/commands-posix.c | 644 ++++++++++++++++++++++++++++++++++++++++++++++++ qga/commands-win32.c | 561 +++++++++++++++++++++++++++++++++++++++++ qga/commands.c | 69 ++++++ qga/guest-agent-core.h | 8 + qga/main.c | 5 + qga/qapi-schema.json | 179 ++++++++++++++ qga/sha256.c | 171 +++++++++++++ qga/sha256.h | 40 +++ qga/win32-definitions.h | 110 +++++++++ qga/win32-iptypes.h | 412 +++++++++++++++++++++++++++++++ 11 files changed, 2200 insertions(+), 1 deletion(-) create mode 100644 qga/sha256.c create mode 100644 qga/sha256.h create mode 100644 qga/win32-definitions.h create mode 100644 qga/win32-iptypes.h
Signature · 1 line
2.3.4
PATCH 1/4 added qga hash file command (win/linux)
From: Itamar Tal <[email protected]>
Added support for retrieving file hash from the guest machine. This is command is useful for ensuring that no changes were made for a given file between consecutive read operations and reduce read/write operations.
--- qga/Makefile.objs | 2 +- qga/commands.c | 43 +++++++++++++ qga/guest-agent-core.h | 6 ++ qga/qapi-schema.json | 13 ++++ qga/sha256.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++++ qga/sha256.h | 40 ++++++++++++ 6 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 qga/sha256.c create mode 100644 qga/sha256.h
qga/Makefile.objs +1 -1
@@ -1,4 +1,4 @@ -qga-obj-y = commands.o guest-agent-command-state.o main.o +qga-obj-y = commands.o guest-agent-command-state.o main.o sha256.o qga-obj-$(CONFIG_POSIX) += commands-posix.o channel-posix.o qga-obj-$(CONFIG_WIN32) += commands-win32.o channel-win32.o service-win32.o qga-obj-$(CONFIG_WIN32) += vss-win32.o
qga/commands.c +43 -0
52 lines not on this page · raw message
qga/guest-agent-core.h +6 -0
@@ -10,6 +10,10 @@* This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory. */ + +#ifndef _GUEST_AGENT_CORE_H_ +#define _GUEST_AGENT_CORE_H_ +#include "qapi/qmp/dispatch.h" #include "qemu-common.h"
@@ -41,3 +45,5 @@ int64_t ga_get_fd_handle(GAState *s, Error **errp);#ifndef _WIN32void reopen_fd_to_null (int fd);#endif + +#endif /* _GUEST_AGENT_CORE_H_ */
qga/qapi-schema.json +13 -0
@@ -891,3 +891,16 @@ ## { 'command': 'guest-get-memory-block-info', 'returns': 'GuestMemoryBlockInfo' } + +## +# @guest-file-hash: +# +# Get the a SHA256 hash for a file in the guest's operating system +# +# Returns: file SHA256 hash string +# +# Since 2.4 +## +{ 'command': 'guest-file-hash', + 'data': { 'path': 'str' }, + 'returns': 'str' }
qga/sha256.c new file +171 -0
171 lines not on this page · raw message
qga/sha256.h new file +40 -0
@@ -0,0 +1,40 @@ +/********************************************************************* +* Filename: sha256.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding SHA1 implementation. +*********************************************************************/ + +#ifndef _SHA256_H_INCLUDE +#define _SHA256_H_INCLUDE + +/*************************** HEADER FILES ***************************/ + +#include <stddef.h> +#include <stdlib.h> +#include <stdio.h> +#include "qga/guest-agent-core.h" + +/****************************** MACROS ******************************/ +#define SHA256_BLOCK_SIZE 32/* SHA256 outputs a 32 byte digest */ + +/**************************** DATA TYPES ****************************/ + +typedef unsigned char BYTE ;/* 8-bit byte */ + +typedef struct { +BYTE data[64 ]; +unsigned int datalen; +unsigned long long bitlen; +unsigned int state[8 ]; +}SHA256_CTX ; + +/*********************** FUNCTION DECLARATIONS **********************/ + +void sha256_init (SHA256_CTX *ctx); +void sha256_update (SHA256_CTX *ctx,const BYTE data[],size_t len); +void sha256_final (SHA256_CTX *ctx,BYTE hash[]); +void sha256_transform (SHA256_CTX *ctx,const BYTE data[]); + +#endif /* _SHA256_H_INCLUDE */
Signature · 1 line
2.3.4
PATCH 2/4 added missing file commands (attributes, deletion)
In reply to [email protected]From: Itamar Tal <[email protected]>
This patch add support for retrieving file attributes and file deletion by file name, to get a more complete file functionality over the guest agent.
--- qga/commands-posix.c | 23 +++++++++ qga/commands-win32.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ qga/commands.c | 8 +++ qga/qapi-schema.json | 42 ++++++++++++++++ 4 files changed, 212 insertions(+)
qga/commands-posix.c +23 -0
@@ -7,6 +7,9 @@* Michael Roth <[email protected]> * Michal Privoznik <[email protected]> * +* Changes ([email protected]): +* - file attributes, removal, hashes +* * This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory. */
@@ -2456,3 +2459,23 @@ void ga_command_state_init(GAState *s, GACommandState *cs)#endif ga_command_state_add (cs, guest_file_init,NULL ); } + +GuestFileAttributes *qmp_guest_file_attributes (const char *path,Error **errp) +{ +GuestFileAttributes *file_stat =g_malloc0 (sizeof (GuestFileAttributes)); +struct stat file_os_stat; + +if (stat (path, &file_os_stat)) { +error_setg (errp,"Failed to get file attributes for '%s' (error %d)" , + path, errno); +return NULL ; + } + + file_stat->mode = file_os_stat.st_mode; + file_stat->size = file_os_stat.st_size; + file_stat->access_time = file_os_stat.st_atime; + file_stat->modification_time = file_os_stat.st_mtime; + file_stat->creation_time =0 ;/* not all Linux FS store file BoD */ + +return file_stat; +}
qga/commands-win32.c +139 -0
148 lines not on this page · raw message
qga/commands.c +8 -0
@@ -113,3 +113,11 @@ char *qmp_guest_file_hash(const char *path, Error **errp)return g_strdup ((char *)sha256_hexdigest); } + +void qmp_guest_file_remove (const char *path,Error **errp) +{ +if (unlink (path)) { +error_setg (errp,"Error deleting file '%s' (error %d)" , path, errno); + errno =0 ; + } +}
qga/qapi-schema.json +42 -0
45 lines not on this page · raw message
Signature · 1 line
2.3.4
PATCH 3/4 added qga system uptime and hostname commands
In reply to [email protected]From: Itamar Tal <[email protected]>
This patch add more missing commands to the qga (both Windows and Linux):
- retrieving system uptime in seconds
- retrieving system hostname
--- qga/commands-posix.c | 13 +++++++++++++ qga/commands-win32.c | 22 ++++++++++++++++++++++ qga/commands.c | 18 ++++++++++++++++++ qga/guest-agent-core.h | 2 ++ qga/main.c | 5 +++++ qga/qapi-schema.json | 25 +++++++++++++++++++++++++ 6 files changed, 85 insertions(+)
qga/commands-posix.c +13 -0
@@ -17,6 +17,7 @@#include <glib.h> #include <sys/types.h> #include <sys/ioctl.h> +#include <sys/sysinfo.h> #include <sys/wait.h> #include <unistd.h> #include <errno.h>
@@ -2479,3 +2480,15 @@ GuestFileAttributes *qmp_guest_file_attributes(const char *path, Error **errp)return file_stat; } + +uint32_t qmp_guest_uptime (Error **errp) +{ +struct sysinfo sys_info; +if (sysinfo (&sys_info)) { +error_setg (errp,"Failed reading system info" ); + } + +return sys_info.uptime; +} + +
qga/commands-win32.c +22 -0
@@ -385,6 +385,21 @@ static void guest_file_init(void)QTAILQ_INIT (&guest_file_state.filehandles); } +int ga_win_commands_init (void ) +{ +WSADATA wsa_data = {0 }; + +if (WSAStartup (MAKEWORD (2 ,2 ), &wsa_data)) { +g_critical ("failed to initialize WSA engine" ); + goto error; + } + +return 1 ; + +error: +return 0 ; +} +GuestFilesystemInfoList *qmp_guest_get_fsinfo (Error **errp) {error_set (errp,QERR_UNSUPPORTED );
@@ -881,3 +896,10 @@ GuestFileAttributes *qmp_guest_file_attributes(const char *path, Error **errp)return result; } +uint32_t qmp_guest_uptime (Error **errp) +{ +uint32_t uptime_milli =GetTickCount (); +return uptime_milli /1000 ; +} + +
qga/commands.c +18 -0
@@ -121,3 +121,21 @@ void qmp_guest_file_remove(const char *path, Error **errp) errno =0 ; } } + +char *qmp_guest_get_hostname (Error **errp) +{ +char hostname[64 ]; + +if (gethostname (hostname,64 )) { +#ifdef _WIN32 +error_setg (errp,"Error getting hostname (error %d)" , + (int )WSAGetLastError ()); +#else +error_setg (errp,"Error getting hostname (error %d)" , errno); + errno =0 ; +#endif +return NULL ; + } + +return g_strdup (hostname); +}
qga/guest-agent-core.h +2 -0
@@ -44,6 +44,8 @@ int64_t ga_get_fd_handle(GAState *s, Error **errp);#ifndef _WIN32void reopen_fd_to_null (int fd); +#else +int ga_win_commands_init (void );#endif #endif /* _GUEST_AGENT_CORE_H_ */
qga/main.c +5 -0
@@ -1163,6 +1163,11 @@ int main(int argc, char **argv)g_critical ("failed to register signal handlers" ); goto out_bad; } +#else +if (!ga_win_commands_init ()) { +g_critical ("failed initializing commands module" ); + goto out_bad; + }#endif s->main_loop =g_main_loop_new (NULL , false);
qga/qapi-schema.json +25 -0
@@ -946,3 +946,28 @@ ## { 'command': 'guest-file-remove', 'data': { 'path': 'str' }} + +## +# @guest-get-hostname: +# +# Get the hostname of the guest's operating system +# +# Returns: hostname string. +# +# Since 2.4 +## +{ 'command': 'guest-get-hostname', + 'returns': 'str' } + +## +# @guest-uptime: +# +# Get the time in seconds since the guest machine operating system was started +# +# Returns: uptime in seconds +# +# Since 2.4 +## +{ 'command': 'guest-uptime', + 'returns': 'uint32' } +
Signature · 1 line
2.3.4
PATCH 4/4 added qga processes and connections enumeration commands (win/linux)
In reply to [email protected]From: Itamar Tal <[email protected]>
This patch add more missing commands to the qga (both Windows and Linux):
- current active process list
- current active connections list
the abovementioned commands are added to align the guest agent functionality with other common hypervisor agents.
--- qga/commands-posix.c | 608 ++++++++++++++++++++++++++++++++++++++++++++++++ qga/commands-win32.c | 404 +++++++++++++++++++++++++++++++- qga/qapi-schema.json | 99 ++++++++ qga/win32-definitions.h | 110 +++++++++ qga/win32-iptypes.h | 412 ++++++++++++++++++++++++++++++++ 5 files changed, 1631 insertions(+), 2 deletions(-) create mode 100644 qga/win32-definitions.h create mode 100644 qga/win32-iptypes.h
qga/commands-posix.c +608 -0
@@ -9,6 +9,7 @@* * Changes ([email protected]): * - file attributes, removal, hashes +* - added process and connection enumeration support * * This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory.
@@ -50,6 +51,53 @@ extern char **environ;#include <sys/socket.h> #include <net/if.h> +#define PRG_HASH_SIZE (211) + +typedef +struct prg_node { +struct prg_node *next; +int inode; +int pid; +}prg_node ; + +typedef +struct prg_hash { +struct prg_node *hash[PRG_HASH_SIZE ]; +}prg_hash ; + +#define PRG_LOCAL_ADDRESS "local_address" +#define PRG_INODE "inode" +#define PRG_SOCKET_PFX "socket:[" +#define PRG_SOCKET_PFXl (strlen(PRG_SOCKET_PFX)) +#define PRG_SOCKET_PFX2 "[0000]:" +#define PRG_SOCKET_PFX2l (strlen(PRG_SOCKET_PFX2)) + +#define PRG_HASHIT (x) ((x) % PRG_HASH_SIZE) +#ifndef LINE_MAX +#define LINE_MAX 4096 +#endif +#define PATH_PROC "/proc" +#define PATH_FD_SUFF "fd" +#define PATH_FD_SUFFl strlen(PATH_FD_SUFF) +#define PATH_PROC_X_FD PATH_PROC "/%s/" PATH_FD_SUFF +#define PATH_CMDLINE "cmdline" +#define PATH_CMDLINEl strlen(PATH_CMDLINE) + +/* These enums are used by IPX too. :-( */ +enum { +TCP_ESTABLISHED =1 , +TCP_SYN_SENT , +TCP_SYN_RECV , +TCP_FIN_WAIT1 , +TCP_FIN_WAIT2 , +TCP_TIME_WAIT , +TCP_CLOSE , +TCP_CLOSE_WAIT , +TCP_LAST_ACK , +TCP_LISTEN , +TCP_CLOSING /* now a valid state */ +}; +#ifdef FIFREEZE #define CONFIG_FSFREEZE #endif
@@ -2491,4 +2539,564 @@ uint32_t qmp_guest_uptime(Error **errp)return sys_info.uptime; } +GuestProcessInfoList *qmp_guest_get_process_list (Error **errp) +{ +struct dirent *ent =NULL ; +GuestProcessInfoList *process_list_head =NULL ; +GuestProcessInfoList *process_list_last =NULL ; + +DIR *procfs =opendir ("/proc" ); + +if (NULL == procfs) { +error_setg (errp,"Failed opening procfs (error %d)" , errno); +return NULL ; + } + +/* read entries until EOD */ + errno =0 ; +while (NULL != (ent =readdir (procfs))) { +int pid =strtoul (ent->d_name,NULL ,10 ); +GuestProcessInfoList *list_item =NULL ; +char local_path[256 ]; +FILE *info_file =NULL ; +char info[256 ]; +size_t len; + +/* ensure directory name is a number */ +if ((0 == pid) || (ULONG_MAX == pid)) { +continue ; + } + + list_item =g_malloc0 (sizeof (GuestProcessInfoList)); +if (NULL == process_list_head) { + process_list_head = list_item; + } +if (NULL != process_list_last) { + process_list_last->next = list_item; + } + process_list_last = list_item; + + list_item->next =NULL ; + list_item->value =g_malloc0 (sizeof (GuestProcessInfo)); + list_item->value->process_id = pid; + +/* read process name */ +snprintf (local_path,256 ,"/proc/%d/comm" , pid); + info_file =fopen (local_path,"rb" ); +if (NULL != info_file) { + len =fread (info,1 ,256 , info_file); + info[len] ='\0' ; +if ((0 < len) && ('\n' == info[len -1 ])) { + info[len -1 ] ='\0' ; + } + list_item->value->process_name =g_strdup (info); + +fclose (info_file); + } + +/* read process parent id */ +snprintf (local_path,256 ,"/proc/%d/status" , pid); + info_file =fopen (local_path,"rb" ); +if (NULL != info_file) { +char *line =NULL ; +size_t line_size =0 ; + +/* read lines, search for ppid */ +for (;;) { + len =getline (&line, &line_size, info_file); +if (-1 == len) { +break ; + } + +if (line ==strstr (line,"PPid:" )) { + list_item->value->parent_id =strtoul (line +5 ,NULL ,10 ); + } + } + +fclose (info_file); + } + +/* read process session id */ +snprintf (local_path,256 ,"/proc/%d/sessionid" , pid); + info_file =fopen (local_path,"rb" ); +if (NULL != info_file) { + len =fread (info,1 ,256 , info_file); + info[len] ='\0' ; + list_item->value->session_id =strtoul (info,NULL ,10 ); + +fclose (info_file); + } + +/* read process image path */ +snprintf (local_path,256 ,"/proc/%d/exe" , pid); + len =readlink (local_path, info,sizeof (info) -1 ); +if (-1 != len) { + info[len] ='\0' ; + list_item->value->image_path =g_strdup (info); + } + + errno =0 ; + } + + (void )closedir (procfs); + +if (0 != errno) { +error_setg (errp,"Error while reading procfs directory (error %d)" , + errno); +return NULL ; + } + +return process_list_head; +} + +/* this function is taken out of netstat (found in net-tools) and modified */ +static void prg_cache_add (prg_hash *hash,int inode,int pid) +{ +unsigned hi =PRG_HASHIT (inode); +struct prg_node **pnp =NULL , *pn =NULL ; + +/* look for the inode in the table or find next free entry for the inode */ +for (pnp = &hash->hash[hi]; (pn = *pnp); pnp = &pn->next) { +if (pn->inode == inode) { +/* Some warning should be appropriate here +as we got multiple processes for one i-node */ +return ; + } + } + +/* allocate new entry for this inode */ + *pnp =g_malloc0 (sizeof (struct prg_node )); +if (NULL == *pnp) { +return ; + } + + pn = *pnp; + pn->next =NULL ; + pn->inode = inode; + pn->pid = pid; +} + +/* this function is taken out of netstat (found in net-tools) and modified */ +static int prg_cache_get (prg_hash *hash,int inode) +{ +unsigned hi =PRG_HASHIT (inode); +struct prg_node *pn =NULL ; + +for (pn = hash->hash[hi]; pn; pn = pn->next) { +if (pn->inode == inode) { +return pn->pid; + } + } + +return -1 ; +} + +/* this function is taken out of netstat (found in net-tools) and modified */ +static void prg_cache_clear (prg_hash *hash) +{ +struct prg_node **pnp =NULL , *pn =NULL ; + + +for (pnp = hash->hash; pnp < hash->hash +PRG_HASH_SIZE ; pnp++) { + pn = *pnp; +while (NULL != pn) { + *pnp = pn->next; +free (pn); + pn = *pnp; + } + } +} + +/* this function is taken out of netstat (found in net-tools) and modified */ +static void extract_type_1_socket_inode (const char lname[],long *inode_p) +{ + +/* If lname is of the form "socket:[12345]", extract the "12345" +as *inode_p. Otherwise, return -1 as *inode_p. +*/ + +if (strlen (lname) < PRG_SOCKET_PFXl+3 ) { + *inode_p =-1 ; + }else if (memcmp (lname,PRG_SOCKET_PFX , PRG_SOCKET_PFXl)) { + *inode_p =-1 ; + }else if (lname[strlen (lname)-1 ] !=']' ) { + *inode_p =-1 ; + }else { +char inode_str[strlen (lname +1 )];/* e.g. "12345" */ +const int inode_str_len =strlen (lname) - PRG_SOCKET_PFXl -1 ; +char *serr =NULL ; + +strncpy (inode_str, lname + PRG_SOCKET_PFXl, inode_str_len); + inode_str[inode_str_len] ='\0' ; + *inode_p =strtol (inode_str, &serr,0 ); +if (!serr || *serr || *inode_p <0 || *inode_p >=INT_MAX ) { + *inode_p =-1 ; + } + } +} + +/* this function is taken out of netstat (found in net-tools) and modified */ +static void extract_type_2_socket_inode (const char lname[],long *inode_p) +{ + +/* If lname is of the form "[0000]:12345", extract the "12345" +as *inode_p. Otherwise, return -1 as *inode_p. +*/ + +if (strlen (lname) < PRG_SOCKET_PFX2l+1 ) { + *inode_p =-1 ; + }else if (memcmp (lname,PRG_SOCKET_PFX2 , PRG_SOCKET_PFX2l)) { + *inode_p =-1 ; + }else { +char *serr =NULL ; + + *inode_p =strtol (lname + PRG_SOCKET_PFX2l, &serr,0 ); +if (!serr || *serr || *inode_p <0 || *inode_p >=INT_MAX ) { + *inode_p =-1 ; + } + } +} + +/* this function is taken out of netstat (found in net-tools) and modified */ +static int prg_cache_load (prg_hash *hash) +{ +char line[LINE_MAX ]; +int procfdlen, lnamelen; +char lname[30 ]; +long inode; +const char *cs =NULL ; +DIR *dirproc =NULL , *dirfd =NULL ; +struct dirent *direproc =NULL , *direfd =NULL ; + +/* reset the hash table */ +memset (hash,0 ,sizeof (prg_hash)); + + dirproc =opendir (PATH_PROC ); +if (NULL == dirproc) { +return 0 ; + } + +for (;;) { +int pid =0 ; + + errno =0 ; + direproc =readdir (dirproc); +if (NULL == direproc) { +break ; + } + +#ifdef DIRENT_HAVE_D_TYPE_WORKS +if (direproc->d_type !=DT_DIR ) { +continue ; + } +#endif +for (cs = direproc->d_name; *cs; cs++) { +if (!isdigit (*cs)) { +break ; + } + } +if (*cs) { +continue ; + } + + procfdlen =snprintf (line,sizeof (line), +PATH_PROC_X_FD , direproc->d_name); +if ((procfdlen <=0 ) || (procfdlen >=sizeof (line) -5 )) { +continue ; + } + errno =0 ; + dirfd =opendir (line); +if (NULL == dirfd) { +continue ; + } + line[procfdlen] ='/' ; +for (;;) { + errno =0 ; + direfd =readdir (dirfd); +if (NULL == direfd) { +break ; + } + +#ifdef DIRENT_HAVE_D_TYPE_WORKS +if (direfd->d_type !=DT_LNK ) { +continue ; + } +#endif +if (procfdlen +1 +strlen (direfd->d_name) +1 >sizeof (line)) { +continue ; + } +memcpy (line + procfdlen - PATH_FD_SUFFl,PATH_FD_SUFF "/" , + PATH_FD_SUFFl +1 ); +strcpy (line + procfdlen +1 , direfd->d_name); + lnamelen =readlink (line, lname,sizeof (lname)-1 ); + lname[lnamelen] ='\0' ;/*make it a null-terminated string*/ + +extract_type_1_socket_inode (lname, &inode); + +if (inode <0 ) { +extract_type_2_socket_inode (lname, &inode); + } + +if (inode <0 ) { +continue ; + } + + pid =strtoul (direproc->d_name,NULL ,10 ); +prg_cache_add (hash, inode, pid); + } +closedir (dirfd); + dirfd =NULL ; + } +if (dirproc) { +closedir (dirproc); + } +if (dirfd) { +closedir (dirfd); + } + +return 1 ; +} + +/* this function is taken out of netstat (found in net-tools) and modified */ +static GuestActiveConnection *udp_do_one (prg_hash *hash,const char *line) +{ +GuestActiveConnection *conn =NULL ; +char local_addr[64 ], rem_addr[64 ]; +char more[512 ]; +int num, local_port, rem_port, d, state, timer_run, uid, timeout; +struct sockaddr_in localaddr, remaddr; +unsigned long rxq, txq, time_len, retr, inode; + + more[0 ] ='\0' ; + num =sscanf (line, +"%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X" +" %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n" , + &d, local_addr, &local_port, + rem_addr, &rem_port, &state, + &txq, &rxq, &timer_run, &time_len, + &retr, &uid, &timeout, &inode, more); + +if (strlen (local_addr) >8 ) { +/* ipv6 support is missing */ + }else { +sscanf (local_addr,"%X" , + &((struct sockaddr_in *) &localaddr)->sin_addr.s_addr); +sscanf (rem_addr,"%X" , + &((struct sockaddr_in *) &remaddr)->sin_addr.s_addr); + ((struct sockaddr *) &localaddr)->sa_family =AF_INET ; + ((struct sockaddr *) &remaddr)->sa_family =AF_INET ; + } + + retr =0L ; + +if (num <10 ) { +return NULL ; + } + + conn =g_malloc0 (sizeof (GuestActiveConnection)); + +if (AF_INET == localaddr.sin_family) { + conn->source_addr =g_strdup (inet_ntoa (localaddr.sin_addr)); + conn->dest_addr =g_strdup (inet_ntoa (remaddr.sin_addr)); + } + + conn->source_port = local_port; + conn->dest_port = rem_port; + conn->owner_process_id =prg_cache_get (hash, inode); + conn->if_family =GUEST_IP_ADDRESS_TYPE_IPV4 ; + conn->protocol =GUEST_IP_PROTOCOL_UDP ; + +switch (state) { +case TCP_CLOSE : + conn->state =GUEST_TCP_PROTOCOL_STATE_CLOSED ; +break ; +case TCP_ESTABLISHED : + conn->state =GUEST_TCP_PROTOCOL_STATE_ESTABLISHED ; +break ; +default : + conn->state =GUEST_TCP_PROTOCOL_STATE_LISTEN ; +break ; + } + +return conn; +} + +/* this function is taken out of netstat (found in net-tools) and modified */ +static GuestActiveConnection *tcp_do_one (prg_hash *hash,const char *line) +{ +unsigned long rxq, txq, time_len, retr, inode; +int num, local_port, rem_port, d, state, uid, timer_run, timeout; +char rem_addr[128 ], local_addr[128 ], more[512 ]; +struct sockaddr_in localaddr, remaddr; +GuestActiveConnection *conn =NULL ; + + num =sscanf (line, +"%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X" +" %lX:%lX %X:%lX %lX %d %d %ld %512s\n" , + &d, local_addr, &local_port, rem_addr, &rem_port, &state, + &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, + &inode, more); + +if (strlen (local_addr) >8 ) { +/* ipv6 support is missing */ + }else { +sscanf (local_addr,"%X" , + &((struct sockaddr_in *) &localaddr)->sin_addr.s_addr); +sscanf (rem_addr,"%X" , + &((struct sockaddr_in *) &remaddr)->sin_addr.s_addr); + ((struct sockaddr *) &localaddr)->sa_family =AF_INET ; + ((struct sockaddr *) &remaddr)->sa_family =AF_INET ; + } + +if (num <11 ) { +return NULL ; + } + +if (state ==TCP_LISTEN ) { + time_len =0 ; + retr =0L ; + rxq =0L ; + txq =0L ; + } + + conn =g_malloc0 (sizeof (GuestActiveConnection)); + +if (AF_INET == localaddr.sin_family) { + conn->source_addr =g_strdup (inet_ntoa (localaddr.sin_addr)); + conn->dest_addr =g_strdup (inet_ntoa (remaddr.sin_addr)); + } + + conn->source_port = local_port; + conn->dest_port = rem_port; + conn->owner_process_id =prg_cache_get (hash, inode); + conn->if_family =GUEST_IP_ADDRESS_TYPE_IPV4 ; + conn->protocol =GUEST_IP_PROTOCOL_TCP ; + +switch (state) { +case TCP_CLOSE : + conn->state =GUEST_TCP_PROTOCOL_STATE_CLOSED ; +break ; +case TCP_LISTEN : + conn->state =GUEST_TCP_PROTOCOL_STATE_LISTEN ; +break ; +case TCP_SYN_SENT : + conn->state =GUEST_TCP_PROTOCOL_STATE_SYN_SENT ; +break ; +case TCP_SYN_RECV : + conn->state =GUEST_TCP_PROTOCOL_STATE_SYN_RCVD ; +break ; +case TCP_ESTABLISHED : + conn->state =GUEST_TCP_PROTOCOL_STATE_ESTABLISHED ; +break ; +case TCP_FIN_WAIT1 : + conn->state =GUEST_TCP_PROTOCOL_STATE_FIN_WAIT1 ; +break ; +case TCP_FIN_WAIT2 : + conn->state =GUEST_TCP_PROTOCOL_STATE_FIN_WAIT2 ; +break ; +case TCP_CLOSE_WAIT : + conn->state =GUEST_TCP_PROTOCOL_STATE_CLOSE_WAIT ; +break ; +case TCP_CLOSING : + conn->state =GUEST_TCP_PROTOCOL_STATE_CLOSING ; +break ; +case TCP_LAST_ACK : + conn->state =GUEST_TCP_PROTOCOL_STATE_LAST_ACK ; +break ; +case TCP_TIME_WAIT : + conn->state =GUEST_TCP_PROTOCOL_STATE_TIME_WAIT ; +break ; + } + +return conn; +} + +GuestActiveConnectionList *qmp_guest_get_active_connections (Error **errp) +{ +GuestActiveConnectionList *conn_list_head =NULL ; +GuestActiveConnectionList *conn_list_last =NULL ; +prg_hash hash; +FILE *udp_info =NULL ; +FILE *tcp_info =fopen ("/proc/net/tcp" ,"r" ); +int i =0 ; + +if (NULL == tcp_info) { +error_setg (errp,"Error opening '/proc/net/tcp' (error %d)" , errno); +return NULL ; + } + +prg_cache_load (&hash); + +for (i =0 ; !feof (tcp_info); ++i) { +char tcp_info_line[1024 ]; +GuestActiveConnectionList *conn_item =NULL ; +GuestActiveConnection *conn =NULL ; + +if (NULL ==fgets (tcp_info_line,1024 , tcp_info)) { +continue ; + } + +if (i ==0 ) { +continue ; + } + + conn =tcp_do_one (&hash, tcp_info_line); +if (NULL == conn) { +continue ; + } + +/* add connection to the end of list */ + conn_item =g_malloc0 (sizeof (GuestActiveConnectionList)); +if (NULL == conn_list_head) { + conn_list_head = conn_item; + } +if (NULL != conn_list_last) { + conn_list_last->next = conn_item; + } + conn_list_last = conn_item; + conn_item->value = conn; + } + +fclose (tcp_info); + + udp_info =fopen ("/proc/net/udp" ,"r" ); +if (NULL == udp_info) { +error_setg (errp,"Error opening '/proc/net/udp' (error %d)" , errno); +return NULL ; + } + +for (i =0 ; !feof (udp_info); ++i) { +char udp_info_line[1024 ]; +GuestActiveConnectionList *conn_item =NULL ; +GuestActiveConnection *conn =NULL ; + +if (NULL ==fgets (udp_info_line,1024 , udp_info)) { +continue ; + } + +if (i ==0 ) { +continue ; + } + + conn =udp_do_one (&hash, udp_info_line); +if (NULL == conn) { +continue ; + } + +/* add connection to the end of list */ + conn_item =g_malloc0 (sizeof (GuestActiveConnectionList)); +if (NULL == conn_list_head) { + conn_list_head = conn_item; + } +if (NULL != conn_list_last) { + conn_list_last->next = conn_item; + } + conn_list_last = conn_item; + conn_item->value = conn; + } + +prg_cache_clear (&hash); + +return conn_list_head; +}
qga/commands-win32.c +402 -2
447 lines not on this page · raw message
qga/qapi-schema.json +99 -0
102 lines not on this page · raw message
qga/win32-definitions.h new file +110 -0
110 lines not on this page · raw message
qga/win32-iptypes.h new file +412 -0
412 lines not on this page · raw message
Signature · 1 line
2.3.4