2 * Copyright 2006-2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler, axeld@pinc-software.de
10 #include <arpa/inet.h>
15 #include <netinet/in.h>
21 #include <SupportDefs.h>
27 extern const char* __progname
;
28 const char* kProgramName
= __progname
;
30 static int sResolveNames
= 1;
32 struct address_family
{
35 const char* identifiers
[4];
36 void (*print_address
)(sockaddr
* address
);
40 static void inet_print_address(sockaddr
* address
);
42 static const address_family kFamilies
[] = {
46 {"AF_INET", "inet", "ipv4", NULL
},
49 { -1, NULL
, {NULL
}, NULL
}
54 inet_print_address(sockaddr
* _address
)
56 sockaddr_in
& address
= *(sockaddr_in
*)_address
;
58 if (address
.sin_family
!= AF_INET
|| address
.sin_len
== 0) {
64 servent
* service
= NULL
;
66 host
= gethostbyaddr((const char*)&address
.sin_addr
, sizeof(in_addr
),
68 service
= getservbyport(address
.sin_port
, NULL
);
73 hostName
= host
->h_name
;
74 else if (address
.sin_addr
.s_addr
== INADDR_ANY
)
77 hostName
= inet_ntoa(address
.sin_addr
);
80 int length
= strlcpy(buffer
, hostName
, sizeof(buffer
));
84 strlcpy(port
, service
->s_name
, sizeof(port
));
85 else if (address
.sin_port
== 0)
88 snprintf(port
, sizeof(port
), "%u", ntohs(address
.sin_port
));
90 snprintf(buffer
+ length
, sizeof(buffer
) - length
, ":%s", port
);
92 printf("%-22s", buffer
);
102 printf("usage: %s [-nh]\n", kProgramName
);
103 printf("options:\n");
104 printf(" -n don't resolve names\n");
105 printf(" -h this help\n");
112 get_address_family(const char* argument
, int32
& familyIndex
)
114 for (int32 i
= 0; kFamilies
[i
].family
>= 0; i
++) {
115 for (int32 j
= 0; kFamilies
[i
].identifiers
[j
]; j
++) {
116 if (!strcmp(argument
, kFamilies
[i
].identifiers
[j
])) {
124 // defaults to AF_INET
134 main(int argc
, char** argv
)
138 static struct option longOptions
[] = {
139 {"help", no_argument
, 0, 'h'},
140 {"numeric", no_argument
, 0, 'n'},
145 opt
= getopt_long(argc
, argv
, "hn", longOptions
, &optionIndex
);
148 // end of arguments, do nothing
162 bool printProgram
= true;
163 // TODO: add some more program options... :-)
165 printf("Proto Recv-Q Send-Q Local Address Foreign Address "
171 while (_kern_get_next_socket_stat(family
, &cookie
, &stat
) == B_OK
) {
172 protoent
* proto
= getprotobynumber(stat
.protocol
);
174 printf("%-6s ", proto
->p_name
);
176 printf("%-6d ", stat
.protocol
);
178 printf("%6lu ", stat
.receive_queue_size
);
179 printf("%6lu ", stat
.send_queue_size
);
181 inet_print_address((sockaddr
*)&stat
.address
);
182 inet_print_address((sockaddr
*)&stat
.peer
);
183 printf("%-12s ", stat
.state
);
186 if (printProgram
&& get_team_info(stat
.owner
, &info
) == B_OK
) {
188 char* name
= strchr(info
.args
, ' ');
193 name
= strrchr(info
.args
, '/');
199 printf("%ld/%s\n", stat
.owner
, name
);
201 printf("%ld\n", stat
.owner
);