mux.c 8.28 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: mux.c,v 0.20 1998/07/08 16:36:30 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM 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 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25 26 27
/*
** mux.c
*/
Per Cederqvist's avatar
Per Cederqvist committed
28

29 30
static const char *
rcsid = "$Id: mux.c,v 0.20 1998/07/08 16:36:30 ceder Exp $";
31 32
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
33

Per Cederqvist's avatar
Per Cederqvist committed
34 35 36
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
37 38 39
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
40
#include <setjmp.h>
41 42 43
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
44 45

#include "isc-interface.h"
Per Cederqvist's avatar
Per Cederqvist committed
46
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
47 48
#include "misc-types.h"
#include "kom-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
49
#include "com.h"
50
#include "async.h"
Per Cederqvist's avatar
Per Cederqvist committed
51 52
#include "connections.h"
#include "mux.h"
Per Cederqvist's avatar
Per Cederqvist committed
53 54
#include "server/smalloc.h"
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
55

Per Cederqvist's avatar
Per Cederqvist committed
56
#ifdef HAVE_STDARG_H
57
extern int UCB_printf( int (*sputc)(int chr), const char *fmt, va_list AP);
Per Cederqvist's avatar
Per Cederqvist committed
58
#endif
Per Cederqvist's avatar
Per Cederqvist committed
59 60 61 62 63 64

/*
** MUX support functions
*/

Mux *
Per Cederqvist's avatar
Per Cederqvist committed
65
mux_create(Mux_type type, IscSession *scb)
Per Cederqvist's avatar
Per Cederqvist committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
{
  Mux *mp;


  mp = (Mux *) smalloc(sizeof(Mux));

  mp->magic    = MUX_MAGIC_ALLOC;
  mp->type     = type;
  mp->scb      = scb;
  mp->client_c = 0;
  mp->client_v = NULL;
  
  mp->parse.parse_pos = 0;
  mp->parse.parse_pos_2 = 0;
  mp->parse.string_parse_pos = 0;
  mp->parse.first_to_parse = 0;
  mp->parse.more_to_parse = TRUE;
  mp->parse.function = 0;
  mp->parse.string = EMPTY_STRING;
  mp->parse.unparsed = EMPTY_STRING;
  
  return mp;
}

void
mux_free_parsed(Mux *mp)
{
    s_clear(&mp->parse.string);
    mp->parse.parse_pos = 0;
    mp->parse.parse_pos_2 = 0;
    mp->parse.string_parse_pos = 0;
} 

void
mux_destruct(Mux *mp)
{
  switch (mp->magic)
  {
    case MUX_MAGIC_ALLOC:
      break;

    case MUX_MAGIC_FREE:
      restart_kom("MUX_DESTRUCT: Trying to destruct freed Mux block\n");

    default:
      restart_kom("MUX_DESTRUCT: Bad magic number\n");
  }

  mp->magic = MUX_MAGIC_FREE;

  mux_free_parsed(mp);
  s_clear(&mp->parse.unparsed);
118

Per Cederqvist's avatar
Per Cederqvist committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
  sfree(mp->client_v);
  sfree(mp);
}



int
mux_clients(Mux *mp)
{
  int i;
  int c;


  switch (mp->magic)
  {
    case MUX_MAGIC_ALLOC:
      break;

    case MUX_MAGIC_FREE:
      restart_kom("MUX_CLIENTS: Trying to work on freed Mux block\n");

    default:
      restart_kom("MUX_CLIENTS: Bad magic number\n");
  }

  for (i = 0, c = 0; i < mp->client_c; i++)
    if (mp->client_v[i].id != -1)
      c++;

  return c;
}


void
mux_addclient(Mux *mp, int id, Connection *cp)
{
  int i;


  switch (mp->magic)
  {
    case MUX_MAGIC_ALLOC:
      break;

    case MUX_MAGIC_FREE:
      restart_kom("MUX_ADDCLIENT: Trying to work on freed Mux block\n");

    default:
      restart_kom("MUX_ADDCLIENT: Bad magic number\n");
  }

  for (i = 0; i < mp->client_c; i++)
    if (mp->client_v[i].id == -1)
      break;

  if (i == mp->client_c)
  {
    mp->client_c++;
    if (mp->client_v == NULL)
      mp->client_v = (Mux_client *) smalloc(sizeof(Mux_client));
    else
      mp->client_v = (Mux_client *) srealloc(mp->client_v,
					      sizeof(Mux_client) *
					      (mp->client_c));
  }
  
  mp->client_v[i].conn   = cp;
  mp->client_v[i].id     = id;
  mp->client_v[i].outmsg = isc_allocmsg(MUX_OUTMSGSIZE);
}



void
mux_delclient(Mux *mp, int id)
{
  int i;


  switch (mp->magic)
  {
    case MUX_MAGIC_ALLOC:
      break;

    case MUX_MAGIC_FREE:
      restart_kom("MUX_DELCLIENT: Trying to work on freed Mux block\n");

    default:
      restart_kom("MUX_DELCLIENT: Bad magic number\n");
  }

  for (i = 0; i < mp->client_c; i++)
    if (mp->client_v[i].id == id)
    {
      mp->client_v[i].id   = -1;
      mp->client_v[i].conn = NULL;
      isc_freemsg(mp->client_v[i].outmsg);
      return;
    }
}


Mux_client *
mux_getclientbyid(Mux *mp, int id)
{
  int i;
  

  switch (mp->magic)
  {
    case MUX_MAGIC_ALLOC:
      break;

    case MUX_MAGIC_FREE:
      restart_kom("MUX_GETCLIENTBYID: Trying to work on freed Mux block\n");

    default:
      restart_kom("MUX_GETCLIENTBYID: Bad magic number\n");
  }


  for (i = 0; i < mp->client_c; i++)
    if (mp->client_v[i].id == id)
      return &mp->client_v[i];

  return NULL;
}



