Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | From 1d386279a449a1a6b96b88a71f35bf13b14b2c2c Mon Sep 17 00:00:00 2001 |
| 2 | From: Li Zhou <li.zhou@windriver.com> |
| 3 | Date: Thu, 14 Jan 2016 17:11:24 +0800 |
| 4 | Subject: [PATCH 3/3] net-tools: add SCTP support for netstat |
| 5 | |
| 6 | Upstream-Status: pending |
| 7 | |
| 8 | Signed-off-by: Li Zhou <li.zhou@windriver.com> |
| 9 | --- |
| 10 | netstat.c | 282 ++++++++++++++++++++++++-------------------------------------- |
| 11 | 1 file changed, 108 insertions(+), 174 deletions(-) |
| 12 | |
| 13 | diff --git a/netstat.c b/netstat.c |
| 14 | index 56a15c2..86adadb 100644 |
| 15 | --- a/netstat.c |
| 16 | +++ b/netstat.c |
| 17 | @@ -1095,23 +1095,21 @@ static void sctp_eps_do_one(int lnr, char *line) |
| 18 | const char *lport_str; |
| 19 | const char *uid_str; |
| 20 | const char *inode_str; |
| 21 | - const char *pladdr_str; |
| 22 | char *laddrs_str; |
| 23 | |
| 24 | if(lnr == 0) { |
| 25 | - /* ENDPT SOCK STY SST HBKT LPORT uid inode pladdr LADDRS*/ |
| 26 | + /* ENDPT SOCK STY SST HBKT LPORT UID INODE LADDRS */ |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | - strtok(line," \t\n"); /*skip ptr*/ |
| 31 | - strtok(0," \t\n"); /*skip ptr*/ |
| 32 | + strtok(line," \t\n"); /*skip endpt*/ |
| 33 | + strtok(0," \t\n"); /*skip sock*/ |
| 34 | sty_str = strtok(0," \t\n"); |
| 35 | sst_str = strtok(0," \t\n"); |
| 36 | strtok(0," \t\n"); /*skip hash bucket*/ |
| 37 | lport_str=strtok(0," \t\n"); |
| 38 | uid_str = strtok(0," \t\n"); |
| 39 | inode_str = strtok(0," \t\n"); |
| 40 | - pladdr_str = strtok(0," \t\n"); |
| 41 | laddrs_str=strtok(0,"\t\n"); |
| 42 | |
| 43 | type = atoi(sty_str); |
| 44 | @@ -1119,61 +1117,35 @@ static void sctp_eps_do_one(int lnr, char *line) |
| 45 | port = atoi(lport_str); |
| 46 | uid = atoi(uid_str); |
| 47 | inode = strtoul(inode_str,0,0); |
| 48 | - |
| 49 | - if(flag_sctp<=1) { |
| 50 | - /* only print the primary address */ |
| 51 | - char local_addr[64]; |
| 52 | - char local_port[16]; |
| 53 | - |
| 54 | - ap = process_sctp_addr_str(pladdr_str, (struct sockaddr*)&localaddr); |
| 55 | - if(ap) |
| 56 | - safe_strncpy(local_addr, |
| 57 | - ap->sprint((struct sockaddr *) &localaddr, flag_not), |
| 58 | - sizeof(local_addr)); |
| 59 | - else |
| 60 | - sprintf(local_addr,_("unsupported address family %d"), ((struct sockaddr*)&localaddr)->sa_family); |
| 61 | - |
| 62 | - snprintf(local_port, sizeof(local_port), "%s", |
| 63 | - get_sname(htons(port), "sctp", |
| 64 | - flag_not & FLAG_NUM_PORT)); |
| 65 | - |
| 66 | - printf("sctp "); |
| 67 | - sprintf(buffer,"%s:%s", local_addr, local_port); |
| 68 | - printf("%-47s", buffer); |
| 69 | - printf(" %-12s", sctp_socket_state_str(state)); |
| 70 | - } else { |
| 71 | - /*print all addresses*/ |
| 72 | - const char *this_local_addr; |
| 73 | - int first=1; |
| 74 | - char local_port[16]; |
| 75 | - snprintf(local_port, sizeof(local_port), "%s", |
| 76 | - get_sname(htons(port), "sctp", |
| 77 | - flag_not & FLAG_NUM_PORT)); |
| 78 | - for(this_local_addr=strtok(laddrs_str," \t\n"); |
| 79 | - this_local_addr; |
| 80 | - this_local_addr=strtok(0," \t\n")) |
| 81 | - { |
| 82 | - char local_addr[64]; |
| 83 | - ap = process_sctp_addr_str(this_local_addr, (struct sockaddr*)&localaddr); |
| 84 | - if(ap) |
| 85 | - safe_strncpy(local_addr, |
| 86 | - ap->sprint((struct sockaddr *) &localaddr, flag_not), |
| 87 | - sizeof(local_addr)); |
| 88 | - else |
| 89 | - sprintf(local_addr,_("unsupported address family %d"), ((struct sockaddr*)&localaddr)->sa_family); |
| 90 | |
| 91 | - if(!first) printf("\n"); |
| 92 | - if(first) |
| 93 | - printf("sctp "); |
| 94 | - else |
| 95 | - printf(" "); |
| 96 | - sprintf(buffer,"%s:%s", local_addr, local_port); |
| 97 | - printf("%-47s", buffer); |
| 98 | - printf(" %-12s", first?sctp_socket_state_str(state):""); |
| 99 | - first = 0; |
| 100 | - } |
| 101 | + const char *this_local_addr; |
| 102 | + int first=1; |
| 103 | + char local_port[16]; |
| 104 | + snprintf(local_port, sizeof(local_port), "%s", |
| 105 | + get_sname(htons(port), "sctp", flag_not & FLAG_NUM_PORT)); |
| 106 | + for(this_local_addr=strtok(laddrs_str," \t\n"); |
| 107 | + this_local_addr; |
| 108 | + this_local_addr=strtok(0," \t\n")) |
| 109 | + { |
| 110 | + char local_addr[64]; |
| 111 | + ap = process_sctp_addr_str(this_local_addr, (struct sockaddr*)&localaddr); |
| 112 | + if(ap) |
| 113 | + safe_strncpy(local_addr, |
| 114 | + ap->sprint((struct sockaddr *) &localaddr, flag_not), |
| 115 | + sizeof(local_addr)); |
| 116 | + else |
| 117 | + sprintf(local_addr,_("unsupported address family %d"), ((struct sockaddr*)&localaddr)->sa_family); |
| 118 | + |
| 119 | + if(!first) printf("\n"); |
| 120 | + if(first) |
| 121 | + printf("sctp "); |
| 122 | + else |
| 123 | + printf(" "); |
| 124 | + sprintf(buffer,"%s:%s", local_addr, local_port); |
| 125 | + printf("%-55s", buffer); |
| 126 | + printf(" %-12s", first?sctp_socket_state_str(state):""); |
| 127 | + first = 0; |
| 128 | } |
| 129 | - |
| 130 | finish_this_one(uid,inode,""); |
| 131 | } |
| 132 | |
| 133 | @@ -1199,32 +1171,29 @@ static void sctp_assoc_do_one(int lnr, char *line) |
| 134 | const char *lport_str,*rport_str; |
| 135 | const char *uid_str; |
| 136 | const char *inode_str; |
| 137 | - const char *pladdr_str; |
| 138 | char *laddrs_str; |
| 139 | - const char *praddr_str; |
| 140 | char *raddrs_str; |
| 141 | - |
| 142 | + |
| 143 | if(lnr == 0) { |
| 144 | - /* ASSOC SOCK STY SST ST HBKT tx_queue rx_queue uid inode LPORT RPORT pladdr praddr LADDRS <-> RADDRS*/ |
| 145 | + /* ASSOC SOCK STY SST ST HBKT ASSOC-ID TX_QUEUE RX_QUEUE UID INODE LPORT RPORT LADDRS <-> RADDRS */ |
| 146 | return; |
| 147 | } |
| 148 | - |
| 149 | - strtok(line," \t\n"); /*skip ptr*/ |
| 150 | - strtok(0," \t\n"); /*skip ptr*/ |
| 151 | + |
| 152 | + strtok(line," \t\n"); /*skip assoc*/ |
| 153 | + strtok(0," \t\n"); /*skip sock*/ |
| 154 | sty_str = strtok(0," \t\n"); |
| 155 | sst_str = strtok(0," \t\n"); |
| 156 | st_str = strtok(0," \t\n"); |
| 157 | strtok(0," \t\n"); /*skip hash bucket*/ |
| 158 | + strtok(0," \t\n"); /*skip hash assoc-id*/ |
| 159 | txqueue_str = strtok(0," \t\n"); |
| 160 | rxqueue_str = strtok(0," \t\n"); |
| 161 | uid_str = strtok(0," \t\n"); |
| 162 | inode_str = strtok(0," \t\n"); |
| 163 | lport_str=strtok(0," \t\n"); |
| 164 | rport_str=strtok(0," \t\n"); |
| 165 | - pladdr_str = strtok(0," \t\n"); |
| 166 | - praddr_str = strtok(0," \t\n"); |
| 167 | - laddrs_str=strtok(0,"<->\t\n"); |
| 168 | - raddrs_str=strtok(0,"<->\t\n"); |
| 169 | + laddrs_str = strtok(0,"<->\t\n"); |
| 170 | + raddrs_str = strtok(0,"<->\t\n"); |
| 171 | |
| 172 | type = atoi(sty_str); |
| 173 | state = atoi(sst_str); |
| 174 | @@ -1235,116 +1204,81 @@ static void sctp_assoc_do_one(int lnr, char *line) |
| 175 | inode = strtoul(inode_str,0,0); |
| 176 | lport = atoi(lport_str); |
| 177 | rport = atoi(rport_str); |
| 178 | - |
| 179 | - if(flag_sctp<=1) { |
| 180 | - /* only print the primary addresses */ |
| 181 | - char local_addr[64]; |
| 182 | - char local_port[16]; |
| 183 | - char remote_addr[64]; |
| 184 | - char remote_port[16]; |
| 185 | - |
| 186 | - ap = process_sctp_addr_str(pladdr_str, (struct sockaddr*)&localaddr); |
| 187 | - if(ap) |
| 188 | - safe_strncpy(local_addr, |
| 189 | - ap->sprint((struct sockaddr *) &localaddr, flag_not), |
| 190 | - sizeof(local_addr)); |
| 191 | - else |
| 192 | - sprintf(local_addr,_("unsupported address family %d"), ((struct sockaddr*)&localaddr)->sa_family); |
| 193 | - |
| 194 | - snprintf(local_port, sizeof(local_port), "%s", |
| 195 | - get_sname(htons(lport), "sctp", |
| 196 | - flag_not & FLAG_NUM_PORT)); |
| 197 | - |
| 198 | - ap = process_sctp_addr_str(praddr_str, (struct sockaddr*)&remoteaddr); |
| 199 | - if(ap) |
| 200 | - safe_strncpy(remote_addr, |
| 201 | - ap->sprint((struct sockaddr *) &remoteaddr, flag_not), |
| 202 | - sizeof(remote_addr)); |
| 203 | - else |
| 204 | - sprintf(remote_addr,_("unsupported address family %d"), ((struct sockaddr*)&remoteaddr)->sa_family); |
| 205 | - |
| 206 | - snprintf(remote_port, sizeof(remote_port), "%s", |
| 207 | - get_sname(htons(rport), "sctp", |
| 208 | - flag_not & FLAG_NUM_PORT)); |
| 209 | - |
| 210 | - printf("sctp"); |
| 211 | - printf(" %6u %6u ", rxqueue, txqueue); |
| 212 | - sprintf(buffer,"%s:%s", local_addr, local_port); |
| 213 | - printf("%-23s", buffer); |
| 214 | - printf(" "); |
| 215 | - sprintf(buffer,"%s:%s", remote_addr, remote_port); |
| 216 | - printf("%-23s", buffer); |
| 217 | - printf(" %-12s", sctp_socket_state_str(state)); |
| 218 | - } else { |
| 219 | - /*print all addresses*/ |
| 220 | - const char *this_local_addr; |
| 221 | - const char *this_remote_addr; |
| 222 | - char *ss1,*ss2; |
| 223 | - int first=1; |
| 224 | - char local_port[16]; |
| 225 | - char remote_port[16]; |
| 226 | - snprintf(local_port, sizeof(local_port), "%s", |
| 227 | - get_sname(htons(lport), "sctp", |
| 228 | - flag_not & FLAG_NUM_PORT)); |
| 229 | - snprintf(remote_port, sizeof(remote_port), "%s", |
| 230 | - get_sname(htons(rport), "sctp", |
| 231 | - flag_not & FLAG_NUM_PORT)); |
| 232 | - |
| 233 | - this_local_addr=strtok_r(laddrs_str," \t\n",&ss1); |
| 234 | - this_remote_addr=strtok_r(raddrs_str," \t\n",&ss2); |
| 235 | - while(this_local_addr || this_remote_addr) { |
| 236 | - char local_addr[64]; |
| 237 | - char remote_addr[64]; |
| 238 | - if(this_local_addr) { |
| 239 | - ap = process_sctp_addr_str(this_local_addr, (struct sockaddr*)&localaddr); |
| 240 | - if(ap) |
| 241 | - safe_strncpy(local_addr, |
| 242 | - ap->sprint((struct sockaddr *) &localaddr, flag_not), |
| 243 | - sizeof(local_addr)); |
| 244 | - else |
| 245 | - sprintf(local_addr,_("unsupported address family %d"), ((struct sockaddr*)&localaddr)->sa_family); |
| 246 | - } |
| 247 | - if(this_remote_addr) { |
| 248 | - ap = process_sctp_addr_str(this_remote_addr, (struct sockaddr*)&remoteaddr); |
| 249 | - if(ap) |
| 250 | - safe_strncpy(remote_addr, |
| 251 | - ap->sprint((struct sockaddr *) &remoteaddr, flag_not), |
| 252 | - sizeof(remote_addr)); |
| 253 | - else |
| 254 | - sprintf(remote_addr,_("unsupported address family %d"), ((struct sockaddr*)&remoteaddr)->sa_family); |
| 255 | - } |
| 256 | |
| 257 | - if(!first) printf("\n"); |
| 258 | - if(first) |
| 259 | - printf("sctp %6u %6u ", rxqueue, txqueue); |
| 260 | - else |
| 261 | - printf(" "); |
| 262 | - if(this_local_addr) { |
| 263 | - if(first) |
| 264 | - sprintf(buffer,"%s:%s", local_addr, local_port); |
| 265 | + /*print all addresses*/ |
| 266 | + const char *this_local_addr; |
| 267 | + const char *this_remote_addr; |
| 268 | + char *ss1,*ss2; |
| 269 | + int first=1; |
| 270 | + char local_port[16]; |
| 271 | + char remote_port[16]; |
| 272 | + snprintf(local_port, sizeof(local_port), "%s", |
| 273 | + get_sname(htons(lport), "sctp", |
| 274 | + flag_not & FLAG_NUM_PORT)); |
| 275 | + snprintf(remote_port, sizeof(remote_port), "%s", |
| 276 | + get_sname(htons(rport), "sctp", |
| 277 | + flag_not & FLAG_NUM_PORT)); |
| 278 | + |
| 279 | + this_local_addr=strtok_r(laddrs_str," \t\n",&ss1); |
| 280 | + this_remote_addr=strtok_r(raddrs_str," \t\n",&ss2); |
| 281 | + while(this_local_addr || this_remote_addr) { |
| 282 | + char local_addr[64]; |
| 283 | + char remote_addr[64]; |
| 284 | + |
| 285 | + if(this_local_addr) { |
| 286 | + if (this_local_addr[0] == '*') { |
| 287 | + /* skip * */ |
| 288 | + this_local_addr++; |
| 289 | + } |
| 290 | + ap = process_sctp_addr_str(this_local_addr, (struct sockaddr*)&localaddr); |
| 291 | + if(ap) |
| 292 | + safe_strncpy(local_addr, |
| 293 | + ap->sprint((struct sockaddr *) &localaddr, flag_not), sizeof(local_addr)); |
| 294 | else |
| 295 | - sprintf(buffer,"%s", local_addr); |
| 296 | - printf("%-23s", buffer); |
| 297 | - } else |
| 298 | - printf("%-23s", ""); |
| 299 | - printf(" "); |
| 300 | - if(this_remote_addr) { |
| 301 | - if(first) |
| 302 | - sprintf(buffer,"%s:%s", remote_addr, remote_port); |
| 303 | + sprintf(local_addr,_("unsupported address family %d"), ((struct sockaddr*)&localaddr)->sa_family); |
| 304 | + } |
| 305 | + if(this_remote_addr) { |
| 306 | + if (this_remote_addr[0] == '*') { |
| 307 | + /* skip * */ |
| 308 | + this_remote_addr++; |
| 309 | + } |
| 310 | + ap = process_sctp_addr_str(this_remote_addr, (struct sockaddr*)&remoteaddr); |
| 311 | + if(ap) |
| 312 | + safe_strncpy(remote_addr, |
| 313 | + ap->sprint((struct sockaddr *) &remoteaddr, flag_not), sizeof(remote_addr)); |
| 314 | else |
| 315 | - sprintf(buffer,"%s", remote_addr); |
| 316 | - printf("%-23s", buffer); |
| 317 | - } else |
| 318 | - printf("%-23s", ""); |
| 319 | - |
| 320 | - printf(" %-12s", first?sctp_socket_state_str(state):""); |
| 321 | + sprintf(remote_addr,_("unsupported address family %d"), ((struct sockaddr*)&remoteaddr)->sa_family); |
| 322 | + } |
| 323 | |
| 324 | - first = 0; |
| 325 | - this_local_addr=strtok_r(0," \t\n",&ss1); |
| 326 | - this_remote_addr=strtok_r(0," \t\n",&ss2); |
| 327 | - } |
| 328 | + if(!first) printf("\n"); |
| 329 | + if(first) |
| 330 | + printf("sctp %6u %6u ", rxqueue, txqueue); |
| 331 | + else |
| 332 | + printf(" "); |
| 333 | + if(this_local_addr) { |
| 334 | + if(first) |
| 335 | + sprintf(buffer,"%s:%s", local_addr, local_port); |
| 336 | + else |
| 337 | + sprintf(buffer,"%s", local_addr); |
| 338 | + printf("%-27s", buffer); |
| 339 | + } else |
| 340 | + printf("%-27s", ""); |
| 341 | + printf(" "); |
| 342 | + if(this_remote_addr) { |
| 343 | + if(first) |
| 344 | + sprintf(buffer,"%s:%s", remote_addr, remote_port); |
| 345 | + else |
| 346 | + sprintf(buffer,"%s", remote_addr); |
| 347 | + printf("%-27s", buffer); |
| 348 | + } else |
| 349 | + printf("%-27s", ""); |
| 350 | + |
| 351 | + printf(" %-12s", first?sctp_socket_state_str(state):""); |
| 352 | + |
| 353 | + first = 0; |
| 354 | + this_local_addr=strtok_r(0," \t\n",&ss1); |
| 355 | + this_remote_addr=strtok_r(0," \t\n",&ss2); |
| 356 | } |
| 357 | - |
| 358 | finish_this_one(uid,inode,""); |
| 359 | } |
| 360 | |
| 361 | -- |
| 362 | 1.8.5.2.233.g932f7e4 |
| 363 | |