Watt-32 tcp/ip  2.2 dev-rel.10
bsdname.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <sys/socket.h>
12 
13 #include "copyrigh.h"
14 #include "wattcp.h"
15 #include "pcdns.h"
16 #include "pcbuf.h"
17 #include "pctcp.h"
18 #include "pcdbug.h"
19 #include "pcconfig.h"
20 #include "ip6_out.h"
21 #include "misc.h"
22 #include "wdpmi.h"
23 #include "x32vm.h"
24 #include "win_dll.h"
25 #include "powerpak.h"
26 #include "strings.h"
27 #include "bsdname.h"
28 
29 #ifdef __CYGWIN__
30  #define XHOSTID_RETV long
31  #define SETHOSTNAME_ARG2 size_t
32 #else
33  #define XHOSTID_RETV u_long
34  #define SETHOSTNAME_ARG2 int
35 #endif
36 
37 #ifdef __DJGPP__
38  #define GETHOSTNAME_ARG2 int
39 #else
40  #define GETHOSTNAME_ARG2 size_t
41 #endif
42 
43 
44 
49 DWORD W32_CALL _gethostid (void)
50 {
51  return (my_ip_addr);
52 }
53 
58 DWORD W32_CALL _sethostid (DWORD ip)
59 {
60  return (my_ip_addr = ip);
61 }
62 
63 #if defined(USE_BSD_API)
64 
68 XHOSTID_RETV W32_CALL gethostid (void)
69 {
70  if (!netdb_init())
71  return (INADDR_NONE);
72  return htonl (my_ip_addr);
73 }
74 
79 XHOSTID_RETV W32_CALL sethostid (DWORD ip)
80 {
81  return (my_ip_addr = ntohl(ip));
82 }
83 #endif /* USE_BSD_API */
84 
85 #if defined(USE_IPV6)
86 
91 const void *_gethostid6 (void)
92 {
93  if (!netdb_init())
94  return (&in6addr_any);
95  return (&in6addr_my_ip);
96 }
97 
102 void _sethostid6 (const void *addr)
103 {
104  memcpy (&in6addr_my_ip, addr, sizeof(in6addr_my_ip));
105 }
106 #endif /* USE_IPV6 */
107 
112 int W32_CALL _getpeername (const sock_type *s, void *dest, int *len)
113 {
114  struct watt_sockaddr temp;
115  size_t ltemp;
116  int chk = _chk_socket (s);
117 
118  if (!s->tcp.hisaddr || !s->tcp.hisport ||
119  (chk != VALID_UDP && chk != VALID_TCP))
120  {
121  if (len)
122  *len = 0;
123  return (-1);
124  }
125 
126  memset (&temp, 0, sizeof(temp));
127  temp.s_ip = s->tcp.hisaddr;
128  temp.s_port = s->tcp.hisport;
129  temp.s_type = s->tcp.ip_type;
130 
131  /* how much do we move?
132  */
133  ltemp = (len ? *(size_t*)len : sizeof(struct watt_sockaddr));
134  if (ltemp > sizeof (struct watt_sockaddr))
135  ltemp = sizeof (struct watt_sockaddr);
136  memcpy (dest, &temp, ltemp);
137 
138  if (len)
139  *len = (int)ltemp;
140  return (0);
141 }
142 
147 int W32_CALL _getsockname (const sock_type *s, void *dest, int *len)
148 {
149  struct watt_sockaddr temp;
150  size_t ltemp;
151  int chk = _chk_socket (s);
152 
153  if (!s->tcp.myaddr || !s->tcp.myport ||
154  (chk != VALID_UDP && chk != VALID_TCP))
155  {
156  if (len)
157  *len = 0;
158  return (-1);
159  }
160 
161  memset (&temp, 0, sizeof(temp));
162  temp.s_ip = s->tcp.myaddr;
163  temp.s_port = s->tcp.myport;
164  temp.s_type = s->tcp.ip_type;
165 
166  /* how much do we move
167  */
168  ltemp = (len ? *(size_t*)len : sizeof (struct watt_sockaddr));
169  if (ltemp > sizeof (struct watt_sockaddr))
170  ltemp = sizeof (struct watt_sockaddr);
171  memcpy (dest, &temp, ltemp);
172 
173  if (len)
174  *len = (int)ltemp;
175  return (0);
176 }
177 
186 int W32_CALL getdomainname (char *buffer, size_t buflen)
187 {
188  const char *my_domainname = def_domain ? def_domain : "";
189 
190  if (!buffer || buflen < strlen(my_domainname))
191  {
192  SOCK_ERRNO (EINVAL);
193  return (-1);
194  }
195 #if (DOSX)
196  if (!valid_addr(buffer, buflen))
197  {
198  SOCK_ERRNO (EFAULT);
199  return (-1);
200  }
201 #endif
202 
203  strncpy (buffer, my_domainname, buflen);
204  /* no terminating '\0' needs to be forced here per BSD spec */
205  return (0);
206 }
207 
213 int W32_CALL setdomainname (const char *name, size_t len)
214 {
215  if (!name || len > sizeof(defaultdomain)-1)
216  {
217  SOCK_ERRNO (EINVAL);
218  return (-1);
219  }
220 
221 #if (DOSX)
222  if (!valid_addr(name, len))
223  {
224  SOCK_ERRNO (EFAULT);
225  return (-1);
226  }
227 #endif
228 
229  def_domain = _strlcpy (defaultdomain, name, len);
230  return (0);
231 }
232 
233 /*
234  * BSD-style: \n
235  * Make a FQDN from `hostname' & `def_domain'.
236  * Set errno on failure and return -1.
237  */
238 int W32_CALL gethostname (char *buffer, GETHOSTNAME_ARG2 buflen)
239 {
240  /* the FQDN when no hostname has been set is "localhost.localdomain".
241  * the FQDN when a hostname has been set, but no my_domname is set, is 'my_hostname'
242  * the FQDN when both are set is 'my_hostname.my_domname'
243  */
244  const char *my_hostname = "localhost";
245  const char *my_domname = "localdomain";
246  GETHOSTNAME_ARG2 pos;
247 
248  if (!buffer)
249  {
250  SOCK_ERRNO (EINVAL);
251  return (-1);
252  }
253  if (buflen == 0)
254  return (0);
255 
256 #if (DOSX)
257  if (!valid_addr(buffer, buflen))
258  {
259  SOCK_ERRNO (EFAULT);
260  return (-1);
261  }
262 #endif
263 
264  if (*hostname) /* otherwise use localhost.localdomain */
265  {
266  my_hostname = hostname;
267  my_domname = NULL; /* have hostname but no domain name */
268  if (def_domain && *def_domain)
269  my_domname = def_domain;
270  }
271  pos = 0;
272  while (pos < buflen && *my_hostname)
273  buffer[pos++] = *my_hostname++;
274 
275  if (pos < buflen && my_domname)
276  {
277  buffer[pos++] = '.';
278  /*@-nullderef@*/
279  while (pos < buflen && *my_domname)
280  buffer[pos++] = *my_domname++;
281  }
282  if (pos < buflen)
283  buffer[pos] = '\0';
284  return (0);
285 }
286 
293 int W32_CALL sethostname (const char *fqdn, SETHOSTNAME_ARG2 len)
294 {
295  SETHOSTNAME_ARG2 pos;
296 
297  if (!fqdn || !*fqdn || len == 0 || len > MAX_HOSTLEN)
298  {
299  SOCK_ERRNO (EINVAL);
300  return (-1);
301  }
302 
303 #if (DOSX)
304  if (!valid_addr(fqdn, len))
305  {
306  SOCK_ERRNO (EFAULT);
307  return (-1);
308  }
309 #endif
310 
311  pos = 0;
312  while (pos < len && fqdn[pos] != '.')
313  {
314  if (!fqdn[pos])
315  {
316  pos = 0;
317  break;
318  }
319  pos++;
320  }
321  if (pos == 0) /* leading '.' or invalid char in name */
322  {
323  SOCK_ERRNO (EINVAL);
324  return (-1);
325  }
326  if (pos >= SIZEOF(hostname))
327  {
328  SOCK_ERRNO (ERANGE);
329  return (-1);
330  }
331  if (fqdn[pos] == '.') /* a trailing '.' is ok too */
332  {
333  if (setdomainname(&fqdn[pos+1], len-pos) != 0)
334  return (-1);
335  }
336  memcpy (&hostname[0], fqdn, pos);
337  hostname[pos] = '\0';
338  return (0);
339 }
340 
341 
342 #if defined(WIN32)
343 
344 #ifndef ComputerNameDnsHostname
345 #define ComputerNameDnsHostname 1
346 #endif
347 
348 /*
349  * Get hostname excluding domain-name
350  */
351 int _get_machine_name (char *buf, int size)
352 {
353  DWORD sz = size;
354  int rc;
355 
356  if (p_GetComputerNameExA)
357  rc = (*p_GetComputerNameExA) (ComputerNameDnsHostname, buf, &sz);
358  else rc = GetComputerNameA (buf, &sz);
359 
360  rc = rc ? 0 : -1;
361  TCP_CONSOLE_MSG (2, ("_get_machine_name(): \"%s\", rc %d\n", buf, rc));
362  return (rc);
363 }
364 
365 #else
366 
367 /*
368  * From Ralf Brown's interrupt list
369  *
370  * --------D-215E00-----------------------------
371  * INT 21 - DOS 3.1+ network - GET MACHINE NAME
372  * AX = 5E00h
373  * DS:DX -> 16-byte buffer for ASCII machine name
374  * Return: CF clear if successful
375  * CH = validity
376  * 00h name invalid
377  * nonzero valid
378  * CL = NetBIOS number for machine name
379  * DS:DX buffer filled with blank-paded name
380  * CF set on error
381  * AX = error code (01h) (see #1366 at AH=59h)
382  * Note: supported by OS/2 v1.3+ compatibility box, PC-NFS
383  */
384 
385 /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
386 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
387 
392 int _get_machine_name (char *buf, int size)
393 {
394  IREGS reg;
395  char *h;
396  char dosBuf[16];
397  int len;
398 
399  memset (&reg, 0, sizeof(reg));
400  reg.r_ax = 0x5E00;
401 
402 #if (DOSX & DJGPP)
403  if (_go32_info_block.size_of_transfer_buffer < sizeof(dosBuf))
404  return (-1);
405  reg.r_ds = __tb / 16;
406  reg.r_dx = __tb & 15;
407 
408 #elif (DOSX & (PHARLAP|X32VM|POWERPAK))
409  if (_watt_dosTbSize < sizeof(dosBuf) || !_watt_dosTbr)
410  return (-1);
411  reg.r_ds = RP_SEG (_watt_dosTbr);
412  reg.r_dx = RP_OFF (_watt_dosTbr);
413 
414 #elif (DOSX & DOS4GW)
415  if (_watt_dosTbSize < sizeof(dosBuf) || !_watt_dosTbSeg)
416  return (-1);
417  reg.r_ds = _watt_dosTbSeg;
418  reg.r_dx = 0;
419 
420 #elif (DOSX == 0)
421  reg.r_ds = FP_SEG (dosBuf);
422  reg.r_dx = FP_OFF (dosBuf);
423 
424 #else
425  #error Help me!
426 #endif
427 
428  GEN_INTERRUPT (0x21, &reg);
429  if ((reg.r_flags & CARRY_BIT) || hiBYTE(reg.r_cx) == 0)
430  return (-1);
431 
432 #if (DOSX & DJGPP)
433  dosmemget (__tb, sizeof(dosBuf), dosBuf);
434 
435 #elif (DOSX & (PHARLAP|X32VM|POWERPAK))
436  ReadRealMem ((void*)&dosBuf, _watt_dosTbr, sizeof(dosBuf));
437 
438 #elif (DOSX & DOS4GW)
439  memcpy (dosBuf, SEG_OFS_TO_LIN(_watt_dosTbSeg,0), sizeof(dosBuf));
440 #endif
441 
442  h = strrtrim (dosBuf);
443  len = strlen (h);
444  if (len + 1 > size)
445  {
446  SOCK_ERRNO (ERANGE);
447  return (-1);
448  }
449  strcpy (buf, h);
450  strlwr (buf);
451  return (0);
452 }
453 #endif /* WIN32 */
454 
int _get_machine_name(char *buf, int size)
Try asking a LAN extension of DOS for a host-name.
Definition: bsdname.c:351
int W32_CALL _getpeername(const sock_type *s, void *dest, int *len)
Core-style: Return 'watt_sockaddr' for peer in a UDP/TCP connection.
Definition: bsdname.c:112
int W32_CALL _getsockname(const sock_type *s, void *dest, int *len)
Core-style: Return 'watt_sockaddr' for our side of a UDP/TCP connection.
Definition: bsdname.c:147
Core definitions.
char * _strlcpy(char *dst, const char *src, size_t len)
Similar to strncpy(), but always returns 'dst' with 0-termination.
Definition: strings.c:226
const void * _gethostid6(void)
Core-style: Returns local IPv6-address.
Definition: bsdname.c:91
Definition: ip.h:67
XHOSTID_RETV W32_CALL gethostid(void)
BSD style: returns local IPv4-address.
Definition: bsdname.c:68
int W32_CALL sethostname(const char *fqdn, SETHOSTNAME_ARG2 len)
BSD-style: Expects a "Fully Qualified Domain Name" in `fqdn'.
Definition: bsdname.c:293
int W32_CALL setdomainname(const char *name, size_t len)
BSD-style: Set the host's domain name.
Definition: bsdname.c:213
char * strrtrim(char *s)
Trim trailing blanks (space/tab) from a string.
Definition: strings.c:255
int W32_CALL getdomainname(char *buffer, size_t buflen)
BSD-style: Return domain name of this host.
Definition: bsdname.c:186
char hostname[MAX_HOSTLEN+1]
Our configured hostname.
Definition: pctcp.c:59
DWORD my_ip_addr
our IP address
Definition: pctcp.c:70
DWORD W32_CALL _sethostid(DWORD ip)
Core-style: Sets local IPv4-address.
Definition: bsdname.c:58
char defaultdomain[MAX_HOSTLEN+1]
The 2 next variables are loaded from WATTCP.CFG file.
Definition: pcdns.c:71
int netdb_init(void)
Called from most <netdb.h> functions in case watt_sock_init() wasn't called first to initialise things...
Definition: pcconfig.c:1215
Definition: misc.h:511
XHOSTID_RETV W32_CALL sethostid(DWORD ip)
BSD-style: Sets local IPv4-address.
Definition: bsdname.c:79
DWORD W32_CALL _gethostid(void)
Core style: Returns local IPv4-address.
Definition: bsdname.c:49
void _sethostid6(const void *addr)
Core-style: Sets local IPv6-address.
Definition: bsdname.c:102