|
|
1.1 ! root 1: /* ident.c */ ! 2: ! 3: /* Synchronet Indentification (RFC1413) functions */ ! 4: ! 5: /* $Id: ident.c,v 1.11 2005/10/13 01:44:53 rswindell Exp $ */ ! 6: ! 7: /**************************************************************************** ! 8: * @format.tab-size 4 (Plain Text/Source Code File Header) * ! 9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) * ! 10: * * ! 11: * Copyright 2000 Rob Swindell - http://www.synchro.net/copyright.html * ! 12: * * ! 13: * This program is free software; you can redistribute it and/or * ! 14: * modify it under the terms of the GNU General Public License * ! 15: * as published by the Free Software Foundation; either version 2 * ! 16: * of the License, or (at your option) any later version. * ! 17: * See the GNU General Public License for more details: gpl.txt or * ! 18: * http://www.fsf.org/copyleft/gpl.html * ! 19: * * ! 20: * Anonymous FTP access to the most recent released source is available at * ! 21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net * ! 22: * * ! 23: * Anonymous CVS access to the development source and modification history * ! 24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: * ! 25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login * ! 26: * (just hit return, no password is necessary) * ! 27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src * ! 28: * * ! 29: * For Synchronet coding style and modification guidelines, see * ! 30: * http://www.synchro.net/source.html * ! 31: * * ! 32: * You are encouraged to submit any modifications (preferably in Unix diff * ! 33: * format) via e-mail to [email protected] * ! 34: * * ! 35: * Note: If this box doesn't appear square, then you need to fix your tabs. * ! 36: ****************************************************************************/ ! 37: ! 38: #include "sbbs.h" ! 39: #include "ident.h" ! 40: ! 41: char* identify(SOCKADDR_IN* client_addr, u_short local_port, char* buf ! 42: ,size_t maxlen, int timeout) ! 43: { ! 44: char req[128]; ! 45: char* identity=NULL; ! 46: int i; ! 47: int result; ! 48: int rd; ! 49: ulong val; ! 50: SOCKET sock=INVALID_SOCKET; ! 51: SOCKADDR_IN addr; ! 52: struct timeval tv; ! 53: fd_set socket_set; ! 54: ! 55: if(timeout<0) ! 56: timeout=10; ! 57: ! 58: do { ! 59: if((sock = open_socket(SOCK_STREAM, "ident")) == INVALID_SOCKET) { ! 60: sprintf(buf,"ERROR %d creating socket",ERROR_VALUE); ! 61: break; ! 62: } ! 63: ! 64: val=1; ! 65: ioctlsocket(sock,FIONBIO,&val); ! 66: ! 67: addr=*client_addr; ! 68: addr.sin_port=htons(113); /* per RFC1413 */ ! 69: ! 70: result=connect(sock, (struct sockaddr*)&addr, sizeof(addr)); ! 71: ! 72: if(result==SOCKET_ERROR ! 73: && (ERROR_VALUE==EWOULDBLOCK || ERROR_VALUE==EINPROGRESS)) { ! 74: tv.tv_sec=timeout; ! 75: tv.tv_usec=0; ! 76: ! 77: FD_ZERO(&socket_set); ! 78: FD_SET(sock,&socket_set); ! 79: if(select(sock+1,NULL,&socket_set,NULL,&tv)==1) ! 80: result=0; /* success */ ! 81: } ! 82: if(result!=0) { ! 83: sprintf(buf,"ERROR %d connecting to server",ERROR_VALUE); ! 84: break; ! 85: } ! 86: ! 87: val=0; ! 88: ioctlsocket(sock,FIONBIO,&val); ! 89: ! 90: tv.tv_sec=10; ! 91: tv.tv_usec=0; ! 92: ! 93: FD_ZERO(&socket_set); ! 94: FD_SET(sock,&socket_set); ! 95: ! 96: i=select(sock+1,NULL,&socket_set,NULL,&tv); ! 97: if(i<1) { ! 98: sprintf(buf,"ERROR %d selecting socket for send",ERROR_VALUE); ! 99: break; ! 100: } ! 101: ! 102: sprintf(req,"%u, %u\r\n", ntohs(client_addr->sin_port), local_port); ! 103: if(sendsocket(sock,req,strlen(req))!=(int)strlen(req)) { ! 104: sprintf(buf,"ERROR %d sending request",ERROR_VALUE); ! 105: break; ! 106: } ! 107: ! 108: tv.tv_sec=10; ! 109: tv.tv_usec=0; ! 110: ! 111: FD_ZERO(&socket_set); ! 112: FD_SET(sock,&socket_set); ! 113: ! 114: i=select(sock+1,&socket_set,NULL,NULL,&tv); ! 115: if(i<1) { ! 116: sprintf(buf,"ERROR %d detecting response",ERROR_VALUE); ! 117: break; ! 118: } ! 119: ! 120: rd=recv(sock,buf,maxlen,0); ! 121: if(rd<1) { ! 122: sprintf(buf,"ERROR %d receiving response",ERROR_VALUE); ! 123: break; ! 124: } ! 125: buf[rd]=0; ! 126: truncsp(buf); ! 127: identity=buf; ! 128: } while(0); ! 129: ! 130: close_socket(sock); ! 131: ! 132: return(identity); ! 133: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.