+int getservport(u_long number, const char *proto)
+{
+ char rpcdata[1024], servdata[1024];
+ struct rpcent rpcbuf, *rpcp;
+ struct servent servbuf, *servp = NULL;
+ int ret;
+
+ ret = getrpcbynumber_r(number, &rpcbuf, rpcdata, sizeof rpcdata,
+ &rpcp);
+ if (ret == 0 && rpcp != NULL) {
+ /* First try name. */
+ ret = getservbyname_r(rpcp->r_name, proto, &servbuf, servdata,
+ sizeof servdata, &servp);
+ if ((ret != 0 || servp == NULL) && rpcp->r_aliases) {
+ const char **a;
+
+ /* Then we try aliases. */
+ for (a = (const char **) rpcp->r_aliases; *a != NULL; a++) {
+ ret = getservbyname_r(*a, proto, &servbuf, servdata,
+ sizeof servdata, &servp);
+ if (ret == 0 && servp != NULL)
+ break;
+ }
+ }
+ }
+
+ if (ret == 0 && servp != NULL)
+ return ntohs(servp->s_port);
+
+ return 0;
+}
+