Class: SCTP::Socket

Inherits:
Object
  • Object
show all
Defined in:
ext/sctp/socket.c

Defined Under Namespace

Classes: Client, Server

Constant Summary collapse

VERSION =

The version of this library

0.0.5
SCTP_UNORDERED =

Message is unordered

INT2NUM(SCTP_UNORDERED)
SCTP_ADDR_OVER =

Override the primary address

INT2NUM(SCTP_ADDR_OVER)
SCTP_ABORT =

Send an ABORT to peer

INT2NUM(SCTP_ABORT)
SCTP_EOF =

Start a shutdown procedure

INT2NUM(SCTP_EOF)
SCTP_SENDALL =

Send to all associations

INT2NUM(SCTP_SENDALL)
MSG_NOTIFICATION =
INT2NUM(MSG_NOTIFICATION)
SCTP_EMPTY =

ASSOCIATION STATES //

INT2NUM(SCTP_EMPTY)
SCTP_CLOSED =
INT2NUM(SCTP_CLOSED)
INT2NUM(SCTP_COOKIE_WAIT)
INT2NUM(SCTP_COOKIE_ECHOED)
SCTP_ESTABLISHED =
INT2NUM(SCTP_ESTABLISHED)
SCTP_SHUTDOWN_PENDING =
INT2NUM(SCTP_SHUTDOWN_PENDING)
SCTP_SHUTDOWN_SENT =
INT2NUM(SCTP_SHUTDOWN_SENT)
SCTP_SHUTDOWN_RECEIVED =
INT2NUM(SCTP_SHUTDOWN_RECEIVED)
SCTP_SHUTDOWN_ACK_SENT =
INT2NUM(SCTP_SHUTDOWN_ACK_SENT)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

Create and return a new SCTP::Socket instance. You may optionally pass in a domain (aka family) value and socket type. By default these are AF_INET and SOCK_SEQPACKET, respectively.

There are only two supported families: SOCK_SEQPACKET for the creation of a one-to-many socket, and SOCK_STREAM for the creation of a one-to-one socket.

Example:

require 'socket'
require 'sctp/socket'

socket1 = SCTP::Socket.new
socket2 = SCTP::Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'ext/sctp/socket.c', line 96

static VALUE rsctp_init(int argc, VALUE* argv, VALUE self){
  int sock_fd;
  VALUE v_domain, v_type;

  rb_scan_args(argc, argv, "02", &v_domain, &v_type);

  if(NIL_P(v_domain))
    v_domain = INT2NUM(AF_INET);
  
  if(NIL_P(v_type))
    v_type = INT2NUM(SOCK_SEQPACKET);

  sock_fd = socket(NUM2INT(v_domain), NUM2INT(v_type), IPPROTO_SCTP);

  if(sock_fd < 0)
    rb_raise(rb_eSystemCallError, "socket: %s", strerror(errno));

  rb_iv_set(self, "@domain", v_domain);
  rb_iv_set(self, "@type", v_type);
  rb_iv_set(self, "@sock_fd", INT2NUM(sock_fd));
  rb_iv_set(self, "@association_id", INT2NUM(0));

  return self;
}

Instance Attribute Details

#association_idObject

#domainObject

#portObject

#sock_fdObject

#typeObject

Instance Method Details

#bind(*args) ⇒ Object

Bind a subset of IP addresses associated with the host system on the given port, or a port assigned by the operating system if none is provided.

Note that you can both add or remove an address to or from the socket using the SCTP_BINDX_ADD_ADDR (default) or SCTP_BINDX_REM_ADDR constants, respectively.

Example:

socket = SCTP::Socket.new

# Bind 2 addresses
socket.bind(:port => 64325, :addresses => ['10.0.4.5', '10.0.5.5'])

# Remove 1 later
socket.bind(:addresses => ['10.0.4.5'], :flags => SCTP::Socket::BINDX_REM_ADDR)

If no addresses are specified, then it will bind to all available interfaces. If no port is specified, then one will be assigned by the host.

Returns the port that it was bound to.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/sctp/socket.c', line 144

static VALUE rsctp_bind(int argc, VALUE* argv, VALUE self){
  struct sockaddr_in addrs[8];
  int i, sock_fd, num_ip, flags, domain, port;
  VALUE v_addresses, v_port, v_flags, v_address, v_options;

  rb_scan_args(argc, argv, "01", &v_options);

  bzero(&addrs, sizeof(addrs));

  if(NIL_P(v_options))
    v_options = rb_hash_new();

  v_addresses = rb_hash_aref2(v_options, "addresses");
  v_flags = rb_hash_aref2(v_options, "flags");
  v_port = rb_hash_aref2(v_options, "port");

  if(NIL_P(v_port))
    port = 0;
  else
    port = NUM2INT(v_port);

  if(NIL_P(v_flags))
    flags = SCTP_BINDX_ADD_ADDR;
  else
    flags = NUM2INT(v_flags);

  if(NIL_P(v_addresses))
    num_ip = 1;
  else
    num_ip = RARRAY_LEN(v_addresses);

  domain = NUM2INT(rb_iv_get(self, "@domain"));
  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));

  if(num_ip > 1){
    for(i = 0; i < num_ip; i++){
      v_address = RARRAY_PTR(v_addresses)[i];
      addrs[i].sin_family = domain;
      addrs[i].sin_port = htons(port);
      addrs[i].sin_addr.s_addr = inet_addr(StringValueCStr(v_address));
    }
  }
  else{
    addrs[0].sin_family = domain;
    addrs[0].sin_port = htons(port);
    addrs[0].sin_addr.s_addr = htonl(INADDR_ANY);
  }

  if(sctp_bindx(sock_fd, (struct sockaddr *) addrs, num_ip, flags) != 0)
    rb_raise(rb_eSystemCallError, "sctp_bindx: %s", strerror(errno));

  if(port == 0){
    struct sockaddr_in sin;
    socklen_t len = sizeof(sin);

    if(getsockname(sock_fd, (struct sockaddr *)&sin, &len) == -1)
      rb_raise(rb_eSystemCallError, "getsockname: %s", strerror(errno));

    port = sin.sin_port;
  }

  return INT2NUM(port);
}

