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
/*
 *  Copyright (c) 2005 Ian McDonald <[email protected]>
 *
 *  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 of the License, 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 "common.hpp"

enum proto default_proto = DCCP;
string port = "5001";
string default_dest_path = "/tmp/send/Timing_Testslate_HD-8-Channel_v1.MXF";
string file = "/tmp/recv/Timing_Testslate_HD-8-Channel_v1.MXF";

void printerrno(const char *msg) {
	cout << msg << " " << errno << " " << strerror(errno) << endl;
}

void sigpipe(int dummy) {
}

ssize_t writen(int fd, const char *vptr, size_t n, enum proto default_proto, struct sockaddr_in server) {
	size_t nleft;
	ssize_t nwritten;
	const char *ptr;

	ptr = (const char *) vptr;
	nleft = n;
	int length=sizeof(struct sockaddr_in);

	while (nleft > 0) {
		if (default_proto != UDP) {
			if ((nwritten = send(fd,ptr,nleft, 0)) <= 0) {
				if (nwritten < 0 && errno == EINTR)
					nwritten = 0;
				else
					return -1;
			}
		}
		else {
			if ((nwritten = sendto(fd,ptr,nleft, 0, (const sockaddr*) &server, length)) <= 0) {
				if (nwritten < 0 && errno == EINTR)
					nwritten = 0;
				else
					return -1;
			}
		}
		
		nleft -= nwritten;
		ptr += nwritten;
	}
	return n;
}

int make_socket(void) {
	int new_sock;
	
	switch (default_proto) {
		case TCP:
			new_sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
			break;
		case DCCP:
			new_sock = socket(AF_INET,SOCK_DCCP,IPPROTO_DCCP);
			break;
		case UDP:
			new_sock = socket(AF_INET, SOCK_DGRAM, 0);
			break;
		default:
			printf("make_socket - invalid protocol\n");
	}
	return new_sock;
}

void socket_options(int conn_sock) {
	int pkt_size=256;

	switch (default_proto) {
		case DCCP:
			setsockopt(conn_sock,SOL_DCCP,DCCP_SOCKOPT_PACKET_SIZE,
					(char*)&pkt_size,sizeof(pkt_size));
			setsockopt(conn_sock,SOL_DCCP,DCCP_SOCKOPT_SERVICE,
					(char*)&pkt_size,sizeof(pkt_size));
			/* above is hack to get a service code 
			 * needs to be done properly
			 */
			break;
		case TCP:
		case UDP:
		case PUDT:
			break;
		default:
			printf("socket_options - invalid protocol\n");
	}

	signal(SIGPIPE, sigpipe);
}

int str_to_int (string str) {
	int i;

	istringstream myStream(str);

	if (myStream >> i)
		return i;
	else
		return -1;
}

string convert_int2string(int64_t i) {
	stringstream s;
	s << i;
	return s.str();
}