summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libugly/getprotoent.c
blob: a648e230744037754e94dc107a0e8dede3bcdb3e (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
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/mman.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <ctype.h>

static int protofd=-1;
static char* protomap;
static unsigned int protolen;

static char* aliases[10];

static char *cur;

/* ip	0	IP		# internet protocol, pseudo protocol number */
struct protoent *getprotoent(void) {
  static struct protoent pe;
  char *last;
  int aliasidx;
  if (protofd<0) {
    protofd=open(_PATH_SERVICES,O_RDONLY);
    if (protofd<0) return 0;
    protolen=lseek(protofd,0,SEEK_END);
    protomap=mmap(0,protolen,PROT_READ|PROT_WRITE,MAP_PRIVATE,protofd,0);
    if ((long)protomap==(-1)) goto error;
    cur=protomap;
  }
  last=protomap+protolen;
again:
  pe.p_name=0;
  pe.p_aliases=aliases; aliases[0]=0;
  pe.p_proto=0;
  if (cur>=last) return 0;
  if (*cur=='#' || *cur=='\n') goto parseerror;
  /* first, the primary name */
  if (!isalpha(*cur)) goto parseerror;
  pe.p_name=cur;
  pe.p_aliases=aliases;
  while (cur<last && isalnum(*cur)) cur++;
  if (cur>=last) return 0;
  if (*cur=='\n') goto parseerror;
  *cur=0; cur++;
  /* second, the protocol number */
  while (cur<last && isblank(*cur)) cur++;
  while (cur<last && isdigit(*cur)) {
    pe.p_proto=pe.p_proto*10+*cur-'0';
    cur++;
  }
/*  pe.p_proto=htons(pe.s_proto); */
  if (cur>=last) return 0;
  if (*cur=='\n') { *cur++=0; return &pe; }
  *cur=0; cur++;
  /* now the aliases */
  for (aliasidx=0;aliasidx<10;++aliasidx) {
    while (cur<last && isblank(*cur)) ++cur;
    aliases[aliasidx]=cur;
    while (cur<last && isalpha(*cur)) ++cur;
    if (*cur=='\n') { *cur++=0; ++aliasidx; break; }
    if (cur>=last || !isblank(*cur)) break;
    *cur++=0;
  }
  aliases[aliasidx]=0;
  return &pe;
parseerror:
  while (cur<last && *cur!='\n') cur++;
  cur++;
  goto again;
error:
  if (protomap!=(char*)-1) munmap(protomap,protolen);
  if (protofd!=-1) close(protofd);
  protomap=(char*)-1;
  protofd=-1;
  errno=ENOMEM;
  return 0;
}

void setprotoent(int stayopen) {
  cur=protomap;
}

struct protoent *getprotobyname(const char *name) {
  struct protoent *s;
  setprotoent(0);
  for (s=getprotoent(); s; s=getprotoent()) {
    char **tmp;
#if 0
    write(1,"found ",6);
    write(1,s->p_name,strlen(s->p_name));
    write(1,"/",1);
    write(1,s->p_proto,strlen(s->p_proto));
    write(1,"\n",1);
    if (!strcmp(name,"auth")) {
      tmp=s->p_aliases;
      write(1,"  aka ",5);
      while (*tmp) {
	write(1,*tmp,strlen(*tmp));
	write(1,", ",2);
	++tmp;
      }
      write(1,"\n",1);
    }
#endif
    if (!strcmp(name,s->p_name))
      return s;
    tmp=s->p_aliases;
    while (*tmp)
      if (!strcmp(name,*tmp++)) return s;
  }
  return 0;
}

struct protoent *getprotobynumber(int proto) {
  struct protoent *s;
  for (s=getprotoent(); s; s=getprotoent()) {
    if (proto==s->p_proto)
      return s;
  }
  return 0;
}

void endprotoent(void) {
  if (protomap!=(char*)-1) munmap(protomap,protolen);
  if (protofd!=-1) close(protofd);
  protomap=(char*)-1;
  protofd=-1;
}