HTTP패킷 과 Service Defined패킷을 분배한다.
Service Defined 패킷의 header 는 최대한 작은 사이즈로 작성한다.
int InterChange(int soc, char **ret_buf)
{
....CPINFO_HEADER cpinfo_header;
....char *buf2, *buf, c;
....int pos, size, len, LEN_BUF=1024;
....buf2 = (char *)calloc(LEN_BUF, sizeof(char));
....len = recv(soc, buf2, sizeof(cpinfo_header), 0);
..../* -- HTTP HEADER가 CPINFO_HEADER 보다 더 크다는 전제하의 루틴 -- */
....if (len == sizeof(cpinfo_header))
....{
........memcpy((char *)&cpinfo_header, buf2, sizeof(cpinfo_header));
........if (ntohl(cpinfo_header.command)==0)
........{
............*ret_buf = buf2;
............/*free(buf2);*/
........
............/* cpinfo head 만큼만 recv됐다면, Service define 패킷으로 판단한다. */
............return sizeof(cpinfo_header);
........}
....}
..../* recv 사이즈가 cpinfo header 보다 크다면 Http 패킷으로 판다한다. */
..../* buffer 사이즈를 재할당 후 위의 버퍼데이터를 옮긴다. */
....size = 4096;
....buf = (char *)calloc(size, sizeof(char));
....memcpy(buf, buf2, sizeof(cpinfo_header));
..../*free(buf2);*/
....len = 0;
..../* buffer 수신 사이즈를 cpinfo heade 만큼 이동시킨다. */
....pos = sizeof(cpinfo_header);
..../* --------- HTTP SOCK 루틴 ----------------- */
....while(1)
....{
........c = '\0';
........len = recv(soc, &c, 1, 0);
........if (len <= 0)
........{
............perror("recv");
............*ret_buf = buf;
............return (pos);
........}
........if (pos+1 >= size)
........{
............size += 4096;
...........
............if (buf == NULL)
............{
................buf = (char *)calloc(size, sizeof(char));
............}else{
................buf = (char *)realloc(buf, size * sizeof(char));
............}
........}
....
........buf[pos] = c;
........pos++;
........if (pos >= 1 && buf[pos-1] == '\n')
........{
............if (pos >= 2 && buf[pos-2] == '\r')
............{
................buf[pos-2]='\0';
................pos -= 2;
................break;
............}else{
................buf[pos-1] = '\0';
................pos--;
................break;
............}
........}
....}
....*ret_buf = buf;
..../*free(buf);*/
....return (pos);
}
'MY IT' 카테고리의 다른 글
바이러스 (0) | 2006.05.01 |
---|---|
Shell 정렬 (0) | 2006.04.14 |
calloc으로 2차원 배열 만들기 (0) | 2006.04.11 |
할당된 메모리 용량 (0) | 2006.04.11 |
스누핑 프로그램 심기 (0) | 2006.03.22 |