#closeObject

Close the socket. You should always do this.

Example:

socket = SCTP::Socket.new
socket.close


271
272
273
274
275
276
277
278
# File 'ext/sctp/socket.c', line 271

static VALUE rsctp_close(VALUE self){
  VALUE v_sock_fd = rb_iv_get(self, "@sock_fd");

  if(close(NUM2INT(v_sock_fd)))
    rb_raise(rb_eSystemCallError, "close: %s", strerror(errno));

  return self;
}

#connect(*args) ⇒ Object

Connect the socket to a multihomed peer via the provided array of addresses using the domain specified in the constructor. You must also specify the port.

Example:

socket = SCTP::Socket.new
socket.connect(:port => 62354, :addresses => ['10.0.4.5', '10.0.5.5'])

Note that this will also set/update the object’s association_id.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'ext/sctp/socket.c', line 219

static VALUE rsctp_connect(int argc, VALUE* argv, VALUE self){
  struct sockaddr_in addrs[8];
  int i, num_ip, sock_fd;
  sctp_assoc_t assoc;
  VALUE v_address, v_domain, v_options, v_addresses, v_port;

  rb_scan_args(argc, argv, "01", &v_options);

  if(NIL_P(v_options))
    rb_raise(rb_eArgError, "you must specify an array of addresses");

  Check_Type(v_options, T_HASH);

  v_addresses = rb_hash_aref2(v_options, "addresses");
  v_port = rb_hash_aref2(v_options, "port");

  if(NIL_P(v_addresses) || RARRAY_LEN(v_addresses) == 0)
    rb_raise(rb_eArgError, "you must specify an array of addresses containing at least one address");

  if(NIL_P(v_port))
    rb_raise(rb_eArgError, "you must specify a port");

  v_domain = rb_iv_get(self, "@domain");

  num_ip = RARRAY_LEN(v_addresses);
  bzero(&addrs, sizeof(addrs));

  for(i = 0; i < num_ip; i++){
    v_address = RARRAY_PTR(v_addresses)[i];
    addrs[i].sin_family = NUM2INT(v_domain);
    addrs[i].sin_port = htons(NUM2INT(v_port));
    addrs[i].sin_addr.s_addr = inet_addr(StringValueCStr(v_address));
  }

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));

  if(sctp_connectx(sock_fd, (struct sockaddr *) addrs, num_ip, &assoc) < 0)
    rb_raise(rb_eSystemCallError, "sctp_connectx: %s", strerror(errno));

  rb_iv_set(self, "@association_id", INT2NUM(assoc));

  return self;
}

#get_association_infoObject



1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'ext/sctp/socket.c', line 1074

static VALUE rsctp_get_association_info(VALUE self){
  int sock_fd;
  socklen_t size;
  sctp_assoc_t assoc_id;
  struct sctp_assocparams assoc;

  bzero(&assoc, sizeof(assoc));

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));
  assoc_id = NUM2INT(rb_iv_get(self, "@association_id"));
  size = sizeof(struct sctp_assocparams);

  if(sctp_opt_info(sock_fd, assoc_id, SCTP_ASSOCINFO, (void*)&assoc, &size) < 0)
    rb_raise(rb_eSystemCallError, "sctp_opt_info: %s", strerror(errno));

  return rb_struct_new(
    v_sctp_associnfo_struct,
    INT2NUM(assoc.sasoc_assoc_id),
    INT2NUM(assoc.sasoc_asocmaxrxt),
    INT2NUM(assoc.sasoc_number_peer_destinations),
    INT2NUM(assoc.sasoc_peer_rwnd),
    INT2NUM(assoc.sasoc_local_rwnd),
    INT2NUM(assoc.sasoc_cookie_life)
  );
}

#get_default_send_paramsObject



1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
# File 'ext/sctp/socket.c', line 1045

