Annotation of sbbs/src/sbbs3/ident.c, revision 1.1.1.2

1.1       root        1: /* ident.c */
                      2: 
                      3: /* Synchronet Indentification (RFC1413) functions */
                      4: 
1.1.1.2 ! root        5: /* $Id: ident.c,v 1.13 2011/04/27 00:58:44 rswindell Exp $ */
1.1       root        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:  *                                                                                                                                                     *
1.1.1.2 ! root       11:  * Copyright 2011 Rob Swindell - http://www.synchro.net/copyright.html         *
1.1       root       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: 
1.1.1.2 ! root       41: BOOL identify(SOCKADDR_IN* client_addr, u_short local_port, char* buf
1.1       root       42:                           ,size_t maxlen, int timeout)
                     43: {
                     44:        char            req[128];
                     45:        int                     i;
                     46:        int                     result;
                     47:        int                     rd;
                     48:        ulong           val;
                     49:        SOCKET          sock=INVALID_SOCKET;
                     50:        SOCKADDR_IN     addr;
                     51:        struct timeval  tv;
                     52:        fd_set                  socket_set;
1.1.1.2 ! root       53:        BOOL            success=FALSE;
1.1       root       54: 
1.1.1.2 ! root       55:        if(timeout<=0)
        !            56:                timeout=IDENT_DEFAULT_TIMEOUT;
1.1       root       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;
1.1.1.2 ! root       68:                addr.sin_port=htons(IPPORT_IDENT);
1.1       root       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: 
1.1.1.2 ! root      102:                sprintf(req,"%u,%u\r\n", ntohs(client_addr->sin_port), local_port);
1.1       root      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);
1.1.1.2 ! root      127:                success=TRUE;
1.1       root      128:        } while(0);
                    129: 
                    130:        close_socket(sock);
                    131: 
1.1.1.2 ! root      132:        return success;
1.1       root      133: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.