Made changes to deal with issues found during self-code review.

Added lock callback functionality for interfacing with OpenSSL
in a multi-threaded environment.
This commit is contained in:
Juan Carlos Luciani
2006-11-30 18:21:42 +00:00
parent a522f9d982
commit 1974ee9875
28 changed files with 1196 additions and 548 deletions

View File

@@ -124,113 +124,135 @@ EncodeData(
void NonHttpTest(void)
{
CasaStatus retStatus;
char authToken[8192];
int authTokenLen = sizeof(authToken);
char *pAuthToken;
int authTokenLen = 0;
// Obtain an authentication token for the targeted service
retStatus = ObtainAuthToken(pServiceName, pServerAddress, authToken, &authTokenLen);
if (!CASA_SUCCESS(retStatus))
// First call to get the authentication token with no output buffer so
// that we can determine the buffer size necessary to hold the token.
retStatus = ObtainAuthToken(pServiceName, pServerAddress, NULL, &authTokenLen);
if (CasaStatusCode(retStatus) == CASA_STATUS_BUFFER_OVERFLOW)
{
printf("-NonHttpTest- ObtainAuthToken failed with status %d\n", retStatus);
}
else
{
SOCKET sock;
struct sockaddr_in localAddr = {0};
struct sockaddr_in remoteAddr = {0};
struct linger linger_opt = {1, 15};
struct hostent *pLookupResult;
printf("-NonHttpTest- ObtainAuthToken succedded, tokenlen = %d\n", authTokenLen);
// Send the token to the server
//
// Open socket
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock != INVALID_SOCKET)
// Allocate buffer to receive the token
pAuthToken = (char*) malloc(authTokenLen);
if (pAuthToken)
{
// Setup the local address structure
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Bind socket
if (!bind(sock, (const struct sockaddr*) &localAddr, sizeof(struct sockaddr_in)))
// Now get the token
retStatus = ObtainAuthToken(pServiceName, pServerAddress, pAuthToken, &authTokenLen);
if (!CASA_SUCCESS(retStatus))
{
// Resolve the server address
pLookupResult = gethostbyname(pServerAddress);
if (pLookupResult)
printf("-NonHttpTest- ObtainAuthToken failed with status %d\n", retStatus);
}
else
{
SOCKET sock;
struct sockaddr_in localAddr = {0};
struct sockaddr_in remoteAddr = {0};
struct linger linger_opt = {1, 15};
struct hostent *pLookupResult;
printf("-NonHttpTest- ObtainAuthToken succedded, tokenlen = %d\n", authTokenLen);
// Send the token to the server
//
// Open socket
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock != INVALID_SOCKET)
{
// Validate the address type returned
if (pLookupResult->h_addrtype == AF_INET)
// Setup the local address structure
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Bind socket
if (!bind(sock, (const struct sockaddr*) &localAddr, sizeof(struct sockaddr_in)))
{
int numAddressesFound = 0;
// Determine how many addresses where returned
while (pLookupResult->h_addr_list[numAddressesFound] != NULL)
// Resolve the server address
pLookupResult = gethostbyname(pServerAddress);
if (pLookupResult)
{
//printf("ServerAddress = %08X\n", *((int*) pLookupResult->h_addr_list[numAddressesFound]));
numAddressesFound ++;
}
//printf("Found %d addresses\n", numAddressesFound);
// Validate the address type returned
if (pLookupResult->h_addrtype == AF_INET)
{
int numAddressesFound = 0;
// Setup the remote address structure with the lookup results
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_port = serverPort;
remoteAddr.sin_addr.s_addr = *((int*) pLookupResult->h_addr_list[0]); // Short-cut
//printf("ServerAddress = %08X\n", remoteAddr.sin_addr.s_addr);
// Determine how many addresses where returned
while (pLookupResult->h_addr_list[numAddressesFound] != NULL)
{
//printf("ServerAddress = %08X\n", *((int*) pLookupResult->h_addr_list[numAddressesFound]));
numAddressesFound ++;
}
//printf("Found %d addresses\n", numAddressesFound);
// Perform connect operation
if (connect(sock,
(struct sockaddr*) &remoteAddr,
sizeof(struct sockaddr_in)) == SOCKET_ERROR)
{
printf("-NonHttpTest- Connection creation failed, error = %d\n", errno);
// Setup the remote address structure with the lookup results
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_port = serverPort;
remoteAddr.sin_addr.s_addr = *((int*) pLookupResult->h_addr_list[0]); // Short-cut
//printf("ServerAddress = %08X\n", remoteAddr.sin_addr.s_addr);
// Perform connect operation
if (connect(sock,
(struct sockaddr*) &remoteAddr,
sizeof(struct sockaddr_in)) == SOCKET_ERROR)
{
printf("-NonHttpTest- Connection creation failed, error = %d\n", errno);
}
else
{
// Now the connection is setup, send the credentials to the server as one line.
// using our cheesy protocol followed by a hello string.
//
// Send the token to the server (including NULL terminator)
send(sock, pAuthToken, (int) strlen(pAuthToken) + 1, 0);
// Send new line
send(sock, "\n", 1, 0);
// Send "hello"
//send(sock, helloString, strlen(helloString) + 1, MSG_NOSIGNAL);
// Send new line
//send(sock, "\n", 1, 0);
// Shutdown the connection
shutdown(sock, 0);
}
}
else
{
printf("-NonHttpTest- Unsupported address type returned %08X\n", pLookupResult->h_addrtype);
}
}
else
{
// Now the connection is setup, send the credentials to the server as one line.
// using our cheesy protocol followed by a hello string.
//
// Send the token to the server (including NULL terminator)
send(sock, authToken, (int) strlen(authToken) + 1, 0);
// Send new line
send(sock, "\n", 1, 0);
// Send "hello"
//send(sock, helloString, strlen(helloString) + 1, MSG_NOSIGNAL);
// Send new line
//send(sock, "\n", 1, 0);
// Shutdown the connection
shutdown(sock, 0);
printf("-NonHttpTest- Lookup for %s failed\n", pServerAddress);
}
}
else
{
printf("-NonHttpTest- Unsupported address type returned %08X\n", pLookupResult->h_addrtype);
printf("-NonHttpTest- Unable to bind socket, error = %d", errno);
}
// Close the socket
setsockopt(sock, SOL_SOCKET, SO_LINGER, (const char*) &linger_opt, sizeof(linger_opt));
closesocket(sock);
}
else
{
printf("-NonHttpTest- Lookup for %s failed\n", pServerAddress);
printf("-NonHttpTest- Unable to open socket, error = %d\n", errno);
}
}
else
{
printf("-NonHttpTest- Unable to bind socket, error = %d", errno);
}
// Close the socket
setsockopt(sock, SOL_SOCKET, SO_LINGER, (const char*) &linger_opt, sizeof(linger_opt));
closesocket(sock);
// Release the buffer allocated for the token
free(pAuthToken);
}
else
{
printf("-NonHttpTest- Unable to open socket, error = %d\n", errno);
printf("-NonHttpTest- Failed to allocate buffer for token\n", 0);
}
}
else
{
printf("-NonHttpTest- ObtainAuthToken failed with status %d\n", retStatus);
}
}
@@ -242,141 +264,162 @@ void NonHttpTest(void)
void HttpTest(void)
{
CasaStatus retStatus;
char authToken[4096];
int authTokenLen = sizeof(authToken);
char *pAuthToken;
int authTokenLen = 0;
// Obtain an authentication token for the targeted service
retStatus = ObtainAuthToken(pServiceName, pServerAddress, authToken, &authTokenLen);
if (!CASA_SUCCESS(retStatus))
// First call to get the authentication token with no output buffer so
// that we can determine the buffer size necessary to hold the token.
retStatus = ObtainAuthToken(pServiceName, pServerAddress, NULL, &authTokenLen);
if (CasaStatusCode(retStatus) == CASA_STATUS_BUFFER_OVERFLOW)
{
printf("-HttpTest- ObtainAuthToken failed with status %d\n", retStatus);
}
else
{
SOCKET sock;
struct sockaddr_in localAddr = {0};
struct sockaddr_in remoteAddr = {0};
struct linger linger_opt = {1, 15};
struct hostent *pLookupResult;
//printf("ObtainAuthToken succedded, token = %s\n", authToken);
printf("-HttpTest- ObtainAuthToken succedded, tokenlen = %d\n", authTokenLen);
// Send the token to the server
// Open socket
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock != INVALID_SOCKET)
// Allocate buffer to receive the token
pAuthToken = (char*) malloc(authTokenLen);
if (pAuthToken)
{
// Setup the local address structure
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Bind socket
if (!bind(sock, (const struct sockaddr*) &localAddr, sizeof(struct sockaddr_in)))
// Now get the token
retStatus = ObtainAuthToken(pServiceName, pServerAddress, pAuthToken, &authTokenLen);
if (!CASA_SUCCESS(retStatus))
{
// Resolve the server address
pLookupResult = gethostbyname(pServerAddress);
if (pLookupResult)
printf("-HttpTest- ObtainAuthToken failed with status %0X\n", retStatus);
}
else
{
SOCKET sock;
struct sockaddr_in localAddr = {0};
struct sockaddr_in remoteAddr = {0};
struct linger linger_opt = {1, 15};
struct hostent *pLookupResult;
printf("-HttpTest- ObtainAuthToken succedded, tokenlen = %d\n", authTokenLen);
// Send the token to the server
// Open socket
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock != INVALID_SOCKET)
{
// Validate the address type returned
if (pLookupResult->h_addrtype == AF_INET)
// Setup the local address structure
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Bind socket
if (!bind(sock, (const struct sockaddr*) &localAddr, sizeof(struct sockaddr_in)))
{
int numAddressesFound = 0;
// Determine how many addresses where returned
while (pLookupResult->h_addr_list[numAddressesFound] != NULL)
// Resolve the server address
pLookupResult = gethostbyname(pServerAddress);
if (pLookupResult)
{
//printf("ServerAddress = %08X\n", *((int*) pLookupResult->h_addr_list[numAddressesFound]));
numAddressesFound ++;
}
//printf("Found %d addresses\n", numAddressesFound);
// Setup the remote address structure with the lookup results
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_port = serverPort;
remoteAddr.sin_addr.s_addr = *((int*) pLookupResult->h_addr_list[0]); // Short-cut
//printf("ServerAddress = %08X\n", remoteAddr.sin_addr.s_addr);
// Perform connect operation
if (connect(sock,
(struct sockaddr*) &remoteAddr,
sizeof(struct sockaddr_in)) == SOCKET_ERROR)
{
printf("-HttpTest- Connection creation failed, error = %d\n", errno);
}
else
{
char *pBasicCredentials;
char *pEncodedBasicCredentials;
int encodedLength;
char CasaPrincipal[] = "CasaPrincipal:";
char HTTPReqPart1[] = "GET /example-info HTTP/1.1\r\\nUser-Agent: CasaTestClient\r\nHost: jcstation.dnsdhcp.provo.novell.com:4096\r\nConnection: Keep-Alive\r\nAuthorization: Basic ";
// Now the connection is setup, send 1st part of HTTP request to the server.
send(sock, HTTPReqPart1, (int) strlen(HTTPReqPart1), 0);
// Now setup the HTTP Basic Credentials
pBasicCredentials = (char*) malloc(strlen(CasaPrincipal) + strlen(authToken) + 1);
if (pBasicCredentials)
// Validate the address type returned
if (pLookupResult->h_addrtype == AF_INET)
{
memcpy(pBasicCredentials, CasaPrincipal, sizeof(CasaPrincipal));
strcat(pBasicCredentials, authToken);
int numAddressesFound = 0;
// Now Base64 encode the credentials
if (EncodeData((const void*) pBasicCredentials,
(const int32_t) strlen(pBasicCredentials),
&pEncodedBasicCredentials,
(int32_t *) &encodedLength) == 0)
// Determine how many addresses where returned
while (pLookupResult->h_addr_list[numAddressesFound] != NULL)
{
// Send the encoded credentials
send(sock, pEncodedBasicCredentials, encodedLength - 1, 0);
//printf("ServerAddress = %08X\n", *((int*) pLookupResult->h_addr_list[numAddressesFound]));
numAddressesFound ++;
}
//printf("Found %d addresses\n", numAddressesFound);
// Send the rest of the header
send(sock, "\r\n\r\n", 4, 0);
// Free the buffer holding the encoded credentials
free(pEncodedBasicCredentials);
// Setup the remote address structure with the lookup results
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_port = serverPort;
remoteAddr.sin_addr.s_addr = *((int*) pLookupResult->h_addr_list[0]); // Short-cut
//printf("ServerAddress = %08X\n", remoteAddr.sin_addr.s_addr);
// Perform connect operation
if (connect(sock,
(struct sockaddr*) &remoteAddr,
sizeof(struct sockaddr_in)) == SOCKET_ERROR)
{
printf("-HttpTest- Connection creation failed, error = %d\n", errno);
}
else
{
printf("-HttpTest- Error encoding credentials\n");
}
char *pBasicCredentials;
char *pEncodedBasicCredentials;
int encodedLength;
char CasaPrincipal[] = "CasaPrincipal:";
char HTTPReqPart1[] = "GET /example-info HTTP/1.1\r\\nUser-Agent: CasaTestClient\r\nHost: jcstation.dnsdhcp.provo.novell.com:4096\r\nConnection: Keep-Alive\r\nAuthorization: Basic ";
// Free the buffer containing the basic credentials
free(pBasicCredentials);
// Now the connection is setup, send 1st part of HTTP request to the server.
send(sock, HTTPReqPart1, (int) strlen(HTTPReqPart1), 0);
// Now setup the HTTP Basic Credentials
pBasicCredentials = (char*) malloc(strlen(CasaPrincipal) + strlen(pAuthToken) + 1);
if (pBasicCredentials)
{
memcpy(pBasicCredentials, CasaPrincipal, sizeof(CasaPrincipal));
strcat(pBasicCredentials, pAuthToken);
// Now Base64 encode the credentials
if (EncodeData((const void*) pBasicCredentials,
(const int32_t) strlen(pBasicCredentials),
&pEncodedBasicCredentials,
(int32_t *) &encodedLength) == 0)
{
// Send the encoded credentials
send(sock, pEncodedBasicCredentials, encodedLength - 1, 0);
// Send the rest of the header
send(sock, "\r\n\r\n", 4, 0);
// Free the buffer holding the encoded credentials
free(pEncodedBasicCredentials);
}
else
{
printf("-HttpTest- Error encoding credentials\n");
}
// Free the buffer containing the basic credentials
free(pBasicCredentials);
}
else
{
printf("-HttpTest- Buffer allocation failure\n");
}
// Shutdown the connection
shutdown(sock, 0);
}
}
else
{
printf("-HttpTest- Buffer allocation failure\n");
printf("-HttpTest- Unsupported address type returned %08X\n", pLookupResult->h_addrtype);
}
// Shutdown the connection
shutdown(sock, 0);
}
else
{
printf("-HttpTest- Lookup for %s failed\n", pServerAddress);
}
}
else
{
printf("-HttpTest- Unsupported address type returned %08X\n", pLookupResult->h_addrtype);
printf("-HttpTest- Unable to bind socket, error = %d", errno);
}
// Close the socket
setsockopt(sock, SOL_SOCKET, SO_LINGER, (const char*) &linger_opt, sizeof(linger_opt));
closesocket(sock);
}
else
{
printf("-HttpTest- Lookup for %s failed\n", pServerAddress);
printf("-HttpTest- Unable to open socket, error = %d\n", errno);
}
}
else
{
printf("-HttpTest- Unable to bind socket, error = %d", errno);
}
// Close the socket
setsockopt(sock, SOL_SOCKET, SO_LINGER, (const char*) &linger_opt, sizeof(linger_opt));
closesocket(sock);
// Release the buffer allocated for the token
free(pAuthToken);
}
else
{
printf("-HttpTest- Unable to open socket, error = %d\n", errno);
printf("-HttpTest- Failed to allocate buffer for token\n", 0);
}
}
else
{
printf("-HttpTest- ObtainAuthToken failed with status %0X\n", retStatus);
}
}