[ Team LiB ] Previous Section Next Section

23.8 Finding an Association ID Given an IP Address

In the recent changes we made to our client in Section 23.7, the client used the association notification to trigger retrieving the list of addresses. This notification was quite convenient since it held the association's identification in the sac_assoc_id field. But, if the application is not tracking association identifications and only has an address of a peer, how can it find an association's identification? In Figure 23.13, we illustrate a simple function that translates a peer's address into an association ID. The server will use this function later in Section 23.10.

Initialize

7–8 Our function first initializes its sctp_paddrparams structure.

Copy address

9 We copy the address, using the passed length, into the sctp_paddrparams structure.

Call socket option

10 The function now uses the SCTP_PEER_ADDR_PARAMS socket option to request peer address parameters. Note that we use sctp_opt_info, instead of getsockopt, since the SCTP_PEER_ADDR_PARAMS socket option requires copying arguments both into and out of the kernel. This call will return the current heartbeat interval, the maximum number of retransmissions before the SCTP implementation considers the peer address to have failed, and most importantly, the association ID. Note that we do not check the return value, since if the call fails, we want to return 0.

Figure 23.13 Translate an address to an association ID.

sctp/sctp_addr_to_associd.c

 1 #include    "unp.h"

 2 sctp_assoc_t
 3 sctp_address_to_associd(int sock_fd, struct sockaddr *sa, socklen_t salen)
 4 {
 5     struct sctp_paddrparams sp;
 6     int     siz;

 7     siz = sizeof(struct sctp_paddrparams);
 8     bzero(&sp, siz);
 9     memcpy(&sp.spp_address, sa, salen);
10     sctp_opt_info(sock_fd, 0, SCTP_PEER_ADDR_PARAMS, &sp, &siz);
11     return (sp.spp_assoc_id);
12 }

11 The function returns the association ID to the caller. Note that if the call fails, the earlier clearing of the structure will assure our caller of getting a 0 as the returned association ID. An association ID of 0 is not allowed and is used to indicate no association by the SCTP implementation as well.

    [ Team LiB ] Previous Section Next Section