This is Unofficial EPICS BASE Doxygen Site
osdSock.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE Versions 3.13.7
7 * and higher are distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 
11 /*
12  * WIN32 specific initialisation for bsd sockets,
13  * based on Chris Timossi's base/src/ca/windows_depend.c,
14  * and also further additions by Kay Kasemir when this was in
15  * dllmain.cc
16  *
17  * 7-1-97 -joh-
18  *
19  */
20 
21 /*
22  * ANSI C
23  */
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 /*
28  * WIN32 specific
29  */
30 #define VC_EXTRALEAN
31 #define STRICT
32 #include <winsock2.h>
33 
34 #include "osiSock.h"
35 #include "errlog.h"
36 #include "epicsVersion.h"
37 
38 static unsigned nAttached = 0;
39 static WSADATA WsaData; /* version of winsock */
40 
41 LIBCOM_API unsigned epicsStdCall wsaMajorVersion ()
42 {
43  return (unsigned) LOBYTE( WsaData.wVersion );
44 }
45 
46 /*
47  * osiSockAttach()
48  */
49 LIBCOM_API int epicsStdCall osiSockAttach()
50 {
51  int status;
52 
53  if (nAttached) {
54  nAttached++;
55  return TRUE;
56  }
57 
58 #if _DEBUG
59  /* for gui applications, setup console for error messages */
60  if (AllocConsole())
61  {
62  char title[256];
63  DWORD titleLength = GetConsoleTitle(title, sizeof(title));
64  if (titleLength) {
65  titleLength = strlen (title);
66  strncat (title, " " EPICS_VERSION_STRING, sizeof(title));
67  }
68  else {
69  strncpy(title, EPICS_VERSION_STRING, sizeof(title));
70  }
71  title[sizeof(title)-1]= '\0';
72  SetConsoleTitle(title);
73  freopen( "CONOUT$", "a", stderr );
74  }
75 #endif
76 
77  /*
78  * attach to win sock
79  */
80  status = WSAStartup(MAKEWORD(/*major*/2,/*minor*/2), &WsaData);
81  if (status != 0) {
82  fprintf(stderr,
83  "Unable to attach to windows sockets version 2. error=%d\n", status);
84  fprintf(stderr,
85  "A Windows Sockets II update for windows 95 is available at\n");
86  fprintf(stderr,
87  "http://www.microsoft.com/windows95/info/ws2.htm");
88  return FALSE;
89  }
90 
91 # if defined ( _DEBUG ) && 0
92  fprintf(stderr, "EPICS attached to winsock version %s\n", WsaData.szDescription);
93 # endif
94 
95  nAttached = 1u;
96 
97  return TRUE;
98 }
99 
100 /*
101  * osiSockRelease()
102  */
103 LIBCOM_API void epicsStdCall osiSockRelease()
104 {
105  if (nAttached) {
106  if (--nAttached==0u) {
107  WSACleanup();
108 # if defined ( _DEBUG ) && 0
109  fprintf(stderr, "EPICS released winsock version %s\n", WsaData.szDescription);
110 # endif
111  memset (&WsaData, '\0', sizeof(WsaData));
112  }
113  }
114 }
115 
116 LIBCOM_API SOCKET epicsStdCall epicsSocketCreate (
117  int domain, int type, int protocol )
118 {
119  return socket ( domain, type, protocol );
120 }
121 
122 LIBCOM_API int epicsStdCall epicsSocketAccept (
123  int sock, struct sockaddr * pAddr, osiSocklen_t * addrlen )
124 {
125  return accept ( sock, pAddr, addrlen );
126 }
127 
128 LIBCOM_API void epicsStdCall epicsSocketDestroy ( SOCKET s )
129 {
130  int status = closesocket ( s );
131  if ( status < 0 ) {
132  char buf [ 64 ];
133  epicsSocketConvertErrnoToString ( buf, sizeof ( buf ) );
134  errlogPrintf (
135  "epicsSocketDestroy: failed to "
136  "close a socket because \"%s\"\n", buf );
137  }
138 }
139 
140 /*
141  * ipAddrToHostName
142  */
143 LIBCOM_API unsigned epicsStdCall ipAddrToHostName
144  (const struct in_addr *pAddr, char *pBuf, unsigned bufSize)
145 {
146  struct hostent *ent;
147 
148  if (bufSize<1) {
149  return 0;
150  }
151 
152  ent = gethostbyaddr((char *) pAddr, sizeof (*pAddr), AF_INET);
153  if (ent) {
154  strncpy (pBuf, ent->h_name, bufSize);
155  pBuf[bufSize-1] = '\0';
156  return strlen (pBuf);
157  }
158  return 0;
159 }
160 
161 /*
162  * hostToIPAddr ()
163  */
164 LIBCOM_API int epicsStdCall hostToIPAddr
165  (const char *pHostName, struct in_addr *pIPA)
166 {
167  struct hostent *phe;
168 
169  phe = gethostbyname (pHostName);
170  if (phe && phe->h_addr_list[0]) {
171  if (phe->h_addrtype==AF_INET && phe->h_length<=sizeof(struct in_addr)) {
172  struct in_addr *pInAddrIn = (struct in_addr *) phe->h_addr_list[0];
173 
174  *pIPA = *pInAddrIn;
175 
176  /*
177  * success
178  */
179  return 0;
180  }
181  }
182 
183  /*
184  * return indicating an error
185  */
186  return -1;
187 }
188 
189 
LIBCOM_API void epicsStdCall epicsSocketDestroy(SOCKET s)
Definition: osdSock.c:117
#define FALSE
Definition: dbDefs.h:32
pvd::Status status
int osiSocklen_t
Definition: osdSock.h:36
void osiSockRelease()
Definition: osdSock.c:62
pvd::StructureConstPtr type
LIBCOM_API int epicsStdCall hostToIPAddr(const char *pHostName, struct in_addr *pIPA)
Definition: osdSock.c:162
void epicsSocketConvertErrnoToString(char *pBuf, unsigned bufSize)
LIBCOM_API SOCKET epicsStdCall epicsSocketCreate(int domain, int type, int protocol)
Definition: osdSock.c:71
LIBCOM_API unsigned epicsStdCall wsaMajorVersion()
Definition: osdSock.c:41
LIBCOM_API int epicsStdCall epicsSocketAccept(int sock, struct sockaddr *pAddr, osiSocklen_t *addrlen)
Definition: osdSock.c:94
int SOCKET
Definition: osdSock.h:31
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
#define TRUE
Definition: dbDefs.h:27
int osiSockAttach()
Definition: osdSock.c:54
#define stderr
Definition: epicsStdio.h:32
LIBCOM_API unsigned epicsStdCall ipAddrToHostName(const struct in_addr *pAddr, char *pBuf, unsigned bufSize)
Definition: osdSock.c:136