diff --git a/assets/lorem b/assets/lorem new file mode 100644 index 0000000..6ddc2fe --- /dev/null +++ b/assets/lorem @@ -0,0 +1,19 @@ +Le passage de Lorem Ipsum standard, utilisé depuis 1500 + +"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." + +Section 1.10.32 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.) + +"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" + +Traduction de H. Rackham (1914) + +"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" + +Section 1.10.33 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.) + +"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat." + +Traduction de H. Rackham (1914) + +"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains." diff --git a/assets/test1 b/assets/test1 new file mode 100644 index 0000000..ca72a04 --- /dev/null +++ b/assets/test1 @@ -0,0 +1,3 @@ +server: client 1 just arrived +client 1: coucou +server: client 1 just left diff --git a/assets/test2 b/assets/test2 new file mode 100644 index 0000000..a36e731 --- /dev/null +++ b/assets/test2 @@ -0,0 +1,25 @@ +server: client 1 just arrived +client 1: coucou +server: client 1 just left +server: client 2 just arrived +client 2: good morning +server: client 2 just left +server: client 3 just arrived +client 3: holà que tal? +server: client 3 just left +server: client 4 just arrived +client 4: ␛[32mGREEN +client 4: ␛[31mRED +server: client 4 just left +server: client 5 just arrived +client 5: +server: client 5 just left +server: client 6 just arrived +client 6: ␛[0mhello bonjour +server: client 6 just left +server: client 7 just arrived +client 7: last one +server: client 7 just left +server: client 8 just arrived +client 8: ✅ +server: client 8 just left diff --git a/assets/test3 b/assets/test3 new file mode 100644 index 0000000..eb2c927 --- /dev/null +++ b/assets/test3 @@ -0,0 +1,21 @@ +server: client 1 just arrived +client 1: Le passage de Lorem Ipsum standard, utilisé depuis 1500 +client 1: +client 1: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +client 1: +client 1: Section 1.10.32 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.) +client 1: +client 1: "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" +client 1: +client 1: Traduction de H. Rackham (1914) +client 1: +client 1: "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" +client 1: +client 1: Section 1.10.33 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.) +client 1: +client 1: "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat." +client 1: +client 1: Traduction de H. Rackham (1914) +client 1: +client 1: "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains." +server: client 1 just left diff --git a/assets/test4 b/assets/test4 new file mode 100644 index 0000000..54edfda --- /dev/null +++ b/assets/test4 @@ -0,0 +1,4 @@ +server: client 1 just arrived +server: client 2 just arrived +client 2: hello +server: client 2 just left diff --git a/assets/test5 b/assets/test5 new file mode 100644 index 0000000..45f9a10 --- /dev/null +++ b/assets/test5 @@ -0,0 +1,3 @@ +server: client 2 just arrived +client 2: hello +server: client 2 just left diff --git a/assets/test6 b/assets/test6 new file mode 100644 index 0000000..5d88274 --- /dev/null +++ b/assets/test6 @@ -0,0 +1,22 @@ +server: client 1 just arrived +server: client 2 just arrived +client 2: Le passage de Lorem Ipsum standard, utilisé depuis 1500 +client 2: +client 2: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +client 2: +client 2: Section 1.10.32 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.) +client 2: +client 2: "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" +client 2: +client 2: Traduction de H. Rackham (1914) +client 2: +client 2: "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" +client 2: +client 2: Section 1.10.33 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.) +client 2: +client 2: "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat." +client 2: +client 2: Traduction de H. Rackham (1914) +client 2: +client 2: "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains." +server: client 2 just left diff --git a/assets/test7 b/assets/test7 new file mode 100644 index 0000000..afc8099 --- /dev/null +++ b/assets/test7 @@ -0,0 +1,21 @@ +server: client 2 just arrived +client 2: Le passage de Lorem Ipsum standard, utilisé depuis 1500 +client 2: +client 2: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +client 2: +client 2: Section 1.10.32 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.) +client 2: +client 2: "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" +client 2: +client 2: Traduction de H. Rackham (1914) +client 2: +client 2: "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" +client 2: +client 2: Section 1.10.33 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.) +client 2: +client 2: "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat." +client 2: +client 2: Traduction de H. Rackham (1914) +client 2: +client 2: "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains." +server: client 2 just left diff --git a/grademe.sh b/grademe.sh new file mode 100644 index 0000000..d924d2c --- /dev/null +++ b/grademe.sh @@ -0,0 +1,151 @@ +#!/bin/bash + +ARGS="Wrong number of arguments" +FATAL="Fatal error" +err=0 +port=0 +while [ $port -lt 1024 -o $port -gt 10000 ]; do + port=$RANDOM +done +echo "Running tests on port $port..." + +check() +{ + # check exit status of most recently executed command + # [ $? -eq 1 ] && echo -e "✅\c" || { echo -e "❌\c"; err=$(( $err + 1 )); } + if [ $? -eq 1 ] + then + echo -e "✅\c"; + else + echo -e "❌\c"; + err=$(( $err + 1 )); + fi + + # compare output of cat with first argument + # [ "$(cat log.txt)" = "$1" ] && echo -e "\033[70G✅" || { echo -e "\033[70G❌"; err=$(( $err + 1 )); } + if [ "$(cat log.txt)" = "$1" ] + then + echo -e "\033[70G✅"; + else + echo -e "\033[70G❌"; + err=$(( $err + 1 )); + fi +} + + +check_diff() +{ +INDENT=50 +echo "coucou" + +case "$1" in + 5 | 7) + INDENT=55 + ;; +esac + +diff "log$1.txt" "assets/test$1" &> /dev/null +[ $? -eq 0 ] && echo -e "\033[${INDENT}G✅\c" || { echo -e "\033[${INDENT}G❌\c"; err=$(( $err + 1 )); } +} + + +### COMPIL +echo -e "\033[36m[+] Checking compilation...\033[0m\033[50G\c" +clang -Wall -Wextra -Werror mini_serv.c -o mini_serv &> log.txt +[ "$(cat log.txt)" = "" ] && echo "✅"|| { echo -e "\033[31mCompilation error...\033[0m"; cat log.txt; rm log.txt; exit 1; } + + +### HANDLE ERRORS +echo -e "\n\033[36m[+] Checking errors\033[0m\033[50GExit status\033[70GDiff Output" +echo -e "\033[33m[+] Checking no arguments output...\033[0m\033[50G\c" +./mini_serv &> log.txt +check "$ARGS" + +echo -e "\033[33m[+] Checking two many arguments output...\033[0m\033[50G\c" +./mini_serv 1 2 &> log.txt +check "$ARGS" + +echo -e "\033[33m[+] Checking syscall error output...\033[0m\033[50G\c" +./mini_serv "$port" & > log.txt +./mini_serv "$port" &> log.txt +check "$FATAL" +echo "[+] Killing mini_serv..." +killall -q mini_serv + + +### DIFF OUTPUT +echo -e "\n\033[36m[+] Checking output...\033[0m\033[50G" +echo -e "\033[33m[+] 1 client and 1 message...\033[0m\033[50G\c" +./mini_serv "$port" & > log1.txt +r=$(nc localhost "$port" > log1.txt) & +echo "coucou" | nc localhost "$port" +check_diff 1 +echo -e "\n[+] Killing mini_serv..." +killall -q mini_serv + + +echo -e "\n\033[33m[+] 1 client and many messages...\033[0m\033[50G\c" +port=$(( $port - 5 )) +./mini_serv "$port" & > log2.txt +r=$(nc localhost "$port" > log2.txt) & +sleep 1 +echo "coucou" | nc localhost "$port" +echo "good morning" | nc localhost "$port" +echo "holà que tal?" | nc localhost "$port" +echo -e "\033[32mGREEN\n\t\033[31mRED" | nc localhost "$port" +echo "" | nc localhost "$port" +echo -e "\033[0mhello\t\tbonjour" | nc localhost "$port" +echo "last one" | nc localhost "$port" +echo "✅" | nc localhost "$port" +check_diff 2 +echo -e "\n[+] Killing mini_serv..." +killall -q mini_serv + + +echo -e "\n\033[33m[+] 1 client and multi lines...\033[0m\033[50G\c" +./mini_serv "$(( $port + 1 ))" & > log3.txt +r=$(nc localhost "$(( $port + 1 ))" > log3.txt) & +sleep 1 +cat assets/lorem | nc localhost "$(( $port + 1 ))" +check_diff 3 +echo -e "\n[+] Killing mini_serv..." +killall -q mini_serv + + +echo -e "\n\033[33m[+] 2 clients and a simple message...\033[0m\033[50G\c" +./mini_serv "$(( $port + 2 ))" & > log4.txt +r=$(nc localhost "$(( $port + 2 ))" > log4.txt) & +sleep 1 +r2=$(nc localhost "$(( $port + 2 ))" > log5.txt) & +sleep 1 +echo "hello" | nc localhost "$(( $port + 2 ))" +check_diff 4 +check_diff 5 +echo -e "\n[+] Killing mini_serv..." +killall -q mini_serv + + +echo -e "\n\033[33m[+] 2 clients and multi ligne message...\033[0m\033[50G\c" +./mini_serv "$(( $port - 1 ))" & > log6.txt +r=$(nc localhost "$(( $port - 1 ))" > log6.txt) & +sleep 1 +r2=$(nc localhost "$(( $port - 1 ))" > log7.txt) & +sleep 1 +cat assets/lorem | nc localhost "$(( $port - 1 ))" +check_diff 6 +check_diff 7 +echo -e "\n[+] Killing mini_serv..." +killall -q mini_serv + + + +### SUMMARY +[ $err -eq 0 ] && echo -e "\n\033[32mAll tests passed :)\033[0m" || echo -e "\033[31m$err test(s) failed :(\033[0m" + + +### CLEAN + +rm -rf mini_serv log*.txt + +exit $err + diff --git a/log.txt b/log.txt new file mode 100644 index 0000000..39a2542 --- /dev/null +++ b/log.txt @@ -0,0 +1,61 @@ +Running tests on port 4200... +server: client 1 just arrived +server: client 1 just left +server: client 2 just arrived +server: client 2 just left +server: client 3 just arrived +server: client 3 just left +server: client 4 just arrived +server: client 4 just left +server: client 5 just arrived +server: client 5 just left +server: client 6 just arrived +server: client 6 just left +server: client 7 just arrived +server: client 7 just left +server: client 8 just arrived +server: client 8 just left +server: client 9 just arrived +server: client 9 just left +server: client 10 just arrived +server: client 10 just left +server: client 11 just arrived +server: client 11 just left +server: client 12 just arrived +server: client 12 just left +server: client 13 just arrived +server: client 13 just left +server: client 14 just arrived +server: client 14 just left +server: client 15 just arrived +server: client 15 just left +server: client 16 just arrived +server: client 16 just left +server: client 17 just arrived +server: client 17 just left +server: client 18 just arrived +server: client 18 just left +server: client 19 just arrived +server: client 19 just left +server: client 20 just arrived +server: client 20 just left +server: client 21 just arrived +server: client 21 just left +server: client 22 just arrived +server: client 22 just left +server: client 23 just arrived +server: client 23 just left +server: client 24 just arrived +server: client 24 just left +server: client 25 just arrived +server: client 25 just left +server: client 26 just arrived +server: client 26 just left +server: client 27 just arrived +server: client 27 just left +server: client 28 just arrived +server: client 28 just left +server: client 29 just arrived +server: client 29 just left +server: client 30 just arrived +server: client 30 just left diff --git a/mini_serv b/mini_serv new file mode 100755 index 0000000..cb9cf44 Binary files /dev/null and b/mini_serv differ diff --git a/mini_serv_01.c b/mini_serv_01.c index 90c5bbd..3fa631f 100644 --- a/mini_serv_01.c +++ b/mini_serv_01.c @@ -3,28 +3,39 @@ #include #include #include -#include +#include //sprintf + #define BUFSIZE 42000 typedef struct s_client { int id; char msg[1024]; -} t_client; +} t_client; -// GLOBALS -int server_fd; -int id; -int max_fd; -socklen_t addr_len; -fd_set fdset; -t_client clients[FD_SETSIZE]; - -void error(char *msg) { - write(2, msg, strlen(msg)); +void error(char *str) { + write(2, str, strlen(str)); exit(1); } +int init_socket(struct sockaddr_in *addr, int len, int port) { + int server_fd; + + if ( (server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + error("Fatal error2\n"); + bzero(addr, len); + addr->sin_family = AF_INET; + addr->sin_addr.s_addr = htonl(2130706433); //127.0.0.1 + addr->sin_port = htons(port); + + if (bind(server_fd, (const struct sockaddr *)addr, len) == -1) + error("Fatal error3\n"); + if (listen(server_fd, 10) == -1) + error("Fatal error4\n"); + + return server_fd; +} + void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) { for(int i = 0; i <= max_fd; ++i) { if (i == server_fd) @@ -36,89 +47,87 @@ void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) } } -void handle_socket(struct sockaddr *addr) { - fd_set rdset; - int client_fd; - int ret; - t_client client; - char buf[BUFSIZE]; - char msg[BUFSIZE]; - - rdset = fdset; - select(max_fd + 1, &rdset, NULL, NULL, NULL); - - if (FD_ISSET(server_fd, &rdset)) { - client_fd = accept(server_fd, addr, &addr_len); - FD_SET(client_fd, &fdset); - if (client_fd > max_fd) - max_fd = client_fd; - client = clients[client_fd]; - client.id = id; - bzero(client.msg, strlen(client.msg)); - sprintf(buf, "server: client %d arrived\n", id); - } - - client_fd = 0; - while(client_fd < max_fd) { - if (FD_ISSET(client_fd, &rdset)) { - bzero(buf, BUFSIZE); - ret = recv(client_fd, buf, BUFSIZE, 0); - if (ret == 0) { - sprintf(msg, "server: client %d just left\n", clients[client_fd].id); - broadcast(msg, &fdset, max_fd, server_fd, client_fd); - FD_CLR(client_fd, &fdset); - } - else if (ret > 0) { - - client = clients[client_fd]; - for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) { - client.msg[j] = buf[i]; - if (buf[i] == '\n') { - client.msg[j] = '\0'; - sprintf(msg, "client %d: %s\n", client.id, client.msg); - broadcast(msg, &fdset, max_fd, server_fd, client_fd); - bzero(client.msg, strlen(client.msg)); - j = -1; - } - } - - } - } - client_fd++; - } -} - int main(int ac, char **av) { - struct sockaddr_in addr; + int server_fd; + int client_fd; + int client_id; + int port; + int max_fd; + int ret; + t_client clients[FD_SETSIZE]; + t_client client; + + char buf[BUFSIZE]; + char msg[BUFSIZE]; + + struct sockaddr_in addr; + socklen_t addr_len; + fd_set fdset; + fd_set rdset; if (ac != 2) - error("Fatal error\n"); - port = atoi(av[1]); + error("Wrong number of arguments\n"); + if ( (port = atoi(av[1])) == -1) + error("Fatal error1\n"); addr_len = sizeof(addr); - // socket create and verification - if ( (server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) - error("Fatal error\n"); - bzero(&addr, addr_len); - - // assign IP, PORT - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1 - addr.sin_port = htons(port); - - // Binding newly created socket to given IP and verification - if (bind(server_fd, (const struct sockaddr *)&addr, addr_len) == -1) - error("Fatal error\n"); - if (listen(server_fd, 10) == -1) - error("Fatal error\n"); + server_fd = init_socket(&addr, addr_len, port); FD_ZERO(&fdset); FD_SET(server_fd, &fdset); max_fd = server_fd; + client_id = 0; + bzero(&clients, sizeof(clients)); - while(1) - handle_socket((struct sockaddr *)&addr); + while (1) { + rdset = fdset; + + select(max_fd + 1, &rdset, NULL, NULL, NULL); + + // new connection + if (FD_ISSET(server_fd, &rdset)) { + client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); + FD_SET(client_fd, &fdset); + clients[client_fd].id = client_id; + if (client_fd > max_fd) + max_fd = client_fd; + sprintf(msg, "server: client %d just arrived\n", client_id); + broadcast(msg, &fdset, max_fd, server_fd, client_fd); + client_id++; + } + + // new message + client_fd = 0; + while(client_fd <= max_fd) { + ret = 1; + if (FD_ISSET(client_fd, &rdset)) { + bzero(buf, BUFSIZE); + ret = recv(client_fd, buf, BUFSIZE, 0); + if (ret == 0) { + sprintf(msg, "server: client %d just left\n", clients[client_fd].id); + broadcast(msg, &fdset, max_fd, server_fd, client_fd); + FD_CLR(client_fd, &fdset); + } + else if (ret > 0) { + + client = clients[client_fd]; + for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) { + client.msg[j] = buf[i]; + if (buf[i] == '\n') { + client.msg[j] = '\0'; + sprintf(msg, "client %d: %s\n", client.id, client.msg); + broadcast(msg, &fdset, max_fd, server_fd, client_fd); + bzero(client.msg, strlen(client.msg)); + j = -1; + } + } + + } + } + client_fd++; + } + } return (0); } diff --git a/mini_serv_02.c b/mini_serv_02.c index 0e4d10e..90c5bbd 100644 --- a/mini_serv_02.c +++ b/mini_serv_02.c @@ -5,56 +5,102 @@ #include #include -#define BUFSIZE 160000 -#define MSGSIZE 1024 +#define BUFSIZE 42000 typedef struct s_client { int id; - char msg[MSGSIZE]; -} t_client; + char msg[1024]; +} t_client; + +// GLOBALS +int server_fd; +int id; +int max_fd; +socklen_t addr_len; +fd_set fdset; +t_client clients[FD_SETSIZE]; void error(char *msg) { - write(1, msg, strlen(msg)); + write(2, msg, strlen(msg)); exit(1); } -void broadcast(fd_set *set, int max_fd, char *msg, int server_fd, int current_fd) { - for(int i = 0; i < max_fd; ++i) { +void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) { + for(int i = 0; i <= max_fd; ++i) { if (i == server_fd) continue; - if (i == current_fd) + if (i == sender_fd) continue; if (FD_ISSET(i, set)) - send(i, msg, strlen(msg), 0); + send(i, buf, strlen(buf), 0); + } +} + +void handle_socket(struct sockaddr *addr) { + fd_set rdset; + int client_fd; + int ret; + t_client client; + char buf[BUFSIZE]; + char msg[BUFSIZE]; + + rdset = fdset; + select(max_fd + 1, &rdset, NULL, NULL, NULL); + + if (FD_ISSET(server_fd, &rdset)) { + client_fd = accept(server_fd, addr, &addr_len); + FD_SET(client_fd, &fdset); + if (client_fd > max_fd) + max_fd = client_fd; + client = clients[client_fd]; + client.id = id; + bzero(client.msg, strlen(client.msg)); + sprintf(buf, "server: client %d arrived\n", id); + } + + client_fd = 0; + while(client_fd < max_fd) { + if (FD_ISSET(client_fd, &rdset)) { + bzero(buf, BUFSIZE); + ret = recv(client_fd, buf, BUFSIZE, 0); + if (ret == 0) { + sprintf(msg, "server: client %d just left\n", clients[client_fd].id); + broadcast(msg, &fdset, max_fd, server_fd, client_fd); + FD_CLR(client_fd, &fdset); + } + else if (ret > 0) { + + client = clients[client_fd]; + for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) { + client.msg[j] = buf[i]; + if (buf[i] == '\n') { + client.msg[j] = '\0'; + sprintf(msg, "client %d: %s\n", client.id, client.msg); + broadcast(msg, &fdset, max_fd, server_fd, client_fd); + bzero(client.msg, strlen(client.msg)); + j = -1; + } + } + + } + } + client_fd++; } } int main(int ac, char **av) { - int server_fd; - int client_fd; - int id; - int port; - int max_fd; - int ret; struct sockaddr_in addr; - socklen_t addr_len; - fd_set fdset; - fd_set rdset; - t_client clients[FD_SETSIZE]; - t_client client; - char buf[BUFSIZE]; - char msg[BUFSIZE]; + int port; if (ac != 2) - error("Wrong number of arguments\n"); - if ( (port = atoi(av[1])) == -1) error("Fatal error\n"); + port = atoi(av[1]); addr_len = sizeof(addr); - // socket create and verification - server_fd = socket(AF_INET, SOCK_STREAM, 0); - if (server_fd == -1) + // socket create and verification + if ( (server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) error("Fatal error\n"); + bzero(&addr, addr_len); // assign IP, PORT addr.sin_family = AF_INET; @@ -62,7 +108,7 @@ int main(int ac, char **av) { addr.sin_port = htons(port); // Binding newly created socket to given IP and verification - if ((bind(server_fd, (const struct sockaddr *)&addr, addr_len)) == -1) + if (bind(server_fd, (const struct sockaddr *)&addr, addr_len) == -1) error("Fatal error\n"); if (listen(server_fd, 10) == -1) error("Fatal error\n"); @@ -70,59 +116,10 @@ int main(int ac, char **av) { FD_ZERO(&fdset); FD_SET(server_fd, &fdset); max_fd = server_fd; - id = 0; - bzero(&clients, sizeof(clients)); - while(1) { - rdset = fdset; + while(1) + handle_socket((struct sockaddr *)&addr); - select(max_fd + 1, &rdset, NULL, NULL, NULL); - - if (FD_ISSET(server_fd, &rdset)) { - client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); - FD_SET(client_fd, &fdset); - if (client_fd > max_fd) - max_fd = client_fd; - client = clients[client_fd]; - client.id = id; - bzero(client.msg, MSGSIZE); - sprintf(msg, "server: client %d arrived\n", client.id); - broadcast(&fdset, max_fd, msg, server_fd, client_fd); - id++; - } - - client_fd = 0; - while(client_fd <= max_fd) { - ret = 1; - if (FD_ISSET(client_fd, &rdset)) { - - client = clients[client_fd]; - bzero(buf, BUFSIZE); - ret = recv(client_fd, buf, BUFSIZE, 0); - - if (ret == 0) { - sprintf(msg, "server: client %d left\n", client.id); - broadcast(&fdset, max_fd, msg, server_fd, client_fd); - FD_CLR(client_fd, &fdset); - } - - else if (ret > 0) { - for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) { - client.msg[j] = buf[i]; - if (buf[i] == '\n') { - client.msg[j] = '\0'; - sprintf(msg, "client %d: %s\n", client.id, client.msg); - broadcast(&fdset, max_fd, msg, server_fd, client_fd); - bzero(client.msg, MSGSIZE); - j = -1; - } - } - } - } - client_fd++; - } - } return (0); } - diff --git a/mini_serv_03.c b/mini_serv_03.c new file mode 100644 index 0000000..0e4d10e --- /dev/null +++ b/mini_serv_03.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include + +#define BUFSIZE 160000 +#define MSGSIZE 1024 + +typedef struct s_client { + int id; + char msg[MSGSIZE]; +} t_client; + +void error(char *msg) { + write(1, msg, strlen(msg)); + exit(1); +} + +void broadcast(fd_set *set, int max_fd, char *msg, int server_fd, int current_fd) { + for(int i = 0; i < max_fd; ++i) { + if (i == server_fd) + continue; + if (i == current_fd) + continue; + if (FD_ISSET(i, set)) + send(i, msg, strlen(msg), 0); + } +} + +int main(int ac, char **av) { + int server_fd; + int client_fd; + int id; + int port; + int max_fd; + int ret; + struct sockaddr_in addr; + socklen_t addr_len; + fd_set fdset; + fd_set rdset; + t_client clients[FD_SETSIZE]; + t_client client; + char buf[BUFSIZE]; + char msg[BUFSIZE]; + + if (ac != 2) + error("Wrong number of arguments\n"); + if ( (port = atoi(av[1])) == -1) + error("Fatal error\n"); + addr_len = sizeof(addr); + + // socket create and verification + server_fd = socket(AF_INET, SOCK_STREAM, 0); + if (server_fd == -1) + error("Fatal error\n"); + + // assign IP, PORT + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1 + addr.sin_port = htons(port); + + // Binding newly created socket to given IP and verification + if ((bind(server_fd, (const struct sockaddr *)&addr, addr_len)) == -1) + error("Fatal error\n"); + if (listen(server_fd, 10) == -1) + error("Fatal error\n"); + + FD_ZERO(&fdset); + FD_SET(server_fd, &fdset); + max_fd = server_fd; + id = 0; + bzero(&clients, sizeof(clients)); + + while(1) { + rdset = fdset; + + select(max_fd + 1, &rdset, NULL, NULL, NULL); + + if (FD_ISSET(server_fd, &rdset)) { + client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); + FD_SET(client_fd, &fdset); + if (client_fd > max_fd) + max_fd = client_fd; + client = clients[client_fd]; + client.id = id; + bzero(client.msg, MSGSIZE); + sprintf(msg, "server: client %d arrived\n", client.id); + broadcast(&fdset, max_fd, msg, server_fd, client_fd); + id++; + } + + client_fd = 0; + while(client_fd <= max_fd) { + ret = 1; + if (FD_ISSET(client_fd, &rdset)) { + + client = clients[client_fd]; + bzero(buf, BUFSIZE); + ret = recv(client_fd, buf, BUFSIZE, 0); + + if (ret == 0) { + sprintf(msg, "server: client %d left\n", client.id); + broadcast(&fdset, max_fd, msg, server_fd, client_fd); + FD_CLR(client_fd, &fdset); + } + + else if (ret > 0) { + for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) { + client.msg[j] = buf[i]; + if (buf[i] == '\n') { + client.msg[j] = '\0'; + sprintf(msg, "client %d: %s\n", client.id, client.msg); + broadcast(&fdset, max_fd, msg, server_fd, client_fd); + bzero(client.msg, MSGSIZE); + j = -1; + } + } + } + } + client_fd++; + } + } + return (0); +} + + diff --git a/nc b/nc deleted file mode 100644 index 65bb659..0000000 Binary files a/nc and /dev/null differ diff --git a/server.py b/server.py deleted file mode 100644 index 74f8bfb..0000000 --- a/server.py +++ /dev/null @@ -1,22 +0,0 @@ - -import socket - -host = 'localhost' -port = 9999 -address = (host, port) - -server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -server_socket.bind((address)) -server_socket.listen(5) -print ("Listening for client . . .") -conn, address = server_socket.accept() -print ("Connected to client at ", address) -while True: - try: - output = conn.recv(2048) - if output: - print ("Message received from client:") - print (output) - - except: - sys.exit(0) diff --git a/tester.sh b/tester.sh new file mode 100644 index 0000000..51b832a --- /dev/null +++ b/tester.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +port=0 +while [ $port -lt 1024 -o $port -gt 10000 ]; do + port=$RANDOM +done +echo "Running tests on port $port..." + +clang -Wall -Wextra -Werror mini_serv.c -o mini_serv + +./mini_serv "$port" & +nc localhost "$port" & + +for i in {1..30} +do + nc localhost "$port" & + kill -KILL $(pidof nc | tr ' ' '\n' | head -n1) +done + +killall -q mini_serv nc +