~ubuntu-branches/ubuntu/oneiric/nis/oneiric-proposed

« back to all changes in this revision

Viewing changes to ypbind-mt-1.19/src/ypbind_slp.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2005-11-16 23:42:06 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20051116234206-p00omaw5ji5q0qhr
Tags: 3.15-3ubuntu1
Resynchronise with Debian.  (me)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2004 Thorsten Kukuk
2
 
   This file is part of ypbind-mt.
3
 
   Author: Thorsten Kukuk <kukuk@suse.de>
4
 
 
5
 
   The ypbind-mt are free software; you can redistribute it and/or
6
 
   modify it under the terms of the GNU General Public License version 2
7
 
   as published by the Free Software Foundation.
8
 
 
9
 
   ypbind-mt is distributed in the hope that it will be useful,
10
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
   General Public License for more details.
13
 
 
14
 
   You should have received a copy of the GNU General Public
15
 
   License along with ypbind-mt; see the file COPYING.  If not,
16
 
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
 
   Boston, MA 02111-1307, USA. */
18
 
 
19
 
#if defined(HAVE_CONFIG_H)
20
 
#include "config.h"
21
 
#endif
22
 
 
23
 
#ifdef USE_SLP
24
 
 
25
 
#include <slp.h>
26
 
#include <string.h>
27
 
#include <locale.h>
28
 
#include <libintl.h>
29
 
#include <rpc/rpc.h>
30
 
 
31
 
#include "ypbind.h"
32
 
#include "local.h"
33
 
#include "log_msg.h"
34
 
 
35
 
#ifndef _
36
 
#define _(String) gettext (String)
37
 
#endif
38
 
 
39
 
/* Ask SLP server for ypserv service.  */
40
 
 
41
 
struct slpcb {
42
 
  char *srvurl;
43
 
  SLPError err;
44
 
  struct slpcb *next;
45
 
};
46
 
 
47
 
static SLPBoolean
48
 
MySLPSrvURLCallback (SLPHandle hslp __attribute__((unused)),
49
 
                     const char *srvurl,
50
 
                     unsigned short lifetime __attribute__((unused)),
51
 
                     SLPError errcode, void *cookie)
52
 
{
53
 
  struct slpcb *cb = (struct slpcb *) cookie;
54
 
 
55
 
  if (errcode == SLP_OK)
56
 
    {
57
 
      if (cb->srvurl != NULL)
58
 
        {
59
 
          struct slpcb *cbt = malloc (sizeof (struct slpcb));
60
 
          if (cbt == NULL)
61
 
            return SLP_FALSE;
62
 
 
63
 
          cbt->srvurl = cb->srvurl;
64
 
          cbt->err = cb->err;
65
 
          cbt->next = cb->next;
66
 
          cb->next = cbt;
67
 
        }
68
 
      cb->srvurl = strdup (srvurl);
69
 
      cb->err = SLP_OK;
70
 
      return SLP_TRUE;
71
 
    }
72
 
  else if (errcode != SLP_LAST_CALL)
73
 
    cb->err = errcode;
74
 
 
75
 
  return SLP_FALSE; /* We don't wan't to be called again.  */
76
 
}
77
 
 
78
 
int
79
 
query_slp (const char *domain)
80
 
{
81
 
  struct slpcb *cb, callbackres = {NULL, 0, NULL};
82
 
  SLPError err;
83
 
  SLPHandle hslp;
84
 
  int found = 0;
85
 
 
86
 
  err = SLPOpen ("en", SLP_FALSE, &hslp);
87
 
  if (err != SLP_OK)
88
 
    {
89
 
      log_msg (LOG_ERR, _("Error opening SLP handle: %i."), err);
90
 
      return 0;
91
 
    }
92
 
 
93
 
  err = SLPFindSrvs (hslp, "ypserv", 0, 0,
94
 
                     MySLPSrvURLCallback, &callbackres);
95
 
 
96
 
  /* err may contain an error code that occurred as the slp library
97
 
     _prepared_ to make the call.  */
98
 
  if (err != SLP_OK || callbackres.err != SLP_OK)
99
 
    {
100
 
      log_msg (LOG_ERR, _("No service found with SLP."));
101
 
      return 0;
102
 
    }
103
 
 
104
 
  /* Now that we're done using slp, close the slp handle */
105
 
  SLPClose (hslp);
106
 
 
107
 
  cb = &callbackres;
108
 
  while (cb != NULL)
109
 
    {
110
 
      if (cb->srvurl != NULL)
111
 
        {
112
 
          char *hostp = strstr (cb->srvurl, "://");
113
 
          char *cp;
114
 
 
115
 
          if (!hostp || strlen(hostp) < strlen("://") + 1)
116
 
            {
117
 
              free (cb->srvurl);
118
 
              continue;
119
 
            }
120
 
          
121
 
          hostp += strlen ("://");
122
 
 
123
 
          cp = strrchr (hostp, '/');
124
 
          if (cp)
125
 
            *cp = '\0';
126
 
 
127
 
          /* Remove any port specification (we should use it!). */
128
 
          cp = strchr (hostp, ':');
129
 
          if (cp)
130
 
            *cp = '\0';
131
 
 
132
 
          if (add_server (domain, hostp, 0))
133
 
            {
134
 
              log_msg (LOG_INFO, "SLP: found server %s for domain %s.",
135
 
                       hostp, domain);
136
 
              found++;
137
 
            }
138
 
          /* Free memory. */
139
 
          free (cb->srvurl);
140
 
        }
141
 
      cb = cb->next;
142
 
    }
143
 
  return found;
144
 
}
145
 
 
146
 
#endif