Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow socket element to reconnect. #448

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions elements/userlevel/fromdevice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <unistd.h>
#include <fcntl.h>
#include "fakepcap.hh"
#include <linux/sockios.h>

#if FROMDEVICE_ALLOW_LINUX
# include <sys/socket.h>
Expand Down
1 change: 1 addition & 0 deletions elements/userlevel/khandlerproxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
CLICK_DECLS

Expand Down
1 change: 1 addition & 0 deletions elements/userlevel/rawsocket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/sockios.h>

#ifndef __sun
#include <sys/ioctl.h>
Expand Down
41 changes: 38 additions & 3 deletions elements/userlevel/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <click/timer.hh>
#include <click/handlercall.hh>
#include "socket.hh"

#ifdef HAVE_PROPER
Expand All @@ -40,19 +42,32 @@
CLICK_DECLS

Socket::Socket()
: _task(this),
: _task(this), _timer(this),
_fd(-1), _active(-1), _rq(0), _wq(0),
_local_port(0), _local_pathname(""),
_timestamp(true), _sndbuf(-1), _rcvbuf(-1),
_snaplen(2048), _headroom(Packet::default_headroom), _nodelay(1),
_verbose(false), _client(false), _proper(false), _allow(0), _deny(0)
_verbose(false), _client(false), _proper(false), _allow(0), _deny(0), _reconnect_call_h(0)
{
}

Socket::~Socket()
{
}

void Socket::run_timer(Timer *) {

ErrorHandler *errh = new ErrorHandler();

if (_active == -1) {
initialize(errh);
}

_timer.reschedule_after_sec(2);
return;

}

int
Socket::configure(Vector<String> &conf, ErrorHandler *errh)
{
Expand All @@ -63,6 +78,7 @@ Socket::configure(Vector<String> &conf, ErrorHandler *errh)
return -1;
socktype = socktype.upper();

String reconnect_call;
// remove keyword arguments
Element *allow = 0, *deny = 0;
if (args.read("VERBOSE", _verbose)
Expand All @@ -74,11 +90,15 @@ Socket::configure(Vector<String> &conf, ErrorHandler *errh)
.read("NODELAY", _nodelay)
.read("CLIENT", _client)
.read("PROPER", _proper)
.read("RECONNECT_CALL", AnyArg(), reconnect_call)
.read("ALLOW", allow)
.read("DENY", deny)
.consume() < 0)
return -1;

if (reconnect_call)
_reconnect_call_h = new HandlerCall(reconnect_call);

if (allow && !(_allow = (IPRouteTable *)allow->cast("IPRouteTable")))
return errh->error("%s is not an IPRouteTable", allow->name().c_str());

Expand Down Expand Up @@ -134,12 +154,24 @@ Socket::initialize_socket_error(ErrorHandler *errh, const char *syscall)
_fd = -1;
}

return errh->error("%s: %s", syscall, strerror(e));
click_chatter("%s: %s: %s", declaration().c_str(), syscall, strerror(e));

return 0;

}

int
Socket::initialize(ErrorHandler *errh)
{

// initialize timer
_timer.initialize(this);
_timer.reschedule_after_sec(2);

// initialize callback
if (_reconnect_call_h && (_reconnect_call_h->initialize_write(this, errh) < 0))
return initialize_socket_error(errh, "callback");

// open socket, set options
_fd = socket(_family, _socktype, _protocol);
if (_fd < 0)
Expand Down Expand Up @@ -250,6 +282,9 @@ Socket::initialize(ErrorHandler *errh)
add_select(_fd, SELECT_WRITE);
}

if (_reconnect_call_h)
(void) _reconnect_call_h->call_write();

return 0;
}

Expand Down
6 changes: 6 additions & 0 deletions elements/userlevel/socket.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include <click/element.hh>
#include <click/string.hh>
#include <click/task.hh>
#include <click/timer.hh>
#include <click/notifier.hh>
#include "../ip/iproutetable.hh"
#include <sys/un.h>
#include <click/handlercall.hh>
CLICK_DECLS

/*
Expand Down Expand Up @@ -186,6 +188,7 @@ class Socket : public Element { public:

void add_handlers() CLICK_COLD;
bool run_task(Task *);
void run_timer(Timer *);
void selected(int fd, int mask);
void push(int port, Packet*);

Expand All @@ -195,6 +198,7 @@ class Socket : public Element { public:

protected:
Task _task;
Timer _timer;

private:
int _fd; // socket descriptor
Expand Down Expand Up @@ -237,6 +241,8 @@ private:

int initialize_socket_error(ErrorHandler *, const char *);

HandlerCall *_reconnect_call_h;

};

CLICK_ENDDECLS
Expand Down