# Arabic translation of manpages # This file is distributed under the same license as the manpages-l10n package. # Copyright © of this file: # زايد السعيدي , 2026. msgid "" msgstr "" "Project-Id-Version: manpages-l10n\n" "POT-Creation-Date: 2026-06-05 06:42+0200\n" "PO-Revision-Date: 2026-06-03 04:49+0400\n" "Last-Translator: Zayed Al-Saidi \n" "Language-Team: Arabic \n" "Language: ar\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" ".MIME-Version: 1.0\n" "Plural-Forms: nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 " "&& n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5;\n" "X-Generator: Lokalize 23.08.5\n" #. type: TH #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "SELECT_TUT" msgstr "SELECT_TUT" #. type: TH #: archlinux debian-unstable mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed #, no-wrap msgid "2026-02-08" msgstr "8 فبراير 2026" #. type: TH #: archlinux debian-unstable #, no-wrap msgid "Linux man-pages 6.18" msgstr "صفحات دليل لينكس 6.18" #. type: SH #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "NAME" msgstr "الاسم" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "select, pselect - synchronous I/O multiplexing" msgstr "select, pselect - تعدد إدخال/إخراج متزامن" #. type: SH #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "LIBRARY" msgstr "المكتبة" #. type: Plain text #: archlinux debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "Standard C library (I,\\ I<-lc>)" msgstr "مكتبة سي المعيارية (I،\\ I<-lc>)" #. type: SH #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "SYNOPSIS" msgstr "موجز" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "See B(2)" #. type: SH #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "DESCRIPTION" msgstr "الوصف" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The B() وB() لمراقبة واصفات ملفات " "متعددة بكفاءة، لمعرفة ما إذا كان أي منها جاهزًا أو يصبح جاهزًا؛ أي لمعرفة ما " "إذا كان الإدخال/الإخراج ممكنًا، أو حدثت \"حالة استثنائية\" على أي من واصفات " "الملفات." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This page provides background and tutorial information on the use of these " "system calls. For details of the arguments and semantics of B(2)." msgstr "" "توفر هذه الصفحة معلومات أساسية وتعليمية حول استخدام استدعاءات النظام هذه. " "للحصول على تفاصيل الوسائط ودلالات B(2)." #. type: SS #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Combining signal and data events" msgstr "دمج أحداث الإشارات والبيانات" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "B() is useful if you are waiting for a signal as well as for file " "descriptor(s) to become ready for I/O. Programs that receive signals " "normally use the signal handler only to raise a global flag. The global " "flag will indicate that the event must be processed in the main loop of the " "program. A signal will cause the B() would block indefinitely." msgstr "" "يكون B() مفيدًا إذا كنت تنتظر إشارة بالإضافة إلى واصف (واصفات) ملف " "لتصبح جاهزة للإدخال/الإخراج. تستخدم البرامج التي تستقبل الإشارات عادةً معالج " "الإشارة فقط لرفع علامة عامة. ستشير العلامة العامة إلى أن الحدث يجب معالجته " "في الحلقة الرئيسة للبرنامج. ستتسبب الإشارة في عودة استدعاء B() إلى أجل " "غير مسمى." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Now, somewhere in the main loop will be a conditional to check the global " "flag. So we must ask: what if a signal arrives after the conditional, but " "before the B() would block " "indefinitely, even though an event is actually pending. This race condition " "is solved by the B() call. This call can be used to set the " "signal mask to a set of signals that are to be received only within the " "B() call. For instance, let us say that the event in question was " "the exit of a child process. Before the start of the main loop, we would " "block B using B(2). Our B() call would " "enable B by using an empty signal mask. Our program would look " "like:" msgstr "" "الآن، سيكون هناك شرط في مكان ما في الحلقة الرئيسة للتحقق من العلامة العامة. " "لذا يجب أن نسأل: ماذا لو وصلت إشارة بعد الشرط، ولكن قبل استدعاء B() سيحظر إلى أجل غير مسمى، على الرغم من أن هناك حدثًا " "معلقًا بالفعل. تُحل حالة السباق هذه بواسطة استدعاء B(). يمكن استخدام " "هذا الاستدعاء لضبط قناع الإشارة إلى مجموعة من الإشارات التي ستُستقبل فقط داخل " "استدعاء B(). على سبيل المثال، لنفترض أن الحدث المعني هو خروج عملية " "تابعة. قبل بدء الحلقة الرئيسة، سنحظر B باستخدام B(2). " "سيمكّن استدعاء B() الخاص بنا B باستخدام قناع إشارة فارغ. " "سيبدو برنامجنا كالتالي:" #. type: Plain text #: archlinux debian-unstable mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed #, no-wrap msgid "" "static volatile sig_atomic_t got_SIGCHLD = 0;\n" "\\&\n" "static void\n" "child_sig_handler(int sig)\n" "{\n" " got_SIGCHLD = 1;\n" "}\n" "\\&\n" "int\n" "main(int argc, char *argv[])\n" "{\n" " sigset_t sigmask, empty_mask;\n" " struct sigaction sa;\n" " fd_set readfds, writefds, exceptfds;\n" " int r;\n" "\\&\n" " sigemptyset(&sigmask);\n" " sigaddset(&sigmask, SIGCHLD);\n" " if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n" " perror(\"sigprocmask\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " sa.sa_flags = 0;\n" " sa.sa_handler = child_sig_handler;\n" " sigemptyset(&sa.sa_mask);\n" " if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n" " perror(\"sigaction\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " sigemptyset(&empty_mask);\n" "\\&\n" " for (;;) { /* main loop */\n" " /* Initialize readfds, writefds, and exceptfds\n" " before the pselect() call. (Code omitted.) */\n" "\\&\n" " r = pselect(nfds, &readfds, &writefds, &exceptfds,\n" " NULL, &empty_mask);\n" " if (r == -1 && errno != EINTR) {\n" " /* Handle error */\n" " }\n" "\\&\n" " if (got_SIGCHLD) {\n" " got_SIGCHLD = 0;\n" "\\&\n" " /* Handle signalled event here; e.g., wait() for all\n" " terminated children. (Code omitted.) */\n" " }\n" "\\&\n" " /* main body of program */\n" " }\n" "}\n" msgstr "" "static volatile sig_atomic_t got_SIGCHLD = 0;\n" "\\&\n" "static void\n" "child_sig_handler(int sig)\n" "{\n" " got_SIGCHLD = 1;\n" "}\n" "\\&\n" "int\n" "main(int argc, char *argv[])\n" "{\n" " sigset_t sigmask, empty_mask;\n" " struct sigaction sa;\n" " fd_set readfds, writefds, exceptfds;\n" " int r;\n" "\\&\n" " sigemptyset(&sigmask);\n" " sigaddset(&sigmask, SIGCHLD);\n" " if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n" " perror(\"sigprocmask\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " sa.sa_flags = 0;\n" " sa.sa_handler = child_sig_handler;\n" " sigemptyset(&sa.sa_mask);\n" " if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n" " perror(\"sigaction\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " sigemptyset(&empty_mask);\n" "\\&\n" " for (;;) { /* الحلقة الرئيسة */\n" " /* هيئ readfds و writefds و exceptfds\n" " قبل استدعاء pselect()‎. (الكود محذوف). */\n" "\\&\n" " r = pselect(nfds, &readfds, &writefds, &exceptfds,\n" " NULL, &empty_mask);\n" " if (r == -1 && errno != EINTR) {\n" " /* عالج الخطأ */\n" " }\n" "\\&\n" " if (got_SIGCHLD) {\n" " got_SIGCHLD = 0;\n" "\\&\n" " /* عالج الحدث المؤشر عليه هنا؛ مثلاً wait()‎ لكل\n" " الأبناء المنتهين. (الكود محذوف). */\n" " }\n" "\\&\n" " /* المتن الرئيس للبرنامج */\n" " }\n" "}\n" #. type: SS #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Practical" msgstr "عملي" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "So what is the point of B() is that it watches " "multiple descriptors at the same time and properly puts the process to sleep " "if there is no activity. UNIX programmers often find themselves in a " "position where they have to handle I/O from more than one file descriptor " "where the data flow may be intermittent. If you were to merely create a " "sequence of B(2) and B(2) calls, you would find that one of " "your calls may block waiting for data from/to a file descriptor, while " "another file descriptor is unused though ready for I/O. B()؟ ألا يمكنني ببساطة القراءة والكتابة إلى واصفات " "الملفات الخاصة بي متى شئت؟ الغاية من B() بكفاءة مع " "هذا الموقف." #. type: SS #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Select law" msgstr "قانون select" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Many people who try to use B(), so here is a list of essentials to watch for when using " "B() سلوكًا يصعب فهمه وينتج نتائج " "غير قابلة للنقل أو نتائج حدية. على سبيل المثال، البرنامج أعلاه مكتوب بعناية " "لعدم الحظر في أي نقطة، على الرغم من أنه لا يضبط واصفات الملفات الخاصة به على " "وضع عدم الحظر. من السهل إدخال أخطاء دقيقة تزيل ميزة استخدام B()." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "1." msgstr "1." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "You should always try to use B() بدون مهلة زمنية. يجب ألا يكون " "لبرنامجك ما يفعله إذا لم تكن هناك بيانات متاحة. الكود الذي يعتمد على المهل " "الزمنية ليس عادةً قابلاً للنقل ويصعب تنقيحه." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "2." msgstr "2." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The value I must be properly calculated for efficiency as explained " "above." msgstr "يجب حساب قيمة I بشكل صحيح لتحقيق الكفاءة كما هو موضح أعلاه." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "3." msgstr "3." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "No file descriptor must be added to any set if you do not intend to check " "its result after the B()، والاستجابة بشكل مناسب. انظر القاعدة التالية." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "4." msgstr "4." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "After B()، يجب فحص جميع واصفات الملفات في جميع المجموعات لمعرفة " "ما إذا كانت جاهزة." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "5." msgstr "5." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The functions B(2), B(2), B(2), and B(2) do I " "necessarily read/write the full amount of data that you have requested. If " "they do read/write the full amount, it's because you have a low traffic load " "and a fast stream. This is not always going to be the case. You should " "cope with the case of your functions managing to send or receive only a " "single byte." msgstr "" "الدوال B(2) وB(2) وB(2) وB(2) I<لا> تقرأ/تكتب " "بالضرورة كامل كمية البيانات التي طلبتها. إذا قرأت/كتبت كامل الكمية، فذلك لأن " "لديك حمولة مرور منخفضة وتدفقًا سريعًا. لن يكون هذا هو الحال دائمًا. يجب أن " "تتعامل مع حالة تمكن دوالك من إرسال أو استقبال بايت واحد فقط." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "6." msgstr "6." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Never read/write only in single bytes at a time unless you are really sure " "that you have a small amount of data to process. It is extremely " "inefficient not to read/write as much data as you can buffer each time. The " "buffers in the example below are 1024 bytes although they could easily be " "made larger." msgstr "" "لا تقرأ/تكتب أبدًا بايتات مفردة فقط في المرة الواحدة إلا إذا كنت متأكدًا حقًا " "من أن لديك كمية صغيرة من البيانات لمعالجتها. من غير الفعال للغاية عدم قراءة/" "كتابة أكبر قدر ممكن من البيانات التي يمكنك تخزينها مؤقتًا في كل مرة. المخازن " "المؤقتة في المثال أدناه هي 1024 بايت على الرغم من أنه يمكن جعلها أكبر بسهولة." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "7." msgstr "7." #. Nonetheless, you should still cope with these errors for completeness. #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Calls to B(2), B(2), B(2), B(2), and B() يمكن " "أن تفشل مع الخطأ B، واستدعاءات B(2) وB(2) وB(2) " "وB(2) يمكن أن تفشل مع ضبط I على B (B). يجب " "إدارة هذه النتائج بشكل صحيح (لم يُفعل ذلك بشكل صحيح أعلاه). إذا كان برنامجك " "لن يستقبل أي إشارات، فمن غير المرجح أن تحصل على B. إذا كان برنامجك لا " "يضبط الإدخال/الإخراج غير المحظور، فلن تحصل على B." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "8." msgstr "8." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Never call B(2), B(2), B(2), or B(2) with a buffer " "length of zero." msgstr "" "لا تستدعِ أبدًا B(2) أو B(2) أو B(2) أو B(2) بطول " "مخزن مؤقت صفري." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "9." msgstr "9." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "If the functions B(2), B(2), B(2), and B(2) fail " "with errors other than those listed in B<7.>, or one of the input functions " "returns 0, indicating end of file, then you should I pass that file " "descriptor to B() مجددًا. في " "المثال أدناه، أغلقتُ واصف الملف فورًا، ثم ضبطتُه على -1 لمنع إدراجه في مجموعة." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "10." msgstr "10." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The timeout value must be initialized with each new call to B()، لأن بعض " "أنظمة التشغيل تعدل البنية. ومع ذلك، فإن B() لا يعدل بنية المهلة " "الخاصة به." #. type: TP #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "11." msgstr "11." #. "I have heard" does not fill me with confidence, and doesn't #. belong in a man page, so I've commented this point out. #. .TP #. 11. #. I have heard that the Windows socket layer does not cope with OOB data #. properly. #. It also does not cope with #. .BR select () #. calls when no file descriptors are set at all. #. Having no file descriptors set is a useful #. way to sleep the process with subsecond precision by using the timeout. #. (See further on.) #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Since B() يعدل مجموعات واصفات الملفات الخاصة به، فإذا كان الاستدعاء " "يُستخدم في حلقة، فيجب إعادة تهيئة المجموعات قبل كل استدعاء." #. type: SH #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "RETURN VALUE" msgstr "قيمة الإرجاع" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "See B(2)." #. type: SH #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "NOTES" msgstr "ملاحظات" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Generally speaking, all operating systems that support sockets also support " "B() can be used to solve many problems in a portable " "and efficient way that naive programmers try to solve in a more complicated " "manner using threads, forking, IPCs, signals, memory sharing, and so on." msgstr "" "بشكل عام، تدعم جميع أنظمة التشغيل التي تدعم المقابس أيضًا B() لحل العديد من المشكلات بطريقة قابلة للنقل وفعالة يحاول " "المبرمجون المبتدئون حلها بطريقة أكثر تعقيدًا باستخدام الخيوط، والتفريع " "(forking)، والتواصل بين العمليات (IPCs)، والإشارات، ومشاركة الذاكرة، وما إلى " "ذلك." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The B(2) system call has the same functionality as B()." msgstr "" "استدعاء النظام B(2) له نفس وظيفة B()." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The Linux-specific B(7) API provides an interface that is more " "efficient than B(2) وB(2) عند مراقبة أعداد كبيرة من واصفات الملفات." #. type: SH #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "EXAMPLES" msgstr "أمثلة" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Here is an example that better demonstrates the true utility of " "B(). القائمة أدناه هي " "برنامج توجيه TCP يوجه من منفذ TCP إلى آخر." #. type: Plain text #: archlinux debian-unstable mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed #, no-wrap msgid "" "#include Earpa/inet.hE\n" "#include Eerrno.hE\n" "#include Enetinet/in.hE\n" "#include Esignal.hE\n" "#include Estdio.hE\n" "#include Estdlib.hE\n" "#include Estring.hE\n" "#include Esys/select.hE\n" "#include Esys/socket.hE\n" "#include Esys/types.hE\n" "#include Eunistd.hE\n" "\\&\n" "static int forward_port;\n" "\\&\n" "#undef max\n" "#define max(x, y) ((x) E (y) ? (x) : (y))\n" "\\&\n" "static int\n" "listen_socket(int listen_port)\n" "{\n" " int lfd;\n" " int yes;\n" " struct sockaddr_in addr;\n" "\\&\n" " lfd = socket(AF_INET, SOCK_STREAM, 0);\n" " if (lfd == -1) {\n" " perror(\"socket\");\n" " return -1;\n" " }\n" "\\&\n" " yes = 1;\n" " if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n" " &yes, sizeof(yes)) == -1)\n" " {\n" " perror(\"setsockopt\");\n" " close(lfd);\n" " return -1;\n" " }\n" "\\&\n" " memset(&addr, 0, sizeof(addr));\n" " addr.sin_port = htons(listen_port);\n" " addr.sin_family = AF_INET;\n" " if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" " perror(\"bind\");\n" " close(lfd);\n" " return -1;\n" " }\n" "\\&\n" " printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n" " listen(lfd, 10);\n" " return lfd;\n" "}\n" "\\&\n" "static int\n" "connect_socket(int connect_port, char *address)\n" "{\n" " int cfd;\n" " struct sockaddr_in addr;\n" "\\&\n" " cfd = socket(AF_INET, SOCK_STREAM, 0);\n" " if (cfd == -1) {\n" " perror(\"socket\");\n" " return -1;\n" " }\n" "\\&\n" " memset(&addr, 0, sizeof(addr));\n" " addr.sin_port = htons(connect_port);\n" " addr.sin_family = AF_INET;\n" "\\&\n" " if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n" " fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n" " close(cfd);\n" " return -1;\n" " }\n" "\\&\n" " if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" " perror(\"connect()\");\n" " shutdown(cfd, SHUT_RDWR);\n" " close(cfd);\n" " return -1;\n" " }\n" " return cfd;\n" "}\n" "\\&\n" "#define SHUT_FD1 do { \\[rs]\n" " if (fd1 E= 0) { \\[rs]\n" " shutdown(fd1, SHUT_RDWR); \\[rs]\n" " close(fd1); \\[rs]\n" " fd1 = -1; \\[rs]\n" " } \\[rs]\n" " } while (0)\n" "\\&\n" "#define SHUT_FD2 do { \\[rs]\n" " if (fd2 E= 0) { \\[rs]\n" " shutdown(fd2, SHUT_RDWR); \\[rs]\n" " close(fd2); \\[rs]\n" " fd2 = -1; \\[rs]\n" " } \\[rs]\n" " } while (0)\n" "\\&\n" "#define BUF_SIZE 1024\n" "\\&\n" "int\n" "main(int argc, char *argv[])\n" "{\n" " int h;\n" " int ready, nfds;\n" " int fd1 = -1, fd2 = -1;\n" " int buf1_avail = 0, buf1_written = 0;\n" " int buf2_avail = 0, buf2_written = 0;\n" " char buf1[BUF_SIZE], buf2[BUF_SIZE];\n" " fd_set readfds, writefds, exceptfds;\n" " ssize_t nbytes;\n" "\\&\n" " if (argc != 4) {\n" " fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd Elisten-portE \"\n" " \"Eforward-to-portE Eforward-to-ip-addressE\\[rs]n\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " signal(SIGPIPE, SIG_IGN);\n" "\\&\n" " forward_port = atoi(argv[2]);\n" "\\&\n" " h = listen_socket(atoi(argv[1]));\n" " if (h == -1)\n" " exit(EXIT_FAILURE);\n" "\\&\n" " for (;;) {\n" " nfds = 0;\n" "\\&\n" " FD_ZERO(&readfds);\n" " FD_ZERO(&writefds);\n" " FD_ZERO(&exceptfds);\n" " FD_SET(h, &readfds);\n" " nfds = max(nfds, h);\n" "\\&\n" " if (fd1 E 0 && buf1_avail E BUF_SIZE)\n" " FD_SET(fd1, &readfds);\n" " /* Note: nfds is updated below, when fd1 is added to\n" " exceptfds. */\n" " if (fd2 E 0 && buf2_avail E BUF_SIZE)\n" " FD_SET(fd2, &readfds);\n" "\\&\n" " if (fd1 E 0 && buf2_avail - buf2_written E 0)\n" " FD_SET(fd1, &writefds);\n" " if (fd2 E 0 && buf1_avail - buf1_written E 0)\n" " FD_SET(fd2, &writefds);\n" "\\&\n" " if (fd1 E 0) {\n" " FD_SET(fd1, &exceptfds);\n" " nfds = max(nfds, fd1);\n" " }\n" " if (fd2 E 0) {\n" " FD_SET(fd2, &exceptfds);\n" " nfds = max(nfds, fd2);\n" " }\n" "\\&\n" " ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n" "\\&\n" " if (ready == -1 && errno == EINTR)\n" " continue;\n" "\\&\n" " if (ready == -1) {\n" " perror(\"select()\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " if (FD_ISSET(h, &readfds)) {\n" " socklen_t addrlen;\n" " struct sockaddr_in client_addr;\n" " int fd;\n" "\\&\n" " addrlen = sizeof(client_addr);\n" " memset(&client_addr, 0, addrlen);\n" " fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n" " if (fd == -1) {\n" " perror(\"accept()\");\n" " } else {\n" " SHUT_FD1;\n" " SHUT_FD2;\n" " buf1_avail = buf1_written = 0;\n" " buf2_avail = buf2_written = 0;\n" " fd1 = fd;\n" " fd2 = connect_socket(forward_port, argv[3]);\n" " if (fd2 == -1)\n" " SHUT_FD1;\n" " else\n" " printf(\"connect from %s\\[rs]n\",\n" " inet_ntoa(client_addr.sin_addr));\n" "\\&\n" " /* Skip any events on the old, closed file\n" " descriptors. */\n" "\\&\n" " continue;\n" " }\n" " }\n" "\\&\n" " /* NB: read OOB data before normal reads. */\n" "\\&\n" " if (fd1 E 0 && FD_ISSET(fd1, &exceptfds)) {\n" " char c;\n" "\\&\n" " nbytes = recv(fd1, &c, 1, MSG_OOB);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " send(fd2, &c, 1, MSG_OOB);\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &exceptfds)) {\n" " char c;\n" "\\&\n" " nbytes = recv(fd2, &c, 1, MSG_OOB);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " send(fd1, &c, 1, MSG_OOB);\n" " }\n" " if (fd1 E 0 && FD_ISSET(fd1, &readfds)) {\n" " nbytes = read(fd1, buf1 + buf1_avail,\n" " BUF_SIZE - buf1_avail);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " buf1_avail += nbytes;\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &readfds)) {\n" " nbytes = read(fd2, buf2 + buf2_avail,\n" " BUF_SIZE - buf2_avail);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " buf2_avail += nbytes;\n" " }\n" " if (fd1 E 0 && FD_ISSET(fd1, &writefds) && buf2_avail E 0) {\n" " nbytes = write(fd1, buf2 + buf2_written,\n" " buf2_avail - buf2_written);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " buf2_written += nbytes;\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &writefds) && buf1_avail E 0) {\n" " nbytes = write(fd2, buf1 + buf1_written,\n" " buf1_avail - buf1_written);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " buf1_written += nbytes;\n" " }\n" "\\&\n" " /* Check if write data has caught read data. */\n" "\\&\n" " if (buf1_written == buf1_avail)\n" " buf1_written = buf1_avail = 0;\n" " if (buf2_written == buf2_avail)\n" " buf2_written = buf2_avail = 0;\n" "\\&\n" " /* One side has closed the connection, keep\n" " writing to the other side until empty. */\n" "\\&\n" " if (fd1 E 0 && buf1_avail - buf1_written == 0)\n" " SHUT_FD2;\n" " if (fd2 E 0 && buf2_avail - buf2_written == 0)\n" " SHUT_FD1;\n" " }\n" " exit(EXIT_SUCCESS);\n" "}\n" msgstr "" "#include Earpa/inet.hE\n" "#include Eerrno.hE\n" "#include Enetinet/in.hE\n" "#include Esignal.hE\n" "#include Estdio.hE\n" "#include Estdlib.hE\n" "#include Estring.hE\n" "#include Esys/select.hE\n" "#include Esys/socket.hE\n" "#include Esys/types.hE\n" "#include Eunistd.hE\n" "\\&\n" "static int forward_port;\n" "\\&\n" "#undef max\n" "#define max(x, y) ((x) E (y) ? (x) : (y))\n" "\\&\n" "static int\n" "listen_socket(int listen_port)\n" "{\n" " int lfd;\n" " int yes;\n" " struct sockaddr_in addr;\n" "\\&\n" " lfd = socket(AF_INET, SOCK_STREAM, 0);\n" " if (lfd == -1) {\n" " perror(\"socket\");\n" " return -1;\n" " }\n" "\\&\n" " yes = 1;\n" " if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n" " &yes, sizeof(yes)) == -1)\n" " {\n" " perror(\"setsockopt\");\n" " close(lfd);\n" " return -1;\n" " }\n" "\\&\n" " memset(&addr, 0, sizeof(addr));\n" " addr.sin_port = htons(listen_port);\n" " addr.sin_family = AF_INET;\n" " if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" " perror(\"bind\");\n" " close(lfd);\n" " return -1;\n" " }\n" "\\&\n" " printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n" " listen(lfd, 10);\n" " return lfd;\n" "}\n" "\\&\n" "static int\n" "connect_socket(int connect_port, char *address)\n" "{\n" " int cfd;\n" " struct sockaddr_in addr;\n" "\\&\n" " cfd = socket(AF_INET, SOCK_STREAM, 0);\n" " if (cfd == -1) {\n" " perror(\"socket\");\n" " return -1;\n" " }\n" "\\&\n" " memset(&addr, 0, sizeof(addr));\n" " addr.sin_port = htons(connect_port);\n" " addr.sin_family = AF_INET;\n" "\\&\n" " if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n" " fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n" " close(cfd);\n" " return -1;\n" " }\n" "\\&\n" " if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" " perror(\"connect()\");\n" " shutdown(cfd, SHUT_RDWR);\n" " close(cfd);\n" " return -1;\n" " }\n" " return cfd;\n" "}\n" "\\&\n" "#define SHUT_FD1 do { \\[rs]\n" " if (fd1 E= 0) { \\[rs]\n" " shutdown(fd1, SHUT_RDWR); \\[rs]\n" " close(fd1); \\[rs]\n" " fd1 = -1; \\[rs]\n" " } \\[rs]\n" " } while (0)\n" "\\&\n" "#define SHUT_FD2 do { \\[rs]\n" " if (fd2 E= 0) { \\[rs]\n" " shutdown(fd2, SHUT_RDWR); \\[rs]\n" " close(fd2); \\[rs]\n" " fd2 = -1; \\[rs]\n" " } \\[rs]\n" " } while (0)\n" "\\&\n" "#define BUF_SIZE 1024\n" "\\&\n" "int\n" "main(int argc, char *argv[])\n" "{\n" " int h;\n" " int ready, nfds;\n" " int fd1 = -1, fd2 = -1;\n" " int buf1_avail = 0, buf1_written = 0;\n" " int buf2_avail = 0, buf2_written = 0;\n" " char buf1[BUF_SIZE], buf2[BUF_SIZE];\n" " fd_set readfds, writefds, exceptfds;\n" " ssize_t nbytes;\n" "\\&\n" " if (argc != 4) {\n" " fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd Elisten-portE \"\n" " \"Eforward-to-portE Eforward-to-ip-addressE\\[rs]n\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " signal(SIGPIPE, SIG_IGN);\n" "\\&\n" " forward_port = atoi(argv[2]);\n" "\\&\n" " h = listen_socket(atoi(argv[1]));\n" " if (h == -1)\n" " exit(EXIT_FAILURE);\n" "\\&\n" " for (;;) {\n" " nfds = 0;\n" "\\&\n" " FD_ZERO(&readfds);\n" " FD_ZERO(&writefds);\n" " FD_ZERO(&exceptfds);\n" " FD_SET(h, &readfds);\n" " nfds = max(nfds, h);\n" "\\&\n" " if (fd1 E 0 && buf1_avail E BUF_SIZE)\n" " FD_SET(fd1, &readfds);\n" " /* Note: nfds is updated below, when fd1 is added to\n" " exceptfds. */\n" " if (fd2 E 0 && buf2_avail E BUF_SIZE)\n" " FD_SET(fd2, &readfds);\n" "\\&\n" " if (fd1 E 0 && buf2_avail - buf2_written E 0)\n" " FD_SET(fd1, &writefds);\n" " if (fd2 E 0 && buf1_avail - buf1_written E 0)\n" " FD_SET(fd2, &writefds);\n" "\\&\n" " if (fd1 E 0) {\n" " FD_SET(fd1, &exceptfds);\n" " nfds = max(nfds, fd1);\n" " }\n" " if (fd2 E 0) {\n" " FD_SET(fd2, &exceptfds);\n" " nfds = max(nfds, fd2);\n" " }\n" "\\&\n" " ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n" "\\&\n" " if (ready == -1 && errno == EINTR)\n" " continue;\n" "\\&\n" " if (ready == -1) {\n" " perror(\"select()\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " if (FD_ISSET(h, &readfds)) {\n" " socklen_t addrlen;\n" " struct sockaddr_in client_addr;\n" " int fd;\n" "\\&\n" " addrlen = sizeof(client_addr);\n" " memset(&client_addr, 0, addrlen);\n" " fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n" " if (fd == -1) {\n" " perror(\"accept()\");\n" " } else {\n" " SHUT_FD1;\n" " SHUT_FD2;\n" " buf1_avail = buf1_written = 0;\n" " buf2_avail = buf2_written = 0;\n" " fd1 = fd;\n" " fd2 = connect_socket(forward_port, argv[3]);\n" " if (fd2 == -1)\n" " SHUT_FD1;\n" " else\n" " printf(\"connect from %s\\[rs]n\",\n" " inet_ntoa(client_addr.sin_addr));\n" "\\&\n" " /* Skip any events on the old, closed file\n" " descriptors. */\n" "\\&\n" " continue;\n" " }\n" " }\n" "\\&\n" " /* NB: read OOB data before normal reads. */\n" "\\&\n" " if (fd1 E 0 && FD_ISSET(fd1, &exceptfds)) {\n" " char c;\n" "\\&\n" " nbytes = recv(fd1, &c, 1, MSG_OOB);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " send(fd2, &c, 1, MSG_OOB);\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &exceptfds)) {\n" " char c;\n" "\\&\n" " nbytes = recv(fd2, &c, 1, MSG_OOB);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " send(fd1, &c, 1, MSG_OOB);\n" " }\n" " if (fd1 E 0 && FD_ISSET(fd1, &readfds)) {\n" " nbytes = read(fd1, buf1 + buf1_avail,\n" " BUF_SIZE - buf1_avail);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " buf1_avail += nbytes;\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &readfds)) {\n" " nbytes = read(fd2, buf2 + buf2_avail,\n" " BUF_SIZE - buf2_avail);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " buf2_avail += nbytes;\n" " }\n" " if (fd1 E 0 && FD_ISSET(fd1, &writefds) && buf2_avail E 0) {\n" " nbytes = write(fd1, buf2 + buf2_written,\n" " buf2_avail - buf2_written);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " buf2_written += nbytes;\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &writefds) && buf1_avail E 0) {\n" " nbytes = write(fd2, buf1 + buf1_written,\n" " buf1_avail - buf1_written);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " buf1_written += nbytes;\n" " }\n" "\\&\n" " /* Check if write data has caught read data. */\n" "\\&\n" " if (buf1_written == buf1_avail)\n" " buf1_written = buf1_avail = 0;\n" " if (buf2_written == buf2_avail)\n" " buf2_written = buf2_avail = 0;\n" "\\&\n" " /* One side has closed the connection, keep\n" " writing to the other side until empty. */\n" "\\&\n" " if (fd1 E 0 && buf1_avail - buf1_written == 0)\n" " SHUT_FD2;\n" " if (fd2 E 0 && buf2_avail - buf2_written == 0)\n" " SHUT_FD1;\n" " }\n" " exit(EXIT_SUCCESS);\n" "}\n" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The above program properly forwards most kinds of TCP connections including " "OOB signal data transmitted by B servers. It handles the tricky " "problem of having data flow in both directions simultaneously. You might " "think it more efficient to use a B(2) call and devote a thread to " "each stream. This becomes more tricky than you might suspect. Another idea " "is to set nonblocking I/O using B(2). This also has its problems " "because you end up using inefficient timeouts." msgstr "" "يقوم البرنامج أعلاه بتوجيه معظم أنواع اتصالات TCP بشكل صحيح بما في ذلك " "بيانات الإشارة خارج النطاق (OOB) التي ترسلها خوادم B. يعالج المشكلة " "الصعبة المتمثلة في تدفق البيانات في كلا الاتجاهين في وقت واحد. قد تظن أنه من " "الأكثر كفاءة استخدام استدعاء B(2) وتخصيص خيط لكل تيار. يصبح هذا أكثر " "تعقيدًا مما قد تظن. فكرة أخرى هي ضبط الإدخال/الإخراج غير المحظور باستخدام " "B(2). هذا أيضًا له مشاكله لأنك ينتهي بك الأمر باستخدام مهلات غير فعالة." #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The program does not handle more than one simultaneous connection at a time, " "although it could easily be extended to do this with a linked list of " "buffers\\[em]one for each connection. At the moment, new connections cause " "the current connection to be dropped." msgstr "" "لا يعالج البرنامج أكثر من اتصال واحد في وقت واحد، على الرغم من أنه يمكن " "توسيعه بسهولة للقيام بذلك باستخدام قائمة مرتبطة من المخازن المؤقتة\\[em]واحد " "لكل اتصال. في الوقت الحالي، تتسبب الاتصالات الجديدة في قطع الاتصال الحالي." #. type: SH #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "SEE ALSO" msgstr "انظر أيضًا" #. type: Plain text #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed msgid "" "B(2), B(2), B(2), B(2), B(2), " "B(2), B(2), B(2), B(2), B(7)" #. type: TH #: debian-trixie #, no-wrap msgid "2024-06-15" msgstr "15 يونيو 2024" #. type: TH #: debian-trixie #, no-wrap msgid "Linux man-pages 6.9.1" msgstr "صفحات دليل لينكس 6.9.1" #. type: Plain text #: debian-trixie msgid "Standard C library (I, I<-lc>)" msgstr "مكتبة سي المعيارية (I، I<-lc>)" #. type: Plain text #: debian-trixie fedora-44 fedora-rawhide #, no-wrap msgid "" "static volatile sig_atomic_t got_SIGCHLD = 0;\n" "\\&\n" "static void\n" "child_sig_handler(int sig)\n" "{\n" " got_SIGCHLD = 1;\n" "}\n" "\\&\n" "int\n" "main(int argc, char *argv[])\n" "{\n" " sigset_t sigmask, empty_mask;\n" " struct sigaction sa;\n" " fd_set readfds, writefds, exceptfds;\n" " int r;\n" "\\&\n" " sigemptyset(&sigmask);\n" " sigaddset(&sigmask, SIGCHLD);\n" " if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n" " perror(\"sigprocmask\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " sa.sa_flags = 0;\n" " sa.sa_handler = child_sig_handler;\n" " sigemptyset(&sa.sa_mask);\n" " if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n" " perror(\"sigaction\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " sigemptyset(&empty_mask);\n" "\\&\n" " for (;;) { /* main loop */\n" " /* Initialize readfds, writefds, and exceptfds\n" " before the pselect() call. (Code omitted.) */\n" "\\&\n" " r = pselect(nfds, &readfds, &writefds, &exceptfds,\n" " NULL, &empty_mask);\n" " if (r == -1 && errno != EINTR) {\n" " /* Handle error */\n" " }\n" "\\&\n" " if (got_SIGCHLD) {\n" " got_SIGCHLD = 0;\n" "\\&\n" " /* Handle signalled event here; e.g., wait() for all\n" " terminated children. (Code omitted.) */\n" " }\n" "\\&\n" " /* main body of program */\n" " }\n" "}\n" msgstr "" "static volatile sig_atomic_t got_SIGCHLD = 0;\n" "\\&\n" "static void\n" "child_sig_handler(int sig)\n" "{\n" " got_SIGCHLD = 1;\n" "}\n" "\\&\n" "int\n" "main(int argc, char *argv[])\n" "{\n" " sigset_t sigmask, empty_mask;\n" " struct sigaction sa;\n" " fd_set readfds, writefds, exceptfds;\n" " int r;\n" "\\&\n" " sigemptyset(&sigmask);\n" " sigaddset(&sigmask, SIGCHLD);\n" " if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n" " perror(\"sigprocmask\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " sa.sa_flags = 0;\n" " sa.sa_handler = child_sig_handler;\n" " sigemptyset(&sa.sa_mask);\n" " if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n" " perror(\"sigaction\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " sigemptyset(&empty_mask);\n" "\\&\n" " for (;;) { /* الحلقة الرئيسية */\n" " /* تهيئة readfds و writefds و exceptfds\n" " قبل استدعاء pselect(). (تم حذف الكود.) */\n" "\\&\n" " r = pselect(nfds, &readfds, &writefds, &exceptfds,\n" " NULL, &empty_mask);\n" " if (r == -1 && errno != EINTR) {\n" " /* معالجة الخطأ */\n" " }\n" "\\&\n" " if (got_SIGCHLD) {\n" " got_SIGCHLD = 0;\n" "\\&\n" " /* معالجة الحدث المُشار إليه هنا؛ على سبيل المثال، wait() لجميع\n" " العمليات الفرعية المنتهية. (تم حذف الكود.) */\n" " }\n" "\\&\n" " /* الجزء الرئيسي من البرنامج */\n" " }\n" "}\n" #. type: Plain text #: debian-trixie fedora-44 fedora-rawhide #, no-wrap msgid "" "#include Earpa/inet.hE\n" "#include Eerrno.hE\n" "#include Enetinet/in.hE\n" "#include Esignal.hE\n" "#include Estdio.hE\n" "#include Estdlib.hE\n" "#include Estring.hE\n" "#include Esys/select.hE\n" "#include Esys/socket.hE\n" "#include Esys/types.hE\n" "#include Eunistd.hE\n" "\\&\n" "static int forward_port;\n" "\\&\n" "#undef max\n" "#define max(x, y) ((x) E (y) ? (x) : (y))\n" "\\&\n" "static int\n" "listen_socket(int listen_port)\n" "{\n" " int lfd;\n" " int yes;\n" " struct sockaddr_in addr;\n" "\\&\n" " lfd = socket(AF_INET, SOCK_STREAM, 0);\n" " if (lfd == -1) {\n" " perror(\"socket\");\n" " return -1;\n" " }\n" "\\&\n" " yes = 1;\n" " if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n" " &yes, sizeof(yes)) == -1)\n" " {\n" " perror(\"setsockopt\");\n" " close(lfd);\n" " return -1;\n" " }\n" "\\&\n" " memset(&addr, 0, sizeof(addr));\n" " addr.sin_port = htons(listen_port);\n" " addr.sin_family = AF_INET;\n" " if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" " perror(\"bind\");\n" " close(lfd);\n" " return -1;\n" " }\n" "\\&\n" " printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n" " listen(lfd, 10);\n" " return lfd;\n" "}\n" "\\&\n" "static int\n" "connect_socket(int connect_port, char *address)\n" "{\n" " int cfd;\n" " struct sockaddr_in addr;\n" "\\&\n" " cfd = socket(AF_INET, SOCK_STREAM, 0);\n" " if (cfd == -1) {\n" " perror(\"socket\");\n" " return -1;\n" " }\n" "\\&\n" " memset(&addr, 0, sizeof(addr));\n" " addr.sin_port = htons(connect_port);\n" " addr.sin_family = AF_INET;\n" "\\&\n" " if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n" " fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n" " close(cfd);\n" " return -1;\n" " }\n" "\\&\n" " if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" " perror(\"connect()\");\n" " shutdown(cfd, SHUT_RDWR);\n" " close(cfd);\n" " return -1;\n" " }\n" " return cfd;\n" "}\n" "\\&\n" "#define SHUT_FD1 do { \\[rs]\n" " if (fd1 E= 0) { \\[rs]\n" " shutdown(fd1, SHUT_RDWR); \\[rs]\n" " close(fd1); \\[rs]\n" " fd1 = -1; \\[rs]\n" " } \\[rs]\n" " } while (0)\n" "\\&\n" "#define SHUT_FD2 do { \\[rs]\n" " if (fd2 E= 0) { \\[rs]\n" " shutdown(fd2, SHUT_RDWR); \\[rs]\n" " close(fd2); \\[rs]\n" " fd2 = -1; \\[rs]\n" " } \\[rs]\n" " } while (0)\n" "\\&\n" "#define BUF_SIZE 1024\n" "\\&\n" "int\n" "main(int argc, char *argv[])\n" "{\n" " int h;\n" " int ready, nfds;\n" " int fd1 = -1, fd2 = -1;\n" " int buf1_avail = 0, buf1_written = 0;\n" " int buf2_avail = 0, buf2_written = 0;\n" " char buf1[BUF_SIZE], buf2[BUF_SIZE];\n" " fd_set readfds, writefds, exceptfds;\n" " ssize_t nbytes;\n" "\\&\n" " if (argc != 4) {\n" " fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd Elisten-portE \"\n" " \"Eforward-to-portE Eforward-to-ip-addressE\\[rs]n\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " signal(SIGPIPE, SIG_IGN);\n" "\\&\n" " forward_port = atoi(argv[2]);\n" "\\&\n" " h = listen_socket(atoi(argv[1]));\n" " if (h == -1)\n" " exit(EXIT_FAILURE);\n" "\\&\n" " for (;;) {\n" " nfds = 0;\n" "\\&\n" " FD_ZERO(&readfds);\n" " FD_ZERO(&writefds);\n" " FD_ZERO(&exceptfds);\n" " FD_SET(h, &readfds);\n" " nfds = max(nfds, h);\n" "\\&\n" " if (fd1 E 0 && buf1_avail E BUF_SIZE)\n" " FD_SET(fd1, &readfds);\n" " /* Note: nfds is updated below, when fd1 is added to\n" " exceptfds. */\n" " if (fd2 E 0 && buf2_avail E BUF_SIZE)\n" " FD_SET(fd2, &readfds);\n" "\\&\n" " if (fd1 E 0 && buf2_avail - buf2_written E 0)\n" " FD_SET(fd1, &writefds);\n" " if (fd2 E 0 && buf1_avail - buf1_written E 0)\n" " FD_SET(fd2, &writefds);\n" "\\&\n" " if (fd1 E 0) {\n" " FD_SET(fd1, &exceptfds);\n" " nfds = max(nfds, fd1);\n" " }\n" " if (fd2 E 0) {\n" " FD_SET(fd2, &exceptfds);\n" " nfds = max(nfds, fd2);\n" " }\n" "\\&\n" " ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n" "\\&\n" " if (ready == -1 && errno == EINTR)\n" " continue;\n" "\\&\n" " if (ready == -1) {\n" " perror(\"select()\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " if (FD_ISSET(h, &readfds)) {\n" " socklen_t addrlen;\n" " struct sockaddr_in client_addr;\n" " int fd;\n" "\\&\n" " addrlen = sizeof(client_addr);\n" " memset(&client_addr, 0, addrlen);\n" " fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n" " if (fd == -1) {\n" " perror(\"accept()\");\n" " } else {\n" " SHUT_FD1;\n" " SHUT_FD2;\n" " buf1_avail = buf1_written = 0;\n" " buf2_avail = buf2_written = 0;\n" " fd1 = fd;\n" " fd2 = connect_socket(forward_port, argv[3]);\n" " if (fd2 == -1)\n" " SHUT_FD1;\n" " else\n" " printf(\"connect from %s\\[rs]n\",\n" " inet_ntoa(client_addr.sin_addr));\n" "\\&\n" " /* Skip any events on the old, closed file\n" " descriptors. */\n" "\\&\n" " continue;\n" " }\n" " }\n" "\\&\n" " /* NB: read OOB data before normal reads. */\n" "\\&\n" " if (fd1 E 0 && FD_ISSET(fd1, &exceptfds)) {\n" " char c;\n" "\\&\n" " nbytes = recv(fd1, &c, 1, MSG_OOB);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " send(fd2, &c, 1, MSG_OOB);\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &exceptfds)) {\n" " char c;\n" "\\&\n" " nbytes = recv(fd2, &c, 1, MSG_OOB);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " send(fd1, &c, 1, MSG_OOB);\n" " }\n" " if (fd1 E 0 && FD_ISSET(fd1, &readfds)) {\n" " nbytes = read(fd1, buf1 + buf1_avail,\n" " BUF_SIZE - buf1_avail);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " buf1_avail += nbytes;\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &readfds)) {\n" " nbytes = read(fd2, buf2 + buf2_avail,\n" " BUF_SIZE - buf2_avail);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " buf2_avail += nbytes;\n" " }\n" " if (fd1 E 0 && FD_ISSET(fd1, &writefds) && buf2_avail E 0) {\n" " nbytes = write(fd1, buf2 + buf2_written,\n" " buf2_avail - buf2_written);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " buf2_written += nbytes;\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &writefds) && buf1_avail E 0) {\n" " nbytes = write(fd2, buf1 + buf1_written,\n" " buf1_avail - buf1_written);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " buf1_written += nbytes;\n" " }\n" "\\&\n" " /* Check if write data has caught read data. */\n" "\\&\n" " if (buf1_written == buf1_avail)\n" " buf1_written = buf1_avail = 0;\n" " if (buf2_written == buf2_avail)\n" " buf2_written = buf2_avail = 0;\n" "\\&\n" " /* One side has closed the connection, keep\n" " writing to the other side until empty. */\n" "\\&\n" " if (fd1 E 0 && buf1_avail - buf1_written == 0)\n" " SHUT_FD2;\n" " if (fd2 E 0 && buf2_avail - buf2_written == 0)\n" " SHUT_FD1;\n" " }\n" " exit(EXIT_SUCCESS);\n" "}\n" msgstr "" "#include Earpa/inet.hE\n" "#include Eerrno.hE\n" "#include Enetinet/in.hE\n" "#include Esignal.hE\n" "#include Estdio.hE\n" "#include Estdlib.hE\n" "#include Estring.hE\n" "#include Esys/select.hE\n" "#include Esys/socket.hE\n" "#include Esys/types.hE\n" "#include Eunistd.hE\n" "\\&\n" "static int forward_port;\n" "\\&\n" "#undef max\n" "#define max(x, y) ((x) E (y) ? (x) : (y))\n" "\\&\n" "static int\n" "listen_socket(int listen_port)\n" "{\n" " int lfd;\n" " int yes;\n" " struct sockaddr_in addr;\n" "\\&\n" " lfd = socket(AF_INET, SOCK_STREAM, 0);\n" " if (lfd == -1) {\n" " perror(\"socket\");\n" " return -1;\n" " }\n" "\\&\n" " yes = 1;\n" " if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n" " &yes, sizeof(yes)) == -1)\n" " {\n" " perror(\"setsockopt\");\n" " close(lfd);\n" " return -1;\n" " }\n" "\\&\n" " memset(&addr, 0, sizeof(addr));\n" " addr.sin_port = htons(listen_port);\n" " addr.sin_family = AF_INET;\n" " if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" " perror(\"bind\");\n" " close(lfd);\n" " return -1;\n" " }\n" "\\&\n" " printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n" " listen(lfd, 10);\n" " return lfd;\n" "}\n" "\\&\n" "static int\n" "connect_socket(int connect_port, char *address)\n" "{\n" " int cfd;\n" " struct sockaddr_in addr;\n" "\\&\n" " cfd = socket(AF_INET, SOCK_STREAM, 0);\n" " if (cfd == -1) {\n" " perror(\"socket\");\n" " return -1;\n" " }\n" "\\&\n" " memset(&addr, 0, sizeof(addr));\n" " addr.sin_port = htons(connect_port);\n" " addr.sin_family = AF_INET;\n" "\\&\n" " if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n" " fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n" " close(cfd);\n" " return -1;\n" " }\n" "\\&\n" " if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" " perror(\"connect()\");\n" " shutdown(cfd, SHUT_RDWR);\n" " close(cfd);\n" " return -1;\n" " }\n" " return cfd;\n" "}\n" "\\&\n" "#define SHUT_FD1 do { \\[rs]\n" " if (fd1 E= 0) { \\[rs]\n" " shutdown(fd1, SHUT_RDWR); \\[rs]\n" " close(fd1); \\[rs]\n" " fd1 = -1; \\[rs]\n" " } \\[rs]\n" " } while (0)\n" "\\&\n" "#define SHUT_FD2 do { \\[rs]\n" " if (fd2 E= 0) { \\[rs]\n" " shutdown(fd2, SHUT_RDWR); \\[rs]\n" " close(fd2); \\[rs]\n" " fd2 = -1; \\[rs]\n" " } \\[rs]\n" " } while (0)\n" "\\&\n" "#define BUF_SIZE 1024\n" "\\&\n" "int\n" "main(int argc, char *argv[])\n" "{\n" " int h;\n" " int ready, nfds;\n" " int fd1 = -1, fd2 = -1;\n" " int buf1_avail = 0, buf1_written = 0;\n" " int buf2_avail = 0, buf2_written = 0;\n" " char buf1[BUF_SIZE], buf2[BUF_SIZE];\n" " fd_set readfds, writefds, exceptfds;\n" " ssize_t nbytes;\n" "\\&\n" " if (argc != 4) {\n" " fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd Elisten-portE \"\n" " \"Eforward-to-portE Eforward-to-ip-addressE\\[rs]n\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " signal(SIGPIPE, SIG_IGN);\n" "\\&\n" " forward_port = atoi(argv[2]);\n" "\\&\n" " h = listen_socket(atoi(argv[1]));\n" " if (h == -1)\n" " exit(EXIT_FAILURE);\n" "\\&\n" " for (;;) {\n" " nfds = 0;\n" "\\&\n" " FD_ZERO(&readfds);\n" " FD_ZERO(&writefds);\n" " FD_ZERO(&exceptfds);\n" " FD_SET(h, &readfds);\n" " nfds = max(nfds, h);\n" "\\&\n" " if (fd1 E 0 && buf1_avail E BUF_SIZE)\n" " FD_SET(fd1, &readfds);\n" " /* Note: nfds is updated below, when fd1 is added to\n" " exceptfds. */\n" " if (fd2 E 0 && buf2_avail E BUF_SIZE)\n" " FD_SET(fd2, &readfds);\n" "\\&\n" " if (fd1 E 0 && buf2_avail - buf2_written E 0)\n" " FD_SET(fd1, &writefds);\n" " if (fd2 E 0 && buf1_avail - buf1_written E 0)\n" " FD_SET(fd2, &writefds);\n" "\\&\n" " if (fd1 E 0) {\n" " FD_SET(fd1, &exceptfds);\n" " nfds = max(nfds, fd1);\n" " }\n" " if (fd2 E 0) {\n" " FD_SET(fd2, &exceptfds);\n" " nfds = max(nfds, fd2);\n" " }\n" "\\&\n" " ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n" "\\&\n" " if (ready == -1 && errno == EINTR)\n" " continue;\n" "\\&\n" " if (ready == -1) {\n" " perror(\"select()\");\n" " exit(EXIT_FAILURE);\n" " }\n" "\\&\n" " if (FD_ISSET(h, &readfds)) {\n" " socklen_t addrlen;\n" " struct sockaddr_in client_addr;\n" " int fd;\n" "\\&\n" " addrlen = sizeof(client_addr);\n" " memset(&client_addr, 0, addrlen);\n" " fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n" " if (fd == -1) {\n" " perror(\"accept()\");\n" " } else {\n" " SHUT_FD1;\n" " SHUT_FD2;\n" " buf1_avail = buf1_written = 0;\n" " buf2_avail = buf2_written = 0;\n" " fd1 = fd;\n" " fd2 = connect_socket(forward_port, argv[3]);\n" " if (fd2 == -1)\n" " SHUT_FD1;\n" " else\n" " printf(\"connect from %s\\[rs]n\",\n" " inet_ntoa(client_addr.sin_addr));\n" "\\&\n" " /* Skip any events on the old, closed file\n" " descriptors. */\n" "\\&\n" " continue;\n" " }\n" " }\n" "\\&\n" " /* NB: read OOB data before normal reads. */\n" "\\&\n" " if (fd1 E 0 && FD_ISSET(fd1, &exceptfds)) {\n" " char c;\n" "\\&\n" " nbytes = recv(fd1, &c, 1, MSG_OOB);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " send(fd2, &c, 1, MSG_OOB);\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &exceptfds)) {\n" " char c;\n" "\\&\n" " nbytes = recv(fd2, &c, 1, MSG_OOB);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " send(fd1, &c, 1, MSG_OOB);\n" " }\n" " if (fd1 E 0 && FD_ISSET(fd1, &readfds)) {\n" " nbytes = read(fd1, buf1 + buf1_avail,\n" " BUF_SIZE - buf1_avail);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " buf1_avail += nbytes;\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &readfds)) {\n" " nbytes = read(fd2, buf2 + buf2_avail,\n" " BUF_SIZE - buf2_avail);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " buf2_avail += nbytes;\n" " }\n" " if (fd1 E 0 && FD_ISSET(fd1, &writefds) && buf2_avail E 0) {\n" " nbytes = write(fd1, buf2 + buf2_written,\n" " buf2_avail - buf2_written);\n" " if (nbytes E 1)\n" " SHUT_FD1;\n" " else\n" " buf2_written += nbytes;\n" " }\n" " if (fd2 E 0 && FD_ISSET(fd2, &writefds) && buf1_avail E 0) {\n" " nbytes = write(fd2, buf1 + buf1_written,\n" " buf1_avail - buf1_written);\n" " if (nbytes E 1)\n" " SHUT_FD2;\n" " else\n" " buf1_written += nbytes;\n" " }\n" "\\&\n" " /* Check if write data has caught read data. */\n" "\\&\n" " if (buf1_written == buf1_avail)\n" " buf1_written = buf1_avail = 0;\n" " if (buf2_written == buf2_avail)\n" " buf2_written = buf2_avail = 0;\n" "\\&\n" " /* One side has closed the connection, keep\n" " writing to the other side until empty. */\n" "\\&\n" " if (fd1 E 0 && buf1_avail - buf1_written == 0)\n" " SHUT_FD2;\n" " if (fd2 E 0 && buf2_avail - buf2_written == 0)\n" " SHUT_FD1;\n" " }\n" " exit(EXIT_SUCCESS);\n" "}\n" #. type: TH #: fedora-44 fedora-rawhide #, no-wrap msgid "2024-07-23" msgstr "23 يوليو 2024" #. type: TH #: fedora-44 fedora-rawhide #, no-wrap msgid "Linux man-pages 6.13" msgstr "صفحات دليل لينكس 6.13" #. type: TH #: mageia-cauldron #, no-wrap msgid "Linux man-pages 6.17" msgstr "صفحات دليل لينكس 6.17" #. type: TH #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Linux man-pages (unreleased)" msgstr "صفحات دليل لينكس (لم تصدر بعد)"