isc_socket.c 4.53 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
2
3
/*
** isc_socket.c                               Socket specified code
**
Per Cederqvist's avatar
Per Cederqvist committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
** Copyright (C) 1992, 1998-1999 by Peter Eriksson and
** Per Cederqvist of the Lysator Academic Computer Association.
**
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Library General Public
** License as published by the Free Software Foundation; either
** version 2 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** Library General Public License for more details.
**
** You should have received a copy of the GNU Library General Public
** License along with this library; if not, write to the Free
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Per Cederqvist's avatar
Per Cederqvist committed
21
22
23
**
** history:
**   920210 pen     initial coding
Per Cederqvist's avatar
Per Cederqvist committed
24
** (See ChangeLog for recent history)
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
*/

#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
28
#ifdef TIME_WITH_SYS_TIME
29
#  include <sys/time.h>
Per Cederqvist's avatar
Per Cederqvist committed
30
#  include <time.h>
31
32
33
34
35
36
#else
#  ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#  else
#    include <time.h>
#  endif
Per Cederqvist's avatar
Per Cederqvist committed
37
#endif
Per Cederqvist's avatar
Per Cederqvist committed
38
39
40
41
42
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include <sys/ioctl.h>
43
#include <sys/socket.h>
Per Cederqvist's avatar
Per Cederqvist committed
44
45
#include <errno.h>
#include <netdb.h>
Per Cederqvist's avatar
Per Cederqvist committed
46
47
48
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif
49
50
51
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
52
53
54
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
55
#include <fcntl.h>
56

Per Cederqvist's avatar
Per Cederqvist committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "isc.h"
#include "intern.h"



#if defined(__GNUC__) && defined(__sparc__)
/*
** inet_ntoa() is buggy on SPARCs running SunOS 4.1.1, so
** we use our own
*/
#ifdef inet_ntoa
#undef inet_ntoa
#endif

71
char *inet_ntoa(ad)
Per Cederqvist's avatar
Per Cederqvist committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    struct in_addr ad;
{
  u_long s_ad;
  int a, b, c, d;
  static char addr[20];
  
  s_ad = ad.s_addr;
  d = s_ad % 256;
  s_ad /= 256;
  c = s_ad % 256;
  s_ad /= 256;
  b = s_ad % 256;
  a = s_ad / 256;
  sprintf(addr, "%d.%d.%d.%d", a, b, c, d);
  
  return addr;
}
#endif



IscAddress *
isc_mkipaddress(struct sockaddr *addr)
{
  IscAddress *ia;


  ISC_XNEW(ia);
  memcpy(&ia->ip.saddr, addr, sizeof(ia->ip.saddr));

  return ia;
}




IscAddress *
isc_getladdress(IscSession *scb)
{
  struct sockaddr addr;
  int len;


  if (scb->type != ISC_TYPE_TCP &&
      scb->type != ISC_TYPE_UDP)
    return NULL;
  
  len = sizeof(addr);
  if (getsockname(scb->fd, &addr, &len) < 0)
    return NULL;

  return isc_mkipaddress(&addr);
}



IscAddress *
isc_getraddress(IscSession *scb)
{
  struct sockaddr addr;
  int len;


  if (scb->type != ISC_TYPE_TCP &&
      scb->type != ISC_TYPE_UDP)
    return NULL;
  
  len = sizeof(addr);
  if (getpeername(scb->fd, &addr, &len) < 0)
    return NULL;

  return isc_mkipaddress(&addr);
}


void
isc_freeaddress(IscAddress *addr)
{
  isc_free(addr);
}


IscAddress *
isc_copyaddress(IscAddress *addr)
{
  IscAddress *new_addr;

  ISC_XNEW(new_addr);
  *new_addr = *addr;

  return new_addr;
}



char *isc_getipnum(IscAddress *ia, char *buf, int len)
{
  static char hostname[256];
  struct sockaddr_in *addr;
  

  if (!buf)
  {
    buf = hostname;
    len = sizeof(hostname)-1;
  }
  
  addr = (struct sockaddr_in *) &ia->ip.saddr;
  
  strncpy(buf, inet_ntoa(addr->sin_addr), len);
  buf[len] = '\0';

  return buf;
}



char *isc_gethostname(IscAddress *ia, char *buf, int len)
{
  static char hostname[256];
  struct sockaddr_in *addr;
  struct hostent *hp;
  
195
196
  if (ia == NULL)
  {
Per Cederqvist's avatar
Per Cederqvist committed
197
    return NULL;
198
  }
Per Cederqvist's avatar
Per Cederqvist committed
199
200
201
202
203
204
205
206
207

  if (!buf)
  {
    buf = hostname;
    len = sizeof(hostname)-1;
  }
  
  addr = (struct sockaddr_in *) &ia->ip.saddr;
  
Per Cederqvist's avatar
Per Cederqvist committed
208
  hp = gethostbyaddr((char*)&addr->sin_addr, sizeof(struct in_addr), AF_INET);
Per Cederqvist's avatar
Per Cederqvist committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  if (!hp)
    return NULL;
  
  strncpy(buf, hp->h_name, len);
  buf[len] = '\0';
  
  return buf;
}



int isc_getportnum(IscAddress *ia)
{
  struct sockaddr_in *addr;

  
  addr = (struct sockaddr_in *) &ia->ip.saddr;

  return ntohs(addr->sin_port);
}


char *isc_getservice(IscAddress *ia,
		     char *buf,
		     int len,
		     IscSessionType type)
{
  static char servbuf[256];
  struct sockaddr_in *addr;
  struct servent *sp;
  
  if (!buf)
  {
    buf = servbuf;
    len = sizeof(servbuf)-1;
  }
  
  addr = (struct sockaddr_in *) &ia->ip.saddr;
  
  switch (type)
  {
    case ISC_TYPE_TCP:
      sp = getservbyport(ntohs(addr->sin_port), "tcp");
      break;
      
    case ISC_TYPE_UDP:
      sp = getservbyport(ntohs(addr->sin_port), "udp");
      break;
    
    default:
      return NULL;
  }

  if (!sp)
    return NULL;
  
  strncpy(buf, sp->s_name, len);
  return buf;
}