I'm trying to create a .so in C to interface with an external program. Theexternal program only accepts functions that have pointers to structures in thedefinition. See the comments ‘Broken Function’ in my code below. I need thefunction declaration to be defined as in my code with ‘struct sockaddr_in6*serv_addr’.
The problem is that the ‘bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr))’ onlyworks if it is in the main() function. It gives an error, if I call it from mycustom function. I believe that the error is caused when I try to castserv_addr into a pointer structure.
My coworker and I cannot figureout how to get the reference in the bind() function that we need in thefunction without changing our custom function declaration. Is it possible toget the reference that we need without changing our custom function declaration?
Thanks.
My Code:
Code:
// Includes
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<stddef.h>
// Function Declarations
void getServerAddrJ(int sockfd, int portNum, struct sockaddr_in6*serv_addr);
int serverBindListenJ(int sockfd, struct sockaddr_in6*serv_addr); // Broken Function
// Main
int main(void) {
// Declare Vars
int portno = 3344;
int sockfd, tempVar;
struct sockaddr_in6serv_addr; // serv_addr Definition
// Sockets Layer Call: socket()
sockfd = socket(AF_INET6,SOCK_STREAM, 0);
// Parse Addr
getServerAddrJ(sockfd, portno,&serv_addr);
//This function needs to pass in &serv_addr
tempVar = serverBindListenJ(sockfd,&serv_addr); // Broken function
printf("\nSuccess= %d\n", tempVar);
// More Code…
return(0);
}
// End Main
void getServerAddrJ(int sockfd, int portNum, struct sockaddr_in6*serv_addr) {
// ParseAddress
serv_addr->sin6_flowinfo = 0;
serv_addr->sin6_family = AF_INET6; // AF_INET6 =10;
serv_addr->sin6_addr = in6addr_any;
serv_addr->sin6_port = htons(portNum); // Convert theport number to network byte order
}
// Broken function
// This function needs to have the argument ‘struct sockaddr_in6*serv_addr’
int serverBindListenJ(int sockfd, struct sockaddr_in6*serv_addr) {
// SocketsLayer Call: bind()
int tmpVar;
tmpVar= bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)); //The bind() works if it is in main(), but not in its own function
// Bind()Error Catch
if (tmpVar < 0){
return(-1);
}
// SocketsLayer Call: listen()
tmpVar= listen(sockfd, 5);
// Listen()Error Catch
return(tmpVar);
}