the new format
git-svn-id: http://172.17.0.253/svn/soft_pkgs/sys_nicmonitor@146 09c3743a-b0dd-45d3-b506-aa74c7a2a6ef
This commit is contained in:
8
code/trunk/test/Makefile
Normal file
8
code/trunk/test/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
all:read_shm recv_msg getmem
|
||||
|
||||
getmem:getmem.c
|
||||
# g++ -g -L ../dynamic_lib -lnic_shm -o $@ $^
|
||||
g++ -g -lnic_shm -o $@ $^
|
||||
clean:
|
||||
rm getmem read_shm recv_msg
|
||||
264
code/trunk/test/dotconf.h
Normal file
264
code/trunk/test/dotconf.h
Normal file
@@ -0,0 +1,264 @@
|
||||
#ifndef DOTCONF_H
|
||||
#define DOTCONF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* stdio.h should be included by the application - as the manual page says */
|
||||
#ifndef _STDIO_H
|
||||
#include <stdio.h> /* needed for FILE* */
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
# ifndef R_OK
|
||||
#define R_OK 0
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* some buffersize definitions */
|
||||
#define CFG_BUFSIZE 4096 /* max length of one line */
|
||||
#define CFG_MAX_OPTION 32 /* max length of any option name */
|
||||
#define CFG_MAX_VALUE 4064 /* max length of any options value */
|
||||
#define CFG_MAX_FILENAME 256 /* max length of a filename */
|
||||
#define CFG_VALUES 16 /* max # of arguments an option takes */
|
||||
|
||||
#define CFG_INCLUDEPATH_ENV "DC_INCLUDEPATH"
|
||||
#define WILDCARDS "*?" /* list of supported wild-card characters */
|
||||
|
||||
/* constants for type of option */
|
||||
#define ARG_TOGGLE 0 /* TOGGLE on,off; yes,no; 1, 0; */
|
||||
#define ARG_INT 1 /* callback wants an integer */
|
||||
#define ARG_STR 2 /* callback expects a \0 terminated str */
|
||||
#define ARG_LIST 3 /* wants list of strings */
|
||||
#define ARG_NAME 4 /* wants option name plus ARG_LIST stuff */
|
||||
#define ARG_RAW 5 /* wants raw argument data */
|
||||
#define ARG_NONE 6 /* does not expect ANY args */
|
||||
|
||||
#define CTX_ALL 0 /* context: option can be used anywhere */
|
||||
|
||||
/* for convenience of terminating the dotconf_options list */
|
||||
#define LAST_OPTION { "", 0, NULL, NULL }
|
||||
#define LAST_CONTEXT_OPTION { "", 0, NULL, NULL, 0 }
|
||||
|
||||
#define DOTCONF_CB(__name) const char *__name(command_t *cmd, \
|
||||
context_t *ctx)
|
||||
#define FUNC_ERRORHANDLER(_name) int _name(configfile_t * configfile, \
|
||||
int type, long dc_errno, const char *msg)
|
||||
|
||||
|
||||
/* some flags that change the runtime behaviour of dotconf */
|
||||
#define NONE 0
|
||||
#define CASE_INSENSITIVE 1<<0 /* match option names case insensitive */
|
||||
#define DONT_SUBSTITUTE 1<<1 /* do not call substitute_env after read_arg */
|
||||
#define NO_INLINE_COMMENTS 1<<2 /* do not allow inline comments */
|
||||
#define DUPLICATE_OPTION_NAMES 1<<3 /* allow for duplicate option names */
|
||||
|
||||
/* syslog style errors as suggested by Sander Steffann <sander@steffann.nl> */
|
||||
#ifdef HAVE_SYSLOG
|
||||
#include <syslog.h>
|
||||
|
||||
#define DCLOG_EMERG LOG_EMERG /* system is unusable */
|
||||
#define DCLOG_ALERT LOG_ALERT /* action must be taken immediately */
|
||||
#define DCLOG_CRIT LOG_CRIT /* critical conditions */
|
||||
#define DCLOG_ERR LOG_ERR /* error conditions */
|
||||
#define DCLOG_WARNING LOG_WARNING /* warning conditions */
|
||||
#define DCLOG_NOTICE LOG_NOTICE /* normal but significant condition */
|
||||
#define DCLOG_INFO LOG_INFO /* informational */
|
||||
#define DCLOG_DEBUG LOG_DEBUG /* debug-level messages */
|
||||
|
||||
#define DCLOG_LEVELMASK LOG_PRIMASK /* mask off the level value */
|
||||
|
||||
#else /* HAVE_SYSLOG */
|
||||
|
||||
#define DCLOG_EMERG 0 /* system is unusable */
|
||||
#define DCLOG_ALERT 1 /* action must be taken immediately */
|
||||
#define DCLOG_CRIT 2 /* critical conditions */
|
||||
#define DCLOG_ERR 3 /* error conditions */
|
||||
#define DCLOG_WARNING 4 /* warning conditions */
|
||||
#define DCLOG_NOTICE 5 /* normal but significant condition */
|
||||
#define DCLOG_INFO 6 /* informational */
|
||||
#define DCLOG_DEBUG 7 /* debug-level messages */
|
||||
|
||||
#define DCLOG_LEVELMASK 7 /* mask off the level value */
|
||||
|
||||
#endif /* HAVE_SYSLOG */
|
||||
|
||||
/* callback types for dotconf_callback */
|
||||
|
||||
/* error constants */
|
||||
#define ERR_NOERROR 0x0000
|
||||
#define ERR_PARSE_ERROR 0x0001
|
||||
#define ERR_UNKNOWN_OPTION 0x0002
|
||||
#define ERR_WRONG_ARG_COUNT 0x0003
|
||||
#define ERR_INCLUDE_ERROR 0x0004
|
||||
#define ERR_NOACCESS 0x0005
|
||||
#define ERR_USER 0x1000 /* base for userdefined errno's */
|
||||
|
||||
/* i needed this to check an ARG_LIST entry if it's toggled in one of my apps; maybe you do too */
|
||||
#define CFG_TOGGLED(_val) ( (_val[0] == 'Y' \
|
||||
|| _val[0] == 'y') \
|
||||
|| (_val[0] == '1') \
|
||||
|| ((_val[0] == 'o' \
|
||||
|| _val[0] == 'O') \
|
||||
&& (_val[1] == 'n' \
|
||||
|| _val[1] == 'N')))
|
||||
|
||||
enum callback_types
|
||||
{
|
||||
ERROR_HANDLER = 1,
|
||||
CONTEXT_CHECKER
|
||||
};
|
||||
|
||||
typedef enum callback_types callback_types;
|
||||
typedef struct configfile_t configfile_t;
|
||||
typedef struct configoption_t configoption_t;
|
||||
typedef struct configoption_t ConfigOption;
|
||||
typedef struct command_t command_t;
|
||||
typedef void context_t;
|
||||
typedef void info_t;
|
||||
|
||||
typedef const char *(*dotconf_callback_t)(command_t *, context_t *);
|
||||
typedef int (*dotconf_errorhandler_t)(configfile_t *, int, unsigned long, const char *);
|
||||
typedef const char *(*dotconf_contextchecker_t)(command_t *, unsigned long);
|
||||
|
||||
struct configfile_t
|
||||
{
|
||||
/* ------ the fields in configfile_t are provided to the app via command_t's ; READ ONLY! --- */
|
||||
|
||||
FILE *stream;
|
||||
char eof; /* end of file reached ? */
|
||||
size_t size; /* file size; cached on-demand for here-documents */
|
||||
|
||||
context_t *context;
|
||||
|
||||
configoption_t const **config_options;
|
||||
int config_option_count;
|
||||
|
||||
/* ------ misc read-only fields ------------------------------------------------------------- */
|
||||
char *filename; /* name of file this option was found in */
|
||||
unsigned long line; /* line number we're currently at */
|
||||
unsigned long flags; /* runtime flags given to dotconf_open */
|
||||
|
||||
char *includepath;
|
||||
|
||||
/* ------ some callbacks for interactivity -------------------------------------------------- */
|
||||
dotconf_errorhandler_t errorhandler;
|
||||
dotconf_contextchecker_t contextchecker;
|
||||
|
||||
int (*cmp_func)(const char *, const char *, size_t);
|
||||
};
|
||||
|
||||
struct configoption_t
|
||||
{
|
||||
const char *name; /* name of configuration option */
|
||||
int type; /* for possible values, see above */
|
||||
dotconf_callback_t callback; /* callback function */
|
||||
info_t *info; /* additional info for multi-option callbacks */
|
||||
unsigned long context; /* context sensitivity flags */
|
||||
};
|
||||
|
||||
struct command_t
|
||||
{
|
||||
const char *name; /* name of the command */
|
||||
configoption_t *option; /* the option as given in the app; READ ONLY */
|
||||
|
||||
/* ------ argument data filled in for each line / command ----------------------------------- */
|
||||
struct {
|
||||
long value; /* ARG_INT, ARG_TOGGLE */
|
||||
char *str; /* ARG_STR */
|
||||
char **list; /* ARG_LIST */
|
||||
} data;
|
||||
int arg_count; /* number of arguments (in data.list) */
|
||||
|
||||
/* ------ misc context information ---------------------------------------------------------- */
|
||||
configfile_t *configfile;
|
||||
context_t *context;
|
||||
};
|
||||
|
||||
/* ------ dotconf_create() - create the configfile_t needed for further dot.conf fun ------------ */
|
||||
configfile_t *dotconf_create(char *, const configoption_t *, context_t *, unsigned long);
|
||||
|
||||
/* ------ dotconf_cleanup() - tidy up behind dotconf_create and the parser dust ----------------- */
|
||||
void dotconf_cleanup(configfile_t *configfile);
|
||||
|
||||
/* ------ dotconf_command_loop() - iterate through each line of file and handle the commands ---- */
|
||||
int dotconf_command_loop(configfile_t *configfile);
|
||||
|
||||
/* ------ dotconf_command_loop_until_error() - like continue_line but return on the first error - */
|
||||
const char *dotconf_command_loop_until_error(configfile_t *configfile);
|
||||
|
||||
/* ------ dotconf_continue_line() - check if line continuation is to be handled ----------------- */
|
||||
int dotconf_continue_line(char *buffer, size_t length);
|
||||
|
||||
/* ------ dotconf_get_next_line() - read in the next line of the configfile_t ------------------- */
|
||||
int dotconf_get_next_line(char *buffer, size_t bufsize, configfile_t *configfile);
|
||||
|
||||
/* ------ dotconf_get_here_document() - read the here document until delimit is found ----------- */
|
||||
char *dotconf_get_here_document(configfile_t *configfile, const char *delimit);
|
||||
|
||||
/* ------ dotconf_invoke_command() - call the callback for command_t ---------------------------- */
|
||||
const char *dotconf_invoke_command(configfile_t *configfile, command_t *cmd);
|
||||
|
||||
/* ------ dotconf_find_command() - iterate through all registered options trying to match ------- */
|
||||
configoption_t *dotconf_find_command(configfile_t *configfile, const char *command);
|
||||
|
||||
/* ------ dotconf_read_arg() - read one argument from the line handling quoting and escaping ---- */
|
||||
/*
|
||||
side effects: the char* returned by dotconf_read_arg is malloc() before, hence that pointer
|
||||
will have to be free()ed later.
|
||||
*/
|
||||
char *dotconf_read_arg(configfile_t *configfile, signed char **line);
|
||||
|
||||
/* ------ dotconf_handle_command() - parse, substitute, find, invoke the command found in buffer */
|
||||
const char *dotconf_handle_command(configfile_t *configfile, char *buffer);
|
||||
|
||||
/* ------ dotconf_register_option() - add a new option table to the list of commands ------------ */
|
||||
void dotconf_register_options(configfile_t *configfile, const configoption_t *options);
|
||||
|
||||
/* ------ dotconf_warning() - handle the dispatch of error messages of various levels ----------- */
|
||||
int dotconf_warning(configfile_t *configfile, int level, unsigned long errnum, const char *, ...);
|
||||
|
||||
/* ------ dotconf_callback() - register a special callback -------------------------------------- */
|
||||
void dotconf_callback(configfile_t *configfile, callback_types type, dotconf_callback_t);
|
||||
|
||||
/* ------ dotconf_substitute_env() - handle the substitution on environment variables ----------- */
|
||||
char *dotconf_substitute_env(configfile_t *, char *);
|
||||
|
||||
/* ------ internal utility function that verifies if a character is in the WILDCARDS list -- */
|
||||
int dotconf_is_wild_card(char value);
|
||||
|
||||
/* ------ internal utility function that calls the appropriate routine for the wildcard passed in -- */
|
||||
int dotconf_handle_wild_card(command_t* cmd, char wild_card, char* path, char* pre, char* ext);
|
||||
|
||||
/* ------ internal utility function that frees allocated memory from dotcont_find_wild_card -- */
|
||||
void dotconf_wild_card_cleanup(char* path, char* pre);
|
||||
|
||||
/* ------ internal utility function to check for wild cards in file path -- */
|
||||
/* ------ path and pre must be freed by the developer ( dotconf_wild_card_cleanup) -- */
|
||||
int dotconf_find_wild_card(char* filename, char* wildcard, char** path, char** pre, char** ext);
|
||||
|
||||
/* ------ internal utility function that compares two stings from back to front -- */
|
||||
int dotconf_strcmp_from_back(const char* s1, const char* s2);
|
||||
|
||||
/* ------ internal utility function that determins if a string matches the '?' criteria -- */
|
||||
int dotconf_question_mark_match(char* dir_name, char* pre, char* ext);
|
||||
|
||||
/* ------ internal utility function that determins if a string matches the '*' criteria -- */
|
||||
int dotconf_star_match(char* dir_name, char* pre, char* ext);
|
||||
|
||||
/* ------ internal utility function that determins matches for filenames with -- */
|
||||
/* ------ a '?' in name and calls the Internal Include function on that filename -- */
|
||||
int dotconf_handle_question_mark(command_t* cmd, char* path, char* pre, char* ext);
|
||||
|
||||
/* ------ internal utility function that determins matches for filenames with -- */
|
||||
/* ------ a '*' in name and calls the Internal Include function on that filename -- */
|
||||
int dotconf_handle_star(command_t* cmd, char* path, char* pre, char* ext);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* DOTCONF_H */
|
||||
82
code/trunk/test/getmem.c
Normal file
82
code/trunk/test/getmem.c
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "nicinfo_shm.h"
|
||||
|
||||
typedef struct net_info{
|
||||
NETCARD_INFO info[MAXNICNUM];
|
||||
}SHM;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int ret;
|
||||
NETCARD_INFO net;
|
||||
#if 0
|
||||
char shm_path[1024];
|
||||
char sem_path[1024];
|
||||
|
||||
memset(shm_path, 0, sizeof(shm_path));
|
||||
memset(sem_path, 0, sizeof(sem_path));
|
||||
|
||||
strcpy(shm_path, getenv("D5000_HOME"));
|
||||
strcat(shm_path, "/conf/nic/shm_sys_netcard_info");
|
||||
strcpy(sem_path, getenv("D5000_HOME"));
|
||||
strcat(sem_path, "/conf/nic/sem_sys_netcard_info");
|
||||
if((ret = init_nic_info(shm_path, sem_path)) == -1){
|
||||
fprintf(stderr, "shm or sem path error!\n");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
memset(&net, 0, sizeof(NETCARD_INFO));
|
||||
if(argc != 2){
|
||||
fprintf(stderr, "Usage:%s netcard\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
while((ret = get_nic_info(argv[1], &net)) != -1){
|
||||
fprintf(stdout, "==========================网卡信息==============================\n");
|
||||
fprintf(stdout,"DEVICE NAME : %s\nIPADDR : %d.%d.%d.%d\nBROADCAST : %d.%d.%d.%d\nNETMASK : %d.%d.%d.%d\nHWADDR : %02x:%02x:%02x:%02x:%02x:%02x\nFLAGS = %d\nMTU = %d\nTx_queue_len = %d\nTime_stamp = %d\n\nReceive bytes:%llu\nReceive packets:%llu\nReceive errors:%lu\nReceive dropped:%lu\nReceive multicast:%lu\nReceive fifo:%lu\n\nTransmit bytes:%llu\nTransmit packets:%llu\nTransmit errors:%lu\nTransmit dropped:%lu\nTransmit fifo:%lu\nTransmit collisions:%lu\nTransmit carrier:%lu\n\nAverage flow:%d\n",
|
||||
net.charname,
|
||||
(unsigned char)net.addr.sa_data[2],
|
||||
(unsigned char)net.addr.sa_data[3],
|
||||
(unsigned char)net.addr.sa_data[4],
|
||||
(unsigned char)net.addr.sa_data[5],
|
||||
(unsigned char)net.broadaddr.sa_data[2],
|
||||
(unsigned char)net.broadaddr.sa_data[3],
|
||||
(unsigned char)net.broadaddr.sa_data[4],
|
||||
(unsigned char)net.broadaddr.sa_data[5],
|
||||
(unsigned char)net.netmask.sa_data[2],
|
||||
(unsigned char)net.netmask.sa_data[3],
|
||||
(unsigned char)net.netmask.sa_data[4],
|
||||
(unsigned char)net.netmask.sa_data[5],
|
||||
(unsigned char)net.hwaddr.sa_data[0],
|
||||
(unsigned char)net.hwaddr.sa_data[1],
|
||||
(unsigned char)net.hwaddr.sa_data[2],
|
||||
(unsigned char)net.hwaddr.sa_data[3],
|
||||
(unsigned char)net.hwaddr.sa_data[4],
|
||||
(unsigned char)net.hwaddr.sa_data[5],
|
||||
net.flags,
|
||||
net.mtu,
|
||||
net.tx_queue_len,
|
||||
net.time_stamp,
|
||||
net.rx_bytes,
|
||||
net.rx_packets,
|
||||
net.rx_errors,
|
||||
net.rx_dropped,
|
||||
net.rx_multicast,
|
||||
net.rx_fifo_errors,
|
||||
net.tx_bytes,
|
||||
net.tx_packets,
|
||||
net.tx_errors,
|
||||
net.tx_dropped,
|
||||
net.tx_fifo_errors,
|
||||
net.collisions,
|
||||
net.tx_carrier_errors,
|
||||
net.average_flow);
|
||||
getchar();
|
||||
}
|
||||
fprintf(stderr, "No information of %s !\n", argv[1]);
|
||||
|
||||
// rls_nic_info();
|
||||
return 0;
|
||||
}
|
||||
66
code/trunk/test/mnic.h
Normal file
66
code/trunk/test/mnic.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#ifndef __MNIC_H
|
||||
#define __MNIC_H
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <unistd.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
#include <time.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sem.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/param.h>
|
||||
#include <linux/types.h>
|
||||
#include <glob.h>
|
||||
#include <linux/sockios.h>
|
||||
#include <linux/ethtool.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <syslog.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "sys_netcard.h"
|
||||
#include "dotconf.h"
|
||||
|
||||
#define IPSIZE 16
|
||||
#define PATH_SIZE 256
|
||||
|
||||
DOTCONF_CB(cb_str);
|
||||
DOTCONF_CB(cb_int);
|
||||
|
||||
typedef struct __config_file_st{
|
||||
unsigned char domain;
|
||||
short serv;
|
||||
short event;
|
||||
int udpport;
|
||||
char sys_netcard_shm_path[PATH_SIZE];
|
||||
char sys_netcard_sem_path[PATH_SIZE];
|
||||
int monitor_interval;
|
||||
int write_interval;
|
||||
int flow_interval;
|
||||
int flow_limit;
|
||||
int flow_peak;
|
||||
char udp[NIC_NAME_LEN];
|
||||
char nic[MAXNICNUM][NIC_NAME_LEN];
|
||||
char ip[IPSIZE] ;
|
||||
}CONFIG_FILE_ST;
|
||||
|
||||
|
||||
char *get_name(char *, char *);
|
||||
int if_fetch(NETCARD_INFO *);
|
||||
int ioc_get_name(NETCARD_INFO *);
|
||||
int get_dev_fields(char *p, NETCARD_INFO *);
|
||||
void send_alarm(D5000_NIC_ALARM *, int , char *, char *);
|
||||
void record_log(void);
|
||||
|
||||
#endif
|
||||
18
code/trunk/test/nicinfo_shm.h
Normal file
18
code/trunk/test/nicinfo_shm.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef NIC_SHM_H
|
||||
#define NIC_SHM_H
|
||||
|
||||
#include "sys_netcard.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int init_nic_info(char *share_path, char *sem_path);
|
||||
int get_nic_info(char *nic_name, NETCARD_INFO *net_info);
|
||||
void rls_nic_info(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
127
code/trunk/test/read_shm.c
Normal file
127
code/trunk/test/read_shm.c
Normal file
@@ -0,0 +1,127 @@
|
||||
#include "mnic.h"
|
||||
|
||||
typedef struct net_info{
|
||||
NETCARD_INFO info[MAXNICNUM];
|
||||
}SHM;
|
||||
|
||||
void get_sem(int semid)
|
||||
{
|
||||
struct sembuf lock;
|
||||
|
||||
lock.sem_num = 0;
|
||||
lock.sem_op = -1;
|
||||
lock.sem_flg = SEM_UNDO;
|
||||
|
||||
while(semop(semid, &lock, 1)){
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
perror("semop()");
|
||||
}
|
||||
}
|
||||
|
||||
void release_sem(int semid)
|
||||
{
|
||||
int ret;
|
||||
struct sembuf unlock;
|
||||
|
||||
unlock.sem_num = 0;
|
||||
unlock.sem_op = 1;
|
||||
unlock.sem_flg = SEM_UNDO;
|
||||
|
||||
if((ret = semop(semid, &unlock, 1)) == -1){
|
||||
perror("semop()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int fd = -1;
|
||||
int i;
|
||||
int semid;
|
||||
key_t key;
|
||||
SHM *p;
|
||||
struct timespec s_time, os_time;
|
||||
char shm_path[1024];
|
||||
char sem_path[1024];
|
||||
|
||||
memset(shm_path, 0, sizeof(shm_path));
|
||||
memset(sem_path, 0, sizeof(sem_path));
|
||||
|
||||
strcpy(shm_path, "/tmp/sys_netcard_shm_path");
|
||||
strcpy(sem_path, "/tmp/sys_netcard_sem_path");
|
||||
|
||||
s_time.tv_sec = 1;
|
||||
s_time.tv_nsec = 0;
|
||||
|
||||
if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) {
|
||||
perror("ftok()");
|
||||
exit(1);
|
||||
}
|
||||
if((semid = semget(key, 1, IPC_CREAT)) == -1){
|
||||
fprintf(stderr, "Create semaphore error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if((fd = open(shm_path, O_RDWR)) == -1){
|
||||
fprintf(stderr, "open():%s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
p = mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0);
|
||||
if(p == MAP_FAILED){
|
||||
perror("mmap");
|
||||
return -1;
|
||||
}
|
||||
perror("mmap");
|
||||
while(1){
|
||||
get_sem(semid);
|
||||
for(i = 0; p->info[i].charname[0] != 0; i++){
|
||||
fprintf(stdout, "==========================网卡信息 [%d]==============================\n", (i + 1));
|
||||
fprintf(stdout,"KEYID = %ld\nBONDING:%s\nDEVICE NAME : %s\nIPADDR : %d.%d.%d.%d\nBROADCAST : %d.%d.%d.%d\nNETMASK : %d.%d.%d.%d\nHWADDR : %02x:%02x:%02x:%02x:%02x:%02x\nFLAGS = %d\nMTU = %d\nTx_queue_len = %d\nTime_stamp = %d\n\nReceive bytes:%llu\nReceive packets:%llu\nReceive errors:%lu\nReceive dropped:%lu\nReceive multicast:%lu\nReceive fifo:%lu\n\nTransmit bytes:%llu\nTransmit packets:%llu\nTransmit errors:%lu\nTransmit dropped:%lu\nTransmit fifo:%lu\nTransmit collisions:%lu\nTransmit carrier:%lu\nAverage flow : %lld\n",
|
||||
p->info[i].ID,
|
||||
p->info[i].descr,
|
||||
p->info[i].charname,
|
||||
(unsigned char)p->info[i].addr.sa_data[2],
|
||||
(unsigned char)p->info[i].addr.sa_data[3],
|
||||
(unsigned char)p->info[i].addr.sa_data[4],
|
||||
(unsigned char)p->info[i].addr.sa_data[5],
|
||||
(unsigned char)p->info[i].broadaddr.sa_data[2],
|
||||
(unsigned char)p->info[i].broadaddr.sa_data[3],
|
||||
(unsigned char)p->info[i].broadaddr.sa_data[4],
|
||||
(unsigned char)p->info[i].broadaddr.sa_data[5],
|
||||
(unsigned char)p->info[i].netmask.sa_data[2],
|
||||
(unsigned char)p->info[i].netmask.sa_data[3],
|
||||
(unsigned char)p->info[i].netmask.sa_data[4],
|
||||
(unsigned char)p->info[i].netmask.sa_data[5],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[0],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[1],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[2],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[3],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[4],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[5],
|
||||
p->info[i].flags,
|
||||
p->info[i].mtu,
|
||||
p->info[i].tx_queue_len,
|
||||
p->info[i].time_stamp,
|
||||
p->info[i].rx_bytes,
|
||||
p->info[i].rx_packets,
|
||||
p->info[i].rx_errors,
|
||||
p->info[i].rx_dropped,
|
||||
p->info[i].rx_multicast,
|
||||
p->info[i].rx_fifo_errors,
|
||||
p->info[i].tx_bytes,
|
||||
p->info[i].tx_packets,
|
||||
p->info[i].tx_errors,
|
||||
p->info[i].tx_dropped,
|
||||
p->info[i].tx_fifo_errors,
|
||||
p->info[i].collisions,
|
||||
p->info[i].tx_carrier_errors,
|
||||
p->info[i].average_flow);
|
||||
}
|
||||
nanosleep(&s_time, &os_time);
|
||||
release_sem(semid);
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
72
code/trunk/test/recv_msg.c
Normal file
72
code/trunk/test/recv_msg.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "mnic.h"
|
||||
#define IP4STRSIZE 16
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int ret, value;
|
||||
int sock_sd, sock_port = 15000;
|
||||
D5000_NIC_ALARM mesg;
|
||||
struct sockaddr_in myend, hisend;
|
||||
socklen_t hisend_len;
|
||||
fd_set rset;
|
||||
char ip4str[IP4STRSIZE];
|
||||
|
||||
if(argc > 1){
|
||||
sock_port = atoi(argv[1]);
|
||||
if(sock_port <= 1024 || sock_port > 65535){
|
||||
fprintf(stderr, "Invalid socket port!\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (sock_sd==-1) {
|
||||
perror("socket()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
value=1;
|
||||
ret = setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value));
|
||||
if (ret==-1) {
|
||||
perror("setsockopt()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
myend.sin_family = AF_INET;
|
||||
myend.sin_port = htons(sock_port);
|
||||
inet_pton(AF_INET, "0.0.0.0", &myend.sin_addr);
|
||||
|
||||
ret = bind(sock_sd, (struct sockaddr*)&myend, sizeof(myend));
|
||||
if (ret==-1) {
|
||||
perror("bind()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
hisend_len = sizeof(hisend);
|
||||
while (1) {
|
||||
FD_ZERO(&rset);
|
||||
FD_SET(sock_sd, &rset);
|
||||
ret = select(sock_sd+1, &rset, NULL, NULL, NULL);
|
||||
if (ret==-1) {
|
||||
perror("select()");
|
||||
continue;
|
||||
}
|
||||
ret = recvfrom(sock_sd, &mesg, sizeof(mesg), 0, (struct sockaddr*)&hisend, &hisend_len);
|
||||
inet_ntop(AF_INET, &hisend.sin_addr, ip4str, IP4STRSIZE);
|
||||
fprintf(stdout, "===========Recieved from %s============\n", ip4str);
|
||||
fprintf(stdout, "message length : %d\nsend sequence : %d\nservices ID : %d\nenevt ID : %d\ndomain ID : %d\n", mesg.tMsgFrame.len, mesg.tMsgFrame.seqno, mesg.tMsgFrame.serv, mesg.tMsgFrame.event, mesg.tMsgFrame.domain);
|
||||
if(mesg.tSysNetcardAlarm.flags == NETCARD_ALARM_SWITCH)
|
||||
fprintf(stdout, "切换网卡名: %s\n", mesg.tSysNetcardAlarm.switch_devname);
|
||||
else if(mesg.tSysNetcardAlarm.flags == NETCARD_ALARM_RESUME)
|
||||
fprintf(stdout, "恢复网卡名: %s\n", mesg.tSysNetcardAlarm.fault_devname);
|
||||
else
|
||||
fprintf(stdout, "故障网卡名: %s\n", mesg.tSysNetcardAlarm.fault_devname);
|
||||
fprintf(stdout, "状态标记: %d\n", mesg.tSysNetcardAlarm.flags);
|
||||
fprintf(stdout, "重发次数: %d\n", mesg.tSysNetcardAlarm.retrytimes);
|
||||
}
|
||||
close(sock_sd);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
87
code/trunk/test/sys_netcard.h
Normal file
87
code/trunk/test/sys_netcard.h
Normal file
@@ -0,0 +1,87 @@
|
||||
#ifndef _SYS_NETCARD_H
|
||||
#define _SYS_NETCARD_H
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#ifndef NIC_NAME_LEN
|
||||
#define NIC_NAME_LEN 64
|
||||
#endif
|
||||
|
||||
#define SEM_PROJ_ID 's'
|
||||
#define MAXNICNUM 32
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
#endif
|
||||
|
||||
typedef long KEYID;
|
||||
|
||||
typedef struct _msg_frame // message frame
|
||||
{
|
||||
short len; // message length
|
||||
short seqno; // send sequence
|
||||
short serv; // services ID
|
||||
short event; // event ID
|
||||
unsigned char domain; // domain ID
|
||||
unsigned char ctxt; // Context ID
|
||||
short stid; // SOURCE task id
|
||||
short dtid; // DESTINATION task ID
|
||||
unsigned char ver_coding; // 版本号 + 编码
|
||||
unsigned char mes_type; // 帧类型
|
||||
}MSG_FRAME, *LPMSG_FRAME;
|
||||
|
||||
typedef struct _net_info
|
||||
{
|
||||
KEYID ID;
|
||||
char charname[NIC_NAME_LEN];
|
||||
char descr[128];
|
||||
struct sockaddr addr;
|
||||
struct sockaddr broadaddr;
|
||||
struct sockaddr netmask;
|
||||
struct sockaddr hwaddr;
|
||||
int time_stamp;
|
||||
short flags;
|
||||
int mtu;
|
||||
int tx_queue_len;
|
||||
int average_flow;
|
||||
unsigned long long rx_packets;
|
||||
unsigned long long tx_packets;
|
||||
unsigned long long rx_bytes;
|
||||
unsigned long long tx_bytes;
|
||||
unsigned long rx_errors;
|
||||
unsigned long tx_errors;
|
||||
unsigned long rx_dropped;
|
||||
unsigned long tx_dropped;
|
||||
unsigned long rx_multicast;
|
||||
unsigned long collisions;
|
||||
unsigned long rx_fifo_errors;
|
||||
unsigned long tx_carrier_errors;
|
||||
unsigned long tx_fifo_errors;
|
||||
}NETCARD_INFO, *LPNETCARD_INFO;
|
||||
|
||||
#define NETCARD_ALARM_FAULT 1
|
||||
#define NETCARD_ALARM_RESUME 2
|
||||
#define NETCARD_ALARM_SWITCH 3
|
||||
#define NETCARD_ALARM_ABNORMAL 4
|
||||
typedef struct _sys_netcard_alarm
|
||||
{
|
||||
char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */
|
||||
char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */
|
||||
short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/
|
||||
short retrytimes; //0,1,2 重发次数
|
||||
}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM;
|
||||
|
||||
|
||||
typedef struct _d5000_nic_alarm
|
||||
{
|
||||
MSG_FRAME tMsgFrame ;
|
||||
SYS_NETCARD_ALARM tSysNetcardAlarm ;
|
||||
}D5000_NIC_ALARM, *LPD5000_NIC_ALARM;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user