NETSTAT
Table of Contents >> Application Reference
The NETSTAT tool outputs useful information retrieved from the stack, such as the routing table, the ARP table, the UDP socket table, and the TCP vector table. A user can pick the information he is interested in and output it in the preferred way through the call back functions.
The NETSTAT API consists of a set of data structures, a function tfNetStat() that enumerates the table entries, a callback interface (ttNtEntryCBFuncPtr) that handles each entry, and a set of helper functions. The helper functions facilitate the writing of user call back functions. They provide the table header string and convert a NETSTAT entry into a string.
These data structures are defined for NETSTAT:
(These data structures hold a mapped copy of the internal stack data, they are only for the output purpose, modifying these data has no effect on the internal data.)
| In order to use the NETSTAT tool, TM_USE_NETSTAT must be #defined in <trsystem.h>. |
Function Calls
Examples
Below is an example of how to implement the NETSTAT APIs to print the NETSTAT TCP table. More examples may be found in the examples/txtelntd.c module of the distribution.
| Note: Since printf is non-reentrant, it therefore should never be used in a preemptive environment. |
tfxNtTcpEntryCBPrintf() is defined as below:
int tfxNtTcpEntryCBPrintf( ttNtEntryUPtr ntEntryUPtr, ttUserGenericUnion genParam1, ttUserGenericUnion genParam2) { char buffer[TM_NT_ENTRY_STR_LEN]; int entryStrLen; genParam1 = genParam1; genParam2 = genParam2; entryStrLen = TM_NT_ENTRY_STR_LEN; if(tfNtTcpEntryToStr(&ntEntryUPtr->ntTcpEntry, buffer, &entryStrLen) != NULL) { printf("%s\n", buffer); } /* * return TM_ENOERROR to ask tfNetStat to call back with more entries * return TM_ENOENT to ask tfNetStat to stop calling back */ return TM_ENOERROR; }
Below is a example of how to use tfNetStat() and the call back function defined above to print out the TCP socket table.
#include <trsocket.h> main() { char buffer[TM_NT_ENTRY_STR_LEN]; int headerStrLen; ttUserGenericUnion genUnion; ... /* print the TCP socket table using NETSTAT tools */ printf("\n------\nTCP sockets\n------\n"); headerStrLen = TM_NT_ENTRY_STR_LEN; if(tfNtGetTcpHeaderStr(buffer,&headerStrLen)!= NULL) { printf("%s\n", buffer); } errorCode = tfNetStat(TM_NT_TABLE_TCP, tfxNtTcpEntryCBPrintf, genUnion, genUnion); }