Example #1
0
File: Socket.cpp Project: crnt/x0
Socket::Socket(struct ev_loop* loop, int fd, int af) :
	loop_(loop),
	watcher_(loop),
	timer_(loop),
	startedAt_(ev_now(loop)),
	lastActivityAt_(ev_now(loop)),
	fd_(fd),
	addressFamily_(af),
	secure_(false),
	state_(Operational),
	mode_(None),
	tcpCork_(false),
	remoteIP_(),
	remotePort_(0),
	localIP_(),
	localPort_(),
	callback_(nullptr),
	callbackData_(0)
{
#ifndef NDEBUG
	setLogging(false);
	static std::atomic<unsigned long long> id(0);
	setLoggingPrefix("Socket(%d, %s:%d)", ++id, remoteIP().c_str(), remotePort());
#endif
	TRACE("created. fd:%d, local(%s:%d)", fd_, localIP().c_str(), localPort());

	watcher_.set<Socket, &Socket::io>(this);
	timer_.set<Socket, &Socket::timeout>(this);
}
Example #2
0
std::string Socket::remote() const
{
	char buf[512];
	size_t n;
	switch (addressFamily_) {
		case AF_INET:
			n = snprintf(buf, sizeof(buf), "%s:%d", remoteIP().c_str(), remotePort());
			break;
		case AF_INET6:
			n = snprintf(buf, sizeof(buf), "[%s]:%d", remoteIP().c_str(), remotePort());
			break;
		default:
			n = snprintf(buf, sizeof(buf), "%s", remoteIP().c_str());
			break;
	}
	return std::string(buf, n);
}
Example #3
0
/** start first async operation for this HttpConnection.
 *
 * This is done by simply registering the underlying socket to the the I/O service
 * to watch for available input.
 *
 * \note This method must be invoked right after the object construction.
 *
 * \see stop()
 */
