isc_socket.c 3.68 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
** Copyright (c) 1992 Peter Eriksson and Per Cederqvist of the
Per Cederqvist's avatar
Per Cederqvist committed
5
6
7
8
9
10
11
**                    Lysator Academic Computer Association.
**
** history:
**   920210 pen     initial coding
*/

#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
12
13
14
#ifdef TIME_WITH_SYS_TIME
#  include <time.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
15
16
17
18
19
20
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include <sys/ioctl.h>
21
#include <sys/socket.h>
Per Cederqvist's avatar
Per Cederqvist committed
22
23
#include <errno.h>
#include <netdb.h>
Per Cederqvist's avatar
Per Cederqvist committed
24
25
26
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif
27
28
29
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
30
31
32
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
33
#include <fcntl.h>
34

Per Cederqvist's avatar
Per Cederqvist committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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

49
char *inet_ntoa(ad)
Per Cederqvist's avatar
Per Cederqvist committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
    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;
  
173
174
  if (ia == NULL)
  {
Per Cederqvist's avatar
Per Cederqvist committed
175
    return NULL;
176
  }
Per Cederqvist's avatar
Per Cederqvist committed
177
178
179
180
181
182
183
184
185

  if (!buf)
  {
    buf = hostname;
    len = sizeof(hostname)-1;
  }
  
  addr = (struct sockaddr_in *) &ia->ip.saddr;
  
Per Cederqvist's avatar
Per Cederqvist committed
186
  hp = gethostbyaddr((char*)&addr->sin_addr, sizeof(struct in_addr), AF_INET);
Per Cederqvist's avatar
Per Cederqvist committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
  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;
}