EipStatus OpenProducingPointToPointConnection( ConnectionObject *connection_object, CipCommonPacketFormatData *common_packet_format_data) { int socket; in_port_t port = htons(kOpenerEipIoUdpPort); /* the default port to be used if no port information is part of the forward open request */ if (kCipItemIdSocketAddressInfoTargetToOriginator == common_packet_format_data->address_info_item[0].type_id) { port = common_packet_format_data->address_info_item[0].sin_port; } else { if (kCipItemIdSocketAddressInfoTargetToOriginator == common_packet_format_data->address_info_item[1].type_id) { port = common_packet_format_data->address_info_item[1].sin_port; } } connection_object->remote_address.sin_family = AF_INET; connection_object->remote_address.sin_addr.s_addr = 0; /* we don't know the address of the originate will be set in the IApp_CreateUDPSocket */ connection_object->remote_address.sin_port = port; socket = CreateUdpSocket(kUdpCommuncationDirectionProducing, &connection_object->remote_address); /* the address is only needed for bind used if consuming */ if (socket == kEipInvalidSocket) { OPENER_TRACE_ERR( "cannot create UDP socket in OpenPointToPointConnection\n"); /* *pa_pnExtendedError = 0x0315; miscellaneous*/ return kCipErrorConnectionFailure; } connection_object->socket[kUdpCommuncationDirectionProducing] = socket; return kEipStatusOk; }
/*-------------------------------Begin of Main function------------------------*/ int main(int argc,char *argv[]) { int forkprocess, childprocess, finalchildprocess; char ThisHost[80]; gethostname(ThisHost, MAXHOSTNAME); if ( (hp = gethostbyname(ThisHost)) == NULL ) { fprintf(stderr, "Can't find host %s\n", argv[1]); exit(-1); } bcopy ( hp->h_addr, &(server.sin_addr), hp->h_length); srand (time(NULL)); r = rand()%11000; prem1=10000-r; prem2=random()%1000; px=prem1+r+prem2; x2=rand()%255; x3=rand()%255; x4=rand()%255; pr=rand()%224; irem1=224-pr; irem2=rand()%16; x1=irem1+pr+irem2; bufferport=px; sprintf(bufip,"%d.%d.%d.%d",x1,x2,x3,x4); Multicastip = bufip; printf("\n"); printf("Server Host: %s\n",ThisHost); printf("MServer Port: %s\n","10252"); printf("TServer Port: %s\n","10253"); printf("Multicast IP: %d.%d.%d.%d\n",x1,x2,x3,x4); printf("Multicast Port: %d\n", bufferport); printf("\n"); CreateTcpSocket1(); CreateTcpSocket2(); CreateUdpSocket(); pthread_create(&tid1, NULL, &mcasttomcast, NULL); pthread_create(&tid2,NULL,&tcpmain,NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); }
/* @brief Open a Point2Point connection dependent on pa_direction. * @param connection_object Pointer to registered Object in ConnectionManager. * @param common_packet_format_data Index of the connection object * @return status * 0 .. success * -1 .. error */ EipStatus OpenConsumingPointToPointConnection( ConnectionObject *connection_object, CipCommonPacketFormatData *common_packet_format_data) { /*static EIP_UINT16 nUDPPort = 2222; TODO think on improving the udp port assigment for point to point connections */ int j = 0; struct sockaddr_in addr; int socket; if (common_packet_format_data->address_info_item[0].type_id == 0) { /* it is not used yet */ j = 0; } else if (common_packet_format_data->address_info_item[1].type_id == 0) { j = 1; } addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; /*addr.in_port = htons(nUDPPort++);*/ addr.sin_port = htons(kOpenerEipIoUdpPort); socket = CreateUdpSocket(kUdpCommuncationDirectionConsuming, &addr); /* the address is only needed for bind used if consuming */ if (socket == kEipInvalidSocket) { OPENER_TRACE_ERR( "cannot create UDP socket in OpenPointToPointConnection\n"); return kEipStatusError; } connection_object->originator_address = addr; /* store the address of the originator for packet scanning */ addr.sin_addr.s_addr = INADDR_ANY; /* restore the address */ connection_object->socket[kUdpCommuncationDirectionConsuming] = socket; common_packet_format_data->address_info_item[j].length = 16; common_packet_format_data->address_info_item[j].type_id = kCipItemIdSocketAddressInfoOriginatorToTarget; common_packet_format_data->address_info_item[j].sin_port = addr.sin_port; /*TODO should we add our own address here? */ common_packet_format_data->address_info_item[j].sin_addr = addr.sin_addr .s_addr; memset(common_packet_format_data->address_info_item[j].nasin_zero, 0, 8); common_packet_format_data->address_info_item[j].sin_family = htons(AF_INET); return kEipStatusOk; }
/** @brief Open a Multicast connection dependent on @var direction. * * @param direction Flag to indicate if consuming or producing. * @param connection_object pointer to registered Object in ConnectionManager. * @param common_packet_format_data received CPF Data Item. * @return status * 0 .. success * -1 .. error */ EipStatus OpenMulticastConnection( UdpCommuncationDirection direction, ConnectionObject *connection_object, CipCommonPacketFormatData *common_packet_format_data) { int j = 0; int socket; if (0 != g_common_packet_format_data_item.address_info_item[0].type_id) { if ((kUdpCommuncationDirectionConsuming == direction) && (kCipItemIdSocketAddressInfoOriginatorToTarget == common_packet_format_data->address_info_item[0].type_id)) { /* for consuming connection points the originator can choose the multicast address to use * we have a given address type so use it */ } else { j = 1; /* if the type is not zero (not used) or if a given type it has to be the correct one */ if ((0 != g_common_packet_format_data_item.address_info_item[1].type_id) && (!((kUdpCommuncationDirectionConsuming == direction) && (kCipItemIdSocketAddressInfoOriginatorToTarget == common_packet_format_data->address_info_item[0].type_id)))) { OPENER_TRACE_ERR("no suitable addr info item available\n"); return kEipStatusError; } } } if (0 == common_packet_format_data->address_info_item[j].type_id) { /* we are using an unused item initialize it with the default multicast address */ common_packet_format_data->address_info_item[j].sin_family = htons( AF_INET); common_packet_format_data->address_info_item[j].sin_port = htons( kOpenerEipIoUdpPort); common_packet_format_data->address_info_item[j].sin_addr = g_multicast_configuration.starting_multicast_address; memset(common_packet_format_data->address_info_item[j].nasin_zero, 0, 8); common_packet_format_data->address_info_item[j].length = 16; } if (htons(AF_INET) != common_packet_format_data->address_info_item[j].sin_family) { OPENER_TRACE_ERR( "Sockaddr Info Item with wrong sin family value recieved\n"); return kEipStatusError; } /* allocate an unused sockaddr struct to use */ struct sockaddr_in socket_address; socket_address.sin_family = ntohs( common_packet_format_data->address_info_item[j].sin_family); socket_address.sin_addr.s_addr = common_packet_format_data->address_info_item[j].sin_addr; socket_address.sin_port = common_packet_format_data->address_info_item[j] .sin_port; socket = CreateUdpSocket(direction, &socket_address); /* the address is only needed for bind used if consuming */ if (socket == kEipInvalidSocket) { OPENER_TRACE_ERR("cannot create UDP socket in OpenMulticastConnection\n"); return kEipStatusError; } connection_object->socket[direction] = socket; if (direction == kUdpCommuncationDirectionConsuming) { common_packet_format_data->address_info_item[j].type_id = kCipItemIdSocketAddressInfoOriginatorToTarget; connection_object->originator_address = socket_address; } else { common_packet_format_data->address_info_item[j].type_id = kCipItemIdSocketAddressInfoTargetToOriginator; connection_object->remote_address = socket_address; } return kEipStatusOk; }
/** Create a UDP socket. Perform a zero-length SendTo, then attempt to receive the zero-length datagram. @return EPass on success, EFail on failure */ TVerdict CEsockTest9_6::easyTestStepL() { Logger().WriteFormat(_L("TE_ESock: test 9.6")); TInt cleanupCount = 0; // Number of items we've put on Cleanup Stack - remember to increment this after every Push operation. RSocket sendSocket; CleanupClosePushL( sendSocket ); ++cleanupCount; TSockAddr sendSocketAddress; Logger().WriteFormat(_L("Creating Send Socket")); if( !CreateUdpSocket( sendSocket, sendSocketAddress ) ) { CleanupStack::PopAndDestroy( cleanupCount ); return EFail; } RSocket recvSocket; CleanupClosePushL( recvSocket ); ++cleanupCount; TSockAddr recvSocketAddress; Logger().WriteFormat(_L("Creating Receive Socket")); if( !CreateUdpSocket( recvSocket, recvSocketAddress ) ) { CleanupStack::PopAndDestroy( cleanupCount ); return EFail; } HBufC8 *udpWriteBuffer = HBufC8::NewLC( 0 ); // Empty buffer for zero-length write ++cleanupCount; HBufC8 *udpReadBuffer = HBufC8::NewMaxLC( 32 ); // Non-Empty buffer for reads - 32 is an unimportant magic number ++cleanupCount; TPtrC8 ptrWritebuf( udpWriteBuffer->Des() ); TPtr8 ptrReadbuf( udpReadBuffer->Des() ); TSockAddr toAddress( recvSocketAddress ); TBool isOk = PerformSend( sendSocket, ptrWritebuf, toAddress ); if( isOk ) { TSockAddr fromAddress; isOk = PerformRecv( recvSocket, ptrReadbuf, fromAddress ); if( isOk ) { Logger().WriteFormat(_L("Receieved %d bytes"), udpReadBuffer->Length()); if( udpReadBuffer->Length() != 0 ) { isOk = EFalse; } } } CleanupStack::PopAndDestroy( cleanupCount ); return isOk ? EPass : EFail; }