static VALUE rsctp_get_default_send_params(VALUE self){
  int sock_fd;
  socklen_t size;
  sctp_assoc_t assoc_id;
  struct sctp_sndrcvinfo sndrcv;

  bzero(&sndrcv, sizeof(sndrcv));

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));
  assoc_id = NUM2INT(rb_iv_get(self, "@association_id"));
  size = sizeof(struct sctp_sndrcvinfo);

  if(sctp_opt_info(sock_fd, assoc_id, SCTP_DEFAULT_SEND_PARAM, (void*)&sndrcv, &size) < 0)
    rb_raise(rb_eSystemCallError, "sctp_opt_info: %s", strerror(errno));

  return rb_struct_new(
    v_sctp_default_send_params_struct,
    INT2NUM(sndrcv.sinfo_stream),
    INT2NUM(sndrcv.sinfo_ssn),
    INT2NUM(sndrcv.sinfo_flags),
    INT2NUM(sndrcv.sinfo_ppid),
    INT2NUM(sndrcv.sinfo_context),
    INT2NUM(sndrcv.sinfo_timetolive),
    INT2NUM(sndrcv.sinfo_tsn),
    INT2NUM(sndrcv.sinfo_cumtsn),
    INT2NUM(sndrcv.sinfo_assoc_id)
  );
}

#get_retransmission_infoObject



1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'ext/sctp/socket.c', line 1122

static VALUE rsctp_get_retransmission_info(VALUE self){
  int sock_fd;
  socklen_t size;
  sctp_assoc_t assoc_id;
  struct sctp_rtoinfo rto;

  bzero(&rto, sizeof(rto));

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));
  assoc_id = NUM2INT(rb_iv_get(self, "@association_id"));
  size = sizeof(struct sctp_rtoinfo);

  if(sctp_opt_info(sock_fd, assoc_id, SCTP_RTOINFO, (void*)&rto, &size) < 0)
    rb_raise(rb_eSystemCallError, "sctp_opt_info: %s", strerror(errno));

  return rb_struct_new(
    v_sctp_rtoinfo_struct,
    INT2NUM(rto.srto_assoc_id),
    INT2NUM(rto.srto_initial),
    INT2NUM(rto.srto_max),
    INT2NUM(rto.srto_min)
  );
}

#get_statusObject



1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'ext/sctp/socket.c', line 1146

static VALUE rsctp_get_status(VALUE self){
  int sock_fd;
  socklen_t size;
  sctp_assoc_t assoc_id;
  struct sctp_status status;
  struct sctp_paddrinfo* spinfo;
  char tmpname[INET_ADDRSTRLEN];

  bzero(&status, sizeof(status));

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));
  assoc_id = NUM2INT(rb_iv_get(self, "@association_id"));
  size = sizeof(struct sctp_status);

  if(sctp_opt_info(sock_fd, assoc_id, SCTP_STATUS, (void*)&status, &size) < 0)
    rb_raise(rb_eSystemCallError, "sctp_opt_info: %s", strerror(errno));

  spinfo = &status.sstat_primary;

  if (spinfo->spinfo_address.ss_family == AF_INET6) {
    struct sockaddr_in6 *sin6;
    sin6 = (struct sockaddr_in6 *)&spinfo->spinfo_address;
    inet_ntop(AF_INET6, &sin6->sin6_addr, tmpname, sizeof (tmpname));
  }
  else {
    struct sockaddr_in *sin;
    sin = (struct sockaddr_in *)&spinfo->spinfo_address;
    inet_ntop(AF_INET, &sin->sin_addr, tmpname, sizeof (tmpname));
  }

  return rb_struct_new(v_sctp_status_struct,
    INT2NUM(status.sstat_assoc_id),
    INT2NUM(status.sstat_state),
    INT2NUM(status.sstat_rwnd),
    INT2NUM(status.sstat_unackdata),
    INT2NUM(status.sstat_penddata),
    INT2NUM(status.sstat_instrms),
    INT2NUM(status.sstat_outstrms),
    INT2NUM(status.sstat_fragmentation_point),
    rb_str_new2(tmpname)
  );
}

#getlocalnamesObject

Return an array of local addresses that are part of the association.

Example:

socket = SCTP::Socket.new
socket.bind(:addresses => ['10.0.4.5', '10.0.5.5'])
socket.getlocalnames # => ['10.0.4.5', '10.0.5.5'])


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'ext/sctp/socket.c', line 322

static VALUE rsctp_getlocalnames(VALUE self){
  sctp_assoc_t assoc_id;
  struct sockaddr* addrs;
  int i, sock_fd, num_addrs;
  char str[16];
  VALUE v_array = rb_ary_new();

  bzero(&addrs, sizeof(addrs));

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));
  assoc_id = NUM2INT(rb_iv_get(self, "@association_id"));

  num_addrs = sctp_getladdrs(sock_fd, assoc_id, &addrs);

  if(num_addrs < 0){
    sctp_freeladdrs(addrs);
    rb_raise(rb_eSystemCallError, "sctp_getladdrs: %s", strerror(errno));
  }

  for(i = 0; i < num_addrs; i++){
    inet_ntop(AF_INET, &(((struct sockaddr_in *)&addrs[i])->sin_addr), str, sizeof(str));
    rb_ary_push(v_array, rb_str_new2(str));
    bzero(&str, sizeof(str));
  }

  sctp_freeladdrs(addrs);

  return v_array;
}

#getpeernamesObject

Return an array of all addresses of a peer.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'ext/sctp/socket.c', line 283

