#include /* strncpy */ #include /* abs */ #include /* printf & friends */ #include /* NULL */ #include /* abs */ #include #include #include #include #include #include "commUdpTcp.h" /*============================================================================= Abs: Exit handler for us. Name: PKMain_Exit_Handler Args: None. Rem: Close our socket. This is to cover the possibility of someone doing a while we're running. We must close the socket else if we're started up again we'll get a bind error at initialize time. Side: None. Ret: Void. =============================================================================*/ static void commUdpTcpExitHandler (int socknum) { char blocking = 0; /* ** Set the socket to blocking so the sclose is synchronous. */ ioctl(socknum, FIONBIO, &blocking); close(socknum); return; } /*============================================================================= Abs: Create a client-side TCP or UDP socket to communicate with a server, then connect to it Name: commUdpTcpClntInit Args: socknum_p Pointer to created socket number socket_ps Pointer to our socket for communicating with server node Node name of device that comm with this knob box proto Protocol, 0 for UDP, 1 for TCP port Port number for server connected Pointer to connected flag, 1 if connected to server, else 0 Rem: Create a socket for communicating with a server Fill in the servaddr socket structure, then connect to it. Ret: 0 on success, -1 on failure =============================================================================*/ int commUdpTcpClntInit(int *socknum_p, struct sockaddr_in *socket_ps, char *node, int proto, int port, int *connected) { int status; /* Status from bind & sioctl */ int reuseon = 1; /* For socket reuse option */ struct hostent *hp; int tmp; char str[100]; /* Create a client-side TCP or UDP socket to communicate with server */ if ( proto == UDP ) *socknum_p = socket( AF_INET, SOCK_DGRAM, 0 ); else *socknum_p = socket( AF_INET, SOCK_STREAM, 0 ); if(*socknum_p < 0) { errlogPrintf("commUdpTcpClntInit: FATAL error %d. Can't create a socket\n", errno); return -1; } if ( proto == TCP ) { /* Set TCP socket to NODELAY to send individual packets instead of queuing them up */ status = setsockopt ( *socknum_p, IPPROTO_TCP, TCP_NODELAY, &reuseon, sizeof(reuseon) ); if(status < 0) { errlogPrintf("commUdpTcpClntInit: FATAL error %d. Can't set TCP to NODELAY\n", errno); return -1; } } /* Set the socket to reuse the address in case we're aborted * this is mabye not needed for this socket ?? */ status = setsockopt ( *socknum_p, SOL_SOCKET, SO_REUSEADDR, &reuseon, sizeof(reuseon) ); if(status < 0) { errlogPrintf("commUdpTcpClntInit: FATAL error %d. Can't set socket to reuse\n", errno); return -1; } /* Fill in the server's address and data */ memset((char*)socket_ps, 0, sizeof(*socket_ps)); socket_ps->sin_family = AF_INET; socket_ps->sin_port = htons(port); /* Look up the address of the server given its name */ hp = gethostbyname(node); if (!hp) { errlogPrintf("commUdpTcpClntInit: FATAL: Could not obtain address of %s\n", node); return -1; } #ifdef DEBUG int i; printf("commUdpTcpClntInit: server port %i host order %i. IPs:\n", socket_ps->sin_port, ntohs( socket_ps->sin_port)); for (i=0; hp->h_addr_list[i] != 0; i++) printf("addr %i is %u\n", i, htonl(*(int *) hp->h_addr_list[i] )); printf("socket number %i\n", *socknum_p); #endif tmp = *(int *)hp->h_addr_list[0]; /* Put the host's address into the server address structure */ memcpy((void *)&socket_ps->sin_addr, &tmp, hp->h_length ); /* Connect to server. Failure to connect is usually not fatal; we can try again later. */ if ( connect( *socknum_p, (struct sockaddr *)socket_ps, sizeof(*socket_ps) ) < 0 ) { sprintf(str, "Warning: Failed to connect to %s port %i", node, port); perror(str); commUdpTcpExitHandler( *socknum_p ); return 0; } *connected = 1; return 0; } /*============================================================================= Abs: Create a server-side TCP or UDP socket to communicate with clients Name: commUdpTcpSrvrInit Args: socknum_p Pointer to created socket number socket_ps Pointer to our socket for communicating with clients port Our port number Rem: Create a socket for communicating with clients Ret: 0 on success, -1 on failure =============================================================================*/ int commUdpTcpSrvrInit (int *socknum_p, struct sockaddr_in *socket_ps, int port) { int status; /* Status from bind & sioctl */ char nonblocking = 1; /* Nonblocking I/O */ int reuseon = 1; /* For socket reuse option */ /* Create a server-side UDP/IP datagram socket to send and receive messages to/from clients */ *socknum_p = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); if( *socknum_p < 0 ) { printf("commUdpSrvrInit Error %d. Can't create a socket!\n", errno); return -1; } /* Bind to our IP address at the 'known' port so our clients can talk to us. */ socket_ps->sin_addr.s_addr = INADDR_ANY; /* Our IP is known to the system */ socket_ps->sin_family = AF_INET; /* Ye olde standard Internet */ socket_ps->sin_port = htons(port); /* Our ``known'' port */ status = bind( *socknum_p, (struct sockaddr *)socket_ps, sizeof(struct sockaddr_in) ); if(status < 0) { printf ("commUdpSrvrInit: Error %d. Can't bind to the socket!\n", errno); return -1; } /* Set the socket to reuse the addresss in case we're aborted. */ status = setsockopt ( *socknum_p, SOL_SOCKET, SO_REUSEADDR, &reuseon , sizeof(reuseon) ); if(status < 0) { printf ("commUdpSrvrInit: Error %d. Can't set socket to reuse!\n", errno); return -1; } /* Set the socket to nonblocking so we can "poll" it. */ status = ioctl(*socknum_p, FIONBIO, &nonblocking); if(status < 0) { printf ("commUdpSrvrInit: Error %d. Can't set non-blocking control!\n", errno); return -1; } return 0; } /*============================================================================= Abs: Send a UDP or TCP message Name: commUdpTcpWrite Args: socknum_p Pointer to our socket number socket_ps Pointer to destination socket bufo_p Bytes to send count Expected number of bytes to send outlet Actual number of bytes sent Ret: None =============================================================================*/ void commUdpTcpWrite(int *socknum_p, struct sockaddr_in *socket_ps, unsigned char *bufo_p, int count, int *outlen) { *outlen = sendto (*socknum_p, (void *)bufo_p, count, 0, (struct sockaddr *)socket_ps, sizeof(struct sockaddr_in)); if (*outlen != count) printf ("commUdpTcpWrite.c: Client to server send error %d! Actual length = %d expected length = %d, sock num %i\n", errno, *outlen, count, *socknum_p); } /*============================================================================= Abs: Read a UDP or TCP message Name: commUdpTcpRead Args: socknum_p Pointer to our socket number socket_ps Pointer to source socket (filled in by recvfrom) insmgbuf_p Buffer to store incoming message elen Expected number of bytes to read inmsglen_p Actual number of bytes read timeout Read timeout timeval structure, set by caller Ret: None =============================================================================*/ int commUdpTcpRead(int *socknum_p, struct sockaddr_in *socket_ps, unsigned char *inmsgbuf_p, int elen, int *inmsglen_p, struct timeval timeout) { unsigned fromlen = sizeof(struct sockaddr_in); /* For recvfrom */ int status; /* Set timeout for receiving messages */ status = setsockopt( *socknum_p, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval) ); if(status < 0) { printf ("Error %d. Can't set socket recv timeout!! I Give up!!\n", errno); return -1; } *inmsglen_p = recvfrom( *socknum_p, (void *)inmsgbuf_p, elen, 0, (struct sockaddr *)socket_ps, &fromlen ); if ( *inmsglen_p > 0 ) return 0; else if ( errno != EWOULDBLOCK) { /* If no msg, check if not std error */ printf ("commUdpTcpRead: Receive Error %d! Actual length = %d\n", errno, *inmsglen_p); printf ("%s\n", strerror( errno )); } return 0; } static long drvCommUdpTcpReport(int level) { printf("Generic UDP/TCP support for knobs\n"); return 0; } static long drvCommUdpTcpInit(void) { return 0; } static struct { long number; DRVSUPFUN report; DRVSUPFUN init; } drvCommUdpTcp={ 2, drvCommUdpTcpReport, drvCommUdpTcpInit }; epicsExportAddress(drvet,drvCommUdpTcp);