File: iocntl.c

package info (click to toggle)
netmaze 0.81+jpg0.82-14.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,208 kB
  • ctags: 915
  • sloc: ansic: 8,277; tcl: 1,223; makefile: 66; sh: 26
file content (192 lines) | stat: -rw-r--r-- 3,765 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>

#include "netmaze.h"

#ifdef RS6000
 #include <sys/select.h>
#endif

#include <netinet/in.h>
#include <signal.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/ioctl.h>

#ifdef HAVE_FDSET
 fd_set readmask;
 fd_set writemask;
#else
 struct fd_mask readmask;
 struct fd_mask writemask;
#endif

struct timeval notimeout;

static struct hostent *hp;            /* pointer to host info for remote host */
static struct sockaddr_in remoteaddr; /* server connect */

static void io_handler(int);
extern void handle_socket(void);
extern void ident_player(void);
extern int init_audio(void);
extern void kick_audio(void);
int own_socket,max_fd=0;
static int audio;

int init_net(char *hostname)
{
  int  ret;
#ifdef USE_SIGVEC
  struct sigvec vec;
#else
  struct sigaction vec;
#endif

#ifndef USE_IPC
  int flag = 1;
#endif

  notimeout.tv_sec = 0;
  notimeout.tv_usec = 0;

#ifdef USE_SIGVEC
  vec.sv_handler = (void (*)) io_handler;
  vec.sv_mask = 0;
  vec.sv_flags = 0;
  if ( sigvector(SIGIO, &vec, (struct sigvec *) 0) == -1)
    perror(" sigaction(SIGIO)");
#else
  vec.sa_handler = (void (*)) io_handler;
 #ifdef RS6000 /* ibm rs/6000 */
   sigemptyset(&vec.sa_mask);
 #else
  vec.sa_mask = 0;
 #endif
  vec.sa_flags = 0;
  if ( sigaction(SIGIO, &vec, (struct sigaction *) 0) == -1)
    perror(" sigaction(SIGIO)");
#endif
  remoteaddr.sin_family = AF_INET;

  if ((hp = gethostbyname(hostname) ) == NULL)
  {
    fprintf(stderr, "netmaze: can't resolve hostname: %s.\n",hostname);
    exit(1);
  }

  remoteaddr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

  if((own_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  {
    fprintf(stderr, "netmaze: unable to create socket.\n"); exit(1);
  }

  remoteaddr.sin_port = htons(NETMAZEPORT);
  FD_ZERO(&readmask);
  FD_ZERO(&writemask);
  FD_SET(own_socket,&readmask);
  max_fd = own_socket+1;

#ifdef USE_SOUND
  audio = init_audio();
  if(audio >= 0)
  {
    FD_SET(audio,&writemask);
    if(audio > max_fd-1)
      max_fd = audio+1;
  }
#endif

#ifdef USE_IPC
  FD_SET(0,&readmask); /* pipe! */
#else
  if (ioctl(own_socket, FIOASYNC, &flag) == -1)
  {
    perror("socket: ioctl(FIOASYNC)"); exit(1);
  }
#endif

  if ( (ret=connect(own_socket,(struct sockaddr *) &remoteaddr, sizeof(struct sockaddr))) == -1)
  {
     perror("connect"); exit(1);
  }

#ifndef USE_IPC
  flag = -getpid();
  if (ioctl(own_socket, SIOCSPGRP, &flag) == -1)
  {
    perror("process group");
  }
#endif

  printf("Connectet to %s. Connectcode: %d.\n",hostname,ret);
  ident_player();

#ifdef USE_IPC
  for(;;) io_handler(0);
#else
  io_handler(0);
#endif

  return 0; /* all is OK */
}

/**********************************************
 * io-handler (socket/pipes)
 */

static void io_handler(int a)
{
  int	numfds;
  static char	buf[256];

#ifdef HAVE_FDSET
  fd_set	 readmask1;
  fd_set	 writemask1;
#else
  struct fd_mask readmask1;
  struct fd_mask writemask1;
#endif

  for(;;)
  {
    extern int playit;
    readmask1 = readmask;
    if(playit)
      writemask1 = writemask;
    else
      FD_ZERO(&writemask1);

#ifdef USE_IPC
    if((numfds = select(max_fd,&readmask1,&writemask1,NULL,NULL))< 0)
      continue;
#else
    if((numfds = select(max_fd,&readmask1,&writemask1,NULL,&notimeout))<0)
    {
      perror("select failed ");
      exit(2);
    }
#endif

    if(numfds == 0) break;

    if(FD_ISSET(own_socket,&readmask1))
      handle_socket();

#ifdef USE_IPC
    if(FD_ISSET(0,&readmask1))
    {
      if(read(0,buf,256) == 0) /* sure is sure */
      {
        fprintf(stderr,"Parent died!\n"); exit(1);
      }
    }
#endif
    if(audio >= 0)
      if(FD_ISSET(audio,&writemask1))
        kick_audio();
  }
}