Add PCICAP_{ID,NEXTPTR} to avoid using magic number
[dragonfly.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 /*
9  * @(#) pmap_set.c 1.1 92/06/11 22:53:16
10  * $FreeBSD: src/usr.sbin/portmap/pmap_set/pmap_set.c,v 1.6 2000/01/15 23:08:30 brian Exp $
11  * $DragonFly: src/usr.sbin/portmap/pmap_set/pmap_set.c,v 1.4 2004/03/30 02:59:00 cpressey Exp $
12  */
13
14 #include <sys/types.h>
15 #ifdef SYSV40
16 #include <netinet/in.h>
17 #endif
18 #include <rpc/rpc.h>
19 #include <rpc/pmap_clnt.h>
20
21 #include <err.h>
22 #include <stdio.h>
23
24 static int parse_line(char *, u_long *, u_long *, int *, unsigned *);
25
26 int
27 main(int argc, char **argv)
28 {
29     struct sockaddr_in addr;
30     char buf[BUFSIZ];
31     u_long prog;
32     u_long vers;
33     int prot;
34     unsigned port;
35
36     get_myaddress(&addr);
37
38     while (fgets(buf, sizeof(buf), stdin)) {
39         if (parse_line(buf, &prog, &vers, &prot, &port) == 0) {
40             warnx("malformed line: %s", buf);
41             return (1);
42         }
43         if (pmap_set(prog, vers, prot, (unsigned short)port) == 0)
44             warnx("not registered: %s", buf);
45     }
46     return (0);
47 }
48
49 /* parse_line - convert line to numbers */
50
51 static int
52 parse_line(char *buf, u_long *prog, u_long *vers, int *prot,
53            unsigned int *port)
54 {
55     char proto_name[BUFSIZ];
56
57     if (sscanf(buf, "%lu %lu %s %u", prog, vers, proto_name, port) != 4) {
58         return (0);
59     }
60     if (strcmp(proto_name, "tcp") == 0) {
61         *prot = IPPROTO_TCP;
62         return (1);
63     }
64     if (strcmp(proto_name, "udp") == 0) {
65         *prot = IPPROTO_UDP;
66         return (1);
67     }
68     if (sscanf(proto_name, "%d", prot) == 1) {
69         return (1);
70     }
71     return (0);
72 }