source: lib/misc.h @ c39cd8e

Last change on this file since c39cd8e was c39cd8e, checked in by Wilmer van der Gaast <wilmer@…>, at 2017-04-02T18:50:49Z

Add reverse_lookup() function which does a more proper (verified) DNS
reverse lookup. (Code copied from OpenSSH instead of redoing it
poorly.)

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Misc. functions                                                      */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
24*/
25
26#ifndef _MISC_H
27#define _MISC_H
28
29#include <gmodule.h>
30#include <time.h>
31#include <sys/socket.h>
32
33struct ns_srv_reply {
34        int prio;
35        int weight;
36        int port;
37        char name[];
38};
39
40#ifndef NAMESER_HAS_NS_TYPES
41
42#define NS_MAXDNAME 1025
43#define NS_INT16SZ  2
44#define NS_INT32SZ  4
45
46#define NS_GET16(s, cp) do { \
47                register const unsigned char *t_cp = (const unsigned char *) (cp); \
48                (s) = ((guint16) t_cp[0] << 8) \
49                      | ((guint16) t_cp[1]) \
50                ; \
51                (cp) += NS_INT16SZ; \
52} while (0)
53
54#define NS_GET32(s, cp) do { \
55                register const unsigned char *t_cp = (const unsigned char *) (cp); \
56                (s) = ((guint16) t_cp[0] << 24) \
57                      | ((guint16) t_cp[1] << 16) \
58                      | ((guint16) t_cp[2] << 8) \
59                      | ((guint16) t_cp[3]) \
60                ; \
61                (cp) += NS_INT32SZ; \
62} while (0)
63
64#define ns_rr_rdlen(rr) ((rr).rdlength + 0)
65#define ns_rr_rdata(rr) ((rr).rdata + 0)
66
67struct _ns_flagdata { int mask, shift; };
68
69typedef struct __ns_rr {
70        char name[NS_MAXDNAME];
71        guint16 type;
72        guint16 rr_class;
73        guint32 ttl;
74        guint16 rdlength;
75        const unsigned char* rdata;
76} ns_rr;
77
78typedef enum __ns_sect {
79        ns_s_qd = 0,
80        ns_s_zn = 0,
81        ns_s_an = 1,
82        ns_s_pr = 1,
83        ns_s_ns = 2,
84        ns_s_ud = 2,
85        ns_s_ar = 3,
86        ns_s_max = 4
87} ns_sect;
88
89typedef struct __ns_msg {
90        const unsigned char* _msg;
91        const unsigned char* _eom;
92        guint16 _id;
93        guint16 _flags;
94        guint16 _counts[ns_s_max];
95        const unsigned char* _sections[ns_s_max];
96        ns_sect _sect;
97        int _rrnum;
98        const unsigned char* _msg_ptr;
99} ns_msg;
100
101typedef enum __ns_class {
102        ns_c_invalid = 0,
103        ns_c_in = 1,
104        ns_c_2 = 2,
105        ns_c_chaos = 3,
106        ns_c_hs = 4,
107        ns_c_none = 254,
108        ns_c_any = 255,
109        ns_c_max = 65536
110} ns_class;
111
112
113/* TODO : fill out the rest */
114typedef enum __ns_type {
115        ns_t_srv = 33
116} ns_type;
117
118#endif /* NAMESER_HAS_NS_INITPARSE */
119
120G_MODULE_EXPORT void strip_linefeed(gchar *text);
121G_MODULE_EXPORT char *add_cr(char *text);
122G_MODULE_EXPORT char *strip_newlines(char *source);
123
124G_MODULE_EXPORT time_t get_time(int year, int month, int day, int hour, int min, int sec);
125G_MODULE_EXPORT time_t mktime_utc(struct tm *tp);
126double gettime(void);
127
128G_MODULE_EXPORT void strip_html(char *msg);
129G_MODULE_EXPORT char *escape_html(const char *html);
130G_MODULE_EXPORT void http_decode(char *s);
131G_MODULE_EXPORT void http_encode(char *s);
132
133G_MODULE_EXPORT char *ipv6_wrap(char *src);
134G_MODULE_EXPORT char *ipv6_unwrap(char *src);
135
136G_MODULE_EXPORT signed int do_iconv(char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf);
137
138G_MODULE_EXPORT void random_bytes(unsigned char *buf, int count);
139
140G_MODULE_EXPORT int is_bool(char *value);
141G_MODULE_EXPORT int bool2int(char *value);
142
143G_MODULE_EXPORT struct ns_srv_reply **srv_lookup(char *service, char *protocol, char *domain);
144G_MODULE_EXPORT void srv_free(struct ns_srv_reply **srv);
145
146G_MODULE_EXPORT char *reverse_lookup(const struct sockaddr *from_, const socklen_t fromlen_);
147G_MODULE_EXPORT void ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len);
148
149G_MODULE_EXPORT char *word_wrap(const char *msg, int line_len);
150G_MODULE_EXPORT gboolean ssl_sockerr_again(void *ssl);
151G_MODULE_EXPORT int md5_verify_password(char *password, char *hash);
152G_MODULE_EXPORT char **split_command_parts(char *command, int limit);
153G_MODULE_EXPORT char *get_rfc822_header(const char *text, const char *header, int len);
154G_MODULE_EXPORT int truncate_utf8(char *string, int maxlen);
155G_MODULE_EXPORT gboolean parse_int64(char *string, int base, guint64 *number);
156G_MODULE_EXPORT char *str_reject_chars(char *string, const char *reject, char replacement);
157G_MODULE_EXPORT char *str_pad_and_truncate(const char *string, long char_len, const char *ellipsis);
158
159#endif
Note: See TracBrowser for help on using the repository browser.