1 
2 /*
3  *  Diverse Bristol midi routines.
4  *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 3 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * socketToolKit.c
24  *
25  * Initiate socket at GUI side. The client side is within the file
26  * clientSide.c
27 #define GLIBC_21
28  */
29 
30 #define _GNU_SOURCE
31 #include <signal.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netdb.h>
40 #include <sys/un.h>
41 #include <strings.h>
42 #include <stdio.h>
43 #include <sys/time.h>
44 
45 #define SOCKTYPE SOCK_STREAM
46 
47 #include "socketToolKit.h"
48 
49 static int remote_socket_descriptor;
50 
51 struct  sockaddr_un local_socket_addr;
52 
53 /*
54  * Set up a socket for the client programmes to talk to mixslab GUI.
55  * This is called from the serverSide.c routines.
56  */
57 int
open_remote_socket(name,port,listens,reqsig)58 open_remote_socket(name, port, listens, reqsig)
59 char		*name; /* service name, must be known */
60 int 		listens; /* Parameter for the number of connections accepted */
61 {
62 	struct	servent		*service, service_tmp;
63 	struct  sockaddr_in remote_socket_addr;
64 
65 #ifdef NEW_DEBUG
66 	printf("open_remote_socket(%s, %i, %i, %i)\n",
67 		name, port, listens, reqsig);
68 #endif
69 	/*
70 	 * Open a socket with minimal error handling.
71 	 */
72 	while((remote_socket_descriptor = socket(PF_INET, SOCKTYPE, IPPROTO_TCP))
73 		== -1)
74 	{
75 		perror("remote_socket_descriptor");
76 	}
77 
78 	service = &service_tmp;
79 	service->s_port = port;
80 	service->s_name = "bristol";
81 
82 	/*
83 	 * zero the struct (fixed size)
84 	 */
85 	bzero((char *) &remote_socket_addr, sizeof (remote_socket_addr));
86 
87 	/*
88 	 * Setup the structure values for subroutines.
89 	 */
90 	remote_socket_addr.sin_family = AF_INET;
91 	remote_socket_addr.sin_addr.s_addr = htonl(INADDR_ANY);
92 	remote_socket_addr.sin_port = htons(service->s_port);
93 	/*
94 	 * Bind the socket:
95 	 */
96 	if ((bind(remote_socket_descriptor, (struct sockaddr *) &remote_socket_addr,
97 		sizeof(remote_socket_addr))) < 0)
98 	{
99 		perror("open_remote_socket");
100 		printf("socket id %i\n", port);
101 		return(-1);
102 	}
103 
104 	if (listen(remote_socket_descriptor, listens) < 0)
105 		printf("Cannot listen to socket\n");
106 
107 	//fcntl(remote_socket_descriptor, F_SETFL, O_NONBLOCK);
108 
109 	return(remote_socket_descriptor);
110 }
111 
112