~ubuntu-branches/ubuntu/karmic/cvm/karmic

« back to all changes in this revision

Viewing changes to module_udp.c

  • Committer: Bazaar Package Importer
  • Author(s): Gerrit Pape
  • Date: 2005-01-15 11:32:52 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050115113252-0jj7skrzjb0s6837
Tags: 0.32-1
* new upstream version.
* debian/copyright: adapt, 2005.
* debian/diff/ld.diff: adapt.
* debian/rules: strip diet binaries if DEB_BUILD_OPTIONS=diet is set.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* cvm/module_udp.c - UDP CVM server module main routine
2
 
 * Copyright (C) 2001  Bruce Guenter <bruceg@em.ca>
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program 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
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 */
18
 
#include <netdb.h>
19
 
#include <signal.h>
20
 
#include <stdio.h>
21
 
#include <stdlib.h>
22
 
#include <string.h>
23
 
#include <unistd.h>
24
 
#include "socket/socket.h"
25
 
#include "module.h"
26
 
 
27
 
static int sock;
28
 
static ipv4addr ip;
29
 
static unsigned short port;
30
 
 
31
 
static int read_input(void)
32
 
{
33
 
  inbuflen = socket_recv4(sock, inbuffer, BUFSIZE, ip, &port);
34
 
  if (inbuflen == (unsigned)-1) return CVME_IO;
35
 
  return 0;
36
 
}
37
 
 
38
 
static void write_output(void)
39
 
{
40
 
  socket_send4(sock, outbuffer, outbuflen, ip, port);
41
 
}
42
 
 
43
 
static void exitfn()
44
 
{
45
 
  log_shutdown();
46
 
  exit(0);
47
 
}
48
 
 
49
 
static const char usagestr[] =
50
 
"usage: cvm-module-udp IP PORT\n";
51
 
 
52
 
static void usage(void)
53
 
{
54
 
  write(2, usagestr, sizeof usagestr);
55
 
  exit(1);
56
 
}
57
 
 
58
 
int main(int argc, char** argv)
59
 
{
60
 
  int code;
61
 
  struct hostent* he;
62
 
  char* tmp;
63
 
  
64
 
  if (argc != 3) usage();
65
 
 
66
 
  signal(SIGINT, exitfn);
67
 
  signal(SIGTERM, exitfn);
68
 
  
69
 
  if ((he = gethostbyname(argv[1])) == 0) usage();
70
 
  memcpy(ip, he->h_addr_list[0], 4);
71
 
  if ((port = strtoul(argv[2], &tmp, 10)) == 0 ||
72
 
      port >= 0xffff || *tmp != 0) usage();
73
 
  if ((sock = socket_udp()) == -1) perror("socket");
74
 
  if (!socket_bind4(sock, ip, port)) perror("bind");
75
 
  if ((code = cvm_auth_init()) != 0) return code;
76
 
  log_startup();
77
 
 
78
 
  code = 0;
79
 
  do {
80
 
    if ((code = read_input()) != 0) continue;
81
 
    code = handle_request();
82
 
    cvm_fact_end(code & CVME_MASK);
83
 
    log_request();
84
 
    write_output();
85
 
  } while ((code & CVME_FATAL) == 0);
86
 
  cvm_auth_stop();
87
 
  return 0;
88
 
}