Austin Schuh | 8d0a285 | 2019-12-28 22:54:28 -0800 | [diff] [blame^] | 1 | /* -*-c-*- |
| 2 | ** |
| 3 | ** sctp-tools: Another bindx test. |
| 4 | ** |
| 5 | ** $Id: bindx_test.c,v 1.1.1.1 2002/08/06 22:31:05 inaky Exp $ |
| 6 | ** |
| 7 | ** Distributed under the terms of the GPL v2.0 as described in |
| 8 | ** $top_srcdir/COPYING. |
| 9 | ** |
| 10 | ** (C) Copyright IBM Corp. 2003 |
| 11 | ** (C) 2002 Intel Corporation |
| 12 | ** Iñaky Pérez-González <inaky.perez-gonzalez@intel.com>: |
| 13 | ** Sridhar Samudrala <sri@us.ibm.com> |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE /* GNU extensions */ |
| 17 | |
| 18 | #include <stdlib.h> /* malloc() */ |
| 19 | #include <arpa/inet.h> /* inet_pton() */ |
| 20 | #include <errno.h> |
| 21 | #include <sys/socket.h> /* socket() */ |
| 22 | #include <stdio.h> /* fprintf */ |
| 23 | #include <netinet/in.h> /* sockaddr_in */ |
| 24 | #include <unistd.h> /* close() */ |
| 25 | #include <string.h> /* strchr() */ |
| 26 | #include <netinet/sctp.h> /* bindx() */ |
| 27 | |
| 28 | /* Global stuff */ |
| 29 | |
| 30 | #ifndef IPPROTO_SCTP |
| 31 | #define IPPROTO_SCTP 132 |
| 32 | #endif |
| 33 | |
| 34 | /*! Main function: initialize, setup, run the main loop |
| 35 | ** |
| 36 | ** |
| 37 | */ |
| 38 | |
| 39 | int main (int argc, char **argv) |
| 40 | { |
| 41 | void *addr_buf, *buf_ptr; |
| 42 | void *addr_buf_size = 0; |
| 43 | size_t addrs, cnt; |
| 44 | int sd, result, port; |
| 45 | int domain = PF_INET6; |
| 46 | |
| 47 | if (argc < 3) { |
| 48 | fprintf(stderr, |
| 49 | "Usage: bindx_test PORT IPADDR1 [IPADDR2 [...]]\n"); |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | port = atoi(argv[1]); |
| 54 | printf("bindx_test: INFO: Port is %d\n", port); |
| 55 | |
| 56 | /* Allocate the maximum space for the specified no. of addresses. |
| 57 | * Assume all of them are v6 addresses. |
| 58 | */ |
| 59 | addr_buf = malloc((argc -2) * sizeof(struct sockaddr_in6)); |
| 60 | if (addr_buf == NULL) { |
| 61 | perror("bindx_test: ERROR: addr buf allocation failed"); |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | /* Get the addresses from the cmd line */ |
| 66 | addrs = 0; /* healthy address iterator [and total counter] */ |
| 67 | cnt = 2; /* argument iterator */ |
| 68 | buf_ptr = addr_buf; |
| 69 | while (cnt < argc) { |
| 70 | printf("bindx_test: INFO: Arg %zu: %s", cnt, argv[cnt]); |
| 71 | fflush(stderr); |
| 72 | if (strchr(argv[cnt], ':')) { |
| 73 | struct sockaddr_in6 *sa6; |
| 74 | |
| 75 | sa6 = (struct sockaddr_in6 *)buf_ptr; |
| 76 | printf(" IPv6 address number %zu", addrs); |
| 77 | sa6->sin6_family = AF_INET6; |
| 78 | sa6->sin6_port = port; |
| 79 | if (inet_pton(AF_INET6, argv[cnt], &sa6->sin6_addr)) { |
| 80 | addrs++; |
| 81 | addr_buf_size += sizeof(struct sockaddr_in6); |
| 82 | buf_ptr += sizeof(struct sockaddr_in6); |
| 83 | } else |
| 84 | printf(" error"); |
| 85 | } else if (strchr(argv[cnt], '.')) { |
| 86 | struct sockaddr_in *sa; |
| 87 | |
| 88 | domain = PF_INET; |
| 89 | sa = (struct sockaddr_in *)buf_ptr; |
| 90 | printf (" IPv4 address number %zu", addrs); |
| 91 | sa->sin_family = AF_INET; |
| 92 | sa->sin_port = port; |
| 93 | if (inet_pton (AF_INET, argv[cnt], &sa->sin_addr)) { |
| 94 | addrs++; |
| 95 | addr_buf_size += sizeof(struct sockaddr_in); |
| 96 | buf_ptr += sizeof(struct sockaddr_in); |
| 97 | } else |
| 98 | printf (" error"); |
| 99 | } else |
| 100 | printf (" Unknown"); |
| 101 | putchar ('\n'); |
| 102 | cnt++; |
| 103 | } |
| 104 | |
| 105 | printf ("bindx_test: INFO: Got %zu addrs\n", addrs); |
| 106 | |
| 107 | /* Create the socket */ |
| 108 | sd = socket(domain, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 109 | if (sd == -1) { |
| 110 | perror("bindx_test: ERROR: Cannot open socket"); |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | /* add all */ |
| 115 | result = sctp_bindx(sd, (struct sockaddr *)addr_buf, addrs, |
| 116 | SCTP_BINDX_ADD_ADDR); |
| 117 | if (result == -1) |
| 118 | perror("bindx_test: ERROR: bindx addition error"); |
| 119 | else { |
| 120 | printf("bindx_test: OK: bindx address addition\n"); |
| 121 | |
| 122 | /* remove all but the last */ |
| 123 | result = sctp_bindx(sd, (struct sockaddr *)addr_buf, addrs-1, |
| 124 | SCTP_BINDX_REM_ADDR); |
| 125 | if (result == -1) |
| 126 | perror("bindx_test: ERROR: bindx address removal"); |
| 127 | else |
| 128 | printf("bindx_test: OK: bindx address removal\n"); |
| 129 | } |
| 130 | |
| 131 | close(sd); |
| 132 | free(addr_buf); |
| 133 | return result; |
| 134 | } |