~ubuntu-branches/ubuntu/trusty/musl/trusty-proposed

« back to all changes in this revision

Viewing changes to src/network/getservbyname_r.c

  • Committer: Package Import Robot
  • Author(s): Kevin Bortis
  • Date: 2013-09-20 20:54:14 UTC
  • Revision ID: package-import@ubuntu.com-20130920205414-5b61trtmma18w58o
Tags: upstream-0.9.13
ImportĀ upstreamĀ versionĀ 0.9.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define _GNU_SOURCE
 
2
#include <sys/socket.h>
 
3
#include <netinet/in.h>
 
4
#include <netdb.h>
 
5
#include <inttypes.h>
 
6
#include <errno.h>
 
7
#include <string.h>
 
8
 
 
9
int getservbyname_r(const char *name, const char *prots,
 
10
        struct servent *se, char *buf, size_t buflen, struct servent **res)
 
11
{
 
12
        struct addrinfo *ai, hint = { .ai_family = AF_INET };
 
13
        int i;
 
14
 
 
15
        if (!prots) {
 
16
                int r = getservbyname_r(name, "tcp", se, buf, buflen, res);
 
17
                if (r) r = getservbyname_r(name, "udp", se, buf, buflen, res);
 
18
                return r;
 
19
        }
 
20
 
 
21
        /* Align buffer */
 
22
        i = (uintptr_t)buf & sizeof(char *)-1;
 
23
        if (!i) i = sizeof(char *);
 
24
        if (buflen < 3*sizeof(char *)-i)
 
25
                return ERANGE;
 
26
        buf += sizeof(char *)-i;
 
27
        buflen -= sizeof(char *)-i;
 
28
 
 
29
        if (!strcmp(prots, "tcp")) hint.ai_protocol = IPPROTO_TCP;
 
30
        else if (!strcmp(prots, "udp")) hint.ai_protocol = IPPROTO_UDP;
 
31
        else return EINVAL;
 
32
 
 
33
        switch (getaddrinfo(0, name, &hint, &ai)) {
 
34
        case EAI_MEMORY:
 
35
        case EAI_SYSTEM:
 
36
                return ENOMEM;
 
37
        default:
 
38
                return ENOENT;
 
39
        case 0:
 
40
                break;
 
41
        }
 
42
 
 
43
        se->s_name = (char *)name;
 
44
        se->s_aliases = (void *)buf;
 
45
        se->s_aliases[0] = se->s_name;
 
46
        se->s_aliases[1] = 0;
 
47
        se->s_port = ((struct sockaddr_in *)ai->ai_addr)->sin_port;
 
48
        se->s_proto = (char *)prots;
 
49
 
 
50
        freeaddrinfo(ai);
 
51
        *res = se;
 
52
        return 0;
 
53
}