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.4 (Berkeley) 03/08/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 "ring.h" 24 25 #include "externs.h" 26 27 FILE *NetTrace = 0; /* Not in bss, since needs to stay */ 28 29 /* 30 * upcase() 31 * 32 * Upcase (in place) the argument. 33 */ 34 35 void 36 upcase(argument) 37 register char *argument; 38 { 39 register int c; 40 41 while ((c = *argument) != 0) { 42 if (islower(c)) { 43 *argument = toupper(c); 44 } 45 argument++; 46 } 47 } 48 49 /* 50 * SetSockOpt() 51 * 52 * Compensate for differences in 4.2 and 4.3 systems. 53 */ 54 55 int 56 SetSockOpt(fd, level, option, yesno) 57 int 58 fd, 59 level, 60 option, 61 yesno; 62 { 63 #ifndef NOT43 64 return setsockopt(fd, level, option, 65 (char *)&yesno, sizeof yesno); 66 #else /* NOT43 */ 67 if (yesno == 0) { /* Can't do that in 4.2! */ 68 fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n", 69 option); 70 return -1; 71 } 72 return setsockopt(fd, level, option, 0, 0); 73 #endif /* NOT43 */ 74 } 75 76 /* 77 * The following are routines used to print out debugging information. 78 */ 79 80 81 void 82 Dump(direction, buffer, length) 83 char direction; 84 char *buffer; 85 int length; 86 { 87 # define BYTES_PER_LINE 32 88 # define min(x,y) ((x<y)? x:y) 89 char *pThis; 90 int offset; 91 92 offset = 0; 93 94 while (length) { 95 /* print one line */ 96 fprintf(NetTrace, "%c 0x%x\t", direction, offset); 97 pThis = buffer; 98 buffer = buffer+min(length, BYTES_PER_LINE); 99 while (pThis < buffer) { 100 fprintf(NetTrace, "%.2x", (*pThis)&0xff); 101 pThis++; 102 } 103 fprintf(NetTrace, "\n"); 104 length -= BYTES_PER_LINE; 105 offset += BYTES_PER_LINE; 106 if (length < 0) { 107 return; 108 } 109 /* find next unique line */ 110 } 111 } 112 113 114 /*VARARGS*/ 115 void 116 printoption(direction, fmt, option, what) 117 char *direction, *fmt; 118 int option, what; 119 { 120 if (!showoptions) 121 return; 122 fprintf(NetTrace, "%s ", direction+1); 123 if (fmt == doopt) 124 fmt = "do"; 125 else if (fmt == dont) 126 fmt = "dont"; 127 else if (fmt == will) 128 fmt = "will"; 129 else if (fmt == wont) 130 fmt = "wont"; 131 else 132 fmt = "???"; 133 if (option < (sizeof telopts/sizeof telopts[0])) 134 fprintf(NetTrace, "%s %s", fmt, telopts[option]); 135 else 136 fprintf(NetTrace, "%s %d", fmt, option); 137 if (*direction == '<') { 138 fprintf(NetTrace, "\r\n"); 139 return; 140 } 141 fprintf(NetTrace, " (%s)\r\n", what ? "reply" : "don't reply"); 142 } 143 144 void 145 printsub(direction, pointer, length) 146 char *direction, /* "<" or ">" */ 147 *pointer; /* where suboption data sits */ 148 int length; /* length of suboption data */ 149 { 150 if (showoptions) { 151 fprintf(NetTrace, "%s suboption ", 152 (direction[0] == '<')? "Received":"Sent"); 153 switch (pointer[0]) { 154 case TELOPT_TTYPE: 155 fprintf(NetTrace, "Terminal type "); 156 switch (pointer[1]) { 157 case TELQUAL_IS: 158 { 159 char tmpbuf[SUBBUFSIZE]; 160 int minlen = min(length, sizeof tmpbuf); 161 162 memcpy(tmpbuf, pointer+2, minlen); 163 tmpbuf[minlen-1] = 0; 164 fprintf(NetTrace, "is %s.\n", tmpbuf); 165 } 166 break; 167 case TELQUAL_SEND: 168 fprintf(NetTrace, "- request to send.\n"); 169 break; 170 default: 171 fprintf(NetTrace, 172 "- unknown qualifier %d (0x%x).\n", pointer[1]); 173 } 174 break; 175 default: 176 fprintf(NetTrace, "Unknown option %d (0x%x)\n", 177 pointer[0], pointer[0]); 178 } 179 } 180 } 181