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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 78 - (hide annotations) (download)
Mon May 17 18:08:07 2004 UTC (19 years, 11 months ago) by capela
File MIME type: text/plain
File size: 8408 byte(s)
Initial alpha release.

1 capela 78 // 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     #else
108    
109     void lscp_socket_perror ( const char *pszPrefix )
110     {
111     perror(pszPrefix);
112     }
113    
114     #endif
115    
116    
117     static void _lscp_socket_getopt_bool ( lscp_socket_t sock, const char *pszOptName, int iOptName )
118     {
119     int iSockOpt;
120     int iSockLen = sizeof(int);
121     char szPrefix[33];
122    
123     sprintf(szPrefix, " %s\t", pszOptName);
124     if (getsockopt(sock, SOL_SOCKET, iOptName, (char *) &iSockOpt, &iSockLen) == SOCKET_ERROR)
125     lscp_socket_perror(szPrefix);
126     else
127     fprintf(stderr, "%s: %s\n", szPrefix, (iSockOpt ? "ON" : "OFF"));
128     }
129    
130     static void _lscp_socket_getopt_int ( lscp_socket_t sock, const char *pszOptName, int iOptName )
131     {
132     int iSockOpt;
133     int iSockLen = sizeof(int);
134     char szPrefix[33];
135    
136     sprintf(szPrefix, " %s\t", pszOptName);
137     if (getsockopt(sock, SOL_SOCKET, iOptName, (char *) &iSockOpt, &iSockLen) == SOCKET_ERROR)
138     lscp_socket_perror(szPrefix);
139     else
140     fprintf(stderr, "%s: %d\n", szPrefix, iSockOpt);
141     }
142    
143     void lscp_socket_getopts ( const char *pszPrefix, lscp_socket_t sock )
144     {
145     fprintf(stderr, "%s: sock=%d:\n", pszPrefix, sock);
146    
147     _lscp_socket_getopt_bool(sock, "SO_BROADCAST", SO_BROADCAST);
148     _lscp_socket_getopt_bool(sock, "SO_DEBUG", SO_DEBUG);
149     #if defined(WIN32)
150     _lscp_socket_getopt_bool(sock, "SO_DONTLINGER", SO_DONTLINGER);
151     #endif
152     _lscp_socket_getopt_bool(sock, "SO_DONTROUTE", SO_DONTROUTE);
153     _lscp_socket_getopt_bool(sock, "SO_KEEPALIVE", SO_KEEPALIVE);
154     _lscp_socket_getopt_bool(sock, "SO_OOBINLINE", SO_OOBINLINE);
155     _lscp_socket_getopt_int (sock, "SO_RCVBUF", SO_RCVBUF);
156     _lscp_socket_getopt_bool(sock, "SO_REUSEADDR", SO_REUSEADDR);
157     _lscp_socket_getopt_int (sock, "SO_SNDBUF", SO_SNDBUF);
158     }
159    
160     void lscp_socket_trace ( const char *pszPrefix, struct sockaddr_in *pAddr, const char *pchBuffer, int cchBuffer )
161     {
162     char *pszBuffer;
163    
164     fprintf(stderr, "%s: addr=%s port=%d:\n",
165     pszPrefix,
166     inet_ntoa(pAddr->sin_addr),
167     htons(pAddr->sin_port)
168     );
169    
170     if (pchBuffer && cchBuffer > 0) {
171     pszBuffer = (char *) malloc(cchBuffer + 1);
172     if (pszBuffer) {
173     memcpy(pszBuffer, pchBuffer, cchBuffer);
174     while (cchBuffer > 0 && (pszBuffer[cchBuffer - 1] == '\n' || pszBuffer[cchBuffer- 1] == '\r'))
175     cchBuffer--;
176     pszBuffer[cchBuffer] = (char) 0;
177     fprintf(stderr, "< %s\n", pszBuffer);
178     free(pszBuffer);
179     }
180     }
181     else fprintf(stderr, "< (null)\n");
182     }
183    
184    
185     //-------------------------------------------------------------------------
186     // Threaded socket agent struct helpers.
187    
188     void lscp_socket_agent_init ( lscp_socket_agent_t *pAgent, lscp_socket_t sock, struct sockaddr_in *pAddr, int cAddr )
189     {
190     memset(pAgent, 0, sizeof(lscp_socket_agent_t));
191    
192     pAgent->sock = sock;
193     memmove((char *) &(pAgent->addr), pAddr, cAddr);
194     pAgent->pThread = NULL;
195     pAgent->iState = 0;
196     }
197    
198    
199     lscp_status_t lscp_socket_agent_start ( lscp_socket_agent_t *pAgent, lscp_thread_proc_t pfnProc, void *pvData, int iDetach )
200     {
201     if (pAgent->iState)
202     pAgent->iState = 0;
203     if (pAgent->pThread)
204     lscp_thread_destroy(pAgent->pThread);
205    
206     pAgent->iState = 1;
207     pAgent->pThread = lscp_thread_create(pfnProc, pvData, iDetach);
208    
209     return (pAgent->pThread == NULL ? LSCP_FAILED : LSCP_OK);
210     }
211    
212    
213     lscp_status_t lscp_socket_agent_join ( lscp_socket_agent_t *pAgent )
214     {
215     lscp_status_t ret = LSCP_FAILED;
216    
217     if (pAgent->pThread)
218     ret = lscp_thread_join(pAgent->pThread);
219    
220     return ret;
221     }
222    
223    
224     lscp_status_t lscp_socket_agent_free ( lscp_socket_agent_t *pAgent )
225     {
226     lscp_status_t ret = LSCP_FAILED;
227    
228     if (pAgent->sock != INVALID_SOCKET)
229     closesocket(pAgent->sock);
230     pAgent->sock = INVALID_SOCKET;
231    
232     if (pAgent->iState)
233     pAgent->iState = 0;
234    
235     if (pAgent->pThread)
236     ret = lscp_thread_destroy(pAgent->pThread);
237     pAgent->pThread = NULL;
238    
239     return ret;
240     }
241    
242    
243     // end of socket.c

  ViewVC Help
Powered by ViewVC