summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authordeva <deva>2006-08-25 21:20:07 +0000
committerdeva <deva>2006-08-25 21:20:07 +0000
commit8196872e3b240ef93fc17dd3c05a7d97a8015d88 (patch)
treee531edeb1c7178a8e58170c84645ce8eb706624e /lib
parent347b1d8ed3a4f780f3a5c0d57a04eab05ca517a2 (diff)
Rearranged the network and socket code.origin
Added status messages sent over the network using udp packets.
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am4
-rw-r--r--lib/liblua_wrapper.cc24
-rw-r--r--lib/liblua_wrapper.h4
-rw-r--r--lib/network.cc39
-rw-r--r--lib/network.h5
-rw-r--r--lib/socket.cc142
-rw-r--r--lib/socket.h50
-rw-r--r--lib/status.h17
-rw-r--r--lib/tcp_socket.cc108
-rw-r--r--lib/tcp_socket.h39
-rw-r--r--lib/udp_socket.cc108
-rw-r--r--lib/udp_socket.h48
12 files changed, 528 insertions, 60 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 6ee8bf4..4e2b923 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -16,6 +16,8 @@ libmiav_la_SOURCES = \
network.cc \
semaphore.cc \
socket.cc \
+ udp_socket.cc \
+ tcp_socket.cc \
status.cc \
thread.cc \
threadsafe_queue.cc \
@@ -42,6 +44,8 @@ EXTRA_DIST = \
queue.h \
semaphore.h \
socket.h \
+ udp_socket.h \
+ tcp_socket.h \
status.h \
thread.h \
threadsafe_queue.h \
diff --git a/lib/liblua_wrapper.cc b/lib/liblua_wrapper.cc
index 6535f4e..9e06be9 100644
--- a/lib/liblua_wrapper.cc
+++ b/lib/liblua_wrapper.cc
@@ -51,10 +51,10 @@ int LibLUAWrapper::loadFile(char *fname)
case LUA_ERRSYNTAX: //syntax error during pre-compilation;
case LUA_ERRMEM: //memory allocation error.
case LUA_ERRFILE: //cannot open/read the file.
- error = std::string(lua_tostring(L, lua_gettop(L)));
+ strerr = std::string(lua_tostring(L, lua_gettop(L)));
return 1;
default:
- error = std::string("Unknown return value of luaL_loadfile.");
+ strerr = std::string("Unknown return value of luaL_loadfile.");
return 1;
}
@@ -66,10 +66,10 @@ int LibLUAWrapper::loadFile(char *fname)
case LUA_ERRRUN:// a runtime error.
case LUA_ERRMEM:// memory allocation error. For such errors, Lua does not call the error handler function.
case LUA_ERRERR:// error while running the error handler function.
- error = std::string(lua_tostring(L, lua_gettop(L)));
+ strerr = std::string(lua_tostring(L, lua_gettop(L)));
return 1;
default:
- error = std::string("Unknown return value of lua_pcall.");
+ strerr = std::string("Unknown return value of lua_pcall.");
return 1;
}
@@ -87,10 +87,10 @@ int LibLUAWrapper::loadBuffer(char *buffer)
break;
case LUA_ERRSYNTAX: //syntax error during pre-compilation;
case LUA_ERRMEM: //memory allocation error.
- error = std::string(lua_tostring(L, lua_gettop(L)));
+ strerr = std::string(lua_tostring(L, lua_gettop(L)));
return 1;
default:
- error = std::string("Unknown return value of luaL_loadstring.");
+ strerr = std::string("Unknown return value of luaL_loadstring.");
return 1;
}
@@ -102,10 +102,10 @@ int LibLUAWrapper::loadBuffer(char *buffer)
case LUA_ERRRUN:// a runtime error.
case LUA_ERRMEM:// memory allocation error. For such errors, Lua does not call the error handler function.
case LUA_ERRERR:// error while running the error handler function.
- error = std::string(lua_tostring(L, lua_gettop(L)));
+ strerr = std::string(lua_tostring(L, lua_gettop(L)));
return 1;
default:
- error = std::string("Unknown return value of lua_pcall.");
+ strerr = std::string("Unknown return value of lua_pcall.");
return 1;
}
@@ -164,9 +164,9 @@ std::string LibLUAWrapper::getString(char *name)
return val;
}
-std::string LibLUAWrapper::getError()
+std::string LibLUAWrapper::error()
{
- return error;
+ return strerr;
}
#ifdef LUA_TEST
@@ -184,12 +184,12 @@ int main()
LibLUAWrapper lua;
if(lua.loadBuffer((char*)preload)) {
- fprintf(stderr, "LUA buffer error: %s\n", lua.getError().c_str());
+ fprintf(stderr, "LUA buffer error: %s\n", lua.error().c_str());
return 1;
}
if(lua.loadFile("test.lua")) {
- fprintf(stderr, "LUA load error: %s\n", lua.getError().c_str());
+ fprintf(stderr, "LUA load error: %s\n", lua.error().c_str());
return 1;
}
diff --git a/lib/liblua_wrapper.h b/lib/liblua_wrapper.h
index 70eb8a8..30371fe 100644
--- a/lib/liblua_wrapper.h
+++ b/lib/liblua_wrapper.h
@@ -54,11 +54,11 @@ public:
bool getBoolean(char *name);
std::string getString(char *name);
- std::string getError();
+ std::string error();
private:
lua_State *L;
- std::string error;
+ std::string strerr;
};
#endif/*__MIAV_LIBLUA_WRAPPER_H__*/
diff --git a/lib/network.cc b/lib/network.cc
index cb3f94e..4957bb6 100644
--- a/lib/network.cc
+++ b/lib/network.cc
@@ -50,11 +50,11 @@ Network::~Network()
int Network::write(void *buf, int size)
{
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Write attempted to a socket not connected!");
return -1;
}
- int n = send(s->ssocket, buf, size, MSG_WAITALL);
+ int n = send(s->sock, buf, size, MSG_WAITALL);
if(n == -1) {
MIaV::info->error("An error occurred!");
@@ -65,11 +65,11 @@ int Network::write(void *buf, int size)
int Network::read(void *buf, int size)
{
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Read attempted from a socket not connected!");
return -1;
}
- int n = recv(s->ssocket, buf, size, MSG_WAITALL);
+ int n = recv(s->sock, buf, size, MSG_WAITALL);
if(n == -1) {
MIaV::info->error("An error occurred!");
@@ -95,7 +95,7 @@ int Network::sendPackage(n_header *h, void* buf, int bufsz)
struct msghdr msg;
struct iovec iovecs[2];
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Write attempted to a socket not connected!");
return -1;
}
@@ -111,7 +111,7 @@ int Network::sendPackage(n_header *h, void* buf, int bufsz)
msg.msg_iov[1].iov_base = buf;
msg.msg_iov[1].iov_len = bufsz;
- int n = sendmsg(s->ssocket, &msg, 0);
+ int n = sendmsg(s->sock, &msg, 0);
if(n < 0) {
MIaV::info->error("A network error ocurred during sendPackage!");
return -1;
@@ -125,7 +125,7 @@ int Network::recvPackage(n_header *h, void* buf, int bufsz)
struct msghdr msg;
struct iovec iovecs[2];
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Read attempted to a socket not connected!");
return -1;
}
@@ -141,7 +141,7 @@ int Network::recvPackage(n_header *h, void* buf, int bufsz)
msg.msg_iov = iovecs;
msg.msg_iovlen = 2;
- int n = recvmsg(s->ssocket, &msg, MSG_WAITALL);
+ int n = recvmsg(s->sock, &msg, MSG_WAITALL);
if(n < 0) {
MIaV::info->error("A network error ocurred during recvPackage!");
@@ -161,7 +161,7 @@ int Network::sendFrame(Frame *frame)
struct msghdr msg;
struct iovec iovecs[2];
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Write attempted to a socket not connected!");
return -1;
}
@@ -196,7 +196,7 @@ int Network::sendFrame(Frame *frame)
msg.msg_iov[1].iov_base = aframe;
msg.msg_iov[1].iov_len = aframesize;
- int n = sendmsg(s->ssocket, &msg, 0);
+ int n = sendmsg(s->sock, &msg, 0);
if(n < 0) {
MIaV::info->error("A network error ocurred during sendPackage!");
@@ -215,7 +215,7 @@ Frame *Network::recvFrame()
struct msghdr msg;
struct iovec iovecs[2];
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Read attempted to a socket not connected!");
return NULL;
}
@@ -239,7 +239,7 @@ Frame *Network::recvFrame()
msg.msg_iov = iovecs;
msg.msg_iovlen = 2;
- int n = recvmsg(s->ssocket, &msg, MSG_WAITALL);
+ int n = recvmsg(s->sock, &msg, MSG_WAITALL);
if(n < 0) {
MIaV::info->error("A network error ocurred during recvPackage!");
@@ -269,18 +269,3 @@ Frame *Network::recvFrame()
return frame;
}
-int Network::sendStatus(Status *status)
-{
- return 0;
-}
-
-int Network::recvStatus(Status *status)
-{
- /*
- status.diskspace = 10;
- status.diskspace_max = 100;
- status.load = 80;
- status.load_max = 100;
- */
- return 0;
-}
diff --git a/lib/network.h b/lib/network.h
index dc7d614..eb545d4 100644
--- a/lib/network.h
+++ b/lib/network.h
@@ -29,6 +29,7 @@
#define __MIAVLIB_NETWORK_H__
#include "socket.h"
+
#include "package.h"
#include "frame.h"
#include "status.h"
@@ -55,10 +56,6 @@ public:
int sendFrame(Frame *frame);
Frame *recvFrame();
- // Status message communication
- int sendStatus(Status *status);
- int recvStatus(Status *status);
-
private:
Socket *s;
diff --git a/lib/socket.cc b/lib/socket.cc
index 6189d23..37e985a 100644
--- a/lib/socket.cc
+++ b/lib/socket.cc
@@ -24,13 +24,147 @@
* 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>
+
+// for inet_addr
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+Socket::Socket(int port, std::string addr)
+{
+ this->prt = port;
+ this->addr = addr;
+ connected = false;
+}
+
+Socket::~Socket()
+{
+ disconnect();
+}
+
+int Socket::setPort(int port)
+{
+ if(connected) {
+ strerr = "Cannot change port, while socket is connected.";
+ return 1;
+ }
+
+ this->prt = port;
+ return 0;
+}
+
+int Socket::port()
+{
+ return prt;
+}
+
+int Socket::setAddress(std::string addr)
+{
+ if(connected) {
+ strerr = "Cannot change address, while socket is connected.";
+ return 1;
+ }
+
+ this->addr = addr;
+ return 0;
+}
+
+std::string Socket::address()
+{
+ return addr;
+}
+
+static int _connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
+{return connect(sockfd, serv_addr, addrlen);}
+int Socket::connect()
+{
+ if(connected) {
+ strerr = "Could not connect, socket is already connected.";
+ return 1;
+ }
+
+ // Do DNS lookup
+ char *ip;
+ struct in_addr **addr_list;
+ struct hostent *he;
+ he = gethostbyname(addr.c_str());
+ if(!he || !he->h_length) {
+ strerr = hstrerror(h_errno);
+ return 1;
+ }
+
+ addr_list = (struct in_addr **)he->h_addr_list;
+ // for(int i = 0; addr_list[i] != NULL; i++) {
+ ip = inet_ntoa(*addr_list[0]);
+ // }
+
+ struct sockaddr_in socketaddr;
+ memset((char *) &socketaddr, sizeof(socketaddr), 0);
+ socketaddr.sin_family = AF_INET;
+ socketaddr.sin_port = htons(prt);
+ socketaddr.sin_addr.s_addr = inet_addr(ip);
+
+ if(_connect(sock, (struct sockaddr*)&socketaddr, sizeof(socketaddr))) {
+ strerr = strerror(errno);
+ return 1;
+ }
+
+ connected = true;
+
+ return 0;
+}
+
+int Socket::disconnect()
+{
+ if(!connected) {
+ strerr = "Could not disconnect, socket is already disconnected.";
+ return 1;
+ }
+
+ close(sock);
+ connected = false;
+
+ return 0;
+}
+
+std::string Socket::error()
+{
+ return strerr;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/*
+
#include "info.h"
#include <errno.h>
+
Socket::Socket()
{
connected = false;
@@ -73,7 +207,7 @@ Socket Socket::slisten()
Socket s;
if(err) {
- //MIaV::info->error("Socket: No socket present!");
+ // MIaV::info->error("Socket: No socket present!");
return s;
}
if(!connected) {
@@ -147,3 +281,5 @@ bool Socket::hasError()
{
return err != 0;
}
+
+*/
diff --git a/lib/socket.h b/lib/socket.h
index dde4729..f82cd82 100644
--- a/lib/socket.h
+++ b/lib/socket.h
@@ -3,7 +3,7 @@
* socket.h
*
* Mon Nov 8 10:49:33 CET 2004
- * Copyright 2004 Bent Bisballe
+ * Copyright 2004 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
@@ -24,10 +24,49 @@
* 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"
-#ifndef __MIAVLIB_SOCKET_H__
-#define __MIAVLIB_SOCKET_H__
+#ifndef __MIAV_SOCKET_H__
+#define __MIAV_SOCKET_H__
+#include <string>
+
+#define PORT_UNDEFINED -1
+
+class Network;
+
+class Socket {
+ friend class Network;
+ // friend class Socket;
+public:
+ Socket(int port, std::string addr);
+ ~Socket();
+
+ int setPort(int port);
+ int port();
+
+ int setAddress(std::string addr);
+ std::string address();
+
+ int connect();
+ int disconnect();
+
+ std::string error();
+
+protected:
+ bool connected;
+
+ // Error string
+ std::string strerr;
+
+ // c socket
+ int sock;
+
+ // host info
+ int prt;
+ std::string addr;
+};
+
+
+/* // Old interface
#include <stdio.h>
#include <string.h>
@@ -42,6 +81,7 @@ public:
Socket();
Socket(u_short port);
~Socket();
+
Socket slisten();
int sconnect(char *ip);
bool isConnected();
@@ -54,5 +94,7 @@ public:
private:
int err;
};
+*/
+
#endif/*__SOCKET_H__*/
diff --git a/lib/status.h b/lib/status.h
index d947123..3988aad 100644
--- a/lib/status.h
+++ b/lib/status.h
@@ -30,14 +30,15 @@
/**
* Internal data class for the server status info.
*/
-class Status {
-public:
- unsigned long long int diskspace;
- unsigned long long int diskspace_max;
- unsigned short load;
- unsigned short load_max;
-};
-
+typedef struct {
+ unsigned char diskspace;
+ unsigned char diskspace_max;
+ unsigned char load;
+ unsigned char load_max;
+} status_t;
+typedef struct {
+ char magic[4];
+} status_request_t;
#endif/*__MIAV_STATUS_H__*/
diff --git a/lib/tcp_socket.cc b/lib/tcp_socket.cc
new file mode 100644
index 0000000..8468df9
--- /dev/null
+++ b/lib/tcp_socket.cc
@@ -0,0 +1,108 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * tcp_socket.cc
+ *
+ * Fri Aug 18 00:12:34 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of MIaV.
+ *
+ * MIaV is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MIaV is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MIaV; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "tcp_socket.h"
+
+#include <errno.h>
+
+// for connect, listen, bind and accept
+#include <sys/types.h>
+#include <sys/socket.h>
+
+// For socket
+#include <sys/types.h>
+#include <sys/socket.h>
+
+// For TCP
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+
+// For inet_ntoa
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+TCPSocket::TCPSocket(int port, std::string addr) : Socket(port, addr)
+{
+ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+}
+
+static int _listen(int sockfd, int backlog){return listen(sockfd, backlog);}
+int TCPSocket::listen(TCPSocket *s)
+{
+ if(!s) {
+ strerr = "TCPSocket is a NULL pointer.";
+ return 1;
+ }
+
+ if(s->connected) {
+ strerr = "TCPSocket already connected.";
+ return 1;
+ }
+
+ if(!connected) {
+ struct sockaddr_in socketaddr;
+ memset((char *) &socketaddr, sizeof(socketaddr), 0);
+ socketaddr.sin_family = AF_INET;
+ socketaddr.sin_port = htons(prt);
+ socketaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ if(bind(sock, (struct sockaddr*)&socketaddr, sizeof(socketaddr)) == -1) {
+ strerr = "Socket: bind() failed! ";
+ strerr.append(strerror(errno));
+ return 1;
+ }
+
+ if(_listen(sock, 5) == -1) {
+ strerr = "Socket: listen() failed! ";
+ strerr.append(strerror(errno));
+ return 1;
+ }
+
+ connected = true;
+ }
+
+ // accept new connection and get its connection descriptor
+ struct sockaddr_in ssocketaddr;
+ int csalen = sizeof(ssocketaddr);
+
+ s->sock = accept(sock, (struct sockaddr*)&ssocketaddr, (socklen_t*)&csalen);
+
+ if (s->sock < 0) {
+ s->connected = false;
+ strerr = "Socket: accept() failed! ";
+ strerr.append(strerror(errno));
+ return 1;
+ }
+
+ // Fill in the host data.
+ s->setAddress(inet_ntoa(ssocketaddr.sin_addr));
+ s->setPort(ssocketaddr.sin_port);
+
+ s->connected = true;
+ return 0;
+}
diff --git a/lib/tcp_socket.h b/lib/tcp_socket.h
new file mode 100644
index 0000000..5815de3
--- /dev/null
+++ b/lib/tcp_socket.h
@@ -0,0 +1,39 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * tcp_socket.h
+ *
+ * Fri Aug 18 00:12:33 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of MIaV.
+ *
+ * MIaV is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MIaV is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MIaV; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __MIAV_TCP_SOCKET_H__
+#define __MIAV_TCP_SOCKET_H__
+
+#include "socket.h"
+
+class TCPSocket : public Socket {
+public:
+ TCPSocket(int port = PORT_UNDEFINED, std::string addr = "");
+
+ int listen(TCPSocket *socket);
+};
+
+#endif/*__MIAV_TCP_SOCKET_H__*/
diff --git a/lib/udp_socket.cc b/lib/udp_socket.cc
new file mode 100644
index 0000000..9668cc6
--- /dev/null
+++ b/lib/udp_socket.cc
@@ -0,0 +1,108 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * udp_socket.cc
+ *
+ * Fri Aug 18 00:12:25 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of MIaV.
+ *
+ * MIaV is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MIaV is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MIaV; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "udp_socket.h"
+
+#include <errno.h>
+
+// For socket
+#include <sys/types.h>
+#include <sys/socket.h>
+
+// For UDP
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+// For inet_ntoa
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "info.h"
+
+UDPSocket::UDPSocket(int port, std::string addr) : Socket(port, addr)
+{
+ sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ if(sock == -1) MIaV::info->error("Could not create UDP socket: %s", strerror(errno));
+}
+
+int UDPSocket::write(void *buf, int size)
+{
+ int n;
+
+ if(!connected) {
+ memset((char *) &sa, sizeof(sa), 0);
+ sa.sin_family = AF_INET;
+ sa.sin_port = htons(prt);
+ if (inet_aton(addr.c_str(), &sa.sin_addr)==0) {
+ MIaV::info->error("An error occurred in inet_aton! %s", strerror(errno));
+ }
+ connected = true;
+ }
+
+ n = sendto(sock, buf, size, 0, (const sockaddr*)&sa, (socklen_t)sizeof(sa));
+
+ if(n==-1) {
+ MIaV::info->error("An error occurred! %s", strerror(errno));
+ }
+
+ return n;
+}
+
+int UDPSocket::read(void *buf, int size)
+{
+ int n;
+
+ if(!connected) {
+ struct sockaddr_in si_me;
+ si_me.sin_family = AF_INET;
+ si_me.sin_port = htons(prt);
+ si_me.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ if (bind(sock, (const sockaddr*)&si_me, (socklen_t)sizeof(si_me))==-1) {
+ strerr = "UDPSocet failed on bind. ";
+ strerr.append(strerror(errno));
+ return -1;
+ }
+
+ connected = true;
+ }
+
+ int slen = sizeof(sa);
+ n = recvfrom(sock, buf, size, 0, (sockaddr*)&sa, (socklen_t*)&slen);
+
+ if(n == -1) {
+ strerr = "UDPSocet failed on read. ";
+ strerr.append(strerror(errno));
+ return -1;
+ }
+
+ // Fill in the host data.
+ setAddress(inet_ntoa(sa.sin_addr));
+ setPort(sa.sin_port);
+
+ return n;
+}
diff --git a/lib/udp_socket.h b/lib/udp_socket.h
new file mode 100644
index 0000000..d89fcb1
--- /dev/null
+++ b/lib/udp_socket.h
@@ -0,0 +1,48 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * udp_socket.h
+ *
+ * Fri Aug 18 00:12:24 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of MIaV.
+ *
+ * MIaV is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MIaV is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MIaV; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __MIAV_UDP_SOCKET_H__
+#define __MIAV_UDP_SOCKET_H__
+
+#include "socket.h"
+
+// for sockaddr_in
+#include <netinet/in.h>
+
+class UDPSocket : public Socket {
+public:
+ UDPSocket(int port = PORT_UNDEFINED, std::string addr = "");
+
+ // Raw communication
+ int write(void *buf, int size);
+ int read(void *buf, int size);
+
+private:
+ struct sockaddr_in sa;
+
+};
+
+#endif/*__MIAV_UDP_SOCKET_H__*/