static VALUE rsctp_getpeernames(VALUE self){
  sctp_assoc_t assoc_id;
  struct sockaddr* addrs;
  int i, sock_fd, num_addrs;
  char str[16];
  VALUE v_array = rb_ary_new();

  bzero(&addrs, sizeof(addrs));

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));
  assoc_id = NUM2INT(rb_iv_get(self, "@association_id"));

  num_addrs = sctp_getpaddrs(sock_fd, assoc_id, &addrs);

  if(num_addrs < 0){
    sctp_freepaddrs(addrs);
    rb_raise(rb_eSystemCallError, "sctp_getpaddrs: %s", strerror(errno));
  }

  for(i = 0; i < num_addrs; i++){
    inet_ntop(AF_INET, &(((struct sockaddr_in *)&addrs[i])->sin_addr), str, sizeof(str));
    rb_ary_push(v_array, rb_str_new2(str));
    bzero(&str, sizeof(str));
  }

  sctp_freepaddrs(addrs);

  return v_array;
}

#listen(*args) ⇒ Object

Marks the socket referred to by sockfd as a passive socket, i.e. a socket that will be used to accept incoming connection requests.

The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. The default is 1024.

Example:

socket = SCTP::Socket.new
socket.bind(:port => 62534, :addresses => ['127.0.0.1'])
socket.listen


1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'ext/sctp/socket.c', line 1005

static VALUE rsctp_listen(int argc, VALUE* argv, VALUE self){
  VALUE v_backlog;
  int backlog, sock_fd;

  rb_scan_args(argc, argv, "01", &v_backlog);

  if(NIL_P(v_backlog))
    backlog = 1024;
  else
    backlog = NUM2INT(v_backlog);

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));

  if(listen(sock_fd, backlog) < 0)
    rb_raise(rb_eSystemCallError, "setsockopt: %s", strerror(errno));
  
  return self;
}

#peeloff!(v_assoc_id) ⇒ Object

Extracts an association contained by a one-to-many socket connection into a one-to-one style socket. Note that this modifies the underlying sock_fd.



1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'ext/sctp/socket.c', line 1028

static VALUE rsctp_peeloff(VALUE self, VALUE v_assoc_id){
  int sock_fd, new_sock_fd;
  sctp_assoc_t assoc_id;
    
  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));
  assoc_id = NUM2INT(v_assoc_id);

  new_sock_fd = sctp_peeloff(sock_fd, assoc_id);

  if(new_sock_fd < 0)
    rb_raise(rb_eSystemCallError, "sctp_peeloff: %s", strerror(errno));

  rb_iv_set(self, "@sock_fd", INT2NUM(new_sock_fd));

  return self;
}

#recvmsg(*args) ⇒ Object

Receive a message from another SCTP endpoint.

Example:

begin
  socket = SCTP::Socket.new
  socket.bind(:port => 62534, :addresses => ['10.0.4.5', '10.0.5.5'])
  socket.subscribe(:data_io => 1)
  socket.listen

  while true
    info = socket.recvmsg
    puts "Received message: #{info.message}"
  end
ensure
  socket.close
end


630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# File 'ext/sctp/socket.c', line 630

