aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/net/getservice.c
blob: fed448eed734396b1a523830048bfafbe08fa48f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * Simple portable service name lookup.
 * $Id$
 * Copyright (c) 1997 by Tycho Softworks.
 * For conditions of distribution and reuse see product license.
 */

#include <net/socket.h>
#include <std/string.h>

int	getservice(const char *service)
{
	char	servname[32];
	char	*p;
	struct	servent *svc;
	struct	protoent *proto;
	
	if(isdigit(*service))
		return atoi(service);
		
	strcpy(servname, service);
	p = strchr(servname, '/');
	if(!p)
		strcat(servname, "/tcp");
		
	p = strchr(servname, '/');
	*(p++) = 0;
	if(isdigit(*p))
	{
		proto = getprotobynumber(atoi(p));
		if(!proto)
			return 0;
		
			p = proto->p_name;
	}
	svc = getservbyname(servname, p);
	if(!svc)
		return 0;
	
	return ntohs(svc->s_port);
}