OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [common/] [v2_0/] [src/] [getserv.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      lib/getserv.c
4
//
5
//      getservbyname(), getservbyport()
6
//
7
//==========================================================================
8
//####BSDCOPYRIGHTBEGIN####
9
//
10
// -------------------------------------------
11
//
12
// Portions of this software may have been derived from OpenBSD or other sources,
13
// and are covered by the appropriate copyright disclaimers included herein.
14
//
15
// -------------------------------------------
16
//
17
//####BSDCOPYRIGHTEND####
18
//==========================================================================
19
//#####DESCRIPTIONBEGIN####
20
//
21
// Author(s):    gthomas
22
// Contributors: gthomas
23
// Date:         2000-01-10
24
// Purpose:      
25
// Description:  
26
//              
27
//
28
//####DESCRIPTIONEND####
29
//
30
//==========================================================================
31
 
32
 
33
#include <sys/param.h>
34
#include <netdb.h>
35
#include <errno.h>
36
 
37
// These must return the port in network byte order.
38
//
39
// This means treated as a short, because it's a port, despite the types in
40
// the API and the struct being ints.
41
// 
42
// The argument to getservbyport() is also network byte order, so that code
43
// must change to flip before comparing.
44
 
45
static struct servent services[] = {
46
    { "ftp",      0,   21 , "tcp" },
47
    { "ftp-data", 0,   20 , "tcp" },
48
    { "domain",   0,   53 , "udp" },
49
    { "tftp",     0,   69 , "udp" },
50
    { "snmp",     0,  161 , "udp" },
51
 
52
    { NULL,       0,     0       , NULL  }
53
};
54
 
55
// Note that this contains no interlocking between clients of the
56
// interface; but this is completely typical of such APIs.
57
 
58
static struct servent *
59
setreturned( struct servent *p )
60
{
61
    static struct servent returned;
62
 
63
    returned.s_name     = p->s_name;
64
    returned.s_aliases  = p->s_aliases;
65
    returned.s_port     = htons(p->s_port); // return in net order
66
    returned.s_proto    = p->s_proto;
67
    return &returned;
68
}
69
 
70
struct servent *
71
getservbyname(const char *name, const char *proto)
72
{
73
    struct servent *p = services;
74
    while (p->s_name) {
75
        if ((strcmp(name, p->s_name) == 0) &&
76
            (strcmp(proto, p->s_proto) == 0)) {
77
            return setreturned(p);
78
        }
79
        p++;
80
    }
81
    errno = ENOENT;
82
    return (struct servent *)0;
83
}
84
 
85
struct servent *
86
getservbyport(const int num, const char *proto)
87
{
88
    struct servent *p = services;
89
    int port = ntohs(num);
90
    while (p->s_name) {
91
        if ((p->s_port == port) &&
92
            (strcmp(proto, p->s_proto) == 0)) {
93
            return setreturned(p);
94
        }
95
        p++;
96
    }
97
    errno = ENOENT;
98
    return (struct servent *)0;
99
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.