static VALUE rsctp_recvmsg(int argc, VALUE* argv, VALUE self){
  VALUE v_flags, v_notification, v_message;
  struct sctp_sndrcvinfo sndrcvinfo;
  struct sockaddr_in clientaddr;
  int flags, bytes, sock_fd;
  char buffer[1024]; // TODO: Let this be configurable?
  socklen_t length;

  rb_scan_args(argc, argv, "01", &v_flags);

  if(NIL_P(v_flags))
    flags = 0;
  else
    flags = NUM2INT(v_flags);  

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));
  length = sizeof(struct sockaddr_in);
  bzero(buffer, sizeof(buffer));

  bytes = sctp_recvmsg(
    sock_fd,
    buffer,
    sizeof(buffer),
    (struct sockaddr*)&clientaddr,
    &length,
    &sndrcvinfo,
    &flags
  );

  if(bytes < 0)
    rb_raise(rb_eSystemCallError, "sctp_recvmsg: %s", strerror(errno));

  v_notification = Qnil;

  if(flags & MSG_NOTIFICATION){
    uint32_t i;
    char str[16];
    union sctp_notification* snp;
    VALUE v_str;
    VALUE* v_temp;

    snp = (union sctp_notification*)buffer;

    switch(snp->sn_header.sn_type){
      case SCTP_ASSOC_CHANGE:
        switch(snp->sn_assoc_change.sac_state){
          case SCTP_COMM_LOST:
            v_str = rb_str_new2("comm lost");
            break;
          case SCTP_COMM_UP:
            v_str = rb_str_new2("comm up");
            break;
          case SCTP_RESTART:
            v_str = rb_str_new2("restart");
            break;
          case SCTP_SHUTDOWN_COMP:
            v_str = rb_str_new2("shutdown complete");
            break;
          case SCTP_CANT_STR_ASSOC:
            v_str = rb_str_new2("association setup failed");
            break;
          default:
            v_str = rb_str_new2("unknown");
        }

        v_notification = rb_struct_new(v_assoc_change_struct,
          UINT2NUM(snp->sn_assoc_change.sac_type),
          UINT2NUM(snp->sn_assoc_change.sac_length),
          UINT2NUM(snp->sn_assoc_change.sac_state),
          UINT2NUM(snp->sn_assoc_change.sac_error),
          UINT2NUM(snp->sn_assoc_change.sac_outbound_streams),
          UINT2NUM(snp->sn_assoc_change.sac_inbound_streams),
          UINT2NUM(snp->sn_assoc_change.sac_assoc_id),
          v_str
        );
        break;
      case SCTP_PEER_ADDR_CHANGE:
        switch(snp->sn_paddr_change.spc_state){
          case SCTP_ADDR_AVAILABLE:
            v_str = rb_str_new2("available");
            break;
          case SCTP_ADDR_UNREACHABLE:
            v_str = rb_str_new2("unreachable");
            break;
          case SCTP_ADDR_REMOVED:
            v_str = rb_str_new2("removed from association");
            break;
          case SCTP_ADDR_ADDED:
            v_str = rb_str_new2("added to association");
            break;
          case SCTP_ADDR_MADE_PRIM:
            v_str = rb_str_new2("primary destination");
            break;
          default:
            v_str = rb_str_new2("unknown");
        }

        inet_ntop(
          ((struct sockaddr_in *)&snp->sn_paddr_change.spc_aaddr)->sin_family,
          &(((struct sockaddr_in *)&snp->sn_paddr_change.spc_aaddr)->sin_addr),
          str,
          sizeof(str)
        );

        v_notification = rb_struct_new(v_peeraddr_change_struct,
          UINT2NUM(snp->sn_paddr_change.spc_type),
          UINT2NUM(snp->sn_paddr_change.spc_length),
          rb_str_new2(str),
          UINT2NUM(snp->sn_paddr_change.spc_state),
          UINT2NUM(snp->sn_paddr_change.spc_error),
          UINT2NUM(snp->sn_paddr_change.spc_assoc_id),
          v_str
        );
        break;
      case SCTP_REMOTE_ERROR:
        v_temp = ALLOCA_N(VALUE, snp->sn_remote_error.sre_length);

        for(i = 0; i < snp->sn_remote_error.sre_length; i++){
          v_temp[i] = UINT2NUM(snp->sn_remote_error.sre_data[i]);
        }

        v_notification = rb_struct_new(v_remote_error_struct,
          UINT2NUM(snp->sn_remote_error.sre_type),
          UINT2NUM(snp->sn_remote_error.sre_length),
          UINT2NUM(snp->sn_remote_error.sre_error),
          UINT2NUM(snp->sn_remote_error.sre_assoc_id),
          rb_ary_new4(snp->sn_remote_error.sre_length, v_temp)
        );
        break;
#ifdef SCTP_SEND_FAILED_EVENT
      case SCTP_SEND_FAILED_EVENT:
        v_temp = ALLOCA_N(VALUE, snp->sn_send_failed_event.ssf_length);

        for(i = 0; i < snp->sn_send_failed_event.ssf_length; i++){
          v_temp[i] = UINT2NUM(snp->sn_send_failed_event.ssf_data[i]);
        }

        v_notification = rb_struct_new(v_send_failed_event_struct,
          UINT2NUM(snp->sn_send_failed_event.ssf_type),
          UINT2NUM(snp->sn_send_failed_event.ssf_length),
          UINT2NUM(snp->sn_send_failed_event.ssf_error),
          rb_struct_new(v_sndinfo_struct,
            UINT2NUM(snp->sn_send_failed_event.ssfe_info.snd_sid),
            UINT2NUM(snp->sn_send_failed_event.ssfe_info.snd_flags),
            UINT2NUM(snp->sn_send_failed_event.ssfe_info.snd_ppid),
            UINT2NUM(snp->sn_send_failed_event.ssfe_info.snd_context),
            UINT2NUM(snp->sn_send_failed_event.ssfe_info.snd_assoc_id)
          ),
          UINT2NUM(snp->sn_send_failed_event.ssf_assoc_id),
          rb_ary_new4(snp->sn_send_failed_event.ssf_length, v_temp)
        );
        break;
#else
      case SCTP_SEND_FAILED:
        v_temp = ALLOCA_N(VALUE, snp->sn_send_failed.ssf_length);

        for(i = 0; i < snp->sn_send_failed.ssf_length; i++){
          v_temp[i] = UINT2NUM(snp->sn_send_failed.ssf_data[i]);
        }

        v_notification = rb_struct_new(v_send_failed_event_struct,
          UINT2NUM(snp->sn_send_failed.ssf_type),
          UINT2NUM(snp->sn_send_failed.ssf_length),
          UINT2NUM(snp->sn_send_failed.ssf_error),
          Qnil,
          UINT2NUM(snp->sn_send_failed.ssf_assoc_id),
          rb_ary_new4(snp->sn_send_failed.ssf_length, v_temp)
        );
        break;
#endif
      case SCTP_SHUTDOWN_EVENT:
        v_notification = rb_struct_new(v_shutdown_event_struct,
          UINT2NUM(snp->sn_shutdown_event.sse_type),
          UINT2NUM(snp->sn_shutdown_event.sse_length),
          UINT2NUM(snp->sn_shutdown_event.sse_assoc_id)
        );
        break;
      case SCTP_ADAPTATION_INDICATION:
        v_notification = rb_struct_new(v_adaptation_event_struct,
          UINT2NUM(snp->sn_adaptation_event.sai_type),
          UINT2NUM(snp->sn_adaptation_event.sai_length),
          UINT2NUM(snp->sn_adaptation_event.sai_adaptation_ind),
          UINT2NUM(snp->sn_adaptation_event.sai_assoc_id)
        );
        break;
      case SCTP_PARTIAL_DELIVERY_EVENT:
        v_notification = rb_struct_new(v_partial_delivery_event_struct,
          UINT2NUM(snp->sn_pdapi_event.pdapi_type),
          UINT2NUM(snp->sn_pdapi_event.pdapi_length),
          UINT2NUM(snp->sn_pdapi_event.pdapi_indication),
          UINT2NUM(snp->sn_pdapi_event.pdapi_stream),
          UINT2NUM(snp->sn_pdapi_event.pdapi_seq),
          UINT2NUM(snp->sn_pdapi_event.pdapi_assoc_id)
        );
        break;
      case SCTP_AUTHENTICATION_EVENT:
        v_notification = rb_struct_new(v_auth_event_struct,
          UINT2NUM(snp->sn_authkey_event.auth_type),
          UINT2NUM(snp->sn_authkey_event.auth_length),
          UINT2NUM(snp->sn_authkey_event.auth_keynumber),
          UINT2NUM(snp->sn_authkey_event.auth_indication),
          UINT2NUM(snp->sn_authkey_event.auth_assoc_id)
        );
        break;
    }
  }

  if(NIL_P(v_notification))
    v_message = rb_str_new(buffer, bytes);
  else
    v_message = Qnil;

  return rb_struct_new(v_sndrcv_struct,
    v_message,
    UINT2NUM(sndrcvinfo.sinfo_stream),
    UINT2NUM(sndrcvinfo.sinfo_flags),
    UINT2NUM(sndrcvinfo.sinfo_ppid),
    UINT2NUM(sndrcvinfo.sinfo_context),
    UINT2NUM(sndrcvinfo.sinfo_timetolive),
    UINT2NUM(sndrcvinfo.sinfo_assoc_id),
    v_notification,
    convert_sockaddr_in_to_struct(&clientaddr)
  );
}

