1 /*
2  * decode_portmap.c
3  *
4  * RPC portmap.
5  *
6  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
7  *
8  * $Id: decode_portmap.c,v 1.8 2001/03/15 08:33:02 dugsong Exp $
9  */
10 
11 #include "config.h"
12 
13 #include <sys/types.h>
14 #include <rpc/rpc.h>
15 #include <rpc/pmap_prot.h>
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <libnet.h>
20 #include <nids.h>
21 
22 #include "rpc.h"
23 #include "trigger.h"
24 #include "decode.h"
25 
26 int
decode_portmap(u_char * buf,int len,u_char * obuf,int olen)27 decode_portmap(u_char *buf, int len, u_char *obuf, int olen)
28 {
29 	XDR xdrs;
30 	struct rpc_msg msg;
31 	struct pmap *pm, pmap;
32 	struct xid_map *xm;
33 	int hdrlen;
34 
35 	if ((hdrlen = rpc_decode(buf, len, &msg)) == 0)
36 		return (0);
37 
38 	if (msg.rm_direction == CALL &&
39 	    msg.rm_call.cb_prog == PMAPPROG &&
40 	    msg.rm_call.cb_proc == PMAPPROC_GETPORT) {
41 		xdrmem_create(&xdrs, buf + hdrlen, len - hdrlen, XDR_DECODE);
42 		if (xdr_pmap(&xdrs, &pmap)) {
43 			if ((pm = malloc(sizeof(*pm))) != NULL) {
44 				*pm = pmap;
45 				xid_map_enter(msg.rm_xid, PMAPPROG, PMAPVERS,
46 					      PMAPPROC_GETPORT, (void *) pm);
47 			}
48 		}
49 		xdr_destroy(&xdrs);
50 	}
51 	else if (msg.rm_direction == REPLY &&
52 		 (xm = xid_map_find(msg.rm_xid)) != NULL) {
53 		if (msg.rm_reply.rp_stat == MSG_ACCEPTED &&
54 		    msg.acpted_rply.ar_stat == SUCCESS) {
55 			pm = (struct pmap *)xm->data;
56 			xdrmem_create(&xdrs, buf + hdrlen, len - hdrlen,
57 				      XDR_DECODE);
58 			if (xdr_u_long(&xdrs, &pm->pm_port)) {
59 				trigger_rpc(pm->pm_prog, pm->pm_prot,
60 					    pm->pm_port);
61 				trigger_rpc(pm->pm_prog, pm->pm_prot,
62 					    0 - (int) pm->pm_port);
63 			}
64 			xdr_destroy(&xdrs);
65 		}
66 		free(xm->data);
67 		memset(xm, 0, sizeof(*xm));
68 	}
69 	return (0);
70 }
71