1 /* 2 * Copyright (c) 1988, 1992 The University of Utah and the Center 3 * for Software Science (CSS). 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * the Center for Software Science of the University of Utah Computer 9 * Science Department. CSS requests users of this software to return 10 * to css-dist@cs.utah.edu any improvements that they make and grant 11 * CSS redistribution rights. 12 * 13 * %sccs.include.redist.c% 14 * 15 * @(#)utils.c 8.1 (Berkeley) 06/04/93 16 * 17 * Utah $Hdr: utils.c 3.1 92/07/06$ 18 * Author: Jeff Forys, University of Utah CSS 19 */ 20 21 #ifndef lint 22 static char sccsid[] = "@(#)utils.c 8.1 (Berkeley) 06/04/93"; 23 #endif /* not lint */ 24 25 #include <sys/param.h> 26 27 #include <fcntl.h> 28 #include <signal.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <syslog.h> 33 #include <time.h> 34 #include <unistd.h> 35 #include "defs.h" 36 37 /* 38 ** DispPkt -- Display the contents of an RMPCONN packet. 39 ** 40 ** Parameters: 41 ** rconn - packet to be displayed. 42 ** direct - direction packet is going (DIR_*). 43 ** 44 ** Returns: 45 ** Nothing. 46 ** 47 ** Side Effects: 48 ** None. 49 */ 50 void 51 DispPkt(rconn, direct) 52 RMPCONN *rconn; 53 int direct; 54 { 55 static char BootFmt[] = "\t\tRetCode:%u SeqNo:%lx SessID:%x Vers:%u"; 56 static char ReadFmt[] = "\t\tRetCode:%u Offset:%lx SessID:%x\n"; 57 58 struct tm *tmp; 59 register struct rmp_packet *rmp; 60 int i, omask; 61 u_int t; 62 63 /* 64 * Since we will be working with RmpConns as well as DbgFp, we 65 * must block signals that can affect either. 66 */ 67 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGUSR1)|sigmask(SIGUSR2)); 68 69 if (DbgFp == NULL) { /* sanity */ 70 (void) sigsetmask(omask); 71 return; 72 } 73 74 /* display direction packet is going using '>>>' or '<<<' */ 75 fputs((direct==DIR_RCVD)?"<<< ":(direct==DIR_SENT)?">>> ":"", DbgFp); 76 77 /* display packet timestamp */ 78 tmp = localtime((time_t *)&rconn->tstamp.tv_sec); 79 fprintf(DbgFp, "%02d:%02d:%02d.%06ld ", tmp->tm_hour, tmp->tm_min, 80 tmp->tm_sec, rconn->tstamp.tv_usec); 81 82 /* display src or dst addr and information about network interface */ 83 fprintf(DbgFp, "Addr: %s Intf: %s\n", EnetStr(rconn), IntfName); 84 85 rmp = &rconn->rmp; 86 87 /* display IEEE 802.2 Logical Link Control header */ 88 (void) fprintf(DbgFp, "\t802.2 LLC: DSAP:%x SSAP:%x CTRL:%x\n", 89 rmp->hp_llc.dsap, rmp->hp_llc.ssap, rmp->hp_llc.cntrl); 90 91 /* display HP extensions to 802.2 Logical Link Control header */ 92 (void) fprintf(DbgFp, "\tHP Ext: DXSAP:%x SXSAP:%x\n", 93 rmp->hp_llc.dxsap, rmp->hp_llc.sxsap); 94 95 /* 96 * Display information about RMP packet using type field to 97 * determine what kind of packet this is. 98 */ 99 switch(rmp->r_type) { 100 case RMP_BOOT_REQ: /* boot request */ 101 (void) fprintf(DbgFp, "\tBoot Request:"); 102 GETWORD(rmp->r_brq.rmp_seqno, t); 103 if (rmp->r_brq.rmp_session == RMP_PROBESID) { 104 if (WORDZE(rmp->r_brq.rmp_seqno)) 105 fputs(" (Send Server ID)", DbgFp); 106 else 107 fprintf(DbgFp," (Send Filename #%u)",t); 108 } 109 (void) fputc('\n', DbgFp); 110 (void) fprintf(DbgFp, BootFmt, rmp->r_brq.rmp_retcode, 111 t, rmp->r_brq.rmp_session, 112 rmp->r_brq.rmp_version); 113 (void) fprintf(DbgFp, "\n\t\tMachine Type: "); 114 for (i = 0; i < RMP_MACHLEN; i++) 115 (void) fputc(rmp->r_brq.rmp_machtype[i], DbgFp); 116 DspFlnm(rmp->r_brq.rmp_flnmsize, &rmp->r_brq.rmp_flnm); 117 break; 118 case RMP_BOOT_REPL: /* boot reply */ 119 fprintf(DbgFp, "\tBoot Reply:\n"); 120 GETWORD(rmp->r_brpl.rmp_seqno, t); 121 (void) fprintf(DbgFp, BootFmt, rmp->r_brpl.rmp_retcode, 122 t, rmp->r_brpl.rmp_session, 123 rmp->r_brpl.rmp_version); 124 DspFlnm(rmp->r_brpl.rmp_flnmsize,&rmp->r_brpl.rmp_flnm); 125 break; 126 case RMP_READ_REQ: /* read request */ 127 (void) fprintf(DbgFp, "\tRead Request:\n"); 128 GETWORD(rmp->r_rrq.rmp_offset, t); 129 (void) fprintf(DbgFp, ReadFmt, rmp->r_rrq.rmp_retcode, 130 t, rmp->r_rrq.rmp_session); 131 (void) fprintf(DbgFp, "\t\tNoOfBytes: %u\n", 132 rmp->r_rrq.rmp_size); 133 break; 134 case RMP_READ_REPL: /* read reply */ 135 (void) fprintf(DbgFp, "\tRead Reply:\n"); 136 GETWORD(rmp->r_rrpl.rmp_offset, t); 137 (void) fprintf(DbgFp, ReadFmt, rmp->r_rrpl.rmp_retcode, 138 t, rmp->r_rrpl.rmp_session); 139 (void) fprintf(DbgFp, "\t\tNoOfBytesSent: %d\n", 140 rconn->rmplen - RMPREADSIZE(0)); 141 break; 142 case RMP_BOOT_DONE: /* boot complete */ 143 (void) fprintf(DbgFp, "\tBoot Complete:\n"); 144 (void) fprintf(DbgFp, "\t\tRetCode:%u SessID:%x\n", 145 rmp->r_done.rmp_retcode, 146 rmp->r_done.rmp_session); 147 break; 148 default: /* ??? */ 149 (void) fprintf(DbgFp, "\tUnknown Type:(%d)\n", 150 rmp->r_type); 151 } 152 (void) fputc('\n', DbgFp); 153 (void) fflush(DbgFp); 154 155 (void) sigsetmask(omask); /* reset old signal mask */ 156 } 157 158 159 /* 160 ** GetEtherAddr -- convert an RMP (Ethernet) address into a string. 161 ** 162 ** An RMP BOOT packet has been received. Look at the type field 163 ** and process Boot Requests, Read Requests, and Boot Complete 164 ** packets. Any other type will be dropped with a warning msg. 165 ** 166 ** Parameters: 167 ** addr - array of RMP_ADDRLEN bytes. 168 ** 169 ** Returns: 170 ** Pointer to static string representation of `addr'. 171 ** 172 ** Side Effects: 173 ** None. 174 ** 175 ** Warnings: 176 ** - The return value points to a static buffer; it must 177 ** be copied if it's to be saved. 178 ** - For speed, we assume a u_char consists of 8 bits. 179 */ 180 char * 181 GetEtherAddr(addr) 182 u_char *addr; 183 { 184 static char Hex[] = "0123456789abcdef"; 185 static char etherstr[RMP_ADDRLEN*3]; 186 register int i; 187 register char *cp1, *cp2; 188 189 /* 190 * For each byte in `addr', convert it to "<hexchar><hexchar>:". 191 * The last byte does not get a trailing `:' appended. 192 */ 193 i = 0; 194 cp1 = (char *)addr; 195 cp2 = etherstr; 196 for(;;) { 197 *cp2++ = Hex[*cp1 >> 4 & 0xf]; 198 *cp2++ = Hex[*cp1++ & 0xf]; 199 if (++i == RMP_ADDRLEN) 200 break; 201 *cp2++ = ':'; 202 } 203 *cp2 = '\0'; 204 205 return(etherstr); 206 } 207 208 209 /* 210 ** DispFlnm -- Print a string of bytes to DbgFp (often, a file name). 211 ** 212 ** Parameters: 213 ** size - number of bytes to print. 214 ** flnm - address of first byte. 215 ** 216 ** Returns: 217 ** Nothing. 218 ** 219 ** Side Effects: 220 ** - Characters are sent to `DbgFp'. 221 */ 222 void 223 DspFlnm(size, flnm) 224 register u_int size; 225 register char *flnm; 226 { 227 register int i; 228 229 (void) fprintf(DbgFp, "\n\t\tFile Name (%d): <", size); 230 for (i = 0; i < size; i++) 231 (void) fputc(*flnm++, DbgFp); 232 (void) fputs(">\n", DbgFp); 233 } 234 235 236 /* 237 ** NewClient -- allocate memory for a new CLIENT. 238 ** 239 ** Parameters: 240 ** addr - RMP (Ethernet) address of new client. 241 ** 242 ** Returns: 243 ** Ptr to new CLIENT or NULL if we ran out of memory. 244 ** 245 ** Side Effects: 246 ** - Memory will be malloc'd for the new CLIENT. 247 ** - If malloc() fails, a log message will be generated. 248 */ 249 CLIENT * 250 NewClient(addr) 251 u_char *addr; 252 { 253 CLIENT *ctmp; 254 255 if ((ctmp = (CLIENT *) malloc(sizeof(CLIENT))) == NULL) { 256 syslog(LOG_ERR, "NewClient: out of memory (%s)", 257 GetEtherAddr(addr)); 258 return(NULL); 259 } 260 261 bzero(ctmp, sizeof(CLIENT)); 262 bcopy(addr, &ctmp->addr[0], RMP_ADDRLEN); 263 return(ctmp); 264 } 265 266 /* 267 ** FreeClient -- free linked list of Clients. 268 ** 269 ** Parameters: 270 ** None. 271 ** 272 ** Returns: 273 ** Nothing. 274 ** 275 ** Side Effects: 276 ** - All malloc'd memory associated with the linked list of 277 ** CLIENTS will be free'd; `Clients' will be set to NULL. 278 ** 279 ** Warnings: 280 ** - This routine must be called with SIGHUP blocked. 281 */ 282 void 283 FreeClients() 284 { 285 register CLIENT *ctmp; 286 287 while (Clients != NULL) { 288 ctmp = Clients; 289 Clients = Clients->next; 290 FreeClient(ctmp); 291 } 292 } 293 294 /* 295 ** NewStr -- allocate memory for a character array. 296 ** 297 ** Parameters: 298 ** str - null terminated character array. 299 ** 300 ** Returns: 301 ** Ptr to new character array or NULL if we ran out of memory. 302 ** 303 ** Side Effects: 304 ** - Memory will be malloc'd for the new character array. 305 ** - If malloc() fails, a log message will be generated. 306 */ 307 char * 308 NewStr(str) 309 char *str; 310 { 311 char *stmp; 312 313 if ((stmp = (char *)malloc((unsigned) (strlen(str)+1))) == NULL) { 314 syslog(LOG_ERR, "NewStr: out of memory (%s)", str); 315 return(NULL); 316 } 317 318 (void) strcpy(stmp, str); 319 return(stmp); 320 } 321 322 /* 323 ** To save time, NewConn and FreeConn maintain a cache of one RMPCONN 324 ** in `LastFree' (defined below). 325 */ 326 327 static RMPCONN *LastFree = NULL; 328 329 /* 330 ** NewConn -- allocate memory for a new RMPCONN connection. 331 ** 332 ** Parameters: 333 ** rconn - initialization template for new connection. 334 ** 335 ** Returns: 336 ** Ptr to new RMPCONN or NULL if we ran out of memory. 337 ** 338 ** Side Effects: 339 ** - Memory may be malloc'd for the new RMPCONN (if not cached). 340 ** - If malloc() fails, a log message will be generated. 341 */ 342 RMPCONN * 343 NewConn(rconn) 344 RMPCONN *rconn; 345 { 346 RMPCONN *rtmp; 347 348 if (LastFree == NULL) { /* nothing cached; make a new one */ 349 if ((rtmp = (RMPCONN *) malloc(sizeof(RMPCONN))) == NULL) { 350 syslog(LOG_ERR, "NewConn: out of memory (%s)", 351 EnetStr(rconn)); 352 return(NULL); 353 } 354 } else { /* use the cached RMPCONN */ 355 rtmp = LastFree; 356 LastFree = NULL; 357 } 358 359 /* 360 * Copy template into `rtmp', init file descriptor to `-1' and 361 * set ptr to next elem NULL. 362 */ 363 bcopy((char *)rconn, (char *)rtmp, sizeof(RMPCONN)); 364 rtmp->bootfd = -1; 365 rtmp->next = NULL; 366 367 return(rtmp); 368 } 369 370 /* 371 ** FreeConn -- Free memory associated with an RMPCONN connection. 372 ** 373 ** Parameters: 374 ** rtmp - ptr to RMPCONN to be free'd. 375 ** 376 ** Returns: 377 ** Nothing. 378 ** 379 ** Side Effects: 380 ** - Memory associated with `rtmp' may be free'd (or cached). 381 ** - File desc associated with `rtmp->bootfd' will be closed. 382 */ 383 void 384 FreeConn(rtmp) 385 register RMPCONN *rtmp; 386 { 387 /* 388 * If the file descriptor is in use, close the file. 389 */ 390 if (rtmp->bootfd >= 0) { 391 (void) close(rtmp->bootfd); 392 rtmp->bootfd = -1; 393 } 394 395 if (LastFree == NULL) /* cache for next time */ 396 rtmp = LastFree; 397 else /* already one cached; free this one */ 398 free((char *)rtmp); 399 } 400 401 /* 402 ** FreeConns -- free linked list of RMPCONN connections. 403 ** 404 ** Parameters: 405 ** None. 406 ** 407 ** Returns: 408 ** Nothing. 409 ** 410 ** Side Effects: 411 ** - All malloc'd memory associated with the linked list of 412 ** connections will be free'd; `RmpConns' will be set to NULL. 413 ** - If LastFree is != NULL, it too will be free'd & NULL'd. 414 ** 415 ** Warnings: 416 ** - This routine must be called with SIGHUP blocked. 417 */ 418 void 419 FreeConns() 420 { 421 register RMPCONN *rtmp; 422 423 while (RmpConns != NULL) { 424 rtmp = RmpConns; 425 RmpConns = RmpConns->next; 426 FreeConn(rtmp); 427 } 428 429 if (LastFree != NULL) { 430 free((char *)LastFree); 431 LastFree = NULL; 432 } 433 } 434 435 /* 436 ** AddConn -- Add a connection to the linked list of connections. 437 ** 438 ** Parameters: 439 ** rconn - connection to be added. 440 ** 441 ** Returns: 442 ** Nothing. 443 ** 444 ** Side Effects: 445 ** - RmpConn will point to new connection. 446 ** 447 ** Warnings: 448 ** - This routine must be called with SIGHUP blocked. 449 */ 450 void 451 AddConn(rconn) 452 register RMPCONN *rconn; 453 { 454 if (RmpConns != NULL) 455 rconn->next = RmpConns; 456 RmpConns = rconn; 457 } 458 459 /* 460 ** FindConn -- Find a connection in the linked list of connections. 461 ** 462 ** We use the RMP (Ethernet) address as the basis for determining 463 ** if this is the same connection. According to the Remote Maint 464 ** Protocol, we can only have one connection with any machine. 465 ** 466 ** Parameters: 467 ** rconn - connection to be found. 468 ** 469 ** Returns: 470 ** Matching connection from linked list or NULL if not found. 471 ** 472 ** Side Effects: 473 ** None. 474 ** 475 ** Warnings: 476 ** - This routine must be called with SIGHUP blocked. 477 */ 478 RMPCONN * 479 FindConn(rconn) 480 register RMPCONN *rconn; 481 { 482 register RMPCONN *rtmp; 483 484 for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next) 485 if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0], 486 (char *)&rtmp->rmp.hp_hdr.saddr[0], RMP_ADDRLEN) == 0) 487 break; 488 489 return(rtmp); 490 } 491 492 /* 493 ** RemoveConn -- Remove a connection from the linked list of connections. 494 ** 495 ** Parameters: 496 ** rconn - connection to be removed. 497 ** 498 ** Returns: 499 ** Nothing. 500 ** 501 ** Side Effects: 502 ** - If found, an RMPCONN will cease to exist and it will 503 ** be removed from the linked list. 504 ** 505 ** Warnings: 506 ** - This routine must be called with SIGHUP blocked. 507 */ 508 void 509 RemoveConn(rconn) 510 register RMPCONN *rconn; 511 { 512 register RMPCONN *thisrconn, *lastrconn; 513 514 if (RmpConns == rconn) { /* easy case */ 515 RmpConns = RmpConns->next; 516 FreeConn(rconn); 517 } else { /* must traverse linked list */ 518 lastrconn = RmpConns; /* set back ptr */ 519 thisrconn = lastrconn->next; /* set current ptr */ 520 while (thisrconn != NULL) { 521 if (rconn == thisrconn) { /* found it */ 522 lastrconn->next = thisrconn->next; 523 FreeConn(thisrconn); 524 break; 525 } 526 lastrconn = thisrconn; 527 thisrconn = thisrconn->next; 528 } 529 } 530 } 531