#include #include #include #include #include #include #include #include #include "tcptool.m" #include #include using namespace std; static const char *my_readlink (const char *path, SSTRING &buf) { buf.setempty(); const char *ret = NULL; char val[PATH_MAX]; int len = readlink (path,val,sizeof(val)-1); if (len > 0){ val[len] = '\0'; buf = val; ret = buf.get(); } return ret; } struct TCP_SOCK{ unsigned long addr; unsigned port; int pid; TCP_SOCK (){ addr = 0; port = 0; pid = 0; } }; struct TCP{ int id; int uid; int state; TCP_SOCK from,to; }; typedef map TCPS; static void printfds_readtcp(TCPS &tcps) { glocal TCPS *tcps = &tcps; ("/proc/net/tcp",true); if (noline > 0){ SSTRINGS words; int nb = str_splitline (line,' ',words); if (nb > 9){ TCP t; int inode; sscanf (words.getitem(1)->get(),"%08lx:%04x",&t.from.addr,&t.from.port); sscanf (words.getitem(2)->get(),"%08lx:%04x",&t.to.addr,&t.to.port); sscanf (words.getitem(3)->get(),"%02x",&t.state); sscanf (words.getitem(7)->get(),"%d",&t.uid); sscanf (words.getitem(9)->get(),"%d",&inode); #if 0 printf ("tcp %08lx:%d %08lx:%d state=%02x uid=%d inode=%d\n",t.from.addr,t.from.port ,t.to.addr,t.to.port ,t.state,t.uid,inode); #endif (*glocal.tcps)[inode] = t; } } return 0; } typedef map UNIXS; static void printfds_readunix(UNIXS &unixs) { glocal UNIXS *unixs = &unixs; ("/proc/net/unix",true); if (noline > 0){ SSTRINGS words; int nb = str_splitline (line,' ',words); if (nb >= 8){ int inode; sscanf (words.getitem(6)->get(),"%d",&inode); const char *path = words.getitem(7)->get(); // printf ("unix %d -> %s\n",inode,path); (*glocal.unixs)[inode] = path; } } return 0; } static void printfds_onepid (int pid, TCPS &tcps, UNIXS &unixs) { glocal TCPS *tcps = &tcps; glocal UNIXS *unixs = &unixs; glocal int pid = pid; glocal bool donepid = false; SSTRING tmp; tmp.setfromf ("/proc/%d/fd",pid); (tmp.get()); if (!glocal.donepid){ SSTRING tmp,exe; tmp.setfromf ("/proc/%d/exe",glocal.pid); my_readlink(tmp.get(),exe); printf ("PID %d: %s\n",glocal.pid,exe.get()); glocal.donepid = true; } char val[PATH_MAX]; int len = readlink(path,val,sizeof(val)-1); if (len > 0){ val[len] = '\0'; if (strncmp(val,"socket:[",8)==0){ int inode = atoi(val+8); TCPS::iterator it = glocal.tcps->find (inode); if (it != glocal.tcps->end()){ char ip1[20],ip2[20]; unsigned long from_addr = it->second.from.addr; unsigned long to_addr = it->second.to.addr; ipnum_ip2a (ntohl(from_addr),ip1); ipnum_ip2a (ntohl(to_addr),ip2); printf ("\t%5s -> TCP %s:%d -> %s:%d\n",basename ,ip1,it->second.from.port ,ip2,it->second.to.port); }else{ UNIXS::iterator uit = glocal.unixs->find (inode); if (uit != glocal.unixs->end()){ printf ("\t%5s -> UNIX %s\n",basename,uit->second.get()); }else{ printf ("\t%5s -> %s\n",basename,val); } } }else if (strncmp(val,"pipe:[",6)==0){ }else{ printf ("\t%5s -> %s\n",basename,val); } } } int main (int argc, char *argv[]) { glocal int pid = -1; int ret = (argc,argv,"tcptool"); setproginfo ("printfds",PACKAGE_REV ,MSG_U(I_PRINTFDS,"Print file handle and sockets for processes\n")); setarg ('p',"pid",MSG_U(I_PID,"Process ID"),glocal.pid,false); glocal TCPS tcps; glocal UNIXS unixs; printfds_readtcp (glocal.tcps); printfds_readunix (glocal.unixs); if (glocal.pid != -1){ printfds_onepid (glocal.pid,glocal.tcps,glocal.unixs); }else{ ("/proc"); return false; if (isdigit(basename[0])){ int pid = atoi(basename); printfds_onepid (pid,glocal.tcps,glocal.unixs); } } return 0; return ret; }