Debian Codegraph @

src/ikarus-getaddrinfo.c

raw
1 /*
2 * Ikarus Scheme -- A compiler for R6RS Scheme.
3 * Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19
20
21 #include "config.h"
22 #if (!HAVE_GETADDRINFO)
23 #include "ikarus-getaddrinfo.h"
24
25 #include <netdb.h>
26 #include <sys/socket.h>
27 #include <cygwin/in.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 int
32 getaddrinfo(const char *hostname, const char* servname,
33 const struct addrinfo* hints, struct addrinfo** res){
34 /* hints not used */
35 struct servent* sent = getservbyname(servname, "tcp");
36 if (sent == 0) return -1;
37 struct hostent* hent = gethostbyname(hostname);
38 if (!hent){
39 return -1;
40 }
41 struct addrinfo* r = malloc(sizeof(struct addrinfo));
42 if(r == 0) return -1;
43 r->ai_family = hent->h_addrtype;
44 r->ai_socktype = SOCK_STREAM;
45 r->ai_protocol = 0;
46 r->ai_addrlen = sizeof(struct sockaddr_in);
47 r->ai_addr = malloc(r->ai_addrlen);
48 if (r->ai_addr == 0){
49 free(r);
50 return -1;
51 }
52 struct sockaddr_in* sa_in = (struct sockaddr_in *)r->ai_addr;
53 memset(sa_in, 0, sizeof(struct sockaddr_in));
54 sa_in->sin_family = PF_INET;
55 sa_in->sin_port = sent->s_port;
56 struct in_addr** ap = (struct in_addr **)hent->h_addr_list;
57 memcpy(&sa_in->sin_addr, *ap, sizeof(struct in_addr));
58 r->ai_next = NULL;
59 *res = r;
60 return 0;
61 }
62
63 void
64 freeaddrinfo(struct addrinfo *ai){
65 free(ai->ai_addr);
66 free(ai);
67 }
68
69 #endif