Browse code

sctp: init function for one-to-one sockets

Andrei Pelinescu-Onciul authored on 01/10/2008 17:32:06
Showing 1 changed files
... ...
@@ -245,7 +245,7 @@ error:
245 245
 
246 246
 
247 247
 /* init, bind & start listening on the corresp. sctp socket
248
-   return 0 on success, -1 on error */
248
+   returns 0 on success, -1 on error */
249 249
 int sctp_init_sock(struct socket_info* sock_info)
250 250
 {
251 251
 	union sockaddr_union* addr;
... ...
@@ -300,6 +300,72 @@ error:
300 300
 }
301 301
 
302 302
 
303
+#define USE_SCTP_OO
304
+
305
+#ifdef USE_SCTP_OO
306
+
307
+/* init, bind & start listening on the corresp. sctp socket, using
308
+   sctp one-to-one mode
309
+   returns 0 on success, -1 on error */
310
+int sctp_init_sock_oo(struct socket_info* sock_info)
311
+{
312
+	union sockaddr_union* addr;
313
+	int optval;
314
+	
315
+	sock_info->proto=PROTO_SCTP;
316
+	addr=&sock_info->su;
317
+	if (sctp_init_su(sock_info)!=0)
318
+		goto error;
319
+	sock_info->socket = socket(AF2PF(addr->s.sa_family), SOCK_STREAM, 
320
+								IPPROTO_SCTP);
321
+	if (sock_info->socket==-1){
322
+		LOG(L_ERR, "ERROR: sctp_init_sock_oo: socket: %s\n", strerror(errno));
323
+		goto error;
324
+	}
325
+	INFO("sctp:oo socket %d initialized (%p)\n", sock_info->socket, sock_info);
326
+	/* make socket non-blocking */
327
+	optval=fcntl(sock_info->socket, F_GETFL);
328
+	if (optval==-1){
329
+		LOG(L_ERR, "ERROR: sctp_init_sock_oo: fnctl failed: (%d) %s\n",
330
+				errno, strerror(errno));
331
+		goto error;
332
+	}
333
+	if (fcntl(sock_info->socket, F_SETFL, optval|O_NONBLOCK)==-1){
334
+		LOG(L_ERR, "ERROR: sctp_init_sock_oo: fcntl: set non-blocking failed:"
335
+				" (%d) %s\n", errno, strerror(errno));
336
+		goto error;
337
+	}
338
+	
339
+	/* set sock opts */
340
+	if (sctp_init_sock_opt_common(sock_info->socket)!=0)
341
+		goto error;
342
+	
343
+#ifdef SCTP_REUSE_PORT
344
+	/* set reuse port */
345
+	optval=1;
346
+	if (setsockopt(sock_info->socket, IPPROTO_SCTP, SCTP_REUSE_PORT ,
347
+					(void*)&optval, sizeof(optval)) ==-1){
348
+		LOG(L_ERR, "ERROR: sctp_init_sock_oo: setsockopt: "
349
+					"SCTP_REUSE_PORT: %s\n", strerror(errno));
350
+		goto error;
351
+	}
352
+#endif /* SCTP_REUSE_PORT */
353
+	
354
+	if (sctp_bind_sock(sock_info)<0)
355
+		goto error;
356
+	if (listen(sock_info->socket, 1)<0){
357
+		LOG(L_ERR, "ERROR: sctp_init_sock_oo: listen(%x, 1) on %s: %s\n",
358
+					sock_info->socket, sock_info->address_str.s,
359
+					strerror(errno));
360
+		goto error;
361
+	}
362
+	return 0;
363
+error:
364
+	return -1;
365
+}
366
+
367
+#endif /* USE_SCTP_OO */
368
+
303 369
 
304 370
 /* debugging: return a string name for SCTP_ASSOC_CHANGE state */
305 371
 static char* sctp_assoc_change_state2s(short int state)