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
|
/*
* Copyright (C) 2016 Intel Corporation
*
* Author: Todor Minchev <todor.minchev@linux.intel.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, or (at your option) any later version, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope 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.
*/
#include "globals.h"
#include <stdlib.h>
#include <signal.h>
#include <netdb.h>
#include "utils.h"
#include "turff_api.h"
#define PORT_RANGE 100
extern char *turff_ops[];
extern char *codi_params[];
static int sock_fd;
/*TODO - close sockets on CTRL+C. Get rid of this when running as a daemon */
void close_sockets(int dummy) {
close(sock_fd);
INFO("\nClosed the sockets. Exiting!\n") ;
exit(0);
}
int main(int argc, char *argv[]) {
struct addrinfo *addr_p = NULL;
struct sockaddr cli_addr;
socklen_t cli_len;
int i, ceed_sock_fd, saved_out = -1, saved_err = -1, bound = 0;
const char *port;
parse_turff_params(argc, argv);
bound = 0;
port = turff_ops[KEY('s')];
while(!bound && (int) *port > (int) *turff_ops[KEY('s')] - PORT_RANGE) {
addr_p = bind_to_socket(NULL, port, &sock_fd);
if (addr_p == NULL) {
asprintf((char**)&port, "%d", atoi(port)-1) ;
} else {
turff_ops[KEY('s')] = (char*) port;
register_agent(turff_ops);
bound = 1;
INFO("TURFF listening on port: %s\n", port);
}
}
if (addr_p == NULL) {
ERR("Could not bind agent to socket\n");
exit(EXIT_FAILURE);
}
listen(sock_fd,1);
cli_len = sizeof(cli_addr);
signal(SIGINT, close_sockets);
while(1) {
ceed_sock_fd = accept(sock_fd, &cli_addr, &cli_len);
if (ceed_sock_fd < 0) {
ERR("ERROR on accept");
}
/* receive parameters from CODI */
receive_args(ceed_sock_fd, codi_params);
if (!strcmp(CODI_NAME, codi_params[KEY('z')])) {
/* save stdout and stderr file descriptors */
dup2(2, saved_err);
dup2(1, saved_out);
/* redirect stdout & stderr to ceed socket */
dup2(ceed_sock_fd, 2);
dup2(ceed_sock_fd, 1);
/* make sure ceed and turff APIs match*/
if (!strcmp(turff_ops[KEY('v')], codi_params[KEY('v')])) {
/* process ceed requests */
if (process_params(codi_params))
ERR("ERROR processing ceed request\n");
} else {
INFO("Incompatible versions: TURFF[%s] - CODI[%s]\n",
turff_ops[KEY('v')], codi_params[KEY('v')]);
}
/* delay EOM - send as separate buffer */
fprintf(stdout, TURFF_EOM);
fflush(stdout);
/* restore stdout and stderr */
dup2(saved_err, 2);
dup2(saved_out, 1);
/* clear parameters and wait for a new service request */
for (i = 0; i< KEY_ARR_SZ; i++){
if (codi_params[i] != NULL) {
#ifdef DBG
DEBUG("Received parameter [%c] : %s\n", i+'a', codi_params[i] );
#endif
free(codi_params[i]);
codi_params[i] = NULL ;
}
}
} else {
/* ceed connected to turff directly. send it EOM*/
INFO("Request did not originate from CODI!\n");
codi_params[KEY('e')] = "set";
send_args(ceed_sock_fd, codi_params);
}
close(ceed_sock_fd);
}
}
|