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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 951 by capela, Sun Sep 24 12:55:48 2006 UTC revision 952 by capela, Tue Nov 28 22:46:32 2006 UTC
# Line 30  Line 30 
30    
31  static struct {  static struct {
32    
33      int         iError;          int         iError;
34      const char *pszError;          const char *pszError;
35                
36  } _wsaErrorCodes[] = {  } _wsaErrorCodes[] = {
37    
38      { 0,                  "No error" },          { 0,                  "No error" },
39      { WSAEINTR,           "Interrupted system call" },          { WSAEINTR,           "Interrupted system call" },
40      { WSAEBADF,           "Bad file number" },          { WSAEBADF,           "Bad file number" },
41      { WSAEACCES,          "Permission denied" },          { WSAEACCES,          "Permission denied" },
42      { WSAEFAULT,          "Bad address" },          { WSAEFAULT,          "Bad address" },
43      { WSAEINVAL,          "Invalid argument" },          { WSAEINVAL,          "Invalid argument" },
44      { WSAEMFILE,          "Too many open sockets" },          { WSAEMFILE,          "Too many open sockets" },
45      { WSAEWOULDBLOCK,     "Operation would block" },          { WSAEWOULDBLOCK,     "Operation would block" },
46      { WSAEINPROGRESS,     "Operation now in progress" },          { WSAEINPROGRESS,     "Operation now in progress" },
47      { WSAEALREADY,        "Operation already in progress" },          { WSAEALREADY,        "Operation already in progress" },
48      { WSAENOTSOCK,        "Socket operation on non-socket" },          { WSAENOTSOCK,        "Socket operation on non-socket" },
49      { WSAEDESTADDRREQ,    "Destination address required" },          { WSAEDESTADDRREQ,    "Destination address required" },
50      { WSAEMSGSIZE,        "Message too long" },          { WSAEMSGSIZE,        "Message too long" },
51      { WSAEPROTOTYPE,      "Protocol wrong type for socket" },          { WSAEPROTOTYPE,      "Protocol wrong type for socket" },
52      { WSAENOPROTOOPT,     "Bad protocol option" },          { WSAENOPROTOOPT,     "Bad protocol option" },
53      { WSAEPROTONOSUPPORT, "Protocol not supported" },          { WSAEPROTONOSUPPORT, "Protocol not supported" },
54      { WSAESOCKTNOSUPPORT, "Socket type not supported" },          { WSAESOCKTNOSUPPORT, "Socket type not supported" },
55      { WSAEOPNOTSUPP,      "Operation not supported on socket" },          { WSAEOPNOTSUPP,      "Operation not supported on socket" },
56      { WSAEPFNOSUPPORT,    "Protocol family not supported" },          { WSAEPFNOSUPPORT,    "Protocol family not supported" },
57      { WSAEAFNOSUPPORT,    "Address family not supported" },          { WSAEAFNOSUPPORT,    "Address family not supported" },
58      { WSAEADDRINUSE,      "Address already in use" },          { WSAEADDRINUSE,      "Address already in use" },
59      { WSAEADDRNOTAVAIL,   "Can't assign requested address" },          { WSAEADDRNOTAVAIL,   "Can't assign requested address" },
60      { WSAENETDOWN,        "Network is down" },          { WSAENETDOWN,        "Network is down" },
61      { WSAENETUNREACH,     "Network is unreachable" },          { WSAENETUNREACH,     "Network is unreachable" },
62      { WSAENETRESET,       "Net connection reset" },          { WSAENETRESET,       "Net connection reset" },
63      { WSAECONNABORTED,    "Software caused connection abort" },          { WSAECONNABORTED,    "Software caused connection abort" },
64      { WSAECONNRESET,      "Connection reset by peer" },          { WSAECONNRESET,      "Connection reset by peer" },
65      { WSAENOBUFS,         "No buffer space available" },          { WSAENOBUFS,         "No buffer space available" },
66      { WSAEISCONN,         "Socket is already connected" },          { WSAEISCONN,         "Socket is already connected" },
67      { WSAENOTCONN,        "Socket is not connected" },          { WSAENOTCONN,        "Socket is not connected" },
68      { WSAESHUTDOWN,       "Can't send after socket shutdown" },          { WSAESHUTDOWN,       "Can't send after socket shutdown" },
69      { WSAETOOMANYREFS,    "Too many references, can't splice" },          { WSAETOOMANYREFS,    "Too many references, can't splice" },
70      { WSAETIMEDOUT,       "Connection timed out" },          { WSAETIMEDOUT,       "Connection timed out" },
71      { WSAECONNREFUSED,    "Connection refused" },          { WSAECONNREFUSED,    "Connection refused" },
72      { WSAELOOP,           "Too many levels of symbolic links" },          { WSAELOOP,           "Too many levels of symbolic links" },
73      { WSAENAMETOOLONG,    "File name too long" },          { WSAENAMETOOLONG,    "File name too long" },
74      { WSAEHOSTDOWN,       "Host is down" },          { WSAEHOSTDOWN,       "Host is down" },
75      { WSAEHOSTUNREACH,    "No route to host" },          { WSAEHOSTUNREACH,    "No route to host" },
76      { WSAENOTEMPTY,       "Directory not empty" },          { WSAENOTEMPTY,       "Directory not empty" },
77      { WSAEPROCLIM,        "Too many processes" },          { WSAEPROCLIM,        "Too many processes" },
78      { WSAEUSERS,          "Too many users" },          { WSAEUSERS,          "Too many users" },
79      { WSAEDQUOT,          "Disc quota exceeded" },          { WSAEDQUOT,          "Disc quota exceeded" },
80      { WSAESTALE,          "Stale NFS file handle" },          { WSAESTALE,          "Stale NFS file handle" },
81      { WSAEREMOTE,         "Too many levels of remote in path" },          { WSAEREMOTE,         "Too many levels of remote in path" },
82      { WSASYSNOTREADY,     "Network system is unavailable" },          { WSASYSNOTREADY,     "Network system is unavailable" },
83      { WSAVERNOTSUPPORTED, "Winsock version out of range" },          { WSAVERNOTSUPPORTED, "Winsock version out of range" },
84      { WSANOTINITIALISED,  "WSAStartup not yet called" },          { WSANOTINITIALISED,  "WSAStartup not yet called" },
85      { WSAEDISCON,         "Graceful shutdown in progress" },          { WSAEDISCON,         "Graceful shutdown in progress" },
86      { WSAHOST_NOT_FOUND,  "Host not found" },          { WSAHOST_NOT_FOUND,  "Host not found" },
87      { WSANO_DATA,         "No host data of that type was found" },          { WSANO_DATA,         "No host data of that type was found" },
88      { 0, NULL }          { 0, NULL }
89  };  };
90    
91  void lscp_socket_perror ( const char *pszPrefix )  void lscp_socket_perror ( const char *pszPrefix )
92  {  {
93      int         iError   = WSAGetLastError();          int         iError   = WSAGetLastError();
94      const char *pszError = "Unknown error code";          const char *pszError = "Unknown error code";
95      int         i;          int         i;
96    
97      for (i = 0; _wsaErrorCodes[i].pszError; i++) {          for (i = 0; _wsaErrorCodes[i].pszError; i++) {
98          if (_wsaErrorCodes[i].iError == iError) {                  if (_wsaErrorCodes[i].iError == iError) {
99              pszError = _wsaErrorCodes[i].pszError;                          pszError = _wsaErrorCodes[i].pszError;
100              break;                          break;
101          }                  }
102      }          }
103                
104      fprintf(stderr, "%s: %s (%d)\n", pszPrefix, pszError, iError);          fprintf(stderr, "%s: %s (%d)\n", pszPrefix, pszError, iError);
105  }  }
106    
107  void lscp_socket_herror ( const char *pszPrefix )  void lscp_socket_herror ( const char *pszPrefix )
108  {  {
109      lscp_socket_perror(pszPrefix);          lscp_socket_perror(pszPrefix);
110  }  }
111    
112  #else  #else
113    
114  void lscp_socket_perror ( const char *pszPrefix )  void lscp_socket_perror ( const char *pszPrefix )
115  {  {
116      perror(pszPrefix);          perror(pszPrefix);
117  }  }
118    
119  void lscp_socket_herror ( const char *pszPrefix )  void lscp_socket_herror ( const char *pszPrefix )
120  {  {
121      herror(pszPrefix);          herror(pszPrefix);
122  }  }
123    
124  #endif  #endif
# Line 126  void lscp_socket_herror ( const char *ps Line 126  void lscp_socket_herror ( const char *ps
126    
127  static void _lscp_socket_getopt_bool ( lscp_socket_t sock, const char *pszOptName, int iOptName )  static void _lscp_socket_getopt_bool ( lscp_socket_t sock, const char *pszOptName, int iOptName )
128  {  {
129      int iSockOpt;          int iSockOpt;
130      socklen_t cSockLen = sizeof(int);          socklen_t cSockLen = sizeof(int);
131      char szPrefix[33];          char szPrefix[33];
132    
133      sprintf(szPrefix, "  %s\t", pszOptName);          sprintf(szPrefix, "  %s\t", pszOptName);
134      if (getsockopt(sock, SOL_SOCKET, iOptName, (char *) &iSockOpt, &cSockLen) == SOCKET_ERROR)          if (getsockopt(sock, SOL_SOCKET, iOptName, (char *) &iSockOpt, &cSockLen) == SOCKET_ERROR)
135          lscp_socket_perror(szPrefix);                  lscp_socket_perror(szPrefix);
136      else          else
137          fprintf(stderr, "%s: %s\n", szPrefix, (iSockOpt ? "ON" : "OFF"));                  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 )  static void _lscp_socket_getopt_int  ( lscp_socket_t sock, const char *pszOptName, int iOptName )
141  {  {
142      int iSockOpt;          int iSockOpt;
143      socklen_t cSockLen = sizeof(int);          socklen_t cSockLen = sizeof(int);
144      char szPrefix[33];          char szPrefix[33];
145    
146      sprintf(szPrefix, "  %s\t", pszOptName);          sprintf(szPrefix, "  %s\t", pszOptName);
147      if (getsockopt(sock, SOL_SOCKET, iOptName, (char *) &iSockOpt, &cSockLen) == SOCKET_ERROR)          if (getsockopt(sock, SOL_SOCKET, iOptName, (char *) &iSockOpt, &cSockLen) == SOCKET_ERROR)
148          lscp_socket_perror(szPrefix);                  lscp_socket_perror(szPrefix);
149      else          else
150          fprintf(stderr, "%s: %d\n", szPrefix, iSockOpt);                  fprintf(stderr, "%s: %d\n", szPrefix, iSockOpt);
151  }  }
152    
153  void lscp_socket_getopts ( const char *pszPrefix, lscp_socket_t sock )  void lscp_socket_getopts ( const char *pszPrefix, lscp_socket_t sock )
154  {  {
155      fprintf(stderr, "%s: sock=%d:\n", pszPrefix, sock);          fprintf(stderr, "%s: sock=%d:\n", pszPrefix, sock);
156                
157      _lscp_socket_getopt_bool(sock, "SO_BROADCAST",  SO_BROADCAST);          _lscp_socket_getopt_bool(sock, "SO_BROADCAST",  SO_BROADCAST);
158      _lscp_socket_getopt_bool(sock, "SO_DEBUG",      SO_DEBUG);          _lscp_socket_getopt_bool(sock, "SO_DEBUG",      SO_DEBUG);
159  #if defined(WIN32)  #if defined(WIN32)
160      _lscp_socket_getopt_bool(sock, "SO_DONTLINGER", SO_DONTLINGER);          _lscp_socket_getopt_bool(sock, "SO_DONTLINGER", SO_DONTLINGER);
161  #endif  #endif
162      _lscp_socket_getopt_bool(sock, "SO_DONTROUTE",  SO_DONTROUTE);          _lscp_socket_getopt_bool(sock, "SO_DONTROUTE",  SO_DONTROUTE);
163      _lscp_socket_getopt_bool(sock, "SO_KEEPALIVE",  SO_KEEPALIVE);          _lscp_socket_getopt_bool(sock, "SO_KEEPALIVE",  SO_KEEPALIVE);
164      _lscp_socket_getopt_bool(sock, "SO_OOBINLINE",  SO_OOBINLINE);          _lscp_socket_getopt_bool(sock, "SO_OOBINLINE",  SO_OOBINLINE);
165      _lscp_socket_getopt_int (sock, "SO_RCVBUF",     SO_RCVBUF);          _lscp_socket_getopt_int (sock, "SO_RCVBUF",     SO_RCVBUF);
166      _lscp_socket_getopt_bool(sock, "SO_REUSEADDR",  SO_REUSEADDR);          _lscp_socket_getopt_bool(sock, "SO_REUSEADDR",  SO_REUSEADDR);
167      _lscp_socket_getopt_int (sock, "SO_SNDBUF",     SO_SNDBUF);          _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 )  void lscp_socket_trace ( const char *pszPrefix, struct sockaddr_in *pAddr, const char *pchBuffer, int cchBuffer )
171  {  {
172      char *pszBuffer;          char *pszBuffer;
173    
174      fprintf(stderr, "%s: addr=%s port=%d:\n",          fprintf(stderr, "%s: addr=%s port=%d:\n",
175          pszPrefix,                  pszPrefix,
176          inet_ntoa(pAddr->sin_addr),                  inet_ntoa(pAddr->sin_addr),
177          htons(pAddr->sin_port)                  htons(pAddr->sin_port)
178      );          );
179                
180      if (pchBuffer && cchBuffer > 0) {          if (pchBuffer && cchBuffer > 0) {
181          pszBuffer = (char *) malloc(cchBuffer + 1);                  pszBuffer = (char *) malloc(cchBuffer + 1);
182          if (pszBuffer) {                  if (pszBuffer) {
183              memcpy(pszBuffer, pchBuffer, cchBuffer);                          memcpy(pszBuffer, pchBuffer, cchBuffer);
184              while (cchBuffer > 0 && (pszBuffer[cchBuffer - 1] == '\n' || pszBuffer[cchBuffer- 1] == '\r'))                          while (cchBuffer > 0 && (pszBuffer[cchBuffer - 1] == '\n' || pszBuffer[cchBuffer- 1] == '\r'))
185                  cchBuffer--;                                  cchBuffer--;
186              pszBuffer[cchBuffer] = (char) 0;                          pszBuffer[cchBuffer] = (char) 0;
187              fprintf(stderr, "< %s\n", pszBuffer);                          fprintf(stderr, "< %s\n", pszBuffer);
188              free(pszBuffer);                          free(pszBuffer);
189          }                  }
190      }          }
191      else fprintf(stderr, "< (null)\n");          else fprintf(stderr, "< (null)\n");
192  }  }
193    
194    
# Line 197  void lscp_socket_trace ( const char *psz Line 197  void lscp_socket_trace ( const char *psz
197    
198  void lscp_socket_agent_init ( lscp_socket_agent_t *pAgent, lscp_socket_t sock, struct sockaddr_in *pAddr, int cAddr )  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));          memset(pAgent, 0, sizeof(lscp_socket_agent_t));
201    
202      pAgent->sock = sock;          pAgent->sock = sock;
203      pAgent->pThread = NULL;          pAgent->pThread = NULL;
204      pAgent->iState = 0;          pAgent->iState = 0;
205    
206      if (pAddr)          if (pAddr)
207          memmove((char *) &(pAgent->addr), pAddr, cAddr);                  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 )  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)          if (pAgent->iState)
214          pAgent->iState = 0;                  pAgent->iState = 0;
215      if (pAgent->pThread)          if (pAgent->pThread)
216          lscp_thread_destroy(pAgent->pThread);                  lscp_thread_destroy(pAgent->pThread);
217    
218      pAgent->iState  = 1;          pAgent->iState  = 1;
219      pAgent->pThread = lscp_thread_create(pfnProc, pvData, iDetach);          pAgent->pThread = lscp_thread_create(pfnProc, pvData, iDetach);
220    
221      return (pAgent->pThread == NULL ? LSCP_FAILED : LSCP_OK);          return (pAgent->pThread == NULL ? LSCP_FAILED : LSCP_OK);
222  }  }
223    
224    
225  lscp_status_t lscp_socket_agent_join ( lscp_socket_agent_t *pAgent )  lscp_status_t lscp_socket_agent_join ( lscp_socket_agent_t *pAgent )
226  {  {
227      lscp_status_t ret = LSCP_FAILED;          lscp_status_t ret = LSCP_FAILED;
228    
229      if (pAgent->pThread)          if (pAgent->pThread)
230          ret = lscp_thread_join(pAgent->pThread);                  ret = lscp_thread_join(pAgent->pThread);
231    
232      return ret;          return ret;
233  }  }
234    
235    
236  lscp_status_t lscp_socket_agent_free ( lscp_socket_agent_t *pAgent )  lscp_status_t lscp_socket_agent_free ( lscp_socket_agent_t *pAgent )
237  {  {
238      lscp_status_t ret = LSCP_FAILED;          lscp_status_t ret = LSCP_FAILED;
239    
240      if (pAgent->iState)          if (pAgent->iState)
241          pAgent->iState = 0;                  pAgent->iState = 0;
242    
243      if (pAgent->sock != INVALID_SOCKET)          if (pAgent->sock != INVALID_SOCKET)
244          closesocket(pAgent->sock);                  closesocket(pAgent->sock);
245      pAgent->sock = INVALID_SOCKET;          pAgent->sock = INVALID_SOCKET;
246    
247      if (pAgent->pThread)          if (pAgent->pThread)
248          ret = lscp_thread_destroy(pAgent->pThread);                  ret = lscp_thread_destroy(pAgent->pThread);
249      pAgent->pThread = NULL;          pAgent->pThread = NULL;
250    
251      return ret;          return ret;
252  }  }
253    
254    

Legend:
Removed from v.951  
changed lines
  Added in v.952

  ViewVC Help
Powered by ViewVC