root/trunk/src/remote_output.c @ 54

Revision 28, 3.8 KB (checked in by andreu, 16 years ago)

debug mode in compilation option - Wno-long-long - copyright update

  • Property svn:eol-style set to native
Line 
1/*
2 * File: remote_output.c
3 *
4 * Authors: ANDREU Francois-Xavier
5 *
6 * Copyright (C) 2005 2006 2007 GIP RENATER
7 */
8
9/*  This file is part of renetcol.
10 *
11 *  renetcol is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  renetcol is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License
22 *  along with renetcol; if not, write to the Free Software
23 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 */
25
26#include <string.h> /* for memset */
27#include "remote_output.h"
28
29
30void 
31printHostDef(RemoteHostPtr rHPtr)
32{
33  char buf[256];
34  fprintf(stderr, " socket : %d\n", rHPtr->sockId);
35  switch (rHPtr->hostAddressPtr->ss_family){
36  case AF_INET:
37    fprintf(stderr, " address : %s\n",
38            inet_ntop(rHPtr->hostAddressPtr->ss_family,
39                      &((struct sockaddr_in *)rHPtr->hostAddressPtr)->sin_addr,
40                      buf, 256));
41    break;
42  case AF_INET6:
43    fprintf(stderr, " address : %s\n",
44            inet_ntop(rHPtr->hostAddressPtr->ss_family,
45                      &((struct sockaddr_in6 *)rHPtr->hostAddressPtr)->sin6_addr,
46                      buf, 256));
47    break;
48  }
49}
50
51/*
52 * freeRemoteHost()
53 */
54void
55freeRemoteHost(RemoteHostPtr rHPtr)
56{
57  if (rHPtr->hostAddressPtr){
58    free(rHPtr->hostAddressPtr);
59    rHPtr->hostAddressPtr = NULL;
60  }
61  free(rHPtr);
62  rHPtr = NULL;
63}
64
65/*
66 * freeDb()
67 */
68void
69freeDb(DbPtr dbPtr)
70{
71  if (dbPtr->dbName){
72    free(dbPtr->dbName);
73    dbPtr->dbName = NULL;
74  }
75  if (dbPtr->password){
76    free(dbPtr->password);
77    dbPtr->password = NULL;
78  }
79  if (dbPtr->tableName){
80    free(dbPtr->tableName);
81    dbPtr->tableName = NULL;
82  } 
83  free(dbPtr);
84  dbPtr = NULL;
85}
86
87/*
88 * freeFile()
89 */
90void
91freeFile(FilePtr fPtr)
92{
93  if (fPtr->fileName){
94    free(fPtr->fileName);
95    fPtr->fileName = NULL;
96  }
97  free(fPtr);
98  fPtr = NULL;
99}
100
101/*
102 * create_socket
103 */
104int 
105create_socket(char *remote_address, char *remote_port, struct sockaddr_storage *addr)
106{
107  int s;
108  struct addrinfo index, *res=NULL;
109  int code;
110
111  memset(&index, 0, sizeof index);
112  index.ai_family = AF_UNSPEC;
113  index.ai_socktype = SOCK_DGRAM;
114  index.ai_protocol = IPPROTO_UDP; 
115  code = getaddrinfo(remote_address, remote_port, &index, &res);
116  if (code!=0) {
117    syslog(LOG_ERR, "Error, %s : %s", gai_strerror(code), remote_address);
118    return 0;
119  }
120  s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
121  if (s < 0)
122    {
123      syslog(LOG_ERR,"Error during opening datagram socket %s",
124             strerror(errno));
125      return 0;
126    }
127  switch (res->ai_family){
128  case AF_INET6:
129    addr->ss_family = AF_INET6;
130    ((struct sockaddr_in6 *)addr)->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
131    ((struct sockaddr_in6 *)addr)->sin6_port =
132      htons((unsigned short)atoi(remote_port));
133    break;
134  case AF_INET:
135    ((struct sockaddr_in *)addr)->sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
136    addr->ss_family = AF_INET;
137    ((struct sockaddr_in *)addr)->sin_port =
138      htons((unsigned short)atoi(remote_port));
139    break;
140  case AF_UNSPEC:
141    break;
142  }
143  freeaddrinfo(res);
144  return s;
145}
146
147/*
148 * send_message
149 */
150int 
151sendMessage(int socket_number, unsigned char *msg, int msgSize,
152            struct sockaddr_storage *addr)
153{
154  int s;
155  socklen_t addrlen = sizeof(struct sockaddr_storage);
156  if ((s=sendto(socket_number, msg, (size_t)msgSize, 0,
157                (struct sockaddr *)addr, addrlen)) < 0)
158    {
159      syslog(LOG_ERR, "Error: %d bytes sent, %s",
160             s, strerror(errno));
161      exit(1);
162    }
163  return s;
164}
Note: See TracBrowser for help on using the browser.