#send(v_options) ⇒ Object

Send a message on an already-connected socket to a specific association.

Example:

socket = SCTP::Socket.new
socket.connect(:port => 42000, :addresses => ['10.0.4.5', '10.0.5.5'])

socket.send(:message => "Hello World")
socket.send(:message => "Hello World", :association_id => 37)


409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'ext/sctp/socket.c', line 409

static VALUE rsctp_send(VALUE self, VALUE v_options){
  uint16_t stream;
  uint32_t ppid, send_flags, ctrl_flags, ttl, context;
  ssize_t num_bytes;
  int sock_fd;
  sctp_assoc_t assoc_id;
  struct sctp_sndrcvinfo info;
  VALUE v_msg, v_stream, v_ppid, v_context, v_send_flags, v_ctrl_flags, v_ttl, v_assoc_id;

  Check_Type(v_options, T_HASH);

  v_msg        = rb_hash_aref2(v_options, "message");
  v_stream     = rb_hash_aref2(v_options, "stream");
  v_ppid       = rb_hash_aref2(v_options, "ppid");
  v_context    = rb_hash_aref2(v_options, "context");
  v_send_flags = rb_hash_aref2(v_options, "send_flags");
  v_ctrl_flags = rb_hash_aref2(v_options, "control_flags");
  v_ttl        = rb_hash_aref2(v_options, "ttl");
  v_assoc_id   = rb_hash_aref2(v_options, "association_id");

  if(NIL_P(v_stream))
    stream = 0;
  else
    stream = NUM2INT(v_stream);

  if(NIL_P(v_send_flags))
    send_flags = 0;
  else
    send_flags = NUM2INT(v_send_flags);

  if(NIL_P(v_ctrl_flags))
    ctrl_flags = 0;
  else
    ctrl_flags = NUM2INT(v_ctrl_flags);

  if(NIL_P(v_ttl)){
    ttl = 0;
  }
  else{
    ttl = NUM2INT(v_ttl);
    send_flags |= SCTP_PR_SCTP_TTL;
  }

  if(NIL_P(v_ppid))
    ppid = 0;
  else
    ppid = NUM2INT(v_ppid);

  if(NIL_P(v_context))
    context = 0;
  else
    context = NUM2INT(v_context);

  if(NIL_P(v_assoc_id))
    assoc_id = NUM2INT(rb_iv_get(self, "@association_id"));
  else
    assoc_id = NUM2INT(v_assoc_id);

  info.sinfo_stream = stream;
  info.sinfo_flags = send_flags;
  info.sinfo_ppid = ppid;
  info.sinfo_context = context;
  info.sinfo_timetolive = ttl;
  info.sinfo_assoc_id = assoc_id;

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));

  num_bytes = sctp_send(
    sock_fd,
    StringValueCStr(v_msg),
    RSTRING_LEN(v_msg),
    &info,
    ctrl_flags
  );

  if(num_bytes < 0)
    rb_raise(rb_eSystemCallError, "sctp_send: %s", strerror(errno));

  return INT2NUM(num_bytes);
}

