diff options
author | deva <deva> | 2008-03-18 11:57:02 +0000 |
---|---|---|
committer | deva <deva> | 2008-03-18 11:57:02 +0000 |
commit | a6c5f2f2db35be2bc3052c02f75f20729ce4f923 (patch) | |
tree | d1acacab4dce8aaffc9debffb92fff67c85e47c4 /server/src/journal_commit.cc | |
parent | 589fd5ffdeaae87410c31c497083e609e7ae1f53 (diff) |
Adding missing files... oops
Diffstat (limited to 'server/src/journal_commit.cc')
-rw-r--r-- | server/src/journal_commit.cc | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/server/src/journal_commit.cc b/server/src/journal_commit.cc new file mode 100644 index 0000000..336ac7a --- /dev/null +++ b/server/src/journal_commit.cc @@ -0,0 +1,124 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * journal_commit.cc + * + * Tue Mar 18 11:10:00 CET 2008 + * Copyright 2008 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup + * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro 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. + * + * Pracro 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 Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "journal_commit.h" + +// for gethostbyname +#include <netdb.h> + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <string.h> +#include <unistd.h> +#include <endian.h> +#include <arpa/inet.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <stdarg.h> +#include <fcntl.h> +#include <time.h> + +//#define NO_NETWORK + +static int mwrite(int sock, char *fmt, ...) +{ + int l = 0; + va_list args; + char *buffer; + + buffer = (char*)malloc(64*1024); + + va_start(args, fmt); + l = vsnprintf(buffer, 64*1024, fmt, args); + va_end(args); + + write(sock, buffer, l); + + free(buffer); + + return l; +} + +int journal_commit(const char *cpr, const char *user, + const char *addr, unsigned short int port, + const char *buf, size_t size) +{ + struct sockaddr_in sin; + int sock = 1; + +#ifndef NO_NETWORK + // Do DNS lookup + char *ip; + struct in_addr **addr_list; + struct hostent *he; + he = gethostbyname(addr); + if(!he || !he->h_length) { + fprintf(stderr, "gethostbyname(%s) failed!\n", addr); + perror(":"); + return -1; + } + + addr_list = (struct in_addr **)he->h_addr_list; + // Get first value. We know for sure that there are at least one. + ip = inet_ntoa(*addr_list[0]); + + // open socket + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = inet_addr(ip); + sin.sin_port = htons(port); + + if( (sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { + fprintf(stderr, "Socket() failed!\n"); + return -1; + } + + if(connect(sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) { + fprintf(stderr, "Connect() failed!\n"); + perror(":"); + return -1; + } +#endif/*NO_NETWORK*/ + + // send header + mwrite(sock, "PUT JOURNAL PROTO1.0 \r\n"); + mwrite(sock, "size : %i\r\n", size); + mwrite(sock, "user: %s\r\n", user); + // mwrite(sock, "password: 1234\r\n"); + mwrite(sock, "cpr: %s\r\n", cpr); + mwrite(sock, "date: %i\r\n", time(NULL)); + mwrite(sock, "charset: utf8\r\n"); + mwrite(sock, "\r\n"); + + // send body + write(sock, buf, size); + + // close socket + close(sock); + + return 0; +} |