static int udpAmsInit(AmsInterface *tsif, char *epspec) { unsigned short portNbr; unsigned int ipAddress; char hostName[MAXHOSTNAMELEN + 1]; struct sockaddr socketName; struct sockaddr_in *inetName; socklen_t nameLength; int fd; char endpointNameText[32]; int eptLen; long longfd; if (strcmp(epspec, "@") == 0) /* Default. */ { epspec = NULL; /* Force default selection. */ } parseSocketSpec(epspec, &portNbr, &ipAddress); if (ipAddress == 0) { getNameOfHost(hostName, sizeof hostName); ipAddress = getInternetAddress(hostName); } else { if (getInternetHostName(ipAddress, hostName) == NULL) { putErrmsg("Unknown host in endpoint.", epspec); return -1; } } portNbr = htons(portNbr); ipAddress = htonl(ipAddress); memset((char *) &socketName, 0, sizeof socketName); inetName = (struct sockaddr_in *) &socketName; inetName->sin_family = AF_INET; inetName->sin_port = portNbr; memcpy((char *) &(inetName->sin_addr.s_addr), (char *) &ipAddress, 4); fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (fd < 0) { putSysErrmsg("udpts can't open AMS SAP", NULL); return -1; } nameLength = sizeof(struct sockaddr); if (reUseAddress(fd) || bind(fd, &socketName, nameLength) < 0 || getsockname(fd, &socketName, &nameLength) < 0) { putSysErrmsg("udpts can't initialize AMS SAP", NULL); return -1; } portNbr = inetName->sin_port; portNbr = ntohs(portNbr); memcpy((char *) &ipAddress, (char *) &(inetName->sin_addr.s_addr), 4); ipAddress = ntohl(ipAddress); tsif->diligence = AmsBestEffort; tsif->sequence = AmsArrivalOrder; isprintf(endpointNameText, sizeof endpointNameText, "%s:%hu", hostName, portNbr); eptLen = strlen(endpointNameText) + 1; tsif->ept = MTAKE(eptLen); if (tsif->ept == NULL) { close(fd); putSysErrmsg(NoMemoryMemo, NULL); return -1; } istrcpy(tsif->ept, endpointNameText, eptLen); longfd = fd; tsif->sap = (void *) longfd; return 0; }
static int udpMamsInit(MamsInterface *tsif) { unsigned short portNbr; unsigned int ipAddress; char hostName[MAXHOSTNAMELEN + 1]; struct sockaddr socketName; struct sockaddr_in *inetName; socklen_t nameLength; int fd; char endpointNameText[32]; int eptLen; long longfd; parseSocketSpec(tsif->endpointSpec, &portNbr, &ipAddress); //printf("parsed endpoint spec to port %d address %d.\n", portNbr, ipAddress); if (ipAddress == 0) { getNameOfHost(hostName, sizeof hostName); ipAddress = getInternetAddress(hostName); } else { if (getInternetHostName(ipAddress, hostName) == NULL) { putErrmsg("Unknown host in endpoint.", tsif->endpointSpec); return -1; } } portNbr = htons(portNbr); ipAddress = htonl(ipAddress); memset((char *) &socketName, 0, sizeof socketName); inetName = (struct sockaddr_in *) &socketName; inetName->sin_family = AF_INET; inetName->sin_port = portNbr; memcpy((char *) &(inetName->sin_addr.s_addr), (char *) &ipAddress, 4); fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (fd < 0) { putSysErrmsg("udpts can't open MAMS SAP", NULL); return -1; } nameLength = sizeof(struct sockaddr); if (reUseAddress(fd) || bind(fd, &socketName, nameLength) < 0 || getsockname(fd, &socketName, &nameLength) < 0) { putSysErrmsg("udpts can't initialize AMS SAP", NULL); return -1; } portNbr = inetName->sin_port; portNbr = ntohs(portNbr); memcpy((char *) &ipAddress, (char *) &(inetName->sin_addr.s_addr), 4); ipAddress = ntohl(ipAddress); isprintf(endpointNameText, sizeof endpointNameText, "%s:%hu", hostName, portNbr); //printf("resulting ept is '%s'.\n", endpointNameText); eptLen = strlen(endpointNameText) + 1; tsif->ept = MTAKE(eptLen); if (tsif->ept == NULL) { close(fd); putSysErrmsg(NoMemoryMemo, NULL); return -1; } istrcpy(tsif->ept, endpointNameText, eptLen); longfd = fd; tsif->sap = (void *) longfd; return 0; }
int udplsi(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10) { char *endpointSpec = (char *) a1; #else int main(int argc, char *argv[]) { char *endpointSpec = (argc > 1 ? argv[1] : NULL); #endif LtpVdb *vdb; unsigned short portNbr = 0; unsigned int ipAddress = INADDR_ANY; struct sockaddr socketName; struct sockaddr_in *inetName; ReceiverThreadParms rtp; socklen_t nameLength; pthread_t receiverThread; int fd; char quit = '\0'; /* Note that ltpadmin must be run before the first * invocation of ltplsi, to initialize the LTP database * (as necessary) and dynamic database. */ if (ltpInit(0) < 0) { putErrmsg("udplsi can't initialize LTP.", NULL); return 1; } vdb = getLtpVdb(); if (vdb->lsiPid != ERROR && vdb->lsiPid != sm_TaskIdSelf()) { putErrmsg("LSI task is already started.", itoa(vdb->lsiPid)); return 1; } /* All command-line arguments are now validated. */ if (endpointSpec) { if(parseSocketSpec(endpointSpec, &portNbr, &ipAddress) != 0) { putErrmsg("Can't get IP/port for endpointSpec.", endpointSpec); return -1; } } if (portNbr == 0) { portNbr = LtpUdpDefaultPortNbr; } portNbr = htons(portNbr); ipAddress = htonl(ipAddress); memset((char *) &socketName, 0, sizeof socketName); inetName = (struct sockaddr_in *) &socketName; inetName->sin_family = AF_INET; inetName->sin_port = portNbr; memcpy((char *) &(inetName->sin_addr.s_addr), (char *) &ipAddress, 4); rtp.linkSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (rtp.linkSocket < 0) { putSysErrmsg("LSI can't open UDP socket", NULL); return -1; } nameLength = sizeof(struct sockaddr); if (reUseAddress(rtp.linkSocket) || bind(rtp.linkSocket, &socketName, nameLength) < 0 || getsockname(rtp.linkSocket, &socketName, &nameLength) < 0) { closesocket(rtp.linkSocket); putSysErrmsg("Can't initialize socket", NULL); return 1; } /* Set up signal handling; SIGTERM is shutdown signal. */ ionNoteMainThread("udplsi"); isignal(SIGTERM, interruptThread); /* Start the receiver thread. */ rtp.running = 1; if (pthread_begin(&receiverThread, NULL, handleDatagrams, &rtp)) { closesocket(rtp.linkSocket); putSysErrmsg("udplsi can't create receiver thread", NULL); return 1; } /* Now sleep until interrupted by SIGTERM, at which point * it's time to stop the link service. */ { char txt[500]; isprintf(txt, sizeof(txt), "[i] udplsi is running, spec=[%s:%d].", inet_ntoa(inetName->sin_addr), ntohs(portNbr)); writeMemo(txt); } ionPauseMainThread(-1); /* Time to shut down. */ rtp.running = 0; /* Wake up the receiver thread by sending it a 1-byte * datagram. */ fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (fd >= 0) { isendto(fd, &quit, 1, 0, &socketName, sizeof(struct sockaddr)); closesocket(fd); } pthread_join(receiverThread, NULL); closesocket(rtp.linkSocket); writeErrmsgMemos(); writeMemo("[i] udplsi has ended."); ionDetach(); return 0; }