/* * Copyright (c) 1996-2007, OpenFWTK Development Group * All rights reserved. See LICENSE. */ /* * Handling of telnet strings * * $Id: telnet.c,v 1.2 2007/09/10 02:49:13 arkenoi Exp $ */ #include #include #include "telnet.h" const char tnesc = '\377'; int unparse_telnet_string(char *str, int ofd, int flags) { char *npos; int count = 0, res; for(npos = str; *npos; npos++) { if(*npos == tnesc) { ++npos; switch(*npos) { case '\377': /* Leave this untouched */ break; case '\373': case '\375': { char buf[3]; buf[0] = tnesc; buf[1] = ('\373' == *npos) ? '\376' : '\374'; buf[2] = *++npos; if((res = send(ofd, buf, 3, flags)) < 0) return res; count++; } /* Fall through */ case '\374': case '\376': npos++; /* Fall through */ default: npos++; } } if(str != npos) *str = *npos; str++; } return count; } /* This probably sucks, but it should suffice for FTP */ void telnet_response(int fd, int flags) { char buf[4]; if(recv(fd, &buf[0], 1, flags) < 0) return; /* XXX */ if(tnesc != buf[0]) return; /* nothing to do here */ if(recv(fd, &buf[1], 1, flags) < 0) return; /* XXX */ if((buf[1] == '\373') || (buf[1] == '\375')) { if(recv(fd, &buf[2], 1, flags) < 0) return; /* XXX */ buf[3] = 0; } else { buf[2] = 0; } unparse_telnet_string(buf, fd, flags); }