-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.c
63 lines (56 loc) · 1.34 KB
/
func.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "myftp.h"
void data_send(int sd, int type, int code, int len, uint8_t *pld)
{
int cnt;
struct myftph ftph;
uint8_t *data;
ftph.type = type;
ftph.code = code;
ftph.length = len;
data = (uint8_t *)malloc(sizeof(struct myftph) + len);
memcpy(data, &ftph, sizeof(struct myftph));
if (len != 0) {
memcpy(data + sizeof(struct myftph), pld, len);
}
if ((cnt = send(sd, data, sizeof(struct myftph) + len, 0)) < 0) {
perror("send");
close(sd);
exit(1);
}
/*
printf("type: %x, code: %x, length: %d\n",
((struct myftph *)data)->type,
((struct myftph *)data)->code,
((struct myftph *)data)->length);
*/
// free(data);
}
struct myftph *ftph_recv(int sd)
{
int cnt;
struct myftph *ftph;
ftph = (struct myftph *)malloc(sizeof(struct myftph));
if ((cnt = recv(sd, ftph, sizeof(struct myftph), 0)) < 0) {
perror("recv");
close(sd);
exit(1);
}
return ftph;
}
uint8_t *pld_recv(int sd, int size)
{
int cnt;
uint8_t *pld;
pld = (uint8_t *)malloc(size);
if ((cnt = recv(sd, pld, size, 0)) < 0) {
perror("recv");
close(sd);
exit(1);
}
return pld;
}