Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/usr/kinit/nfsmount/portmap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1122 - (show annotations) (download)
Wed Aug 18 21:11:40 2010 UTC (13 years, 8 months ago) by niro
File MIME type: text/plain
File size: 1501 byte(s)
-updated to klibc-1.5.19
1 #include <sys/types.h>
2 #include <netinet/in.h>
3 #include <asm/byteorder.h> /* __constant_hton* */
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include "nfsmount.h"
8 #include "sunrpc.h"
9
10 struct portmap_call {
11 struct rpc_call rpc;
12 uint32_t program;
13 uint32_t version;
14 uint32_t proto;
15 uint32_t port;
16 };
17
18 struct portmap_reply {
19 struct rpc_reply rpc;
20 uint32_t port;
21 };
22
23 static struct portmap_call call = {
24 .rpc = {
25 .program = __constant_htonl(RPC_PMAP_PROGRAM),
26 .prog_vers = __constant_htonl(RPC_PMAP_VERSION),
27 .proc = __constant_htonl(PMAP_PROC_GETPORT),
28 }
29 };
30
31 uint32_t portmap(uint32_t server, uint32_t program, uint32_t version, uint32_t proto)
32 {
33 struct portmap_reply reply;
34 struct client *clnt;
35 struct rpc rpc;
36 uint32_t port = 0;
37
38 if ((clnt = tcp_client(server, RPC_PMAP_PORT, 0)) == NULL) {
39 if ((clnt = udp_client(server, RPC_PMAP_PORT, 0)) == NULL) {
40 goto bail;
41 }
42 }
43
44 call.program = htonl(program);
45 call.version = htonl(version);
46 call.proto = htonl(proto);
47
48 rpc.call = (struct rpc_call *)&call;
49 rpc.call_len = sizeof(call);
50 rpc.reply = (struct rpc_reply *)&reply;
51 rpc.reply_len = sizeof(reply);
52
53 if (rpc_call(clnt, &rpc) < 0)
54 goto bail;
55
56 if (rpc.reply_len < sizeof(reply)) {
57 fprintf(stderr, "incomplete reply: %zu < %zu\n",
58 rpc.reply_len, sizeof(reply));
59 goto bail;
60 }
61
62 port = ntohl(reply.port);
63
64 bail:
65 dprintf("Port for %d/%d[%s]: %d\n", program, version,
66 proto == IPPROTO_TCP ? "tcp" : "udp", port);
67
68 if (clnt) {
69 client_free(clnt);
70 }
71
72 return port;
73 }