// $Id: pdinrs_distribution.h,v 1.1.1.1 2005/01/10 18:47:02 pdi Exp $
/******************************************************************************/
// Copyright 1998-2022 Stonie R. Cooper, Licensed under the auspices of GPLv3,
// See top-level copyright disclaimer (COPYRIGHT) and license (LICENSE)
// File:   distribute.c
// Author: Stonie R. Cooper
// Documentation:
// Revision Record:
//         Rewritten: 20050114 by Stonie R. Cooper
//                  - created file and added includes and function declara-
//                    tion.
/******************************************************************************/
// include file listing - standard C

#ifndef PDINRS_DISTRIBUTION_H
#define PDINRS_DISTRIBUTION_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <zlib.h>
#include <bzlib.h>
#include <pdinrs.h>

#ifndef __USE_MISC
#warning "Reentrant flags must be set"
#endif

typedef struct
{
  PDI_Byte HDLC_address;
  PDI_Byte HDLC_control;
  PDI_Byte SBN_version;
  PDI_Byte SBN_control;
  PDI_Byte SBN_command;
  PDI_Byte SBN_data_stream;
  PDI_Byte SBN_source;
  PDI_Byte SBN_destination;
  PDI_Byte SBN_sequence_number[4];
  PDI_Byte SBN_run[2];
  PDI_Byte SBN_checksum[2];
} FrameLevelHeaderType;

typedef struct
{
  PDI_Byte PDHVN;
  PDI_Byte Transfer_type;
  PDI_Byte Header_length[2];
  PDI_Byte Block_number[2];
  PDI_Byte Data_block_offset[2];
  PDI_Byte Data_block_size[2];
  PDI_Byte Records_per_block;
  PDI_Byte Blocks_per_record;
  PDI_Byte Product_sequence_number[4];
} ProductDefinitionHeaderType;

typedef struct
{
  PDI_Byte Option_field_number;
  PDI_Byte Option_field_type;
  PDI_Byte Option_field_length[2];
  PDI_Byte Product_specific_version;
  PDI_Byte Product_specific_flag;
  PDI_Byte Product_specific_data_length[2];
  PDI_Byte Number_of_bytes_per_record[2];
  PDI_Byte Product_specific_type;
  PDI_Byte Product_specific_category;
  PDI_Byte Product_code[2];
  PDI_Byte Fragments[2];
  PDI_Byte Next_header_offset[2];
  PDI_Byte Reserved;
  PDI_Byte Product_source;
  PDI_Byte Original_product_sequence_number[4];
  PDI_Byte Product_NCF_receive_time[4];
  PDI_Byte Product_NCF_transmit_time[4];
  PDI_Byte Current_product_run_id[2];
  PDI_Byte Product_run_ID[2];
} ProductSpecificHeaderType;

typedef struct
{
  PDI_Byte FF_CCB_length[2];
  PDI_Byte Mode_Submode[2];
  PDI_Byte Precedence;
  PDI_Byte Classification;
  PDI_Byte ICAO_Originator[4];
  PDI_Byte Category;
  PDI_Byte Subcategory;
  PDI_Byte UserDefined1;
  PDI_Byte UserDefined2;
  PDI_Byte Year;
  PDI_Byte Month;
  PDI_Byte Day;
  PDI_Byte Hour;
  PDI_Byte Minute;
  PDI_Byte Destination;
  PDI_Byte ICAO_Destination[4];
} CCBType;

typedef struct
{
  char host[SHORTLINESIZE];
  int  port;
  unsigned char open_flag;
  int  sock_ptr;
  time_t last_time;
} sock_list_type;

typedef struct
{
  char host[SHORTLINESIZE];
  unsigned char open_flag;
  unsigned char open_elsewhere;
  char temp_file[(MAXLINESIZE-2)];
  char dest_file[(MAXLINESIZE-2)];
  char method[SHORTLINESIZE];
  FILE *temp_ptr;
} ftp_list_type;

typedef struct
{
  char destination[(MAXLINESIZE-2)];
  unsigned char open_flag;
  unsigned char open_elsewhere;
  char temp_file[(MAXLINESIZE-2)];
  FILE *temp_ptr;
} mail_list_type;

typedef struct
{
  char destination[(MAXLINESIZE-2)];
  unsigned char open_flag;
  unsigned char open_elsewhere;
  char temp_file[(MAXLINESIZE-2)];
  FILE *temp_ptr;
} list_list_type;

void printFrameHeader(FrameLevelHeaderType fh);

void printProductDefinitionHeader(ProductDefinitionHeaderType pdh);

void printProductSpecificHeader(ProductSpecificHeaderType psh);

void correctFrameHeaderBits(FrameLevelHeaderType *fh);

void correctProductDefinitionHeaderBits(ProductDefinitionHeaderType *pdh);

void correctProductSpecificHeaderBits(ProductSpecificHeaderType *psh);

char *strrepl(char *Str, size_t BufSiz, char *OldStr, char *NewStr);

int open_socket(char *hostname, unsigned short int port, time_t seconds);

int Send(int fd, const char *vptr, size_t n, time_t seconds, unsigned int retries);

void ignore_spipe(void);

#endif
