]> git.neil.brown.name Git - portmap.git/blob - pmap_set.c
Only fail an 'unregister' attempt if nothing can be unregistered.
[portmap.git] / 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 #include <stdio.h>
9 #include <sys/types.h>
10 #ifdef SYSV40
11 #include <netinet/in.h>
12 #endif
13 #include <rpc/rpc.h>
14 #include <rpc/pmap_clnt.h>
15
16 static int
17 parse_line(char *buf, u_long *prog, u_long *vers,
18            int *prot, unsigned *port);
19
20 int
21 main(int argc, char **argv)
22 {
23     char    buf[BUFSIZ];
24     u_long  prog;
25     u_long  vers;
26     int     prot;
27     unsigned port;
28
29     while (fgets(buf, sizeof(buf), stdin)) {
30         if (parse_line(buf, &prog, &vers, &prot, &port) == 0) {
31             fprintf(stderr, "%s: malformed line: %s", argv[0], buf);
32             return (1);
33         }
34         if (pmap_set(prog, vers, prot, (unsigned short) port) == 0)
35             fprintf(stderr, "not registered: %s", buf);
36     }
37     return (0);
38 }
39
40 /* parse_line - convert line to numbers */
41
42 static int
43 parse_line(char *buf, u_long *prog, u_long *vers,
44            int *prot, unsigned *port)
45 {
46     char    proto_name[256];
47
48     if (sscanf(buf, "%lu %lu %255s %u", prog, vers, proto_name, port) != 4) {
49         return (0);
50     }
51     if (strcmp(proto_name, "tcp") == 0) {
52         *prot = IPPROTO_TCP;
53         return (1);
54     }
55     if (strcmp(proto_name, "udp") == 0) {
56         *prot = IPPROTO_UDP;
57         return (1);
58     }
59     if (sscanf(proto_name, "%d", prot) == 1) {
60         return (1);
61     }
62     return (0);
63 }