void packet_force_logout(DESC_DATA *desc, CONN_DATA *conn) { PACKET_DATA *buf = &desc->packet_buffer; packet_header(buf, HEADER_TG_FORCE_LOGOUT, desc->desc_num); packet(buf, GET_LOGIN(conn), MAX_LOGIN_LEN); }
void packet_login_notice(CONN_DATA *conn) { DESC_DATA *desc = GET_DESC(conn); if (NULL==desc) return; PACKET_DATA *buf = &desc->packet_buffer; packet_header(buf, HEADER_TG_LOGIN_NOTICE, desc->desc_num); packet(buf, GET_LOGIN(conn), MAX_LOGIN_LEN); packet(buf, encode_4bytes(GET_ON_TIME(conn)), 4); packet(buf, encode_4bytes(GET_OFF_TIME(conn)), 4); }
void packet_teen_notice(CONN_DATA *conn, int hour) { DESC_DATA *desc = GET_DESC(conn); PACKET_DATA *buf = &desc->packet_buffer; packet_header(buf, HEADER_TG_TEEN_NOTICE, desc->desc_num); packet(buf, GET_LOGIN(conn), MAX_LOGIN_LEN); packet(buf, encode_4bytes(hour), 4); sys_log("NOTICE: (login,hour) = (%s, %d)", GET_LOGIN(conn), hour); }
unsigned char* create_kv_packet(const unsigned char* key, size_t key_sz, const unsigned char* val, size_t val_sz, struct packet* pkt, unsigned char* ptr) { size_t sz = PACKET_PREFIX + 2 + key_sz + val_sz; pkt->data = ptr; pkt->size = sz; memset(ptr, 0, sz); ptr = packet_header(sz, ptr); ptr = packet_memmove(ptr, key, key_sz); *ptr = ' '; ++ptr; ptr = packet_memmove(ptr, val, val_sz); *ptr = '\n'; ++ptr; return ptr; }
int parse_kv_packet(const struct packet* pkt, const unsigned char** key, size_t* key_sz, const unsigned char** val, size_t* val_sz) { unsigned char* tmp = NULL; unsigned char prefix[PACKET_PREFIX]; *key = NULL; *key_sz = 0; *val = NULL; *val_sz = 0; if (pkt->size > PACKET_MAX_SIZE) { return -1; } packet_header(pkt->size, prefix); if (pkt->size < PACKET_PREFIX + 2 || memcmp(pkt->data, prefix, PACKET_PREFIX) != 0 || pkt->data[pkt->size - 1] != '\n') { return -1; } tmp = memchr(pkt->data + PACKET_PREFIX, ' ', pkt->size - PACKET_PREFIX); if (!tmp) { return -1; } *key = pkt->data + PACKET_PREFIX; *key_sz = tmp - *key; *val = tmp + 1; *val_sz = pkt->size - PACKET_PREFIX - 2 - *key_sz; return 0; }
static int detect_geos(void) { int i, j; uint8_t *gnss_buffer = chHeapAlloc(NULL, 1024); int detected = 0; palSetPad(IOPORT1, PIOA_GPS_NRST); chThdSleepMilliseconds(100); k2_usart1_geos(); for (i = 0; i < 20; i++) { int t = sdReadTimeout(&SD1, gnss_buffer, 1024, 250); if (t > 0) { for (j = 0; j < t; j++) { if (packet_header(gnss_buffer[j], (uint8_t *) "PSGG", 4)) detected = 1; } } } chHeapFree(gnss_buffer); return detected; }
void packet_detector_geos(int c) { static int state = 0; static int packetnum = 0; static int packetsize = 0; static int packet_count = 0; int crc; static struct packet_msg *packet = NULL; if (state != 0) crc = geos_crc(c, 0); switch(state) { case 0: if (packet_header(c, (uint8_t *)"PSGG", 4)) { state = 1; geos_crc('P', 1); geos_crc('S', 0); geos_crc('G', 0); geos_crc('G', 0); } break; case 1: packetnum = c; state = 2; break; case 2: packetnum |= (c << 8); state = 3; break; case 3: packetsize = c; state = 4; break; case 4: packetsize |= (c << 8); state = 5; packet = chHeapAlloc(NULL, sizeof(struct packet_msg) + packetsize * 4); if (!packet) { printf("Drop\r\n"); state = 0; break; } packet->len = packetsize *4; packet->id = packetnum; break; case 5: case 6: case 7: case 8: if (packet) { if (packet->len > packet_count) { packet->data[packet_count] = c; packet_count++; } } if (state == 8) { packetsize--; if(!packetsize) state = 100; else state = 5; } else state++; break; /* CRC */ case 100: case 101: case 102: state++; break; case 103: state = 0; if(crc) { printf("b\r\n"); chHeapFree(packet); } else { process_packet(packet); } packet_count = 0; break; } }
int input_login(DESC_DATA *d, const char *data, int plen) { const int PACKET_LENGTH = 4; if (plen < PACKET_LENGTH) return 0; DWORD account_id = decode_4bytes((const BYTE*)data); // sys_log("LOGIN: %u", account_id); /* 기존 정보가 있으면 접속을 끊으라고 알려줌 */ CONN_DATA *conn = conn_find(account_id); if (conn) { send_force_logout(conn); //conn_delete(GET_ID(conn)); // 새로운 접속자도 접속을 끊는다. { PACKET_DATA *buf = &d->packet_buffer; packet_header(buf, HEADER_TG_FORCE_LOGOUT, d->desc_num); packet(buf, GET_LOGIN(conn), MAX_LOGIN_LEN); } return PACKET_LENGTH; } conn = (CONN_DATA*) calloc(1, sizeof(CONN_DATA)); GET_ID(conn) = account_id; GET_DESC(conn) = d; GET_EVENT(conn) = NULL; int login_succ = FALSE; if (teen_all_flag) login_succ = account_login_teen_all(account_id, conn); else login_succ = account_login(account_id, conn); if (FALSE==login_succ) { //conn_delete(GET_ID(conn)); SAFE_FREE(conn); return PACKET_LENGTH; } conn_insert(conn); if (YES==GET_TEENAGE(conn)) { conn_event_info *info = (conn_event_info*) calloc(1, sizeof(conn_event_info)); info->conn = conn; GET_EVENT(conn) = event_create(conn_event_func, info, (CHECK_TIME_SEC * PASSES_PER_SEC)); } else { GET_EVENT(conn) = NULL; } if (YES==GET_TEENAGE(conn)) { send_login_notice(conn); set_notice_step(conn); send_notice(conn); } sys_log("CONN_INSERT : %u, %s", GET_ID(conn), GET_LOGIN(conn)); return PACKET_LENGTH; } /* end of input_login() */