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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [src/] [getserv.c] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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