📄 tcp.c
字号:
register struct inpcb *inp;
register struct inpcb *low_inp = NULL;
oid lowest[24];
oid newname[24];
u_char *cp;
oid *op;
struct tcpcb *tp;
// This is indexed by local-address, local-port, remote-address, remport
// at 1,3,6,1,2,1,6,13,1
// so we get 1,3,6,1,2,1,6,13,1,?,A,B,C,D,lport,P,Q,R,S,rport
// initial length is 10, out-length is 20,
// Local address is offsets 10-13, port is offset 14.
// Remote address is offsets 15-18, port is offset 19.
// starting from udbtable, look in
// low_pcb->inp_laddr.s_addr
// and low_pcb->inp_lport
// /_l/_f/ for foreign port,address.
/* fill in object part of name for current (less sizeof instance part) */
memcpy((char *)newname, (char *)vp->name, (int)vp->namelen * sizeof(oid));
for (
#ifdef CYGPKG_NET_OPENBSD_STACK
inp = tcbtable.inpt_queue.cqh_first;
inp != (struct inpcb *)&tcbtable.inpt_queue;
inp = inp->inp_queue.cqe_next
#endif
#ifdef CYGPKG_NET_FREEBSD_STACK
inp = tcb.lh_first;
inp;
inp = inp->inp_list.le_next
#endif
) {
cp = (u_char *)&inp->inp_laddr.s_addr;
op = newname + 10;
*op++ = *cp++;
*op++ = *cp++;
*op++ = *cp++;
*op++ = *cp++;
newname[14] = ntohs(inp->inp_lport);
cp = (u_char *)&inp->inp_faddr.s_addr;
op = newname + 15;
*op++ = *cp++;
*op++ = *cp++;
*op++ = *cp++;
*op++ = *cp++;
newname[19] = ntohs(inp->inp_fport);
if (exact){
if (snmp_oid_compare(newname, 20, name, *length) == 0){
memcpy( (char *)lowest,(char *)newname, 20 * sizeof(oid));
low_inp = inp;
break; /* no need to search further */
}
} else {
if ((snmp_oid_compare(newname, 20, name, *length) > 0) &&
(!low_inp || (snmp_oid_compare(newname, 20, lowest, 20) < 0))){
/*
* if new one is greater than input and closer to input than
* previous lowest, save this one as the "next" one.
*/
memcpy( (char *)lowest,(char *)newname, 20 * sizeof(oid));
low_inp = inp;
}
}
}
if ( ! low_inp )
return NULL;
tp = intotcpcb( low_inp );
if ( ! tp )
return NULL; // Shouldn't happen
memcpy( (char *)name,(char *)lowest, 20 * sizeof(oid));
*length = 20;
*var_len = sizeof( long_ret );
*write_method = 0;
switch(vp->magic) {
case TCPCONNSTATE:
// NOTSUPPORTED: *write_method = write_tcpConnState;
switch ( tp->t_state ) {
case TCPS_CLOSED : // 0 /* closed */
long_ret = 1; break;
case TCPS_LISTEN : // 1 /* listening for connection */
long_ret = 2; break;
case TCPS_SYN_SENT : // 2 /* active, have sent syn */
long_ret = 3; break;
case TCPS_SYN_RECEIVED : // 3 /* have sent and received syn */
long_ret = 4; break;
case TCPS_ESTABLISHED : // 4 /* established */
long_ret = 5; break;
case TCPS_CLOSE_WAIT : // 5 /* rcvd fin, waiting for close */
long_ret = 8; break;
case TCPS_FIN_WAIT_1 : // 6 /* have closed, sent fin */
long_ret = 6; break;
case TCPS_CLOSING : // 7 /* closed xchd FIN; await ACK */
long_ret = 10; break;
case TCPS_LAST_ACK : // 8 /* had fin and close; await FIN ACK */
long_ret = 9; break;
case TCPS_FIN_WAIT_2 : // 9 /* have closed, fin is acked */
long_ret = 7; break;
case TCPS_TIME_WAIT : // 10 /* in 2*msl quiet wait after close */
long_ret = 11; break;
default:
long_ret = 1;
}
return (unsigned char *) &long_ret;
case TCPCONNLOCALADDRESS:
cp = (u_char *)&low_inp->inp_laddr.s_addr;
string[0] = *cp++;
string[1] = *cp++;
string[2] = *cp++;
string[3] = *cp++;
*var_len = 4;
return (unsigned char *) string;
case TCPCONNLOCALPORT:
long_ret = (long)ntohs(low_inp->inp_lport);
return (unsigned char *) &long_ret;
case TCPCONNREMADDRESS:
cp = (u_char *)&low_inp->inp_faddr.s_addr;
string[0] = *cp++;
string[1] = *cp++;
string[2] = *cp++;
string[3] = *cp++;
*var_len = 4;
return (unsigned char *) string;
case TCPCONNREMPORT:
long_ret = (long)ntohs(low_inp->inp_fport);
return (unsigned char *) &long_ret;
default:
ERROR_MSG("");
}
return NULL;
}
#if 0 // NOTSUPPORTED:
int
write_tcpConnState(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 long *long_ret;
int size;
switch ( action ) {
case RESERVE1:
if (var_val_type != ASN_INTEGER){
fprintf(stderr, "write to tcpConnState not ASN_INTEGER\n");
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > sizeof(long_ret)){
fprintf(stderr,"write to tcpConnState: bad length\n");
return SNMP_ERR_WRONGLENGTH;
}
break;
case RESERVE2:
size = var_val_len;
long_ret = (long *) var_val;
break;
case FREE:
/* Release any resources that have been allocated */
break;
case ACTION:
/* The variable has been stored in long_ret for
you to use, and you have just been asked to do something with
it. Note that anything done here must be reversable in the UNDO case */
break;
case UNDO:
/* Back out any changes made in the ACTION case */
break;
case COMMIT:
/* Things are working well, so it's now safe to make the change
permanently. Make sure that anything done here can't fail! */
break;
}
return SNMP_ERR_NOERROR;
}
#endif
#if 0 // NOTSUPPORTED:
/*
* var_ipv6TcpConnTable():
* Handle this table separately from the scalar value case.
* The workings of this are basically the same as for var_tcp above.
*/
unsigned char *
var_ipv6TcpConnTable(struct variable *vp,
oid *name,
size_t *length,
int exact,
size_t *var_len,
WriteMethod **write_method)
{
/* variables we may use later */
static long long_ret;
static unsigned char string[SPRINT_MAX_LEN];
static oid objid[MAX_OID_LEN];
static struct counter64 c64;
/*
* This assumes that the table is a 'simple' table.
* See the implementation documentation for the meaning of this.
* You will need to provide the correct value for the TABLE_SIZE parameter
*
* If this table does not meet the requirements for a simple table,
* you will need to provide the replacement code yourself.
* Mib2c is not smart enough to write this for you.
* Again, see the implementation documentation for what is required.
*/
// if (header_simple_table(vp,name,length,exact,var_len,write_method,
// IPV6TCPCONNTABLE_TABLE_SIZE)
// == MATCH_FAILED )
return NULL;
/*
* this is where we do the value assignments for the mib results.
*/
switch(vp->magic) {
case IPV6TCPCONNSTATE:
*write_method = write_ipv6TcpConnState;
long_ret = 0;
return (unsigned char *) &long_ret;
default:
ERROR_MSG("");
}
return NULL;
}
int
write_ipv6TcpConnState(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 long *long_ret;
int size;
switch ( action ) {
case RESERVE1:
if (var_val_type != ASN_INTEGER){
fprintf(stderr, "write to tcpConnState not ASN_INTEGER\n");
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > sizeof(long_ret)){
fprintf(stderr,"write to tcpConnState: bad length\n");
return SNMP_ERR_WRONGLENGTH;
}
break;
case RESERVE2:
size = var_val_len;
long_ret = (long *) var_val;
break;
case FREE:
/* Release any resources that have been allocated */
break;
case ACTION:
/* The variable has been stored in long_ret for
you to use, and you have just been asked to do something with
it. Note that anything done here must be reversable in the UNDO case */
break;
case UNDO:
/* Back out any changes made in the ACTION case */
break;
case COMMIT:
/* Things are working well, so it's now safe to make the change
permanently. Make sure that anything done here can't fail! */
break;
}
return SNMP_ERR_NOERROR;
}
#endif // NOTSUPPORTED: ipv6
// EOF tcp.c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -