Changeset 38:68c1890f7049 in freeDiameter for freeDiameter/sctp.c
- Timestamp:
- Nov 5, 2009, 5:29:12 PM (15 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
freeDiameter/sctp.c
r33 r38 641 641 } 642 642 643 /* Create a client socket and connect to remote server*/644 int fd_sctp_client( int *sock, int no_ip6, uint16_t port, struct fd_list * list)643 /* Add addresses from the list that match the flags to the array */ 644 static int add_addresses_from_list_mask(uint8_t ** array, int * count, size_t * offset, uint16_t port, struct fd_list * list, uint32_t mask, uint32_t val) 645 645 { 646 int family; 647 int count = 0; 648 size_t offset = 0, sz; 646 size_t sz; 647 struct fd_list * li; 649 648 union { 650 649 uint8_t *buf; 651 sSA *sa;652 } sar;653 union {654 uint8_t *buf;655 sSA *sa;656 650 sSA4 *sin; 657 651 sSA6 *sin6; 658 652 } ptr; 659 struct fd_list * li; 660 int ret; 661 662 sar.buf = NULL; 663 664 TRACE_ENTRY("%p %i %hu %p", sock, no_ip6, port, list); 665 CHECK_PARAMS( sock && list && (!FD_IS_LIST_EMPTY(list)) ); 666 667 if (no_ip6) { 668 family = AF_INET; 669 } else { 670 family = AF_INET6; 671 } 672 673 /* Create the socket */ 674 CHECK_SYS( *sock = socket(family, SOCK_STREAM, IPPROTO_SCTP) ); 675 676 /* Cleanup if we are cancelled */ 677 pthread_cleanup_push(fd_cleanup_socket, sock); 678 679 /* Set the socket options */ 680 CHECK_FCT_DO( ret = fd_setsockopt_prebind(*sock), goto fail ); 681 682 /* Create the array of addresses for sctp_connectx */ 653 683 654 for (li = list->next; li != list; li = li->next) { 684 655 struct fd_endpoint * ep = (struct fd_endpoint *) li; 685 656 686 count++; 687 688 /* Size of the new SA we are adding (sar may contain a mix of sockaddr_in and sockaddr_in6) */ 657 /* Do the flag match ? */ 658 if ((val & mask) != (ep->flags & mask)) 659 continue; 660 661 /* We add this endpoint at the end of array */ 662 (*count)++; 663 664 /* Size of the new SA we are adding (array may contain a mix of sockaddr_in and sockaddr_in6) */ 689 665 #ifndef SCTP_USE_MAPPED_ADDRESSES 690 666 if (ep->sa.sa_family == AF_INET6) … … 696 672 sz = sizeof(sSA4); 697 673 698 /* augment sarto contain the additional info */699 CHECK_MALLOC _DO( sar.buf = realloc(sar.buf, offset + sz), { ret = ENOMEM; goto fail; });700 701 ptr.buf = sar.buf +offset; /* place of the new SA */702 offset+= sz; /* update to end of sar */674 /* augment array to contain the additional info */ 675 CHECK_MALLOC( *array = realloc(*array, (*offset) + sz) ); 676 677 ptr.buf = *array + *offset; /* place of the new SA */ 678 (*offset) += sz; /* update to end of sar */ 703 679 704 680 if (sz == sizeof(sSA4)) { 705 681 memcpy(ptr.buf, &ep->sin, sz); 706 ptr.sin->sin_port = htons(port);682 ptr.sin->sin_port = port; 707 683 } else { 708 684 if (ep->sa.sa_family == AF_INET) { /* We must map the address */ … … 713 689 memcpy(ptr.sin6, &ep->sin6, sz); 714 690 } 715 ptr.sin6->sin6_port = htons(port); 716 } 717 } 691 ptr.sin6->sin6_port = port; 692 } 693 } 694 695 return 0; 696 } 697 698 /* Create a client socket and connect to remote server */ 699 int fd_sctp_client( int *sock, int no_ip6, uint16_t port, struct fd_list * list ) 700 { 701 int family; 702 int count = 0; 703 size_t offset = 0; 704 union { 705 uint8_t *buf; 706 sSA *sa; 707 } sar; 708 int ret; 709 710 sar.buf = NULL; 711 712 TRACE_ENTRY("%p %i %hu %p", sock, no_ip6, port, list); 713 CHECK_PARAMS( sock && list && (!FD_IS_LIST_EMPTY(list)) ); 714 715 if (no_ip6) { 716 family = AF_INET; 717 } else { 718 family = AF_INET6; 719 } 720 721 /* Create the socket */ 722 CHECK_SYS( *sock = socket(family, SOCK_STREAM, IPPROTO_SCTP) ); 723 724 /* Cleanup if we are cancelled */ 725 pthread_cleanup_push(fd_cleanup_socket, sock); 726 727 /* Set the socket options */ 728 CHECK_FCT_DO( ret = fd_setsockopt_prebind(*sock), goto fail ); 729 730 /* Create the array of addresses, add first the configured addresses, then the discovered, then the other ones */ 731 CHECK_FCT_DO( ret = add_addresses_from_list_mask(&sar.buf, &count, &offset, htons(port), list, EP_FL_CONF, EP_FL_CONF ), goto fail ); 732 CHECK_FCT_DO( ret = add_addresses_from_list_mask(&sar.buf, &count, &offset, htons(port), list, EP_FL_CONF | EP_FL_DISC, EP_FL_DISC ), goto fail ); 733 CHECK_FCT_DO( ret = add_addresses_from_list_mask(&sar.buf, &count, &offset, htons(port), list, EP_FL_CONF | EP_FL_DISC, 0 ), goto fail ); 718 734 719 735 /* Try connecting */
Note: See TracChangeset
for help on using the changeset viewer.