aboutsummaryrefslogtreecommitdiffstats
path: root/rttsends.c
blob: 7038e2c1d9132985cae3259ef70ae8fee0afa278 (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
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
/* 
   Copyright (C)  2006 Renaissance Technologies Corp.
                  main developer: HP Wei <hp@rentec.com> 
   Copyright (C)  2005 Renaissance Technologies Corp.
   Copyright (C)  2001 Renaissance Technologies Corp.
                  main developer: HP Wei <hp@rentec.com>
                  codes in this file are extracted and modified from sends.c

   Copyright (C)  2000 Aaron Hillegass <aaron@classmax.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; see the file COPYING.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
*/

#include "rttmain.h"
#include <net/if.h>
#ifdef _SUN
#include <sys/sockio.h>  /* define SIOCGIFADDR */
#else
#include <linux/sockios.h>
#endif

extern char *      my_MCAST_ADDR;  
extern int         my_PORT;        
extern int         my_TTL;
extern int         my_LOOP;
extern char *      my_IFname;      

/* buffer for sending (same structure as those in sends.c */
int          *mode_ptr; /* char type would cause alignment problem on Sparc */
int          *current_page_ptr;
int          *total_bytes_ptr;
char         *data_ptr;
char          send_buff[PAGE_BUFFSIZE];

int pageSize;

/* Send socket */
int send_fd;
#ifndef IPV6
struct sockaddr_in send_addr;
#else
struct sockaddr_in6 send_addr;
#endif

/*
  set_mode sets the caster into a new mode.
  modes are defined in main.h:
*/
void set_mode(int new_mode)
{
  *mode_ptr = htonl(new_mode);
}


/* init_sends initializes the send buffer */
void init_sends(int npagesize)
{
  pageSize = (npagesize>PAGE_SIZE) ? PAGE_SIZE : npagesize;

  mode_ptr = (int *)send_buff;   /* hp: add (int*) */
  current_page_ptr = (int *) (mode_ptr + 1);
  total_bytes_ptr = (int *)(current_page_ptr + 1);
  data_ptr = (char *)(total_bytes_ptr + 1);

  send_fd = send_socket(&send_addr, my_MCAST_ADDR, my_PORT);

    /******* change MULTICAST_IF ********/
  if (my_IFname != NULL && mcast_set_if(send_fd, my_IFname, 0)<0) 
    perror("init_sends(): when set MULTICAST_IF\n");
  
  /* set multicast_ttl such that UDP can go to 2nd subnetwork */  
  if (mcast_set_ttl(send_fd, my_TTL) < 0) 
    perror("init_sends(): when set MULTICAST_TTL\n");
  
  /* disable multicast_loop such that there is no echo back on master */
  if (mcast_set_loop(send_fd, my_LOOP) < 0) 
    perror("init_sends(): when set MULTICAST_LOOP\n");  

  /* put dummy contents into send (UDP) buffer */
  memset(data_ptr, 1, PAGE_SIZE);
}

/*
  send_buffer will send the buffer with the file information
  out to the socket connection with the catcher.
*/
int send_buffer(int bytes_read)
{
    /* Else send the data */
    if(sendto(send_fd, send_buff, bytes_read + HEAD_SIZE, 
	      0, (const struct sockaddr *)&send_addr, sizeof(send_addr)) < 0) {
      perror("Sending packet");
      exit(1);
    }
    return (1);
}

/*
  send_page takes a page from the current file and controls
  sending it out the socket to the catcher.  It calls send_buffer
  to do the actuall call to sendto.
*/
int send_page(int page)
{
  if (verbose>=2) fprintf(stderr, "in send_page\n");
  *total_bytes_ptr = htonl(pageSize+HEAD_SIZE);
  *current_page_ptr = htonl(page);
  
  return send_buffer(pageSize);
}


void send_cmd(int code, int pages) 
{  
  *mode_ptr = htonl(code);
  *current_page_ptr = htonl(pages);  
  *total_bytes_ptr = htonl(HEAD_SIZE);

  send_buffer(0);
}


void send_all_done_cmd()
{
  *mode_ptr = htonl(ALL_DONE_CMD);  
  *current_page_ptr = 0; 
  *total_bytes_ptr = htonl(HEAD_SIZE) ;
  
  send_buffer(0);
  if (verbose) fprintf(stderr, "(ALL DONE)\n");
}