summaryrefslogtreecommitdiff
path: root/libs/portset-demuxer.c
blob: 2d67964c020abd2bea0183e951b478edb4adc3aa (plain)
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

/* Demuxer wrapper.

   Copyright (C) 2008 Free Software Foundation, Inc.

   Written by FlĂvio Cruz <flaviocruz@gmail.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <mach.h>
#include <mach/notify.h>
#include <mach/mig_errors.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>

typedef int (*demuxer_fun_type) (mach_port_t,
				 mach_msg_header_t *, mach_msg_header_t *);

static demuxer_fun_type demuxer = NULL;

int
portset_demuxer (mach_msg_header_t * inp, mach_msg_header_t * outheadp)
{
  assert (demuxer != NULL);

  register mig_reply_header_t *outp = (mig_reply_header_t *) outheadp;
  static const mach_msg_type_t RetCodeType = {
    /* msgt_name = */ MACH_MSG_TYPE_INTEGER_32,
    /* msgt_size = */ 32,
    /* msgt_number = */ 1,
    /* msgt_inline = */ TRUE,
    /* msgt_longform = */ FALSE,
    /* msgt_deallocate = */ FALSE,
    /* msgt_unused = */ 0
  };

  /* Fill in default response */
  outp->Head.msgh_bits
    = MACH_MSGH_BITS (MACH_MSGH_BITS_REMOTE (inp->msgh_bits), 0);
  outp->Head.msgh_size = sizeof (*outp);
  outp->Head.msgh_remote_port = inp->msgh_remote_port;
  outp->Head.msgh_local_port = MACH_PORT_NULL;
  outp->Head.msgh_seqno = 0;
  outp->Head.msgh_id = inp->msgh_id + 100;
  outp->RetCodeType = RetCodeType;
  outp->RetCode = MIG_BAD_ID;

  int ret = demuxer (inp->msgh_local_port, inp, outheadp);

  if (ret == EOPNOTSUPP)
    {
      outp->RetCode = EOPNOTSUPP;
      return (1);
    }
  else
    {
      return (ret);
    }
}

void
set_demuxer (demuxer_fun_type fun)
{
  assert (fun != NULL);

  demuxer = fun;
}