Initial import from FreeBSD RELENG_4:
[games.git] / usr.sbin / portmap / pmap_set / pmap_set.c
1  /*
2   * pmap_set - set portmapper table from data produced by pmap_dump
3   *
4   * Author: Wietse Venema (wietse@wzv.win.tue.nl), dept. of Mathematics and
5   * Computing Science, Eindhoven University of Technology, The Netherlands.
6   */
7
8 #ifndef lint
9 #if 0
10 static char sccsid[] = "@(#) pmap_set.c 1.1 92/06/11 22:53:16";
11 #endif
12 static const char rcsid[] =
13   "$FreeBSD: src/usr.sbin/portmap/pmap_set/pmap_set.c,v 1.6 2000/01/15 23:08:30 brian Exp $";
14 #endif
15
16 #include <err.h>
17 #include <stdio.h>
18 #include <sys/types.h>
19 #ifdef SYSV40
20 #include <netinet/in.h>
21 #endif
22 #include <rpc/rpc.h>
23 #include <rpc/pmap_clnt.h>
24
25 static int parse_line __P((char *, u_long *, u_long *, int *, unsigned *));
26
27 int
28 main(argc, argv)
29     int argc;
30     char **argv;
31 {
32     struct sockaddr_in addr;
33     char    buf[BUFSIZ];
34     u_long  prog;
35     u_long  vers;
36     int     prot;
37     unsigned port;
38
39     get_myaddress(&addr);
40
41     while (fgets(buf, sizeof(buf), stdin)) {
42         if (parse_line(buf, &prog, &vers, &prot, &port) == 0) {
43             warnx("malformed line: %s", buf);
44             return (1);
45         }
46         if (pmap_set(prog, vers, prot, (unsigned short) port) == 0)
47             warnx("not registered: %s", buf);
48     }
49     return (0);
50 }
51
52 /* parse_line - convert line to numbers */
53
54 static int
55 parse_line(buf, prog, vers, prot, port)
56     char *buf;
57     u_long *prog, *vers;
58     int *prot;
59     unsigned *port;
60 {
61     char    proto_name[BUFSIZ];
62
63     if (sscanf(buf, "%lu %lu %s %u", prog, vers, proto_name, port) != 4) {
64         return (0);
65     }
66     if (strcmp(proto_name, "tcp") == 0) {
67         *prot = IPPROTO_TCP;
68         return (1);
69     }
70     if (strcmp(proto_name, "udp") == 0) {
71         *prot = IPPROTO_UDP;
72         return (1);
73     }
74     if (sscanf(proto_name, "%d", prot) == 1) {
75         return (1);
76     }
77     return (0);
78 }