home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NET-TOOL.1 / NET-TOOL / net-tools / lib / getsock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  1.5 KB  |  63 lines

  1. /*
  2.  * NET-2    This file contains part of the support functions module
  3.  *        for the NET-2 base distribution.
  4.  *
  5.  *        Fetch a socket-address from a /proc readout.
  6.  *
  7.  * Version:    @(#)getsock.c    1.10    10/07/93
  8.  *
  9.  * Author:    Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  10.  *        Copyright 1993 MicroWalt Corporation
  11.  *
  12.  *        This program is free software; you can redistribute it
  13.  *        and/or  modify it under  the terms of  the GNU General
  14.  *        Public  License as  published  by  the  Free  Software
  15.  *        Foundation;  either  version 2 of the License, or  (at
  16.  *        your option) any later version.
  17.  */
  18. #include "config.h"
  19.  
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <netinet/in.h>
  29. #include "support.h"
  30. #include "pathnames.h"
  31.  
  32.  
  33. char *
  34. getsock(char *bufp, struct sockaddr *sap)
  35. {
  36.   unsigned char *ptr;
  37.   char *sp = bufp;
  38.   int i, val;
  39.   struct sockaddr_in *sai=(struct sockaddr_in *)sap;
  40.  
  41.   ptr = (unsigned char *) sap;
  42.   if(sscanf(bufp,"%lX",&sai->sin_addr.s_addr)==1)
  43.   {
  44.       sai->sin_family=AF_INET;
  45.       return bufp+8;
  46.   }
  47. /*  printf("Burped on '%s'\n",bufp);  */
  48.   for (i = 0; i < sizeof(struct sockaddr); i++) {
  49.     val = 0;
  50.     if (*sp == '\t') break;
  51.     if (*sp >= 'A') val = (int) (*sp - 'A') + 10;
  52.       else val = (int) (*sp - '0');
  53.     val <<= 4;
  54.     sp++;
  55.     if (*sp >= 'A') val |= (int) (*sp - 'A') + 10;
  56.       else val |= (int) (*sp - '0');
  57.     *ptr++ = (unsigned char) (val & 0377);
  58.     sp++;
  59.   }
  60.  
  61.   return(sp);
  62. }
  63.