⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcptable.c

📁 Snmp(简单网管协议)软件包。
💻 C
📖 第 1 页 / 共 2 页
字号:
#else /*  defined(freebsd2) || defined(netbsd1) || defined(openbsd2) */	while (inpcb.INP_NEXT_SYMBOL != (struct inpcb *) auto_nlist_value(TCP_SYMBOL))#endif /*  defined(freebsd2) || defined(netbsd1) */	{		next = inpcb.INP_NEXT_SYMBOL;		if((klookup((unsigned long)next, (char *)&inpcb, sizeof (inpcb)) == 0)) {		    snmp_log_perror("TCP_Count_Connections - inpcb");		    break;		}#if !(defined(freebsd2) || defined(netbsd1) || defined(openbsd2))		if (inpcb.INP_PREV_SYMBOL != prev) {	    /* ??? */			sleep(1);			goto Again;		}#endif /*  !(defined(freebsd2) || defined(netbsd1) || defined(openbsd2)) */		if (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) {#if !(defined(freebsd2) || defined(netbsd1) || defined(openbsd2))			prev = next;#endif /*  !(defined(freebsd2) || defined(netbsd1) || defined(openbsd2)) */			continue;		}		if(klookup((unsigned long)inpcb.inp_ppcb, (char *)&tcpcb, sizeof (tcpcb)) == 0) {		    snmp_log_perror("TCP_Count_Connections - tcpcb");		    break;		}		if ((tcpcb.t_state == TCPS_ESTABLISHED) ||		    (tcpcb.t_state == TCPS_CLOSE_WAIT))		    Established++;#if !(defined(freebsd2) || defined(netbsd1) || defined(openbsd2))		prev = next;#endif /*  !(defined(freebsd2) || defined(netbsd1) || defined(openbsd2)) */	}	return(Established);}#endifstatic struct inpcb tcp_inpcb, *tcp_prev;#ifdef PCB_TABLEstatic struct inpcb *tcp_next, *tcp_head;#endif#ifdef linuxstatic struct inpcb *inpcb_list;#endif#if defined(CAN_USE_SYSCTL) && defined(TCPCTL_PCBLIST)static char *tcpcb_buf = NULL;static struct xinpgen *xig = NULL;#endif /* !defined(CAN_USE_SYSCTL) || !define(TCPCTL_PCBLIST) */void TCP_Scan_Init (void){#if !defined(CAN_USE_SYSCTL) || !defined(TCPCTL_PCBLIST)#ifdef PCB_TABLE    struct inpcbtable table;#endif#ifndef linux#ifdef PCB_TABLE    auto_nlist(TCP_SYMBOL, (char *)&table, sizeof(table));    tcp_head = tcp_prev = (struct inpcb *)&((struct inpcbtable *)auto_nlist_value(TCP_SYMBOL))->inpt_queue.cqh_first;    tcp_next = table.inpt_queue.cqh_first;#else	/* PCB_TABLE */    auto_nlist(TCP_SYMBOL, (char *)&tcp_inpcb, sizeof(tcp_inpcb));#if !(defined(freebsd2) || defined(netbsd1) || defined(openbsd2))    tcp_prev = (struct inpcb *) auto_nlist_value(TCP_SYMBOL);#endif#endif	/* PCB_TABLE */#else	/* linux */    FILE *in;    char line [256];    struct inpcb **pp;    struct timeval now;    static unsigned long Time_Of_Last_Reload = 0;    /*     * save some cpu-cycles, and reload after 5 secs...     */    gettimeofday (&now, (struct timezone *) 0);    if (Time_Of_Last_Reload + 5 > now.tv_sec)      {	tcp_prev = inpcb_list;	return;      }    Time_Of_Last_Reload = now.tv_sec;    if (! (in = fopen ("/proc/net/tcp", "r")))      { snmp_log(LOG_ERR, "snmpd: cannot open /proc/net/tcp ...\n");	tcp_prev = NULL;	return;      }    /* free old chain: */    while (inpcb_list)      {	struct inpcb *p = inpcb_list;	inpcb_list = inpcb_list->INP_NEXT_SYMBOL;	free (p);      }    /* scan proc-file and append: */    pp = &inpcb_list;        while (line == fgets (line, sizeof(line), in))      {	struct inpcb pcb, *nnew;	static int linux_states [12] = { 0, 4, 2, 3, 6, 9, 10, 0, 5, 8, 1, 7 };	int state, lp, fp, uid;	if (6 != sscanf (line,			 "%*d: %x:%x %x:%x %x %*X:%*X %*X:%*X %*X %d",			 &pcb.inp_laddr.s_addr, &lp,			 &pcb.inp_faddr.s_addr, &fp,			 &state, &uid))	  continue;	pcb.inp_lport = htons ((unsigned short) lp);	pcb.inp_fport = htons ((unsigned short) fp);	pcb.inp_state = (state & 0xf) < 12 ? linux_states [state & 0xf] : 1;	pcb.uid = uid;    	nnew = (struct inpcb *) malloc (sizeof (struct inpcb));	if (nnew == NULL) break;	*nnew = pcb;	nnew->INP_NEXT_SYMBOL = 0;	*pp = nnew;	pp = & nnew->INP_NEXT_SYMBOL;      }    fclose (in);    /* first entry to go: */    tcp_prev = inpcb_list;#endif /* linux */#else /*  !defined(CAN_USE_SYSCTL) || !defined(TCPCTL_PCBLIST) */    {	size_t len;	int sname[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_PCBLIST };	if (tcpcb_buf) {	    free(tcpcb_buf);	    tcpcb_buf = NULL;	}	xig = NULL;	len = 0;	if (sysctl(sname, 4, 0, &len, 0, 0) < 0) {	    return;	}	if ((tcpcb_buf = malloc(len)) == NULL) {	    return;	}	if (sysctl(sname, 4, tcpcb_buf, &len, 0, 0) < 0) {	    free(tcpcb_buf);	    tcpcb_buf = NULL;	    return;	}	xig = (struct xinpgen *)tcpcb_buf;	xig = (struct xinpgen *)((char *)xig + xig->xig_len);	return;    }#endif /*  !defined(CAN_USE_SYSCTL) || !defined(TCPCTL_PCBLIST) */}int TCP_Scan_Next(int *State,		  struct inpcb *RetInPcb){#if !defined(CAN_USE_SYSCTL) || !defined(TCPCTL_PCBLIST)	register struct inpcb *next;#ifndef linux	struct tcpcb tcpcb;#ifdef PCB_TABLE	if (tcp_next == tcp_head)#elif defined(freebsd2) || defined(netbsd1) || defined(openbsd2)	if (tcp_inpcb.INP_NEXT_SYMBOL == NULL ||	    tcp_inpcb.INP_NEXT_SYMBOL == (struct inpcb *) auto_nlist_value(TCP_SYMBOL))#else	if (tcp_inpcb.INP_NEXT_SYMBOL == (struct inpcb *) auto_nlist_value(TCP_SYMBOL))#endif	{	    return(0);	    /* "EOF" */	}#ifdef PCB_TABLE	klookup((unsigned long)tcp_next, (char *)&tcp_inpcb, sizeof(tcp_inpcb));	tcp_next = tcp_inpcb.inp_queue.cqe_next;#else	next = tcp_inpcb.INP_NEXT_SYMBOL;	klookup((unsigned long)next, (char *)&tcp_inpcb, sizeof (tcp_inpcb));#if !(defined(netbsd1) || defined(freebsd2)) || defined(openbsd2)	if (tcp_inpcb.INP_PREV_SYMBOL != tcp_prev)	   /* ??? */          return(-1); /* "FAILURE" */#endif /*  !(defined(netbsd1) || defined(freebsd2) || defined(openbsd2)) */#endif	/* PCB_TABLE */	klookup ( (int)tcp_inpcb.inp_ppcb, (char *)&tcpcb, sizeof (tcpcb));	*State = tcpcb.t_state;#else /* linux */	if (! tcp_prev)	  return 0;	tcp_inpcb = *tcp_prev;	*State = tcp_inpcb.inp_state;	next = tcp_inpcb.INP_NEXT_SYMBOL;#endif	*RetInPcb = tcp_inpcb;#if !(defined(netbsd1) || defined(freebsd2) || defined(openbsd2))	tcp_prev = next;#endif#else /*  !defined(CAN_USE_SYSCTL) || !defined(TCPCTL_PCBLIST) */	/* Are we done? */	if ((xig == NULL) ||	    (xig->xig_len <= sizeof(struct xinpgen)))	    return(0);  		*State = ((struct xtcpcb *)xig)->xt_tp.t_state;	*RetInPcb = ((struct xtcpcb *)xig)->xt_inp;		/* Prepare for Next read */	xig = (struct xinpgen *)((char *)xig + xig->xig_len);#endif /*  !defined(CAN_USE_SYSCTL) || !defined(TCPCTL_PCBLIST) */	return(1);	/* "OK" */}#endif /* solaris2 */#else /* WIN32 */#include <iphlpapi.h>WriteMethod writeTcpEntry;MIB_TCPROW tcp_row;u_char *var_tcpEntry(struct variable *vp,	     oid *name,	     size_t *length,	     int exact,	     size_t *var_len,	     WriteMethod **write_method){    oid newname[MAX_OID_LEN], lowest[MAX_OID_LEN], *op;    u_char *cp;    int LowState = -1;    static PMIB_TCPTABLE pTcpTable = NULL;    DWORD status = NO_ERROR;    DWORD dwActualSize = 0;    UINT i;    struct timeval now;    static long Time_Of_Last_Reload = 0;    memcpy( (char *)newname,(char *)vp->name, (int)vp->namelen * sizeof(oid));        /*     * save some cpu-cycles, and reload after 5 secs...     */    gettimeofday (&now, (struct timezone *) 0);    if ((Time_Of_Last_Reload + 5 <= now.tv_sec) || (pTcpTable == NULL) )    {        if(pTcpTable != NULL)            free(pTcpTable);        Time_Of_Last_Reload = now.tv_sec;        /* query for buffer size needed */        status = GetTcpTable(pTcpTable, &dwActualSize, TRUE);        if (status == ERROR_INSUFFICIENT_BUFFER)        {            pTcpTable = (PMIB_TCPTABLE) malloc(dwActualSize);            if(pTcpTable != NULL){                  /* Get the sorted TCP table */                status = GetTcpTable(pTcpTable, &dwActualSize, TRUE);            }        }    }       if(status == NO_ERROR)    {        for (i = 0; i < pTcpTable->dwNumEntries; ++i)        {	               cp = (u_char *)&pTcpTable->table[i].dwLocalAddr;            op = newname + 10;            *op++ = *cp++;            *op++ = *cp++;            *op++ = *cp++;            *op++ = *cp++;                newname[14] = ntohs((unsigned short)(0x0000FFFF & pTcpTable->table[i].dwLocalPort));            cp = (u_char *)&pTcpTable->table[i].dwRemoteAddr;            op = newname + 15;            *op++ = *cp++;            *op++ = *cp++;            *op++ = *cp++;            *op++ = *cp++;                    newname[19] = ntohs((unsigned short)(0x0000FFFF & pTcpTable->table[i].dwRemotePort));            if (exact){                if (snmp_oid_compare(newname, 20, name, *length) == 0){                    memcpy( (char *)lowest,(char *)newname, 20 * sizeof(oid));                    LowState = i;                    break;  /* no need to search further */                }            } else {                if (snmp_oid_compare(newname, 20, name, *length) > 0)                 {                    memcpy( (char *)lowest,(char *)newname, 20 * sizeof(oid));                    LowState = i;                    break; /* As the table is sorted, no need to search further */                }            }        }    }    if ((LowState < 0) || (status != NO_ERROR))      return(NULL);    memcpy( (char *)name,(char *)lowest, (vp->namelen + 10) * sizeof(oid));    *length = vp->namelen + 10;    *write_method = 0;    *var_len = sizeof(long);    switch (vp->magic) {        case TCPCONNSTATE:            *write_method = writeTcpEntry;            tcp_row = pTcpTable->table[i];             return (u_char *) &pTcpTable->table[i].dwState;        case TCPCONNLOCALADDRESS:            return (u_char *) &pTcpTable->table[i].dwLocalAddr;        case TCPCONNLOCALPORT:            long_return = ntohs((unsigned short)(0x0000FFFF & pTcpTable->table[i].dwLocalPort));            return (u_char *) &long_return;        case TCPCONNREMADDRESS:            return (u_char *) &pTcpTable->table[i].dwRemoteAddr;        case TCPCONNREMPORT:            long_return = ntohs((unsigned short)(0x0000FFFF & pTcpTable->table[i].dwRemotePort));            return (u_char *) &long_return;        default:            DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_tcpEntry\n", vp->magic));    }    return NULL;}intwriteTcpEntry(int action,	     	    u_char *var_val,	    u_char var_val_type,	    size_t var_val_len,	    u_char *statP,	    oid *name,	    size_t name_len){   static int oldbuf;   DWORD status = NO_ERROR;   /* Only tcpConnState is writable */   if((char)name[9] != 1)     return SNMP_ERR_NOTWRITABLE;    switch ( action ) {	case RESERVE1:		/* Check values for acceptability */	    if (var_val_type != ASN_INTEGER){                snmp_log(LOG_ERR, "not integer\n");		     return SNMP_ERR_WRONGTYPE;	    }	    if (var_val_len > sizeof(int)){                snmp_log(LOG_ERR, "bad length\n");		     return SNMP_ERR_WRONGLENGTH;	    }	            if((int)(*var_val) != MIB_TCP_STATE_DELETE_TCB){        snmp_log(LOG_ERR, "not supported admin state\n");		    return SNMP_ERR_WRONGVALUE;		}	    break;	case RESERVE2:		/* Allocate memory and similar resources */		/* Using static strings, so nothing needs to be done */    	    break;	case ACTION:		    /* Save the old value, in case of UNDO */          oldbuf   = tcp_row.dwState;      tcp_row.dwState = (int)*var_val;	    break;	case UNDO:		/* Reverse the SET action and free resources */   	  tcp_row.dwState = oldbuf;	    break;	case COMMIT:		/* Confirm the SET, performing any irreversible actions,					and free resources */        if((status = SetTcpEntry(&tcp_row)) != NO_ERROR){            snmp_log(LOG_ERR,  "Error while trying to write connState %d, status = %d \n",                                tcp_row.dwState, status);            return SNMP_ERR_COMMITFAILED;    }    	case FREE:		/* Free any resources allocated */    	    break;    }    return SNMP_ERR_NOERROR;} /* end of writeTcpEntry */#endif /* WIN32 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -