char *print_tcp_descriptor (char *desc, int prov, int num) { nstat_tcp_descriptor *ntd = (nstat_tcp_descriptor *) desc; #if 0 int i = 0; fprintf(stderr, "\nDESC:\n 0: "); for (i = 0; i < 160; i++) { unsigned char *debug = (char *) ntd; fprintf (stderr, "%x ", debug[i]); if (i %16 == 15) { fprintf(stderr, "\n%d: ", i+1);} } #endif int ipv6 = 0; char localAddrPrint[40]; char remoteAddrPrint[40]; const char *localPrint, *remotePrint; unsigned short localPort, remotePort; char timeBuf[30]; time_t now; struct tm *n; char *returned = (char *) malloc(1024); returned[0] ='\0'; // Dont need AF_ because we have this already in the address, right? time(&now); n = localtime(&now); strftime(timeBuf, 29, "%H:%M:%S", n); //sprintf(timeBuf, "%d......", num); sprintf (returned + strlen(returned), "%-8s ", timeBuf); // This was changed in ML - this might change again.. char *pname; int pid; switch (g_OsVer) { case 7: pid = ntd->pid; pname = (char *)&ntd->pname; break; case 8: case 9: pid = ((nstat_tcp_descriptor_10_8 *) ntd)->pid; pname = ((nstat_tcp_descriptor_10_8 *) ntd)->pname; break; case 10: pid = ((nstat_tcp_descriptor_10_10 *) ntd)->pid; pname = ((nstat_tcp_descriptor_10_10 *) ntd)->pname; break; default: // When we get to 10.11, we'll deal with it.. for now , stay silent pname = NULL; pid = -1; break; } sprintf(returned + strlen(returned), "%6d\t", pid); if (pname[0]) { sprintf(returned + strlen(returned), "%-16s\t", pname );} else { sprintf(returned + strlen(returned), " \t");} // sprintf(returned+strlen(returned), "%d\t", ntd->sndbufused); if (ntd->local.v4.sin_family == AF_INET6) { //printf("IPv6\t"); ipv6++; } if (!ipv6) { localPort = ntohs(ntd->local.v4.sin_port); remotePort = ntohs(ntd->remote.v4.sin_port); } else { localPort = ntohs(ntd->local.v6.sin6_port); remotePort = ntohs(ntd->remote.v6.sin6_port); } if (ntd->remote.v6.sin6_family == AF_INET6) { localPrint = inet_ntop(ntd->local.v6.sin6_family,&(ntd->local.v6.sin6_addr), localAddrPrint, 40); remotePrint = inet_ntop(ntd->remote.v6.sin6_family,&(ntd->remote.v6.sin6_addr), remoteAddrPrint, 40); } if (ntd->remote.v4.sin_family == AF_INET) { localPrint = inet_ntop(ntd->local.v4.sin_family,&(ntd->local.v4.sin_addr), localAddrPrint, 40); remotePrint = inet_ntop(ntd->remote.v4.sin_family,&(ntd->remote.v4.sin_addr), remoteAddrPrint, 40); } if (remoteAddrPrint) { sprintf (remoteAddrPrint + strlen(remoteAddrPrint),":%-5hu", (unsigned short) remotePort); } else sprintf (remoteAddrPrint, "*.*"); if (localAddrPrint) { sprintf (localAddrPrint + strlen(localAddrPrint),":%-5hu", (unsigned short) localPort); if (remotePort == 0) { sprintf(returned + strlen(returned),"%-21s\t*.* \t ", localAddrPrint); } else sprintf(returned + strlen(returned), "%-21s\t%-25s\t", localAddrPrint, remoteAddrPrint); } sprintf(returned + strlen(returned), "%d\t%-10s\t", ntd->ifindex, stateToText(ntd->state)); if (wantPackets) sprintf(returned+strlen(returned), "%lld\t%lld", descriptorCounts[num].nstat_rxpackets, descriptorCounts[num].nstat_txpackets); else sprintf(returned+strlen(returned), "%lld\t%lld", descriptorCounts[num].nstat_rxbytes, descriptorCounts[num].nstat_txbytes); if ( strstr(returned, "CLOSED") && strstr(returned,"(")) { refreshSrc(prov,num); } return (returned); }
char * print_tcp_descriptor (char *desc, int prov, int num) { nstat_tcp_descriptor *ntd = (nstat_tcp_descriptor *) desc; int ipv6 = 0; char localAddrPrint[40]; char remoteAddrPrint[40]; const char *localPrint, *remotePrint; unsigned short localPort, remotePort; char timeBuf[30]; time_t now; struct tm *n; char *returned = (char *) malloc(1024); returned[0] ='\0'; // Dont need AF_ because we have this already in the address, right? time(&now); n = localtime(&now); strftime(timeBuf, 29, "%H:%M:%S", n); //sprintf(timeBuf, "%d......", num); sprintf (returned + strlen(returned), "%-8s ", timeBuf); // sprintf(returned+strlen(returned), "%d\t", ntd->sndbufused); if (ntd->local.v4.sin_family == AF_INET6) { //printf("IPv6\t"); ipv6++; } if (!ipv6) { localPort = ntohs(ntd->local.v4.sin_port); remotePort = ntohs(ntd->remote.v4.sin_port); } else { localPort = ntohs(ntd->local.v6.sin6_port); remotePort = ntohs(ntd->remote.v6.sin6_port); } if (ntd->remote.v6.sin6_family == AF_INET6) { localPrint = inet_ntop(ntd->local.v6.sin6_family,&(ntd->local.v6.sin6_addr), localAddrPrint, 40); remotePrint = inet_ntop(ntd->remote.v6.sin6_family,&(ntd->remote.v6.sin6_addr), remoteAddrPrint, 40); } if (ntd->remote.v4.sin_family == AF_INET) { localPrint = inet_ntop(ntd->local.v4.sin_family,&(ntd->local.v4.sin_addr), localAddrPrint, 40); remotePrint = inet_ntop(ntd->remote.v4.sin_family,&(ntd->remote.v4.sin_addr), remoteAddrPrint, 40); } if (remoteAddrPrint) { sprintf (remoteAddrPrint + strlen(remoteAddrPrint),":%-5hu", (unsigned short) remotePort); } else sprintf (remoteAddrPrint, "*.*"); if (localAddrPrint) { sprintf (localAddrPrint + strlen(localAddrPrint),":%-5hu", (unsigned short) localPort); if (remotePort == 0) { sprintf(returned + strlen(returned),"%-21s\t*.* \t ", localAddrPrint); } else sprintf(returned + strlen(returned), "%-21s\t%-25s\t", localAddrPrint, remoteAddrPrint); } sprintf(returned + strlen(returned), "%d\t%-10s\t", ntd->ifindex, stateToText(ntd->state)); sprintf(returned + strlen(returned), "%-5d ", ntd->pid); if (ntd->pname[0]) { sprintf(returned + strlen(returned), "(%s)", ntd->pname );} else { sprintf(returned + strlen(returned), " ");} if ( strstr(returned, "CLOSED") && strstr(returned,"(")) { refreshSrc(prov,num); } return (returned); }