static int udpComputeCsepName(char *endpointSpec, char *endpointName) { unsigned short portNbr; unsigned int ipAddress; char hostName[MAXHOSTNAMELEN + 1]; if (endpointName == NULL) { putErrmsg(BadParmsMemo, NULL); return -1; } parseSocketSpec(endpointSpec, &portNbr, &ipAddress); if (portNbr == 0) { portNbr = 2357; /* Default. */ } if (ipAddress == 0) /* Default to local host. */ { getNameOfHost(hostName, sizeof hostName); ipAddress = getInternetAddress(hostName); } else { if (getInternetHostName(ipAddress, hostName) == NULL) { putErrmsg("Unknown host in endpoint.", endpointSpec); return -1; } } isprintf(endpointName, MAX_EP_NAME + 1, "%s:%hu", hostName, portNbr); 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; }
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; }
int tcpclo(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10) { char *ductName = (char *) a1; #else int main(int argc, char *argv[]) { char *ductName = (argc > 1 ? argv[1] : NULL); #endif unsigned char *buffer; VOutduct *vduct; PsmAddress vductElt; Sdr sdr; Outduct duct; ClProtocol protocol; Outflow outflows[3]; int i; char *hostName; unsigned short portNbr; unsigned int hostNbr; struct sockaddr socketName; struct sockaddr_in *inetName; int running = 1; pthread_mutex_t mutex; KeepaliveThreadParms parms; ReceiveThreadParms rparms; pthread_t keepaliveThread; pthread_t receiverThread; Object bundleZco; BpExtendedCOS extendedCOS; char destDuctName[MAX_CL_DUCT_NAME_LEN + 1]; unsigned int bundleLength; int ductSocket = -1; int bytesSent; int keepalivePeriod = 0; VInduct *viduct; if (ductName == NULL) { PUTS("Usage: tcpclo <remote host name>[:<port number>]"); return 0; } if (bpAttach() < 0) { putErrmsg("tcpclo can't attach to BP", NULL); return 1; } buffer = MTAKE(TCPCLA_BUFSZ); if (buffer == NULL) { putErrmsg("No memory for TCP buffer in tcpclo.", NULL); return 1; } findOutduct("tcp", ductName, &vduct, &vductElt); if (vductElt == 0) { putErrmsg("No such tcp duct.", ductName); MRELEASE(buffer); return 1; } if (vduct->cloPid != ERROR && vduct->cloPid != sm_TaskIdSelf()) { putErrmsg("CLO task is already started for this duct.", itoa(vduct->cloPid)); MRELEASE(buffer); return 1; } /* All command-line arguments are now validated. */ sdr = getIonsdr(); CHKERR(sdr_begin_xn(sdr)); sdr_read(sdr, (char *) &duct, sdr_list_data(sdr, vduct->outductElt), sizeof(Outduct)); sdr_read(sdr, (char *) &protocol, duct.protocol, sizeof(ClProtocol)); sdr_exit_xn(sdr); if (protocol.nominalRate == 0) { vduct->xmitThrottle.nominalRate = DEFAULT_TCP_RATE; } else { vduct->xmitThrottle.nominalRate = protocol.nominalRate; } memset((char *) outflows, 0, sizeof outflows); outflows[0].outboundBundles = duct.bulkQueue; outflows[1].outboundBundles = duct.stdQueue; outflows[2].outboundBundles = duct.urgentQueue; for (i = 0; i < 3; i++) { outflows[i].svcFactor = 1 << i; } hostName = ductName; parseSocketSpec(ductName, &portNbr, &hostNbr); if (portNbr == 0) { portNbr = BpTcpDefaultPortNbr; } portNbr = htons(portNbr); if (hostNbr == 0) { putErrmsg("Can't get IP address for host.", hostName); MRELEASE(buffer); return 1; } hostNbr = htonl(hostNbr); 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 *) &hostNbr, 4); if (_tcpOutductId(&socketName, "tcp", ductName) < 0) { putErrmsg("Can't record TCP Outduct ID for connection.", NULL); MRELEASE(buffer); return -1; } /* Set up signal handling. SIGTERM is shutdown signal. */ oK(tcpcloSemaphore(&(vduct->semaphore))); isignal(SIGTERM, shutDownClo); #ifndef mingw isignal(SIGPIPE, handleConnectionLoss); #endif /* Start the keepalive thread for the eventual connection. */ tcpDesiredKeepAlivePeriod = KEEPALIVE_PERIOD; parms.cloRunning = &running; pthread_mutex_init(&mutex, NULL); parms.mutex = &mutex; parms.socketName = &socketName; parms.ductSocket = &ductSocket; parms.keepalivePeriod = &keepalivePeriod; if (pthread_begin(&keepaliveThread, NULL, sendKeepalives, &parms)) { putSysErrmsg("tcpclo can't create keepalive thread", NULL); MRELEASE(buffer); pthread_mutex_destroy(&mutex); return 1; } // Returns the VInduct Object of first induct with same protocol // as the outduct. The VInduct is required to create an acq area. // The Acq Area inturn uses the throttle information from VInduct // object while receiving bundles. The throttle information // of all inducts of the same induct will be the same, so choosing // any induct will serve the purpose. findVInduct(&viduct,protocol.name); if(viduct == NULL) { putErrmsg("tcpclo can't get VInduct", NULL); MRELEASE(buffer); pthread_mutex_destroy(&mutex); return 1; } rparms.vduct = viduct; rparms.bundleSocket = &ductSocket; rparms.mutex = &mutex; rparms.cloRunning = &running; if (pthread_begin(&receiverThread, NULL, receiveBundles, &rparms)) { putSysErrmsg("tcpclo can't create receive thread", NULL); MRELEASE(buffer); pthread_mutex_destroy(&mutex); return 1; } /* Can now begin transmitting to remote duct. */ { char txt[500]; isprintf(txt, sizeof(txt), "[i] tcpclo is running, spec=[%s:%d].", inet_ntoa(inetName->sin_addr), ntohs(inetName->sin_port)); writeMemo(txt); } while (running && !(sm_SemEnded(tcpcloSemaphore(NULL)))) { if (bpDequeue(vduct, outflows, &bundleZco, &extendedCOS, destDuctName, 0, -1) < 0) { running = 0; /* Terminate CLO. */ continue; } if (bundleZco == 0) /* Interrupted. */ { continue; } CHKZERO(sdr_begin_xn(sdr)); bundleLength = zco_length(sdr, bundleZco); sdr_exit_xn(sdr); pthread_mutex_lock(&mutex); bytesSent = sendBundleByTCPCL(&socketName, &ductSocket, bundleLength, bundleZco, buffer, &keepalivePeriod); pthread_mutex_unlock(&mutex); if(bytesSent < 0) { running = 0; /* Terminate CLO. */ } /* Make sure other tasks have a chance to run. */ sm_TaskYield(); } writeMemo("[i] tcpclo done sending"); if (sendShutDownMessage(&ductSocket, SHUT_DN_NO, -1, &socketName) < 0) { putErrmsg("Sending Shutdown message failed!!",NULL); } if (ductSocket != -1) { closesocket(ductSocket); ductSocket=-1; } running = 0; pthread_join(keepaliveThread, NULL); writeMemo("[i] tcpclo keepalive thread killed"); pthread_join(receiverThread, NULL); writeMemo("[i] tcpclo receiver thread killed"); writeErrmsgMemos(); writeMemo("[i] tcpclo duct has ended."); oK(_tcpOutductId(&socketName, NULL, NULL)); MRELEASE(buffer); pthread_mutex_destroy(&mutex); bp_detach(); 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; }
int stcpclo(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10) { char *ductName = (char *) a1; #else int main(int argc, char *argv[]) { char *ductName = (argc > 1 ? argv[1] : NULL); #endif unsigned char *buffer; VOutduct *vduct; PsmAddress vductElt; Sdr sdr; Outduct duct; ClProtocol protocol; Outflow outflows[3]; int i; char *hostName; unsigned short portNbr; unsigned int hostNbr; struct sockaddr socketName; struct sockaddr_in *inetName; int running = 1; pthread_mutex_t mutex; KeepaliveThreadParms parms; pthread_t keepaliveThread; Object bundleZco; BpExtendedCOS extendedCOS; char destDuctName[MAX_CL_DUCT_NAME_LEN + 1]; unsigned int bundleLength; int ductSocket = -1; int bytesSent; if (ductName == NULL) { PUTS("Usage: stcpclo <remote host name>[:<port number>]"); return 0; } if (bpAttach() < 0) { putErrmsg("stcpclo can't attach to BP.", NULL); return -1; } buffer = MTAKE(TCPCLA_BUFSZ); if (buffer == NULL) { putErrmsg("No memory for TCP buffer in stcpclo.", NULL); return -1; } findOutduct("stcp", ductName, &vduct, &vductElt); if (vductElt == 0) { putErrmsg("No such stcp duct.", ductName); MRELEASE(buffer); return -1; } if (vduct->cloPid > 0 && vduct->cloPid != sm_TaskIdSelf()) { putErrmsg("CLO task is already started for this duct.", itoa(vduct->cloPid)); MRELEASE(buffer); return -1; } /* All command-line arguments are now validated. */ sdr = getIonsdr(); sdr_read(sdr, (char *) &duct, sdr_list_data(sdr, vduct->outductElt), sizeof(Outduct)); sdr_read(sdr, (char *) &protocol, duct.protocol, sizeof(ClProtocol)); if (protocol.nominalRate <= 0) { vduct->xmitThrottle.nominalRate = DEFAULT_TCP_RATE; } else { vduct->xmitThrottle.nominalRate = protocol.nominalRate; } memset((char *) outflows, 0, sizeof outflows); outflows[0].outboundBundles = duct.bulkQueue; outflows[1].outboundBundles = duct.stdQueue; outflows[2].outboundBundles = duct.urgentQueue; for (i = 0; i < 3; i++) { outflows[i].svcFactor = 1 << i; } hostName = ductName; parseSocketSpec(ductName, &portNbr, &hostNbr); if (portNbr == 0) { portNbr = BpTcpDefaultPortNbr; } portNbr = htons(portNbr); if (hostNbr == 0) { putErrmsg("Can't get IP address for host.", hostName); MRELEASE(buffer); return -1; } hostNbr = htonl(hostNbr); 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 *) &hostNbr, 4); /* Set up signal handling. SIGTERM is shutdown signal. */ oK(stcpcloSemaphore(&(vduct->semaphore))); isignal(SIGTERM, shutDownClo); isignal(SIGPIPE, handleConnectionLoss); /* Start the keepalive thread for the eventual connection. */ parms.cloRunning = &running; pthread_mutex_init(&mutex, NULL); parms.mutex = &mutex; parms.socketName = &socketName; parms.ductSocket = &ductSocket; if (pthread_create(&keepaliveThread, NULL, sendKeepalives, &parms)) { putSysErrmsg("stcpclo can't create keepalive thread", NULL); MRELEASE(buffer); pthread_mutex_destroy(&mutex); return 1; } /* Can now begin transmitting to remote duct. */ writeMemo("[i] stcpclo is running."); while (!(sm_SemEnded(stcpcloSemaphore(NULL)))) { if (bpDequeue(vduct, outflows, &bundleZco, &extendedCOS, destDuctName) < 0) { sm_SemEnd(stcpcloSemaphore(NULL));/* Stop. */ continue; } if (bundleZco == 0) /* Interrupted. */ { continue; } bundleLength = zco_length(sdr, bundleZco); pthread_mutex_lock(&mutex); bytesSent = sendBundleByTCP(&socketName, &ductSocket, bundleLength, bundleZco, buffer); pthread_mutex_unlock(&mutex); if (bytesSent < bundleLength) { sm_SemEnd(stcpcloSemaphore(NULL));/* Stop. */ continue; } /* Make sure other tasks have a chance to run. */ sm_TaskYield(); } running = 0; /* Terminate keepalive. */ pthread_join(keepaliveThread, NULL); if (ductSocket != -1) { close(ductSocket); } pthread_mutex_destroy(&mutex); writeErrmsgMemos(); writeMemo("[i] stcpclo duct has ended."); MRELEASE(buffer); ionDetach(); return 0; }
int udpclo(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10) { #else int main(int argc, char *argv[]) { #endif unsigned char *buffer; VOutduct *vduct; PsmAddress vductElt; Sdr sdr; Outduct outduct; ClProtocol protocol; Outflow outflows[3]; int i; char *hostName; unsigned short portNbr; unsigned int hostNbr; struct sockaddr socketName; struct sockaddr_in *inetName; Object bundleZco; BpExtendedCOS extendedCOS; char destDuctName[MAX_CL_DUCT_NAME_LEN + 1]; unsigned int bundleLength; int ductSocket = -1; int bytesSent; if (bpAttach() < 0) { putErrmsg("udpclo can't attach to BP.", NULL); return -1; } buffer = MTAKE(UDPCLA_BUFSZ); if (buffer == NULL) { putErrmsg("No memory for UDP buffer in udpclo.", NULL); return -1; } findOutduct("udp", "*", &vduct, &vductElt); if (vductElt == 0) { putErrmsg("No such udp duct.", "*"); MRELEASE(buffer); return -1; } if (vduct->cloPid > 0 && vduct->cloPid != sm_TaskIdSelf()) { putErrmsg("CLO task is already started for this duct.", itoa(vduct->cloPid)); MRELEASE(buffer); return -1; } /* All command-line arguments are now validated. */ sdr = getIonsdr(); sdr_read(sdr, (char *) &outduct, sdr_list_data(sdr, vduct->outductElt), sizeof(Outduct)); sdr_read(sdr, (char *) &protocol, outduct.protocol, sizeof(ClProtocol)); if (protocol.nominalRate <= 0) { vduct->xmitThrottle.nominalRate = DEFAULT_UDP_RATE; } else { vduct->xmitThrottle.nominalRate = protocol.nominalRate; } memset((char *) outflows, 0, sizeof outflows); outflows[0].outboundBundles = outduct.bulkQueue; outflows[1].outboundBundles = outduct.stdQueue; outflows[2].outboundBundles = outduct.urgentQueue; for (i = 0; i < 3; i++) { outflows[i].svcFactor = 1 << i; } /* Set up signal handling. SIGTERM is shutdown signal. */ oK(udpcloSemaphore(&(vduct->semaphore))); isignal(SIGTERM, shutDownClo); /* Can now begin transmitting to remote duct. */ writeMemo("[i] udpclo is running."); while (!(sm_SemEnded(vduct->semaphore))) { if (bpDequeue(vduct, outflows, &bundleZco, &extendedCOS, destDuctName) < 0) { sm_SemEnd(udpcloSemaphore(NULL));/* Stop. */ continue; } if (bundleZco == 0) /* Interrupted. */ { continue; } hostName = destDuctName; parseSocketSpec(destDuctName, &portNbr, &hostNbr); if (portNbr == 0) { portNbr = BpUdpDefaultPortNbr; } portNbr = htons(portNbr); if (hostNbr == 0) { writeMemoNote("[?] Can't get IP address for host", hostName); } hostNbr = htonl(hostNbr); 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 *) &hostNbr, 4); bundleLength = zco_length(sdr, bundleZco); bytesSent = sendBundleByUDP(&socketName, &ductSocket, bundleLength, bundleZco, buffer); if (bytesSent < bundleLength) { sm_SemEnd(udpcloSemaphore(NULL));/* Stop. */ continue; } /* Make sure other tasks have a chance to run. */ sm_TaskYield(); } if (ductSocket != -1) { close(ductSocket); } writeErrmsgMemos(); writeMemo("[i] udpclo duct has ended."); MRELEASE(buffer); ionDetach(); return 0; }