-
Notifications
You must be signed in to change notification settings - Fork 160
/
getservbyport.c
68 lines (52 loc) · 1.97 KB
/
getservbyport.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
POSIX requires that systems must keep a database that given a protocol links:
service name (strings) to their ports and vice versa.
# servent struct
Represents a service.
Must contain at least the following fields:
char *s_name Official name of the service.
char **s_aliases A pointer to an array of pointers to
alternative service names, terminated by
a null pointer.
int s_port The port number at which the service
resides, in network byte order.
char *s_proto The name of the protocol to use when
contacting the service.
# getservbyport
struct servent *getservbyport(int port, const char *proto);
- protocol: protocol name to look for.
If `NULL`, match the first service found on that port for any protocol.
# getservbyname
Same as getservbyport but using the service name itself
struct servent *getservbyname(const char *name, const char *proto);
*/
#include "common.h"
int main(void) {
struct servent *servent;
int port;
char proto[] = "tcp";
char name[] = "http";
/*
TODO what is a service? PING, HTTP, etc. application layer stuff?
How to gete those working then?
*/
port = 80;
servent = getservbyport(port, proto);
if (servent == NULL) {
fprintf(stderr, "getservbyport(%d, %s) failed\n", port, proto);
} else {
printf("getservbyport\n");
printf(" s_name = %s\n", servent->s_name);
printf(" s_proto = %s\n", servent->s_proto);
}
servent = getservbyname(name, proto);
if (servent == NULL) {
fprintf(stderr, "getservbyname(%s, %s) failed\n", name, proto);
} else {
printf("getservbyname\n");
printf(" s_name = %s\n", servent->s_name);
printf(" s_port = %d\n", servent->s_port);
printf(" s_proto = %s\n", servent->s_proto);
}
return EXIT_SUCCESS;
}