Showing
11 changed files
with
678 additions
and
0 deletions
doc/참고 링크 목록/링크 목록.docx
0 → 100644
No preview for this file type
doc/최종보고서/최종보고서.docx
0 → 100644
No preview for this file type
source/hackcode/shellcode_test.c
0 → 100644
| 1 | +#include <stdio.h> | ||
| 2 | +#include <string.h> | ||
| 3 | + | ||
| 4 | +unsigned char shellcode[] = "\x01\x30\x8f\xe2\x13\xff\x2f\xe1\x02\xa0\x49\x40\x52\x40\xc2\x71\x0b\x27\x01\xdf\x2f\x62\x69\x6e\x2f\x73\x68\x78"; | ||
| 5 | + | ||
| 6 | +int main(){ | ||
| 7 | + printf("length: %d\n", strlen(shellcode)); | ||
| 8 | + | ||
| 9 | + int(*ret)() = (int(*)()) shellcode; | ||
| 10 | + | ||
| 11 | + ret(); | ||
| 12 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
source/modified/Makefile
0 → 100644
| 1 | +CC = gcc | ||
| 2 | +CFLAGS = | ||
| 3 | +LDFLAGS = -lpthread | ||
| 4 | + | ||
| 5 | +.SUFFIXES : .c .o | ||
| 6 | +.c.o : | ||
| 7 | + $(CC) -c $(CFLAGS) $< | ||
| 8 | + | ||
| 9 | +ALL = chats chatc | ||
| 10 | + | ||
| 11 | +all: $(ALL) | ||
| 12 | + | ||
| 13 | +chats: chats.o | ||
| 14 | + $(CC) -o $@ $< $(LDFLAGS) | ||
| 15 | + | ||
| 16 | +chatc: chatc.o | ||
| 17 | + $(CC) -o $@ $< $(LDFLAGS) | ||
| 18 | + | ||
| 19 | +clean : | ||
| 20 | + rm -rf *.o $(ALL) |
source/modified/chat.h
0 → 100644
source/modified/chatc.c
0 → 100644
| 1 | +#include <stdio.h> | ||
| 2 | +#include <unistd.h> | ||
| 3 | +#include <string.h> | ||
| 4 | +#include <signal.h> | ||
| 5 | +#include <sys/types.h> | ||
| 6 | +#include <sys/socket.h> | ||
| 7 | +#include <netinet/in.h> | ||
| 8 | +#include <arpa/inet.h> | ||
| 9 | +#include <netdb.h> | ||
| 10 | +#include "chat.h" | ||
| 11 | + | ||
| 12 | +#define MAX_BUF 256 | ||
| 13 | + | ||
| 14 | +int Sockfd; | ||
| 15 | + | ||
| 16 | +void | ||
| 17 | +ChatClient(void) | ||
| 18 | +{ | ||
| 19 | + char buf[MAX_BUF]; | ||
| 20 | + int count, n; | ||
| 21 | + fd_set fdset; | ||
| 22 | + | ||
| 23 | + printf("Enter ID: "); | ||
| 24 | + fflush(stdout); | ||
| 25 | + fgets(buf, MAX_BUF, stdin); | ||
| 26 | + *strchr(buf, '\n') = '\0'; | ||
| 27 | + if (send(Sockfd, buf, strlen(buf)+1, 0) < 0) { | ||
| 28 | + perror("send"); | ||
| 29 | + exit(1); | ||
| 30 | + } | ||
| 31 | + printf("Press ^C to exit\n"); | ||
| 32 | + | ||
| 33 | + while (1) { | ||
| 34 | + FD_ZERO(&fdset); | ||
| 35 | + FD_SET(Sockfd, &fdset); | ||
| 36 | + FD_SET(STDIN_FILENO, &fdset); | ||
| 37 | + | ||
| 38 | + if ((count = select(10, &fdset, (fd_set *)NULL, (fd_set *)NULL, | ||
| 39 | + (struct timeval *)NULL)) < 0) { | ||
| 40 | + perror("select"); | ||
| 41 | + exit(1); | ||
| 42 | + } | ||
| 43 | + while (count--) { | ||
| 44 | + if (FD_ISSET(Sockfd, &fdset)) { | ||
| 45 | + if ((n = recv(Sockfd, buf, MAX_BUF, 0)) < 0) { | ||
| 46 | + perror("recv"); | ||
| 47 | + exit(1); | ||
| 48 | + } | ||
| 49 | + if (n == 0) { | ||
| 50 | + fprintf(stderr, "Server terminated.....\n"); | ||
| 51 | + close(Sockfd); | ||
| 52 | + exit(1); | ||
| 53 | + } | ||
| 54 | + printf("%s", buf); | ||
| 55 | + } | ||
| 56 | + else if (FD_ISSET(STDIN_FILENO, &fdset)) { | ||
| 57 | + fgets(buf, MAX_BUF, stdin); | ||
| 58 | + if ((n = send(Sockfd, buf, strlen(buf)+1, 0)) < 0) { | ||
| 59 | + perror("send"); | ||
| 60 | + exit(1); | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | + | ||
| 68 | +void | ||
| 69 | +CloseClient(int signo) | ||
| 70 | +{ | ||
| 71 | + close(Sockfd); | ||
| 72 | + printf("\nChat client terminated.....\n"); | ||
| 73 | + | ||
| 74 | + exit(0); | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | + | ||
| 78 | +main(int argc, char *argv[]) | ||
| 79 | +{ | ||
| 80 | + struct sockaddr_in servAddr; | ||
| 81 | + struct hostent *hp; | ||
| 82 | + | ||
| 83 | + if (argc != 2) { | ||
| 84 | + fprintf(stderr, "Usage: %s ServerIPaddress\n", argv[0]); | ||
| 85 | + exit(1); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + if ((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { | ||
| 89 | + perror("socket"); | ||
| 90 | + exit(1); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + bzero((char *)&servAddr, sizeof(servAddr)); | ||
| 94 | + servAddr.sin_family = PF_INET; | ||
| 95 | + servAddr.sin_port = htons(SERV_TCP_PORT); | ||
| 96 | + | ||
| 97 | + if (isdigit(argv[1][0])) { | ||
| 98 | + servAddr.sin_addr.s_addr = inet_addr(argv[1]); | ||
| 99 | + } | ||
| 100 | + else { | ||
| 101 | + if ((hp = gethostbyname(argv[1])) == NULL) { | ||
| 102 | + fprintf(stderr, "Unknown host: %s\n", argv[1]); | ||
| 103 | + exit(1); | ||
| 104 | + } | ||
| 105 | + memcpy(&servAddr.sin_addr, hp->h_addr, hp->h_length); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + if (connect(Sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { | ||
| 109 | + perror("connect"); | ||
| 110 | + exit(1); | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + signal(SIGINT, CloseClient); | ||
| 114 | + | ||
| 115 | + ChatClient(); | ||
| 116 | +} |
source/modified/chats.c
0 → 100644
| 1 | +#include <stdio.h> | ||
| 2 | +#include <pthread.h> | ||
| 3 | +#include <sys/types.h> | ||
| 4 | +#include <sys/socket.h> | ||
| 5 | +#include <netinet/in.h> | ||
| 6 | +#include <arpa/inet.h> | ||
| 7 | +#include <signal.h> | ||
| 8 | +#include "chat.h" | ||
| 9 | + | ||
| 10 | +#define DEBUG | ||
| 11 | + | ||
| 12 | +#define MAX_CLIENT 5 | ||
| 13 | +#define MAX_ID 32 | ||
| 14 | +#define MAX_BUF 256 | ||
| 15 | + | ||
| 16 | +typedef struct { | ||
| 17 | + int sockfd; | ||
| 18 | + int inUse; | ||
| 19 | + pthread_t tid; | ||
| 20 | + char uid[MAX_ID]; | ||
| 21 | +} | ||
| 22 | + ClientType; | ||
| 23 | + | ||
| 24 | +int Sockfd; | ||
| 25 | +pthread_mutex_t Mutex; | ||
| 26 | + | ||
| 27 | +ClientType Client[MAX_CLIENT]; | ||
| 28 | + | ||
| 29 | + | ||
| 30 | +int | ||
| 31 | +GetID() | ||
| 32 | +{ | ||
| 33 | + int i; | ||
| 34 | + | ||
| 35 | + for (i = 0 ; i < MAX_CLIENT ; i++) { | ||
| 36 | + if (! Client[i].inUse) { | ||
| 37 | + Client[i].inUse = 1; | ||
| 38 | + return i; | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +void | ||
| 44 | +SendToOtherClients(int id, char *buf) | ||
| 45 | +{ | ||
| 46 | + int i; | ||
| 47 | + char msg[128+MAX_ID]; // Original: char msg[MAX_BUF+MAX_ID]; | ||
| 48 | + | ||
| 49 | + sprintf(msg, "%s> %s", Client[id].uid, buf); | ||
| 50 | +#ifdef DEBUG | ||
| 51 | + printf("%s", msg); | ||
| 52 | + fflush(stdout); | ||
| 53 | +#endif | ||
| 54 | + | ||
| 55 | + pthread_mutex_lock(&Mutex); | ||
| 56 | + for (i = 0 ; i < MAX_CLIENT ; i++) { | ||
| 57 | + if (Client[i].inUse && (i != id)) { | ||
| 58 | + if (send(Client[i].sockfd, msg, strlen(msg)+1, 0) < 0) { | ||
| 59 | + perror("send"); | ||
| 60 | + exit(1); | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + pthread_mutex_unlock(&Mutex); | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | + | ||
| 68 | +void | ||
| 69 | +ProcessClient(int id) | ||
| 70 | +{ | ||
| 71 | + char buf[MAX_BUF]; | ||
| 72 | + int n; | ||
| 73 | + | ||
| 74 | + if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) { | ||
| 75 | + perror("pthread_setcancelstate"); | ||
| 76 | + exit(1); | ||
| 77 | + } | ||
| 78 | + if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) { | ||
| 79 | + perror("pthread_setcanceltype"); | ||
| 80 | + exit(1); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + if ((n = recv(Client[id].sockfd, Client[id].uid, MAX_ID, 0)) < 0) { | ||
| 84 | + perror("recv"); | ||
| 85 | + exit(1); | ||
| 86 | + } | ||
| 87 | + printf("Client %d log-in(ID: %s).....\n", id, Client[id].uid); | ||
| 88 | + | ||
| 89 | + while (1) { | ||
| 90 | + if ((n = recv(Client[id].sockfd, buf, MAX_BUF, 0)) < 0) { | ||
| 91 | + perror("recv"); | ||
| 92 | + exit(1); | ||
| 93 | + } | ||
| 94 | + if (n == 0) { | ||
| 95 | + printf("Client %d log-out(ID: %s).....\n", id, Client[id].uid); | ||
| 96 | + | ||
| 97 | + pthread_mutex_lock(&Mutex); | ||
| 98 | + close(Client[id].sockfd); | ||
| 99 | + Client[id].inUse = 0; | ||
| 100 | + pthread_mutex_unlock(&Mutex); | ||
| 101 | + | ||
| 102 | + strcpy(buf, "log-out.....\n"); | ||
| 103 | + SendToOtherClients(id, buf); | ||
| 104 | + | ||
| 105 | + pthread_exit(NULL); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + SendToOtherClients(id, buf); | ||
| 109 | + } | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | + | ||
| 113 | +void | ||
| 114 | +CloseServer(int signo) | ||
| 115 | +{ | ||
| 116 | + int i; | ||
| 117 | + | ||
| 118 | + close(Sockfd); | ||
| 119 | + | ||
| 120 | + for (i = 0 ; i < MAX_CLIENT ; i++) { | ||
| 121 | + if (Client[i].inUse) { | ||
| 122 | + if (pthread_cancel(Client[i].tid)) { | ||
| 123 | + perror("pthread_cancel"); | ||
| 124 | + exit(1); | ||
| 125 | + } | ||
| 126 | + if (pthread_join(Client[i].tid, NULL)) { | ||
| 127 | + perror("pthread_join"); | ||
| 128 | + exit(1); | ||
| 129 | + } | ||
| 130 | + close(Client[i].sockfd); | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + if (pthread_mutex_destroy(&Mutex) < 0) { | ||
| 134 | + perror("pthread_mutex_destroy"); | ||
| 135 | + exit(1); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + printf("\nChat server terminated.....\n"); | ||
| 139 | + | ||
| 140 | + exit(0); | ||
| 141 | +} | ||
| 142 | + | ||
| 143 | +main(int argc, char *argv[]) | ||
| 144 | +{ | ||
| 145 | + int newSockfd, cliAddrLen, id, one = 1; | ||
| 146 | + struct sockaddr_in cliAddr, servAddr; | ||
| 147 | + | ||
| 148 | + signal(SIGINT, CloseServer); | ||
| 149 | + if (pthread_mutex_init(&Mutex, NULL) < 0) { | ||
| 150 | + perror("pthread_mutex_init"); | ||
| 151 | + exit(1); | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + if ((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { | ||
| 155 | + perror("socket"); | ||
| 156 | + exit(1); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + if (setsockopt(Sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { | ||
| 160 | + perror("setsockopt"); | ||
| 161 | + exit(1); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + bzero((char *)&servAddr, sizeof(servAddr)); | ||
| 165 | + servAddr.sin_family = PF_INET; | ||
| 166 | + servAddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
| 167 | + servAddr.sin_port = htons(SERV_TCP_PORT); | ||
| 168 | + | ||
| 169 | + if (bind(Sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { | ||
| 170 | + perror("bind"); | ||
| 171 | + exit(1); | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + listen(Sockfd, 5); | ||
| 175 | + | ||
| 176 | + printf("Chat server started.....\n"); | ||
| 177 | + | ||
| 178 | + cliAddrLen = sizeof(cliAddr); | ||
| 179 | + while (1) { | ||
| 180 | + newSockfd = accept(Sockfd, (struct sockaddr *) &cliAddr, &cliAddrLen); | ||
| 181 | + if (newSockfd < 0) { | ||
| 182 | + perror("accept"); | ||
| 183 | + exit(1); | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + id = GetID(); | ||
| 187 | + Client[id].sockfd = newSockfd; | ||
| 188 | + | ||
| 189 | + if (pthread_create(&Client[id].tid, NULL, (void *)ProcessClient, (void *)id) < 0) { | ||
| 190 | + perror("pthread_create"); | ||
| 191 | + exit(1); | ||
| 192 | + } | ||
| 193 | + } | ||
| 194 | +} |
source/original/Makefile
0 → 100644
| 1 | +CC = gcc | ||
| 2 | +CFLAGS = | ||
| 3 | +LDFLAGS = -lpthread | ||
| 4 | + | ||
| 5 | +.SUFFIXES : .c .o | ||
| 6 | +.c.o : | ||
| 7 | + $(CC) -c $(CFLAGS) $< | ||
| 8 | + | ||
| 9 | +ALL = chats chatc | ||
| 10 | + | ||
| 11 | +all: $(ALL) | ||
| 12 | + | ||
| 13 | +chats: chats.o | ||
| 14 | + $(CC) -o $@ $< $(LDFLAGS) | ||
| 15 | + | ||
| 16 | +chatc: chatc.o | ||
| 17 | + $(CC) -o $@ $< $(LDFLAGS) | ||
| 18 | + | ||
| 19 | +clean : | ||
| 20 | + rm -rf *.o $(ALL) |
source/original/chat.h
0 → 100644
source/original/chatc.c
0 → 100644
| 1 | +#include <stdio.h> | ||
| 2 | +#include <unistd.h> | ||
| 3 | +#include <string.h> | ||
| 4 | +#include <signal.h> | ||
| 5 | +#include <sys/types.h> | ||
| 6 | +#include <sys/socket.h> | ||
| 7 | +#include <netinet/in.h> | ||
| 8 | +#include <arpa/inet.h> | ||
| 9 | +#include <netdb.h> | ||
| 10 | +#include "chat.h" | ||
| 11 | + | ||
| 12 | +#define MAX_BUF 256 | ||
| 13 | + | ||
| 14 | +int Sockfd; | ||
| 15 | + | ||
| 16 | +void | ||
| 17 | +ChatClient(void) | ||
| 18 | +{ | ||
| 19 | + char buf[MAX_BUF]; | ||
| 20 | + int count, n; | ||
| 21 | + fd_set fdset; | ||
| 22 | + | ||
| 23 | + printf("Enter ID: "); | ||
| 24 | + fflush(stdout); | ||
| 25 | + fgets(buf, MAX_BUF, stdin); | ||
| 26 | + *strchr(buf, '\n') = '\0'; | ||
| 27 | + if (send(Sockfd, buf, strlen(buf)+1, 0) < 0) { | ||
| 28 | + perror("send"); | ||
| 29 | + exit(1); | ||
| 30 | + } | ||
| 31 | + printf("Press ^C to exit\n"); | ||
| 32 | + | ||
| 33 | + while (1) { | ||
| 34 | + FD_ZERO(&fdset); | ||
| 35 | + FD_SET(Sockfd, &fdset); | ||
| 36 | + FD_SET(STDIN_FILENO, &fdset); | ||
| 37 | + | ||
| 38 | + if ((count = select(10, &fdset, (fd_set *)NULL, (fd_set *)NULL, | ||
| 39 | + (struct timeval *)NULL)) < 0) { | ||
| 40 | + perror("select"); | ||
| 41 | + exit(1); | ||
| 42 | + } | ||
| 43 | + while (count--) { | ||
| 44 | + if (FD_ISSET(Sockfd, &fdset)) { | ||
| 45 | + if ((n = recv(Sockfd, buf, MAX_BUF, 0)) < 0) { | ||
| 46 | + perror("recv"); | ||
| 47 | + exit(1); | ||
| 48 | + } | ||
| 49 | + if (n == 0) { | ||
| 50 | + fprintf(stderr, "Server terminated.....\n"); | ||
| 51 | + close(Sockfd); | ||
| 52 | + exit(1); | ||
| 53 | + } | ||
| 54 | + printf("%s", buf); | ||
| 55 | + } | ||
| 56 | + else if (FD_ISSET(STDIN_FILENO, &fdset)) { | ||
| 57 | + fgets(buf, MAX_BUF, stdin); | ||
| 58 | + if ((n = send(Sockfd, buf, strlen(buf)+1, 0)) < 0) { | ||
| 59 | + perror("send"); | ||
| 60 | + exit(1); | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | + | ||
| 68 | +void | ||
| 69 | +CloseClient(int signo) | ||
| 70 | +{ | ||
| 71 | + close(Sockfd); | ||
| 72 | + printf("\nChat client terminated.....\n"); | ||
| 73 | + | ||
| 74 | + exit(0); | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | + | ||
| 78 | +main(int argc, char *argv[]) | ||
| 79 | +{ | ||
| 80 | + struct sockaddr_in servAddr; | ||
| 81 | + struct hostent *hp; | ||
| 82 | + | ||
| 83 | + if (argc != 2) { | ||
| 84 | + fprintf(stderr, "Usage: %s ServerIPaddress\n", argv[0]); | ||
| 85 | + exit(1); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + if ((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { | ||
| 89 | + perror("socket"); | ||
| 90 | + exit(1); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + bzero((char *)&servAddr, sizeof(servAddr)); | ||
| 94 | + servAddr.sin_family = PF_INET; | ||
| 95 | + servAddr.sin_port = htons(SERV_TCP_PORT); | ||
| 96 | + | ||
| 97 | + if (isdigit(argv[1][0])) { | ||
| 98 | + servAddr.sin_addr.s_addr = inet_addr(argv[1]); | ||
| 99 | + } | ||
| 100 | + else { | ||
| 101 | + if ((hp = gethostbyname(argv[1])) == NULL) { | ||
| 102 | + fprintf(stderr, "Unknown host: %s\n", argv[1]); | ||
| 103 | + exit(1); | ||
| 104 | + } | ||
| 105 | + memcpy(&servAddr.sin_addr, hp->h_addr, hp->h_length); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + if (connect(Sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { | ||
| 109 | + perror("connect"); | ||
| 110 | + exit(1); | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + signal(SIGINT, CloseClient); | ||
| 114 | + | ||
| 115 | + ChatClient(); | ||
| 116 | +} |
source/original/chats.c
0 → 100644
| 1 | +#include <stdio.h> | ||
| 2 | +#include <pthread.h> | ||
| 3 | +#include <sys/types.h> | ||
| 4 | +#include <sys/socket.h> | ||
| 5 | +#include <netinet/in.h> | ||
| 6 | +#include <arpa/inet.h> | ||
| 7 | +#include <signal.h> | ||
| 8 | +#include "chat.h" | ||
| 9 | + | ||
| 10 | +#define DEBUG | ||
| 11 | + | ||
| 12 | +#define MAX_CLIENT 5 | ||
| 13 | +#define MAX_ID 32 | ||
| 14 | +#define MAX_BUF 256 | ||
| 15 | + | ||
| 16 | +typedef struct { | ||
| 17 | + int sockfd; | ||
| 18 | + int inUse; | ||
| 19 | + pthread_t tid; | ||
| 20 | + char uid[MAX_ID]; | ||
| 21 | +} | ||
| 22 | + ClientType; | ||
| 23 | + | ||
| 24 | +int Sockfd; | ||
| 25 | +pthread_mutex_t Mutex; | ||
| 26 | + | ||
| 27 | +ClientType Client[MAX_CLIENT]; | ||
| 28 | + | ||
| 29 | + | ||
| 30 | +int | ||
| 31 | +GetID() | ||
| 32 | +{ | ||
| 33 | + int i; | ||
| 34 | + | ||
| 35 | + for (i = 0 ; i < MAX_CLIENT ; i++) { | ||
| 36 | + if (! Client[i].inUse) { | ||
| 37 | + Client[i].inUse = 1; | ||
| 38 | + return i; | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +void | ||
| 44 | +SendToOtherClients(int id, char *buf) | ||
| 45 | +{ | ||
| 46 | + int i; | ||
| 47 | + char msg[MAX_BUF+MAX_ID]; | ||
| 48 | + | ||
| 49 | + sprintf(msg, "%s> %s", Client[id].uid, buf); | ||
| 50 | +#ifdef DEBUG | ||
| 51 | + printf("%s", msg); | ||
| 52 | + fflush(stdout); | ||
| 53 | +#endif | ||
| 54 | + | ||
| 55 | + pthread_mutex_lock(&Mutex); | ||
| 56 | + for (i = 0 ; i < MAX_CLIENT ; i++) { | ||
| 57 | + if (Client[i].inUse && (i != id)) { | ||
| 58 | + if (send(Client[i].sockfd, msg, strlen(msg)+1, 0) < 0) { | ||
| 59 | + perror("send"); | ||
| 60 | + exit(1); | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + pthread_mutex_unlock(&Mutex); | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | + | ||
| 68 | +void | ||
| 69 | +ProcessClient(int id) | ||
| 70 | +{ | ||
| 71 | + char buf[MAX_BUF]; | ||
| 72 | + int n; | ||
| 73 | + | ||
| 74 | + if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) { | ||
| 75 | + perror("pthread_setcancelstate"); | ||
| 76 | + exit(1); | ||
| 77 | + } | ||
| 78 | + if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) { | ||
| 79 | + perror("pthread_setcanceltype"); | ||
| 80 | + exit(1); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + if ((n = recv(Client[id].sockfd, Client[id].uid, MAX_ID, 0)) < 0) { | ||
| 84 | + perror("recv"); | ||
| 85 | + exit(1); | ||
| 86 | + } | ||
| 87 | + printf("Client %d log-in(ID: %s).....\n", id, Client[id].uid); | ||
| 88 | + | ||
| 89 | + while (1) { | ||
| 90 | + if ((n = recv(Client[id].sockfd, buf, MAX_BUF, 0)) < 0) { | ||
| 91 | + perror("recv"); | ||
| 92 | + exit(1); | ||
| 93 | + } | ||
| 94 | + if (n == 0) { | ||
| 95 | + printf("Client %d log-out(ID: %s).....\n", id, Client[id].uid); | ||
| 96 | + | ||
| 97 | + pthread_mutex_lock(&Mutex); | ||
| 98 | + close(Client[id].sockfd); | ||
| 99 | + Client[id].inUse = 0; | ||
| 100 | + pthread_mutex_unlock(&Mutex); | ||
| 101 | + | ||
| 102 | + strcpy(buf, "log-out.....\n"); | ||
| 103 | + SendToOtherClients(id, buf); | ||
| 104 | + | ||
| 105 | + pthread_exit(NULL); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + SendToOtherClients(id, buf); | ||
| 109 | + } | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | + | ||
| 113 | +void | ||
| 114 | +CloseServer(int signo) | ||
| 115 | +{ | ||
| 116 | + int i; | ||
| 117 | + | ||
| 118 | + close(Sockfd); | ||
| 119 | + | ||
| 120 | + for (i = 0 ; i < MAX_CLIENT ; i++) { | ||
| 121 | + if (Client[i].inUse) { | ||
| 122 | + if (pthread_cancel(Client[i].tid)) { | ||
| 123 | + perror("pthread_cancel"); | ||
| 124 | + exit(1); | ||
| 125 | + } | ||
| 126 | + if (pthread_join(Client[i].tid, NULL)) { | ||
| 127 | + perror("pthread_join"); | ||
| 128 | + exit(1); | ||
| 129 | + } | ||
| 130 | + close(Client[i].sockfd); | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + if (pthread_mutex_destroy(&Mutex) < 0) { | ||
| 134 | + perror("pthread_mutex_destroy"); | ||
| 135 | + exit(1); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + printf("\nChat server terminated.....\n"); | ||
| 139 | + | ||
| 140 | + exit(0); | ||
| 141 | +} | ||
| 142 | + | ||
| 143 | +main(int argc, char *argv[]) | ||
| 144 | +{ | ||
| 145 | + int newSockfd, cliAddrLen, id, one = 1; | ||
| 146 | + struct sockaddr_in cliAddr, servAddr; | ||
| 147 | + | ||
| 148 | + signal(SIGINT, CloseServer); | ||
| 149 | + if (pthread_mutex_init(&Mutex, NULL) < 0) { | ||
| 150 | + perror("pthread_mutex_init"); | ||
| 151 | + exit(1); | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + if ((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { | ||
| 155 | + perror("socket"); | ||
| 156 | + exit(1); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + if (setsockopt(Sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { | ||
| 160 | + perror("setsockopt"); | ||
| 161 | + exit(1); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + bzero((char *)&servAddr, sizeof(servAddr)); | ||
| 165 | + servAddr.sin_family = PF_INET; | ||
| 166 | + servAddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
| 167 | + servAddr.sin_port = htons(SERV_TCP_PORT); | ||
| 168 | + | ||
| 169 | + if (bind(Sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { | ||
| 170 | + perror("bind"); | ||
| 171 | + exit(1); | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + listen(Sockfd, 5); | ||
| 175 | + | ||
| 176 | + printf("Chat server started.....\n"); | ||
| 177 | + | ||
| 178 | + cliAddrLen = sizeof(cliAddr); | ||
| 179 | + while (1) { | ||
| 180 | + newSockfd = accept(Sockfd, (struct sockaddr *) &cliAddr, &cliAddrLen); | ||
| 181 | + if (newSockfd < 0) { | ||
| 182 | + perror("accept"); | ||
| 183 | + exit(1); | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + id = GetID(); | ||
| 187 | + Client[id].sockfd = newSockfd; | ||
| 188 | + | ||
| 189 | + if (pthread_create(&Client[id].tid, NULL, (void *)ProcessClient, (void *)id) < 0) { | ||
| 190 | + perror("pthread_create"); | ||
| 191 | + exit(1); | ||
| 192 | + } | ||
| 193 | + } | ||
| 194 | +} |
-
Please register or login to post a comment