#sendmsg(v_options) ⇒ Object

Transmit a message to an SCTP endpoint. The following hash of options is permitted:

:message -> The message to send to the endpoint. Mandatory.
:stream  -> The SCTP stream number you wish to send the message on.
:to      -> An array of addresses to send the message to.
:context -> The default context used for the sendmsg call if the send fails.
:ppid    -> The payload protocol identifier that is passed to the peer endpoint.
:flags   -> A bitwise integer that contain one or more values that control behavior.

Note that the :to option is not mandatory in a one-to-one (SOCK_STREAM)
socket connection. However, it must have been set previously via the
connect method.

Example:

  socket = SCTP::Socket.new

  socket.sendmsg(
    :message => "Hello World!",
    :stream  => 3,
    :flags   => SCTP::Socket::SCTP_UNORDERED | SCTP::Socket::SCTP_SENDALL,
    :ttl     => 100,
    :to      => ['10.0.5.4', '10.0.6.4']
  )


517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'ext/sctp/socket.c', line 517

static VALUE rsctp_sendmsg(VALUE self, VALUE v_options){
  VALUE v_msg, v_ppid, v_flags, v_stream, v_ttl, v_context, v_addresses;
  uint16_t stream;
  uint32_t ppid, flags, ttl, context;
  ssize_t num_bytes;
  struct sockaddr_in addrs[8];
  int sock_fd, size;

  Check_Type(v_options, T_HASH);

  bzero(&addrs, sizeof(addrs));

  v_msg       = rb_hash_aref2(v_options, "message");
  v_stream    = rb_hash_aref2(v_options, "stream");
  v_ppid      = rb_hash_aref2(v_options, "ppid");
  v_context   = rb_hash_aref2(v_options, "context");
  v_flags     = rb_hash_aref2(v_options, "flags");
  v_ttl       = rb_hash_aref2(v_options, "ttl");
  v_addresses = rb_hash_aref2(v_options, "addresses");

  if(NIL_P(v_stream))
    stream = 0;
  else
    stream = NUM2INT(v_stream);

  if(NIL_P(v_flags))
    flags = 0;
  else
    flags = NUM2INT(v_flags);

  if(NIL_P(v_ttl)){
    ttl = 0;
  }
  else{
    ttl = NUM2INT(v_ttl);
    flags |= SCTP_PR_SCTP_TTL;
  }

  if(NIL_P(v_ppid))
    ppid = 0;
  else
    ppid = NUM2INT(v_ppid);

  if(NIL_P(v_context))
    context = 0;
  else
    context = NUM2INT(v_context);

  if(!NIL_P(v_addresses)){
    int i, num_ip, port;
    VALUE v_address, v_port;

    num_ip = RARRAY_LEN(v_addresses);
    v_port = rb_hash_aref2(v_options, "port");

    if(NIL_P(v_port))
      port = 0;
    else
      port = NUM2INT(v_port);

    for(i = 0; i < num_ip; i++){
      v_address = RARRAY_PTR(v_addresses)[i];
      addrs[i].sin_family = NUM2INT(rb_iv_get(self, "@domain"));
      addrs[i].sin_port = htons(port);
      addrs[i].sin_addr.s_addr = inet_addr(StringValueCStr(v_address));
    }

    size = sizeof(addrs);
  }
  else{
    size = 0;
  }

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));

  num_bytes = sctp_sendmsg(
    sock_fd,
    StringValueCStr(v_msg),
    RSTRING_LEN(v_msg),
    (struct sockaddr*)addrs,
    size,
    ppid,
    flags,
    stream,
    ttl,
    context
  );

  if(num_bytes < 0)
    rb_raise(rb_eSystemCallError, "sctp_sendmsg: %s", strerror(errno));

  return INT2NUM(num_bytes);
}

#sendv(v_messages) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'ext/sctp/socket.c', line 353

static VALUE rsctp_sendv(VALUE self, VALUE v_messages){
  struct iovec* iov;
  struct sockaddr* addrs[8];
  struct sctp_sndinfo info;
  int sock_fd, num_bytes, size;

  Check_Type(v_messages, T_ARRAY);
  bzero(&addrs, sizeof(addrs));

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));

  Check_Type(v_messages, T_ARRAY);
  size = RARRAY_LEN(v_messages);

  if(!size)
    rb_raise(rb_eArgError, "Must contain at least one message");

  if(size > IOV_MAX)
    rb_raise(rb_eArgError, "Array size is greater than IOV_MAX");

  ARY2IOVEC(iov, v_messages);

  info.snd_flags = SCTP_UNORDERED;
  info.snd_assoc_id = NUM2INT(rb_iv_get(self, "@association_id"));

  num_bytes = sctp_sendv(
    sock_fd,
    iov,
    size,
    NULL,
    0,
    &info,
    sizeof(info),
    SCTP_SENDV_SNDINFO,
    0
  );

  if(num_bytes < 0)
    rb_raise(rb_eSystemCallError, "sctp_sendv: %s", strerror(errno));

  return INT2NUM(num_bytes);
}

