xref: /netbsd/external/bsd/ipf/dist/lib/portname.c (revision f52ace7a)
1*f52ace7aSdarrenr /*	$NetBSD: portname.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
226945a25Schristos 
326945a25Schristos /*
4c50c2f6fSdarrenr  * Copyright (C) 2012 by Darren Reed.
526945a25Schristos  *
626945a25Schristos  * See the IPFILTER.LICENCE file for details on licencing.
726945a25Schristos  *
8*f52ace7aSdarrenr  * Id: portname.c,v 1.1.1.2 2012/07/22 13:44:40 darrenr Exp $
926945a25Schristos  */
1026945a25Schristos #include "ipf.h"
1126945a25Schristos 
1226945a25Schristos 
portname(pr,port)1326945a25Schristos char *portname(pr, port)
1426945a25Schristos 	int pr, port;
1526945a25Schristos {
1626945a25Schristos 	static char buf[32];
1726945a25Schristos 	struct protoent *p = NULL;
1826945a25Schristos 	struct servent *sv = NULL;
1926945a25Schristos 	struct servent *sv1 = NULL;
2026945a25Schristos 
2126945a25Schristos 	if ((opts & OPT_NORESOLVE) == 0) {
2226945a25Schristos 		if (pr == -1) {
2326945a25Schristos 			if ((sv = getservbyport(htons(port), "tcp"))) {
2426945a25Schristos 				strncpy(buf, sv->s_name, sizeof(buf)-1);
2526945a25Schristos 				buf[sizeof(buf)-1] = '\0';
2626945a25Schristos 				sv1 = getservbyport(htons(port), "udp");
2726945a25Schristos 				sv = strncasecmp(buf, sv->s_name, strlen(buf)) ?
2826945a25Schristos 				     NULL : sv1;
2926945a25Schristos 			}
3026945a25Schristos 			if (sv)
3126945a25Schristos 				return buf;
3226945a25Schristos 		} else if ((pr != -2) && (p = getprotobynumber(pr))) {
3326945a25Schristos 			if ((sv = getservbyport(htons(port), p->p_name))) {
3426945a25Schristos 				strncpy(buf, sv->s_name, sizeof(buf)-1);
3526945a25Schristos 				buf[sizeof(buf)-1] = '\0';
3626945a25Schristos 				return buf;
3726945a25Schristos 			}
3826945a25Schristos 		}
3926945a25Schristos 	}
4026945a25Schristos 
4126945a25Schristos 	(void) sprintf(buf, "%d", port);
4226945a25Schristos 	return buf;
4326945a25Schristos }
44