void HttpConnection::start(ServerSocket* listener, Socket* client, const HttpWorker::ConnectionHandle& handle)
{
	handle_ = handle;
	listener_ = listener;

	socket_ = client;
	socket_->setReadyCallback<HttpConnection, &HttpConnection::io>(this);

	sink_.setSocket(socket_);

#if defined(TCP_NODELAY)
	if (worker_->server().tcpNoDelay())
		socket_->setTcpNoDelay(true);
#endif

#if !defined(NDEBUG)
	setLoggingPrefix("HttpConnection[%d,%llu|%s:%d]", worker_->id(), id_, remoteIP().c_str(), remotePort());
#endif

	TRACE("starting (fd=%d)", socket_->handle());

	ref(); // <-- this reference is being decremented in close()

	worker_->server_.onConnectionOpen(this);

	if (isAborted()) {
		// The connection got directly closed (aborted) upon connection instance creation (e.g. within the onConnectionOpen-callback),
		// so delete the object right away.
		close();
		return;
	}

	request_ = new HttpRequest(*this);

	ref();
	if (socket_->state() == Socket::Handshake) {
		TRACE("start: handshake.");
		socket_->handshake<HttpConnection, &HttpConnection::handshakeComplete>(this);
	} else {
#if defined(TCP_DEFER_ACCEPT) && defined(WITH_TCP_DEFER_ACCEPT)
		TRACE("start: processing input");

		// it is ensured, that we have data pending, so directly start reading
		if (readSome())
			process();
		else
			close();

		TRACE("start: processing input done");
#else
		TRACE("start: watchInput.");
		// client connected, but we do not yet know if we have data pending
		watchInput(worker_->server_.maxReadIdle());
#endif
	}
	unref();
}
Example #4
0
void HttpConnection::log(Severity s, const char *fmt, ...)
{
	va_list va;
	va_start(va, fmt);
	char buf[512];
	vsnprintf(buf, sizeof(buf), fmt, va);
	va_end(va);

	worker().server().log(s, "connection[%s]: %s", !isClosed() ? remoteIP().c_str() : "(null)", buf);
}
Example #5
0
int32 VisionApp::Identity(void*)
{
	int32 identSock(0), accepted(0);
	BString ident;
	char received[64];

	struct sockaddr_in localAddr;
	localAddr.sin_family = AF_INET;
	localAddr.sin_port = htons(113);
	localAddr.sin_addr.s_addr = INADDR_ANY;

	if ((identSock = socket(AF_INET, SOCK_STREAM, 0)) >= 0 &&
		bind(identSock, (struct sockaddr*)&localAddr, sizeof(localAddr)) == 0) {
		vision_app->fIdentSocket = identSock;

		struct linger lng = {0, 0};
		setsockopt(identSock, SOL_SOCKET, SO_LINGER, &lng, sizeof(linger));
		listen(identSock, 1);

		while (!vision_app->fShuttingDown) {
			struct fd_set rset, eset;
			struct sockaddr_in remoteSock;
			int size(sizeof(sockaddr_in));
			struct timeval tv = {10, 0};
			FD_ZERO(&rset);
			FD_ZERO(&eset);
			FD_SET(identSock, &rset);
			FD_SET(identSock, &eset);

			if (select(identSock + 1, &rset, 0, &eset, NULL) < 0 || FD_ISSET(identSock, &eset))
				break;
			else if (FD_ISSET(identSock, &rset)) {
				accepted = accept(identSock, (struct sockaddr*)&remoteSock, (socklen_t*)&size);
				if (accepted >= 0) {
					FD_ZERO(&rset);
					FD_ZERO(&eset);

					BString remoteIP(inet_ntoa(remoteSock.sin_addr));
					ident = vision_app->GetIdent(remoteIP.String());

					if (ident.Length() > 0) {
						memset(received, 0, 64);
						FD_SET(accepted, &rset);
						FD_SET(accepted, &eset);
						if (select(accepted + 1, &rset, 0, &eset, &tv) > 0 &&
							FD_ISSET(accepted, &rset) && !FD_ISSET(accepted, &eset)) {

							recv(accepted, received, 64, 0);
							int32 len(0);

							received[63] = 0;
							while ((len = strlen(received)) && isspace(received[len - 1]))
								received[len - 1] = 0;

							BString string;

							string.Append(received);
							string.Append(" : USERID : BeOS : ");
							string.Append(ident);
							string.Append("\r\n");

							send(accepted, string.String(), string.Length(), 0);
						}
					} else {
						BString string("0 , 0 : UNKNOWN : UNKNOWN-ERROR");
						send(accepted, string.String(), string.Length(), 0);
					}
					close(accepted);
				}
			}
		}
	}

	close(identSock);
	return 0;
}
Example #6
0
void
main(int argc, char **argv)
{
	int afd, dfd, lcfd, forceSTA = 0;
	char aserve[128], net[128], adir[40], ldir[40];
	char *remote, *serve = "tcp!*!5356", *S = "secstore";
	Ndb *db2;

	setnetmtpt(net, sizeof(net), nil);
	ARGBEGIN{
	case 'R':
		forceSTA = 1;
		break;
	case 's':
		serve = EARGF(usage());
		break;
	case 'S':
		S = EARGF(usage());
		break;
	case 'x':
		setnetmtpt(net, sizeof(net), EARGF(usage()));
		break;
	case 'v':
		verbose++;
		break;
	default:
		usage();
	}ARGEND;

	if(!verbose)
		switch(rfork(RFNOTEG|RFPROC|RFFDG)) {
		case -1:
			sysfatal("fork: %r");
		case 0:
			break;
		default:
			exits(0);
		}

	snprint(aserve, sizeof aserve, "%s/%s", net, serve);
	afd = announce(aserve, adir);
	if(afd < 0)
		sysfatal("%s: %r", aserve);
	syslog(0, LOG, "ANNOUNCE %s", aserve);
	for(;;){
		if((lcfd = listen(adir, ldir)) < 0)
			exits("can't listen");
		switch(fork()){
		case -1:
			fprint(2, "secstore forking: %r\n");
			close(lcfd);
			break;
		case 0:
			/*
			 * "/lib/ndb/common.radius does not exist"
			 * if db set before fork.
			 */
			db = ndbopen("/lib/ndb/auth");
			if(db == 0)
				syslog(0, LOG, "no /lib/ndb/auth");
			db2 = ndbopen(0);
			if(db2 == 0)
				syslog(0, LOG, "no /lib/ndb/local");
			db = ndbcat(db, db2);
			if((dfd = accept(lcfd, ldir)) < 0)
				exits("can't accept");
			alarm(30*60*1000);		/* 30 min */
			remote = remoteIP(ldir);
			syslog(0, LOG, "secstore from %s", remote);
			free(remote);
			dologin(dfd, S, forceSTA);
			exits(nil);
		default:
			close(lcfd);
			break;
		}
	}
}
Example #7
0
void HttpConnection::log(LogMessage&& msg)
{
	msg.addTag(!isClosed() ? remoteIP().c_str() : "(null)");

	worker().log(std::forward<LogMessage>(msg));
}