/*
* $Id: sendto.c,v 1.1 1998/09/08 10:52:12 mash Exp mash $
*
* $Log: sendto.c,v $
* Revision 1.1 1998/09/08 10:52:12 mash
* Initial revision
*
*/
#include "m_imp.h"
#include
#include
#include
#ifdef UNIX
#include
#include
#include
#define SOCKET_ERROR -1
#else
#include
#endif
#include
/* global defaults */
#define PORT_NR 3000
#define HOST "localhost"
#define BUF_SIZE 1000
/* send message */
void send_message(int sockfd, char *buf){
int err;
/* debug */
printf("%s",buf);
err = send(sockfd, buf, strlen(buf), 0);
if (err <= 0){
perror("netsend");
exit(err);
}
}
main(int argc, char** argv){
struct sockaddr_in server;
struct hostent *hp;
int sockfd = -1;
int portno = PORT_NR;
char host[256];
int err,i;
char buf[BUF_SIZE];
// char buf_roh[1000];
char *basename = NULL;
s_cgi **cgiArgs = NULL;
#ifdef CGI
cgiDebug(0,0);
cgiArgs = (s_cgi**)cgiInit();
//cgiHeader();
printf("Content-type: text/html\n\n");
printf("\ncgi-sendto\n\n\n");
printf("cgi-sendto-test
\n");
#endif
/* create a socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
perror("creating socket");
exit(sockfd);
}
/* connect socket using hostname provided in command line */
server.sin_family = AF_INET;
/* host to contact */
if (getenv("PD_HOST") != 0){
sprintf(host, "%s", getenv("PD_HOST"));
} else if((cgiArgs) && cgiGetValue(cgiArgs, "PD_HOST"))
sprintf(host, "%s", cgiGetValue(cgiArgs, "PD_HOST"));
else
sprintf(host, "%s", HOST);
hp = gethostbyname(host);
if (hp == 0){
herror("bad host: %s ?\n");
fprintf(stderr, "host: %s\n", host);
exit(hp);
}
memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
/* assign client port number */
server.sin_port = htons((u_short)portno);
if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0){
perror("connecting stream socket");
exit(err);
}
/* build message */
#ifndef CGI
if(argc > 1){
if(strlen(argv[1]) > BUF_SIZE){
fprintf(stderr, "to much data to send\n");
exit(0);
}
/* strcpy(buf, argv[1]); */
/* err = strlen(buf); */
/* buf[err] = ';'; */
/* buf[err++] = '\n'; */
/* buf[err++] = 0; */
sprintf(buf, "%s;\n", argv[1]);
send_message(sockfd, buf);
}
#else // cgi
if(cgiArgs){
err = 0;
for (i=0;(cgiArgs[i]);i++){
//debug
printf("netsend %d %s %s;
\n", i, cgiArgs[i]->name,
cgiArgs[i]->value);
fflush(stderr);
if (5 + strlen(cgiArgs[i]->name) +
strlen(cgiArgs[i]->value) < BUF_SIZE){
/* fprintf(stderr, "to much data to send\n"); */
/* break; */
/* } */
/* strcpy(buf + err, cgiArgs[i]->name); */
/* err += strlen(cgiArgs[i]->name); */
/* buf[err] = ' '; */
/* err++; */
/* strcpy(buf + err, cgiArgs[i]->value); */
/* err += strlen(cgiArgs[i]->value); */
/* buf[err] = ';'; */
/* buf[err++] = '\n'; */
/* buf[err++] = 0; */
sprintf(buf, "%s %s;\n", cgiArgs[i]->name, cgiArgs[i]->value);
send_message(sockfd, buf);
}
}
#endif
} else if(getenv("PD_DATA") != 0){
sprintf(buf, "%s;\n", getenv("PD_DATA"));
} else {
fprintf(stderr,
"no data to send in argv[1] or PD_DATA or CGI-input\n");
exit(0);
}
#ifdef CGI
printf("\n\n");
#endif
}