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 /* #define DEBUG */
23
24 /*
25 * audioNetClient.c
26 *
27 * This will initialise an audio connection if an IP and port is specified. All
28 * audio data is copied to this tap if it is active. Only use at the moment will
29 * be the audio IO Oscilloscope. Should support copy of both input or output
30 * data by selections.
31 */
32 #define _GNU_SOURCE
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <netinet/in.h>
38 #include <netdb.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 #include <strings.h>
46 #include <errno.h>
47
48 #define SERVICE "bristol"
49
50 #include "socketToolKit.h"
51
52 #define MAX_MTU 1024
53
54 #define SOCKTYPE SOCK_STREAM
55 #define PROTOCOL "tcp"
56 /*
57 * For the service name, defaults to "slab", and can be entered in services
58 * file (although this is not implemented yet!).
59 */
60 #define NAME_LENGTH 32
61
62 int socket_descriptor = -1, recvFlag;
63
64 void clientCheckSocket();
65
66 int
initControlPort(host,port)67 initControlPort(host, port)
68 char *host;
69 {
70 struct sockaddr_in connect_socket_addr;
71 char hostname[NAME_LENGTH];
72 struct servent *service, service_tmp;
73 struct hostent *hstp;
74 char *tport;
75
76 /*
77 * Set up our default parameters.
78 */
79 (void) gethostname(hostname, NAME_LENGTH);
80
81 /*
82 * See if we have been given new ones.
83 */
84 if (host != (char *) NULL)
85 strcpy(hostname, host);
86
87 if (port <= 0)
88 port = 5028;
89
90 printf("hostname is %s, %s\n", hostname, SERVICE);
91
92 if ((tport = index(hostname, ':')) != NULL)
93 {
94 *tport = '\0';
95
96 if ((port = atoi(++tport)) <= 0)
97 port = 5028;
98 }
99
100 /*
101 * Check out a few default signals. This should be dropped, since they were
102 * actually for some other programmes.
103 */
104 if ((hstp = gethostbyname(hostname)) == NULL)
105 {
106 printf("could not resolve %s, defaulting to localhost\n", hostname);
107 hstp = gethostbyname("localhost");
108 }
109
110 /*
111 * Get a socket in the inet domain
112 */
113 if ((socket_descriptor = socket(PF_INET, SOCKTYPE, IPPROTO_TCP))
114 == -1) {
115 perror ( "socket failed" );
116 exit(-1);
117 }
118 /*
119 * Zero the addr struct, set a few parts of it.
120 */
121 (void) bzero((char *) &connect_socket_addr, sizeof (connect_socket_addr));
122
123 /*
124 * Add our service specific components. The use of a pointer is actually
125 * superfluous, but is residual from some more complex service lookup
126 * schemes that could be implemented (ie, from /etc/services).
127 */
128 service = &service_tmp;
129 service->s_port = port;
130 service->s_name = SERVICE;
131
132 connect_socket_addr.sin_family = AF_INET;
133 connect_socket_addr.sin_port = htons(service->s_port);
134
135 printf("TCP port: %i\n", service->s_port);
136 /*
137 * Got to check the address parameter for sockaddr. Grey area!
138 */
139 if (hstp == NULL) {
140 (void) printf("%s: %s", hostname, "Unknown host?!"); /* no such host */
141 }
142
143 /* if (hstp->h_length != sizeof(u_long)) */
144 /* return(-1); this is fundamental */
145
146 (void) bcopy(hstp->h_addr, (char *) &connect_socket_addr.sin_addr,
147 hstp->h_length);
148 /*
149 * Attempt a connection on the known socket.
150 */
151 if (connect(socket_descriptor, (struct sockaddr *) &connect_socket_addr,
152 sizeof(connect_socket_addr)) == -1)
153 {
154 char errtext[1024];
155
156 sprintf(errtext, "connect failed on %i", port);
157 perror(errtext);
158 (void) close(socket_descriptor);
159 return(-2);
160 }
161
162 return(socket_descriptor);
163 }
164
165