Commit 4075a0bc authored by Per Cederqvist's avatar Per Cederqvist

(logout_client): Use ISC instead of the removed mux layer.

(parse_packet): Likewise.
(parse_unparsed): Likewise.
(login_request): Likewise.  Don't handle muxes.
(logout_request): Likewise.
(message_request): Likewise.
(mux_handle_packet): Removed.
(mux_logout): Removed.
(mux_parse_unparsed): Removed.
(mux_parse_message): Removed.
(milli_timevaldiff): New name for former timevaldiff.  Return the
	difference in milliseconds instead of microseconds.
(toploop): Use milli_timevaldiff instead of timevaldiff.  This is
	a thousand times better at avoiding overflow.  Use isc_puts
	instead of isc_printf.
parent 2069b6d9
/*
* $Id: connections.c,v 0.56 1999/04/17 15:15:54 ceder Exp $
* $Id: connections.c,v 0.57 1999/04/28 22:07:22 ceder Exp $
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
......@@ -36,7 +36,7 @@
#endif
static const char *
rcsid = "$Id: connections.c,v 0.56 1999/04/17 15:15:54 ceder Exp $";
rcsid = "$Id: connections.c,v 0.57 1999/04/28 22:07:22 ceder Exp $";
#include "rcs.h"
USE(rcsid);
......@@ -79,13 +79,11 @@ USE(rcsid);
#include "log.h"
#include "lyskomd.h"
#include "services.h"
#include "mux.h"
#include "isc-parse.h"
#include "prot-a.h"
#include "prot-a-parse.h"
#include "server/smalloc.h"
#include "end-of-atomic.h"
#include "mux-parse.h"
#include "send-async.h"
#include "cache.h"
#include "rfc931.h"
......@@ -130,6 +128,7 @@ static void
logout_client(Connection *cp)
{
Connection *real_active_connection;
int ret;
if ( active_connection != NULL )
{
......@@ -175,7 +174,10 @@ logout_client(Connection *cp)
restart_kom("logout_client(): Bad protocol.\n");
}
mux_close(cp); /* Close fd's. Clear mux and isc structures. */
ret = isc_destroy(kom_server_mcb, cp->isc_session);
if (ret < 0)
log("logout_client(): isc_destroyed returned %d\n", ret);
cp->isc_session = NULL;
kill_client(cp); /* Free the Connection */
}
......@@ -258,9 +260,8 @@ parse_packet(Connection *client)
default:
client->protocol = '\0';
mux_printf(client,
"%%%%LysKOM unsupported protocol.\n");
mux_flush(client);
isc_puts("%%LysKOM unsupported protocol.\n", client->isc_session);
isc_flush(client->isc_session);
BUG(("%%%%Unsupported protocol.\n"));
longjmp(parse_env, ISC_LOGOUT);
}
......@@ -356,8 +357,8 @@ parse_unparsed(Connection *client)
case ISC_PROTOCOL_ERR:
s_clear(&client->string0);
free_parsed(client);
mux_printf(client, "%% LysKOM protocol error.\n");
mux_flush(client);
isc_puts("%% LysKOM protocol error.\n", client->isc_session);
isc_flush(client->isc_session);
BUG(("%%%% Protocol error.\n"));
s_clear(&client->unparsed);
client->first_to_parse = 0;
......@@ -408,190 +409,9 @@ parse_message(Connection *cp, String tmp_str)
return;
}
static void
mux_handle_packet(Mux *mux)
{
Mux_client *mcp = NULL;
Connection *cp = NULL;
switch(mux->parse.function)
{
case 0: /* ping */
isc_printf(mux->scb, "0\n");
isc_flush(mux->scb);
break;
case 1: /* login */
mcp = mux_getclientbyid(mux, mux->parse.num);
if (mcp)
{ /* ERROR %d: Already logged in\n */
isc_printf(mux->scb, "4\n");
isc_flush(mux->scb);
return;
}
cp = new_client();
cp->mux = mux;
cp->hostname = mux->parse.string;
mux->parse.string = EMPTY_STRING;
mux_addclient(mux, mux->parse.num, cp);
BUG(("\n[Client %lu from %.*s via MUX %s",
cp->session_no,
(int)cp->hostname.len,
cp->hostname.string,
isc_gethostname(mux->scb->info.tcp.raddr, NULL, 0)));
BUG((" is connecting]\n"));
break;
case 2: /* logout */
mcp = mux_getclientbyid(mux, mux->parse.num);
if (!mcp)
{ /* ERROR %d: No such mux-session */
isc_printf(mux->scb, "4\n");
isc_flush(mux->scb);
return;
}
cp = mcp->conn;
BUG(("\n[Client %lu via MUX(%s)",
cp->session_no,
isc_gethostname(mux->scb->info.tcp.raddr, NULL, 0)));
BUG((" is logging out by request]\n"));
add_to_kill_list(cp);
break;
case 3: /* msg */
mcp = mux_getclientbyid(mux, mux->parse.num);
if (!mcp)
{ /* ERROR %d: No such mux-session */
isc_printf(mux->scb, "4\n");
isc_flush(mux->scb);
return;
}
cp = mcp->conn;
VBUG(("\n[Message client %lu via MUX #%lu]\n",
cp->session_no, (unsigned long)mux->scb));
parse_message(cp, mux->parse.string);
break;
default:
VBUG(("\nMUX Protocol error from MUX #%lu]\n",
(unsigned long)mux->scb));
isc_printf(mux->scb, "5\n");
isc_flush(mux->scb);
break;
}
}
static void
mux_logout(Mux *mp)
{
int i;
for (i = 0; i < mp->client_c; i++)
if (mp->client_v[i].conn)
{
BUG(("\n[Client %lu via MUX(%s)",
mp->client_v[i].conn->session_no,
isc_gethostname(mp->scb->info.tcp.raddr, NULL, 0)));
BUG((" is logging out by MUX shutdown]\n"));
logout_client(mp->client_v[i].conn);
end_of_atomic(FALSE);
}
isc_destroy(kom_server_mcb, mp->scb);
mux_destruct(mp);
}
static void
mux_parse_unparsed(Mux *mux)
{
String tmp_str;
while ( mux->parse.more_to_parse )
{
switch ( setjmp(mux_parse_env) )
{
case 0 :
/* Parse message. Take apropriate action if message is complete. */
mux_parse_packet(mux);
mux_handle_packet(mux);
mux_free_parsed(mux);
break;
case MUX_PROTOCOL_ERR:
s_clear(&mux->parse.string);
mux_free_parsed(mux);
isc_printf(mux->scb, "5\n");
isc_flush(mux->scb);
BUG(("%%%% Mux protocol error.\n"));
s_clear(&mux->parse.unparsed);
mux->parse.first_to_parse = 0;
mux->parse.more_to_parse = FALSE;
break;
case MUX_MSG_INCOMPLETE:
mux->parse.more_to_parse = FALSE;
break;
case MUX_LOGOUT:
mux_logout(mux);
return;
}
/* Delete the parsed part of 'unparsed' */
tmp_str = EMPTY_STRING;
if ( s_substr(&tmp_str, mux->parse.unparsed,
mux->parse.first_to_parse, END_OF_STRING) != OK )
restart_kom("mux_parse_unparsed: s_substr\n");
s_clear(&mux->parse.unparsed);
mux->parse.unparsed = tmp_str;
mux->parse.first_to_parse = 0;
}
}
static void
mux_parse_message(Mux *mp, String tmp_str)
{
VBUGSTR(tmp_str);
BUG(("%s(): mp->parse.unparsed = { len = %lu, string = %p }\n",
"mux_parse_message",
mp->parse.unparsed.len,
(void *)mp->parse.unparsed.string));
if ( s_strcat(&mp->parse.unparsed, tmp_str) != OK )
restart_kom("mux_parse_message(): s_strcat\n");
tmp_str = EMPTY_STRING;
/* Parse this packet, but leave the last token if it isn't complete. */
mux_parse_unparsed(mp);
return;
}
/*
* parse data in client->unparsed and mux->unparsed if there
* parse data in client->unparsed if there
* is data that has not yet been taken care of. Return TRUE
* if there was no data to take care of.
*/
......@@ -754,94 +574,53 @@ login_request(IscEvent *event)
{
Connection * cp;
const char *realuser;
int localport;
IscAddress *isc_adr;
char *hostname = NULL;
/* Supress logins if /etc/nologin exists */
if (fexists("/etc/nologin"))
{
isc_printf(event->session, "%s", "%% No logins allowed.\n");
isc_puts("%% No logins allowed.\n", event->session);
isc_flush(event->session);
isc_destroy(kom_server_mcb, event->session);
return;
}
/* Find out if it is a mux or a client that tries to connect to
us. Do it by getting the local port number. */
hostname = isc_gethostname(event->session->info.tcp.raddr, NULL, 0);
if (hostname == NULL)
hostname = isc_getipnum(event->session->info.tcp.raddr, NULL, 0);
if (hostname == NULL)
log("WNG: login_request(): unknown hostid.\n");
isc_adr = isc_getladdress (event->session);
if (isc_adr == NULL)
restart_kom("login_request(): can't isc_getladdress (%lu)\n",
(unsigned long)event->session);
localport = isc_getportnum (isc_adr);
isc_freeaddress (isc_adr);
if (localport == param.num_ip_mux_port)
/* Get the real user name, as returned by the Ident protocol (rfc 931). */
realuser = get_real_username(event->session, hostname);
if (realuser == NULL && param.authentication_level == 2)
{
/* Multiplexer requesting connection */
log ("Connection from %s rejected - no IDENT available.\n",
hostname);
/*
** Create, and setup the MUX for the MUX case
*/
event->session->udg = mux_create(MUX_TYPE_MUX, event->session);
BUG(("MUX #%lu at %s connecting.\n",
(unsigned long)event->session,
isc_gethostname(event->session->info.tcp.raddr, NULL, 0)));
isc_puts("%% No IDENT server reachable at your site.\n",
event->session);
isc_flush(event->session);
isc_destroy(kom_server_mcb, event->session);
return;
}
else if (localport == param.num_ip_client_port)
{
/* Client requesting connection */
hostname = isc_gethostname(event->session->info.tcp.raddr, NULL, 0);
if (hostname == NULL)
hostname = isc_getipnum(event->session->info.tcp.raddr, NULL, 0);
if (hostname == NULL)
log("WNG: login_request(): unknown hostid.\n");
/* Get the real user name, as returned by the Ident protocol
(rfc 931). */
realuser = get_real_username(event->session, hostname);
if (realuser == NULL && param.authentication_level == 2)
{
log ("Connection from %s rejected - no IDENT available.\n",
hostname);
isc_printf(event->session, "%s",
"%% No IDENT server reachable at your site.\n");
isc_flush(event->session);
isc_destroy(kom_server_mcb, event->session);
return;
}
/* Create a Connection, and link the Connection and the
isc_session together. */
/*
** Create, and setup the MUX for the CLIENT case
*/
event->session->udg = mux_create(MUX_TYPE_CLIENT,
event->session);
cp = new_client();
cp->mux = event->session->udg;
if (hostname == NULL)
s_crea_str(&cp->hostname, "unknown");
else
s_crea_str(&cp->hostname, hostname);
if (realuser != NULL)
s_crea_str(&cp->ident_user, realuser);
cp = new_client();
cp->isc_session = event->session;
event->session->udg = cp;
mux_addclient(event->session->udg, 0, cp);
BUG(("\n[Client %lu from %s is connecting]\n", cp->session_no,
hostname));
}
if (hostname == NULL)
s_crea_str(&cp->hostname, "unknown");
else
{
/* Help! It was neither a client, nor a mux. */
restart_kom("login_request(): Neither client, nor mux.\n");
}
s_crea_str(&cp->hostname, hostname);
if (realuser != NULL)
s_crea_str(&cp->ident_user, realuser);
BUG(("\n[Client %lu from %s is connecting]\n", cp->session_no, hostname));
}
static void
......@@ -849,27 +628,13 @@ logout_request(IscEvent *event)
{
Connection * cp;
if (event->session->udg->type == MUX_TYPE_MUX)
{
BUG(("\n[MUX #%lu at %s", (unsigned long)event->session,
isc_gethostname(event->session->info.tcp.raddr, NULL, 0)));
BUG((" is logging out]\n"));
/*
** Logout any remaining clients
*/
mux_logout(event->session->udg);
}
else
{
cp = event->session->udg->client_v[0].conn;
BUG(("\n[Client %lu from %s", cp->session_no,
isc_gethostname(event->session->info.tcp.raddr, NULL, 0)));
BUG((" is logging out]\n"));
cp = event->session->udg;
add_to_kill_list(cp);
}
BUG(("\n[Client %lu from %s", cp->session_no,
isc_gethostname(event->session->info.tcp.raddr, NULL, 0)));
BUG((" is logging out]\n"));
add_to_kill_list(cp);
}
......@@ -879,38 +644,26 @@ message_request(IscEvent *event)
Connection *cp;
String tmp_str;
if (event->session->udg->type == MUX_TYPE_MUX)
{
event->session->udg->parse.more_to_parse = TRUE;
tmp_str.string = (unsigned char *)event->msg->buffer;
tmp_str.len = event->msg->length;
mux_parse_message(event->session->udg, tmp_str);
}
else
{
cp = event->session->udg->client_v[0].conn;
VBUG(("\n[Message from client %lu]\n",
cp->session_no));
/*
** Pass the message on to the parser
*/
tmp_str.string = (unsigned char *)event->msg->buffer;
tmp_str.len = event->msg->length;
parse_message(cp, tmp_str);
}
cp = event->session->udg;
VBUG(("\n[Message from client %lu]\n", cp->session_no));
/* Pass the message on to the parser */
tmp_str.string = (unsigned char *)event->msg->buffer;
tmp_str.len = event->msg->length;
parse_message(cp, tmp_str);
}
static long
timevaldiff(struct timeval a,
struct timeval b)
milli_timevaldiff(struct timeval a,
struct timeval b)
{
return 1000000 * (a.tv_sec - b.tv_sec) + a.tv_usec - b.tv_usec;
return 1000 * (a.tv_sec - b.tv_sec) + (a.tv_usec - b.tv_usec) / 1000;
}
void
toploop(void)
{
IscEvent * event;
IscEvent * event;
Bool pending_input = TRUE; /* Should be TRUE whenever there
is or might be pending input
from any client in the unparsed
......@@ -933,7 +686,7 @@ toploop(void)
pending_input ? 0 : timeout );
gettimeofday(&after, NULL);
timeout -= timevaldiff(after, before) / 1000;
timeout -= milli_timevaldiff(after, before);
if ( timeout < 0 )
timeout = 0;
......@@ -978,7 +731,7 @@ toploop(void)
case ISC_EVENT_LOGIN_UNRELOCATED:
pending_input = TRUE;
BUG(("Connection attempt rejected.\n"));
isc_printf(event->session, "%s", "%% No connections left.\n");
isc_puts("%% No connections left.\n", event->session);
isc_flush(event->session);
isc_destroy(kom_server_mcb, event->session);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment