summaryrefslogtreecommitdiff
path: root/src/socket.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2014-05-14 14:24:34 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2014-05-14 14:24:34 +0200
commit0ff825e0e6fe5fc7238e3964d24779a07cb53518 (patch)
tree7b47a9fe58a09e12ed99fdfacc84fff198ce5ce1 /src/socket.cc
parent29ae5ac36d4ffc520232ff393b2455130ec0227e (diff)
Split miav server and client apart. Port client to Qt4. Replace libraw1994 with libiec61883. Add unit tests for multiplexer and fix some bugs in it.
Diffstat (limited to 'src/socket.cc')
-rw-r--r--src/socket.cc28
1 files changed, 22 insertions, 6 deletions
diff --git a/src/socket.cc b/src/socket.cc
index 2ae88dc..b34bae6 100644
--- a/src/socket.cc
+++ b/src/socket.cc
@@ -24,12 +24,13 @@
* along with MIaV; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-#include <config.h>
-
#include "socket.h"
#include <errno.h>
+// for gethostbyname
+#include <netdb.h>
+
Socket::Socket(Info *ginfo)
{
info = ginfo;
@@ -54,6 +55,7 @@ Socket::Socket(u_short port, Info *ginfo)
info->error("Socket: socket() failed!");
}
+ memset((char *) &socketaddr, 0, sizeof(socketaddr));
socketaddr.sin_family = AF_INET; // Use "internet protocol" IP
socketaddr.sin_port = htons(port); // connect to that port
socketaddr.sin_addr.s_addr = INADDR_ANY;
@@ -115,7 +117,7 @@ Socket Socket::slisten()
}
-int Socket::sconnect(char *ip)
+int Socket::sconnect(char *addr)
{
if(err) {
connected = false;
@@ -123,9 +125,23 @@ int Socket::sconnect(char *ip)
return err;
}
- // FIXME: gethostbyname()
- socketaddr.sin_addr.s_addr = inet_addr(ip);
- //inet_aton (ip, &socketaddr.sin_addr);
+ // Do DNS lookup
+ char *ip;
+ struct in_addr **addr_list;
+ struct hostent *he;
+ he = gethostbyname(addr);
+ if(!he || !he->h_length) {
+ /*
+ throw TCPConnectException(addr, toString(port),
+ std::string("host lookup failed: ") + hstrerror(h_errno));
+ */
+ }
+
+ addr_list = (struct in_addr **)he->h_addr_list;
+ // Get first value. We know for sure that there are at least one.
+ ip = inet_ntoa(*addr_list[0]);
+
+ socketaddr.sin_addr.s_addr = inet_addr(ip);
err = connect(ssocket, (struct sockaddr*)&socketaddr, sizeof(socketaddr));
if (err) {