Mux_client *
mux_getclientbyconn(Mux *mp, Connection *cp)
{
  int i;


  switch (mp->magic)
  {
    case MUX_MAGIC_ALLOC:
      break;

    case MUX_MAGIC_FREE:
      restart_kom("MUX_GETCLIENTBYCONN: Trying to work on freed Mux block\n");

    default:
      restart_kom("MUX_GETCLIENTBYCONN: Bad magic number\n");
  }

  for (i = 0; i < mp->client_c; i++)
    if (mp->client_v[i].conn == cp)
      return &mp->client_v[i];

  return NULL;
}


/*
** MUX output functions
*/


int
Per Cederqvist's avatar
Per Cederqvist committed
281 282 283
mux_write(Connection  		* cp,
	  const unsigned char   * buf,
	  size_t        	  size)
Per Cederqvist's avatar
Per Cederqvist committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
{
  Mux         * mp = cp->mux;
  Mux_client  * mcp;
  size_t        osize;
  size_t        wlen;
  size_t        clen;
  

  mcp = mux_getclientbyconn(mp, cp);
  if (mcp == NULL)
    /* BETTER ERROR HANDLING SHOULD GO HERE */
    restart_kom("mux_write: STALE CLIENT ID\n");

  osize = size;
  while (size > 0)
  {
    while ((wlen = mcp->outmsg->size - mcp->outmsg->length) == 0)
      mux_flush(cp);
    
    clen = size > wlen ? wlen : size;
    memcpy(mcp->outmsg->buffer + mcp->outmsg->length, buf, clen);
    mcp->outmsg->length += clen;
    size -= clen;
    buf  += clen;
  }

  return osize;
}




int
mux_putc(int           chr,
	 Connection  * cp)
{
  Mux         * mp = cp->mux;
  Mux_client  * mcp;

  
  mcp = mux_getclientbyconn(mp, cp);
  if (mcp == NULL)
    /* BETTER ERROR HANDLING SHOULD GO HERE */
    restart_kom("mux_putc: STALE CLIENT ID\n");

  while (mcp->outmsg->length == mcp->outmsg->size)
    mux_flush(cp);

  mcp->outmsg->buffer[mcp->outmsg->length++] = chr;

  return chr;
}



static Connection *send_cp;

static int
send_putc(int chr)
{
  return mux_putc(chr, send_cp);
}


Per Cederqvist's avatar
Per Cederqvist committed
348
#ifdef HAVE_STDARG_H
Per Cederqvist's avatar
Per Cederqvist committed
349 350 351 352 353 354 355 356 357 358 359 360
int
mux_printf(Connection  * cp,
	   const char  * format,
	   ...)
{
  va_list       AP;
  int           retval = -1;

  
  va_start(AP, format);

  send_cp = cp;
361
  retval = UCB_printf(&send_putc, format, AP);
Per Cederqvist's avatar
Per Cederqvist committed
362 363 364 365 366

  va_end(AP);

  return retval;
}
Per Cederqvist's avatar
Per Cederqvist committed
367
#endif
Per Cederqvist's avatar
Per Cederqvist committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

void
mux_flush(Connection  * cp)
{
  Mux         * mp = cp->mux;
  Mux_client  * mcp;


  mcp = mux_getclientbyconn(mp, cp);
  if (mcp == NULL)
    /* BETTER ERROR HANDLING SHOULD GO HERE */
    restart_kom("mux_flush: STALE CLIENT ID\n");

  if (mcp->outmsg->length == 0)
    return;
  
  switch (mp->type)
  {
    case MUX_TYPE_CLIENT:
      isc_send(mp->scb, mcp->outmsg);
      isc_flush(mp->scb);
      mcp->outmsg = isc_allocmsg(MUX_OUTMSGSIZE);
      break;

    case MUX_TYPE_MUX:
      isc_printf(mp->scb, "3 %d %dH",
		 mcp->id, mcp->outmsg->length);
      isc_write(mp->scb, mcp->outmsg->buffer, mcp->outmsg->length);
      isc_printf(mp->scb, "\n");
      isc_flush(mp->scb);
      mcp->outmsg->length = 0;
      break;
      
    default:
       restart_kom("mux_flush: ILLEGAL MUX TYPE: %d\n", mp->type);
  }
}


void
mux_close(Connection  * cp)
{
  Mux         * mp = cp->mux;
  Mux_client  * mcp;
  

  mcp = mux_getclientbyconn(mp, cp);
  if (mcp == NULL)
    /* BETTER ERROR HANDLING SHOULD GO HERE */
    restart_kom("mux_close: STALE CLIENT ID\n");
  
  mux_flush(cp);
  
  switch (mp->type)
  {
    case MUX_TYPE_CLIENT:
Per Cederqvist's avatar
Per Cederqvist committed
424
      isc_destroy(kom_server_mcb, mp->scb);
425 426 427 428 429 430

      if (mp->client_c != 1)
	  restart_kom("ERROR: mux_close(): client_c == %d\n", mp->client_c);
  
      isc_freemsg(mp->client_v[0].outmsg);

Per Cederqvist's avatar
Per Cederqvist committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
      mux_destruct(mp);
      cp->mux = NULL;
      break;

    case MUX_TYPE_MUX:
      isc_printf(mp->scb, "2 %d\n", mcp->id);
      isc_flush(mp->scb);

      mux_delclient(mp, mcp->id);
      cp->mux = NULL;
      break;
      
    default:
       restart_kom("mux_close: ILLEGAL MUX TYPE: %d\n", mp->type);
  }
}