~ubuntu-branches/ubuntu/utopic/binkd/utopic-proposed

« back to all changes in this revision

Viewing changes to iptools.c

  • Committer: Bazaar Package Importer
  • Author(s): Marco d'Itri
  • Date: 2002-03-24 22:52:25 UTC
  • Revision ID: james.westby@ubuntu.com-20020324225225-7ru6itlapn03nl35
Tags: upstream-0.9.5a
ImportĀ upstreamĀ versionĀ 0.9.5a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  iptools.c -- Some useful TCP/IP utils
 
3
 *
 
4
 *  iptools.c is a part of binkd project
 
5
 *
 
6
 *  Copyright (C) 1997-1998  Dima Maloff, 5047/13
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 2 of the License, or
 
11
 *  (at your option) any later version. See COPYING.
 
12
 */
 
13
 
 
14
/*
 
15
 * $Id: iptools.c,v 2.0 2001/01/10 12:12:38 gul Exp $
 
16
 *
 
17
 * $Log: iptools.c,v $
 
18
 * Revision 2.0  2001/01/10 12:12:38  gul
 
19
 * Binkd is under CVS again
 
20
 *
 
21
 * Revision 1.3  1998/06/19  05:19:33  mff
 
22
 * changes in get_hostname()
 
23
 *
 
24
 * Revision 1.2  1997/10/23  04:01:29  mff
 
25
 * +find_port(), minor changes for Amiga port
 
26
 *
 
27
 * Revision 1.1  1997/03/28  06:52:14  mff
 
28
 * Initial revision
 
29
 */
 
30
 
 
31
#include <string.h>
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <fcntl.h>
 
35
#include <ctype.h>
 
36
 
 
37
#if !defined(WIN32) && !defined(DOS)
 
38
#include <sys/ioctl.h>
 
39
#endif
 
40
 
 
41
#include "tools.h"
 
42
#include "readcfg.h"
 
43
#include "iphdr.h"
 
44
#include "iptools.h"
 
45
 
 
46
/*
 
47
 * Finds ASCIIZ address
 
48
 */
 
49
const char *get_hostname (struct sockaddr_in *addr, char *host, int len)
 
50
{
 
51
  struct hostent *hp;
 
52
  struct sockaddr_in s;
 
53
 
 
54
  memcpy(&s, addr, sizeof(s));
 
55
  hp = ((backresolv == 0) ? 0 :
 
56
        gethostbyaddr ((char *) &s.sin_addr,
 
57
                       sizeof s.sin_addr,
 
58
                       AF_INET));
 
59
 
 
60
  strnzcpy (host, hp ? hp->h_name : inet_ntoa (s.sin_addr), len);
 
61
 
 
62
  return host;
 
63
}
 
64
 
 
65
#ifdef HAVE_THREADS
 
66
void copy_hostent(struct hostent *dest, struct hostent *src)
 
67
{
 
68
  int naddr;
 
69
  char **cp;
 
70
 
 
71
  memcpy(dest, src, sizeof(struct hostent));
 
72
  for (cp = src->h_addr_list, naddr = 0; cp && *cp; naddr++, cp++);
 
73
  dest->h_addr_list = malloc((naddr+1)*sizeof(dest->h_addr_list[0]));
 
74
  if (dest->h_addr_list)
 
75
  {
 
76
    dest->h_addr_list[0] = malloc(naddr*src->h_length);
 
77
    if (dest->h_addr_list[0])
 
78
    {
 
79
      for (cp = src->h_addr_list, naddr=0; cp && *cp; cp++, naddr++)
 
80
      {
 
81
        dest->h_addr_list[naddr] = dest->h_addr_list[0]+naddr*src->h_length;
 
82
        memcpy(dest->h_addr_list[naddr], *cp, src->h_length);
 
83
      }
 
84
      dest->h_addr_list[naddr] = NULL;
 
85
    }
 
86
  }
 
87
}
 
88
#endif
 
89
 
 
90
/*
 
91
 * Sets non-blocking mode for a given socket
 
92
 */
 
93
void setsockopts (SOCKET s)
 
94
{
 
95
 
 
96
#if defined(FIONBIO)
 
97
#if defined(UNIX) || defined(IBMTCPIP) || defined(AMIGA)
 
98
  int arg;
 
99
 
 
100
  arg = 1;
 
101
  if (ioctl (s, FIONBIO, (char *) &arg, sizeof arg) < 0)
 
102
    Log (1, "ioctl (FIONBIO): %s", TCPERR ());
 
103
 
 
104
#elif defined(WIN32)
 
105
  u_long arg;
 
106
 
 
107
  arg = 1;
 
108
  if (ioctlsocket (s, FIONBIO, &arg) < 0)
 
109
    Log (1, "ioctlsocket (FIONBIO): %s", TCPERR ());
 
110
#endif
 
111
#endif
 
112
 
 
113
#if defined(UNIX) || defined(EMX) || defined(AMIGA)
 
114
  if (fcntl (s, F_SETFL, O_NONBLOCK) == -1)
 
115
    Log (1, "fcntl: %s", strerror (errno));
 
116
#endif
 
117
}
 
118
 
 
119
/*
 
120
 * Find the port number (in the host byte order) by a port number string or
 
121
 * a service name. Find_port ("") will return binkp's port from
 
122
 * /etc/services or even (if there is no binkp entry) 24554.
 
123
 * Returns 0 on error.
 
124
 */
 
125
int find_port (char *s)
 
126
{
 
127
  struct servent *entry = getservbyname (*s ? s : PRTCLNAME, "tcp");
 
128
 
 
129
  if (entry)
 
130
    return ntohs (entry->s_port);
 
131
  if (*s == 0)
 
132
    return DEF_PORT;
 
133
  if (isdigit (*s))
 
134
    return atoi (s);
 
135
 
 
136
  Log (1, "%s: incorrect port", s);
 
137
  return 0;
 
138
}