/[svn]/liblscp/trunk/src/socket.c
ViewVC logotype

Contents of /liblscp/trunk/src/socket.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 178 - (show annotations) (download)
Tue Jul 6 15:52:25 2004 UTC (19 years, 8 months ago) by capela
File MIME type: text/plain
File size: 8596 byte(s)
Slight change to thread join termination code.

1 // socket.c
2 //
3 /****************************************************************************
4 liblscp - LinuxSampler Control Protocol API
5 Copyright (C) 2004, rncbc aka Rui Nuno Capela. All rights reserved.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21 *****************************************************************************/
22
23 #include "lscp/socket.h"
24
25
26 //-------------------------------------------------------------------------
27 // Socket info debugging.
28
29 #if defined(WIN32)
30
31 static struct {
32
33 int iError;
34 const char *pszError;
35
36 } _wsaErrorCodes[] = {
37
38 { 0, "No error" },
39 { WSAEINTR, "Interrupted system call" },
40 { WSAEBADF, "Bad file number" },
41 { WSAEACCES, "Permission denied" },
42 { WSAEFAULT, "Bad address" },
43 { WSAEINVAL, "Invalid argument" },
44 { WSAEMFILE, "Too many open sockets" },
45 { WSAEWOULDBLOCK, "Operation would block" },
46 { WSAEINPROGRESS, "Operation now in progress" },
47 { WSAEALREADY, "Operation already in progress" },
48 { WSAENOTSOCK, "Socket operation on non-socket" },
49 { WSAEDESTADDRREQ, "Destination address required" },
50 { WSAEMSGSIZE, "Message too long" },
51 { WSAEPROTOTYPE, "Protocol wrong type for socket" },
52 { WSAENOPROTOOPT, "Bad protocol option" },
53 { WSAEPROTONOSUPPORT, "Protocol not supported" },
54 { WSAESOCKTNOSUPPORT, "Socket type not supported" },
55 { WSAEOPNOTSUPP, "Operation not supported on socket" },
56 { WSAEPFNOSUPPORT, "Protocol family not supported" },
57 { WSAEAFNOSUPPORT, "Address family not supported" },
58 { WSAEADDRINUSE, "Address already in use" },
59 { WSAEADDRNOTAVAIL, "Can't assign requested address" },
60 { WSAENETDOWN, "Network is down" },
61 { WSAENETUNREACH, "Network is unreachable" },
62 { WSAENETRESET, "Net connection reset" },
63 { WSAECONNABORTED, "Software caused connection abort" },
64 { WSAECONNRESET, "Connection reset by peer" },
65 { WSAENOBUFS, "No buffer space available" },
66 { WSAEISCONN, "Socket is already connected" },
67 { WSAENOTCONN, "Socket is not connected" },
68 { WSAESHUTDOWN, "Can't send after socket shutdown" },
69 { WSAETOOMANYREFS, "Too many references, can't splice" },
70 { WSAETIMEDOUT, "Connection timed out" },
71 { WSAECONNREFUSED, "Connection refused" },
72 { WSAELOOP, "Too many levels of symbolic links" },
73 { WSAENAMETOOLONG, "File name too long" },
74 { WSAEHOSTDOWN, "Host is down" },
75 { WSAEHOSTUNREACH, "No route to host" },
76 { WSAENOTEMPTY, "Directory not empty" },
77 { WSAEPROCLIM, "Too many processes" },
78 { WSAEUSERS, "Too many users" },
79 { WSAEDQUOT, "Disc quota exceeded" },
80 { WSAESTALE, "Stale NFS file handle" },
81 { WSAEREMOTE, "Too many levels of remote in path" },
82 { WSASYSNOTREADY, "Network system is unavailable" },
83 { WSAVERNOTSUPPORTED, "Winsock version out of range" },
84 { WSANOTINITIALISED, "WSAStartup not yet called" },
85 { WSAEDISCON, "Graceful shutdown in progress" },
86 { WSAHOST_NOT_FOUND, "Host not found" },
87 { WSANO_DATA, "No host data of that type was found" },
88 { 0, NULL }
89 };
90
91 void lscp_socket_perror ( const char *pszPrefix )
92 {
93 int iError = WSAGetLastError();
94 const char *pszError = "Unknown error code";
95 int i;
96
97 for (i = 0; _wsaErrorCodes[i].pszError; i++) {
98 if (_wsaErrorCodes[i].iError == iError) {
99 pszError = _wsaErrorCodes[i].pszError;
100 break;
101 }
102 }
103
104 fprintf(stderr, "%s: %s (%d)\n", pszPrefix, pszError, iError);
105 }
106
107 void lscp_socket_herror ( const char *pszPrefix )
108 {
109 lscp_socket_perror(pszPrefix);
110 }
111
112 #else
113
114 void lscp_socket_perror ( const char *pszPrefix )
115 {
116 perror(pszPrefix);
117 }
118
119 void lscp_socket_herror ( const char *pszPrefix )
120 {
121 herror(pszPrefix);
122 }
123
124 #endif
125
126
127 static void _lscp_socket_getopt_bool ( lscp_socket_t sock, const char *pszOptName, int iOptName )
128 {
129 int iSockOpt;
130 int iSockLen = sizeof(int);
131 char szPrefix[33];
132
133 sprintf(szPrefix, " %s\t", pszOptName);
134 if (getsockopt(sock, SOL_SOCKET, iOptName, (char *) &iSockOpt, &iSockLen) == SOCKET_ERROR)
135 lscp_socket_perror(szPrefix);
136 else
137 fprintf(stderr, "%s: %s\n", szPrefix, (iSockOpt ? "ON" : "OFF"));
138 }
139
140 static void _lscp_socket_getopt_int ( lscp_socket_t sock, const char *pszOptName, int iOptName )
141 {
142 int iSockOpt;
143 int iSockLen = sizeof(int);
144 char szPrefix[33];
145
146 sprintf(szPrefix, " %s\t", pszOptName);
147 if (getsockopt(sock, SOL_SOCKET, iOptName, (char *) &iSockOpt, &iSockLen) == SOCKET_ERROR)
148 lscp_socket_perror(szPrefix);
149 else
150 fprintf(stderr, "%s: %d\n", szPrefix, iSockOpt);
151 }
152
153 void lscp_socket_getopts ( const char *pszPrefix, lscp_socket_t sock )
154 {
155 fprintf(stderr, "%s: sock=%d:\n", pszPrefix, sock);
156
157 _lscp_socket_getopt_bool(sock, "SO_BROADCAST", SO_BROADCAST);
158 _lscp_socket_getopt_bool(sock, "SO_DEBUG", SO_DEBUG);
159 #if defined(WIN32)
160 _lscp_socket_getopt_bool(sock, "SO_DONTLINGER", SO_DONTLINGER);
161 #endif
162 _lscp_socket_getopt_bool(sock, "SO_DONTROUTE", SO_DONTROUTE);
163 _lscp_socket_getopt_bool(sock, "SO_KEEPALIVE", SO_KEEPALIVE);
164 _lscp_socket_getopt_bool(sock, "SO_OOBINLINE", SO_OOBINLINE);
165 _lscp_socket_getopt_int (sock, "SO_RCVBUF", SO_RCVBUF);
166 _lscp_socket_getopt_bool(sock, "SO_REUSEADDR", SO_REUSEADDR);
167 _lscp_socket_getopt_int (sock, "SO_SNDBUF", SO_SNDBUF);
168 }
169
170 void lscp_socket_trace ( const char *pszPrefix, struct sockaddr_in *pAddr, const char *pchBuffer, int cchBuffer )
171 {
172 char *pszBuffer;
173
174 fprintf(stderr, "%s: addr=%s port=%d:\n",
175 pszPrefix,
176 inet_ntoa(pAddr->sin_addr),
177 htons(pAddr->sin_port)
178 );
179
180 if (pchBuffer && cchBuffer > 0) {
181 pszBuffer = (char *) malloc(cchBuffer + 1);
182 if (pszBuffer) {
183 memcpy(pszBuffer, pchBuffer, cchBuffer);
184 while (cchBuffer > 0 && (pszBuffer[cchBuffer - 1] == '\n' || pszBuffer[cchBuffer- 1] == '\r'))
185 cchBuffer--;
186 pszBuffer[cchBuffer] = (char) 0;
187 fprintf(stderr, "< %s\n", pszBuffer);
188 free(pszBuffer);
189 }
190 }
191 else fprintf(stderr, "< (null)\n");
192 }
193
194
195 //-------------------------------------------------------------------------
196 // Threaded socket agent struct helpers.
197
198 void lscp_socket_agent_init ( lscp_socket_agent_t *pAgent, lscp_socket_t sock, struct sockaddr_in *pAddr, int cAddr )
199 {
200 memset(pAgent, 0, sizeof(lscp_socket_agent_t));
201
202 pAgent->sock = sock;
203 pAgent->pThread = NULL;
204 pAgent->iState = 0;
205
206 if (pAddr)
207 memmove((char *) &(pAgent->addr), pAddr, cAddr);
208 }
209
210
211 lscp_status_t lscp_socket_agent_start ( lscp_socket_agent_t *pAgent, lscp_thread_proc_t pfnProc, void *pvData, int iDetach )
212 {
213 if (pAgent->iState)
214 pAgent->iState = 0;
215 if (pAgent->pThread)
216 lscp_thread_destroy(pAgent->pThread);
217
218 pAgent->iState = 1;
219 pAgent->pThread = lscp_thread_create(pfnProc, pvData, iDetach);
220
221 return (pAgent->pThread == NULL ? LSCP_FAILED : LSCP_OK);
222 }
223
224
225 lscp_status_t lscp_socket_agent_join ( lscp_socket_agent_t *pAgent )
226 {
227 lscp_status_t ret = LSCP_FAILED;
228
229 if (pAgent->pThread)
230 ret = lscp_thread_join(pAgent->pThread);
231
232 return ret;
233 }
234
235
236 lscp_status_t lscp_socket_agent_free ( lscp_socket_agent_t *pAgent )
237 {
238 lscp_status_t ret = LSCP_FAILED;
239
240 if (pAgent->iState)
241 pAgent->iState = 0;
242
243 if (pAgent->sock != INVALID_SOCKET)
244 closesocket(pAgent->sock);
245 pAgent->sock = INVALID_SOCKET;
246
247 if (pAgent->pThread)
248 ret = lscp_thread_destroy(pAgent->pThread);
249 pAgent->pThread = NULL;
250
251 return ret;
252 }
253
254
255 // end of socket.c

  ViewVC Help
Powered by ViewVC