summaryrefslogtreecommitdiffstats
path: root/src/windows/wshelper/hesservb.c
blob: 01db3a4bf04dea303087b712a18cf5498971f1fe (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *	@doc HESIOD
 *
 * @module hesservb.c |
 *
 *
 *	  Contains the definition for hes_getservbyname,
 *
 *	  WSHelper DNS/Hesiod Library for WINSOCK
 *
 */

/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getservbyname.c	5.3 (Berkeley) 5/19/86";
#endif /* LIBC_SCCS and not lint */

#include <hesiod.h>
#include <windows.h>
#include <winsock.h>
#include <windns.h>

#include <string.h>

#include <stdio.h>
#include <ctype.h>

#define cistrcmp stricmp

#define LISTSIZE 15


/*
	This function will query a Hesiod server for a servent structure given
	a service name and protocol. This is a replacement for the Winsock
	getservbyname function which normally just uses a local services
	file. This allows a site to use a centralized database for adding new
	services.

	defined in hesservb.c

	\param[in]	name	pointer to the official name of the service, eg "POP3".
	\param[in]	proto	pointer to the protocol to use when contacting the service, e.g. "TCP"

	\retval				NULL if there was an error or a pointer to a servent structure. The caller must
						never attempt to modify this structure or to free any of its components.
						Furthermore, only one copy of this structure is allocated per call per thread, so the application should copy any information it needs before
						issuing another hes_getservbyname call

*/

extern DWORD dwHesServIndex;
struct servent *
WINAPI
hes_getservbyname(char *name, char *proto)
{
    struct servent *p;
    register char **cp;
    register char** hesinfo;
    register int i = 0;

    char buf[DNS_MAX_NAME_BUFFER_LENGTH];
    char* l;

    hesinfo = hes_resolve(name, "service");
    cp = hesinfo;
    if (cp == NULL)
	return(NULL);
    p = (struct servent*)(TlsGetValue(dwHesServIndex));
    if (p == NULL) {
	LPVOID lpvData = (LPVOID) LocalAlloc(LPTR, sizeof(struct servent));
	if (lpvData != NULL) {
	    TlsSetValue(dwHesServIndex, lpvData);
	    p = (struct servent*)lpvData;
	} else
	    return NULL;
    }
    if (!p->s_name)
        p->s_name = LocalAlloc(LPTR, DNS_MAX_LABEL_BUFFER_LENGTH);
    if (!p->s_proto)
        p->s_proto = LocalAlloc(LPTR, DNS_MAX_LABEL_BUFFER_LENGTH);
    if (!p->s_aliases)
        p->s_aliases = LocalAlloc(LPTR, LISTSIZE*sizeof(LPSTR));

    for (;*cp; cp++) {
	register char *servicename, *protoname, *port;
        strcpy(buf, *cp);
        l = buf;
	while(*l && (*l == ' ' || *l == '\t')) l++;
	servicename = l;
	while(*l && *l != ' ' && *l != '\t' && *l != ';') l++;
	if (*l == '\0') continue; /* malformed entry */
	*l++ = '\0';
	while(*l && (*l == ' ' || *l == '\t')) l++;
	protoname = l;
	while(*l && *l != ' ' && *l != ';') l++;
	if (*l == '\0') continue; /* malformed entry */
	*l++ = '\0';
	if (cistrcmp(proto, protoname)) continue; /* wrong port */
	while(*l && (*l == ' ' || *l == '\t' || *l == ';')) l++;
	if (*l == '\0') continue; /* malformed entry */
	port = l;
	while(*l && (*l != ' ' && *l != '\t' && *l != ';')) l++;
	if (*l) *l++ = '\0';
	if (*l != '\0') {
	    do {
		char* tmp = l;
		while(*l && !isspace(*l)) l++;
		if (*l) *l++ = 0;
                if (p->s_aliases[i])
                    p->s_aliases[i] = LocalAlloc(LPTR, strlen(tmp));
                strcpy(p->s_aliases[i], tmp);
                i++;
	    } while(*l);
	}
	p->s_aliases[i] = NULL;
        for (; i<LISTSIZE; i++)
        {
            if (p->s_aliases[i]){
                LocalFree(p->s_aliases[i]);
                p->s_aliases[i] = NULL;
            }
        }
	strcpy(p->s_name, servicename);
	p->s_port = htons((u_short)atoi(port));
	strcpy(p->s_proto, protoname);
        if (hesinfo)
            hes_free(hesinfo);
	return (p);
    }
    return(NULL);
}