📄 ospsocket.c
字号:
/**########################################################################*########################################################################*########################################################################* * COPYRIGHT (c) 1998, 1999 by TransNexus, LLC * * This software contains proprietary and confidential information * of TransNexus, LLC. Except as may be set forth in the license * agreement under which this software is supplied, use, disclosure, * or reproduction is prohibited without the prior, express, written* consent of TransNexus, LLC. * * $Date: 2000/12/05 00:46:44 $** $Id: ospsocket.c,v 1.1 2000/12/05 00:46:44 wbreu Exp $** $RCSfile: ospsocket.c,v $** $Revision: 1.1 $** $Source: /cvsroot/vocal/contrib/OSPToolkit-2.5.2/src/ospsocket.c,v $**#########################################################################*#########################################################################*#########################################################################*//* * ospsocket.cpp - Socket functions. */#include "osp.h"#include "ospconfig.h"#include "ospsocket.h"#include "osputils.h"#include "ospcomm.h"#include "osphttp.h"#include "ospssl.h"intOSPPSockClose( OSPTBOOL ospvGracefulSSLShutdown, OSPTSOCKET *ospvSockFd, OSPTSSLSESSION **SSLSession){ int tmperror = OSPC_ERR_NO_ERROR; OSPM_DBGENTER(("ENTER: OSPPSockClose()\n")); /* delete the SSL context if it's still hanging around */ OSPPSSLSessionDelete(ospvGracefulSSLShutdown, SSLSession); if (*ospvSockFd != OSPC_SOCK_INVALID) { OSPM_CLOSE(*ospvSockFd, tmperror); *ospvSockFd = OSPC_SOCK_INVALID; } OSPM_DBGEXIT(("EXIT : OSPPSockClose()\n")); return OSPC_ERR_NO_ERROR;}intOSPPSockConnect( OSPTSOCKET *ospvSockFd, OSPTBOOL ospvBlocking, OSPTIPADDR ospvIpAddr, unsigned short ospvPort, struct timeval *ospvTimeout, OSPTSSLSESSION **ospvSSLSession){ int errorcode = OSPC_ERR_NO_ERROR; char *ipstr = OSPC_OSNULL; /* * set the connection socket. * This will also affect reads and writes. */ errorcode = OSPPSockSetBlockingIO(*ospvSockFd, ospvBlocking); /* * attempt connection */ if (errorcode == OSPC_ERR_NO_ERROR) { OSPM_CONNECT(*ospvSockFd, ospvIpAddr, ospvPort, errorcode); if ((errorcode != OSPC_ERR_NO_ERROR) && (errorcode != OSPC_ERR_SOCK_CONN_IN_PROGRESS)) { errorcode = OSPC_ERR_SOCK_CONNECT_FAILED; OSPM_INET_NTOA(ospvIpAddr, ipstr); OSPM_DBGERROR(("connect to %s failed, error = %d\n", ipstr, errorcode)); OSPPSockClose(OSPC_FALSE, ospvSockFd, ospvSSLSession); } else { /* * use the configured timeout to limit how * long to wait for a connection */ errorcode = OSPPSockWaitTillReady(*ospvSockFd, OSPC_FALSE, ospvTimeout); } if (errorcode != OSPC_ERR_NO_ERROR) { errorcode = OSPC_ERR_SOCK_CONNECT_FAILED; OSPM_INET_NTOA(ospvIpAddr, ipstr); OSPM_DBGERROR(("connect to %s failed, error = %d\n", ipstr, errorcode)); OSPPSockClose(OSPC_FALSE, ospvSockFd, ospvSSLSession); } } return errorcode;}intOSPPSockConnectAuditURL( OSPTHTTP *ospvHttp, OSPTBOOL *ospvConnected){ int errorcode = OSPC_ERR_NO_ERROR; OSPTCOMM *comm = OSPC_OSNULL; struct timeval timeout; unsigned socktimeout = 0; OSPM_DBGENTER(("ENTER: OSPPSockConnectAuditURL()\n")); if (ospvHttp == (OSPTHTTP *)OSPC_OSNULL) { errorcode = OSPC_ERR_HTTP_INVALID_ARG; } /* * see if the socket is or has been connected. If so, shut it * down now. */ if (errorcode == OSPC_ERR_NO_ERROR) { if (ospvHttp->SockFd != OSPC_SOCK_INVALID) { errorcode = OSPPSockClose(OSPC_FALSE, &(ospvHttp->SockFd), &(ospvHttp->SSLSession)); if (errorcode != OSPC_ERR_NO_ERROR) { errorcode = OSPC_ERR_SOCK_CLOSE_FAILED; OSPM_DBGERRORLOG(errorcode, "cannot close socket"); } } } if (errorcode == OSPC_ERR_NO_ERROR) { /* * create a new socket */ errorcode = OSPPSockNew(ospvHttp); if (errorcode != OSPC_ERR_NO_ERROR) { errorcode = OSPC_ERR_SOCK_CREATE_FAILED; OSPM_DBGERRORLOG(errorcode, "cannot create socket"); } else { comm = (OSPTCOMM *)ospvHttp->Comm; /* get auditURL svcpt */ ospvHttp->ServicePoint = comm->AuditURL; /* * perform actual connection attempt */ OSPM_DBGNET(("connecting to %s%s", ospvHttp->ServicePoint->HostName, ospvHttp->ServicePoint->URI)); if (ospvHttp->ServicePoint != OSPC_OSNULL) { ospvHttp->Flags = (unsigned char) (ospvHttp->Flags & OSPC_SOCK_CONNECTED_MASK); /* * set the response timeout for the socket */ OSPPCommGetTimeout((OSPTCOMM *)ospvHttp->Comm, &socktimeout); timeout.tv_sec = socktimeout / 1000; timeout.tv_usec = socktimeout % 1000 * 1000; /* ** CHANGED OSPCFALSE -> OSPCTRUE in Sock conenct experimental */ errorcode = OSPPSockConnect(&(ospvHttp->SockFd), OSPC_DEFAULT_BLOCKING_FLAG, ospvHttp->ServicePoint->IpAddr, ospvHttp->ServicePoint->Port, &timeout, &(ospvHttp->SSLSession)); if(errorcode == OSPC_ERR_NO_ERROR) { if (errorcode == OSPC_ERR_NO_ERROR) { /* * begin an SSL Session if a secured connection * is required */ errorcode = OSPPSSLSessionNew(ospvHttp, comm->Security); if (errorcode == OSPC_ERR_NO_ERROR) { *ospvConnected = OSPC_TRUE; ospvHttp->Flags |= OSPC_SOCK_CONNECTED_BIT; } else { OSPPSockClose(OSPC_FALSE, &(ospvHttp->SockFd), &(ospvHttp->SSLSession)); } } } } else { errorcode = OSPC_ERR_COMM_NO_SVC_PTS_AVAIL; OSPM_DBGERRORLOG(errorcode, "no svc pt avail"); } } } OSPM_DBGEXIT(("EXIT : OSPPSockConnectAuditURL() (%d)\n", errorcode)); return errorcode;}intOSPPSockConnectServicePoint( OSPTHTTP *ospvHttp, OSPTBOOL *ospvRollover, OSPTBOOL *ospvConnected){ unsigned numsvcpts = 0, socktimeout = 0; OSPTSVCPT *svcptlist = OSPC_OSNULL, *svcptitem = OSPC_OSNULL; int errorcode = OSPC_ERR_NO_ERROR; struct timeval timeout; OSPTCOMM *comm = OSPC_OSNULL; OSPM_DBGENTER(("ENTER: OSPPSockConnectServicePoint()\n")); if (ospvHttp == (OSPTHTTP *)OSPC_OSNULL) { errorcode = OSPC_ERR_HTTP_INVALID_ARG; } /* * see if the socket is or has been connected. If so, shut it * down now. */ if (errorcode == OSPC_ERR_NO_ERROR) { if (ospvHttp->SockFd != OSPC_SOCK_INVALID) { errorcode = OSPPSockClose(OSPC_FALSE, &(ospvHttp->SockFd), &(ospvHttp->SSLSession)); if (errorcode != OSPC_ERR_NO_ERROR) { errorcode = OSPC_ERR_SOCK_CLOSE_FAILED; OSPM_DBGERRORLOG(errorcode, "cannot close socket"); } } } if (errorcode == OSPC_ERR_NO_ERROR) { /* * create a new socket */ errorcode = OSPPSockNew(ospvHttp); if (errorcode != OSPC_ERR_NO_ERROR) { errorcode = OSPC_ERR_SOCK_CREATE_FAILED; OSPM_DBGERRORLOG(errorcode, "cannot create socket"); } else { comm = (OSPTCOMM *)ospvHttp->Comm; errorcode = OSPPCommGetNumberOfServicePoints(comm, &numsvcpts); if (numsvcpts == 0) { OSPM_DBGERRORLOG(OSPC_ERR_COMM_NO_SVC_PTS, "num of svc pts = 0"); errorcode = OSPC_ERR_COMM_NO_SVC_PTS; } else { if (errorcode == OSPC_ERR_NO_ERROR) { /* * get a pointer to the service point list */ OSPPCommGetServicePointList(comm, &svcptlist); /* * if the http connection has never been connected or * the service point list has been exhausted, get the * first service point in the list */ if (ospvHttp->ServicePoint == (OSPTSVCPT *)OSPC_OSNULL) { ospvHttp->ServicePoint = (OSPTSVCPT *)OSPPListFirst( (OSPTLIST *)&svcptlist); } else { ospvHttp->ServicePoint = (OSPTSVCPT *)OSPPListNext( (OSPTLIST *)&svcptlist, ospvHttp->ServicePoint); /* * let the calling function know that the * service point list has been rolled over * to the beginning */ if (ospvHttp->ServicePoint == (OSPTSVCPT *)OSPC_OSNULL) { ospvHttp->ServicePoint = (OSPTSVCPT *)OSPPListFirst( (OSPTLIST *)&svcptlist); } } /* * if the next item in the service point list is null, * then the list is exhausted */ if ((svcptitem = (OSPTSVCPT *)OSPPListNext( (OSPTLIST *)&svcptlist, ospvHttp->ServicePoint)) == (OSPTSVCPT *)OSPC_OSNULL) { *ospvRollover = OSPC_TRUE; } /* * perform actual connection attempt */ OSPM_DBGNET(("connecting to %s%s", ospvHttp->ServicePoint->HostName, ospvHttp->ServicePoint->URI)); if (ospvHttp->ServicePoint != OSPC_OSNULL) { ospvHttp->Flags = (unsigned char) (ospvHttp->Flags & OSPC_SOCK_CONNECTED_MASK); /* * set the response timeout for the socket */ OSPPCommGetTimeout((OSPTCOMM *)ospvHttp->Comm, &socktimeout); timeout.tv_sec = socktimeout / 1000; timeout.tv_usec = socktimeout % 1000 * 1000; /* ** CHANGED OSPCFALSE -> TRUE */ errorcode = OSPPSockConnect(&(ospvHttp->SockFd), OSPC_DEFAULT_BLOCKING_FLAG, ospvHttp->ServicePoint->IpAddr, ospvHttp->ServicePoint->Port, &timeout, &(ospvHttp->SSLSession)); if (errorcode == OSPC_ERR_NO_ERROR) { /* * begin an SSL Session if a secured connection * is required */ errorcode = OSPPSSLSessionNew(ospvHttp, comm->Security); if (errorcode == OSPC_ERR_NO_ERROR) { *ospvConnected = OSPC_TRUE; ospvHttp->Flags |= OSPC_SOCK_CONNECTED_BIT; } else { OSPPSockClose(OSPC_FALSE, &(ospvHttp->SockFd), &(ospvHttp->SSLSession)); } } } else { errorcode = OSPC_ERR_COMM_NO_SVC_PTS_AVAIL; OSPM_DBGERRORLOG(errorcode, "no svc pts avail"); } } } } } OSPM_DBGEXIT(("EXIT : OSPPSockConnectServicePoint() (%d)\n", errorcode)); return errorcode;}intOSPPSockDisableNagle( OSPTHTTP *ospvHttp){ int errorcode = OSPC_ERR_NO_ERROR; OSPM_DBGENTER(("ENTER: OSPPSockDisableNagle()\n")); OSPM_DISABLE_NAGLE(ospvHttp->SockFd, errorcode); if (errorcode != OSPC_ERR_NO_ERROR) { errorcode = OSPC_ERR_SOCK_DISABLE_NAGLE_FAILED; } OSPM_DBGEXIT(("EXIT : OSPPSockDisableNagle()\n")); return errorcode;}intOSPPSockGetHostIP( char *Host, OSPTIPADDR *ospvIpAddr){ int errorcode = OSPC_ERR_NO_ERROR; OSPM_DBGENTER(("ENTER: OSPPSockGetHostIP()\n")); /* * see if char IP was passed */ OSPM_INET_ADDR((const char *)Host, *ospvIpAddr); if (*ospvIpAddr) return errorcode; OSPM_GETHOSTBYNAME(Host, *ospvIpAddr, errorcode); OSPM_DBGEXIT(("EXIT : OSPPSockGetHostIP() (%d)\n", errorcode)); return errorcode; } int
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -