1 /* 2 * Copyright (c) 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 */ 12 13 #ifndef lint 14 static char sccsid[] = "@(#)utilities.c 1.5 (Berkeley) 05/15/88"; 15 #endif /* not lint */ 16 17 #define TELOPTS 18 #include <arpa/telnet.h> 19 #include <sys/types.h> 20 21 #include <ctype.h> 22 23 #include "general.h" 24 25 #include "ring.h" 26 27 #include "externs.h" 28 29 FILE *NetTrace = 0; /* Not in bss, since needs to stay */ 30 31 /* 32 * upcase() 33 * 34 * Upcase (in place) the argument. 35 */ 36 37 void 38 upcase(argument) 39 register char *argument; 40 { 41 register int c; 42 43 while ((c = *argument) != 0) { 44 if (islower(c)) { 45 *argument = toupper(c); 46 } 47 argument++; 48 } 49 } 50 51 /* 52 * SetSockOpt() 53 * 54 * Compensate for differences in 4.2 and 4.3 systems. 55 */ 56 57 int 58 SetSockOpt(fd, level, option, yesno) 59 int 60 fd, 61 level, 62 option, 63 yesno; 64 { 65 #ifndef NOT43 66 return setsockopt(fd, level, option, 67 (char *)&yesno, sizeof yesno); 68 #else /* NOT43 */ 69 if (yesno == 0) { /* Can't do that in 4.2! */ 70 fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n", 71 option); 72 return -1; 73 } 74 return setsockopt(fd, level, option, 0, 0); 75 #endif /* NOT43 */ 76 } 77 78 /* 79 * The following are routines used to print out debugging information. 80 */ 81 82 83 void 84 Dump(direction, buffer, length) 85 char direction; 86 char *buffer; 87 int length; 88 { 89 # define BYTES_PER_LINE 32 90 # define min(x,y) ((x<y)? x:y) 91 char *pThis; 92 int offset; 93 94 offset = 0; 95 96 while (length) { 97 /* print one line */ 98 fprintf(NetTrace, "%c 0x%x\t", direction, offset); 99 pThis = buffer; 100 buffer = buffer+min(length, BYTES_PER_LINE); 101 while (pThis < buffer) { 102 fprintf(NetTrace, "%.2x", (*pThis)&0xff); 103 pThis++; 104 } 105 fprintf(NetTrace, "\n"); 106 length -= BYTES_PER_LINE; 107 offset += BYTES_PER_LINE; 108 if (length < 0) { 109 return; 110 } 111 /* find next unique line */ 112 } 113 } 114 115 116 /*VARARGS*/ 117 void 118 printoption(direction, fmt, option, what) 119 char *direction, *fmt; 120 int option, what; 121 { 122 if (!showoptions) 123 return; 124 fprintf(NetTrace, "%s ", direction+1); 125 if (fmt == doopt) 126 fmt = "do"; 127 else if (fmt == dont) 128 fmt = "dont"; 129 else if (fmt == will) 130 fmt = "will"; 131 else if (fmt == wont) 132 fmt = "wont"; 133 else 134 fmt = "???"; 135 if (option < (sizeof telopts/sizeof telopts[0])) 136 fprintf(NetTrace, "%s %s", fmt, telopts[option]); 137 else 138 fprintf(NetTrace, "%s %d", fmt, option); 139 if (*direction == '<') { 140 fprintf(NetTrace, "\r\n"); 141 return; 142 } 143 fprintf(NetTrace, " (%s)\r\n", what ? "reply" : "don't reply"); 144 } 145 146 void 147 printsub(direction, pointer, length) 148 char *direction, /* "<" or ">" */ 149 *pointer; /* where suboption data sits */ 150 int length; /* length of suboption data */ 151 { 152 if (showoptions) { 153 fprintf(NetTrace, "%s suboption ", 154 (direction[0] == '<')? "Received":"Sent"); 155 switch (pointer[0]) { 156 case TELOPT_TTYPE: 157 fprintf(NetTrace, "Terminal type "); 158 switch (pointer[1]) { 159 case TELQUAL_IS: 160 { 161 char tmpbuf[SUBBUFSIZE]; 162 int minlen = min(length, sizeof tmpbuf); 163 164 memcpy(tmpbuf, pointer+2, minlen); 165 tmpbuf[minlen-1] = 0; 166 fprintf(NetTrace, "is %s.\n", tmpbuf); 167 } 168 break; 169 case TELQUAL_SEND: 170 fprintf(NetTrace, "- request to send.\n"); 171 break; 172 default: 173 fprintf(NetTrace, 174 "- unknown qualifier %d (0x%x).\n", pointer[1]); 175 } 176 break; 177 default: 178 fprintf(NetTrace, "Unknown option %d (0x%x)\n", 179 pointer[0], pointer[0]); 180 } 181 } 182 } 183