Add missing includes for FreeBSD compile
[bcusdk.git] / eibd / client / c / openremote.c
blob7eda28a4dcb596fa236b47d8cf8fec7a77c2a2d6
1 /*
2 EIBD client library
3 Copyright (C) 2005-2007 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 In addition to the permissions in the GNU General Public License,
11 you may link the compiled version of this file into combinations
12 with other programs, and distribute those combinations without any
13 restriction coming from the use of this file. (The General Public
14 License restrictions do apply in other respects; for example, they
15 cover modification of the file, and distribution when not linked into
16 a combine executable.)
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/tcp.h>
32 #include <netdb.h>
34 #include "eibclient.h"
35 #include "eibclient-int.h"
37 #include "config.h"
39 /** resolve host name */
40 static int
41 GetHostIP (struct sockaddr_in *sock, const char *Name)
43 #ifdef HAVE_GETHOSTBYNAME_R
44 int len = 2000;
45 struct hostent host;
46 char *buf = (char *) malloc (len);
47 int res;
48 int err;
49 #endif
50 struct hostent *h;
51 if (!Name)
52 return 0;
53 memset (sock, 0, sizeof (*sock));
54 #ifdef HAVE_GETHOSTBYNAME_R
57 res = gethostbyname_r (Name, &host, buf, len, &h, &err);
58 if (res == ERANGE)
60 len += 2000;
61 buf = (char *) realloc (buf, len);
63 if (!buf)
64 return 0;
66 while (res == ERANGE);
68 if (res || !h)
70 free (buf);
71 return 0;
73 #else
74 h = gethostbyname (Name);
75 if (!h)
76 return 0;
77 #endif
78 sock->sin_family = h->h_addrtype;
79 sock->sin_addr.s_addr = (*((unsigned long *) h->h_addr_list[0]));
80 #ifdef HAVE_GETHOSTBYNAME_R
81 if (buf)
82 free (buf);
83 #endif
84 return 1;
87 EIBConnection *
88 EIBSocketRemote (const char *host, int port)
90 EIBConnection *con = (EIBConnection *) malloc (sizeof (EIBConnection));
91 struct sockaddr_in addr;
92 int val = 1;
93 if (!con)
95 errno = ENOMEM;
96 return 0;
99 if (!GetHostIP (&addr, host))
101 free (con);
102 errno = ECONNREFUSED;
103 return 0;
105 addr.sin_port = htons (port);
107 con->fd = socket (addr.sin_family, SOCK_STREAM, 0);
108 if (con->fd == -1)
110 free (con);
111 return 0;
114 if (connect (con->fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
116 int saveerr = errno;
117 close (con->fd);
118 free (con);
119 errno = saveerr;
120 return 0;
122 setsockopt (con->fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
123 con->buflen = 0;
124 con->buf = 0;
125 con->readlen = 0;
127 return con;