ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

net.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1989, 1993
00003  *  The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  *  This product includes software developed by the University of
00019  *  California, Berkeley and its contributors.
00020  * 4. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  */
00036 
00037 #include "precomp.h"
00038 
00039 void
00040 netfinger(char *name)
00041 {
00042     extern int lflag;
00043     char c, lastc;
00044     struct in_addr defaddr;
00045     struct hostent *hp, def;
00046     struct servent *sp;
00047     struct sockaddr_in sin;
00048     SOCKET s;
00049     char *alist[1], *host;
00050 
00051     /* If this is a local request */
00052     if (!(host = rindex(name, '@')))
00053         return;
00054 
00055     *host++ = '\0';
00056     if (isdigit(*host) && (defaddr.s_addr = inet_addr(host)) != (unsigned long)-1) {
00057         def.h_name = host;
00058         def.h_addr_list = alist;
00059         def.h_addr = (char *)&defaddr;
00060         def.h_length = sizeof(struct in_addr);
00061         def.h_addrtype = AF_INET;
00062         def.h_aliases = 0;
00063         hp = &def;
00064     } else if (!(hp = gethostbyname(host))) {
00065         (void)fprintf(stderr,
00066             "finger: unknown host: %s\n", host);
00067         return;
00068     }
00069     if (!(sp = getservbyname("finger", "tcp"))) {
00070         (void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
00071         return;
00072     }
00073     sin.sin_family = hp->h_addrtype;
00074     bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
00075     sin.sin_port = sp->s_port;
00076     if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) == INVALID_SOCKET) {
00077         perror("finger: socket");
00078         return;
00079     }
00080 
00081     /* have network connection; identify the host connected with */
00082     (void)printf("[%s]\n", hp->h_name);
00083     if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
00084         fprintf(stderr, "finger: connect rc = %d", WSAGetLastError());
00085         (void)closesocket(s);
00086         return;
00087     }
00088 
00089     /* -l flag for remote fingerd  */
00090     if (lflag)
00091         send(s, "/W ", 3, 0);
00092     /* send the name followed by <CR><LF> */
00093     send(s, name, strlen(name), 0);
00094     send(s, "\r\n", 2, 0);
00095 
00096     /*
00097      * Read from the remote system; once we're connected, we assume some
00098      * data.  If none arrives, we hang until the user interrupts.
00099      *
00100      * If we see a <CR> or a <CR> with the high bit set, treat it as
00101      * a newline; if followed by a newline character, only output one
00102      * newline.
00103      *
00104      * Otherwise, all high bits are stripped; if it isn't printable and
00105      * it isn't a space, we can simply set the 7th bit.  Every ASCII
00106      * character with bit 7 set is printable.
00107      */
00108     lastc = 0;
00109     while (recv(s, &c, 1, 0) == 1) {
00110         c &= 0x7f;
00111         if (c == 0x0d) {
00112             if (lastc == '\r')  /* ^M^M - skip dupes */
00113                 continue;
00114             c = '\n';
00115             lastc = '\r';
00116         } else {
00117             if (!isprint(c) && !isspace(c))
00118                 c |= 0x40;
00119             if (lastc != '\r' || c != '\n')
00120                 lastc = c;
00121             else {
00122                 lastc = '\n';
00123                 continue;
00124             }
00125         }
00126         putchar(c);
00127     }
00128     if (lastc != '\n')
00129         putchar('\n');
00130     putchar('\n');
00131 }

Generated on Sun May 27 2012 04:17:12 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.