aboutsummaryrefslogtreecommitdiff
path: root/dpid/misc_new.c
blob: 8c07a4eb364ffcb15150197de30b2d9557006c90 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * File: misc_new.c
 *
 * Copyright 2008 Jorge Arellano Cid <jcid@dillo.org>
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 */

#include <errno.h>      /* errno, err-codes */
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>   /* stat */
#include <stdlib.h>     /* rand, srand */

#include "../dlib/dlib.h"
#include "dpid_common.h"
#include "misc_new.h"   /* for function prototypes */


/*
 * Close a FD handling EINTR.
 */
int a_Misc_close_fd(int fd)
{
   int st;

   do {
      st = close(fd);
   } while (st < 0 && errno == EINTR);
   return st;
}

/*! Reads a dpi tag from a socket
 * \li Continues after a signal interrupt
 * \Return
 * Dstr pointer to tag on success, NULL on failure
 * \important Caller is responsible for freeing the returned Dstr *
 */
Dstr *a_Misc_rdtag(int socket)
{
   char c = '\0';
   ssize_t rdlen;
   Dstr *tag;

   tag = dStr_sized_new(64);

   errno = 0;

   do {
      rdlen = read(socket, &c, 1);
      if (rdlen == -1 && errno != EINTR)
         break;
      dStr_append_c(tag, c);
   } while (c != '>');

   if (rdlen == -1) {
      perror("a_Misc_rdtag");
      dStr_free(tag, TRUE);
      return (NULL);
   }
   return (tag);
}

/*!
 * Read a dpi tag from sock
 * \return
 * pointer to dynamically allocated request tag
 */
char *a_Misc_readtag(int sock)
{
   char *tag, c;
   size_t i;
   size_t taglen = 0, tagmem = 10;
   ssize_t rdln = 1;

   tag = NULL;
   // new start
   tag = (char *) dMalloc(tagmem + 1);
   for (i = 0; (rdln = read(sock, &c, 1)) != 0; i++) {
      if (i == tagmem) {
         tagmem += tagmem;
         tag = (char *) dRealloc(tag, tagmem + 1);
      }
      tag[i] = c;
      taglen += rdln;
      if (c == '>') {
         tag[i + 1] = '\0';
         break;
      }
   }
   // new end
   if (rdln == -1) {
      ERRMSG("a_Misc_readtag", "read", errno);
   }

   return (tag);
}

/*! Reads a dpi tag from a socket without hanging on read.
 * \li Continues after a signal interrupt
 * \Return
 * \li 1 on success
 * \li 0 if input is not available within timeout microseconds.
 * \li -1 on failure
 * \important Caller is responsible for freeing the returned Dstr *
 */
/* Is this useful?
int a_Misc_nohang_rdtag(int socket, int timeout, Dstr **tag)
{
   int n_fd;
   fd_set sock_set, select_set;
   struct timeval tout;

   FD_ZERO(&sock_set);
   FD_SET(socket, &sock_set);

   errno = 0;
   do {
      select_set = sock_set;
      tout.tv_sec = 0;
      tout.tv_usec = timeout;
      n_fd = select(socket + 1, &select_set, NULL, NULL, &tout);
   } while (n_fd == -1 && errno == EINTR);

   if (n_fd == -1) {
      MSG_ERR("%s:%d: a_Misc_nohang_rdtag: %s\n",
              __FILE__, __LINE__, dStrerror(errno));
      return(-1);
   }
   if (n_fd == 0) {
      return(0);
   } else {
      *tag = a_Misc_rdtag(socket);
      return(1);
   }
}
*/

/*
 * Alternative to mkdtemp().
 * Not as strong as mkdtemp, but enough for creating a directory.
 */
char *a_Misc_mkdtemp(char *template)
{
   for (;;) {
      if (a_Misc_mkfname(template) && mkdir(template, 0700) == 0)
         break;
      if (errno == EEXIST)
         continue;
      return 0;
   }
   return template;
}

/*
 * Return a new, nonexistent file name from a template
 * (adapted from dietlibc; alternative to mkdtemp())
 */
char *a_Misc_mkfname(char *template)
{
   char *tmp = template + strlen(template) - 6;
   int i;
   uint_t random;
   struct stat stat_buf;

   if (tmp < template)
      goto error;
   for (i = 0; i < 6; ++i)
      if (tmp[i] != 'X') {
       error:
         errno = EINVAL;
         return 0;
      }
   srand((uint_t)(time(0) ^ getpid()));

   for (;;) {
      random = (unsigned) rand();
      for (i = 0; i < 6; ++i) {
         int hexdigit = (random >> (i * 5)) & 0x1f;

         tmp[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
      }
      if (stat(template, &stat_buf) == -1 && errno == ENOENT)
         return template;

      MSG_ERR("a_Misc_mkfname: another round for %s \n", template);
   }
}

/*
 * Return a new, random hexadecimal string of 'nchar' characters.
 */
char *a_Misc_mksecret(int nchar)
{
   int i;
   uint_t random;
   char *secret = dNew(char, nchar + 1);

   srand((uint_t)(time(0) ^ getpid()));
   random = (unsigned) rand();
   for (i = 0; i < nchar; ++i) {
      int hexdigit = (random >> (i * 5)) & 0x0f;

      secret[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
   }
   secret[i] = 0;
   MSG("a_Misc_mksecret: %s\n", secret);

   return secret;
}