-
Notifications
You must be signed in to change notification settings - Fork 0
/
netstrings.c
131 lines (120 loc) · 3.01 KB
/
netstrings.c
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
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Joris Dedieu
* ----------------------------------------------------------------------------
*
* $Id$
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/stat.h>
int netprintf(int socket, const char* format, ...);
char *netread(int socket);
/* Write to socket */
int netprintf(int socket, const char* format, ...){
va_list ap;
char *resolved, *netstring, *number;
size_t len;
/* resolv the format */
va_start(ap, format);
if((resolved=(char *) malloc(sizeof(char))) == NULL){
perror("malloc");
return EXIT_FAILURE;
}
len=vsnprintf(resolved,1,format, ap);
if(len >=1){
len++;
if((resolved=(char *) realloc(resolved,sizeof(char)*len)) == NULL){
perror("realloc");
return EXIT_FAILURE;
}
(void) vsnprintf(resolved,len,format, ap);
}
va_end(ap);
/* compose the netstring */
if((number=(char *) malloc(sizeof(char))) == NULL){
perror("malloc");
return EXIT_FAILURE;
}
len=snprintf(number,1,"%d",strlen(resolved));
if(len >=1){
len++;
if((number=(char *) realloc(number,sizeof(char)*len)) == NULL){
perror("realloc");
return EXIT_FAILURE;
}
(void) snprintf(number,len,"%d",strlen(resolved));
}
len += sizeof(char) * strlen(resolved);
len += sizeof(char) * 2; // ':' + '\0'
if((netstring=(char *) malloc(len)) == NULL){
perror("malloc");
return EXIT_FAILURE;
}
len=snprintf(netstring,len,"%s:%s", number, resolved);
if(resolved) free(resolved);
/* change \0 to , */
netstring[len]=',';
/* Write to socket */
if(write(socket, netstring, len+1)) {
/* free need a limit */
netstring[len]='\0';
if(netstring) free(netstring);
return EXIT_SUCCESS;
} else{
netstring[len]='\0';
if(netstring) free(netstring);
return EXIT_FAILURE;
}
}
char *netread(int socket){
char *buffer, c[2];
size_t len=0;
if((buffer = (char *) malloc(sizeof(char))) == NULL){
perror("malloc");
return NULL;
}
for(;;){
read(socket,&c, 1);
c[1]='\0';
if(c[0] == ':')
break;
if(isdigit(c[0])){
len = 10 * len + atoi(c);
}
else {
#ifdef DAEMON
fprintf(stderr, "Not a netstring\n");
#endif /* DAEMON */
return NULL;
}
}
if(len > 0){
if((buffer = (char *) malloc(sizeof(char) * (len +1))) == NULL){
perror("malloc");
return NULL;
}
read(socket, buffer, len + 1);
if(buffer[len] != ','){
#ifdef DAEMON
fprintf(stderr, "Not a netstring\n");
#endif /* DAEMON */
free(buffer);
return NULL;
}
buffer[len] = '\0';
return buffer;
}
#ifdef DAEMON
fprintf(stderr, "Not a netstring\n");
#endif /* DAEMON */
return NULL;
}