-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgcli.c
More file actions
44 lines (40 loc) · 1.15 KB
/
Copy pathmsgcli.c
File metadata and controls
44 lines (40 loc) · 1.15 KB
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
#include "msgcli.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
uint16_t compose_entete(uint8_t codeReq, uint16_t id) {
uint16_t temp = (id & 0x7FF) | (codeReq << 11);
uint16_t e = htons(temp);
return e;
}
void extract_entete(uint16_t e, uint8_t* codeReq, uint16_t* id) {
uint16_t r = ntohs(e);
*codeReq = (r >> 11) & 0x1F;
*id = r & 0x7FF;
}
msg_inscri * compose_msg_inscri(uint16_t entete, const char * pseudo){
msg_inscri * m = malloc(sizeof(msg_inscri));
m->entete = entete;
strcpy(m->pseudo, pseudo);
return m;
}
msg_fil * compose_msg_fil(const char * data, uint16_t codeReq, uint16_t id, uint16_t numfil, uint16_t nb){
size_t data_size = 0;
if(data != NULL){
data_size = strlen(data) + 1;
}
msg_fil * message = malloc(sizeof(msg_fil));
message->entete = compose_entete(codeReq, id);
message->numfil = htons(numfil);
message->nb = htons(nb);
if(data != NULL){
message->datalen = data_size;
strcpy(message->data, data);
}
return message;
}