#set_initmsg(v_options) ⇒ Object

Set the initial parameters used by the socket when sending out the INIT message.

Example:

socket = SCTP::Socket.new
socket.set_initmsg(:output_streams => 5, :input_streams => 5, :max_attempts => 4, :timeout => 30)

The following parameters can be configured:

:output_streams - The number of outbound SCTP streams an application would like to request. :input_streams - The maximum number of inbound streams an application is prepared to allow. :max_attempts - How many times the the SCTP stack should send the initial INIT message before it’s considered unreachable. :timeout - The maximum RTO value for the INIT timer.

By default these values are set to zero (i.e. ignored).



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
# File 'ext/sctp/socket.c', line 872

static VALUE rsctp_set_initmsg(VALUE self, VALUE v_options){
  int sock_fd;
  struct sctp_initmsg initmsg;
  VALUE v_output, v_input, v_attempts, v_timeout;

  bzero(&initmsg, sizeof(initmsg));

  v_output   = rb_hash_aref2(v_options, "output_streams");
  v_input    = rb_hash_aref2(v_options, "input_streams");
  v_attempts = rb_hash_aref2(v_options, "max_attempts");
  v_timeout  = rb_hash_aref2(v_options, "timeout");

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));

  if(!NIL_P(v_output))
    initmsg.sinit_num_ostreams = NUM2INT(v_output);

  if(!NIL_P(v_input))
    initmsg.sinit_max_instreams = NUM2INT(v_input);

  if(!NIL_P(v_attempts))
    initmsg.sinit_max_attempts = NUM2INT(v_attempts);

  if(!NIL_P(v_timeout))
    initmsg.sinit_max_init_timeo = NUM2INT(v_timeout);

  if(setsockopt(sock_fd, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg)) < 0)
    rb_raise(rb_eSystemCallError, "setsockopt: %s", strerror(errno));

  return self;
}

#shutdown(*args) ⇒ Object



1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# File 'ext/sctp/socket.c', line 1100

static VALUE rsctp_shutdown(int argc, VALUE* argv, VALUE self){
  int how, sock_fd;
  VALUE v_how;

  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));

  rb_scan_args(argc, argv, "01", &v_how);

  if(NIL_P(v_how)){
    how = SHUT_RDWR;
  }
  else{
    Check_Type(v_how, T_FIXNUM);
    how = NUM2INT(v_how);
  }

  if(shutdown(sock_fd, how) < 0)
    rb_raise(rb_eSystemCallError, "shutdown: %s", strerror(errno));

  return self;
}

#subscribe(v_options) ⇒ Object

Subscribe to various notification types, which will generate additional data that the socket may receive. The possible notification types are as follows:

:association
- A change has occurred to an association, either a new one has begun or an existing one has end.

:address
- The state of one of the peer's addresses has experienced a change.

:send_failure
- The message could not be delivered to a peer.

:shutdown
- The peer has sent a shutdown to the local endpoint.

:data_io
- Message data was received. On by default.

Others:

:adaptation
:authentication
:partial_delivery

Not yet supported:

:sender_dry
:peer_error

By default only data_io is subscribed to.

Example:

socket = SCTP::Socket.new

socket.bind(:port => port, :addresses => ['127.0.0.1'])
socket.subscribe(:shutdown => true, :send_failure => true)


944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
# File 'ext/sctp/socket.c', line 944

static VALUE rsctp_subscribe(VALUE self, VALUE v_options){
  int sock_fd;
  struct sctp_event_subscribe events;

  bzero(&events, sizeof(events));
  sock_fd = NUM2INT(rb_iv_get(self, "@sock_fd"));

  if(RTEST(rb_hash_aref2(v_options, "data_io")))
    events.sctp_data_io_event = 1;

  if(RTEST(rb_hash_aref2(v_options, "association")))
    events.sctp_association_event = 1;

  if(RTEST(rb_hash_aref2(v_options, "address")))
    events.sctp_address_event = 1;

  if(RTEST(rb_hash_aref2(v_options, "send_failure")))
#ifdef HAVE_STRUCT_SCTP_EVENT_SUBSCRIBE_SCTP_SEND_FAILURE_EVENT
    events.sctp_send_failure_event = 1;
#else
    events.sctp_send_failure_event_event = 1;
#endif

  if(RTEST(rb_hash_aref2(v_options, "peer_error")))
    events.sctp_peer_error_event = 1;

  if(RTEST(rb_hash_aref2(v_options, "shutdown")))
    events.sctp_shutdown_event = 1;

  if(RTEST(rb_hash_aref2(v_options, "partial_delivery")))
    events.sctp_partial_delivery_event = 1;

  if(RTEST(rb_hash_aref2(v_options, "adaptation_layer")))
    events.sctp_adaptation_layer_event = 1;

  if(RTEST(rb_hash_aref2(v_options, "authentication")))
    events.sctp_authentication_event = 1;

  if(RTEST(rb_hash_aref2(v_options, "sender_dry")))
    events.sctp_sender_dry_event = 1;

  if(setsockopt(sock_fd, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof(events)) < 0)
    rb_raise(rb_eSystemCallError, "setsockopt: %s", strerror(errno));

  return self;
}