isc_socket.c 4.06 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, 1993 Peter Eriksson and Per Cederqvist of the
Per Cederqvist's avatar
Per Cederqvist committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
**                    Lysator Academic Computer Association.
**
** history:
**   920210 pen     initial coding
*/

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

Per Cederqvist's avatar
Per Cederqvist committed
33
34
35
36
37
38
39
40
41
42

#include "isc.h"
#include "intern.h"



/*
 * External function declarations.
 */
extern void *bzero(void *buf, unsigned length);
Per Cederqvist's avatar
Per Cederqvist committed
43
/*extern void *memcpy(void *, const void *, int);*/
44
45
/*extern int getsockname(int fd, struct sockaddr *, int *);*/
/*extern int getpeername(int fd, struct sockaddr *, int *);*/
Per Cederqvist's avatar
Per Cederqvist committed
46
extern time_t time(time_t *buf);
47
/*extern int select(int setsize,
Per Cederqvist's avatar
Per Cederqvist committed
48
		  fd_set *rs, fd_set *rw, fd_set *es,
49
50
		  struct timeval *wait);*/
/*extern int accept(int fd, struct sockaddr *sock, int *len);*/
Per Cederqvist's avatar
Per Cederqvist committed
51
52
53
54
55
56
57
58
59
60
61
62



#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

63
char *inet_ntoa(ad)
Per Cederqvist's avatar
Per Cederqvist committed
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
246
247
248
249
250
251
252
253
254
255
    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;
  

  if (!buf)
  {
    buf = hostname;
    len = sizeof(hostname)-1;
  }
  
  addr = (struct sockaddr_in *) &ia->ip.saddr;
  
  hp = gethostbyaddr(&addr->sin_addr,sizeof(struct in_addr),AF_INET);
  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;
}